@@ -134,16 +134,29 @@ const otOperationalDataset &DataSet::getDataset() const {
134
134
}
135
135
136
136
void DataSet::setNetworkName (const char *name) {
137
- strncpy (mDataset .mNetworkName .m8 , name, sizeof (mDataset .mNetworkName .m8 ));
137
+ if (!name) {
138
+ log_w (" Network name is null" );
139
+ return ;
140
+ }
141
+ // char m8[OT_NETWORK_KEY_SIZE + 1] bytes space by definition
142
+ strncpy (mDataset .mNetworkName .m8 , name, OT_NETWORK_KEY_SIZE);
138
143
mDataset .mComponents .mIsNetworkNamePresent = true ;
139
144
}
140
145
141
146
void DataSet::setExtendedPanId (const uint8_t *extPanId) {
147
+ if (!extPanId) {
148
+ log_w (" Extended PAN ID is null" );
149
+ return ;
150
+ }
142
151
memcpy (mDataset .mExtendedPanId .m8 , extPanId, OT_EXT_PAN_ID_SIZE);
143
152
mDataset .mComponents .mIsExtendedPanIdPresent = true ;
144
153
}
145
154
146
155
void DataSet::setNetworkKey (const uint8_t *key) {
156
+ if (!key) {
157
+ log_w (" Network key is null" );
158
+ return ;
159
+ }
147
160
memcpy (mDataset .mNetworkKey .m8 , key, OT_NETWORK_KEY_SIZE);
148
161
mDataset .mComponents .mIsNetworkKeyPresent = true ;
149
162
}
@@ -183,10 +196,18 @@ void DataSet::apply(otInstance *instance) {
183
196
}
184
197
185
198
// OpenThread Implementation
186
- bool OpenThread::otStarted = false ;
199
+ bool OpenThread::otStarted;
200
+ otInstance *OpenThread::mInstance ;
201
+ DataSet OpenThread::mCurrentDataset ;
202
+ otNetworkKey OpenThread::mNetworkKey ;
187
203
188
- otInstance *OpenThread::mInstance = nullptr ;
189
- OpenThread::OpenThread () {}
204
+ OpenThread::OpenThread () {
205
+ // static initialization (node data and stack starting information)
206
+ otStarted = false ;
207
+ mCurrentDataset .clear (); // Initialize the current dataset
208
+ memset (&mNetworkKey , 0 , sizeof (mNetworkKey )); // Initialize the network key
209
+ mInstance = nullptr ;
210
+ }
190
211
191
212
OpenThread::~OpenThread () {
192
213
end ();
@@ -216,13 +237,7 @@ void OpenThread::begin(bool OThreadAutoStart) {
216
237
return ;
217
238
}
218
239
log_d (" OpenThread task created successfully" );
219
- // get the OpenThread instance that will be used for all operations
220
- mInstance = esp_openthread_get_instance ();
221
- if (!mInstance ) {
222
- log_e (" Error: Failed to initialize OpenThread instance" );
223
- end ();
224
- return ;
225
- }
240
+
226
241
// starts Thread with default dataset from NVS or from IDF default settings
227
242
if (OThreadAutoStart) {
228
243
otOperationalDatasetTlvs dataset;
@@ -240,24 +255,46 @@ void OpenThread::begin(bool OThreadAutoStart) {
240
255
log_i (" AUTO start OpenThread done" );
241
256
}
242
257
}
243
- delay (500 ); // Give some time for the OpenThread tasks to initialize
258
+
259
+ // get the OpenThread instance that will be used for all operations
260
+ mInstance = esp_openthread_get_instance ();
261
+ if (!mInstance ) {
262
+ log_e (" Error: Failed to initialize OpenThread instance" );
263
+ end ();
264
+ return ;
265
+ }
266
+
244
267
otStarted = true ;
245
268
}
246
269
247
270
void OpenThread::end () {
271
+ if (!otStarted) {
272
+ log_w (" OpenThread already stopped" );
273
+ return ;
274
+ }
275
+
248
276
if (s_ot_task != NULL ) {
249
277
vTaskDelete (s_ot_task);
250
278
s_ot_task = NULL ;
251
- // Clean up
252
- esp_openthread_deinit ();
253
- esp_openthread_netif_glue_deinit ();
254
- #if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM
255
- ot_lwip_netif = NULL ;
256
- #endif
279
+ }
280
+
281
+ // Clean up in reverse order of initialization
282
+ if (openthread_netif != NULL ) {
257
283
esp_netif_destroy (openthread_netif);
258
- esp_vfs_eventfd_unregister () ;
284
+ openthread_netif = NULL ;
259
285
}
286
+
287
+ esp_openthread_netif_glue_deinit ();
288
+ esp_openthread_deinit ();
289
+ esp_vfs_eventfd_unregister ();
290
+
291
+ #if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM
292
+ ot_lwip_netif = NULL ;
293
+ #endif
294
+
295
+ mInstance = nullptr ;
260
296
otStarted = false ;
297
+ log_d (" OpenThread ended successfully" );
261
298
}
262
299
263
300
void OpenThread::start () {
@@ -393,9 +430,8 @@ const uint8_t *OpenThread::getNetworkKey() const {
393
430
log_w (" Error: OpenThread instance not initialized" );
394
431
return nullptr ;
395
432
}
396
- static otNetworkKey networkKey; // Static storage to persist after function return
397
- otThreadGetNetworkKey (mInstance , &networkKey);
398
- return networkKey.m8 ;
433
+ otThreadGetNetworkKey (mInstance , &mNetworkKey );
434
+ return mNetworkKey .m8 ;
399
435
}
400
436
401
437
// Get the Node Channel
@@ -427,41 +463,40 @@ otInstance *OpenThread::getInstance() {
427
463
428
464
// Get the current dataset
429
465
const DataSet &OpenThread::getCurrentDataSet () const {
430
- static DataSet currentDataset;
431
466
432
467
if (!mInstance ) {
433
468
log_w (" Error: OpenThread instance not initialized" );
434
- currentDataset .clear ();
435
- return currentDataset ;
469
+ mCurrentDataset .clear ();
470
+ return mCurrentDataset ;
436
471
}
437
472
438
473
otOperationalDataset dataset;
439
474
otError error = otDatasetGetActive (mInstance , &dataset);
440
475
441
476
if (error == OT_ERROR_NONE) {
442
- currentDataset .clear ();
477
+ mCurrentDataset .clear ();
443
478
444
479
if (dataset.mComponents .mIsNetworkNamePresent ) {
445
- currentDataset .setNetworkName (dataset.mNetworkName .m8 );
480
+ mCurrentDataset .setNetworkName (dataset.mNetworkName .m8 );
446
481
}
447
482
if (dataset.mComponents .mIsExtendedPanIdPresent ) {
448
- currentDataset .setExtendedPanId (dataset.mExtendedPanId .m8 );
483
+ mCurrentDataset .setExtendedPanId (dataset.mExtendedPanId .m8 );
449
484
}
450
485
if (dataset.mComponents .mIsNetworkKeyPresent ) {
451
- currentDataset .setNetworkKey (dataset.mNetworkKey .m8 );
486
+ mCurrentDataset .setNetworkKey (dataset.mNetworkKey .m8 );
452
487
}
453
488
if (dataset.mComponents .mIsChannelPresent ) {
454
- currentDataset .setChannel (dataset.mChannel );
489
+ mCurrentDataset .setChannel (dataset.mChannel );
455
490
}
456
491
if (dataset.mComponents .mIsPanIdPresent ) {
457
- currentDataset .setPanId (dataset.mPanId );
492
+ mCurrentDataset .setPanId (dataset.mPanId );
458
493
}
459
494
} else {
460
495
log_w (" Failed to get active dataset (error: %d)" , error);
461
- currentDataset .clear ();
496
+ mCurrentDataset .clear ();
462
497
}
463
498
464
- return currentDataset ;
499
+ return mCurrentDataset ;
465
500
}
466
501
467
502
// Get the Mesh Local Prefix
@@ -541,7 +576,7 @@ void OpenThread::populateUnicastAddressCache() const {
541
576
addr = addr->mNext ;
542
577
}
543
578
544
- log_d (" Populated unicast address cache with %d addresses" , mCachedUnicastAddresses .size ());
579
+ log_d (" Populated unicast address cache with %zu addresses" , mCachedUnicastAddresses .size ());
545
580
}
546
581
547
582
// Populate multicast address cache from OpenThread
@@ -560,7 +595,7 @@ void OpenThread::populateMulticastAddressCache() const {
560
595
mAddr = mAddr ->mNext ;
561
596
}
562
597
563
- log_d (" Populated multicast address cache with %d addresses" , mCachedMulticastAddresses .size ());
598
+ log_d (" Populated multicast address cache with %zu addresses" , mCachedMulticastAddresses .size ());
564
599
}
565
600
566
601
// Clear unicast address cache
0 commit comments