Skip to content

Commit 0ae4855

Browse files
committed
feat(openthread): code improvements
1 parent c336d93 commit 0ae4855

File tree

2 files changed

+72
-36
lines changed

2 files changed

+72
-36
lines changed

libraries/OpenThread/src/OThread.cpp

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,29 @@ const otOperationalDataset &DataSet::getDataset() const {
134134
}
135135

136136
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);
138143
mDataset.mComponents.mIsNetworkNamePresent = true;
139144
}
140145

141146
void DataSet::setExtendedPanId(const uint8_t *extPanId) {
147+
if (!extPanId) {
148+
log_w("Extended PAN ID is null");
149+
return;
150+
}
142151
memcpy(mDataset.mExtendedPanId.m8, extPanId, OT_EXT_PAN_ID_SIZE);
143152
mDataset.mComponents.mIsExtendedPanIdPresent = true;
144153
}
145154

146155
void DataSet::setNetworkKey(const uint8_t *key) {
156+
if (!key) {
157+
log_w("Network key is null");
158+
return;
159+
}
147160
memcpy(mDataset.mNetworkKey.m8, key, OT_NETWORK_KEY_SIZE);
148161
mDataset.mComponents.mIsNetworkKeyPresent = true;
149162
}
@@ -183,10 +196,18 @@ void DataSet::apply(otInstance *instance) {
183196
}
184197

185198
// OpenThread Implementation
186-
bool OpenThread::otStarted = false;
199+
bool OpenThread::otStarted;
200+
otInstance *OpenThread::mInstance;
201+
DataSet OpenThread::mCurrentDataset;
202+
otNetworkKey OpenThread::mNetworkKey;
187203

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+
}
190211

191212
OpenThread::~OpenThread() {
192213
end();
@@ -216,13 +237,7 @@ void OpenThread::begin(bool OThreadAutoStart) {
216237
return;
217238
}
218239
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+
226241
// starts Thread with default dataset from NVS or from IDF default settings
227242
if (OThreadAutoStart) {
228243
otOperationalDatasetTlvs dataset;
@@ -240,24 +255,46 @@ void OpenThread::begin(bool OThreadAutoStart) {
240255
log_i("AUTO start OpenThread done");
241256
}
242257
}
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+
244267
otStarted = true;
245268
}
246269

247270
void OpenThread::end() {
271+
if (!otStarted) {
272+
log_w("OpenThread already stopped");
273+
return;
274+
}
275+
248276
if (s_ot_task != NULL) {
249277
vTaskDelete(s_ot_task);
250278
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) {
257283
esp_netif_destroy(openthread_netif);
258-
esp_vfs_eventfd_unregister();
284+
openthread_netif = NULL;
259285
}
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;
260296
otStarted = false;
297+
log_d("OpenThread ended successfully");
261298
}
262299

263300
void OpenThread::start() {
@@ -393,9 +430,8 @@ const uint8_t *OpenThread::getNetworkKey() const {
393430
log_w("Error: OpenThread instance not initialized");
394431
return nullptr;
395432
}
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;
399435
}
400436

401437
// Get the Node Channel
@@ -427,41 +463,40 @@ otInstance *OpenThread::getInstance() {
427463

428464
// Get the current dataset
429465
const DataSet &OpenThread::getCurrentDataSet() const {
430-
static DataSet currentDataset;
431466

432467
if (!mInstance) {
433468
log_w("Error: OpenThread instance not initialized");
434-
currentDataset.clear();
435-
return currentDataset;
469+
mCurrentDataset.clear();
470+
return mCurrentDataset;
436471
}
437472

438473
otOperationalDataset dataset;
439474
otError error = otDatasetGetActive(mInstance, &dataset);
440475

441476
if (error == OT_ERROR_NONE) {
442-
currentDataset.clear();
477+
mCurrentDataset.clear();
443478

444479
if (dataset.mComponents.mIsNetworkNamePresent) {
445-
currentDataset.setNetworkName(dataset.mNetworkName.m8);
480+
mCurrentDataset.setNetworkName(dataset.mNetworkName.m8);
446481
}
447482
if (dataset.mComponents.mIsExtendedPanIdPresent) {
448-
currentDataset.setExtendedPanId(dataset.mExtendedPanId.m8);
483+
mCurrentDataset.setExtendedPanId(dataset.mExtendedPanId.m8);
449484
}
450485
if (dataset.mComponents.mIsNetworkKeyPresent) {
451-
currentDataset.setNetworkKey(dataset.mNetworkKey.m8);
486+
mCurrentDataset.setNetworkKey(dataset.mNetworkKey.m8);
452487
}
453488
if (dataset.mComponents.mIsChannelPresent) {
454-
currentDataset.setChannel(dataset.mChannel);
489+
mCurrentDataset.setChannel(dataset.mChannel);
455490
}
456491
if (dataset.mComponents.mIsPanIdPresent) {
457-
currentDataset.setPanId(dataset.mPanId);
492+
mCurrentDataset.setPanId(dataset.mPanId);
458493
}
459494
} else {
460495
log_w("Failed to get active dataset (error: %d)", error);
461-
currentDataset.clear();
496+
mCurrentDataset.clear();
462497
}
463498

464-
return currentDataset;
499+
return mCurrentDataset;
465500
}
466501

467502
// Get the Mesh Local Prefix
@@ -541,7 +576,7 @@ void OpenThread::populateUnicastAddressCache() const {
541576
addr = addr->mNext;
542577
}
543578

544-
log_d("Populated unicast address cache with %d addresses", mCachedUnicastAddresses.size());
579+
log_d("Populated unicast address cache with %zu addresses", mCachedUnicastAddresses.size());
545580
}
546581

547582
// Populate multicast address cache from OpenThread
@@ -560,7 +595,7 @@ void OpenThread::populateMulticastAddressCache() const {
560595
mAddr = mAddr->mNext;
561596
}
562597

563-
log_d("Populated multicast address cache with %d addresses", mCachedMulticastAddresses.size());
598+
log_d("Populated multicast address cache with %zu addresses", mCachedMulticastAddresses.size());
564599
}
565600

566601
// Clear unicast address cache

libraries/OpenThread/src/OThread.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ class OpenThread {
150150

151151
private:
152152
static otInstance *mInstance;
153-
DataSet mCurrentDataSet;
153+
static DataSet mCurrentDataset; // Current dataset being used by the OpenThread instance.
154+
static otNetworkKey mNetworkKey; // Static storage to persist after function return
154155

155156
// Address caching for performance (user-controlled)
156157
mutable std::vector<IPAddress> mCachedUnicastAddresses;

0 commit comments

Comments
 (0)