add support for multi at device

This commit is contained in:
mculover666
2022-03-01 15:11:30 +08:00
parent 705078136b
commit 9de2090de9
68 changed files with 1255 additions and 800 deletions

View File

@@ -1,13 +1,17 @@
#include "tos_at.h"
#include "esp8266.h"
#include "sal_module_wrapper.h"
#include "stdio.h"
#include "stdbool.h"
#include "ctype.h"
#define TOS_CFG_MODULE_SINGLE_LINK_EN 0u
at_agent_t esp8266_agent;
static k_stack_t esp8266_at_parse_task_stk[AT_PARSER_TASK_STACK_SIZE];
#define AT_AGENT ((at_agent_t *)&esp8266_agent)
static int esp8266_restore(void)
{
int try = 0;
@@ -15,7 +19,7 @@ static int esp8266_restore(void)
tos_at_echo_create(&echo, NULL, 0, "ready");
while (try++ < 10) {
tos_at_cmd_exec_until(&echo, 5000, "AT+RESTORE\r\n");
tos_at_cmd_exec_until(AT_AGENT, &echo, 5000, "AT+RESTORE\r\n");
if (echo.status == AT_ECHO_STATUS_EXPECT) {
return 0;
}
@@ -28,7 +32,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 +61,7 @@ static int esp8266_net_mode_set(esp8266_net_mode_t mode)
tos_at_echo_create(&echo, NULL, 0, NULL);
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) {
return 0;
}
@@ -72,7 +76,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 +91,7 @@ static int esp8266_multilink_set(esp8266_multilink_state_t state)
tos_at_echo_create(&echo, NULL, 0, NULL);
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) {
return 0;
} else if (echo.status == AT_ECHO_STATUS_ERROR) {
@@ -104,7 +108,7 @@ int esp8266_join_ap(const char *ssid, const char *pwd)
tos_at_echo_create(&echo, NULL, 0, NULL);
while (try++ < 10) {
tos_at_cmd_exec(&echo, 15000, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pwd);
tos_at_cmd_exec(AT_AGENT, &echo, 15000, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pwd);
if (echo.status == AT_ECHO_STATUS_OK) {
return 0;
}
@@ -143,7 +147,7 @@ static int esp8266_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;
}
@@ -154,7 +158,7 @@ static int esp8266_connect(const char *ip, const char *port, sal_proto_t proto)
"AT+CIPSTART=\"%s\",\"%s\",%s\r\n",
proto == TOS_SAL_PROTO_UDP ? "UDP" : "TCP", ip, port);
#else
tos_at_cmd_exec(&echo, 10000,
tos_at_cmd_exec(AT_AGENT, &echo, 10000,
"AT+CIPSTART=%d,\"%s\",\"%s\",%s\r\n",
id, proto == TOS_SAL_PROTO_UDP ? "UDP" : "TCP", ip, port);
#endif
@@ -162,19 +166,19 @@ 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)
{
return tos_at_channel_read(id, buf, len);
return tos_at_channel_read(AT_AGENT, id, buf, len);
}
static int esp8266_is_link_broken(const char *echo_buffer)
@@ -192,11 +196,11 @@ 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;
}
@@ -205,16 +209,16 @@ static int esp8266_send(int id, const void *buf, size_t len)
tos_at_cmd_exec_until(&echo, 5000,
"AT+CIPSEND=%d\r\n", len);
#else
tos_at_cmd_exec_until(&echo, 5000,
tos_at_cmd_exec_until(AT_AGENT, &echo, 5000,
"AT+CIPSEND=%d,%d\r\n",
id, len);
#endif
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;
}
@@ -224,17 +228,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 +247,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;
}
@@ -258,7 +262,7 @@ static int esp8266_sendto(int id, char *ip, char *port, const void *buf, size_t
tos_at_cmd_exec_until(&echo, 1000,
"AT+CIPSEND=%d,\"%s\",%s\r\n", len, ip, port);
#else
tos_at_cmd_exec_until(&echo, 1000,
tos_at_cmd_exec_until(AT_AGENT, &echo, 1000,
"AT+CIPSEND=%d,%d,\"%s\",%s\r\n", id, len, ip, port);
#endif
} else {
@@ -266,17 +270,17 @@ static int esp8266_sendto(int id, char *ip, char *port, const void *buf, size_t
tos_at_cmd_exec_until(&echo, 1000,
"AT+CIPSEND=%d\r\n", len);
#else
tos_at_cmd_exec_until(&echo, 1000,
tos_at_cmd_exec_until(AT_AGENT, &echo, 1000,
"AT+CIPSEND=%d,%d\r\n", id, len);
#endif
}
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;
}
@@ -286,23 +290,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)
@@ -317,11 +321,11 @@ static int esp8266_close(int id)
tos_at_echo_create(&echo, NULL, 0, NULL);
#if TOS_CFG_MODULE_SINGLE_LINK_EN > 0u
tos_at_cmd_exec(&echo, 1000, "AT+CIPCLOSE\r\n");
tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPCLOSE\r\n");
#else
tos_at_cmd_exec(&echo, 1000, "AT+CIPCLOSE=%d\r\n", id);
tos_at_cmd_exec(AT_AGENT, &echo, 1000, "AT+CIPCLOSE=%d\r\n", id);
#endif
tos_at_channel_free(id);
tos_at_channel_free(AT_AGENT, id);
return 0;
}
@@ -334,7 +338,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;
@@ -409,7 +413,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 == ',') {
@@ -420,7 +424,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 == ':') {
@@ -434,14 +438,14 @@ __STATIC__ void esp8266_incoming_data_process(void)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
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;
}
//delay has two reason, wait for the data to be cached and untrigger scheduling
tos_stopwatch_delay(200);
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;
}
@@ -469,7 +473,8 @@ 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,
if (tos_at_init(AT_AGENT, "esp8266_at", esp8266_at_parse_task_stk,
uart_port, esp8266_at_event,
sizeof(esp8266_at_event) / sizeof(esp8266_at_event[0])) != 0) {
return -1;
}
@@ -495,7 +500,7 @@ int esp8266_sal_deinit()
tos_sal_module_register_default();
tos_at_deinit();
tos_at_deinit(AT_AGENT);
return 0;
}