Compare commits

...

23 Commits

Author SHA1 Message Date
chunyexixiaoyu 909f3cc635 APP_Framework/Framework/: solve the problem the rebase cause. 2 weeks ago
xuedongliang 33e3b798b7 feat add watchdog on xidatong board from Liu_Weichao 2 weeks ago
xuedongliang 3c4a7c8f0a update rt-thread bsp(xidatong and aiit-arm32-board) from Tian_Chunyu 2 weeks ago
Liu_Weichao 67666c1707 feat add watchdog on xidatong board 2 weeks ago
yl1185 cb134029d2 aiit_board/aiit-arm32-board/ add test jpg 2 weeks ago
yl1185 ef700c0611 connection/bluetooth/ BT verify 2 weeks ago
yl1185 d8d11ed58e Framework/connection/bluetooth/ BT verify 2 weeks ago
chunyexixiaoyu 3c69cb8d14 Ubiquitous/RT-Thread_Fusion_XiUOS/: add reboot function 3 weeks ago
chunyexixiaoyu f3d7e7e3d4 Ubiquitous/RT-Thread_Fusion_XiUOS/: add romfs function 3 weeks ago
chunyexixiaoyu c271cd16ea Ubiquitous/RT-Thread_Fusion_XiUOS/: solve the problem that output bin bigger than 550MB 4 weeks ago
chunyexixiaoyu 279757406e APP_Framework/Framework/:solve the bug that compiling when the macro SUPPORT_CONNECTION_FRAMEWORK ' was not been chose 4 weeks ago
yl1185 525d9d6f11 connection/bluetooth/bluetooth verify 4 weeks ago
yl1185 ec446bb44c connection/wifi/wifi verify 4 weeks ago
yl1185 50995f3c1f Framework/connection/wifi verify 4 weeks ago
yl1185 bf93a554ca Framework/connection/adapter SConscript 4 weeks ago
yl1185 8349aa1aa7 Framework/connection/wifi verify 4 weeks ago
xuedongliang d04059e356 optimize adapter lora client and gateway function from Liu_Weichao 3 weeks ago
Liu_Weichao 46291764d4 Merge branch 'prepare_for_master' of https://git.trustie.net/xuos/xiuos into xidatong 3 weeks ago
Liu_Weichao cc88f22f14 feat make sure only one lora client connect to the lora gateway at one time 3 weeks ago
Liu_Weichao a58e744c45 optimize adapter lora send-receive and add recv-data-error solution 3 weeks ago
Liu_Weichao 32bce719f6 1、set e220 baud rate 115200;2、add adapter lora client work condition(1:gateway send cmd;2:update automatically。)。 3 weeks ago
Liu_Weichao 03e120dd0c optimize adapter_lora gateway and client data transfer 3 weeks ago
Liu_Weichao 808f00ade1 feat add sem obtain wait_time and modify e220 lora receive len operation 3 weeks ago
  1. 21
      APP_Framework/Framework/connection/SConscript
  2. 10
      APP_Framework/Framework/connection/adapter.c
  3. 3
      APP_Framework/Framework/connection/adapter.h
  4. 21
      APP_Framework/Framework/connection/adapter_agent.c
  5. 14
      APP_Framework/Framework/connection/at_agent.h
  6. 18
      APP_Framework/Framework/connection/bluetooth/SConscript
  7. 13
      APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c
  8. 6
      APP_Framework/Framework/connection/bluetooth/hc08/Kconfig
  9. 10
      APP_Framework/Framework/connection/bluetooth/hc08/SConscript
  10. 34
      APP_Framework/Framework/connection/bluetooth/hc08/hc08.c
  11. 4
      APP_Framework/Framework/connection/lora/Kconfig
  12. 704
      APP_Framework/Framework/connection/lora/adapter_lora.c
  13. 42
      APP_Framework/Framework/connection/lora/e220/e220.c
  14. 18
      APP_Framework/Framework/connection/wifi/SConscript
  15. 72
      APP_Framework/Framework/connection/wifi/adapter_wifi.c
  16. 4
      APP_Framework/Framework/connection/wifi/hfa21_wifi/Kconfig
  17. 10
      APP_Framework/Framework/connection/wifi/hfa21_wifi/SConscript
  18. 63
      APP_Framework/Framework/connection/wifi/hfa21_wifi/hfa21_wifi.c
  19. 1
      APP_Framework/Framework/transform_layer/xizi/transform.c
  20. 5
      APP_Framework/Framework/transform_layer/xizi/transform.h
  21. 26
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/aiit-arm32-board/.config
  22. 11
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/aiit-arm32-board/rtconfig.h
  23. BIN
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/aiit-arm32-board/test/IMG_1079.HEIC.JPG.JPG
  24. BIN
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/aiit-arm32-board/test/IMG_1080.PNG.JPG
  25. 2
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/.config
  26. 3
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/board/SConscript
  27. 7
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/board/board.c
  28. 2
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/board/linker_scripts/link.lds
  29. 20
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/board/ports/mnt_romfs.c
  30. 18
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/board/ports/romfs.c
  31. 7
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/board/ports/sdcard_port.c
  32. 1
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/rtconfig.h
  33. 3
      Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong/rtconfig.py
  34. 11
      Ubiquitous/XiZi/board/aiit-arm32-board/third_party_driver/uart/connect_usart.c
  35. 2
      Ubiquitous/XiZi/board/aiit-arm32-board/third_party_driver/watchdog/connect_wdg.c
  36. 17
      Ubiquitous/XiZi/board/aiit-riscv64-board/third_party_driver/uart/connect_uart.c
  37. 11
      Ubiquitous/XiZi/board/cortex-m0-emulator/connect_uart.c
  38. 11
      Ubiquitous/XiZi/board/cortex-m3-emulator/connect_uart.c
  39. 11
      Ubiquitous/XiZi/board/cortex-m4-emulator/third_party_driver/uart/connect_usart.c
  40. 11
      Ubiquitous/XiZi/board/gapuino/third_party_driver/uart/connect_uart.c
  41. 15
      Ubiquitous/XiZi/board/gd32vf103_rvstar/third_party_driver/uart/connect_uart.c
  42. 11
      Ubiquitous/XiZi/board/hifive1-emulator/third_party_driver/connect_usart.c
  43. 11
      Ubiquitous/XiZi/board/hifive1-rev-B/third_party_driver/connect_usart.c
  44. 11
      Ubiquitous/XiZi/board/k210-emulator/third_party_driver/uart/connect_uart.c
  45. 17
      Ubiquitous/XiZi/board/kd233/third_party_driver/uart/connect_uart.c
  46. 17
      Ubiquitous/XiZi/board/maix-go/third_party_driver/uart/connect_uart.c
  47. 11
      Ubiquitous/XiZi/board/nuvoton-m2354/third_party_driver/uart/connect_uart.c
  48. 11
      Ubiquitous/XiZi/board/ok1052-c/third_party_driver/uart/connect_uart.c
  49. 16
      Ubiquitous/XiZi/board/rv32m1_vega/third_party_driver/uart/connect_uart.c
  50. 11
      Ubiquitous/XiZi/board/stm32f103-nano/third_party_driver/uart/connect_uart.c
  51. 11
      Ubiquitous/XiZi/board/stm32f407-st-discovery/third_party_driver/uart/connect_usart.c
  52. 11
      Ubiquitous/XiZi/board/stm32f407zgt6/third_party_driver/uart/connect_usart.c
  53. 7
      Ubiquitous/XiZi/board/xidatong/board.c
  54. 8
      Ubiquitous/XiZi/board/xidatong/third_party_driver/Kconfig
  55. 4
      Ubiquitous/XiZi/board/xidatong/third_party_driver/Makefile
  56. 11
      Ubiquitous/XiZi/board/xidatong/third_party_driver/ch438/connect_ch438.c
  57. 38
      Ubiquitous/XiZi/board/xidatong/third_party_driver/include/connect_wdt.h
  58. 312
      Ubiquitous/XiZi/board/xidatong/third_party_driver/include/fsl_wdog.h
  59. 11
      Ubiquitous/XiZi/board/xidatong/third_party_driver/uart/connect_uart.c
  60. 13
      Ubiquitous/XiZi/board/xidatong/third_party_driver/wdt/Kconfig
  61. 3
      Ubiquitous/XiZi/board/xidatong/third_party_driver/wdt/Makefile
  62. 182
      Ubiquitous/XiZi/board/xidatong/third_party_driver/wdt/connect_wdt.c
  63. 206
      Ubiquitous/XiZi/board/xidatong/third_party_driver/wdt/fsl_wdog.c
  64. 16
      Ubiquitous/XiZi/kernel/kernel_test/test_iwg.c
  65. 3
      Ubiquitous/XiZi/kernel/thread/console.c
  66. 8
      Ubiquitous/XiZi/kernel/thread/init.c
  67. 7
      Ubiquitous/XiZi/kernel/thread/msgqueue.c
  68. 6
      Ubiquitous/XiZi/kernel/thread/semaphore.c
  69. 4
      Ubiquitous/XiZi/resources/include/bus.h
  70. 1
      Ubiquitous/XiZi/resources/include/bus_serial.h
  71. 2
      Ubiquitous/XiZi/resources/include/dev_serial.h
  72. 4
      Ubiquitous/XiZi/resources/serial/dev_serial.c

21
APP_Framework/Framework/connection/SConscript

@ -0,0 +1,21 @@
import os
Import('RTT_ROOT')
Import('rtconfig')
from building import *
cwd = GetCurrentDir()
SOURCES = []
if GetDepend(['SUPPORT_CONNECTION_FRAMEWORK']):
SOURCES = ['adapter.c'] +['adapter_agent.c']+ SOURCES
path = [cwd]
objs = []
group = DefineGroup('connection', SOURCES, depend = [], CPPPATH = [cwd])
objs = objs + group
list = os.listdir(cwd)
if GetDepend(['SUPPORT_CONNECTION_FRAMEWORK']):
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(path, 'SConscript'))
Return('objs')

10
APP_Framework/Framework/connection/adapter.c

@ -21,9 +21,11 @@
#include <adapter.h>
static DoublelistType adapter_list;
#ifdef ADD_XIZI_FETURES
static int adapter_list_lock;
#else
static pthread_mutex_t adapter_list_lock;
#endif
/**
* @description: Init adapter framework
* @return 0
@ -481,6 +483,7 @@ int AdapterDeviceDisconnect(struct Adapter *adapter, unsigned char *priv_net_gro
*/
int AdapterDeviceSetUp(struct Adapter *adapter)
{
if (!adapter)
return -1;
@ -488,10 +491,10 @@ int AdapterDeviceSetUp(struct Adapter *adapter)
struct IpProtocolDone *ip_done = NULL;
struct PrivProtocolDone *priv_done = NULL;
switch (adapter->net_protocol)
{
case PRIVATE_PROTOCOL:
priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->setup)
return 0;
@ -515,6 +518,7 @@ int AdapterDeviceSetUp(struct Adapter *adapter)
return 0;
result = ip_done->setup(adapter);
if (0 == result) {
printf("Device %s setup success.\n", adapter->name);
adapter->adapter_status = INSTALL;

3
APP_Framework/Framework/connection/adapter.h

@ -190,6 +190,9 @@ struct Adapter
void *done;
void *adapter_param;
sem_t sem;
pthread_mutex_t lock;
struct DoublelistNode link;
};

21
APP_Framework/Framework/connection/adapter_agent.c

@ -29,7 +29,9 @@
#ifdef ADD_XIZI_FETURES
# include <user_api.h>
#endif
#ifdef ADD_RTTHREAD_FETURES
#include <rtthread.h>
#endif
#define AT_CMD_MAX_LEN 128
#define AT_AGENT_MAX 2
static char send_buf[AT_CMD_MAX_LEN];
@ -139,7 +141,7 @@ int ATOrderSend(ATAgentType agent, uint32_t timeout_s, ATReplyType reply, const
PrivMutexObtain(&agent->lock);
agent->receive_mode = AT_MODE;
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
agent->maintain_len = 0;
@ -300,7 +302,7 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
//PrivTaskDelay(1000);
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) {
printf("wait sem[%d] timeout\n",agent->entm_rx_notice);
return -ERROR;
return -1;
}
PrivMutexObtain(&agent->lock);
@ -390,7 +392,7 @@ static int GetCompleteATReply(ATAgentType agent)
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
agent->maintain_len = 0;
PrivMutexAbandon(&agent->lock);
return -ERROR;
return -1;
}
printf("GetCompleteATReply done\n");
@ -437,6 +439,7 @@ int DeleteATAgent(ATAgentType agent)
printf("delete agent lock = %d\n",agent->lock.sem.semcount);
PrivMutexDelete(&agent->lock);
}
#elif defined ADD_RTTHREAD_FETURES
#else
if (agent->lock) {
printf("delete agent lock = %d\n",agent->lock);
@ -448,12 +451,12 @@ int DeleteATAgent(ATAgentType agent)
printf("delete agent entm_rx_notice = %d\n",agent->entm_rx_notice);
PrivSemaphoreDelete(&agent->entm_rx_notice);
}
if (agent->rsp_sem) {
printf("delete agent rsp_sem = %d\n",agent->rsp_sem);
PrivSemaphoreDelete(&agent->rsp_sem);
#ifdef ADD_XIZI_FETURES
if (agent->rsp_sem) {
printf("delete agent rsp_sem = %d\n",agent->rsp_sem);
PrivSemaphoreDelete(&agent->rsp_sem);
}
#endif
if (agent->maintain_buffer) {
PrivFree(agent->maintain_buffer);
}

14
APP_Framework/Framework/connection/at_agent.h

@ -42,7 +42,6 @@ struct ATReply
uint32 reply_len;
};
typedef struct ATReply *ATReplyType;
struct ATAgent
{
char agent_name[64];
@ -53,10 +52,10 @@ struct ATAgent
uint32 maintain_len;
uint32 maintain_max;
#ifdef ADD_NUTTX_FETURES
pthread_mutex_t lock;
#else
#ifdef ADD_XIZI_FETURES
int lock;
#else
pthread_mutex_t lock;
#endif
ATReplyType reply;
@ -64,8 +63,11 @@ struct ATAgent
char reply_end_last_char;
char reply_end_char;
uint32 reply_char_num;
int rsp_sem;
#ifdef ADD_XIZI_FETURES
int rsp_sem;
#else
sem_t rsp_sem;
#endif
pthread_t at_handler;
#define ENTM_RECV_MAX 256

18
APP_Framework/Framework/connection/bluetooth/SConscript

@ -0,0 +1,18 @@
import os
Import('RTT_ROOT')
from building import *
SOURCES = []
SOURCES = ['adapter_bluetooth.c'] + SOURCES
objs = []
cwd = GetCurrentDir()
path = [cwd]
group = DefineGroup('bluetooth', SOURCES, depend = [], CPPPATH = [cwd])
objs = objs + group
list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(path, 'SConscript'))
Return('objs')

13
APP_Framework/Framework/connection/bluetooth/adapter_bluetooth.c

@ -78,13 +78,13 @@ int AdapterBlueToothInit(void)
return ret;
}
/******************4G TEST*********************/
/******************BT TEST*********************/
int AdapterBlueToothTest(void)
{
const char *bluetooth_msg = "BT Adapter Test";
char bluetooth_recv_msg[128];
int len;
int baud_rate = BAUD_RATE_115200;
int baud_rate = BAUD_RATE_9600;
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_BLUETOOTH_NAME);
@ -99,7 +99,7 @@ int AdapterBlueToothTest(void)
printf("bluetooth_recv_msg %s\n", bluetooth_recv_msg);
AdapterDeviceSend(adapter, bluetooth_msg, len);
printf("send %s after recv\n", bluetooth_msg);
PrivTaskDelay(100);
PrivTaskDelay(1000);
memset(bluetooth_recv_msg, 0, 128);
}
@ -107,4 +107,9 @@ int AdapterBlueToothTest(void)
return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, AdapterBlueToothTest, AdapterBlueToothTest, show adapter BT information);
#ifdef ADD_RTTHREAD_FETURES
MSH_CMD_EXPORT(AdapterBlueToothTest,a bt adpter sample);
#endif
#ifdef ADD_XIZI_FETURES
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, AdapterWifiTest, AdapterWifiTest, show adapter wifi information);
#endif

6
APP_Framework/Framework/connection/bluetooth/hc08/Kconfig

@ -36,5 +36,9 @@ if ADD_NUTTX_FETURES
endif
if ADD_RTTHREAD_FETURES
config ADAPTER_HC08_DRIVER
string "HC08 device uart driver path"
default "/dev/uart4"
config ADAPTER_HC08_WORK_ROLE
string "HC08 work role M(MASTER) or S(SLAVER)"
endif

10
APP_Framework/Framework/connection/bluetooth/hc08/SConscript

@ -0,0 +1,10 @@
from building import *
import os
cwd = GetCurrentDir()
src = []
if GetDepend(['ADAPTER_HC08']):
src += ['hc08.c']
group = DefineGroup('connection bluetooth hc08', src, depend = [], CPPPATH = [cwd])
Return('group')

34
APP_Framework/Framework/connection/bluetooth/hc08/hc08.c

@ -92,7 +92,7 @@ static int Hc08AtConfigure(ATAgentType agent, enum Hc08AtCmd hc08_at_cmd, void *
ATReplyType reply = CreateATReply(64);
if (NULL == reply) {
printf("Hc08AtConfigure CreateATReply failed !\n");
return -ENOMEMORY;
return -5;
}
switch (hc08_at_cmd)
@ -152,20 +152,20 @@ static int Hc08AtConfigure(ATAgentType agent, enum Hc08AtCmd hc08_at_cmd, void *
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_ROLE_CMD);
reply_ok_flag = 0;
break;
// case HC08_AT_CMD_SET_ADDR:
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_SET_ROLE_CMD);
// break;
// case HC08_AT_CMD_GET_ADDR:
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_ROLE_CMD);
// reply_ok_flag = 0;
// break;
// case HC08_AT_CMD_SET_NAME:
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_SET_NAME_CMD);
// break;
// case HC08_AT_CMD_GET_NAME:
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_NAME_CMD);
// reply_ok_flag = 0;
// break;
case HC08_AT_CMD_SET_ADDR:
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_SET_ROLE_CMD);
break;
case HC08_AT_CMD_GET_ADDR:
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_ROLE_CMD);
reply_ok_flag = 0;
break;
case HC08_AT_CMD_SET_NAME:
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_SET_NAME_CMD);
break;
case HC08_AT_CMD_GET_NAME:
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_NAME_CMD);
reply_ok_flag = 0;
break;
default:
printf("hc08 do not support no.%d cmd\n", hc08_at_cmd);
DeleteATReply(reply);
@ -209,7 +209,7 @@ static int Hc08Open(struct Adapter *adapter)
if (!adapter->agent) {
char *agent_name = "bluetooth_uart_client";
printf("InitATAgent agent_name %s fd %u\n", agent_name, adapter->fd);
if (EOK != InitATAgent(agent_name, adapter->fd, 512)) {
if (0 != InitATAgent(agent_name, adapter->fd, 512)) {
printf("at agent init failed !\n");
return -1;
}
@ -343,7 +343,7 @@ static int Hc08Connect(struct Adapter *adapter, enum NetRoleType net_role, const
static int Hc08Send(struct Adapter *adapter, const void *buf, size_t len)
{
x_err_t result = EOK;
long result = 0;
if (adapter->agent) {
EntmSend(adapter->agent, (const char *)buf, len);
} else {

4
APP_Framework/Framework/connection/lora/Kconfig

@ -17,6 +17,10 @@ choice
bool "config as a client"
endchoice
config ADAPTER_LORA_CLIENT_NUM
int "Lora net client num"
default "20"
if AS_LORA_GATEWAY_ROLE
config ADAPTER_LORA_NET_ROLE_ID
hex "if Lora device config as a gateway, set gateway net id"

704
APP_Framework/Framework/connection/lora/adapter_lora.c

@ -28,9 +28,15 @@ extern AdapterProductInfoType Sx1278Attach(struct Adapter *adapter);
extern AdapterProductInfoType E220Attach(struct Adapter *adapter);
#endif
#define ADAPTER_LORA_CLIENT_NUM 255
#define ADAPTER_LORA_DATA_LENGTH 256
#define ADAPTER_LORA_RECV_DATA_LENGTH ADAPTER_LORA_DATA_LENGTH + 16
//#define CLIENT_UPDATE_MODE
#define GATEWAY_CMD_MODE
//Client num index 1-ADAPTER_LORA_CLIENT_NUM
//#define ADAPTER_LORA_CLIENT_NUM 20
//LORA single transfer data max size 128 bytes: data format 16 bytes and user data 112 bytes
#define ADAPTER_LORA_DATA_LENGTH 112
#define ADAPTER_LORA_TRANSFER_DATA_LENGTH ADAPTER_LORA_DATA_LENGTH + 16
#define ADAPTER_LORA_DATA_HEAD 0x3C
#define ADAPTER_LORA_NET_PANID 0x0102
@ -40,6 +46,9 @@ extern AdapterProductInfoType E220Attach(struct Adapter *adapter);
#define ADAPTER_LORA_DATA_TYPE_QUIT_REPLY 0x0D
#define ADAPTER_LORA_DATA_TYPE_USERDATA 0x0E
#define ADAPTER_LORA_DATA_TYPE_CMD 0x0F
#define ADAPTER_LORA_DATA_END 0x5A
#define ADAPTER_LORA_RECEIVE_ERROR_CNT 1
//need to change status if the lora client wants to quit the net when timeout or a certain event
//eg.can also use sem to trigger quit function
@ -66,28 +75,26 @@ enum LoraGatewayState {
LORA_RECV_DATA,
};
static uint8 LoraGatewayState = LORA_STATE_IDLE;
struct LoraGatewayParam
{
uint8 gateway_id;
uint16 panid;
uint8 client_id[ADAPTER_LORA_CLIENT_NUM];
uint8_t gateway_id;
uint16_t panid;
uint8_t client_id[ADAPTER_LORA_CLIENT_NUM];
int client_num;
int receive_data_sem;
};
struct LoraClientParam
{
uint8 client_id;
uint16 panid;
uint8_t client_id;
uint16_t panid;
enum ClientState client_state;
uint8 gateway_id;
uint8_t gateway_id;
};
/*LoraDataFormat:
**flame_head : 0x3C
**flame_head : 0x3C3C
**length : sizeof(struct LoraDataFormat)
**panid : 0x0102
**client_id : 0x010x020x03...
@ -95,23 +102,31 @@ struct LoraClientParam
**data_type : 0x0A(join net, client->gateway)0x0B(join net reply, gateway->client)
0x0C(user data, client->gateway)0x0D(user data cmd, gateway->client)
**data : user data
**crc16 : CRC 16 check data
**flame_end : 0x5A5A
*/
struct LoraDataFormat
{
uint8 flame_head;
uint8 reserved[3];
uint32 length;
uint16 panid;
uint8 client_id;
uint8 gateway_id;
uint16_t flame_head;
uint16_t flame_index;
uint32_t length;
uint16_t panid;
uint8_t client_id;
uint8_t gateway_id;
uint16 data_type;
uint8 data[ADAPTER_LORA_DATA_LENGTH];
uint16_t data_type;
uint8_t data[ADAPTER_LORA_DATA_LENGTH];
uint16 crc16;
uint16_t flame_end;
};
uint8_t lora_recv_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH];
struct LoraDataFormat client_recv_data_format[ADAPTER_LORA_CLIENT_NUM];
static sem_t gateway_recv_data_sem;
struct LoraDataFormat gateway_recv_data_format;
static int recv_error_cnt = 0;
/*******************LORA MESH FUNCTION***********************/
/**
@ -119,10 +134,10 @@ struct LoraDataFormat
* @param data input data buffer
* @param length input data buffer minus check code
*/
static uint16 LoraCrc16(uint8 *data, uint16 length)
static uint16_t LoraCrc16(uint8_t *data, uint16_t length)
{
int j;
uint16 crc_data = 0xFFFF;
uint16_t crc_data = 0xFFFF;
while (length--) {
crc_data ^= *data++;
@ -142,10 +157,10 @@ static uint16 LoraCrc16(uint8 *data, uint16 length)
* @param data input data buffer
* @param length input data buffer minus check code
*/
static int LoraCrc16Check(uint8 *data, uint16 length)
static int LoraCrc16Check(uint8_t *data, uint16_t length)
{
uint16 crc_data;
uint16 input_data = (((uint16)data[length - 1] << 8) & 0xFF00) | ((uint16)data[length - 2] & 0x00FF);
uint16_t crc_data;
uint16_t input_data = (((uint16_t)data[length - 1] << 8) & 0xFF00) | ((uint16_t)data[length - 2] & 0x00FF);
crc_data = LoraCrc16(data, length - 2);
@ -155,29 +170,6 @@ static int LoraCrc16Check(uint8 *data, uint16 length)
return -1;
}
/**
* @description: Lora receive data check
* @param data receive data buffer
* @param length receive data length
* @param recv_data LoraDataFormat data
*/
static int LoraReceiveDataCheck(uint8 *data, uint16 length, struct LoraDataFormat *recv_data)
{
int i;
uint32 recv_data_length = 0;
for ( i = 0; i < length; i ++) {
if (ADAPTER_LORA_DATA_HEAD == data[i]) {
recv_data_length = (data[i + 4] & 0xFF) | ((data[i + 5] & 0xFF) << 8) | ((data[i + 6] & 0xFF) << 16) | ((data[i + 7] & 0xFF) << 24);
if (sizeof(struct LoraDataFormat) == recv_data_length) {
memcpy(recv_data, (uint8 *)(data + i), sizeof(struct LoraDataFormat));
return 0;
}
}
}
return -1;
}
/**
* @description: Lora Gateway reply connect request to Client
* @param adapter Lora adapter pointer
@ -187,10 +179,13 @@ static int LoraGatewayReply(struct Adapter *adapter, struct LoraDataFormat *gate
{
int i;
int client_join_flag = 0;
uint16_t gateway_data_type;
uint32_t gateway_reply_length = ADAPTER_LORA_TRANSFER_DATA_LENGTH;
struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)adapter->adapter_param;
struct LoraDataFormat gateway_reply_data;
memset(&gateway_reply_data, 0, sizeof(struct LoraDataFormat));
uint8_t gateway_reply_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH];
memset(gateway_reply_data, 0, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) {
@ -203,23 +198,35 @@ static int LoraGatewayReply(struct Adapter *adapter, struct LoraDataFormat *gate
}
if (!client_join_flag) {
if (gateway->client_num > 6) {
printf("Lora gateway only support 6(max) client\n");
if (gateway->client_num > ADAPTER_LORA_CLIENT_NUM) {
printf("Lora gateway only support %u(max) client\n", ADAPTER_LORA_CLIENT_NUM);
gateway->client_num = 0;
}
gateway->client_id[gateway->client_num] = gateway_recv_data->client_id;
gateway->client_num ++;
}
gateway_reply_data.flame_head = ADAPTER_LORA_DATA_HEAD;
gateway_reply_data.length = sizeof(struct LoraDataFormat);
gateway_reply_data.panid = gateway->panid;
gateway_reply_data.data_type = ADAPTER_LORA_DATA_TYPE_JOIN_REPLY;
gateway_reply_data.client_id = gateway_recv_data->client_id;
gateway_reply_data.gateway_id = gateway->gateway_id;
gateway_reply_data.crc16 = LoraCrc16((uint8 *)&gateway_reply_data, sizeof(struct LoraDataFormat) - 2);
if (AdapterDeviceSend(adapter, (uint8 *)&gateway_reply_data, gateway_reply_data.length) < 0) {
gateway_data_type = ADAPTER_LORA_DATA_TYPE_JOIN_REPLY;
gateway_reply_data[0] = ADAPTER_LORA_DATA_HEAD;
gateway_reply_data[1] = ADAPTER_LORA_DATA_HEAD;
gateway_reply_data[2] = 0;
gateway_reply_data[3] = 0;
gateway_reply_data[4] = (gateway_reply_length >> 24) & 0xFF;
gateway_reply_data[5] = (gateway_reply_length >> 16) & 0xFF;
gateway_reply_data[6] = (gateway_reply_length >> 8) & 0xFF;
gateway_reply_data[7] = gateway_reply_length & 0xFF;
gateway_reply_data[8] = (gateway->panid >> 8) & 0xFF;
gateway_reply_data[9] = gateway->panid & 0xFF;
gateway_reply_data[10] = gateway_recv_data->client_id;
gateway_reply_data[11] = gateway->gateway_id;
gateway_reply_data[12] = (gateway_data_type >> 8) & 0xFF;
gateway_reply_data[13] = gateway_data_type & 0xFF;
gateway_reply_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 2] = ADAPTER_LORA_DATA_END;
gateway_reply_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 1] = ADAPTER_LORA_DATA_END;
if (AdapterDeviceSend(adapter, gateway_reply_data, ADAPTER_LORA_TRANSFER_DATA_LENGTH) < 0) {
return -1;
}
@ -233,15 +240,27 @@ static int LoraGatewayReply(struct Adapter *adapter, struct LoraDataFormat *gate
}
}
gateway_reply_data.flame_head = ADAPTER_LORA_DATA_HEAD;
gateway_reply_data.length = sizeof(struct LoraDataFormat);
gateway_reply_data.panid = gateway->panid;
gateway_reply_data.data_type = ADAPTER_LORA_DATA_TYPE_QUIT_REPLY;
gateway_reply_data.client_id = gateway_recv_data->client_id;
gateway_reply_data.gateway_id = gateway->gateway_id;
gateway_reply_data.crc16 = LoraCrc16((uint8 *)&gateway_reply_data, sizeof(struct LoraDataFormat) - 2);
if (AdapterDeviceSend(adapter, (uint8 *)&gateway_reply_data, gateway_reply_data.length) < 0) {
gateway_data_type = ADAPTER_LORA_DATA_TYPE_QUIT_REPLY;
gateway_reply_data[0] = ADAPTER_LORA_DATA_HEAD;
gateway_reply_data[1] = ADAPTER_LORA_DATA_HEAD;
gateway_reply_data[2] = 0;
gateway_reply_data[3] = 0;
gateway_reply_data[4] = (gateway_reply_length >> 24) & 0xFF;
gateway_reply_data[5] = (gateway_reply_length >> 16) & 0xFF;
gateway_reply_data[6] = (gateway_reply_length >> 8) & 0xFF;
gateway_reply_data[7] = gateway_reply_length & 0xFF;
gateway_reply_data[8] = (gateway->panid >> 8) & 0xFF;
gateway_reply_data[9] = gateway->panid & 0xFF;
gateway_reply_data[10] = gateway_recv_data->client_id;
gateway_reply_data[11] = gateway->gateway_id;
gateway_reply_data[12] = (gateway_data_type >> 8) & 0xFF;
gateway_reply_data[13] = gateway_data_type & 0xFF;
gateway_reply_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 2] = ADAPTER_LORA_DATA_END;
gateway_reply_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 1] = ADAPTER_LORA_DATA_END;
if (AdapterDeviceSend(adapter, gateway_reply_data, ADAPTER_LORA_TRANSFER_DATA_LENGTH) < 0) {
return -1;
}
@ -257,22 +276,36 @@ static int LoraGatewayReply(struct Adapter *adapter, struct LoraDataFormat *gate
* @param client_id Lora Client id
* @param cmd Lora cmd
*/
static int LoraGatewaySendCmd(struct Adapter *adapter, unsigned char client_id, int cmd)
static int LoraGatewaySendCmd(struct Adapter *adapter, uint8_t client_id, uint16_t cmd)
{
struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)adapter->adapter_param;
struct LoraDataFormat gateway_cmd_data;
memset(&gateway_cmd_data, 0, sizeof(struct LoraDataFormat));
gateway_cmd_data.flame_head = ADAPTER_LORA_DATA_HEAD;
gateway_cmd_data.length = sizeof(struct LoraDataFormat);
gateway_cmd_data.panid = gateway->panid;
gateway_cmd_data.data_type = cmd;
gateway_cmd_data.client_id = client_id;
gateway_cmd_data.gateway_id = gateway->gateway_id;
gateway_cmd_data.crc16 = LoraCrc16((uint8 *)&gateway_cmd_data, sizeof(struct LoraDataFormat) - 2);
uint16_t gateway_cmd_type;
uint32_t gateway_cmd_length = ADAPTER_LORA_TRANSFER_DATA_LENGTH;
uint8_t gateway_cmd_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH];
memset(gateway_cmd_data, 0, sizeof(struct LoraDataFormat));
gateway_cmd_type = cmd;
gateway_cmd_data[0] = ADAPTER_LORA_DATA_HEAD;
gateway_cmd_data[1] = ADAPTER_LORA_DATA_HEAD;
gateway_cmd_data[2] = 0;
gateway_cmd_data[3] = 0;
gateway_cmd_data[4] = (gateway_cmd_length >> 24) & 0xFF;
gateway_cmd_data[5] = (gateway_cmd_length >> 16) & 0xFF;
gateway_cmd_data[6] = (gateway_cmd_length >> 8) & 0xFF;
gateway_cmd_data[7] = gateway_cmd_length & 0xFF;
gateway_cmd_data[8] = (gateway->panid >> 8) & 0xFF;
gateway_cmd_data[9] = gateway->panid & 0xFF;
gateway_cmd_data[10] = client_id;
gateway_cmd_data[11] = gateway->gateway_id;
gateway_cmd_data[12] = (gateway_cmd_type >> 8) & 0xFF;
gateway_cmd_data[13] = gateway_cmd_type & 0xFF;
gateway_cmd_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 2] = ADAPTER_LORA_DATA_END;
gateway_cmd_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 1] = ADAPTER_LORA_DATA_END;
if (AdapterDeviceSend(adapter, (uint8 *)&gateway_cmd_data, gateway_cmd_data.length) < 0) {
if (AdapterDeviceSend(adapter, gateway_cmd_data, ADAPTER_LORA_TRANSFER_DATA_LENGTH) < 0) {
return -1;
}
@ -289,6 +322,8 @@ static int LoraGatewayHandleData(struct Adapter *adapter, struct LoraDataFormat
/*User needs to handle client data depends on the requirement*/
printf("Lora Gateway receive Client %d data:\n", gateway_recv_data->client_id);
printf("%s\n", gateway_recv_data->data);
PrivSemaphoreAbandon(&gateway_recv_data_sem);
return 0;
}
@ -321,8 +356,9 @@ static int LoraClientUpdate(struct Adapter *adapter, struct LoraDataFormat *clie
* @param adapter Lora adapter pointer
* @param send_buf Lora Client send user data buf
* @param length user data length (max size is ADAPTER_LORA_DATA_LENGTH)
* @param send_index user data index, max size ADAPTER_LORA_DATA_LENGTH single index
*/
static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int length)
static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int length, uint16_t send_index)
{
struct LoraClientParam *client = (struct LoraClientParam *)adapter->adapter_param;
@ -336,22 +372,35 @@ static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int lengt
return 0;
}
struct LoraDataFormat client_user_data;
memset(&client_user_data, 0, sizeof(struct LoraDataFormat));
client_user_data.flame_head = ADAPTER_LORA_DATA_HEAD;
client_user_data.length = sizeof(struct LoraDataFormat);
client_user_data.panid = client->panid;
client_user_data.data_type = ADAPTER_LORA_DATA_TYPE_USERDATA;
client_user_data.client_id = client->client_id;
client_user_data.gateway_id = client->gateway_id;
memcpy(client_user_data.data, (uint8 *)send_buf, length);
client_user_data.crc16 = LoraCrc16((uint8 *)&client_user_data, sizeof(struct LoraDataFormat) - 2);
uint16_t client_user_type;
uint32_t client_user_length = ADAPTER_LORA_TRANSFER_DATA_LENGTH;
uint8_t client_user_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH];
memset(client_user_data, 0, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
client_user_type = ADAPTER_LORA_DATA_TYPE_USERDATA;
client_user_data[0] = ADAPTER_LORA_DATA_HEAD;
client_user_data[1] = ADAPTER_LORA_DATA_HEAD;
client_user_data[2] = (send_index >> 8) & 0xFF;
client_user_data[3] = send_index & 0xFF;
client_user_data[4] = (client_user_length >> 24) & 0xFF;
client_user_data[5] = (client_user_length >> 16) & 0xFF;
client_user_data[6] = (client_user_length >> 8) & 0xFF;
client_user_data[7] = client_user_length & 0xFF;
client_user_data[8] = (client->panid >> 8) & 0xFF;
client_user_data[9] = client->panid & 0xFF;
client_user_data[10] = client->client_id;
client_user_data[11] = client->gateway_id;
client_user_data[12] = (client_user_type >> 8) & 0xFF;
client_user_data[13] = client_user_type & 0xFF;
memcpy((uint8_t *)(client_user_data + 14), (uint8_t *)send_buf, length);
client_user_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 2] = ADAPTER_LORA_DATA_END;
client_user_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 1] = ADAPTER_LORA_DATA_END;
if (AdapterDeviceSend(adapter, (uint8 *)&client_user_data, client_user_data.length) < 0) {
if (AdapterDeviceSend(adapter, client_user_data, ADAPTER_LORA_TRANSFER_DATA_LENGTH) < 0) {
return -1;
}
@ -361,35 +410,32 @@ static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int lengt
/**
* @description: Lora Gateway receive data analyzing
* @param adapter Lora adapter pointer
* @param gateway_recv_data Lora gateway receive data pointer
*/
static int LoraGateWayDataAnalyze(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data)
static int LoraGateWayDataAnalyze(struct Adapter *adapter)
{
int ret = 0;
printf("%s:gateway_recv_data\n",__func__);
printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x crc 0x%x\n",
gateway_recv_data->flame_head, gateway_recv_data->length, gateway_recv_data->panid, gateway_recv_data->data_type,
gateway_recv_data->client_id, gateway_recv_data->gateway_id, gateway_recv_data->crc16);
if (LoraCrc16Check((uint8 *)gateway_recv_data, sizeof(struct LoraDataFormat)) < 0) {
printf("LoraGateWayDataAnalyze CRC check error\n");
return -1;
}
if (ADAPTER_LORA_NET_PANID == gateway_recv_data_format.panid) {
if ((ADAPTER_LORA_DATA_HEAD == gateway_recv_data->flame_head) &&
(ADAPTER_LORA_NET_PANID == gateway_recv_data->panid)) {
switch (gateway_recv_data->data_type)
printf("%s: gateway_recv_data\n", __func__);
printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x end 0x%x\n",
gateway_recv_data_format.flame_head, gateway_recv_data_format.length, gateway_recv_data_format.panid, gateway_recv_data_format.data_type,
gateway_recv_data_format.client_id, gateway_recv_data_format.gateway_id, gateway_recv_data_format.flame_end);
switch (gateway_recv_data_format.data_type)
{
case ADAPTER_LORA_DATA_TYPE_JOIN :
case ADAPTER_LORA_DATA_TYPE_QUIT :
ret = LoraGatewayReply(adapter, gateway_recv_data);
ret = LoraGatewayReply(adapter, &gateway_recv_data_format);
break;
case ADAPTER_LORA_DATA_TYPE_USERDATA :
ret = LoraGatewayHandleData(adapter, gateway_recv_data);
ret = LoraGatewayHandleData(adapter, &gateway_recv_data_format);
break;
default:
break;
}
} else {
ret = -1;
}
return ret;
@ -400,58 +446,44 @@ static int LoraGateWayDataAnalyze(struct Adapter *adapter, struct LoraDataFormat
* @param adapter Lora adapter pointer
* @param send_buf Lora Client send user data buf
* @param length user data length (max size is ADAPTER_LORA_DATA_LENGTH)
* @param index user data index, max size ADAPTER_LORA_DATA_LENGTH single index
*/
static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int length)
static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int length, uint16_t index)
{
int ret = 0;
uint8 lora_recv_data[ADAPTER_LORA_RECV_DATA_LENGTH] = {0};
struct LoraDataFormat *client_recv_data = PrivMalloc(sizeof(struct LoraDataFormat));
memset(client_recv_data, 0, sizeof(struct LoraDataFormat));
ret = AdapterDeviceRecv(adapter, lora_recv_data, ADAPTER_LORA_RECV_DATA_LENGTH);
if (ret <= 0) {
printf("LoraClientDataAnalyze recv error.Just return\n");
PrivFree(client_recv_data);
return -1;
}
LoraReceiveDataCheck(lora_recv_data, ADAPTER_LORA_RECV_DATA_LENGTH, client_recv_data);
uint8_t client_id = adapter->net_role_id;
printf("%s:client_recv_data\n", __func__);
printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x crc 0x%x\n",
client_recv_data->flame_head, client_recv_data->length, client_recv_data->panid, client_recv_data->data_type,
client_recv_data->client_id, client_recv_data->gateway_id, client_recv_data->crc16);
ret = PrivSemaphoreObtainWait(&adapter->sem, NULL);
if (0 == ret) {
//only handle this client_id information from gateway
if ((client_recv_data_format[client_id - 1].client_id == adapter->net_role_id) &&
(ADAPTER_LORA_NET_PANID == client_recv_data_format[client_id - 1].panid)) {
if ((ADAPTER_LORA_DATA_HEAD == client_recv_data->flame_head) &&
(ADAPTER_LORA_NET_PANID == client_recv_data->panid)) {
if (LoraCrc16Check((uint8 *)client_recv_data, sizeof(struct LoraDataFormat)) < 0) {
printf("LoraClientDataAnalyze CRC check error\n");
PrivFree(client_recv_data);
return -1;
}
printf("%s: 0x%x client_recv_data\n", __func__, client_recv_data_format[client_id - 1].client_id);
printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x end 0x%x\n",
client_recv_data_format[client_id - 1].flame_head, client_recv_data_format[client_id - 1].length, client_recv_data_format[client_id - 1].panid, client_recv_data_format[client_id - 1].data_type,
client_recv_data_format[client_id - 1].client_id, client_recv_data_format[client_id - 1].gateway_id, client_recv_data_format[client_id - 1].flame_end);
//only handle this client_id information from gateway
if (client_recv_data->client_id == adapter->net_role_id) {
switch (client_recv_data->data_type)
switch (client_recv_data_format[client_id - 1].data_type)
{
case ADAPTER_LORA_DATA_TYPE_JOIN_REPLY :
case ADAPTER_LORA_DATA_TYPE_QUIT_REPLY :
ret = LoraClientUpdate(adapter, client_recv_data);
ret = LoraClientUpdate(adapter, &client_recv_data_format[client_id - 1]);
break;
case ADAPTER_LORA_DATA_TYPE_CMD :
if (send_buf) {
ret = LoraClientSendData(adapter, send_buf, length);
ret = LoraClientSendData(adapter, send_buf, length, index);
}
break;
default:
break;
}
//Client operation done
memset(&client_recv_data_format[client_id - 1], 0 , sizeof(struct LoraDataFormat));
}
}
PrivFree(client_recv_data);
return ret;
}
@ -462,30 +494,43 @@ static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int le
*/
static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid)
{
struct LoraDataFormat client_join_data;
struct AdapterData priv_lora_net;
memset(&client_join_data, 0, sizeof(struct LoraDataFormat));
uint16_t client_join_type;
uint32_t client_join_length = ADAPTER_LORA_TRANSFER_DATA_LENGTH;
uint8_t client_join_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH];
client_join_data.flame_head = ADAPTER_LORA_DATA_HEAD;
client_join_data.length = sizeof(struct LoraDataFormat);
client_join_data.panid = panid;
client_join_data.data_type = ADAPTER_LORA_DATA_TYPE_JOIN;
client_join_data.client_id = adapter->net_role_id;
client_join_data.crc16 = LoraCrc16((uint8 *)&client_join_data, sizeof(struct LoraDataFormat) - 2);
memset(client_join_data, 0, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
printf("%s:client_join_data\n", __func__);
printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x crc 0x%x\n",
client_join_data.flame_head, client_join_data.length, client_join_data.panid, client_join_data.data_type,
client_join_data.client_id, client_join_data.gateway_id, client_join_data.crc16);
priv_lora_net.len = sizeof(struct LoraDataFormat);
priv_lora_net.buffer = (uint8 *)&client_join_data;
client_join_type = ADAPTER_LORA_DATA_TYPE_JOIN;
client_join_data[0] = ADAPTER_LORA_DATA_HEAD;
client_join_data[1] = ADAPTER_LORA_DATA_HEAD;
client_join_data[2] = 0;
client_join_data[3] = 0;
client_join_data[4] = (client_join_length >> 24) & 0xFF;
client_join_data[5] = (client_join_length >> 16) & 0xFF;
client_join_data[6] = (client_join_length >> 8) & 0xFF;
client_join_data[7] = client_join_length & 0xFF;
client_join_data[8] = (panid >> 8) & 0xFF;
client_join_data[9] = panid & 0xFF;
client_join_data[10] = adapter->net_role_id & 0xFF;
client_join_data[11] = 0;
client_join_data[12] = (client_join_type >> 8) & 0xFF;
client_join_data[13] = client_join_type & 0xFF;
client_join_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 2] = ADAPTER_LORA_DATA_END;
client_join_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 1] = ADAPTER_LORA_DATA_END;
priv_lora_net.len = ADAPTER_LORA_TRANSFER_DATA_LENGTH;
priv_lora_net.buffer = client_join_data;
if (AdapterDeviceJoin(adapter, (uint8 *)&priv_lora_net) < 0) {
if (AdapterDeviceJoin(adapter, (uint8_t *)&priv_lora_net) < 0) {
return -1;
}
printf("%s:client_join_data panid 0x%x client_id 0x%x\n", __func__, panid, adapter->net_role_id);
return 0;
}
@ -496,107 +541,166 @@ static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid)
*/
static int LoraClientQuitNet(struct Adapter *adapter, unsigned short panid)
{
struct LoraDataFormat client_quit_data;
struct AdapterData priv_lora_net;
memset(&client_quit_data, 0, sizeof(struct LoraDataFormat));
uint16_t client_quit_type;
uint32_t client_quit_length = ADAPTER_LORA_TRANSFER_DATA_LENGTH;
uint8_t client_quit_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH];
client_quit_data.flame_head = ADAPTER_LORA_DATA_HEAD;
client_quit_data.length = sizeof(struct LoraDataFormat);
client_quit_data.panid = panid;
client_quit_data.data_type = ADAPTER_LORA_DATA_TYPE_QUIT;
client_quit_data.client_id = adapter->net_role_id;
client_quit_data.crc16 = LoraCrc16((uint8 *)&client_quit_data, sizeof(struct LoraDataFormat) - 2);
priv_lora_net.len = sizeof(struct LoraDataFormat);
priv_lora_net.buffer = (uint8 *)&client_quit_data;
memset(client_quit_data, 0, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
client_quit_type = ADAPTER_LORA_DATA_TYPE_QUIT;
client_quit_data[0] = ADAPTER_LORA_DATA_HEAD;
client_quit_data[1] = ADAPTER_LORA_DATA_HEAD;
client_quit_data[2] = 0;
client_quit_data[3] = 0;
client_quit_data[4] = (client_quit_length >> 24) & 0xFF;
client_quit_data[5] = (client_quit_length >> 16) & 0xFF;
client_quit_data[6] = (client_quit_length >> 8) & 0xFF;
client_quit_data[7] = client_quit_length & 0xFF;
client_quit_data[8] = (panid >> 8) & 0xFF;
client_quit_data[9] = panid & 0xFF;
client_quit_data[10] = adapter->net_role_id & 0xFF;
client_quit_data[11] = 0;
client_quit_data[12] = (client_quit_type >> 8) & 0xFF;
client_quit_data[13] = client_quit_type & 0xFF;
client_quit_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 2] = ADAPTER_LORA_DATA_END;
client_quit_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH - 1] = ADAPTER_LORA_DATA_END;
if (AdapterDeviceDisconnect(adapter, (uint8 *)&priv_lora_net) < 0) {
priv_lora_net.len = ADAPTER_LORA_TRANSFER_DATA_LENGTH;
priv_lora_net.buffer = client_quit_data;
if (AdapterDeviceDisconnect(adapter, (uint8_t *)&priv_lora_net) < 0) {
return -1;
}
printf("%s:client_quit_data panid 0x%x client_id 0x%x\n", __func__, panid, adapter->net_role_id);
return 0;
}
/**
* @description: Lora Gateway Process function
* @param lora_adapter Lora adapter pointer
* @param gateway Lora gateway pointer
* @param gateway_recv_data Lora gateway receive data pointer
* @description: Lora receive data check
* @param adapter Lora adapter pointer
* @param recv_data receive data buffer
* @param length receive data length
*/
int LoraGatewayProcess(struct Adapter *lora_adapter, struct LoraGatewayParam *gateway, struct LoraDataFormat *gateway_recv_data)
static int LoraReceiveDataCheck(struct Adapter *adapter, uint8_t *recv_data, uint16_t length)
{
int i, ret = 0;
uint8 lora_recv_data[ADAPTER_LORA_RECV_DATA_LENGTH];
switch (LoraGatewayState)
{
case LORA_STATE_IDLE:
memset(lora_recv_data, 0, ADAPTER_LORA_RECV_DATA_LENGTH);
ret = AdapterDeviceRecv(lora_adapter, lora_recv_data, ADAPTER_LORA_RECV_DATA_LENGTH);
if (ret <= 0) {
printf("LoraGatewayProcess IDLE recv error.Just return\n");
break;
int ret;
uint8_t client_id;
if ((ADAPTER_LORA_DATA_HEAD == recv_data[0]) && (ADAPTER_LORA_DATA_HEAD == recv_data[1]) &&
(ADAPTER_LORA_DATA_END == recv_data[length - 1]) && (ADAPTER_LORA_DATA_END == recv_data[length - 2])) {
#ifdef AS_LORA_GATEWAY_ROLE
gateway_recv_data_format.flame_head = ((recv_data[0] << 8) & 0xFF00) | recv_data[1];
gateway_recv_data_format.flame_index = ((recv_data[2] << 8) & 0xFF00) | recv_data[3];
gateway_recv_data_format.length = ((recv_data[4] << 24) & 0xFF000000) | ((recv_data[5] << 16) & 0xFF0000) |
((recv_data[6] << 8) & 0xFF00) | (recv_data[7] & 0xFF);
gateway_recv_data_format.panid = ((recv_data[8] << 8) & 0xFF00) | recv_data[9];
gateway_recv_data_format.client_id = recv_data[10];
gateway_recv_data_format.gateway_id = recv_data[11];
gateway_recv_data_format.data_type = ((recv_data[12] << 8) & 0xFF00) | recv_data[13];
gateway_recv_data_format.flame_end = ((recv_data[length - 2] << 8) & 0xFF00) | recv_data[length - 1];
memcpy(gateway_recv_data_format.data, (uint8_t *)(recv_data + 14), ADAPTER_LORA_DATA_LENGTH);
ret = LoraGateWayDataAnalyze(adapter);
return ret;
#else
client_id = recv_data[10];
if (client_id == adapter->net_role_id) {
printf("client_id 0x%x recv data\n", client_id);
client_recv_data_format[client_id - 1].flame_head = ((recv_data[0] << 8) & 0xFF00) | recv_data[1];
client_recv_data_format[client_id - 1].flame_index = ((recv_data[2] << 8) & 0xFF00) | recv_data[3];
client_recv_data_format[client_id - 1].length = ((recv_data[4] << 24) & 0xFF000000) | ((recv_data[5] << 16) & 0xFF0000) |
((recv_data[6] << 8) & 0xFF00) | (recv_data[7] & 0xFF);
client_recv_data_format[client_id - 1].panid = ((recv_data[8] << 8) & 0xFF00) | recv_data[9];
client_recv_data_format[client_id - 1].client_id = recv_data[10];
client_recv_data_format[client_id - 1].gateway_id = recv_data[11];
client_recv_data_format[client_id - 1].data_type = ((recv_data[12] << 8) & 0xFF00) | recv_data[13];
client_recv_data_format[client_id - 1].flame_end = ((recv_data[length - 2] << 8) & 0xFF00) | recv_data[length - 1];
memcpy(client_recv_data_format[client_id - 1].data, (uint8_t *)(recv_data + 14), ADAPTER_LORA_DATA_LENGTH);
return 0;
}
#endif
}
LoraReceiveDataCheck(lora_recv_data, ADAPTER_LORA_RECV_DATA_LENGTH, gateway_recv_data);
return -1;
}
/**
* @description: Lora data receive task
* @param parameter - Lora adapter pointer
*/
static void *LoraReceiveTask(void *parameter)
{
int ret = 0;
struct Adapter *lora_adapter = (struct Adapter *)parameter;
if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) {
LoraGatewayState = LORA_JOIN_NET;
} else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) {
LoraGatewayState = LORA_QUIT_NET;
} else {
LoraGatewayState = LORA_STATE_IDLE;
while (1) {
memset(lora_recv_data, 0, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
ret = AdapterDeviceRecv(lora_adapter, lora_recv_data, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
if (ret <= 0) {
printf("AdapterDeviceRecv error.Just return\n");
recv_error_cnt++;
if (recv_error_cnt > ADAPTER_LORA_RECEIVE_ERROR_CNT) {
recv_error_cnt = 0;
#ifdef AS_LORA_GATEWAY_ROLE
PrivSemaphoreAbandon(&gateway_recv_data_sem);
#endif
}
continue;
}
break;
case LORA_JOIN_NET:
case LORA_QUIT_NET:
ret = LoraGateWayDataAnalyze(lora_adapter, gateway_recv_data);
ret = LoraReceiveDataCheck(lora_adapter, lora_recv_data, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
if (ret < 0) {
printf("LoraGateWayDataAnalyze state %d error, re-send data cmd to client\n", LoraGatewayState);
PrivTaskDelay(500);
continue;
}
LoraGatewayState = LORA_RECV_DATA;
break;
case LORA_RECV_DATA:
for (i = 0; i < gateway->client_num; i ++) {
if (gateway->client_id[i]) {
printf("LoraGatewayProcess send to client %d for data\n", gateway->client_id[i]);
ret = LoraGatewaySendCmd(lora_adapter, gateway->client_id[i], ADAPTER_LORA_DATA_TYPE_CMD);
if (ret < 0) {
printf("LoraGatewaySendCmd client ID %d error\n", gateway->client_id[i]);
PrivTaskDelay(500);
continue;
}
memset(lora_recv_data, 0, ADAPTER_LORA_RECV_DATA_LENGTH);
ret = AdapterDeviceRecv(lora_adapter, lora_recv_data, ADAPTER_LORA_RECV_DATA_LENGTH);
if (ret <= 0) {
printf("LoraGatewayProcess recv error.Just return\n");
continue;
}
PrivSemaphoreAbandon(&lora_adapter->sem);
}
LoraReceiveDataCheck(lora_recv_data, ADAPTER_LORA_RECV_DATA_LENGTH, gateway_recv_data);
if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) {
LoraGatewayState = LORA_JOIN_NET;
} else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) {
LoraGatewayState = LORA_QUIT_NET;
} else {
ret = LoraGateWayDataAnalyze(lora_adapter, gateway_recv_data);
if (ret < 0) {
printf("LoraGateWayDataAnalyze error, re-send data cmd to client\n");
PrivTaskDelay(500);
}
}
return 0;
}
/**
* @description: Lora Gateway Process function
* @param lora_adapter Lora adapter pointer
* @param gateway Lora gateway pointer
*/
void LoraGatewayProcess(struct Adapter *lora_adapter, struct LoraGatewayParam *gateway)
{
int i, ret = 0;
#ifdef GATEWAY_CMD_MODE
for (i = 0; i < gateway->client_num; i ++) {
if (gateway->client_id[i]) {
printf("LoraGatewayProcess send to client %d for data\n", gateway->client_id[i]);
ret = LoraGatewaySendCmd(lora_adapter, gateway->client_id[i], ADAPTER_LORA_DATA_TYPE_CMD);
if (ret < 0) {
printf("LoraGatewaySendCmd client ID %d error\n", gateway->client_id[i]);
continue;
}
ret = PrivSemaphoreObtainWait(&gateway_recv_data_sem, NULL);
if (0 == ret) {
printf("LoraGatewayProcess receive client %d data done\n", gateway->client_id[i]);
}
}
break;
default:
break;
}
#endif
return 0;
return;
}
/**
@ -609,10 +713,16 @@ static void *LoraGatewayTask(void *parameter)
int ret = 0;
struct Adapter *lora_adapter = (struct Adapter *)parameter;
struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)lora_adapter->adapter_param;
struct LoraDataFormat gateway_recv_data;
memset(&gateway_recv_data_format, 0, sizeof(struct LoraDataFormat));
gateway->client_num = ADAPTER_LORA_CLIENT_NUM;
for (i = 0; i < ADAPTER_LORA_CLIENT_NUM;i ++) {
gateway->client_id[i] = i + 1;
}
while (1) {
LoraGatewayProcess(lora_adapter, gateway, &gateway_recv_data);
LoraGatewayProcess(lora_adapter, gateway);
}
return 0;
@ -628,69 +738,36 @@ static void *LoraClientDataTask(void *parameter)
struct Adapter *lora_adapter = (struct Adapter *)parameter;
struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param;
for (i = 0; i < ADAPTER_LORA_CLIENT_NUM; i ++) {
memset(&client_recv_data_format[i], 0, sizeof(struct LoraDataFormat));
}
client->gateway_id = 0xFF;
client->panid = ADAPTER_LORA_NET_PANID;
client->client_state = CLIENT_CONNECT;
//set lora_send_buf for test
uint8 lora_send_buf[ADAPTER_LORA_DATA_LENGTH];
uint8_t lora_send_buf[ADAPTER_LORA_DATA_LENGTH];
memset(lora_send_buf, 0, ADAPTER_LORA_DATA_LENGTH);
sprintf(lora_send_buf, "Lora client %d adapter test\n", client->client_id);
while (1) {
PrivTaskDelay(100);
if ((CLIENT_DISCONNECT == client->client_state) && (!g_adapter_lora_quit_flag)) {
ret = LoraClientJoinNet(lora_adapter, client->panid);
if (ret < 0) {
printf("LoraClientJoinNet error panid 0x%x\n", client->panid);
}
ret = LoraClientDataAnalyze(lora_adapter, NULL, 0);
if (ret < 0) {
printf("LoraClientDataAnalyze error, reconnect to gateway\n");
PrivTaskDelay(500);
continue;
}
}
if (CLIENT_CONNECT == client->client_state) {
ret = LoraClientDataAnalyze(lora_adapter, (void *)lora_send_buf, strlen(lora_send_buf));
if (ret < 0) {
printf("LoraClientDataAnalyze error, wait for next data cmd\n");
PrivTaskDelay(500);
continue;
}
//Condition 1: Gateway send user_data cmd, client send user_data after receiving user_data cmd
#ifdef GATEWAY_CMD_MODE
ret = LoraClientDataAnalyze(lora_adapter, (void *)lora_send_buf, strlen(lora_send_buf), 0);
if (ret < 0) {
printf("LoraClientDataAnalyze error, wait for next data cmd\n");
continue;
}
}
return 0;
}
/**
* @description: Lora Client quit task
* @param parameter - Lora adapter pointer
*/
static void *LoraClientQuitTask(void *parameter)
{
int ret = 0;
struct Adapter *lora_adapter = (struct Adapter *)parameter;
struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param;
while (1) {
PrivTaskDelay(100);
if ((CLIENT_CONNECT == client->client_state) && (g_adapter_lora_quit_flag)) {
ret = LoraClientQuitNet(lora_adapter, client->panid);
if (ret < 0) {
printf("LoraClientQuitNet error panid 0x%x, re-quit net\n", client->panid);
continue;
}
ret = LoraClientDataAnalyze(lora_adapter, NULL, 0);
if (ret < 0) {
printf("LoraClientQuitTask LoraClientDataAnalyze error\n");
PrivTaskDelay(500);
continue;
}
#endif
//Condition 2: client send user_data automatically
#ifdef CLIENT_UPDATE_MODE
if (lora_send_buf) {
PrivTaskDelay(2000);
printf("LoraClientSendData\n");
LoraClientSendData(lora_adapter, (void *)lora_send_buf, strlen(lora_send_buf), 0);
}
#endif
}
return 0;
@ -803,15 +880,22 @@ int AdapterLoraInit(void)
adapter->done = product_info->model_done;
#endif
PrivSemaphoreCreate(&adapter->sem, 0, 0);
PrivSemaphoreCreate(&gateway_recv_data_sem, 0, 0);
PrivMutexCreate(&adapter->lock, 0);
return ret;
}
/******************Lora TEST*********************/
static pthread_t lora_recv_data_task;
#ifdef AS_LORA_GATEWAY_ROLE
static pthread_t lora_gateway_task;
#else //AS_LORA_CLIENT_ROLE
static pthread_t lora_client_data_task;
static pthread_t lora_client_quit_task;
#endif
int AdapterLoraTest(void)
@ -832,6 +916,15 @@ int AdapterLoraTest(void)
lora_gateway_attr.stacksize = 2048;
#endif
PrivTaskCreate(&lora_recv_data_task, &lora_gateway_attr, &LoraReceiveTask, (void *)adapter);
PrivTaskStartup(&lora_recv_data_task);
#ifdef ADD_NUTTX_FETURES
lora_gateway_attr.priority = 19;
#else
lora_gateway_attr.schedparam.sched_priority = 19;
#endif
PrivTaskCreate(&lora_gateway_task, &lora_gateway_attr, &LoraGatewayTask, (void *)adapter);
PrivTaskStartup(&lora_gateway_task);