[at]优化at格式化输出,避免多个at client和server输出冲突

This commit is contained in:
yangpeng 2023-09-22 17:34:26 +08:00 committed by guo
parent 4fc88fa894
commit c386a2c956
5 changed files with 63 additions and 61 deletions

View File

@ -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

View File

@ -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 ============================ */

View File

@ -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)

View File

@ -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);
}

View File

@ -13,9 +13,6 @@
#include <stdlib.h>
#include <stdio.h>
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