Skip to content

Commit 3302ef9

Browse files
committed
[net][at] Add at_client_recv function receive data timeout
1 parent 8f95b78 commit 3302ef9

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

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.2"
36-
#define AT_SW_VERSION_NUM 0x10002
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)