1
1
/*
2
2
*
3
- * Copyright (c) 2022 Project CHIP Authors
3
+ * Copyright (c) 2022-2023 Project CHIP Authors
4
4
* All rights reserved.
5
5
*
6
6
* Licensed under the Apache License, Version 2.0 (the "License");
22
22
#include " AppEvent.h"
23
23
#include " ButtonManager.h"
24
24
#include " binding-handler.h"
25
- #include < app/server/OnboardingCodesUtil.h>
26
- #include < app/server/Server.h>
27
-
28
- #include < DeviceInfoProviderImpl.h>
29
25
30
26
#include " ThreadUtil.h"
31
27
28
+ #include < DeviceInfoProviderImpl.h>
32
29
#include < app/clusters/identify-server/identify-server.h>
30
+ #include < app/server/OnboardingCodesUtil.h>
31
+ #include < app/server/Server.h>
33
32
#include < app/util/attribute-storage.h>
34
-
35
33
#include < credentials/DeviceAttestationCredsProvider.h>
36
34
#include < credentials/examples/DeviceAttestationCredsExample.h>
37
35
@@ -67,7 +65,8 @@ using namespace ::chip::Credentials;
67
65
using namespace ::chip::DeviceLayer;
68
66
69
67
namespace {
70
- constexpr int kFactoryResetTriggerTimeout = 2000 ;
68
+ constexpr int kFactoryResetCalcTimeout = 3000 ;
69
+ constexpr int kFactoryResetTriggerCntr = 3 ;
71
70
constexpr int kAppEventQueueSize = 10 ;
72
71
constexpr uint8_t kButtonPushEvent = 1 ;
73
72
constexpr uint8_t kButtonReleaseEvent = 0 ;
@@ -84,20 +83,28 @@ constexpr uint32_t kIdentifyBreatheRateMs = 1000;
84
83
85
84
const struct pwm_dt_spec sPwmIdentifySpecGreenLed = LIGHTING_PWM_SPEC_IDENTIFY_GREEN;
86
85
86
+ #if CONFIG_CHIP_FACTORY_DATA
87
+ // NOTE! This key is for test/certification only and should not be available in production devices!
88
+ uint8_t sTestEventTriggerEnableKey [TestEventTriggerDelegate::kEnableKeyLength ] = { 0x00 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 ,
89
+ 0x88 , 0x99 , 0xaa , 0xbb , 0xcc , 0xdd , 0xee , 0xff };
90
+ #endif
91
+
87
92
K_MSGQ_DEFINE (sAppEventQueue , sizeof (AppEvent), kAppEventQueueSize , alignof(AppEvent));
88
93
k_timer sFactoryResetTimer ;
94
+ uint8_t sFactoryResetCntr = 0 ;
89
95
96
+ #if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
90
97
LEDWidget sStatusLED ;
98
+ #endif
91
99
92
100
Button sFactoryResetButton ;
93
101
Button sThreadStartButton ;
94
102
Button sBleAdvStartButton ;
95
103
96
- bool sIsThreadProvisioned = false ;
97
- bool sIsThreadEnabled = false ;
98
- bool sIsThreadAttached = false ;
99
- bool sHaveBLEConnections = false ;
100
- bool sIsFactoryResetTimerActive = false ;
104
+ bool sIsThreadProvisioned = false ;
105
+ bool sIsThreadEnabled = false ;
106
+ bool sIsThreadAttached = false ;
107
+ bool sHaveBLEConnections = false ;
101
108
102
109
chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider ;
103
110
@@ -135,24 +142,21 @@ CHIP_ERROR AppTask::Init(void)
135
142
{
136
143
LOG_INF (" SW Version: %u, %s" , CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
137
144
138
- // Initialize status LED
145
+ // Initialize LEDs
146
+ #if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
139
147
LEDWidget::InitGpio (LEDS_PORT);
140
148
LEDWidget::SetStateUpdateCallback (LEDStateUpdateHandler);
141
149
sStatusLED .Init (SYSTEM_STATE_LED);
142
150
143
151
UpdateStatusLED ();
152
+ #endif
144
153
145
154
InitButtons ();
146
155
147
156
// Initialize function button timer
148
157
k_timer_init (&sFactoryResetTimer , &AppTask::FactoryResetTimerTimeoutCallback, nullptr );
149
158
k_timer_user_data_set (&sFactoryResetTimer , this );
150
159
151
- // Init ZCL Data Model and start server
152
- static chip::CommonCaseDeviceServerInitParams initParams;
153
- (void ) initParams.InitializeStaticResourcesBeforeServerInit ();
154
- chip::Server::GetInstance ().Init (initParams);
155
-
156
160
// Initialize PWM Identify led
157
161
CHIP_ERROR err = sAppTask .mPwmIdentifyLed .Init (&sPwmIdentifySpecGreenLed , kDefaultMinLevel , kDefaultMaxLevel , kDefaultMaxLevel );
158
162
if (err != CHIP_NO_ERROR)
@@ -163,10 +167,28 @@ CHIP_ERROR AppTask::Init(void)
163
167
164
168
sAppTask .mPwmIdentifyLed .SetCallbacks (nullptr , nullptr , ActionIdentifyStateUpdateHandler);
165
169
166
- // Initialize device attestation config
170
+ // Initialize CHIP server
171
+ #if CONFIG_CHIP_FACTORY_DATA
172
+ ReturnErrorOnFailure (mFactoryDataProvider .Init ());
173
+ SetDeviceInstanceInfoProvider (&mFactoryDataProvider );
174
+ SetDeviceAttestationCredentialsProvider (&mFactoryDataProvider );
175
+ SetCommissionableDataProvider (&mFactoryDataProvider );
176
+ // Read EnableKey from the factory data.
177
+ MutableByteSpan enableKey (sTestEventTriggerEnableKey );
178
+ err = mFactoryDataProvider .GetEnableKey (enableKey);
179
+ if (err != CHIP_NO_ERROR)
180
+ {
181
+ LOG_ERR (" mFactoryDataProvider.GetEnableKey() failed. Could not delegate a test event trigger" );
182
+ memset (sTestEventTriggerEnableKey , 0 , sizeof (sTestEventTriggerEnableKey ));
183
+ }
184
+ #else
167
185
SetDeviceAttestationCredentialsProvider (Examples::GetExampleDACProvider ());
186
+ #endif
168
187
169
- gExampleDeviceInfoProvider .SetStorageDelegate (&chip::Server::GetInstance ().GetPersistentStorage ());
188
+ static CommonCaseDeviceServerInitParams initParams;
189
+ (void ) initParams.InitializeStaticResourcesBeforeServerInit ();
190
+ ReturnErrorOnFailure (chip::Server::GetInstance ().Init (initParams));
191
+ gExampleDeviceInfoProvider .SetStorageDelegate (&Server::GetInstance ().GetPersistentStorage ());
170
192
chip::DeviceLayer::SetDeviceInfoProvider (&gExampleDeviceInfoProvider );
171
193
172
194
#if CONFIG_CHIP_OTA_REQUESTOR
@@ -226,13 +248,8 @@ CHIP_ERROR AppTask::StartApp(void)
226
248
227
249
while (true )
228
250
{
229
- int ret = k_msgq_get (&sAppEventQueue , &event, K_MSEC (10 ));
230
-
231
- while (!ret)
232
- {
233
- DispatchEvent (&event);
234
- ret = k_msgq_get (&sAppEventQueue , &event, K_NO_WAIT);
235
- }
251
+ k_msgq_get (&sAppEventQueue , &event, K_FOREVER);
252
+ DispatchEvent (&event);
236
253
}
237
254
}
238
255
@@ -298,15 +315,20 @@ void AppTask::FactoryResetButtonEventHandler(void)
298
315
299
316
void AppTask::FactoryResetHandler (AppEvent * aEvent)
300
317
{
301
- if (! sIsFactoryResetTimerActive )
318
+ if (sFactoryResetCntr == 0 )
302
319
{
303
- k_timer_start (&sFactoryResetTimer , K_MSEC (kFactoryResetTriggerTimeout ), K_NO_WAIT);
304
- sIsFactoryResetTimerActive = true ;
320
+ k_timer_start (&sFactoryResetTimer , K_MSEC (kFactoryResetCalcTimeout ), K_NO_WAIT);
305
321
}
306
- else
322
+
323
+ sFactoryResetCntr ++;
324
+ LOG_INF (" Factory Reset Trigger Counter: %d/%d" , sFactoryResetCntr , kFactoryResetTriggerCntr );
325
+
326
+ if (sFactoryResetCntr == kFactoryResetTriggerCntr )
307
327
{
308
328
k_timer_stop (&sFactoryResetTimer );
309
- sIsFactoryResetTimerActive = false ;
329
+ sFactoryResetCntr = 0 ;
330
+
331
+ chip::Server::GetInstance ().ScheduleFactoryReset ();
310
332
}
311
333
}
312
334
@@ -369,6 +391,7 @@ void AppTask::StartBleAdvHandler(AppEvent * aEvent)
369
391
}
370
392
}
371
393
394
+ #if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
372
395
void AppTask::UpdateLedStateEventHandler (AppEvent * aEvent)
373
396
{
374
397
if (aEvent->Type == AppEvent::kEventType_UpdateLedState )
@@ -404,20 +427,25 @@ void AppTask::UpdateStatusLED(void)
404
427
sStatusLED .Blink (50 , 950 );
405
428
}
406
429
}
430
+ #endif
407
431
408
432
void AppTask::ChipEventHandler (const ChipDeviceEvent * event, intptr_t /* arg */ )
409
433
{
410
434
switch (event->Type )
411
435
{
412
436
case DeviceEventType::kCHIPoBLEAdvertisingChange :
413
437
sHaveBLEConnections = ConnectivityMgr ().NumBLEConnections () != 0 ;
438
+ #if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
414
439
UpdateStatusLED ();
440
+ #endif
415
441
break ;
416
442
case DeviceEventType::kThreadStateChange :
417
443
sIsThreadProvisioned = ConnectivityMgr ().IsThreadProvisioned ();
418
444
sIsThreadEnabled = ConnectivityMgr ().IsThreadEnabled ();
419
445
sIsThreadAttached = ConnectivityMgr ().IsThreadAttached ();
446
+ #if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
420
447
UpdateStatusLED ();
448
+ #endif
421
449
break ;
422
450
case DeviceEventType::kThreadConnectivityChange :
423
451
#if CONFIG_CHIP_OTA_REQUESTOR
@@ -485,16 +513,21 @@ void AppTask::FactoryResetTimerEventHandler(AppEvent * aEvent)
485
513
return ;
486
514
}
487
515
488
- sIsFactoryResetTimerActive = false ;
489
- LOG_INF (" FactoryResetHandler" );
490
- chip::Server::GetInstance ().ScheduleFactoryReset ();
516
+ sFactoryResetCntr = 0 ;
517
+ LOG_INF (" Factory Reset Trigger Counter is cleared" );
491
518
}
492
519
493
520
void AppTask::InitButtons (void )
494
521
{
495
- sFactoryResetButton .Configure (BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, true , FactoryResetButtonEventHandler);
496
- sThreadStartButton .Configure (BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, false , StartThreadButtonEventHandler);
497
- sBleAdvStartButton .Configure (BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, false , StartBleAdvButtonEventHandler);
522
+ #if CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE
523
+ sFactoryResetButton .Configure (BUTTON_PORT, BUTTON_PIN_1, FactoryResetButtonEventHandler);
524
+ sThreadStartButton .Configure (BUTTON_PORT, BUTTON_PIN_3, StartThreadButtonEventHandler);
525
+ sBleAdvStartButton .Configure (BUTTON_PORT, BUTTON_PIN_4, StartBleAdvButtonEventHandler);
526
+ #else
527
+ sFactoryResetButton .Configure (BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler);
528
+ sThreadStartButton .Configure (BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_2, StartThreadButtonEventHandler);
529
+ sBleAdvStartButton .Configure (BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler);
530
+ #endif
498
531
499
532
ButtonManagerInst ().AddButton (sFactoryResetButton );
500
533
ButtonManagerInst ().AddButton (sThreadStartButton );
0 commit comments