Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit e078fcc

Browse files
Catalin IoanaIslam Wahdan
authored andcommitted
Pymesh feature added from Pygo to Lopy (#16)
* Squashed all openthread commits into a single one. esp32/mesh: saving settings in NVM; RSSI is computed considering SNR lora-mesh: forced Lora MAC as 802.15.4 Extended Address (not random anymore) esp32/lora: openthread socket UPD packets are 512B max, and 8 are buffered esp32/lora: added lora.mstatus, role, neighbors, routers micropy commands esp32/lora: increased mesh task priority (6), delay just 1ms, lora deinit esp32/lora: added mesh rx callback, increased mpy isr stack; removed server task esp32/mods added machtouch, frozen python scripts for oled, pytrack and msgpack esp32: openthread synch'ed with branch openthread_1_Nov from pycom-esp-idf sigfox task not started, some micropy wrapper for busy channel Lora sleep time decreased Lora LBT implemented openthread: 14 Dev 2018, fixed NVS usage, RSSI without SNR esp32: openthread cli fixed esp32: modlora added Border Router * Squashed all openthread commits into a single one. esp32/mesh: saving settings in NVM; RSSI is computed considering SNR lora-mesh: forced Lora MAC as 802.15.4 Extended Address (not random anymore) esp32/lora: openthread socket UPD packets are 512B max, and 8 are buffered esp32/lora: added lora.mstatus, role, neighbors, routers micropy commands esp32/lora: increased mesh task priority (6), delay just 1ms, lora deinit esp32/lora: added mesh rx callback, increased mpy isr stack; removed server task esp32/mods added machtouch, frozen python scripts for oled, pytrack and msgpack esp32: openthread synch'ed with branch openthread_1_Nov from pycom-esp-idf sigfox task not started, some micropy wrapper for busy channel Lora sleep time decreased Lora LBT implemented openthread: 14 Dev 2018, fixed NVS usage, RSSI without SNR esp32: openthread cli fixed esp32: modlora added Border Router * esp32/application.mk: added group of lora openthread files, buit only if OPENTHREAD flag is enable (yes by default) * esp32: added modmesh.c as micropy wrapper for mesh commands from modlora.c * esp32/mods/modmesh: added Mesh.deinit command, removed debug, made functions static * esp32/lib: updated idf libraries, from idf_v3.1 * esp32/modmesh: modmesh task is started dinamically at Lora.Mesh() and erased at Lora.Mesh.deinit()
1 parent ad55267 commit e078fcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1448
-656
lines changed

esp32/application.mk

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,17 @@ APP_LORA_SRC_C = $(addprefix lora/,\
192192
sx1276-board.c \
193193
sx1272-board.c \
194194
board.c \
195+
)
196+
197+
APP_LORA_OPENTHREAD_SRC_C = $(addprefix lora/,\
195198
otplat_alarm.c \
196199
otplat_radio.c \
197200
ot-settings.c \
198201
ot-log.c \
199-
ot-task.c \
202+
)
203+
204+
APP_MOD_MESH_SRC_C = $(addprefix mods/,\
205+
modmesh.c \
200206
)
201207

202208
APP_LIB_LORA_SRC_C = $(addprefix lib/lora/,\
@@ -305,6 +311,13 @@ ifeq ($(BOARD),$(filter $(BOARD), FIPY GPY))
305311
OBJ += $(addprefix $(BUILD)/, $(APP_LTE_SRC_C:.c=.o) $(APP_MODS_LTE_SRC_C:.c=.o))
306312
endif
307313

314+
# add OPENTHREAD code only if flag enabled and for LOPY, LOPY4 and FIPY
315+
ifeq ($(OPENTHREAD), on)
316+
ifeq ($(BOARD), $(filter $(BOARD), LOPY LOPY4 FIPY))
317+
OBJ += $(addprefix $(BUILD)/, $(APP_LORA_OPENTHREAD_SRC_C:.c=.o) $(APP_MOD_MESH_SRC_C:.c=.o))
318+
endif
319+
endif # ifeq ($(OPENTHREAD), on)
320+
308321
OBJ += $(addprefix $(BUILD)/, $(APP_MAIN_SRC_C:.c=.o) $(APP_HAL_SRC_C:.c=.o) $(APP_LIB_SRC_C:.c=.o))
309322
OBJ += $(addprefix $(BUILD)/, $(APP_MODS_SRC_C:.c=.o) $(APP_STM_SRC_C:.c=.o))
310323
OBJ += $(addprefix $(BUILD)/, $(APP_FATFS_SRC_C:.c=.o) $(APP_LITTLEFS_SRC_C:.c=.o) $(APP_UTIL_SRC_C:.c=.o) $(APP_TELNET_SRC_C:.c=.o))
@@ -325,6 +338,12 @@ ifeq ($(BOARD),$(filter $(BOARD), FIPY GPY))
325338
SRC_QSTR += $(APP_MODS_LTE_SRC_C)
326339
endif
327340

341+
ifeq ($(OPENTHREAD), on)
342+
ifeq ($(BOARD), $(filter $(BOARD), LOPY LOPY4 FIPY))
343+
SRC_QSTR += $(APP_MOD_MESH_SRC_C)
344+
endif
345+
endif # ifeq ($(OPENTHREAD), on)
346+
328347
# Append any auto-generated sources that are needed by sources listed in
329348
# SRC_QSTR
330349
SRC_QSTR_AUTO_DEPS +=
0 Bytes
Binary file not shown.

esp32/bootloader/lib/liblog.a

0 Bytes
Binary file not shown.

esp32/bootloader/lib/libmicro-ecc.a

0 Bytes
Binary file not shown.

esp32/bootloader/lib/libsoc.a

0 Bytes
Binary file not shown.

esp32/bootloader/lib/libspi_flash.a

0 Bytes
Binary file not shown.

esp32/lib/libbootloader_support.a

0 Bytes
Binary file not shown.

esp32/lib/libbt.a

0 Bytes
Binary file not shown.

esp32/lib/libcxx.a

0 Bytes
Binary file not shown.

esp32/lib/libdriver.a

0 Bytes
Binary file not shown.

esp32/lib/libesp32.a

-4 Bytes
Binary file not shown.

esp32/lib/libesp_adc_cal.a

0 Bytes
Binary file not shown.

esp32/lib/libexpat.a

0 Bytes
Binary file not shown.

esp32/lib/libfreertos.a

0 Bytes
Binary file not shown.

esp32/lib/libheap.a

0 Bytes
Binary file not shown.

esp32/lib/libjsmn.a

0 Bytes
Binary file not shown.

esp32/lib/libjson.a

0 Bytes
Binary file not shown.

esp32/lib/liblog.a

0 Bytes
Binary file not shown.

esp32/lib/liblwip.a

0 Bytes
Binary file not shown.

esp32/lib/libmbedtls.a

0 Bytes
Binary file not shown.

esp32/lib/libmicro-ecc.a

0 Bytes
Binary file not shown.

esp32/lib/libnewlib.a

0 Bytes
Binary file not shown.

esp32/lib/libnghttp.a

0 Bytes
Binary file not shown.

esp32/lib/libnvs_flash.a

0 Bytes
Binary file not shown.

esp32/lib/libopenssl.a

0 Bytes
Binary file not shown.

esp32/lib/libopenthread.a

898 KB
Binary file not shown.

esp32/lib/libpthread.a

0 Bytes
Binary file not shown.

esp32/lib/libsdmmc.a

0 Bytes
Binary file not shown.

esp32/lib/libsmartconfig_ack.a

0 Bytes
Binary file not shown.

esp32/lib/libsoc.a

0 Bytes
Binary file not shown.

esp32/lib/libspi_flash.a

0 Bytes
Binary file not shown.

esp32/lib/libtcpip_adapter.a

0 Bytes
Binary file not shown.

esp32/lib/libvfs.a

0 Bytes
Binary file not shown.

esp32/lib/libwpa_supplicant.a

0 Bytes
Binary file not shown.

esp32/lib/libxtensa-debug-module.a

0 Bytes
Binary file not shown.

esp32/lora/ot-log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "ot-log.h"
1515

1616
#include "pycom_config.h"
17-
#include <openthread/types.h>
1817
#include <openthread/config.h>
1918
#include <openthread/platform/alarm-milli.h>
2019
#include <openthread/platform/uart.h>
@@ -73,6 +72,7 @@ void otPlatLogBufHex(const uint8_t *aBuf, uint16_t aBufLength) {
7372
for (int i = 0; i < aBufLength; i++) {
7473
length += sprintf(&logString[length], "%x ", aBuf[i]);
7574
}
75+
logString[length++] = '\n';
7676
}
7777

7878
void otPlatLogFlush(void) {

esp32/lora/ot-settings.c

Lines changed: 113 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,27 @@
1212
#include <stdbool.h>
1313
#include <string.h>
1414
#include "pycom_config.h"
15+
#include "nvs.h"
1516

1617
#include <openthread/instance.h>
1718
#include <openthread/ip6.h>
1819
#include <openthread/platform/settings.h>
20+
#include "ot-log.h"
1921

20-
#define OT_PLAT_SETTINGS_NUM 7
21-
#define OT_PLAT_SETTINGS_VAL_MAX_SIZE 128
22-
#define OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY 1
23-
24-
typedef struct settings_row_s {
25-
int16_t size;
26-
uint8_t value[OT_PLAT_SETTINGS_VAL_MAX_SIZE];
27-
} settings_row_t;
28-
29-
typedef struct settings_s {
30-
//uint16_t key; # key is the actual index
31-
int8_t rows_num;
32-
settings_row_t rows[OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY];
33-
} settings_t;
34-
35-
settings_t settings[OT_PLAT_SETTINGS_NUM];
36-
37-
void printRow(settings_row_t row) {
38-
printf("size %d:", row.size);
39-
/*
40-
for (int i = 0; i < row.size; i++)
41-
printf("%d, ", row.value[i]);
42-
*/
43-
printf("\n");
44-
}
22+
#define OT_NVS_NAMESPACE "LORA_MESH"
4523

46-
void printKey(settings_t key) {
47-
printf(" num_rows: %d", key.rows_num);
48-
for (int i = 0; i < key.rows_num; i++) {
49-
printf("\nRow %d, ", i);
50-
printRow(key.rows[i]);
51-
}
52-
}
24+
#define OT_PLAT_SETTINGS_NUM 255
25+
#define OT_PLAT_SETTINGS_VAL_MAX_SIZE 256
26+
#define OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY 256
27+
28+
/* number of chars to store key and index as string, 256 key max, 256 index max, so in hex FFFF 4 chars + 1 termination */
29+
#define KEY_STRING_CHARS_NUM 5
30+
31+
// returns the string, zzzOutStr, created from integer: 2 hex digits xxxKey and 2 hex digits yyyIndex
32+
#define KEY_INDEX_TO_STR(xxxKey, yyyIndex, zzzOutStr) itoa((((xxxKey << 16) | yyyIndex) & 0xFFFF), zzzOutStr, 16)
33+
34+
static nvs_handle ot_nvs_handle;
5335

54-
void otPlatSettingsPrint(void) {
55-
printf("----------------------------------------------");
56-
for (int i = 0; i < OT_PLAT_SETTINGS_NUM; i++) {
57-
printf("\nKey %d: ", i);
58-
printKey(settings[i]);
59-
}
60-
printf("----------------------------------------------\n");
61-
}
6236
/**
6337
* Performs any initialization for the settings subsystem, if necessary.
6438
*
@@ -69,14 +43,12 @@ void otPlatSettingsPrint(void) {
6943

7044
void otPlatSettingsInit(otInstance *aInstance) {
7145
(void) aInstance;
72-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, "otPlatSettingsInit mem %d B\n",
73-
OT_PLAT_SETTINGS_NUM * sizeof(settings_t));
74-
for (int i = 0; i < OT_PLAT_SETTINGS_NUM; i++) {
75-
settings[i].rows_num = -1;
76-
for (int j = 0; j < OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY; j++) {
77-
settings[i].rows[j].size = -1;
78-
}
46+
47+
if (nvs_open(OT_NVS_NAMESPACE, NVS_READWRITE, &ot_nvs_handle) != ESP_OK) {
48+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "otPlatSettingsInit NOT OK");
7949
}
50+
//nvs_erase_all(ot_nvs_handle);
51+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "otPlatSettingsInit ok");
8052
//printSettings();
8153
}
8254

@@ -126,27 +98,44 @@ void otPlatSettingsInit(otInstance *aInstance) {
12698
otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex,
12799
uint8_t *aValue, uint16_t *aValueLength) {
128100
(void) aInstance;
129-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, "otPlatSettingsGet %d %d %d ", aKey, aIndex,
130-
*aValueLength);
101+
esp_err_t err;
102+
size_t length = 0;
103+
char key_str[KEY_STRING_CHARS_NUM];
131104

132-
if (aKey > OT_PLAT_SETTINGS_NUM)
133-
return OT_ERROR_NOT_FOUND;
105+
// transform key to string
106+
KEY_INDEX_TO_STR(aKey, aIndex, key_str);
134107

135-
if (aIndex > settings[aKey].rows_num
136-
|| settings[aKey].rows[aIndex].size < 0) {
137-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, " not found\n");
108+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "otPlatSettingsGet %d 0x%s %d %d %p", aKey, key_str, aIndex,
109+
*aValueLength, aValue);
110+
111+
if (aKey > OT_PLAT_SETTINGS_NUM || aIndex > OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY) {
112+
otPlatLog(OT_LOG_LEVEL_INFO, 0, " not found\n");
138113
return OT_ERROR_NOT_FOUND;
139114
}
140115

141-
if (*aValueLength < settings[aKey].rows[aIndex].size) {
142-
aValueLength = NULL;
116+
if (aValue == NULL && aValueLength == NULL) {
117+
// checking if key exists
118+
if (ESP_OK != (err = nvs_get_blob(ot_nvs_handle, key_str, aValue, &length))) {
119+
otPlatLog(OT_LOG_LEVEL_INFO, 0, " nvs_get_blob 1 0x%X\n", err);
120+
return OT_ERROR_NOT_FOUND;
121+
}
122+
} else if (aValue == NULL && aValueLength != NULL) {
123+
// checking the size of a key
124+
if (ESP_OK != (err = nvs_get_blob(ot_nvs_handle, key_str, aValue, &length))) {
125+
otPlatLog(OT_LOG_LEVEL_INFO, 0, " nvs_get_blob 2 0x%X\n", err);
126+
return OT_ERROR_NOT_FOUND;
127+
}
128+
*aValueLength = length;
143129
} else {
144-
memcpy(aValue, settings[aKey].rows[aIndex].value,
145-
settings[aKey].rows[aIndex].size);
146-
*aValueLength = settings[aKey].rows[aIndex].size;
130+
length = *aValueLength;
131+
if (ESP_OK != (err = nvs_get_blob(ot_nvs_handle, key_str, aValue, &length))) {
132+
otPlatLog(OT_LOG_LEVEL_INFO, 0, " nvs_get_blob 3 0x%X\n", err);
133+
return OT_ERROR_NOT_FOUND;
134+
}
135+
*aValueLength = length;
147136
}
148-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, " ok\n");
149-
//printSettings();
137+
138+
otPlatLog(OT_LOG_LEVEL_INFO, 0, " ok\n");
150139
return OT_ERROR_NONE;
151140
}
152141

@@ -179,25 +168,13 @@ otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex,
179168
otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey,
180169
const uint8_t *aValue, uint16_t aValueLength) {
181170
(void) aInstance;
182-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, "otPlatSettingsSet key %d len %d", aKey,
183-
aValueLength);
184171

185-
if (aKey > OT_PLAT_SETTINGS_NUM)
186-
return OT_ERROR_NOT_IMPLEMENTED;
187-
188-
if (aValueLength > OT_PLAT_SETTINGS_VAL_MAX_SIZE)
189-
return OT_ERROR_NOT_IMPLEMENTED;
190-
191-
settings[aKey].rows_num = 1;
172+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "otPlatSettingsSet key %d len %d", aKey, aValueLength);
192173

193-
settings[aKey].rows[0].size = aValueLength;
174+
// delete all records for this aKey
175+
otPlatSettingsDelete(aInstance, aKey, -1);
194176

195-
if (aValueLength > 0) {
196-
memcpy(settings[aKey].rows[0].value, aValue, aValueLength);
197-
}
198-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, " ok\n");
199-
//printSettings();
200-
return OT_ERROR_NONE;
177+
return otPlatSettingsAdd(aInstance, aKey, aValue, aValueLength);
201178
}
202179

203180
/// Removes a setting from the setting store
@@ -225,22 +202,35 @@ otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey,
225202
*/
226203
otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) {
227204
(void) aInstance;
228-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, "otPlatSettingsDelete %d %d\n", aKey,
229-
aIndex);
205+
char key_str[KEY_STRING_CHARS_NUM];
230206

231-
if (aKey > OT_PLAT_SETTINGS_NUM)
232-
return OT_ERROR_NOT_FOUND;
207+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "otPlatSettingsDelete %d %d", aKey,
208+
aIndex);
233209

234-
if (aIndex >= OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY)
210+
if (aKey > OT_PLAT_SETTINGS_NUM || aIndex > OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY)
235211
return OT_ERROR_NOT_FOUND;
236212

237-
if (aIndex == -1) {
238-
for (int i = 0; i < settings[aKey].rows_num; i++)
239-
settings[aKey].rows[i].size = -1;
213+
if (aIndex != -1) {
214+
// transform key to string
215+
KEY_INDEX_TO_STR(aKey, aIndex, key_str);
240216

241-
settings[aKey].rows_num = -1;
217+
if (ESP_OK == nvs_erase_key(ot_nvs_handle, key_str)) {
218+
nvs_commit(ot_nvs_handle);
219+
}
220+
} else {
221+
// delete all indexes for this key
222+
for (int index = 0; index < OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY; index++) {
223+
KEY_INDEX_TO_STR(aKey, index, key_str);
224+
// try to erase the key for this index
225+
if (ESP_ERR_NVS_NOT_FOUND == nvs_erase_key(ot_nvs_handle, key_str)) {
226+
// key is not found, so we're done for this aKey
227+
break;
228+
}
229+
}
230+
nvs_commit(ot_nvs_handle);
242231
}
243-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, "otPlatSettingsDelete ok\n");
232+
233+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "otPlatSettingsDelete ok");
244234
return OT_ERROR_NONE;
245235

246236
}
@@ -278,10 +268,40 @@ otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) {
278268
*/
279269
otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey,
280270
const uint8_t *aValue, uint16_t aValueLength) {
271+
int index = 0;
272+
size_t length = 0;
273+
esp_err_t err = ESP_OK;
274+
char key_str[KEY_STRING_CHARS_NUM];
275+
281276
(void) aInstance;
282-
otPlatLog(OT_LOG_LEVEL_DEBG, 0, "!!!!! otPlatSettingsAdd %d %d %d\n", aKey,
283-
*aValue, aValueLength);
284-
return OT_ERROR_NOT_IMPLEMENTED;
277+
278+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "otPlatSettingsAdd %d 0x%p %d", aKey,
279+
aValue, aValueLength);
280+
281+
if (aKey > OT_PLAT_SETTINGS_NUM)
282+
return OT_ERROR_NOT_FOUND;
283+
284+
// search for first index available for aKey
285+
for (index = 0; index < OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY; index++) {
286+
KEY_INDEX_TO_STR(aKey, index, key_str);
287+
// checking if key exists
288+
if (ESP_OK != (err = nvs_get_blob(ot_nvs_handle, key_str, NULL, &length))) {
289+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "found free index %d\n", index);
290+
// key doesn't exist
291+
break;
292+
}
293+
}
294+
295+
if (ESP_OK != err) {
296+
// try to write
297+
if (ESP_OK != (err = nvs_set_blob(ot_nvs_handle, key_str, aValue, aValueLength))) {
298+
otPlatLog(OT_LOG_LEVEL_INFO, 0, " nvs_set_blob 0x%X\n", err);
299+
return OT_ERROR_NOT_IMPLEMENTED;
300+
}
301+
nvs_commit(ot_nvs_handle);
302+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "stored as index %d\n", index);
303+
}
304+
return OT_ERROR_NONE;
285305
}
286306

287307
/// Removes all settings from the setting store
@@ -292,12 +312,9 @@ otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey,
292312
* The OpenThread instance structure.
293313
*/
294314
void otPlatSettingsWipe(otInstance *aInstance) {
295-
for (int i = 0; i < OT_PLAT_SETTINGS_NUM; i++) {
296-
settings[i].rows_num = -1;
297-
for (int j = 0; j < OT_PLAT_SETTINGS_MAX_INDEX_PER_KEY; j++) {
298-
settings[i].rows[j].size = -1;
299-
}
300-
}
315+
nvs_erase_all(ot_nvs_handle);
316+
nvs_commit(ot_nvs_handle);
317+
otPlatLog(OT_LOG_LEVEL_INFO, 0, "!!!!! otPlatSettingsWipe");
301318
}
302319

303320
/**

0 commit comments

Comments
 (0)