Skip to content

Commit df6c183

Browse files
authored
Merge pull request #1707 from qgyhd1234/at
[net][at] at_client_wait_connect
2 parents 7d255c6 + f3bce48 commit df6c183

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

components/net/at/include/at.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include <rtthread.h>
2929

30-
#define AT_SW_VERSION "0.2.3"
30+
#define AT_SW_VERSION "0.2.4"
3131

3232
#define DBG_ENABLE
3333
#define DBG_SECTION_NAME "AT"
@@ -216,6 +216,9 @@ int at_req_parse_args(const char *req_args, const char *req_expr, ...);
216216
/* AT client initialize and start */
217217
int at_client_init(void);
218218

219+
/* AT client wait for connection to external devices. */
220+
int at_client_wait_connect(rt_uint32_t timeout);
221+
219222
/* AT client send commands to AT server and waiter response */
220223
int at_exec_cmd(at_response_t resp, const char *cmd_expr, ...);
221224

components/net/at/src/at_client.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,63 @@ int at_exec_cmd(at_response_t resp, const char *cmd_expr, ...)
329329
return result;
330330
}
331331

332+
/**
333+
* Waiting for connection to external devices.
334+
*
335+
* @param timeout millisecond for timeout
336+
*
337+
* @return 0 : success
338+
* -2 : timeout
339+
* -5 : no memory
340+
*/
341+
int at_client_wait_connect(rt_uint32_t timeout)
342+
{
343+
rt_err_t result = RT_EOK;
344+
at_response_t resp = RT_NULL;
345+
at_client_t client = at_client_local;
346+
rt_tick_t start_time = 0;
347+
348+
resp = at_create_resp(16, 0, rt_tick_from_millisecond(500));
349+
if (!resp)
350+
{
351+
LOG_E("No memory for response structure!");
352+
return -RT_ENOMEM;
353+
}
354+
355+
rt_mutex_take(client->lock, RT_WAITING_FOREVER);
356+
client->resp = resp;
357+
358+
start_time = rt_tick_get();
359+
360+
while (1)
361+
{
362+
/* Check whether it is timeout */
363+
if (rt_tick_get() - start_time > timeout)
364+
{
365+
LOG_E("wait connect timeout (%d millisecond)!", timeout);
366+
result = -RT_ETIMEOUT;
367+
break;
368+
}
369+
370+
/* Check whether it is already connected */
371+
resp->line_counts = 0;
372+
rt_device_write(client->device, 0, "AT\r\n", 4);
373+
374+
if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK)
375+
continue;
376+
else
377+
break;
378+
}
379+
380+
at_delete_resp(resp);
381+
382+
client->resp = RT_NULL;
383+
384+
rt_mutex_release(client->lock);
385+
386+
return result;
387+
}
388+
332389
/**
333390
* Send data to AT server, send data don't have end sign(eg: \r\n).
334391
*

0 commit comments

Comments
 (0)