[components][at] 优化AT组件,并解决内存泄露

1.优化at server endmark判断,支持自动识别'\r''\n'"\r\n"。
2.优化at_recvfrom,修复大数据量时sem多次释放造成的接收错误。
3.修复at组件中可能存在的内存泄露。
4.优化部分代码逻辑,减少冗余代码。
This commit is contained in:
yangpengya authored and GitHub committed 2024-02-18 11:07:05 -05:00
1 parent 02eaf76d7b
commit a8d5a645f2
7 files changed
+172 -338

No files matched your search

-19
View File
@@ -26,25 +26,6 @@ if RT_USING_AT
int "The maximum length of server commands buffer"
default 256
choice
prompt "The commands new line sign"
help
This end mark can used for AT server determine the end of commands ,
it can choose "\r", "\n" or "\r\n"
default AT_CMD_END_MARK_CRLF
config AT_CMD_END_MARK_CRLF
bool "\\r\\n"
config AT_CMD_END_MARK_CR
bool "\\r"
config AT_CMD_END_MARK_LF
bool "\\n"
endchoice
endif
config AT_USING_CLIENT
File diff suppressed because it is too large. Load diff
-15
View File
@@ -22,20 +22,6 @@ extern "C" {
#define AT_SW_VERSION "1.3.1"
#define AT_CMD_NAME_LEN 16
#define AT_END_MARK_LEN 4
#ifndef AT_CMD_MAX_LEN
#define AT_CMD_MAX_LEN 128
#endif
/* the server AT commands new line sign */
#if defined(AT_CMD_END_MARK_CRLF)
#define AT_CMD_END_MARK "\r\n"
#elif defined(AT_CMD_END_MARK_CR)
#define AT_CMD_END_MARK "\r"
#elif defined(AT_CMD_END_MARK_LF)
#define AT_CMD_END_MARK "\n"
#endif
#ifndef AT_SERVER_RECV_BUFF_LEN
#define AT_SERVER_RECV_BUFF_LEN 256
@@ -104,7 +90,6 @@ struct at_server
char recv_buffer[AT_SERVER_RECV_BUFF_LEN];
rt_size_t cur_recv_len;
rt_sem_t rx_notice;
char end_mark[AT_END_MARK_LEN];
rt_thread_t parser;
void (*parser_entry)(struct at_server *server);
+3 -13
View File
@@ -114,7 +114,6 @@ static void server_cli_parser(void)
rt_base_t level;
static rt_device_t device_bak;
static rt_err_t (*getchar_bak)(struct at_server *server, char *ch, rt_int32_t timeout);
static char endmark_back[AT_END_MARK_LEN];
/* backup server device and getchar function */
{
@@ -123,16 +122,10 @@ static void server_cli_parser(void)
device_bak = server->device;
getchar_bak = server->get_char;
rt_memset(endmark_back, 0x00, AT_END_MARK_LEN);
rt_memcpy(endmark_back, server->end_mark, strlen(server->end_mark));
/* setup server device as console device */
server->device = rt_console_get_device();
server->get_char = at_server_console_getchar;
rt_memset(server->end_mark, 0x00, AT_END_MARK_LEN);
server->end_mark[0] = '\r';
rt_hw_interrupt_enable(level);
}
@@ -154,9 +147,6 @@ static void server_cli_parser(void)
server->device = device_bak;
server->get_char = getchar_bak;
rt_memset(server->end_mark, 0x00, AT_END_MARK_LEN);
rt_memcpy(server->end_mark, endmark_back, strlen(endmark_back));
rt_hw_interrupt_enable(level);
}
}
@@ -223,6 +213,9 @@ static void client_cli_parser(at_client_t client)
client->status = AT_STATUS_CLI;
}
rt_sem_init(&client_rx_notice, "cli_r", 0, RT_IPC_FLAG_FIFO);
client_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
/* backup client device RX indicate */
{
level = rt_hw_interrupt_disable();
@@ -231,9 +224,6 @@ static void client_cli_parser(at_client_t client)
rt_hw_interrupt_enable(level);
}
rt_sem_init(&client_rx_notice, "cli_r", 0, RT_IPC_FLAG_FIFO);
client_rx_fifo = rt_ringbuffer_create(AT_CLI_FIFO_SIZE);
at_client = rt_thread_create("at_cli", at_client_entry, RT_NULL, 512, 8, 8);
if (client_rx_fifo && at_client)
{
+22 -39
View File
@@ -146,7 +146,6 @@ at_response_t at_resp_set_info(at_response_t resp, rt_size_t buf_size, rt_size_t
const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line)
{
char *resp_buf = resp->buf;
char *resp_line_buf = RT_NULL;
rt_size_t line_num = 1;
RT_ASSERT(resp);
@@ -161,9 +160,7 @@ const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line)
{
if (resp_line == line_num)
{
resp_line_buf = resp_buf;
return resp_line_buf;
return resp_buf;
}
resp_buf += strlen(resp_buf) + 1;
@@ -184,7 +181,6 @@ const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line)
const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword)
{
char *resp_buf = resp->buf;
char *resp_line_buf = RT_NULL;
rt_size_t line_num = 1;
RT_ASSERT(resp);
@@ -194,9 +190,7 @@ const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword)
{
if (strstr(resp_buf, keyword))
{
resp_line_buf = resp_buf;
return resp_line_buf;
return resp_buf;
}
resp_buf += strlen(resp_buf) + 1;
@@ -332,17 +326,14 @@ int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr
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)
else if (client->resp_status != AT_RESP_OK)
{
LOG_E("execute command (%.*s) failed!", client->last_cmd_len, client->send_buf);
result = -RT_ERROR;
goto __exit;
}
}
__exit:
client->resp = RT_NULL;
rt_mutex_release(client->lock);
@@ -401,9 +392,7 @@ int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout)
resp->line_counts = 0;
at_utils_send(client->device, 0, "AT\r\n", 4);
if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
continue;
else
if (rt_sem_take(client->resp_notice, resp->timeout) == RT_EOK)
break;
}
@@ -494,25 +483,23 @@ rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_i
return 0;
}
while (1)
while (size)
{
rt_size_t read_len;
rt_sem_control(client->rx_notice, RT_IPC_CMD_RESET, RT_NULL);
read_len = rt_device_read(client->device, 0, buf + len, size);
if(read_len > 0)
if (read_len > 0)
{
len += read_len;
size -= read_len;
if(size == 0)
break;
continue;
}
if(rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout)) != RT_EOK)
break;
else
{
if (rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout)) != RT_EOK)
break;
}
}
#ifdef AT_PRINT_RAW_CMD
@@ -676,7 +663,6 @@ static const struct at_urc *get_urc_obj(at_client_t client)
static int at_recv_readline(at_client_t client)
{
rt_size_t read_len = 0;
char ch = 0, last_ch = 0;
rt_bool_t is_full = RT_FALSE;
@@ -687,10 +673,9 @@ static int at_recv_readline(at_client_t client)
{
at_client_getchar(client, &ch, RT_WAITING_FOREVER);
if (read_len < client->recv_bufsz)
if (client->recv_line_len < client->recv_bufsz)
{
client->recv_line_buf[read_len++] = ch;
client->recv_line_len = read_len;
client->recv_line_buf[client->recv_line_len++] = ch;
}
else
{
@@ -714,10 +699,10 @@ static int at_recv_readline(at_client_t client)
}
#ifdef AT_PRINT_RAW_CMD
at_print_raw_cmd("recvline", client->recv_line_buf, read_len);
at_print_raw_cmd("recvline", client->recv_line_buf, client->recv_line_len);
#endif
return read_len;
return client->recv_line_len;
}
static void client_parser(at_client_t client)
@@ -882,7 +867,6 @@ static int at_client_para_init(at_client_t client)
if (client->parser == RT_NULL)
{
result = -RT_ENOMEM;
goto __exit;
}
__exit:
@@ -903,16 +887,16 @@ __exit:
rt_sem_delete(client->resp_notice);
}
if (client->device)
{
rt_device_close(client->device);
}
if (client->recv_line_buf)
{
rt_free(client->recv_line_buf);
}
if (client->send_buf)
{
rt_free(client->send_buf);
}
rt_memset(client, 0x00, sizeof(struct at_client));
}
else
@@ -928,6 +912,7 @@ __exit:
*
* @param dev_name AT client device name
* @param recv_bufsz the maximum number of receive buffer length
* @param send_bufsz the maximum number of send command length
*
* @return 0 : initialize success
* -1 : initialize failed
@@ -974,6 +959,7 @@ int at_client_init(const char *dev_name, rt_size_t recv_bufsz, rt_size_t send_bu
{
RT_ASSERT(client->device->type == RT_Device_Class_Char);
rt_device_set_rx_indicate(client->device, at_client_rx_ind);
/* using DMA mode first */
open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
/* using interrupt mode when DMA mode not supported */
@@ -982,14 +968,11 @@ int at_client_init(const char *dev_name, rt_size_t recv_bufsz, rt_size_t send_bu
open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
}
RT_ASSERT(open_result == RT_EOK);
rt_device_set_rx_indicate(client->device, at_client_rx_ind);
}
else
{
LOG_E("AT client initialize failed! Not find the device(%s).", dev_name);
result = -RT_ERROR;
goto __exit;
}
__exit:
File diff suppressed because it is too large. Load diff
+3
View File
@@ -81,6 +81,8 @@ static const struct sal_socket_ops at_socket_ops =
NULL,
#endif
at_sendto,
NULL,
NULL,
at_recvfrom,
at_getsockopt,
at_setsockopt,
@@ -88,6 +90,7 @@ static const struct sal_socket_ops at_socket_ops =
NULL,
NULL,
NULL,
NULL,
#ifdef SAL_USING_POSIX
at_poll,
#endif /* SAL_USING_POSIX */