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

build config: make openthread over lora a build-time configuration option #253

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion esp32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ RELEASE_DIR ?= build/

COPY_IDF_LIB ?= 0

# by default openthread over LoRa is enabled
OPENTHREAD ?= on

# by default Secure Boot and Flash Encryption are disabled
SECURE ?= off

Expand Down Expand Up @@ -97,7 +100,7 @@ LIBS = -L$(ESP_IDF_COMP_PATH)/esp32/lib -L$(ESP_IDF_COMP_PATH)/esp32/ld -L$(ESP_
$(ESP_IDF_COMP_PATH)/newlib/lib/libm-psram-workaround.a \
$(ESP_IDF_COMP_PATH)/newlib/lib/libc-psram-workaround.a \
-lfreertos -ljson -ljsmn -llwip -lnewlib -lvfs -lopenssl -lmbedtls -lwpa_supplicant \
-lxtensa-debug-module -lbt -lsdmmc -lsoc -lheap -lbootloader_support -lmicro-ecc -lopenthread \
-lxtensa-debug-module -lbt -lsdmmc -lsoc -lheap -lbootloader_support -lmicro-ecc \
-u ld_include_panic_highint_hdl -lsmartconfig_ack -lmesh
ifeq ($(BOARD), $(filter $(BOARD), FIPY))
LIBS += sigfox/modsigfox_fipy.a
Expand All @@ -111,6 +114,11 @@ ifeq ($(BOARD), $(filter $(BOARD), SIPY))
LIBS += sigfox/modsigfox_sipy.a
endif

ifeq ($(OPENTHREAD), on)
LIBS += -lopenthread
CFLAGS += -DLORA_OPENTHREAD_ENABLED
endif

B_LIBS = -Lbootloader/lib -Lbootloader -L$(BUILD)/bootloader -L$(ESP_IDF_COMP_PATH)/esp32/ld \
-L$(ESP_IDF_COMP_PATH)/esp32/lib -llog -lcore -lbootloader_support \
-lspi_flash -lsoc -lmicro-ecc -lgcc -lstdc++ -lgcov
Expand Down
31 changes: 30 additions & 1 deletion esp32/mods/modlora.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "lora/mac/region/RegionEU868.h"

// openThread includes
#ifdef LORA_OPENTHREAD_ENABLED
#include <openthread/udp.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
Expand All @@ -70,6 +71,7 @@
#include "lora/ot-settings.h"
#include "lora/ot-log.h"
#include "lora/ot-task.h"
#endif // #ifdef LORA_OPENTHREAD_ENABLED

/******************************************************************************
DEFINE PRIVATE CONSTANTS
Expand Down Expand Up @@ -269,7 +271,7 @@ static const char *modlora_nvs_data_key[E_LORA_NVS_NUM_KEYS] = { "JOINED", "UPLN
"MACPARAMS", "CHANNELS", "SRVACK", "MACNXTTX",
"MACBUFIDX", "MACRPTIDX", "MACBUF", "MACRPTBUF",
"REGION", "CHANMASK", "CHANMASKREM" };

#ifdef LORA_OPENTHREAD_ENABLED
static otInstance *ot = NULL;
bool ot_ready = false;

Expand All @@ -279,6 +281,7 @@ int otCliBufferLen = 0;
static char meshCliOutput[MESH_CLI_OUTPUT_SIZE];
static int meshCliOutputLen = 0;
static bool meshCliOutputDone = false;
#endif // #ifdef LORA_OPENTHREAD_ENABLED

/******************************************************************************
DECLARE PUBLIC DATA
Expand Down Expand Up @@ -405,6 +408,7 @@ bool modlora_is_module_sleep(void)
}
}

#ifdef LORA_OPENTHREAD_ENABLED
int lora_ot_recv(uint8_t *buf, int8_t *rssi) {

// put Lora into RX mode
Expand All @@ -429,6 +433,7 @@ void lora_ot_send(const uint8_t *buf, uint16_t len) {

otPlatLog(OT_LOG_LEVEL_INFO, 0, "radio TX: %d", len);
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

/******************************************************************************
DEFINE PRIVATE FUNCTIONS
Expand Down Expand Up @@ -2208,6 +2213,7 @@ STATIC mp_obj_t lora_nvram_erase (mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lora_nvram_erase_obj, lora_nvram_erase);

#ifdef LORA_OPENTHREAD_ENABLED
/*
* openthread CLI callback, triggered when an response from openthread has to be published on REPL
*/
Expand Down Expand Up @@ -2312,6 +2318,7 @@ STATIC mp_obj_t lora_cli(mp_obj_t self_in, mp_obj_t data) {
return mp_obj_new_str("", 0);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(lora_cli_obj, lora_cli);
#endif // #ifdef LORA_OPENTHREAD_ENABLED


STATIC const mp_map_elem_t lora_locals_dict_table[] = {
Expand All @@ -2338,8 +2345,10 @@ STATIC const mp_map_elem_t lora_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvram_save), (mp_obj_t)&lora_nvram_save_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvram_restore), (mp_obj_t)&lora_nvram_restore_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvram_erase), (mp_obj_t)&lora_nvram_erase_obj },
#ifdef LORA_OPENTHREAD_ENABLED
{ MP_OBJ_NEW_QSTR(MP_QSTR_mesh), (mp_obj_t)&lora_mesh_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_cli), (mp_obj_t)&lora_cli_obj },
#endif // #ifdef LORA_OPENTHREAD_ENABLED

// exceptions
{ MP_OBJ_NEW_QSTR(MP_QSTR_timeout), (mp_obj_t)&mp_type_TimeoutError },
Expand Down Expand Up @@ -2409,10 +2418,12 @@ static int lora_socket_socket (mod_network_socket_obj_t *s, int *_errno) {
}
s->sock_base.u.sd = 1;

#ifdef LORA_OPENTHREAD_ENABLED
// if mesh is enabled, assume socket is for mesh, not for LoraWAN
if (ot_ready) {
return mesh_socket_open(_errno);
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

uint32_t dr = DR_0;
switch (lora_obj.region) {
Expand All @@ -2437,18 +2448,22 @@ static int lora_socket_socket (mod_network_socket_obj_t *s, int *_errno) {

static void lora_socket_close (mod_network_socket_obj_t *s) {
s->sock_base.u.sd = -1;
#ifdef LORA_OPENTHREAD_ENABLED
mesh_socket_close();
#endif // #ifdef LORA_OPENTHREAD_ENABLED
}

static int lora_socket_send (mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, int *_errno) {
mp_int_t n_bytes = -1;

LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
*_errno = MP_EOPNOTSUPP;
return -1;
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

// is the radio able to transmit
if (lora_obj.pwr_mode == E_LORA_MODE_SLEEP) {
Expand Down Expand Up @@ -2484,10 +2499,12 @@ static int lora_socket_send (mod_network_socket_obj_t *s, const byte *buf, mp_ui
static int lora_socket_recv (mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno) {
LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
*_errno = MP_EOPNOTSUPP;
return -1;
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

int ret = lora_recv (buf, len, s->sock_base.timeout, NULL);
if (ret < 0) {
Expand All @@ -2500,9 +2517,11 @@ static int lora_socket_recv (mod_network_socket_obj_t *s, byte *buf, mp_uint_t l
static int lora_socket_recvfrom (mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) {
LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
return mesh_socket_recvfrom(buf, len, ip, port, _errno);
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

*port = 0; // in case there's no data received
int ret = lora_recv (buf, len, s->sock_base.timeout, (uint32_t *)port);
Expand All @@ -2516,10 +2535,12 @@ static int lora_socket_recvfrom (mod_network_socket_obj_t *s, byte *buf, mp_uint
static int lora_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) {
LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
*_errno = MP_EOPNOTSUPP;
return -1;
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

if (level != SOL_LORA) {
*_errno = MP_EOPNOTSUPP;
Expand Down Expand Up @@ -2548,10 +2569,12 @@ static int lora_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level,
static int lora_socket_settimeout (mod_network_socket_obj_t *s, mp_int_t timeout_ms, int *_errno) {
LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
*_errno = MP_EOPNOTSUPP;
return -1;
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

s->sock_base.timeout = timeout_ms;
return 0;
Expand All @@ -2560,9 +2583,11 @@ static int lora_socket_settimeout (mod_network_socket_obj_t *s, mp_int_t timeout
static int lora_socket_bind(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) {
LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
return mesh_socket_bind(ip, port, _errno);
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

if (port > 224) {
*_errno = MP_EOPNOTSUPP;
Expand All @@ -2577,10 +2602,12 @@ static int lora_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp

LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
*_errno = MP_EOPNOTSUPP;
return -1;
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
Expand All @@ -2600,9 +2627,11 @@ static int lora_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp
static int lora_socket_sendto(struct _mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) {
LORA_CHECK_SOCKET(s);

#ifdef LORA_OPENTHREAD_ENABLED
if (ot_ready) {
return mesh_socket_sendto(buf, len, ip, port, _errno);
}
#endif // #ifdef LORA_OPENTHREAD_ENABLED

// not implemented for LoraWAN
*_errno = MP_EOPNOTSUPP;
Expand Down
4 changes: 4 additions & 0 deletions esp32/mptask.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@

#if defined (LOPY) || defined (LOPY4) || defined (FIPY)
#include "modlora.h"
#ifdef LORA_OPENTHREAD_ENABLED
#include "lora/ot-task.h"
#endif // #ifdef LORA_OPENTHREAD_ENABLED
#endif
#if defined (SIPY) || defined(LOPY4) || defined (FIPY)
#include "sigfox/modsigfox.h"
Expand Down Expand Up @@ -243,7 +245,9 @@ void TASK_Micropython (void *pvParameters) {
// these ones are special because they need uPy running and they launch tasks
#if defined(LOPY) || defined (LOPY4) || defined (FIPY)
modlora_init0();
# ifdef LORA_OPENTHREAD_ENABLED
openthread_task_init();
# endif // #ifdef LORA_OPENTHREAD_ENABLED
#endif
#if defined(SIPY) || defined(LOPY4) || defined (FIPY)
modsigfox_init0();
Expand Down