From 53f0ec07d06376e163b7bb97c5fb31a00cb0784f Mon Sep 17 00:00:00 2001 From: zylx Date: Thu, 9 Aug 2018 13:56:41 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BF=AE=E6=94=B9]=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?at=5Fclient=5Fwait=5Fconnect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- at_socket_esp8266.c | 27 +++++++++++++++++++++++---- at_socket_m26.c | 34 +++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/at_socket_esp8266.c b/at_socket_esp8266.c index 78bcbf4..e349870 100644 --- a/at_socket_esp8266.c +++ b/at_socket_esp8266.c @@ -34,6 +34,7 @@ #ifndef AT_DEVICE_NOT_SELECTED #define ESP8266_MODULE_SEND_MAX_SIZE 2048 +#define ESP8266_WAIT_CONNECT_TIME 5000 /* set real event by current socket and current state */ #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event)) @@ -557,20 +558,29 @@ int at_client_port_init(void) if (at_exec_cmd(at_resp_set_info(resp, 256, 0, rt_tick_from_millisecond(5000)), cmd) < 0) \ { \ LOG_E("RT AT send commands(%s) error!", cmd); \ - return -1; \ + result = -RT_ERROR; \ + goto __exit; \ } \ } while(0); \ static int esp8266_net_init(void) { at_response_t resp = RT_NULL; + rt_err_t result = RT_EOK; rt_size_t i; resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000)); if (!resp) { LOG_E("No memory for response structure!"); - return -RT_ENOMEM; + result = -RT_ENOMEM; + goto __exit; + } + + if (at_client_wait_connect(ESP8266_WAIT_CONNECT_TIME)) + { + result = -RT_ETIMEOUT; + goto __exit; } /* reset module */ AT_SEND_CMD(resp, "AT+RST"); @@ -592,17 +602,26 @@ static int esp8266_net_init(void) AT_DEVICE_WIFI_SSID, AT_DEVICE_WIFI_PASSWORD) != RT_EOK) { LOG_E("AT network initialize failed, check ssid(%s) and password(%s).", AT_DEVICE_WIFI_SSID, AT_DEVICE_WIFI_PASSWORD); - return -RT_ERROR; + result = -RT_ERROR; + goto __exit; } AT_SEND_CMD(resp, "AT+CIPMUX=1"); +__exit: if (resp) { at_delete_resp(resp); } - LOG_I("AT network initialize success!"); + if (!result) + { + LOG_I("AT network initialize success!"); + } + else + { + LOG_E("AT network initialize failed (%d)!", result); + } return RT_EOK; } diff --git a/at_socket_m26.c b/at_socket_m26.c index a0f61fe..9d143c7 100644 --- a/at_socket_m26.c +++ b/at_socket_m26.c @@ -34,6 +34,7 @@ #ifndef AT_DEVICE_NOT_SELECTED #define M26_MODULE_SEND_MAX_SIZE 1460 +#define M26_WAIT_CONNECT_TIME 5000 /* set real event by current socket and current state */ #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event)) @@ -641,7 +642,8 @@ int at_client_port_init(void) { \ if (at_exec_cmd(at_resp_set_info(resp, 128, resp_line, rt_tick_from_millisecond(timeout)), cmd) < 0) \ { \ - return -RT_ERROR; \ + result = -RT_ERROR; \ + goto __exit; \ } \ } while(0); \ @@ -656,17 +658,19 @@ int m26_net_init(void) at_response_t resp = RT_NULL; int i, qimux, qimode; char parsed_data[10]; + rt_err_t result = RT_EOK; const char *line_buffer = RT_NULL; resp = at_create_resp(128, 0, rt_tick_from_millisecond(300)); if (!resp) { LOG_E("No memory for response structure!"); - return -RT_ENOMEM; + result = -RT_ENOMEM; + goto __exit; } LOG_D("Start initializing the M26/MC20 module"); /* wait M26 startup finish */ - at_client_wait_connect(5000); + at_client_wait_connect(M26_WAIT_CONNECT_TIME); /* disable echo */ AT_SEND_CMD(resp, 0, 300, "ATE0"); @@ -692,7 +696,8 @@ int m26_net_init(void) if (i == CPIN_RETRY) { LOG_E("SIM card detection failed!"); - return -RT_ERROR; + result = -RT_ERROR; + goto __exit; } /* waiting for dirty data to be digested */ rt_thread_delay(rt_tick_from_millisecond(10)); @@ -711,7 +716,8 @@ int m26_net_init(void) if (i == CSQ_RETRY) { LOG_E("Signal strength check failed (%s)", parsed_data); - return -RT_ERROR; + result = -RT_ERROR; + goto __exit; } /* check the GSM network is registered */ for (i = 0; i < CREG_RETRY; i++) @@ -728,7 +734,8 @@ int m26_net_init(void) if (i == CREG_RETRY) { LOG_E("The GSM network is register failed (%s)", parsed_data); - return -RT_ERROR; + result = -RT_ERROR; + goto __exit; } /* check the GPRS network is registered */ for (i = 0; i < CGREG_RETRY; i++) @@ -745,7 +752,8 @@ int m26_net_init(void) if (i == CGREG_RETRY) { LOG_E("The GPRS network is register failed (%s)", parsed_data); - return -RT_ERROR; + result = -RT_ERROR; + goto __exit; } AT_SEND_CMD(resp, 0, 300, "AT+QIFGCNT=0"); @@ -776,14 +784,22 @@ int m26_net_init(void) AT_SEND_CMD(resp, 2, 300, "AT+QILOCIP"); +__exit: if (resp) { at_delete_resp(resp); } - LOG_I("AT network initialize success!"); + if (!result) + { + LOG_I("AT network initialize success!"); + } + else + { + LOG_E("AT network initialize failed (%d)!", result); + } - return RT_EOK; + return result; } int m26_ping(int argc, char **argv)