diff --git a/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/stm32l4xx_it_module.c b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/stm32l4xx_it_module.c index 2473e742..a2c81cd7 100644 --- a/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/stm32l4xx_it_module.c +++ b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/stm32l4xx_it_module.c @@ -298,9 +298,17 @@ void LPUART1_IRQHandler(void) void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { extern uint8_t data; + extern at_agent_t esp8266_at_agent; + extern uint8_t ec20_data; + extern at_agent_t ec20_at_agent; + if (huart->Instance == LPUART1) { HAL_UART_Receive_IT(&hlpuart1, &data, 1); - tos_at_uart_input_byte(data); + tos_at_uart_input_byte(&esp8266_at_agent, data); + } + else if(huart->Instance == USART3) { + HAL_UART_Receive_IT(&huart3, &ec20_data, 1); + tos_at_uart_input_byte(&ec20_at_agent, ec20_data); } } /* USER CODE END 1 */ diff --git a/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/usart.c b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/usart.c index 5ec09a9a..ba49e4e6 100644 --- a/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/usart.c +++ b/board/TencentOS_tiny_EVB_MX_Plus/BSP/Src/usart.c @@ -23,6 +23,7 @@ /* USER CODE BEGIN 0 */ uint8_t data; uint8_t msg; +uint8_t ec20_data; /* USER CODE END 0 */ UART_HandleTypeDef hlpuart1; @@ -99,7 +100,7 @@ void MX_USART3_UART_Init(void) { huart3.Instance = USART3; - huart3.Init.BaudRate = 9600; + huart3.Init.BaudRate = 115200; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1; huart3.Init.Parity = UART_PARITY_NONE; @@ -112,7 +113,7 @@ void MX_USART3_UART_Init(void) { Error_Handler(); } - HAL_UART_Receive_IT(&huart3, &msg, 1);//(uint8_t *)aRxBuffer, 1); + HAL_UART_Receive_IT(&huart3, &ec20_data, 1);//(uint8_t *)aRxBuffer, 1); } void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) diff --git a/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvoptx b/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvoptx index 07c4baab..f50eb360 100644 --- a/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvoptx +++ b/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvoptx @@ -154,22 +154,6 @@ 0 69 1 -
134234708
- 0 - 0 - 0 - 0 - 0 - 1 - ..\..\..\..\examples\tcp_through_module\tcp_through_module.c - - \\TencentOS_tiny\../../../../examples/tcp_through_module/tcp_through_module.c\69 - - - 1 - 0 - 66 - 1
0
0 0 @@ -303,7 +287,7 @@ Application/User - 0 + 1 0 0 0 @@ -1203,7 +1187,7 @@ at - 0 + 1 0 0 0 @@ -1223,7 +1207,7 @@ devices - 0 + 1 0 0 0 @@ -1239,17 +1223,29 @@ 0 0 + + 12 + 73 + 1 + 0 + 0 + 0 + ..\..\..\..\devices\ec20\ec20.c + ec20.c + 0 + 0 + sal_module_wrapper - 0 + 1 0 0 0 13 - 73 + 74 1 0 0 @@ -1263,13 +1259,13 @@ hal - 0 + 1 0 0 0 14 - 74 + 75 1 0 0 diff --git a/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvprojx b/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvprojx index d4c86d18..57e52d3b 100644 --- a/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvprojx +++ b/board/TencentOS_tiny_EVB_MX_Plus/KEIL/tcp_through_module/TencentOS_tiny.uvprojx @@ -798,6 +798,11 @@ 1 ..\..\..\..\devices\esp8266\esp8266.c + + ec20.c + 1 + ..\..\..\..\devices\ec20\ec20.c + diff --git a/devices/ec20/ec20.c b/devices/ec20/ec20.c index 565360f0..f406486e 100644 --- a/devices/ec20/ec20.c +++ b/devices/ec20/ec20.c @@ -24,6 +24,8 @@ #include #include +static k_stack_t ec20_at_parser_task_stack[AT_PARSER_TASK_STACK_SIZE]; + typedef struct ip_addr_st { uint8_t seg1; uint8_t seg2; @@ -34,12 +36,15 @@ typedef struct ip_addr_st { static ip_addr_t domain_parser_addr = {0}; k_sem_t domain_parser_sem; +at_agent_t ec20_at_agent; +#define AT_AGENT ((at_agent_t *)(&ec20_at_agent)) + static int ec20_echo_close(void) { at_echo_t echo; tos_at_echo_create(&echo, NULL, 0, NULL); - tos_at_cmd_exec(&echo, 1000, "ATE0\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "ATE0\r\n"); if (echo.status == AT_ECHO_STATUS_OK) { return 0; @@ -56,7 +61,7 @@ static int ec20_sim_card_check(void) while (try++ < 10) { - tos_at_cmd_exec(&echo, 1000, "AT+CPIN?\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CPIN?\r\n"); if (echo.status != AT_ECHO_STATUS_OK) { return -1; } @@ -79,7 +84,7 @@ static int ec20_signal_quality_check(void) tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL); while (try++ < 10) { - tos_at_cmd_exec(&echo, 1000, "AT+CSQ\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CSQ\r\n"); if (echo.status != AT_ECHO_STATUS_OK) { return -1; @@ -108,7 +113,7 @@ static int ec20_gsm_network_check(void) tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL); while (try++ < 10) { - tos_at_cmd_exec(&echo, 1000, "AT+CREG?\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CREG?\r\n"); if (echo.status != AT_ECHO_STATUS_OK) { return -1; } @@ -137,7 +142,7 @@ static int ec20_gprs_network_check(void) while (try++ < 10) { - tos_at_cmd_exec(&echo, 1000, "AT+CGREG?\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CGREG?\r\n"); if (echo.status != AT_ECHO_STATUS_OK) { return -1; @@ -163,7 +168,7 @@ static int ec20_close_apn(void) at_echo_t echo; tos_at_echo_create(&echo, NULL, 0, NULL); - tos_at_cmd_exec(&echo, 3000, "AT+QIDEACT=1\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 3000, "AT+QIDEACT=1\r\n"); if (echo.status == AT_ECHO_STATUS_OK) { return 0; @@ -177,13 +182,13 @@ static int ec20_set_apn(void) at_echo_t echo; tos_at_echo_create(&echo, NULL, 0, NULL); - tos_at_cmd_exec(&echo, 300, "AT+QICSGP=1,1,\"CMNET\"\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 300, "AT+QICSGP=1,1,\"CMNET\"\r\n"); if (echo.status != AT_ECHO_STATUS_OK) { return -1; } - tos_at_cmd_exec_until(&echo, 3000, "AT+QIACT=1\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 3000, "AT+QIACT=1\r\n"); if (echo.status != AT_ECHO_STATUS_OK) { return -1; @@ -246,29 +251,29 @@ static int ec20_connect(const char *ip, const char *port, sal_proto_t proto) int id; at_echo_t echo; - id = tos_at_channel_alloc(ip, port); + id = tos_at_channel_alloc(AT_AGENT, ip, port); if (id == -1) { return -1; } - tos_at_cmd_exec(NULL, 1000, "AT+QICLOSE=%d\r\n", id); + tos_at_cmd_exec(AT_AGENT, NULL, 1000, "AT+QICLOSE=%d\r\n", id); tos_at_echo_create(&echo, NULL, 0, "CONNECT OK"); - tos_at_cmd_exec(&echo, 4000, "AT+QIOPEN=1,%d,\"%s\",\"%s\",%d,0,1\r\n", + tos_at_cmd_exec(AT_AGENT, &echo, 4000, "AT+QIOPEN=1,%d,\"%s\",\"%s\",%d,0,1\r\n", id, proto == TOS_SAL_PROTO_UDP ? "UDP" : "TCP", ip, atoi(port)); if (echo.status == AT_ECHO_STATUS_OK) { return id; } - tos_at_channel_free(id); + tos_at_channel_free(AT_AGENT, id); return -1; } static int ec20_recv_timeout(int id, void *buf, size_t len, uint32_t timeout) { - return tos_at_channel_read_timed(id, buf, len, timeout); + return tos_at_channel_read_timed(AT_AGENT, id, buf, len, timeout); } static int ec20_recv(int id, void *buf, size_t len) @@ -280,41 +285,41 @@ int ec20_send(int id, const void *buf, size_t len) { at_echo_t echo; - if (!tos_at_channel_is_working(id)) { + if (!tos_at_channel_is_working(AT_AGENT, id)) { return -1; } - if (tos_at_global_lock_pend() != 0) + if (tos_at_global_lock_pend(AT_AGENT) != 0) { return -1; } tos_at_echo_create(&echo, NULL, 0, ">"); - tos_at_cmd_exec(&echo, 1000, "AT+QISEND=%d,%d\r\n", id, len); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+QISEND=%d,%d\r\n", id, len); if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) { - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } tos_at_echo_create(&echo, NULL, 0, "SEND OK"); - tos_at_raw_data_send_until(&echo, 10000, (uint8_t *)buf, len); + tos_at_raw_data_send_until(AT_AGENT, &echo, 10000, (uint8_t *)buf, len); if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) { - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return len; } int ec20_recvfrom_timeout(int id, void *buf, size_t len, uint32_t timeout) { - return tos_at_channel_read_timed(id, buf, len, timeout); + return tos_at_channel_read_timed(AT_AGENT, id, buf, len, timeout); } int ec20_recvfrom(int id, void *buf, size_t len) @@ -326,46 +331,46 @@ int ec20_sendto(int id, char *ip, char *port, const void *buf, size_t len) { at_echo_t echo; - if (tos_at_global_lock_pend() != 0) + if (tos_at_global_lock_pend(AT_AGENT) != 0) { return -1; } tos_at_echo_create(&echo, NULL, 0, ">"); - tos_at_cmd_exec(&echo, 1000, "AT+QISEND=%d,%d\r\n", id, len); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+QISEND=%d,%d\r\n", id, len); if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) { - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } tos_at_echo_create(&echo, NULL, 0, "SEND OK"); - tos_at_raw_data_send(&echo, 1000, (uint8_t *)buf, len); + tos_at_raw_data_send(AT_AGENT, &echo, 1000, (uint8_t *)buf, len); if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) { - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return len; } static void ec20_transparent_mode_exit(void) { - tos_at_cmd_exec(NULL, 500, "+++"); + tos_at_cmd_exec(AT_AGENT, NULL, 500, "+++"); } static int ec20_close(int id) { ec20_transparent_mode_exit(); - tos_at_cmd_exec(NULL, 1000, "AT+QICLOSE=%d\r\n", id); + tos_at_cmd_exec(AT_AGENT, NULL, 1000, "AT+QICLOSE=%d\r\n", id); - tos_at_channel_free(id); + tos_at_channel_free(AT_AGENT, id); return 0; } @@ -378,7 +383,7 @@ static int ec20_parse_domain(const char *host_name, char *host_ip, size_t host_i tos_sem_create_max(&domain_parser_sem, 0, 1); tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL); - tos_at_cmd_exec(&echo, 2000, "AT+QIDNSGIP=1,\"%s\"\r\n", host_name); + tos_at_cmd_exec(AT_AGENT, &echo, 2000, "AT+QIDNSGIP=1,\"%s\"\r\n", host_name); if (echo.status != AT_ECHO_STATUS_OK) { return -1; @@ -393,6 +398,38 @@ static int ec20_parse_domain(const char *host_name, char *host_ip, size_t host_i return 0; } +static int ec20_get_local_ip(char *ip, char *gw, char *mask) +{ + char *str; + at_echo_t echo; + char echo_buffer[100]; + int seg1, seg2, seg3, seg4; + + tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL); + tos_at_cmd_exec(AT_AGENT, &echo, 2000, "AT+QIACT?\r\n"); + + if (echo.status != AT_ECHO_STATUS_OK) { + return -1; + } + + /* + +QIACT: 1,1,1,"10.186.107.143" + + OK + */ + str = strstr((const char *)echo.buffer, "+QIACT"); + if (!str) { + return -1; + } + sscanf(str + strlen("+QIACT: 1,1,1,"), "\"%d.%d.%d.%d\"", &seg1, &seg2, &seg3, &seg4); + sprintf(ip, "%d.%d.%d.%d", seg1, seg2, seg3, seg4); + printf("GOT EC20 MODULE IP: %s\n", ip); + + /* ec20 has not provide the ability to query the subnet mask and gw ip address*/ + + return 0; +} + __STATIC__ void ec20_incoming_data_process(void) { uint8_t data; @@ -406,7 +443,7 @@ __STATIC__ void ec20_incoming_data_process(void) while (1) { - if (tos_at_uart_read(&data, 1) != 1) + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } @@ -420,7 +457,7 @@ __STATIC__ void ec20_incoming_data_process(void) while (1) { - if (tos_at_uart_read(&data, 1) != 1) + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } @@ -432,7 +469,7 @@ __STATIC__ void ec20_incoming_data_process(void) data_len = data_len * 10 + (data - '0'); } - if (tos_at_uart_read(&data, 1) != 1) + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } @@ -440,11 +477,11 @@ __STATIC__ void ec20_incoming_data_process(void) do { #define MIN(a, b) ((a) < (b) ? (a) : (b)) read_len = MIN(data_len, sizeof(buffer)); - if (tos_at_uart_read(buffer, read_len) != read_len) { + if (tos_at_uart_read(AT_AGENT, buffer, read_len) != read_len) { return; } - if (tos_at_channel_write(channel_id, buffer, read_len) <= 0) { + if (tos_at_channel_write(AT_AGENT, channel_id, buffer, read_len) <= 0) { return; } @@ -464,7 +501,7 @@ __STATIC__ void ec20_domain_data_process(void) +QIURC: "dnsgip","xxx.xxx.xxx.xxx" */ - if (tos_at_uart_read(&data, 1) != 1) { + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } @@ -475,7 +512,7 @@ __STATIC__ void ec20_domain_data_process(void) if (data == '\"') { /* start parser domain */ while (1) { - if (tos_at_uart_read(&data, 1) != 1) { + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } if (data == '.') { @@ -484,7 +521,7 @@ __STATIC__ void ec20_domain_data_process(void) domain_parser_addr.seg1 = domain_parser_addr.seg1 *10 + (data-'0'); } while (1) { - if (tos_at_uart_read(&data, 1) != 1) { + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } if (data == '.') { @@ -493,7 +530,7 @@ __STATIC__ void ec20_domain_data_process(void) domain_parser_addr.seg2 = domain_parser_addr.seg2 *10 + (data-'0'); } while (1) { - if (tos_at_uart_read(&data, 1) != 1) { + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } if (data == '.') { @@ -502,7 +539,7 @@ __STATIC__ void ec20_domain_data_process(void) domain_parser_addr.seg3 = domain_parser_addr.seg3 *10 + (data-'0'); } while (1) { - if (tos_at_uart_read(&data, 1) != 1) { + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } if (data == '\"') { @@ -522,32 +559,49 @@ at_event_t ec20_at_event[] = { }; sal_module_t sal_module_ec20 = { - .init = ec20_init, - .connect = ec20_connect, - .send = ec20_send, - .recv_timeout = ec20_recv_timeout, - .recv = ec20_recv, - .sendto = ec20_sendto, - .recvfrom = ec20_recvfrom, - .recvfrom_timeout = ec20_recvfrom_timeout, - .close = ec20_close, - .parse_domain = ec20_parse_domain, + .sal_module_name = "ec20", + .sal_module_ip = "0.0.0.0", + .sal_module_type = TOS_SAL_MODULE_4G_LTE, + .sal_module_status = TOS_SAL_MODULE_UNAVAILABLE, + .init = ec20_init, + .connect = ec20_connect, + .send = ec20_send, + .recv_timeout = ec20_recv_timeout, + .recv = ec20_recv, + .sendto = ec20_sendto, + .recvfrom = ec20_recvfrom, + .recvfrom_timeout = ec20_recvfrom_timeout, + .close = ec20_close, + .parse_domain = ec20_parse_domain, }; int ec20_sal_init(hal_uart_port_t uart_port) { - if (tos_at_init(uart_port, ec20_at_event, - sizeof(ec20_at_event) / sizeof(ec20_at_event[0])) != 0) { + char ip[15] = {0}; + + if (tos_at_init(AT_AGENT, ec20_at_parser_task_stack, uart_port, ec20_at_event, + sizeof(ec20_at_event) / sizeof(ec20_at_event[0])) != 0) { return -1; } if (tos_sal_module_register(&sal_module_ec20) != 0) { return -1; } - if (tos_sal_module_init() != 0) { + + if (ec20_init() != 0) { return -1; } - + + if (ec20_get_local_ip(ip, NULL, NULL) != 0) { + return -1; + } + + tos_sal_module_set_local_ip(&sal_module_ec20, ip); + + tos_sal_module_set_status(&sal_module_ec20, TOS_SAL_MODULE_AVAILABLE); + + printf("%s sal device register success\r\n", sal_module_ec20.sal_module_name); + return 0; } @@ -559,9 +613,15 @@ int ec20_sal_deinit() tos_sal_module_close(id); } - tos_sal_module_register_default(); + tos_sal_module_set_local_ip(&sal_module_ec20, "0.0.0.0"); - tos_at_deinit(); + tos_sal_module_set_status(&sal_module_ec20, TOS_SAL_MODULE_UNAVAILABLE); + + tos_sal_module_unregister(&sal_module_ec20); + + tos_at_deinit(AT_AGENT); + + printf("%s sal device unregister success\r\n", sal_module_ec20.sal_module_name); return 0; } diff --git a/devices/esp8266/esp8266.c b/devices/esp8266/esp8266.c index cd3df29f..2acfd851 100644 --- a/devices/esp8266/esp8266.c +++ b/devices/esp8266/esp8266.c @@ -8,6 +8,12 @@ #define TOS_CFG_MODULE_SINGLE_LINK_EN 0u +at_agent_t esp8266_at_agent; + +#define AT_AGENT ((at_agent_t *)(&esp8266_at_agent)) + +static k_stack_t esp8266_at_parser_task_stack[AT_PARSER_TASK_STACK_SIZE]; + static int esp8266_restore(void) { int try = 0; @@ -15,7 +21,7 @@ static int esp8266_restore(void) tos_at_echo_create(&echo, NULL, 0, NULL); while (try++ < 10) { - tos_at_cmd_exec(&echo, 3000, "AT+RESTORE\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 3000, "AT+RESTORE\r\n"); if (echo.status == AT_ECHO_STATUS_OK) { return 0; } @@ -28,7 +34,7 @@ static int esp8266_echo_close(void) at_echo_t echo; tos_at_echo_create(&echo, NULL, 0, NULL); - tos_at_cmd_exec(&echo, 1000, "ATE0\r\n"); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "ATE0\r\n"); if (echo.status == AT_ECHO_STATUS_OK) { return 0; } @@ -57,7 +63,7 @@ static int esp8266_net_mode_set(esp8266_net_mode_t mode) tos_at_echo_create(&echo, NULL, 0, "no change"); while (try++ < 10) { - tos_at_cmd_exec(&echo, 1000, cmd); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, cmd); if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) { return 0; } @@ -72,7 +78,7 @@ static int esp8266_send_mode_set(esp8266_send_mode_t mode) tos_at_echo_create(&echo, NULL, 0, NULL); while (try++ < 10) { - tos_at_cmd_exec(&echo, 1000, "AT+CIPMODE=%d\r\n", mode == ESP8266_SEND_MODE_NORMAL ? 0 : 1); + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPMODE=%d\r\n", mode == ESP8266_SEND_MODE_NORMAL ? 0 : 1); if (echo.status == AT_ECHO_STATUS_OK) { return 0; } @@ -87,7 +93,7 @@ static int esp8266_multilink_set(esp8266_multilink_state_t state) tos_at_echo_create(&echo, NULL, 0, "link is builded"); while (try++ < 10) { - tos_at_cmd_exec(&echo, 500, "AT+CIPMUX=%d\r\n", state == ESP8266_MULTILINK_STATE_ENABLE ? 1 : 0); + tos_at_cmd_exec(AT_AGENT, &echo, 500, "AT+CIPMUX=%d\r\n", state == ESP8266_MULTILINK_STATE_ENABLE ? 1 : 0); if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) { return 0; } @@ -102,7 +108,7 @@ int esp8266_join_ap(const char *ssid, const char *pwd) tos_at_echo_create(&echo, NULL, 0, "OK"); while (try++ < 10) { - tos_at_cmd_exec_until(&echo, 15000, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pwd); + tos_at_cmd_exec_until(AT_AGENT, &echo, 15000, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pwd); if (echo.status == AT_ECHO_STATUS_EXPECT) { return 0; } @@ -141,20 +147,20 @@ static int esp8266_connect(const char *ip, const char *port, sal_proto_t proto) int id; at_echo_t echo; - esp8266_reconnect_init(); + //esp8266_reconnect_init(); - id = tos_at_channel_alloc(ip, port); + id = tos_at_channel_alloc(AT_AGENT, ip, port); if (id == -1) { return -1; } tos_at_echo_create(&echo, NULL, 0, "OK"); #if TOS_CFG_MODULE_SINGLE_LINK_EN > 0u - tos_at_cmd_exec_until(&echo, 10000, + tos_at_cmd_exec_until(AT_AGENT, &echo, 10000, "AT+CIPSTART=\"%s\",\"%s\",%s\r\n", proto == TOS_SAL_PROTO_UDP ? "UDP" : "TCP", ip, port); #else - tos_at_cmd_exec_until(&echo, 10000, + tos_at_cmd_exec_until(AT_AGENT, &echo, 10000, "AT+CIPSTART=%d,\"%s\",\"%s\",%s\r\n", id, proto == TOS_SAL_PROTO_UDP ? "UDP" : "TCP", ip, port); #endif @@ -162,14 +168,14 @@ static int esp8266_connect(const char *ip, const char *port, sal_proto_t proto) return id; } - tos_at_channel_free(id); + tos_at_channel_free(AT_AGENT, id); return -1; } static int esp8266_recv_timeout(int id, void *buf, size_t len, uint32_t timeout) { - return tos_at_channel_read_timed(id, buf, len, timeout); + return tos_at_channel_read_timed(AT_AGENT, id, buf, len, timeout); } static int esp8266_recv(int id, void *buf, size_t len) @@ -192,29 +198,29 @@ static int esp8266_send(int id, const void *buf, size_t len) at_echo_t echo; char echo_buffer[64]; - if (!tos_at_channel_is_working(id)) { + if (!tos_at_channel_is_working(AT_AGENT, id)) { return -1; } - if (tos_at_global_lock_pend() != 0) { + if (tos_at_global_lock_pend(AT_AGENT) != 0) { return -1; } tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), ">"); #if TOS_CFG_MODULE_SINGLE_LINK_EN > 0u - tos_at_cmd_exec(&echo, 1000, + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPSEND=%d\r\n", len); #else - tos_at_cmd_exec(&echo, 1000, + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPSEND=%d,%d\r\n", id, len); #endif if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) { if (esp8266_is_link_broken((const char *)echo.buffer)) { - tos_at_channel_set_broken(id); + tos_at_channel_set_broken(AT_AGENT, id); } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } @@ -224,17 +230,17 @@ static int esp8266_send(int id, const void *buf, size_t len) ATTENTION: we should wait util "SEND OK" is echoed, otherwise the next time we execute at command, the esp8266 maybe in a "busy s ..." state. */ - tos_at_raw_data_send_until(&echo, 10000, (uint8_t *)buf, len); + tos_at_raw_data_send_until(AT_AGENT, &echo, 10000, (uint8_t *)buf, len); if (echo.status != AT_ECHO_STATUS_EXPECT) { if (esp8266_is_link_broken((const char *)echo.buffer)) { - tos_at_channel_set_broken(id); + tos_at_channel_set_broken(AT_AGENT, id); } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return len; } @@ -243,11 +249,11 @@ static int esp8266_sendto(int id, char *ip, char *port, const void *buf, size_t at_echo_t echo; char echo_buffer[64]; - if (!tos_at_channel_is_working(id)) { + if (!tos_at_channel_is_working(AT_AGENT, id)) { return -1; } - if (tos_at_global_lock_pend() != 0) { + if (tos_at_global_lock_pend(AT_AGENT) != 0) { return -1; } @@ -255,28 +261,28 @@ static int esp8266_sendto(int id, char *ip, char *port, const void *buf, size_t if (ip && port) { #if TOS_CFG_MODULE_SINGLE_LINK_EN > 0u - tos_at_cmd_exec(&echo, 1000, + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPSEND=%d,\"%s\",%s\r\n", len, ip, port); #else - tos_at_cmd_exec(&echo, 1000, + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPSEND=%d,%d,\"%s\",%s\r\n", id, len, ip, port); #endif } else { #if TOS_CFG_MODULE_SINGLE_LINK_EN > 0u - tos_at_cmd_exec(&echo, 1000, + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPSEND=%d\r\n", len); #else - tos_at_cmd_exec(&echo, 1000, + tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPSEND=%d,%d\r\n", id, len); #endif } if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) { if (esp8266_is_link_broken((const char *)echo.buffer)) { - tos_at_channel_set_broken(id); + tos_at_channel_set_broken(AT_AGENT, id); } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } @@ -286,23 +292,23 @@ static int esp8266_sendto(int id, char *ip, char *port, const void *buf, size_t ATTENTION: we should wait util "SEND OK" is echoed, otherwise the next time we execute at command, the esp8266 maybe in a "busy s ..." state. */ - tos_at_raw_data_send_until(&echo, 10000, (uint8_t *)buf, len); + tos_at_raw_data_send_until(AT_AGENT, &echo, 10000, (uint8_t *)buf, len); if (echo.status != AT_ECHO_STATUS_EXPECT) { if (esp8266_is_link_broken((const char *)echo.buffer)) { - tos_at_channel_set_broken(id); + tos_at_channel_set_broken(AT_AGENT, id); } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return -1; } - tos_at_global_lock_post(); + tos_at_global_lock_post(AT_AGENT); return len; } static int esp8266_recvfrom_timeout(int id, void *buf, size_t len, uint32_t timeout) { - return tos_at_channel_read_timed(id, buf, len, timeout); + return tos_at_channel_read_timed(AT_AGENT, id, buf, len, timeout); } static int esp8266_recvfrom(int id, void *buf, size_t len) @@ -313,11 +319,11 @@ static int esp8266_recvfrom(int id, void *buf, size_t len) static int esp8266_close(int id) { #if TOS_CFG_MODULE_SINGLE_LINK_EN > 0u - tos_at_cmd_exec(NULL, 1000, "AT+CIPCLOSE\r\n"); + tos_at_cmd_exec(AT_AGENT, NULL, 1000, "AT+CIPCLOSE\r\n"); #else - tos_at_cmd_exec(NULL, 1000, "AT+CIPCLOSE=%d\r\n", id); + tos_at_cmd_exec(AT_AGENT, NULL, 1000, "AT+CIPCLOSE=%d\r\n", id); #endif - tos_at_channel_free(id); + tos_at_channel_free(AT_AGENT, id); return 0; } @@ -330,7 +336,7 @@ static int esp8266_parse_domain(const char *host_name, char *host_ip, size_t hos esp8266_reconnect_init(); tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL); - tos_at_cmd_exec(&echo, 2000, "AT+CIPDOMAIN=\"%s\"\r\n", host_name); + tos_at_cmd_exec(AT_AGENT, &echo, 2000, "AT+CIPDOMAIN=\"%s\"\r\n", host_name); if (echo.status != AT_ECHO_STATUS_OK) { return -1; @@ -349,6 +355,37 @@ static int esp8266_parse_domain(const char *host_name, char *host_ip, size_t hos return 0; } +static int esp8266_get_local_ip(char *ip, char *gw, char *mask) +{ + char *str; + at_echo_t echo; + char echo_buffer[100]; + int seg1, seg2, seg3, seg4; + + tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL); + tos_at_cmd_exec(AT_AGENT, &echo, 2000, "AT+CIFSR\r\n"); + + if (echo.status != AT_ECHO_STATUS_OK) { + return -1; + } + + /* + +CIFSR:STAIP, + +CIFSR:STAMAC, + */ + str = strstr((const char *)echo.buffer, "STAIP"); + if (!str) { + return -1; + } + sscanf(str + strlen("STAIP,"), "\"%d.%d.%d.%d\"", &seg1, &seg2, &seg3, &seg4); + sprintf(ip, "%d.%d.%d.%d", seg1, seg2, seg3, seg4); + printf("GOT MODULE STA IP: %s\n", ip); + + /* esp8266 has not provide the ability to query the subnet mask and gw ip address*/ + + return 0; +} + static int esp8266_init(void) { printf("Init ESP8266 ...\n" ); @@ -384,6 +421,10 @@ static int esp8266_init(void) return -1; } #endif + + if (esp8266_join_ap(ESP8266_STA_SSID, ESP8266_STA_PASSWD) != 0) { + return -1; + } printf("Init ESP8266 Done\n" ); return 0; @@ -405,7 +446,7 @@ __STATIC__ void esp8266_incoming_data_process(void) */ #if TOS_CFG_MODULE_SINGLE_LINK_EN == 0u while (1) { - if (tos_at_uart_read(&data, 1) != 1) { + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } if (data == ',') { @@ -416,7 +457,7 @@ __STATIC__ void esp8266_incoming_data_process(void) #endif while (1) { - if (tos_at_uart_read(&data, 1) != 1) { + if (tos_at_uart_read(AT_AGENT, &data, 1) != 1) { return; } if (data == ':') { @@ -428,11 +469,11 @@ __STATIC__ void esp8266_incoming_data_process(void) do { #define MIN(a, b) ((a) < (b) ? (a) : (b)) read_len = MIN(data_len, sizeof(buffer)); - if (tos_at_uart_read(buffer, read_len) != read_len) { + if (tos_at_uart_read(AT_AGENT, buffer, read_len) != read_len) { return; } - if (tos_at_channel_write(channel_id, buffer, read_len) <= 0) { + if (tos_at_channel_write(AT_AGENT, channel_id, buffer, read_len) <= 0) { return; } @@ -445,6 +486,10 @@ at_event_t esp8266_at_event[] = { }; sal_module_t sal_module_esp8266 = { + .sal_module_name = "esp8266", + .sal_module_ip = "0.0.0.0", + .sal_module_type = TOS_SAL_MODULE_WIFI, + .sal_module_status = TOS_SAL_MODULE_UNAVAILABLE, .init = esp8266_init, .connect = esp8266_connect, .send = esp8266_send, @@ -459,8 +504,10 @@ sal_module_t sal_module_esp8266 = { int esp8266_sal_init(hal_uart_port_t uart_port) { - if (tos_at_init(uart_port, esp8266_at_event, - sizeof(esp8266_at_event) / sizeof(esp8266_at_event[0])) != 0) { + char ip[15] = {0}; + + if (tos_at_init(AT_AGENT, esp8266_at_parser_task_stack, uart_port, esp8266_at_event, + sizeof(esp8266_at_event) / sizeof(esp8266_at_event[0])) != 0) { return -1; } @@ -468,9 +515,19 @@ int esp8266_sal_init(hal_uart_port_t uart_port) return -1; } - if (tos_sal_module_init() != 0) { + if (esp8266_init() != 0){ + return -1; + } + + if (esp8266_get_local_ip(ip, NULL, NULL) != 0) { return -1; } + + tos_sal_module_set_local_ip(&sal_module_esp8266, ip); + + tos_sal_module_set_status(&sal_module_esp8266, TOS_SAL_MODULE_AVAILABLE); + + printf("%s sal device register success\r\n", sal_module_esp8266.sal_module_name); return 0; } @@ -483,9 +540,15 @@ int esp8266_sal_deinit() tos_sal_module_close(id); } - tos_sal_module_register_default(); + tos_sal_module_set_local_ip(&sal_module_esp8266, "0.0.0.0"); - tos_at_deinit(); + tos_sal_module_set_status(&sal_module_esp8266, TOS_SAL_MODULE_UNAVAILABLE); + + tos_sal_module_unregister(&sal_module_esp8266); + + tos_at_deinit(AT_AGENT); + + printf("%s sal device unregister success\r\n", sal_module_esp8266.sal_module_name); return 0; } diff --git a/devices/esp8266/esp8266.h b/devices/esp8266/esp8266.h index c68d3aaa..8ca25fe9 100644 --- a/devices/esp8266/esp8266.h +++ b/devices/esp8266/esp8266.h @@ -20,6 +20,9 @@ #include "tos_hal.h" +#define ESP8266_STA_SSID "Mculover666" +#define ESP8266_STA_PASSWD "mculover666" + typedef enum esp8266_net_mode { ESP8266_NET_MODE_STA, ESP8266_NET_MODE_AP, diff --git a/examples/tcp_through_module/tcp_through_module.c b/examples/tcp_through_module/tcp_through_module.c index 7d15a127..2fc4bb85 100644 --- a/examples/tcp_through_module/tcp_through_module.c +++ b/examples/tcp_through_module/tcp_through_module.c @@ -5,7 +5,7 @@ #define USE_ESP8266 //#define USE_SIM800A //#define USE_BC26 -//#define USE_EC20 +#define USE_EC20 #ifdef USE_ESP8266 #include "esp8266.h" @@ -85,15 +85,17 @@ void application_entry(void *arg) { #if defined(USE_ESP8266) esp8266_sal_init(HAL_UART_PORT_0); - esp8266_join_ap("SheldonDai", "srnr6x9xbhmb0"); #elif defined(USE_SIM800A) sim800a_power_on(); sim800a_sal_init(HAL_UART_PORT_2); #elif defined(USE_BC26) bc26_sal_init(HAL_UART_PORT_2); #elif defined(USE_EC20) - ec20_sal_deinit(HAL_PORT_0); + ec20_sal_init(HAL_UART_PORT_3); #endif + + //tos_sal_module_set_default("esp8266"); + tos_sal_module_set_default("ec20"); socket_id_0 = tos_sal_module_connect("117.50.111.72", "8080", TOS_SAL_PROTO_TCP); if (socket_id_0 == -1) { diff --git a/net/at/include/tos_at.h b/net/at/include/tos_at.h index a119fa42..709f5ac2 100644 --- a/net/at/include/tos_at.h +++ b/net/at/include/tos_at.h @@ -25,10 +25,10 @@ #define AT_AGENT_ECHO_FAIL "FAIL" #define AT_AGENT_ECHO_ERROR "ERROR" -#define AT_DATA_CHANNEL_NUM 6 -#define AT_DATA_CHANNEL_FIFO_BUFFER_SIZE (2048 + 1024) +#define AT_DATA_CHANNEL_NUM 2 +#define AT_DATA_CHANNEL_FIFO_BUFFER_SIZE (2048) -#define AT_UART_RX_FIFO_BUFFER_SIZE (2048 + 1024) +#define AT_UART_RX_FIFO_BUFFER_SIZE (2048) #define AT_RECV_CACHE_SIZE 2048 #define AT_CMD_BUFFER_SIZE 512 @@ -128,14 +128,13 @@ typedef struct at_agent_st { uint8_t *uart_rx_fifo_buffer; } at_agent_t; -#define AT_AGENT ((at_agent_t *)(&at_agent)) - /** * @brief Write data to a channel. * Write data to a channel with certain id. * * @attention None * + * @param[in] at_agent at agent object * @param[in] channel_id id of the channel. * @param[in] buffer data buffer to write. * @param[in] buffer_len length of the buffer. @@ -144,7 +143,7 @@ typedef struct at_agent_st { * @retval -1 write failed(error). * @retval none -1 the number of bytes written. */ -__API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_len); +__API__ int tos_at_channel_write(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len); /** * @brief Read data from a channel. @@ -161,7 +160,7 @@ __API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_ * @retval -1 read failed(error). * @retval none -1 the number of bytes read. */ -__API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout); +__API__ int tos_at_channel_read_timed(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout); /** * @brief Read data from a channel. @@ -177,7 +176,7 @@ __API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t bu * @retval -1 read failed(error). * @retval none -1 the number of bytes read. */ -__API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_len); +__API__ int tos_at_channel_read(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len); /** * @brief Allocate a channel. @@ -193,7 +192,7 @@ __API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_l * @retval -1 allocate failed(error). * @retval none -1 the id of the channel. */ -__API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *port); +__API__ int tos_at_channel_alloc_id(at_agent_t *at_agent, int channel_id, const char *ip, const char *port); /** * @brief Allocate a channel. @@ -208,7 +207,7 @@ __API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char * * @retval -1 allocate failed(error). * @retval none -1 the id of the channel. */ -__API__ int tos_at_channel_alloc(const char *ip, const char *port); +__API__ int tos_at_channel_alloc(at_agent_t *at_agent, const char *ip, const char *port); /** * @brief Free a channel. @@ -222,7 +221,7 @@ __API__ int tos_at_channel_alloc(const char *ip, const char *port); * @retval -1 free failed(error). * @retval 0 free successfully. */ -__API__ int tos_at_channel_free(int channel_id); +__API__ int tos_at_channel_free(at_agent_t *at_agent, int channel_id); /** * @brief Set channel broken. @@ -235,7 +234,7 @@ __API__ int tos_at_channel_free(int channel_id); * @retval -1 set failed(error). * @retval 0 set successfully. */ -__API__ int tos_at_channel_set_broken(int channel_id); +__API__ int tos_at_channel_set_broken(at_agent_t *at_agent, int channel_id); /** * @brief Judge whether channel is working. @@ -246,7 +245,7 @@ __API__ int tos_at_channel_set_broken(int channel_id); * * @return at channel status(type of at_channel_status_t) */ -__API__ int tos_at_channel_is_working(int channel_id); +__API__ int tos_at_channel_is_working(at_agent_t *at_agent, int channel_id); /** * @brief Initialize the at framework. @@ -261,7 +260,7 @@ __API__ int tos_at_channel_is_working(int channel_id); * @retval -1 initialize failed(error). * @retval 0 initialize successfully. */ -__API__ int tos_at_init(hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size); +__API__ int tos_at_init(at_agent_t *at_agent, k_stack_t *stk, hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size); /** * @brief De-initialize the at framework. @@ -271,7 +270,7 @@ __API__ int tos_at_init(hal_uart_port_t uart_port, at_event_t *event_table, size * @return None */ -__API__ void tos_at_deinit(void); +__API__ void tos_at_deinit(at_agent_t *at_agent); /** * @brief Create a echo struct. @@ -318,7 +317,7 @@ __API__ int tos_at_echo_fuzzy_matching_create(at_echo_t *echo, char *buffer, siz * @retval -1 execute failed(error). * @retval 0 execute successfully. */ -__API__ int tos_at_cmd_exec(at_echo_t *echo, uint32_t timeout, const char *cmd, ...); +__API__ int tos_at_cmd_exec(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...); /** * @brief Execute an at command. @@ -334,7 +333,7 @@ __API__ int tos_at_cmd_exec(at_echo_t *echo, uint32_t timeout, const char *cmd, * @retval -1 execute failed(error). * @retval 0 execute successfully. */ -__API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char *cmd, ...); +__API__ int tos_at_cmd_exec_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...); /** * @brief Send raw data througth uart. @@ -350,7 +349,7 @@ __API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char * @retval -1 execute failed(error). * @retval 0 execute successfully. */ -__API__ int tos_at_raw_data_send(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size); +__API__ int tos_at_raw_data_send(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size); /** * @brief Send raw data througth uart. @@ -367,7 +366,7 @@ __API__ int tos_at_raw_data_send(at_echo_t *echo, uint32_t timeout, const uint8_ * @retval -1 execute failed(error). * @retval 0 execute successfully. */ -__API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size); +__API__ int tos_at_raw_data_send_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size); /** * @brief Write byte to the at uart. @@ -379,7 +378,7 @@ __API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const * * @return None */ -__API__ void tos_at_uart_input_byte(uint8_t data); +__API__ void tos_at_uart_input_byte(at_agent_t *at_agent, uint8_t data); /** * @brief A global lock provided by at framework. @@ -393,7 +392,7 @@ __API__ void tos_at_uart_input_byte(uint8_t data); * @retval -1 pend failed(error). * @retval 0 pend successfully. */ -__API__ int tos_at_global_lock_pend(void); +__API__ int tos_at_global_lock_pend(at_agent_t *at_agent); /** * @brief A global lock provided by at framework. @@ -407,7 +406,7 @@ __API__ int tos_at_global_lock_pend(void); * @retval -1 post failed(error). * @retval 0 post successfully. */ -__API__ int tos_at_global_lock_post(void); +__API__ int tos_at_global_lock_post(at_agent_t *at_agent); /** * @brief Read data from the uart. @@ -420,7 +419,7 @@ __API__ int tos_at_global_lock_post(void); * * @return length of the data read from the uart. */ -__API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len); +__API__ int tos_at_uart_read(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len); /** * @brief Read data from the uart. @@ -433,7 +432,7 @@ __API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len); * * @return length of the data read from the uart. */ -__API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len); +__API__ int tos_at_uart_readline(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len); /** * @brief Read data from the uart. @@ -446,7 +445,7 @@ __API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len); * * @return length of the data read from the uart. */ -__API__ int tos_at_uart_drain(uint8_t *buffer, size_t buffer_len); +__API__ int tos_at_uart_drain(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len); /** * @brief Get the remote ip of a channel. diff --git a/net/at/src/tos_at.c b/net/at/src/tos_at.c index cead9cf6..92b14297 100644 --- a/net/at/src/tos_at.c +++ b/net/at/src/tos_at.c @@ -17,55 +17,51 @@ #include "tos_at.h" -__STATIC__ at_agent_t at_agent; - -__STATIC__ k_stack_t at_parser_task_stack[AT_PARSER_TASK_STACK_SIZE]; - -__API__ int tos_at_global_lock_pend(void) +__API__ int tos_at_global_lock_pend(at_agent_t *at_agent) { - if (tos_mutex_pend(&AT_AGENT->global_lock) != K_ERR_NONE) { + if (tos_mutex_pend(&at_agent->global_lock) != K_ERR_NONE) { return -1; } return 0; } -__API__ int tos_at_global_lock_post(void) +__API__ int tos_at_global_lock_post(at_agent_t *at_agent) { - if (tos_mutex_post(&AT_AGENT->global_lock) != K_ERR_NONE) { + if (tos_mutex_post(&at_agent->global_lock) != K_ERR_NONE) { return -1; } return 0; } -__STATIC__ int at_uart_getchar(uint8_t *data, k_tick_t timeout) +__STATIC__ int at_uart_getchar(at_agent_t *at_agent, uint8_t *data, k_tick_t timeout) { k_err_t err; tos_stopwatch_delay(1); - if (tos_sem_pend(&AT_AGENT->uart_rx_sem, timeout) != K_ERR_NONE) { + if (tos_sem_pend(&at_agent->uart_rx_sem, timeout) != K_ERR_NONE) { return -1; } - if (tos_mutex_pend(&AT_AGENT->uart_rx_lock) != K_ERR_NONE) { + if (tos_mutex_pend(&at_agent->uart_rx_lock) != K_ERR_NONE) { return -1; } - err = tos_chr_fifo_pop(&AT_AGENT->uart_rx_fifo, data); + err = tos_chr_fifo_pop(&at_agent->uart_rx_fifo, data); - tos_mutex_post(&AT_AGENT->uart_rx_lock); + tos_mutex_post(&at_agent->uart_rx_lock); return err == K_ERR_NONE ? 0 : -1; } -__STATIC__ at_event_t *at_event_do_get(char *buffer, size_t buffer_len) +__STATIC__ at_event_t *at_event_do_get(at_agent_t *at_agent, char *buffer, size_t buffer_len) { int i = 0; at_event_t *event_table = K_NULL, *event = K_NULL; size_t event_table_size = 0, event_len; - event_table = AT_AGENT->event_table; - event_table_size = AT_AGENT->event_table_size; + event_table = at_agent->event_table; + event_table_size = at_agent->event_table_size; for (i = 0; i < event_table_size; ++i) { event = &event_table[i]; @@ -83,27 +79,27 @@ __STATIC__ at_event_t *at_event_do_get(char *buffer, size_t buffer_len) return K_NULL; } -__STATIC__ at_event_t *at_get_event(void) +__STATIC__ at_event_t *at_get_event(at_agent_t *at_agent) { char *buffer; size_t buffer_len; at_cache_t *at_cache = K_NULL; - at_cache = &AT_AGENT->recv_cache; + at_cache = &at_agent->recv_cache; buffer = (char *)at_cache->buffer; buffer_len = at_cache->recv_len; - return at_event_do_get(buffer, buffer_len); + return at_event_do_get(at_agent, buffer, buffer_len); } -__API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len) +__API__ int tos_at_uart_read(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len) { uint8_t data; size_t read_len = 0; while (K_TRUE) { - if (at_uart_getchar(&data, TOS_TIME_FOREVER) != 0) { + if (at_uart_getchar(at_agent, &data, TOS_TIME_FOREVER) != 0) { return read_len; } @@ -115,13 +111,13 @@ __API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len) } } -__API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len) +__API__ int tos_at_uart_readline(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len) { uint8_t data; size_t read_len = 0; while (K_TRUE) { - if (at_uart_getchar(&data, TOS_TIME_FOREVER) != 0) { + if (at_uart_getchar(at_agent, &data, TOS_TIME_FOREVER) != 0) { return read_len; } @@ -135,13 +131,13 @@ __API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len) } } -__API__ int tos_at_uart_drain(uint8_t *buffer, size_t buffer_len) +__API__ int tos_at_uart_drain(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len) { uint8_t data; size_t read_len = 0; while (K_TRUE) { - if (at_uart_getchar(&data, TOS_TIME_NOWAIT) != 0) { + if (at_uart_getchar(at_agent, &data, TOS_TIME_NOWAIT) != 0) { return read_len; } @@ -153,20 +149,20 @@ __API__ int tos_at_uart_drain(uint8_t *buffer, size_t buffer_len) } } -__STATIC__ int at_is_echo_expect(void) +__STATIC__ int at_is_echo_expect(at_agent_t *at_agent) { char *recv_buffer, *expect; size_t recv_buffer_len, expect_len; at_echo_t *at_echo = K_NULL; at_cache_t *at_cache = K_NULL; - at_echo = AT_AGENT->echo; + at_echo = at_agent->echo; if (!at_echo || !at_echo->echo_expect) { return 0; } - at_cache = &AT_AGENT->recv_cache; + at_cache = &at_agent->recv_cache; recv_buffer = (char *)at_cache->buffer; recv_buffer_len = at_cache->recv_len; @@ -192,19 +188,19 @@ __STATIC__ int at_is_echo_expect(void) return 0; } -__STATIC__ at_parse_status_t at_uart_line_parse(void) +__STATIC__ at_parse_status_t at_uart_line_parse(at_agent_t *at_agent) { size_t curr_len = 0; uint8_t data, last_data = 0; at_cache_t *recv_cache = K_NULL; - recv_cache = &AT_AGENT->recv_cache; + recv_cache = &at_agent->recv_cache; recv_cache->recv_len = 0; memset(recv_cache->buffer, 0, recv_cache->buffer_size); while (K_TRUE) { - if (at_uart_getchar(&data, TOS_TIME_FOREVER) != 0) { + if (at_uart_getchar(at_agent, &data, TOS_TIME_FOREVER) != 0) { continue; } @@ -220,11 +216,11 @@ __STATIC__ at_parse_status_t at_uart_line_parse(void) return AT_PARSE_STATUS_OVERFLOW; } - if (at_get_event() != K_NULL) { + if (at_get_event(at_agent) != K_NULL) { return AT_PARSE_STATUS_EVENT; } - if (at_is_echo_expect()) { + if (at_is_echo_expect(at_agent)) { return AT_PARSE_STATUS_EXPECT; } @@ -247,12 +243,12 @@ __STATIC__ at_parse_status_t at_uart_line_parse(void) } } -__STATIC__ void at_echo_status_set(at_echo_t *echo) +__STATIC__ void at_echo_status_set(at_agent_t *at_agent, at_echo_t *echo) { char *buffer; at_cache_t *at_cache; - at_cache = &AT_AGENT->recv_cache; + at_cache = &at_agent->recv_cache; buffer = (char *)at_cache->buffer; @@ -292,10 +288,12 @@ __STATIC__ void at_parser(void *arg) at_cache_t *recv_cache = K_NULL; at_parse_status_t at_parse_status; - recv_cache = &AT_AGENT->recv_cache; + at_agent_t *at_agent = (at_agent_t *)arg; + + recv_cache = &at_agent->recv_cache; while (K_TRUE) { - at_parse_status = at_uart_line_parse(); + at_parse_status = at_uart_line_parse(at_agent); if (at_parse_status == AT_PARSE_STATUS_OVERFLOW) { tos_kprintln("AT parse overflow!"); @@ -303,14 +301,14 @@ __STATIC__ void at_parser(void *arg) } if (at_parse_status == AT_PARSE_STATUS_EVENT) { - at_event = at_get_event(); + at_event = at_get_event(at_agent); if (at_event && at_event->event_callback) { at_event->event_callback(); } continue; } - at_echo = AT_AGENT->echo; + at_echo = at_agent->echo; if (!at_echo) { continue; } @@ -322,7 +320,7 @@ __STATIC__ void at_parser(void *arg) } } else if (at_parse_status == AT_PARSE_STATUS_NEWLINE && at_echo->status == AT_ECHO_STATUS_NONE) { - at_echo_status_set(at_echo); + at_echo_status_set(at_agent, at_echo); } if (at_echo->buffer) { @@ -333,13 +331,13 @@ __STATIC__ void at_parser(void *arg) } } -__STATIC__ int at_uart_send(const uint8_t *buf, size_t size, uint32_t timeout) +__STATIC__ int at_uart_send(at_agent_t *at_agent, const uint8_t *buf, size_t size, uint32_t timeout) { int ret; - tos_mutex_pend(&AT_AGENT->uart_tx_lock); - ret = tos_hal_uart_write(&AT_AGENT->uart, buf, size, timeout); - tos_mutex_post(&AT_AGENT->uart_tx_lock); + tos_mutex_pend(&at_agent->uart_tx_lock); + ret = tos_hal_uart_write(&at_agent->uart, buf, size, timeout); + tos_mutex_post(&at_agent->uart_tx_lock); return ret; } @@ -393,30 +391,30 @@ __STATIC_INLINE__ void at_echo_flush(at_echo_t *echo) echo->__w_idx = 0; } -__STATIC_INLINE__ void at_echo_attach(at_echo_t *echo) +__STATIC_INLINE__ void at_echo_attach(at_agent_t *at_agent, at_echo_t *echo) { at_echo_flush(echo); - AT_AGENT->echo = echo; + at_agent->echo = echo; } -__API__ int tos_at_raw_data_send(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size) +__API__ int tos_at_raw_data_send(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size) { int ret = 0; if (echo) { - at_echo_attach(echo); + at_echo_attach(at_agent, echo); } - ret = at_uart_send(buf, size, 0xFFFF); + ret = at_uart_send(at_agent, buf, size, 0xFFFF); tos_task_delay(tos_millisec2tick(timeout)); - AT_AGENT->echo = K_NULL; + at_agent->echo = K_NULL; return ret; } -__API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size) +__API__ int tos_at_raw_data_send_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size) { int ret = 0; @@ -428,9 +426,9 @@ __API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const return -1; } echo->__is_expecting = K_TRUE; - at_echo_attach(echo); + at_echo_attach(at_agent, echo); - ret = at_uart_send(buf, size, 0xFFFF); + ret = at_uart_send(at_agent, buf, size, 0xFFFF); if (tos_sem_pend(&echo->__expect_notify, tos_millisec2tick(timeout)) != K_ERR_NONE) { ret = -1; @@ -438,56 +436,56 @@ __API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const tos_sem_destroy(&echo->__expect_notify); - AT_AGENT->echo = K_NULL; + at_agent->echo = K_NULL; return ret; } -__STATIC__ int at_cmd_do_exec(const char *format, va_list args) +__STATIC__ int at_cmd_do_exec(at_agent_t *at_agent, const char *format, va_list args) { size_t cmd_len = 0; - if (tos_mutex_pend(&AT_AGENT->cmd_buf_lock) != K_ERR_NONE) { + if (tos_mutex_pend(&at_agent->cmd_buf_lock) != K_ERR_NONE) { return -1; } - cmd_len = vsnprintf(AT_AGENT->cmd_buf, AT_CMD_BUFFER_SIZE, format, args); + cmd_len = vsnprintf(at_agent->cmd_buf, AT_CMD_BUFFER_SIZE, format, args); - printf("AT CMD:\n%s\n", AT_AGENT->cmd_buf); + printf("AT CMD:\n%s\n", at_agent->cmd_buf); - at_uart_send((uint8_t *)AT_AGENT->cmd_buf, cmd_len, 0xFFFF); + at_uart_send(at_agent, (uint8_t *)at_agent->cmd_buf, cmd_len, 0xFFFF); - tos_mutex_post(&AT_AGENT->cmd_buf_lock); + tos_mutex_post(&at_agent->cmd_buf_lock); return 0; } -__API__ int tos_at_cmd_exec(at_echo_t *echo, uint32_t timeout, const char *cmd, ...) +__API__ int tos_at_cmd_exec(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...) { int ret = 0; va_list args; if (echo) { - at_echo_attach(echo); + at_echo_attach(at_agent, echo); } va_start(args, cmd); - ret = at_cmd_do_exec(cmd, args); + ret = at_cmd_do_exec(at_agent, cmd, args); va_end(args); if (ret != 0) { - AT_AGENT->echo = K_NULL; + at_agent->echo = K_NULL; return -1; } tos_task_delay(tos_millisec2tick(timeout)); - AT_AGENT->echo = K_NULL; + at_agent->echo = K_NULL; return 0; } -__API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char *cmd, ...) +__API__ int tos_at_cmd_exec_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...) { int ret = 0; va_list args; @@ -500,14 +498,14 @@ __API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char return -1; } echo->__is_expecting = K_TRUE; - at_echo_attach(echo); + at_echo_attach(at_agent, echo); va_start(args, cmd); - ret = at_cmd_do_exec(cmd, args); + ret = at_cmd_do_exec(at_agent, cmd, args); va_end(args); if (ret != 0) { - AT_AGENT->echo = K_NULL; + at_agent->echo = K_NULL; return -1; } @@ -517,42 +515,42 @@ __API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char tos_sem_destroy(&echo->__expect_notify); - AT_AGENT->echo = K_NULL; + at_agent->echo = K_NULL; return ret; } -__STATIC__ int at_recv_cache_init(void) +__STATIC__ int at_recv_cache_init(at_agent_t *at_agent) { uint8_t *buffer = K_NULL; buffer = tos_mmheap_alloc(AT_RECV_CACHE_SIZE); if (!buffer) { - AT_AGENT->recv_cache.buffer = K_NULL; + at_agent->recv_cache.buffer = K_NULL; return - 1; } - AT_AGENT->recv_cache.buffer = buffer; - AT_AGENT->recv_cache.buffer_size = AT_RECV_CACHE_SIZE; - AT_AGENT->recv_cache.recv_len = 0; + at_agent->recv_cache.buffer = buffer; + at_agent->recv_cache.buffer_size = AT_RECV_CACHE_SIZE; + at_agent->recv_cache.recv_len = 0; return 0; } -__STATIC__ void at_recv_cache_deinit(void) +__STATIC__ void at_recv_cache_deinit(at_agent_t *at_agent) { uint8_t *buffer = K_NULL; - buffer = AT_AGENT->recv_cache.buffer; + buffer = at_agent->recv_cache.buffer; if (buffer) { tos_mmheap_free(buffer); } - AT_AGENT->recv_cache.buffer = K_NULL; - AT_AGENT->recv_cache.buffer_size = 0; - AT_AGENT->recv_cache.recv_len = 0; + at_agent->recv_cache.buffer = K_NULL; + at_agent->recv_cache.buffer_size = 0; + at_agent->recv_cache.recv_len = 0; } -__STATIC__ at_data_channel_t *at_channel_get(int channel_id, int is_alloc) +__STATIC__ at_data_channel_t *at_channel_get(at_agent_t *at_agent, int channel_id, int is_alloc) { /* if is_alloc is K_TRUE, means we are allocating a channel with certain id, @@ -566,7 +564,7 @@ __STATIC__ at_data_channel_t *at_channel_get(int channel_id, int is_alloc) return K_NULL; } - data_channel = &AT_AGENT->data_channel[channel_id]; + data_channel = &at_agent->data_channel[channel_id]; if (is_alloc && data_channel->is_free) { return data_channel; @@ -579,13 +577,13 @@ __STATIC__ at_data_channel_t *at_channel_get(int channel_id, int is_alloc) return K_NULL; } -__API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_len) +__API__ int tos_at_channel_read(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len) { int read_len; size_t total_read_len = 0; at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); if (!data_channel || data_channel->status == AT_CHANNEL_STATUS_BROKEN) { return -1; } @@ -608,23 +606,23 @@ __API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_l } } -__API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout) +__API__ int tos_at_channel_read_timed(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout) { int read_len = 0; size_t total_read_len = 0; k_tick_t tick, remain_tick; at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); if (!data_channel || data_channel->status == AT_CHANNEL_STATUS_BROKEN) { return -1; } tick = tos_millisec2tick(timeout); - tos_stopwatch_countdown(&AT_AGENT->timer, tick); - while (!tos_stopwatch_is_expired(&AT_AGENT->timer)) { - remain_tick = tos_stopwatch_remain(&AT_AGENT->timer); + tos_stopwatch_countdown(&at_agent->timer, tick); + while (!tos_stopwatch_is_expired(&at_agent->timer)) { + remain_tick = tos_stopwatch_remain(&at_agent->timer); if (remain_tick == (k_tick_t)0u) { return total_read_len; } @@ -648,12 +646,12 @@ __API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t bu return total_read_len; } -__API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_len) +__API__ int tos_at_channel_write(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len) { int ret; at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); if (!data_channel) { return -1; } @@ -697,11 +695,11 @@ errout: return -1; } -__API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *port) +__API__ int tos_at_channel_alloc_id(at_agent_t *at_agent, int channel_id, const char *ip, const char *port) { at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_TRUE); + data_channel = at_channel_get(at_agent, channel_id, K_TRUE); if (!data_channel) { return -1; } @@ -713,13 +711,13 @@ __API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char * return channel_id; } -__API__ int tos_at_channel_alloc(const char *ip, const char *port) +__API__ int tos_at_channel_alloc(at_agent_t *at_agent, const char *ip, const char *port) { int id = 0; at_data_channel_t *data_channel = K_NULL; for (id = 0; id < AT_DATA_CHANNEL_NUM; ++id) { - data_channel = &AT_AGENT->data_channel[id]; + data_channel = &at_agent->data_channel[id]; if (data_channel->is_free) { break; } @@ -736,11 +734,11 @@ __API__ int tos_at_channel_alloc(const char *ip, const char *port) return id; } -__API__ int tos_at_channel_free(int channel_id) +__API__ int tos_at_channel_free(at_agent_t *at_agent, int channel_id) { at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); if (!data_channel) { return -1; } @@ -758,11 +756,11 @@ __API__ int tos_at_channel_free(int channel_id) return 0; } -__API__ int tos_at_channel_set_broken(int channel_id) +__API__ int tos_at_channel_set_broken(at_agent_t *at_agent, int channel_id) { at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); if (!data_channel) { return -1; } @@ -771,39 +769,39 @@ __API__ int tos_at_channel_set_broken(int channel_id) return 0; } -__API__ int tos_at_channel_is_working(int channel_id) +__API__ int tos_at_channel_is_working(at_agent_t *at_agent, int channel_id) { at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); return data_channel && data_channel->status == AT_CHANNEL_STATUS_WORKING; } -__STATIC__ void at_channel_init(void) +__STATIC__ void at_channel_init(at_agent_t *at_agent) { int i = 0; for (i = 0; i < AT_DATA_CHANNEL_NUM; ++i) { - memset(&AT_AGENT->data_channel[i], 0, sizeof(at_data_channel_t)); - AT_AGENT->data_channel[i].is_free = K_TRUE; - AT_AGENT->data_channel[i].status = AT_CHANNEL_STATUS_HANGING; + memset(&at_agent->data_channel[i], 0, sizeof(at_data_channel_t)); + at_agent->data_channel[i].is_free = K_TRUE; + at_agent->data_channel[i].status = AT_CHANNEL_STATUS_HANGING; } } -__STATIC__ void at_channel_deinit(void) +__STATIC__ void at_channel_deinit(at_agent_t *at_agent) { int i = 0; for (i = 0; i < AT_DATA_CHANNEL_NUM; ++i) { - tos_at_channel_free(i); + tos_at_channel_free(at_agent, i); } } -__API__ const char *tos_at_channel_ip_get(int channel_id) +__API__ const char *tos_at_channel_ip_get(at_agent_t *at_agent, int channel_id) { at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); if (!data_channel) { return K_NULL; } @@ -811,11 +809,11 @@ __API__ const char *tos_at_channel_ip_get(int channel_id) return data_channel[channel_id].remote_ip; } -__API__ const char *tos_at_channel_port_get(int channel_id) +__API__ const char *tos_at_channel_port_get(at_agent_t *at_agent, int channel_id) { at_data_channel_t *data_channel = K_NULL; - data_channel = at_channel_get(channel_id, K_FALSE); + data_channel = at_channel_get(at_agent, channel_id, K_FALSE); if (!data_channel) { return K_NULL; } @@ -823,142 +821,142 @@ __API__ const char *tos_at_channel_port_get(int channel_id) return data_channel[channel_id].remote_port; } -__STATIC__ void at_event_table_set(at_event_t *event_table, size_t event_table_size) +__STATIC__ void at_event_table_set(at_agent_t *at_agent, at_event_t *event_table, size_t event_table_size) { - AT_AGENT->event_table = event_table; - AT_AGENT->event_table_size = event_table_size; + at_agent->event_table = event_table; + at_agent->event_table_size = event_table_size; } -__API__ int tos_at_init(hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size) +__API__ int tos_at_init(at_agent_t *at_agent, k_stack_t *stk, hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size) { void *buffer = K_NULL; - memset(AT_AGENT, 0, sizeof(at_agent_t)); + memset(at_agent, 0, sizeof(at_agent_t)); - at_event_table_set(event_table, event_table_size); + at_event_table_set(at_agent, event_table, event_table_size); - at_channel_init(); + at_channel_init(at_agent); - tos_stopwatch_create(&AT_AGENT->timer); + tos_stopwatch_create(&at_agent->timer); buffer = tos_mmheap_alloc(AT_UART_RX_FIFO_BUFFER_SIZE); if (!buffer) { return -1; } - AT_AGENT->uart_rx_fifo_buffer = (uint8_t *)buffer; - tos_chr_fifo_create(&AT_AGENT->uart_rx_fifo, buffer, AT_UART_RX_FIFO_BUFFER_SIZE); + at_agent->uart_rx_fifo_buffer = (uint8_t *)buffer; + tos_chr_fifo_create(&at_agent->uart_rx_fifo, buffer, AT_UART_RX_FIFO_BUFFER_SIZE); buffer = tos_mmheap_alloc(AT_CMD_BUFFER_SIZE); if (!buffer) { goto errout0; } - AT_AGENT->cmd_buf = (char *)buffer; + at_agent->cmd_buf = (char *)buffer; - if (tos_mutex_create(&AT_AGENT->cmd_buf_lock) != K_ERR_NONE) { + if (tos_mutex_create(&at_agent->cmd_buf_lock) != K_ERR_NONE) { goto errout1; } - if (at_recv_cache_init() != 0) { + if (at_recv_cache_init(at_agent) != 0) { goto errout2; } - if (tos_sem_create(&AT_AGENT->uart_rx_sem, (k_sem_cnt_t)0u) != K_ERR_NONE) { + if (tos_sem_create(&at_agent->uart_rx_sem, (k_sem_cnt_t)0u) != K_ERR_NONE) { goto errout3; } - if (tos_mutex_create(&AT_AGENT->uart_rx_lock) != K_ERR_NONE) { + if (tos_mutex_create(&at_agent->uart_rx_lock) != K_ERR_NONE) { goto errout4; } - if (tos_mutex_create(&AT_AGENT->uart_tx_lock) != K_ERR_NONE) { + if (tos_mutex_create(&at_agent->uart_tx_lock) != K_ERR_NONE) { goto errout5; } - if (tos_task_create(&AT_AGENT->parser, "at_parser", at_parser, - K_NULL, AT_PARSER_TASK_PRIO, at_parser_task_stack, + if (tos_task_create(&at_agent->parser, "at_parser", at_parser, + at_agent, AT_PARSER_TASK_PRIO, stk, AT_PARSER_TASK_STACK_SIZE, 0) != K_ERR_NONE) { goto errout6; } - if (tos_hal_uart_init(&AT_AGENT->uart, uart_port) != 0) { + if (tos_hal_uart_init(&at_agent->uart, uart_port) != 0) { goto errout7; } - if (tos_mutex_create(&AT_AGENT->global_lock) != K_ERR_NONE) { + if (tos_mutex_create(&at_agent->global_lock) != K_ERR_NONE) { goto errout8; } return 0; errout8: - tos_hal_uart_deinit(&AT_AGENT->uart); + tos_hal_uart_deinit(&at_agent->uart); errout7: - tos_task_destroy(&AT_AGENT->parser); + tos_task_destroy(&at_agent->parser); errout6: - tos_mutex_destroy(&AT_AGENT->uart_tx_lock); + tos_mutex_destroy(&at_agent->uart_tx_lock); errout5: - tos_mutex_destroy(&AT_AGENT->uart_rx_lock); + tos_mutex_destroy(&at_agent->uart_rx_lock); errout4: - tos_sem_destroy(&AT_AGENT->uart_rx_sem); + tos_sem_destroy(&at_agent->uart_rx_sem); errout3: - at_recv_cache_deinit(); + at_recv_cache_deinit(at_agent); errout2: - tos_mutex_destroy(&AT_AGENT->cmd_buf_lock); + tos_mutex_destroy(&at_agent->cmd_buf_lock); errout1: - tos_mmheap_free(AT_AGENT->cmd_buf); - AT_AGENT->cmd_buf = K_NULL; + tos_mmheap_free(at_agent->cmd_buf); + at_agent->cmd_buf = K_NULL; errout0: - tos_mmheap_free(AT_AGENT->uart_rx_fifo_buffer); - AT_AGENT->uart_rx_fifo_buffer = K_NULL; - tos_chr_fifo_destroy(&AT_AGENT->uart_rx_fifo); + tos_mmheap_free(at_agent->uart_rx_fifo_buffer); + at_agent->uart_rx_fifo_buffer = K_NULL; + tos_chr_fifo_destroy(&at_agent->uart_rx_fifo); return -1; } -__API__ void tos_at_deinit(void) +__API__ void tos_at_deinit(at_agent_t *at_agent) { - tos_mutex_destroy(&AT_AGENT->global_lock); + tos_mutex_destroy(&at_agent->global_lock); - tos_hal_uart_deinit(&AT_AGENT->uart); + tos_hal_uart_deinit(&at_agent->uart); - tos_task_destroy(&AT_AGENT->parser); + tos_task_destroy(&at_agent->parser); - tos_mutex_destroy(&AT_AGENT->uart_tx_lock); + tos_mutex_destroy(&at_agent->uart_tx_lock); - tos_sem_destroy(&AT_AGENT->uart_rx_sem); + tos_sem_destroy(&at_agent->uart_rx_sem); - at_recv_cache_deinit(); + at_recv_cache_deinit(at_agent); - tos_mutex_destroy(&AT_AGENT->cmd_buf_lock); + tos_mutex_destroy(&at_agent->cmd_buf_lock); - tos_mmheap_free(AT_AGENT->cmd_buf); - AT_AGENT->cmd_buf = K_NULL; + tos_mmheap_free(at_agent->cmd_buf); + at_agent->cmd_buf = K_NULL; - tos_mmheap_free(AT_AGENT->uart_rx_fifo_buffer); - AT_AGENT->uart_rx_fifo_buffer = K_NULL; + tos_mmheap_free(at_agent->uart_rx_fifo_buffer); + at_agent->uart_rx_fifo_buffer = K_NULL; - tos_chr_fifo_destroy(&AT_AGENT->uart_rx_fifo); + tos_chr_fifo_destroy(&at_agent->uart_rx_fifo); - tos_stopwatch_destroy(&AT_AGENT->timer); + tos_stopwatch_destroy(&at_agent->timer); - at_channel_deinit(); + at_channel_deinit(at_agent); } /* To completely decouple the uart intterupt and at agent, we need a more powerful hal(driver framework), that would be a huge work, we place it in future plans. */ -__API__ void tos_at_uart_input_byte(uint8_t data) +__API__ void tos_at_uart_input_byte(at_agent_t *at_agent, uint8_t data) { - if (tos_chr_fifo_push(&AT_AGENT->uart_rx_fifo, data) == K_ERR_NONE) { - tos_sem_post(&AT_AGENT->uart_rx_sem); + if (tos_chr_fifo_push(&at_agent->uart_rx_fifo, data) == K_ERR_NONE) { + tos_sem_post(&at_agent->uart_rx_sem); } } diff --git a/net/sal_module_wrapper/sal_module_wrapper.c b/net/sal_module_wrapper/sal_module_wrapper.c index 5449b479..95503fc8 100644 --- a/net/sal_module_wrapper/sal_module_wrapper.c +++ b/net/sal_module_wrapper/sal_module_wrapper.c @@ -1,100 +1,161 @@ #include "sal_module_wrapper.h" -static sal_module_t *g_sal_module = NULL; +static sal_module_t *cur_sal_module = NULL; +static k_list_t sal_module_device_list; int tos_sal_module_register(sal_module_t *module) { - if (!g_sal_module) { - g_sal_module = module; + if (!module) { + return -1; + } + + if (sal_module_device_list.next == NULL || sal_module_device_list.prev == NULL) {\ + tos_list_init(&sal_module_device_list); + } + + tos_list_add_tail(&module->sal_module_list, &sal_module_device_list); + + return 0; +} + +int tos_sal_module_unregister(sal_module_t *module) +{ + if (!module) { + return -1; + } + + tos_list_del(&module->sal_module_list); + + return 0; +} + +int tos_sal_module_set_status(sal_module_t *module, sal_device_status_t status) +{ + if (module) { + module->sal_module_status = status; return 0; } return -1; } -int tos_sal_module_register_default() +void tos_sal_module_set_local_ip(sal_module_t *module, char* ip) { - g_sal_module = NULL; + if (module && ip) { + strncpy(module->sal_module_ip, ip, sizeof(module->sal_module_ip)); + } - return 0; + return; } -int tos_sal_module_init(void) +int tos_sal_module_set_default(char *sal_module_device_name) { - if (g_sal_module && g_sal_module->init) { - return g_sal_module->init(); + k_list_t *cur = NULL; + sal_module_t *sal_module = NULL; + + TOS_LIST_FOR_EACH(cur, &sal_module_device_list) { + sal_module = TOS_LIST_ENTRY(cur, sal_module_t, sal_module_list); + if (sal_module == NULL) { + continue; + } + else { + if (strcmp(sal_module->sal_module_name, sal_module_device_name) == 0) { +// tos_list_del(cur); +// tos_list_add(cur, &sal_module_device_list); + cur_sal_module = sal_module; + return 0; + } + } } + return -1; } +int tos_sal_module_get_type(void) +{ + if (cur_sal_module) { + return cur_sal_module->sal_module_type; + } + + return -1; +} + +// int tos_sal_module_init(void) +// { +// if (cur_sal_module && cur_sal_module->init) { +// return cur_sal_module->init(); +// } +// return -1; +// } + int tos_sal_module_parse_domain(const char *host_name, char *host_ip, size_t host_ip_len) { - if (g_sal_module && g_sal_module->parse_domain) { - return g_sal_module->parse_domain(host_name, host_ip, host_ip_len); + if (cur_sal_module && cur_sal_module->parse_domain) { + return cur_sal_module->parse_domain(host_name, host_ip, host_ip_len); } return -1; } int tos_sal_module_connect(const char *ip, const char *port, sal_proto_t proto) { - if (g_sal_module && g_sal_module->connect) { - return g_sal_module->connect(ip, port, proto); + if (cur_sal_module && cur_sal_module->connect) { + return cur_sal_module->connect(ip, port, proto); } return -1; } int tos_sal_module_send(int sock, const void *buf, size_t len) { - if (g_sal_module && g_sal_module->send) { - return g_sal_module->send(sock, buf, len); + if (cur_sal_module && cur_sal_module->send) { + return cur_sal_module->send(sock, buf, len); } return -1; } int tos_sal_module_recv(int sock, void *buf, size_t len) { - if (g_sal_module && g_sal_module->recv) { - return g_sal_module->recv(sock, buf, len); + if (cur_sal_module && cur_sal_module->recv) { + return cur_sal_module->recv(sock, buf, len); } return -1; } int tos_sal_module_recv_timeout(int sock, void *buf, size_t len, uint32_t timeout) { - if (g_sal_module && g_sal_module->recv_timeout) { - return g_sal_module->recv_timeout(sock, buf, len, timeout); + if (cur_sal_module && cur_sal_module->recv_timeout) { + return cur_sal_module->recv_timeout(sock, buf, len, timeout); } return -1; } int tos_sal_module_sendto(int sock, char *ip, char *port, const void *buf, size_t len) { - if (g_sal_module && g_sal_module->sendto) { - return g_sal_module->sendto(sock, ip, port, buf, len); + if (cur_sal_module && cur_sal_module->sendto) { + return cur_sal_module->sendto(sock, ip, port, buf, len); } return -1; } int tos_sal_module_recvfrom(int sock, void *buf, size_t len) { - if (g_sal_module && g_sal_module->recvfrom) { - return g_sal_module->recvfrom(sock, buf, len); + if (cur_sal_module && cur_sal_module->recvfrom) { + return cur_sal_module->recvfrom(sock, buf, len); } return -1; } int tos_sal_module_recvfrom_timeout(int sock, void *buf, size_t len, uint32_t timeout) { - if (g_sal_module && g_sal_module->recvfrom_timeout) { - return g_sal_module->recvfrom_timeout(sock, buf, len, timeout); + if (cur_sal_module && cur_sal_module->recvfrom_timeout) { + return cur_sal_module->recvfrom_timeout(sock, buf, len, timeout); } return -1; } int tos_sal_module_close(int sock) { - if (g_sal_module && g_sal_module->close) { - return g_sal_module->close(sock); + if (cur_sal_module && cur_sal_module->close) { + return cur_sal_module->close(sock); } return -1; } diff --git a/net/sal_module_wrapper/sal_module_wrapper.h b/net/sal_module_wrapper/sal_module_wrapper.h index f481dd7c..b3f5c5e5 100644 --- a/net/sal_module_wrapper/sal_module_wrapper.h +++ b/net/sal_module_wrapper/sal_module_wrapper.h @@ -20,13 +20,39 @@ #include #include +#include "tos_k.h" + +#define SAL_MODULE_DEVICE_NAME_MAX 10 typedef enum sal_protocol_en { TOS_SAL_PROTO_TCP, TOS_SAL_PROTO_UDP, } sal_proto_t; +typedef enum sal_device_status_en { + TOS_SAL_MODULE_AVAILABLE, + TOS_SAL_MODULE_UNAVAILABLE, +} sal_device_status_t; + +typedef enum sal_device_type_en { + TOS_SAL_MODULE_GPRS, + TOS_SAL_MODULE_WIFI, + TOS_SAL_MODULE_4G_LTE, + TOS_SAL_MODULE_4G_CAT1, + TOS_SAL_MODULE_NB_IOT, +} sal_device_type_t; + typedef struct sal_module_st { + char sal_module_name[SAL_MODULE_DEVICE_NAME_MAX]; + + char sal_module_ip[15]; + + k_list_t sal_module_list; + + sal_device_type_t sal_module_type; + + sal_device_status_t sal_module_status; + int (*init)(void); int (*get_local_mac)(char *mac); @@ -63,6 +89,8 @@ typedef struct sal_module_st { */ int tos_sal_module_register(sal_module_t *module); +int tos_sal_module_unregister(sal_module_t *module); + /** * @brief Register a default sal module. * @@ -72,16 +100,24 @@ int tos_sal_module_register(sal_module_t *module); * * @return errcode */ -int tos_sal_module_register_default(void); -/** - * @brief Initialize the module. - * - * @attention None - * - * @return errcode - */ -int tos_sal_module_init(void); + +int tos_sal_module_set_status(sal_module_t *module, sal_device_status_t status); + +void tos_sal_module_set_local_ip(sal_module_t *module, char* ip); + +int tos_sal_module_set_default(char *sal_module_device_name); + +int tos_sal_module_get_type(void); + +// /** +// * @brief Initialize the module. +// * +// * @attention None +// * +// * @return errcode +// */ +// int tos_sal_module_init(void); /** * @brief Convert domain to ip address.