45
45
#endif
46
46
#include < app/server/OnboardingCodesUtil.h>
47
47
48
+ #include < app/TestEventTriggerDelegate.h>
49
+ #include < app/clusters/general-diagnostics-server/GenericFaultTestEventTriggerHandler.h>
50
+ #include < src/platform/cc13xx_26xx/DefaultTestEventTriggerDelegate.h>
51
+
48
52
#include < ti/drivers/apps/Button.h>
49
53
#include < ti/drivers/apps/LED.h>
50
54
56
60
#define APP_EVENT_QUEUE_SIZE 10
57
61
#define BUTTON_ENABLE 1
58
62
63
+ #define OTAREQUESTOR_INIT_TIMER_DELAY_MS 10000
64
+
59
65
using namespace ::chip;
60
66
using namespace ::chip::Credentials;
61
67
using namespace ::chip::DeviceLayer;
@@ -70,6 +76,12 @@ AppTask AppTask::sAppTask;
70
76
71
77
constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE ;
72
78
79
+ void StartTimer (uint32_t aTimeoutMs);
80
+ void CancelTimer (void );
81
+
82
+ uint8_t sTestEventTriggerEnableKey [TestEventTriggerDelegate::kEnableKeyLength ] = { 0x00 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 ,
83
+ 0x88 , 0x99 , 0xaa , 0xbb , 0xcc , 0xdd , 0xee , 0xff };
84
+
73
85
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
74
86
static DefaultOTARequestor sRequestorCore ;
75
87
static DefaultOTARequestorStorage sRequestorStorage ;
@@ -90,6 +102,15 @@ void InitializeOTARequestor(void)
90
102
}
91
103
#endif
92
104
105
+ TimerHandle_t sOTAInitTimer = 0 ;
106
+
107
+ // The OTA Init Timer is only started upon the first Thread State Change
108
+ // detected if the device is already on a Thread Network, or during the AppTask
109
+ // Init sequence if the device is not yet on a Thread Network. Once the timer
110
+ // has been started once, it does not need to be started again so the flag will
111
+ // be set to false.
112
+ bool isAppStarting = true ;
113
+
93
114
#ifdef AUTO_PRINT_METRICS
94
115
static void printMetrics (void )
95
116
{
@@ -132,13 +153,31 @@ void DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
132
153
case DeviceEventType::kCommissioningComplete :
133
154
PLAT_LOG (" Commissioning complete" );
134
155
break ;
156
+ case DeviceEventType::kThreadStateChange :
157
+ PLAT_LOG (" Thread State Change" );
158
+ bool isThreadAttached = ThreadStackMgrImpl ().IsThreadAttached ();
159
+
160
+ if (isThreadAttached){
161
+ PLAT_LOG (" Device is on the Thread Network" );
162
+ #if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
163
+ if (isAppStarting){
164
+ StartTimer (OTAREQUESTOR_INIT_TIMER_DELAY_MS);
165
+ isAppStarting = false ;
166
+ }
167
+ #endif
168
+ }
169
+ break ;
135
170
}
136
171
137
172
#ifdef AUTO_PRINT_METRICS
138
173
printMetrics ();
139
174
#endif
140
175
}
141
176
177
+ void OTAInitTimerEventHandler (TimerHandle_t xTimer){
178
+ InitializeOTARequestor ();
179
+ }
180
+
142
181
int AppTask::StartAppTask ()
143
182
{
144
183
int ret = 0 ;
@@ -171,6 +210,9 @@ int AppTask::Init()
171
210
// Init Chip memory management before the stack
172
211
Platform::MemoryInit ();
173
212
213
+ PLAT_LOG (" Software Version: %d" , CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION);
214
+ PLAT_LOG (" Software Version String: %s" , CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
215
+
174
216
CHIP_ERROR ret = PlatformMgr ().InitChipStack ();
175
217
if (ret != CHIP_NO_ERROR)
176
218
{
@@ -179,6 +221,23 @@ int AppTask::Init()
179
221
;
180
222
}
181
223
224
+ // Create FreeRTOS sw timer for OTA timer.
225
+ sOTAInitTimer = xTimerCreate (" OTAInitTmr" , // Just a text name, not used by the RTOS kernel
226
+ OTAREQUESTOR_INIT_TIMER_DELAY_MS, // timer period (mS)
227
+ false , // no timer reload (==one-shot)
228
+ (void *) this , // init timer id = light obj context
229
+ OTAInitTimerEventHandler // timer callback handler
230
+ );
231
+
232
+ if (sOTAInitTimer == NULL )
233
+ {
234
+ PLAT_LOG (" sOTAInitTimer timer create failed" );
235
+ }
236
+ else
237
+ {
238
+ PLAT_LOG (" sOTAInitTimer timer created successfully " );
239
+ }
240
+
182
241
ret = ThreadStackMgr ().InitThreadStack ();
183
242
if (ret != CHIP_NO_ERROR)
184
243
{
@@ -202,14 +261,6 @@ int AppTask::Init()
202
261
;
203
262
}
204
263
205
- ret = PlatformMgr ().StartEventLoopTask ();
206
- if (ret != CHIP_NO_ERROR)
207
- {
208
- PLAT_LOG (" PlatformMgr().StartEventLoopTask() failed" );
209
- while (1 )
210
- ;
211
- }
212
-
213
264
ret = ThreadStackMgrImpl ().StartThreadTask ();
214
265
if (ret != CHIP_NO_ERROR)
215
266
{
@@ -234,6 +285,9 @@ int AppTask::Init()
234
285
// Init ZCL Data Model and start server
235
286
PLAT_LOG (" Initialize Server" );
236
287
static chip::CommonCaseDeviceServerInitParams initParams;
288
+ static DefaultTestEventTriggerDelegate sTestEventTriggerDelegate { ByteSpan (sTestEventTriggerEnableKey ) };
289
+ initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate ;
290
+
237
291
(void ) initParams.InitializeStaticResourcesBeforeServerInit ();
238
292
239
293
// Initialize info provider
@@ -242,18 +296,32 @@ int AppTask::Init()
242
296
243
297
chip::Server::GetInstance ().Init (initParams);
244
298
299
+ ret = PlatformMgr ().StartEventLoopTask ();
300
+ if (ret != CHIP_NO_ERROR)
301
+ {
302
+ PLAT_LOG (" PlatformMgr().StartEventLoopTask() failed" );
303
+ while (1 )
304
+ ;
305
+ }
306
+
245
307
ConfigurationMgr ().LogDeviceConfig ();
246
308
247
309
// We only have network commissioning on endpoint 0.
248
310
emberAfEndpointEnableDisable (kNetworkCommissioningEndpointSecondary , false );
249
311
312
+
250
313
// Register a function to receive events from the CHIP device layer. Note that calls to
251
314
// this function will happen on the CHIP event loop thread, not the app_main thread.
252
315
PlatformMgr ().AddEventHandler (DeviceEventCallback, reinterpret_cast <intptr_t >(nullptr ));
253
316
317
+ bool isThreadEnabled = ThreadStackMgrImpl ().IsThreadEnabled ();
318
+ if (!isThreadEnabled && isAppStarting){
254
319
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
255
- InitializeOTARequestor ();
320
+ PLAT_LOG (" Thread is Disabled, enable OTA Requestor" );
321
+ StartTimer (OTAREQUESTOR_INIT_TIMER_DELAY_MS);
322
+ isAppStarting = false ;
256
323
#endif
324
+ }
257
325
// QR code will be used with CHIP Tool
258
326
PrintOnboardingCodes (RendezvousInformationFlags (RendezvousInformationFlag::kBLE ));
259
327
@@ -276,6 +344,33 @@ void AppTask::AppTaskMain(void * pvParameter)
276
344
}
277
345
}
278
346
347
+ void StartTimer (uint32_t aTimeoutMs)
348
+ {
349
+ PLAT_LOG (" Start OTA Init Timer" )
350
+ if (xTimerIsTimerActive (sOTAInitTimer ))
351
+ {
352
+ PLAT_LOG (" app timer already started!" );
353
+ CancelTimer ();
354
+ }
355
+
356
+ // timer is not active, change its period to required value (== restart).
357
+ // FreeRTOS- Block for a maximum of 100 ticks if the change period command
358
+ // cannot immediately be sent to the timer command queue.
359
+ if (xTimerChangePeriod (sOTAInitTimer , pdMS_TO_TICKS (aTimeoutMs), 100 ) != pdPASS)
360
+ {
361
+ PLAT_LOG (" sOTAInitTimer timer start() failed" );
362
+ }
363
+ }
364
+
365
+ void CancelTimer (void )
366
+ {
367
+ if (xTimerStop (sOTAInitTimer , 0 ) == pdFAIL)
368
+ {
369
+ PLAT_LOG (" sOTAInitTimer stop() failed" );
370
+ }
371
+ }
372
+
373
+
279
374
void AppTask::PostEvent (const AppEvent * aEvent)
280
375
{
281
376
if (xQueueSend (sAppEventQueue , aEvent, 0 ) != pdPASS)
0 commit comments