From f3bce48502933a4db01b8ca5dc2e7d7dbe82871d Mon Sep 17 00:00:00 2001 From: zylx Date: Wed, 8 Aug 2018 14:32:24 +0800 Subject: [PATCH] [net][at] add at_client_wait_connect --- components/net/at/include/at.h | 5 ++- components/net/at/src/at_client.c | 57 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/components/net/at/include/at.h b/components/net/at/include/at.h index 782cb0c400..aafb819e5e 100644 --- a/components/net/at/include/at.h +++ b/components/net/at/include/at.h @@ -27,7 +27,7 @@ #include -#define AT_SW_VERSION "0.2.3" +#define AT_SW_VERSION "0.2.4" #define DBG_ENABLE #define DBG_SECTION_NAME "AT" @@ -216,6 +216,9 @@ int at_req_parse_args(const char *req_args, const char *req_expr, ...); /* AT client initialize and start */ int at_client_init(void); +/* AT client wait for connection to external devices. */ +int at_client_wait_connect(rt_uint32_t timeout); + /* AT client send commands to AT server and waiter response */ int at_exec_cmd(at_response_t resp, const char *cmd_expr, ...); diff --git a/components/net/at/src/at_client.c b/components/net/at/src/at_client.c index 8e5161f2ce..31bd0dcbf9 100644 --- a/components/net/at/src/at_client.c +++ b/components/net/at/src/at_client.c @@ -329,6 +329,63 @@ __exit: return result; } +/** + * Waiting for connection to external devices. + * + * @param timeout millisecond for timeout + * + * @return 0 : success + * -2 : timeout + * -5 : no memory + */ +int at_client_wait_connect(rt_uint32_t timeout) +{ + rt_err_t result = RT_EOK; + at_response_t resp = RT_NULL; + at_client_t client = at_client_local; + rt_tick_t start_time = 0; + + resp = at_create_resp(16, 0, rt_tick_from_millisecond(500)); + if (!resp) + { + LOG_E("No memory for response structure!"); + return -RT_ENOMEM; + } + + rt_mutex_take(client->lock, RT_WAITING_FOREVER); + client->resp = resp; + + start_time = rt_tick_get(); + + while (1) + { + /* Check whether it is timeout */ + if (rt_tick_get() - start_time > timeout) + { + LOG_E("wait connect timeout (%d millisecond)!", timeout); + result = -RT_ETIMEOUT; + break; + } + + /* Check whether it is already connected */ + resp->line_counts = 0; + rt_device_write(client->device, 0, "AT\r\n", 4); + + if (rt_sem_take(client->resp_notice, resp->timeout) != RT_EOK) + continue; + else + break; + } + + at_delete_resp(resp); + + client->resp = RT_NULL; + + rt_mutex_release(client->lock); + + return result; +} + /** * Send data to AT server, send data don't have end sign(eg: \r\n). *