![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.
Dependencies: mbed FXOS8700CQ MODSERIAL
Diff: wnc_control.cpp
- Revision:
- 43:6821a9c78c4b
- Parent:
- 42:be4b9ee3a615
- Child:
- 44:c95a85b5cf92
--- a/wnc_control.cpp Sun Jul 24 03:10:21 2016 +0000 +++ b/wnc_control.cpp Sun Jul 24 16:58:17 2016 +0000 @@ -221,43 +221,47 @@ int check_wnc_ready(void) { - string * pRespStr; - size_t pos; - int regSts; + string * pRespStr; + size_t pos; + int regSts; - pc.puts("<-------- Begin Cell Status ------------\r\n"); + pc.puts("<-------- Begin Cell Status ------------\r\n"); - at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS); // Check RSSI,BER - at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS); // Check if SIM locked + at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS); // Check RSSI,BER + at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS); // Check if SIM locked - // If SIM Card not ready don't bother with commands! - if (pRespStr->find("CPIN: READY") == string::npos) - { - pc.puts("------------ WNC SIM Problem! --------->\r\n"); - return (-1); - } + if (WNC_MDM_ERR == WNC_NO_RESPONSE) + { + pc.puts("------------ WNC No Response! --------->\r\n"); + return (-2); + } - // SIM card OK, now check for signal and cellular network registration -// do -// { + // If SIM Card not ready don't bother with commands! + if (pRespStr->find("CPIN: READY") == string::npos) + { + pc.puts("------------ WNC SIM Problem! --------->\r\n"); + return (-1); + } + + // SIM card OK, now check for signal and cellular network registration at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS); // Check if registered on network pos = pRespStr->find("CREG: "); if (pos != string::npos) { - // The registration is the 2nd arg in the comma separated list - *pRespStr = pRespStr->substr(pos+8, 1); - regSts = atoi(pRespStr->c_str()); - // 1 - registered home, 5 - registered roaming - if ((regSts != 1) && (regSts != 5)) - { - pc.puts("------------ WNC Cell Link Down! ------>\r\n"); - return (-2); - } + // The registration is the 2nd arg in the comma separated list + *pRespStr = pRespStr->substr(pos+8, 1); + regSts = atoi(pRespStr->c_str()); + // 1 - registered home, 5 - registered roaming + if ((regSts != 1) && (regSts != 5)) + { + pc.puts("------------ WNC Cell Link Down! ------>\r\n"); + return (-2); + } } -// } while ((regSts != 1) && (regSts !=5)); - pc.puts("------------ WNC Ready ---------------->\r\n"); + + pc.puts("------------ WNC Ready ---------------->\r\n"); - return (0); + return (0); } // Sets a global with failure or success, assumes 1 thread all the time @@ -338,15 +342,23 @@ if (hardReset == true) { + pc.puts("Hard Reset!\r\n"); pdnSet = false; intSet = false; sockDialSet = false; } + WNC_MDM_ERR = WNC_OK; // Below commands will re-establish error state pc.puts("Start AT init of WNC:\r\n"); - send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? - send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS); // Echo Off - send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS); // 2 - verbose error, 1 - numeric error, 0 - just ERROR + // Quick commands below do not need to check cellular connectivity + at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? + at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS); // Echo Off + at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS); // 2 - verbose error, 1 - numeric error, 0 - just ERROR + + // If the simple commands are not working no chance of more complex. + // I have seen re-trying commands make it worse. + if (WNC_MDM_ERR != WNC_OK) + return ; if ((WNC_MDM_ERR == WNC_OK) && (intSet == false)) send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); @@ -402,16 +414,13 @@ { string * pRespStr; string str(s); - str = "AT@DNSRESVDON=\"" + str; - str += "\"\r\n"; + str = "AT@DNSRESVDON=\"" + str + "\""; if (send_wnc_cmd(str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0) { size_t pos_start = pRespStr->find(":\"") + 2; - if (pos_start != string::npos) + size_t pos_end = pRespStr->rfind("\"") - 1; + if ((pos_start != string::npos) && (pos_end != string::npos)) { - size_t pos_end = pRespStr->rfind("\"") - 1; - if (pos_end != string::npos) - { if (pos_end > pos_start) { // Make a copy for use later (the source string is re-used) @@ -420,9 +429,6 @@ } else pc.puts("URL Resolve fail, substr Err\r\n"); - } - else - pc.puts("URL Resolve fail, no 2nd quote\r\n"); } else pc.puts("URL Resolve fail, no quotes\r\n"); @@ -430,6 +436,8 @@ else pc.puts("URL Resolve fail, WNC cmd fail\r\n"); + *ipStr = "192.168.0.1"; + return -1; } @@ -484,9 +492,16 @@ wait_ms(10); send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS); + size_t pos_start = pRespStr->find("\"") + 1; size_t pos_end = pRespStr->rfind("\"") - 1; - i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte + + // Make sure search finds what it's looking for! + if (pos_start != string::npos && pos_end != string::npos) + i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte + else + i = 0; + if (i > 0) { retries = 1; // If any data found retry 1 more time to catch data that might be in another @@ -501,10 +516,9 @@ numBytes += i/2; } } - return numBytes; } else pc.puts("sockread Err, to many to read\r\n"); - return 0; + return (numBytes); }