From e7b63ed7ab2bed790d8b24b68333f65232f71926 Mon Sep 17 00:00:00 2001 From: malongwei <2544047213@qq.com> Date: Wed, 7 Jul 2021 23:49:42 +0800 Subject: [PATCH 1/2] fix at_vprintf and at_vprintfln and end_sign response --- components/net/at/src/at_client.c | 9 ++++++++- components/net/at/src/at_utils.c | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/components/net/at/src/at_client.c b/components/net/at/src/at_client.c index a613ea1435..b320072b00 100644 --- a/components/net/at/src/at_client.c +++ b/components/net/at/src/at_client.c @@ -735,6 +735,8 @@ static void client_parser(at_client_t client) { at_response_t resp = client->resp; + char end_ch = client->recv_line_buf[client->recv_line_len - 1]; + /* current receive is response */ client->recv_line_buf[client->recv_line_len - 1] = '\0'; if (resp->buf_len + client->recv_line_len < resp->buf_size) @@ -752,7 +754,12 @@ static void client_parser(at_client_t client) LOG_E("Read response buffer failed. The Response buffer size is out of buffer size(%d)!", resp->buf_size); } /* check response result */ - if (rt_memcmp(client->recv_line_buf, AT_RESP_END_OK, rt_strlen(AT_RESP_END_OK)) == 0 + if ((client->end_sign != 0) && (end_ch == client->end_sign) && (resp->line_num == 0)) + { + /* get the end sign, return response state END_OK.*/ + client->resp_status = AT_RESP_OK; + } + else if (rt_memcmp(client->recv_line_buf, AT_RESP_END_OK, rt_strlen(AT_RESP_END_OK)) == 0 && resp->line_num == 0) { /* get the end data by response result, return response state END_OK. */ diff --git a/components/net/at/src/at_utils.c b/components/net/at/src/at_utils.c index 8d8887fe5e..d75ee0990e 100644 --- a/components/net/at/src/at_utils.c +++ b/components/net/at/src/at_utils.c @@ -68,21 +68,30 @@ const char *at_get_last_cmd(rt_size_t *cmd_size) rt_size_t at_vprintf(rt_device_t device, 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 rt_device_write(device, 0, send_buf, last_cmd_len); + return at_device_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; - len = at_vprintf(device, format, args); + last_cmd_len = vsnprintf(send_buf, sizeof(send_buf) - 2, format, args); + 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); - rt_device_write(device, 0, "\r\n", 2); + len = last_cmd_len + 2; - return len + 2; +#ifdef AT_PRINT_RAW_CMD + at_print_raw_cmd("sendline", send_buf, len); +#endif + + return at_device_send(device, 0, send_buf, len); } From 180d0e122499ad88f02558fdcab4a9e19b20bdbf Mon Sep 17 00:00:00 2001 From: malongwei <2544047213@qq.com> Date: Thu, 8 Jul 2021 12:02:29 +0800 Subject: [PATCH 2/2] add at_utils_send virtual function --- components/net/at/src/at_client.c | 16 +++++++++++++--- components/net/at/src/at_server.c | 6 +++++- components/net/at/src/at_utils.c | 12 ++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/components/net/at/src/at_client.c b/components/net/at/src/at_client.c index b320072b00..7904c24d92 100644 --- a/components/net/at/src/at_client.c +++ b/components/net/at/src/at_client.c @@ -8,7 +8,7 @@ * 2018-03-30 chenyong first version * 2018-04-12 chenyong add client implement * 2018-08-17 chenyong multiple client support - * 2021-03-17 Meco Man fix a buf of leaking memory + * 2021-03-17 Meco Man fix a buf of leaking memory */ #include @@ -28,6 +28,10 @@ static struct at_client at_client_table[AT_CLIENT_NUM_MAX] = { 0 }; +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 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); @@ -392,7 +396,7 @@ int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout) /* Check whether it is already connected */ resp->buf_len = 0; resp->line_counts = 0; - rt_device_write(client->device, 0, "AT\r\n", 4); + at_utils_send(client->device, 0, "AT\r\n", 4); if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK) continue; @@ -433,7 +437,13 @@ rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size at_print_raw_cmd("sendline", buf, size); #endif - return rt_device_write(client->device, 0, buf, size); + rt_mutex_take(client->lock, RT_WAITING_FOREVER); + + rt_size_t len = at_utils_send(client->device, 0, buf, size); + + rt_mutex_release(client->lock); + + return len; } static rt_err_t at_client_getchar(at_client_t client, char *ch, rt_int32_t timeout) diff --git a/components/net/at/src/at_server.c b/components/net/at/src/at_server.c index 71435da25b..1db26a260b 100644 --- a/components/net/at/src/at_server.c +++ b/components/net/at/src/at_server.c @@ -38,6 +38,10 @@ static at_server_t at_server_local = RT_NULL; static at_cmd_t cmd_table = RT_NULL; static rt_size_t cmd_num; +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); @@ -187,7 +191,7 @@ rt_size_t at_server_send(at_server_t server, const char *buf, rt_size_t size) return 0; } - return rt_device_write(server->device, 0, buf, size); + return at_utils_send(server->device, 0, buf, size); } /** diff --git a/components/net/at/src/at_utils.c b/components/net/at/src/at_utils.c index d75ee0990e..028f262566 100644 --- a/components/net/at/src/at_utils.c +++ b/components/net/at/src/at_utils.c @@ -65,6 +65,14 @@ const char *at_get_last_cmd(rt_size_t *cmd_size) return send_buf; } +RT_WEAK rt_size_t at_utils_send(rt_device_t dev, + rt_off_t pos, + const void *buffer, + rt_size_t size) +{ + return rt_device_write(dev, pos, buffer, size); +} + rt_size_t at_vprintf(rt_device_t device, const char *format, va_list args) { last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, args); @@ -75,7 +83,7 @@ rt_size_t at_vprintf(rt_device_t device, const char *format, va_list args) at_print_raw_cmd("sendline", send_buf, last_cmd_len); #endif - return at_device_send(device, 0, send_buf, last_cmd_len); + 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) @@ -93,5 +101,5 @@ rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args) at_print_raw_cmd("sendline", send_buf, len); #endif - return at_device_send(device, 0, send_buf, len); + return at_utils_send(device, 0, send_buf, len); }