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;
rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
client->recv_line_len = 0;
while (1)
{
at_client_getchar(client, &ch, RT_WAITING_FOREVER);
if (read_len < client->recv_bufsz)
{
client->recv_line_buf[read_len++] = ch;
client->recv_line_len = read_len;
}
else
{
is_full = RT_TRUE;
}
/* is newline or URC data */
if ((ch == '\n' && last_ch == '\r') || (client->end_sign != 0 && ch == client->end_sign)
|| get_urc_obj(client))
{
if (is_full)
{
LOG_E("read line failed. The line data length is out of buffer size(%d)!", client->recv_bufsz);
rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
client->recv_line_len = 0;
return -RT_EFULL;
}
break;
}
last_ch = ch;
}
#ifdef AT_PRINT_RAW_CMD
at_print_raw_cmd("recvline", client->recv_line_buf, read_len);
#endif
return read_len;
}
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)
{
/* current receive is request, try to execute related operations */
if (urc->func != RT_NULL)
{
urc->func(client, client->recv_line_buf, client->recv_line_len);
}
}
else if (client->resp != RT_NULL)
{
at_response_t resp = client->resp;
/* 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)
{
/* copy response lines, separated by '\0' */
rt_memcpy(resp->buf + resp->buf_len, client->recv_line_buf, client->recv_line_len);
/* update the current response information */
resp->buf_len += client->recv_line_len;
resp->line_counts++;
}
else
{
client->resp_status = AT_RESP_BUFF_FULL;
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
&& resp->line_num == 0)
{
/* get the end data by response result, return response state END_OK. */
client->resp_status = AT_RESP_OK;
}
else if (rt_strstr(client->recv_line_buf, AT_RESP_END_ERROR)
|| (rt_memcmp(client->recv_line_buf, AT_RESP_END_FAIL, rt_strlen(AT_RESP_END_FAIL)) == 0))
{
client->resp_status = AT_RESP_ERROR;
}
else if (resp->line_counts == resp->line_num && resp->line_num)
{
/* get the end data by response line, return response state END_OK.*/
client->resp_status = AT_RESP_OK;
}
else
{
continue;
}
client->resp = RT_NULL;
rt_sem_release(client->resp_notice);
}
else
{
// log_d("unrecognized line: %.*s", client->recv_line_len, client->recv_line_buf);
}
}
}
}