From cafe9c8a8793af9651945d487f6e67ae0ba35e1e Mon Sep 17 00:00:00 2001 From: Winfred LIN Date: Wed, 1 Apr 2020 18:47:45 +1100 Subject: [PATCH] added sending unconfirmed message for RHF76 1. added AT+ command "AT+MSGHEX" and change previous RHF76_ATCMD_FMT_SEND_MSGHEX to RHF76_ATCMD_FMT_SEND_CMSGHEX 2. implement send_unconfirmed with a non-breaking approach: added int (*send_unconfirmed)(const void *buf, size_t len); event handle in lora_module_st; added int tos_lora_module_send_unconfirmed(const void *buf, size_t len); in lora_module_wrapper.c; and implement static int rhf76_send_unconfirmed(const void *buf, size_t len) in RHF76.c 3. added { "+MSGHEX: PORT:", rhf76_incoming_data_process } in event array rhf76_at_event so that the device is able to process the downlink data when it is configurated to send unconfirmed message. 4. added AT+ command "AT+REPT", and implement the static int rhf76_set_repeat(uint8_t num) to enable user to set repeat times (1-15) while using "unconfirmed message" 5. updated the instructions of implemeting up-link parser and downlink parser on the server in the file "..\board\NUCLEO_STM32L073RZ\BSP\Src\lora_demo.c" --- board/NUCLEO_STM32L073RZ/BSP/Src/lora_demo.c | 47 +++++++++++-- devices/rhf76_lora/RHF76.c | 68 ++++++++++++++++--- devices/rhf76_lora/RHF76.h | 5 +- net/lora_module_wrapper/lora_module_wrapper.c | 8 +++ net/lora_module_wrapper/lora_module_wrapper.h | 14 ++++ 5 files changed, 127 insertions(+), 15 deletions(-) diff --git a/board/NUCLEO_STM32L073RZ/BSP/Src/lora_demo.c b/board/NUCLEO_STM32L073RZ/BSP/Src/lora_demo.c index 12a9034d..35583663 100644 --- a/board/NUCLEO_STM32L073RZ/BSP/Src/lora_demo.c +++ b/board/NUCLEO_STM32L073RZ/BSP/Src/lora_demo.c @@ -1,8 +1,8 @@ #include "lora_demo.h" #include "RHF76.h" -#include #include "bsp.h" #include +#include /* ================================================================================== @@ -19,11 +19,43 @@ step: 1 unit: % + property pressure pressure integer read-write range: [259, 1260] + initial: 260 + step: 1 + unit: hPa + + property magnFullscale magnFullscale integer read-write range: [4, 16] + initial: 4 + step: 1 + unit: guass + + property magn_x magn_x float read-write range: [-16, 16] + initial: 0 + step: 1 + unit: guass + + property magn_y magn_y float read-write range: [-16, 16] + initial: 0 + step: 1 + unit: guass + + property magn_z magn_z float read-write range: [-16, 16] + initial: 0 + step: 1 + unit: guass + + property altitude altitude float read-write range: [-1000, 10000] + initial: 0 + step: 1 + unit: m + + property isconfirmed isconfirmed bool read-write 0: confirmed message + 1: unconfirmed message + property report_period period integer read-write range: [0, 3600] initial: 0 step: 1 unit: second - ================================================================================== up-link parser javascript: @@ -87,7 +119,7 @@ */ uint16_t report_period = 10; -bool isconfirmed = false; +bool is_confirmed = true; typedef struct device_data_st { uint8_t magn_fullscale; // fullscale of magnetometer(RW) @@ -126,7 +158,7 @@ void recv_callback(uint8_t *data, uint8_t len) } else if (len >= 2) { report_period = data[0] | (data[1] << 8); LIS3MDL_Set_FullScale((LIS3MDL_FullScaleTypeDef)data[2]); - isconfirmed = (bool)data[3]; + is_confirmed = (bool)data[3]; } } @@ -175,7 +207,12 @@ void application_entry(void *arg) dev_data_wrapper.u.dev_data.pressure = (uint32_t)(sensor_data.sensor_press.pressure); dev_data_wrapper.u.dev_data.period = report_period; // send data to the server (via gateway) - tos_lora_module_send(dev_data_wrapper.u.serialize, sizeof(dev_data_t)); + if(is_confirmed){ + tos_lora_module_send(dev_data_wrapper.u.serialize, sizeof(dev_data_t)); + }else{ + tos_lora_module_send_unconfirmed(dev_data_wrapper.u.serialize, sizeof(dev_data_t)); + } + tos_task_delay(report_period * 1000); } } diff --git a/devices/rhf76_lora/RHF76.c b/devices/rhf76_lora/RHF76.c index 73f67abb..a9a956a0 100644 --- a/devices/rhf76_lora/RHF76.c +++ b/devices/rhf76_lora/RHF76.c @@ -154,6 +154,26 @@ static int rhf76_set_chanel(void) return -1; } +static int rhf76_set_repeat(uint8_t num) +{ + int try = 0; + at_echo_t echo; + char cmd[14] = {0}; + char expect[10] = {'\0'}; + snprintf(cmd, sizeof(cmd), RHF76_ATCMD_SET_REPT, num); + snprintf(expect, sizeof(expect), "+REPT: %d", num); + + tos_at_echo_create(&echo, NULL, 0, expect); + + while (try++ < 10) { + tos_at_cmd_exec(&echo, 3000, cmd); + if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) { + return 0; + } + } + return -1; +} + static int rhf76_set_adr_off(void) { int try = 0; @@ -305,9 +325,14 @@ static int rhf76_init(void) return -1; } + if(rhf76_set_repeat(1)!=0){ + printf("rhf76 set repeat times for unconfirmed message FAILED\n"); + return -1; + } + at_delay_ms(2000); printf("Init RHF76 LoRa done\n"); - + return 0; } @@ -377,6 +402,7 @@ __STATIC__ void rhf76_incoming_data_process(void) at_event_t rhf76_at_event[] = { { "+CMSGHEX: PORT:", rhf76_incoming_data_process }, + { "+MSGHEX: PORT:", rhf76_incoming_data_process } }; static char __num2hex(uint8_t num) @@ -405,7 +431,6 @@ static void __hex2str(uint8_t *in, char *out, int len) static int rhf76_send(const void *buf, size_t len) { - char *str_buf = NULL; str_buf = tos_mmheap_calloc(2 * len + 1, sizeof(char)); @@ -416,7 +441,7 @@ static int rhf76_send(const void *buf, size_t len) char cmd[100] = {0}; at_echo_t echo; - snprintf(cmd, sizeof(cmd), RHF76_ATCMD_FMT_SEND_MSGHEX, str_buf); + snprintf(cmd, sizeof(cmd), RHF76_ATCMD_FMT_SEND_CMSGHEX, str_buf); cmd[sizeof(cmd) - 1] = '\0'; tos_mmheap_free(str_buf); tos_at_echo_create(&echo, NULL, 0, "+CMSG: ACK Received"); @@ -427,18 +452,43 @@ static int rhf76_send(const void *buf, size_t len) return len; } +static int rhf76_send_unconfirmed(const void *buf, size_t len) +{ + + char *str_buf = NULL; + at_echo_t echo; + + str_buf = tos_mmheap_calloc(2 * len + 1, sizeof(char)); + if (!str_buf) { + return -1; + } + __hex2str((uint8_t *)buf, str_buf, len); + + char cmd[100] = {0}; + snprintf(cmd, sizeof(cmd), RHF76_ATCMD_FMT_SEND_MSGHEX, str_buf); + cmd[sizeof(cmd) - 1] = '\0'; + tos_mmheap_free(str_buf); + tos_at_echo_create(&echo, NULL, 0, "+MSGHEX: Done"); + tos_at_cmd_exec(&echo, 6000, cmd); + if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) { + return -1; + } + return len; +} + static int rhf76_close(void) { return 0; } lora_module_t lora_module_rhf76 = { - .init = rhf76_init, - .join_otaa = rhf76_join_otaa, - .join_abp = rhf76_join_abp, - .send = rhf76_send, - .close = rhf76_close, - .recv_callback = K_NULL, + .init = rhf76_init, + .join_otaa = rhf76_join_otaa, + .join_abp = rhf76_join_abp, + .send = rhf76_send, + .send_unconfirmed = rhf76_send_unconfirmed, + .close = rhf76_close, + .recv_callback = K_NULL, }; int rhf76_lora_init(hal_uart_port_t uart_port) diff --git a/devices/rhf76_lora/RHF76.h b/devices/rhf76_lora/RHF76.h index 79cf1dea..f5a2ec34 100644 --- a/devices/rhf76_lora/RHF76.h +++ b/devices/rhf76_lora/RHF76.h @@ -65,7 +65,10 @@ typedef enum lora_key_type { #define RHF76_ATCMD_FMT_SET_KEY_TYPE_APPKEY "AT+KEY=\"appkey\",%s\r\n" #define RHF76_ATCMD_FMT_SET_KEY_TYPE_APPSKEY "AT+KEY=\"appskey\",%s\r\n" #define RHF76_ATCMD_FMT_SET_KEY_TYPE_NWKSKEY "AT+KEY=\"nwkskey\",%s\r\n" -#define RHF76_ATCMD_FMT_SEND_MSGHEX "AT+CMSGHEX=\"%s\"\r\n" +#define RHF76_ATCMD_FMT_SEND_CMSGHEX "AT+CMSGHEX=\"%s\"\r\n" +#define RHF76_ATCMD_FMT_SEND_MSGHEX "AT+MSGHEX=\"%s\"\r\n" + +#define RHF76_ATCMD_SET_REPT "AT+REPT=%d\r\n" #define RHF76_ATCMD_SET_BAND_CN470 "AT+DR=CN470\r\n" #define RHF76_ATCMD_REPLY_BAND_CN470 "+DR: CN470" diff --git a/net/lora_module_wrapper/lora_module_wrapper.c b/net/lora_module_wrapper/lora_module_wrapper.c index 82d64ded..a5811c0d 100644 --- a/net/lora_module_wrapper/lora_module_wrapper.c +++ b/net/lora_module_wrapper/lora_module_wrapper.c @@ -44,6 +44,14 @@ int tos_lora_module_send(const void *buf, size_t len) return -1; } +int tos_lora_module_send_unconfirmed(const void *buf, size_t len) +{ + if (g_lora_module && g_lora_module->send_unconfirmed) { + return g_lora_module->send_unconfirmed(buf, len); + } + return -1; +} + int tos_lora_module_recvcb_register(lora_recv_callback_t recv_callback) { if (g_lora_module) { diff --git a/net/lora_module_wrapper/lora_module_wrapper.h b/net/lora_module_wrapper/lora_module_wrapper.h index b5d95c1d..509e3379 100644 --- a/net/lora_module_wrapper/lora_module_wrapper.h +++ b/net/lora_module_wrapper/lora_module_wrapper.h @@ -32,6 +32,8 @@ typedef struct lora_module_st { int (*send)(const void *buf, size_t len); + int (*send_unconfirmed)(const void *buf, size_t len); + int (*close)(void); lora_recv_callback_t recv_callback; @@ -87,6 +89,18 @@ int tos_lora_module_join_abp(const char *deveui, const char *devaddr, const char */ int tos_lora_module_send(const void *buf, size_t len); +/** + * @brief Send unconfirmed data (message) by lora module. + * + * @attention None + * + * @param[in] buf data to send + * @param[in] len length of the data + * + * @return errcode + */ +int tos_lora_module_send_unconfirmed(const void *buf, size_t len); + /** * @brief Register a Receive callback method by lora module. *