summaryrefslogtreecommitdiff
path: root/components/net/at
diff options
context:
space:
mode:
authorzylx <[email protected]>2018-08-08 14:32:24 +0800
committerzylx <[email protected]>2018-08-08 18:18:03 +0800
commitf3bce48502933a4db01b8ca5dc2e7d7dbe82871d (patch)
tree3f061f76ebb6129f9183f30892c9d7edbdcdd345 /components/net/at
parent7d255c68e576ae98606ddb76f1513ecf01c01cc5 (diff)
[net][at] add at_client_wait_connect
Diffstat (limited to 'components/net/at')
-rw-r--r--components/net/at/include/at.h5
-rw-r--r--components/net/at/src/at_client.c57
2 files changed, 61 insertions, 1 deletions
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 <rtthread.h>
-#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
@@ -330,6 +330,63 @@ __exit:
}
/**
+ * 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).
*
* @param buf send data buffer