Skip to content

Commit 3a5d9e7

Browse files
authored
Merge pull request #1807 from Lawlieta/at
fix at receive data issue
2 parents 0aa4b72 + 3302ef9 commit 3a5d9e7

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

components/net/at/at_socket/at_socket.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ static void at_closed_notice_cb(int socket, at_socket_evt_t event, const char *b
513513
sock->state = AT_SOCKET_CLOSED;
514514
rt_sem_release(sock->recv_notice);
515515
}
516+
516517
int at_connect(int socket, const struct sockaddr *name, socklen_t namelen)
517518
{
518519
struct at_socket *sock;
@@ -611,7 +612,13 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
611612
sock->state = AT_SOCKET_CONNECT;
612613
}
613614

614-
if (sock->state != AT_SOCKET_CONNECT)
615+
/* socket passively closed, receive function return 0 */
616+
if (sock->state == AT_SOCKET_CLOSED)
617+
{
618+
result = 0;
619+
goto __exit;
620+
}
621+
else if (sock->state != AT_SOCKET_CONNECT)
615622
{
616623
LOG_E("received data error, current socket (%d) state (%d) is error.", socket, sock->state);
617624
result = -1;
@@ -664,26 +671,28 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
664671
else
665672
{
666673
LOG_D("received data exit, current socket (%d) is closed by remote.", socket);
667-
result = -1;
674+
result = 0;
668675
goto __exit;
669676
}
670677
}
671678
}
672679

673680
__exit:
674681

675-
if (result < 0)
676-
{
677-
at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
678-
}
679-
else
682+
if (recv_len > 0)
680683
{
681684
result = recv_len;
682-
if (recv_len)
685+
at_do_event_changes(sock, AT_EVENT_RECV, RT_FALSE);
686+
687+
if (!rt_slist_isempty(&sock->recvpkt_list))
683688
{
684-
at_do_event_changes(sock, AT_EVENT_RECV, RT_FALSE);
689+
at_do_event_changes(sock, AT_EVENT_RECV, RT_TRUE);
685690
}
686691
}
692+
else
693+
{
694+
at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
695+
}
687696

688697
return result;
689698
}

components/net/at/include/at.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
extern "C" {
3333
#endif
3434

35-
#define AT_SW_VERSION "1.0.1"
36-
#define AT_SW_VERSION_NUM 0x10000
35+
#define AT_SW_VERSION "1.1.0"
36+
#define AT_SW_VERSION_NUM 0x10100
3737

3838
#define DBG_ENABLE
3939
#define DBG_SECTION_NAME "AT"
@@ -231,7 +231,7 @@ int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout);
231231

232232
/* AT client send or receive data */
233233
rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size);
234-
rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size);
234+
rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout);
235235

236236
/* set AT client a line end sign */
237237
void at_obj_set_end_sign(at_client_t client, char ch);
@@ -263,7 +263,7 @@ int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const
263263
#define at_exec_cmd(resp, ...) at_obj_exec_cmd(at_client_get_first(), resp, __VA_ARGS__)
264264
#define at_client_wait_connect(timeout) at_client_obj_wait_connect(at_client_get_first(), timeout)
265265
#define at_client_send(buf, size) at_client_obj_send(at_client_get_first(), buf, size)
266-
#define at_client_recv(buf, size) at_client_obj_recv(at_client_get_first(), buf, size)
266+
#define at_client_recv(buf, size, timeout) at_client_obj_recv(at_client_get_first(), buf, size, timeout)
267267
#define at_set_end_sign(ch) at_obj_set_end_sign(at_client_get_first(), ch)
268268
#define at_set_urc_table(urc_table, table_sz) at_obj_set_urc_table(at_client_get_first(), urc_table, table_sz)
269269

components/net/at/src/at_client.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,17 +425,22 @@ rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size
425425
return rt_device_write(client->device, 0, buf, size);
426426
}
427427

428-
static char at_client_getchar(at_client_t client)
428+
static rt_err_t at_client_getchar(at_client_t client, char *ch, rt_int32_t timeout)
429429
{
430-
char ch;
430+
rt_err_t result = RT_EOK;
431431

432-
while (rt_device_read(client->device, 0, &ch, 1) == 0)
432+
while (rt_device_read(client->device, 0, ch, 1) == 0)
433433
{
434434
rt_sem_control(client->rx_notice, RT_IPC_CMD_RESET, RT_NULL);
435-
rt_sem_take(client->rx_notice, RT_WAITING_FOREVER);
435+
436+
result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout));
437+
if (result != RT_EOK)
438+
{
439+
return result;
440+
}
436441
}
437442

438-
return ch;
443+
return RT_EOK;
439444
}
440445

441446
/**
@@ -444,15 +449,17 @@ static char at_client_getchar(at_client_t client)
444449
* @param client current AT client object
445450
* @param buf receive data buffer
446451
* @param size receive fixed data size
452+
* @param timeout receive data timeout (ms)
447453
*
448454
* @note this function can only be used in execution function of URC data
449455
*
450456
* @return >0: receive data size
451457
* =0: receive failed
452458
*/
453-
rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size)
459+
rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout)
454460
{
455461
rt_size_t read_idx = 0;
462+
rt_err_t result = RT_EOK;
456463
char ch;
457464

458465
RT_ASSERT(buf);
@@ -467,7 +474,12 @@ rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size)
467474
{
468475
if (read_idx < size)
469476
{
470-
ch = at_client_getchar(client);
477+
result = at_client_getchar(client, &ch, timeout);
478+
if (result != RT_EOK)
479+
{
480+
LOG_E("AT Client receive failed, uart device get data error(%d)", result);
481+
return 0;
482+
}
471483

472484
buf[read_idx++] = ch;
473485
}
@@ -610,7 +622,7 @@ static int at_recv_readline(at_client_t client)
610622

611623
while (1)
612624
{
613-
ch = at_client_getchar(client);
625+
at_client_getchar(client, &ch, RT_WAITING_FOREVER);
614626

615627
if (read_len < client->recv_bufsz)
616628
{

0 commit comments

Comments
 (0)