From c386a2c95615afe70750bce44f840bfc390fd2e0 Mon Sep 17 00:00:00 2001 From: yangpeng Date: Fri, 22 Sep 2023 17:34:26 +0800 Subject: [PATCH] =?UTF-8?q?[at]=E4=BC=98=E5=8C=96at=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E8=BE=93=E5=87=BA=EF=BC=8C=E9=81=BF=E5=85=8D=E5=A4=9A?= =?UTF-8?q?=E4=B8=AAat=20client=E5=92=8Cserver=E8=BE=93=E5=87=BA=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/net/at/Kconfig | 8 ++--- components/net/at/include/at.h | 10 +++++- components/net/at/src/at_client.c | 41 +++++++++++++--------- components/net/at/src/at_server.c | 8 ++--- components/net/at/src/at_utils.c | 57 ++++++++++++------------------- 5 files changed, 63 insertions(+), 61 deletions(-) diff --git a/components/net/at/Kconfig b/components/net/at/Kconfig index 43c2bec1b6..4b0dd6735c 100644 --- a/components/net/at/Kconfig +++ b/components/net/at/Kconfig @@ -22,6 +22,10 @@ if RT_USING_AT int "The maximum length of server data accepted" default 256 + config AT_SERVER_SEND_BUFF_LEN + int "The maximum length of server commands buffer" + default 256 + choice prompt "The commands new line sign" help @@ -81,10 +85,6 @@ if RT_USING_AT bool "Enable print RAW format AT command communication data" default n - config AT_CMD_MAX_LEN - int "The maximum length of AT Commands buffer" - default 128 - endif config AT_SW_VERSION_NUM diff --git a/components/net/at/include/at.h b/components/net/at/include/at.h index aec11372dd..fa6ccca503 100644 --- a/components/net/at/include/at.h +++ b/components/net/at/include/at.h @@ -100,6 +100,7 @@ struct at_server rt_err_t (*get_char)(struct at_server *server, char *ch, rt_int32_t timeout); rt_bool_t echo_mode; + char send_buffer[AT_SERVER_SEND_BUFF_LEN]; char recv_buffer[AT_SERVER_RECV_BUFF_LEN]; rt_size_t cur_recv_len; rt_sem_t rx_notice; @@ -166,6 +167,12 @@ struct at_client at_status_t status; char end_sign; + char *send_buf; + /* The maximum supported send cmd length */ + rt_size_t send_bufsz; + /* The length of last cmd */ + rt_size_t last_cmd_len; + /* the current received one line data buffer */ char *recv_line_buf; /* The length of the currently received one line data */ @@ -181,6 +188,7 @@ struct at_client struct at_urc_table *urc_table; rt_size_t urc_table_size; + const struct at_urc *urc; rt_thread_t parser; }; @@ -205,7 +213,7 @@ int at_req_parse_args(const char *req_args, const char *req_expr, ...); #ifdef AT_USING_CLIENT /* AT client initialize and start*/ -int at_client_init(const char *dev_name, rt_size_t recv_bufsz); +int at_client_init(const char *dev_name, rt_size_t recv_bufsz, rt_size_t send_bufsz); /* ========================== multiple AT client function ============================ */ diff --git a/components/net/at/src/at_client.c b/components/net/at/src/at_client.c index de4c8a408e..e0d97c94ea 100644 --- a/components/net/at/src/at_client.c +++ b/components/net/at/src/at_client.c @@ -33,9 +33,8 @@ extern rt_size_t at_utils_send(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); -extern rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args); +extern rt_size_t at_vprintfln(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args); extern void at_print_raw_cmd(const char *type, const char *cmd, rt_size_t size); -extern const char *at_get_last_cmd(rt_size_t *cmd_size); /** * Create response object. @@ -289,9 +288,7 @@ int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...) { va_list args; - rt_size_t cmd_size = 0; rt_err_t result = RT_EOK; - const char *cmd = RT_NULL; RT_ASSERT(cmd_expr); @@ -321,23 +318,25 @@ int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr rt_sem_control(client->resp_notice, RT_IPC_CMD_RESET, RT_NULL); va_start(args, cmd_expr); - at_vprintfln(client->device, cmd_expr, args); + client->last_cmd_len = at_vprintfln(client->device, client->send_buf, client->send_bufsz, cmd_expr, args); + if (client->last_cmd_len > 2) + { + client->last_cmd_len -= 2; /* "\r\n" */ + } va_end(args); if (resp != RT_NULL) { if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK) { - cmd = at_get_last_cmd(&cmd_size); - LOG_W("execute command (%.*s) timeout (%d ticks)!", cmd_size, cmd, resp->timeout); + LOG_W("execute command (%.*s) timeout (%d ticks)!", client->last_cmd_len, client->send_buf, resp->timeout); client->resp_status = AT_RESP_TIMEOUT; result = -RT_ETIMEOUT; goto __exit; } if (client->resp_status != AT_RESP_OK) { - cmd = at_get_last_cmd(&cmd_size); - LOG_E("execute command (%.*s) failed!", cmd_size, cmd); + LOG_E("execute command (%.*s) failed!", client->last_cmd_len, client->send_buf); result = -RT_ERROR; goto __exit; } @@ -700,7 +699,7 @@ static int at_recv_readline(at_client_t client) /* is newline or URC data */ if ((ch == '\n' && last_ch == '\r') || (client->end_sign != 0 && ch == client->end_sign) - || get_urc_obj(client)) + || (client->urc = get_urc_obj(client)) != RT_NULL) { if (is_full) { @@ -723,19 +722,18 @@ static int at_recv_readline(at_client_t client) static void client_parser(at_client_t client) { - const struct at_urc *urc; - while(1) { if (at_recv_readline(client) > 0) { - if ((urc = get_urc_obj(client)) != RT_NULL) + if (client->urc != RT_NULL) { /* current receive is request, try to execute related operations */ - if (urc->func != RT_NULL) + if (client->urc->func != RT_NULL) { - urc->func(client, client->recv_line_buf, client->recv_line_len); + client->urc->func(client, client->recv_line_buf, client->recv_line_len); } + client->urc = RT_NULL; } else if (client->resp != RT_NULL) { @@ -835,6 +833,15 @@ static int at_client_para_init(at_client_t client) goto __exit; } + client->last_cmd_len = 0; + client->send_buf = (char *) rt_calloc(1, client->send_bufsz); + if (client->send_buf == RT_NULL) + { + LOG_E("AT client initialize failed! No memory for send buffer."); + result = -RT_ENOMEM; + goto __exit; + } + rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_LOCK_NAME, at_client_num); client->lock = rt_mutex_create(name, RT_IPC_FLAG_PRIO); if (client->lock == RT_NULL) @@ -926,7 +933,7 @@ __exit: * -1 : initialize failed * -5 : no memory */ -int at_client_init(const char *dev_name, rt_size_t recv_bufsz) +int at_client_init(const char *dev_name, rt_size_t recv_bufsz, rt_size_t send_bufsz) { int idx = 0; int result = RT_EOK; @@ -935,6 +942,7 @@ int at_client_init(const char *dev_name, rt_size_t recv_bufsz) RT_ASSERT(dev_name); RT_ASSERT(recv_bufsz > 0); + RT_ASSERT(send_bufsz > 0); if (at_client_get(dev_name) != RT_NULL) { @@ -952,6 +960,7 @@ int at_client_init(const char *dev_name, rt_size_t recv_bufsz) client = &at_client_table[idx]; client->recv_bufsz = recv_bufsz; + client->send_bufsz = send_bufsz; result = at_client_para_init(client); if (result != RT_EOK) diff --git a/components/net/at/src/at_server.c b/components/net/at/src/at_server.c index c9f98ad39d..563fe39235 100644 --- a/components/net/at/src/at_server.c +++ b/components/net/at/src/at_server.c @@ -43,8 +43,8 @@ extern rt_size_t at_utils_send(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size); -extern void at_vprintf(rt_device_t device, const char *format, va_list args); -extern void at_vprintfln(rt_device_t device, const char *format, va_list args); +extern void at_vprintf(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args); +extern void at_vprintfln(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args); /** * AT server send data to AT device @@ -57,7 +57,7 @@ void at_server_printf(const char *format, ...) va_start(args, format); - at_vprintf(at_server_local->device, format, args); + at_vprintf(at_server_local->device, at_server_local->send_buffer, sizeof(at_server_local->send_buffer), format, args); va_end(args); } @@ -73,7 +73,7 @@ void at_server_printfln(const char *format, ...) va_start(args, format); - at_vprintfln(at_server_local->device, format, args); + at_vprintfln(at_server_local->device, at_server_local->send_buffer, sizeof(at_server_local->send_buffer), format, args); va_end(args); } diff --git a/components/net/at/src/at_utils.c b/components/net/at/src/at_utils.c index 3c0527d88f..bee6817bb0 100644 --- a/components/net/at/src/at_utils.c +++ b/components/net/at/src/at_utils.c @@ -13,9 +13,6 @@ #include #include -static char send_buf[AT_CMD_MAX_LEN]; -static rt_size_t last_cmd_len = 0; - /** * dump hex format data to console device * @@ -60,12 +57,6 @@ void at_print_raw_cmd(const char *name, const char *buf, rt_size_t size) } } -const char *at_get_last_cmd(rt_size_t *cmd_size) -{ - *cmd_size = last_cmd_len; - return send_buf; -} - rt_weak rt_size_t at_utils_send(rt_device_t dev, rt_off_t pos, const void *buffer, @@ -74,38 +65,32 @@ rt_weak rt_size_t at_utils_send(rt_device_t dev, return rt_device_write(dev, pos, buffer, size); } -rt_size_t at_vprintf(rt_device_t device, const char *format, va_list args) +rt_size_t at_vprintf(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args) { - last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, args); - if(last_cmd_len > sizeof(send_buf)) - last_cmd_len = sizeof(send_buf); - -#ifdef AT_PRINT_RAW_CMD - at_print_raw_cmd("sendline", send_buf, last_cmd_len); -#endif - - return at_utils_send(device, 0, send_buf, last_cmd_len); -} - -rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args) -{ - rt_size_t len; - - last_cmd_len = vsnprintf(send_buf, sizeof(send_buf) - 2, format, args); - - if(last_cmd_len == 0) + rt_size_t len = vsnprintf(send_buf, buf_size, format, args); + if (len == 0) { return 0; } - if(last_cmd_len > sizeof(send_buf) - 2) - { - last_cmd_len = sizeof(send_buf) - 2; - } - rt_memcpy(send_buf + last_cmd_len, "\r\n", 2); - - len = last_cmd_len + 2; - +#ifdef AT_PRINT_RAW_CMD + at_print_raw_cmd("sendline", send_buf, len); +#endif + + return at_utils_send(device, 0, send_buf, len); +} + +rt_size_t at_vprintfln(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args) +{ + rt_size_t len = vsnprintf(send_buf, buf_size - 2, format, args); + if (len == 0) + { + return 0; + } + + send_buf[len++] = '\r'; + send_buf[len++] = '\n'; + #ifdef AT_PRINT_RAW_CMD at_print_raw_cmd("sendline", send_buf, len); #endif