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