![](/media/cache/profiles/5f55d0baa59f4bc1dc393149183f1492.jpg.50x50_q85.jpg)
wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/wnc14a2a-driver/WNC14A2AInterface/WncControllerK64F/WncController/WncController.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /* |
JMF | 0:24d3eb812fd4 | 2 | Copyright (c) 2016 Fred Kellerman |
JMF | 0:24d3eb812fd4 | 3 | |
JMF | 0:24d3eb812fd4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
JMF | 0:24d3eb812fd4 | 5 | of this software and associated documentation files (the "Software"), to deal |
JMF | 0:24d3eb812fd4 | 6 | in the Software without restriction, including without limitation the rights |
JMF | 0:24d3eb812fd4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
JMF | 0:24d3eb812fd4 | 8 | copies of the Software, and to permit persons to whom the Software is |
JMF | 0:24d3eb812fd4 | 9 | furnished to do so, subject to the following conditions: |
JMF | 0:24d3eb812fd4 | 10 | |
JMF | 0:24d3eb812fd4 | 11 | The above copyright notice and this permission notice shall be included in |
JMF | 0:24d3eb812fd4 | 12 | all copies or substantial portions of the Software. |
JMF | 0:24d3eb812fd4 | 13 | |
JMF | 0:24d3eb812fd4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
JMF | 0:24d3eb812fd4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
JMF | 0:24d3eb812fd4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
JMF | 0:24d3eb812fd4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
JMF | 0:24d3eb812fd4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
JMF | 0:24d3eb812fd4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
JMF | 0:24d3eb812fd4 | 20 | THE SOFTWARE. |
JMF | 0:24d3eb812fd4 | 21 | |
JMF | 0:24d3eb812fd4 | 22 | @file WncController.cpp |
JMF | 0:24d3eb812fd4 | 23 | @purpose Controls WNC 14A2A Cellular Modem |
JMF | 0:24d3eb812fd4 | 24 | @version 1.0 |
JMF | 0:24d3eb812fd4 | 25 | @date July 2016 |
JMF | 0:24d3eb812fd4 | 26 | @author Fred Kellerman |
JMF | 0:24d3eb812fd4 | 27 | |
JMF | 0:24d3eb812fd4 | 28 | |
JMF | 0:24d3eb812fd4 | 29 | An Example of usage: |
JMF | 0:24d3eb812fd4 | 30 | |
JMF | 0:24d3eb812fd4 | 31 | WncControllerK64F mdm(&wncPinList, &mdmUart, &debugUart); |
JMF | 0:24d3eb812fd4 | 32 | |
JMF | 0:24d3eb812fd4 | 33 | mdm.enableDebug(true, true); |
JMF | 0:24d3eb812fd4 | 34 | |
JMF | 0:24d3eb812fd4 | 35 | if (false == mdm.powerWncOn("m2m.com.attz", 60)) { |
JMF | 0:24d3eb812fd4 | 36 | while(1); |
JMF | 0:24d3eb812fd4 | 37 | } |
JMF | 0:24d3eb812fd4 | 38 | |
JMF | 0:24d3eb812fd4 | 39 | // ICCID and MSISDN |
JMF | 0:24d3eb812fd4 | 40 | string iccid; string msisdn; |
JMF | 0:24d3eb812fd4 | 41 | if (mdm.getICCID(&iccid) == true) { |
JMF | 0:24d3eb812fd4 | 42 | if (mdm.convertICCIDtoMSISDN(iccid, &msisdn) == true) { |
JMF | 0:24d3eb812fd4 | 43 | // Send an SMS message (must use 15-digit MISDN number!) |
JMF | 0:24d3eb812fd4 | 44 | mdm.sendSMSText(msisdn.c_str(), "Hello from WNC Kit -> from WNC"); |
JMF | 0:24d3eb812fd4 | 45 | } |
JMF | 0:24d3eb812fd4 | 46 | } |
JMF | 0:24d3eb812fd4 | 47 | |
JMF | 0:24d3eb812fd4 | 48 | // Get an IP address setup for the socket #1 (0 indexed)) |
JMF | 0:24d3eb812fd4 | 49 | if (true == mdm.resolveUrl(0, "www.att.com")) |
JMF | 0:24d3eb812fd4 | 50 | { |
JMF | 0:24d3eb812fd4 | 51 | // Report server IP |
JMF | 0:24d3eb812fd4 | 52 | if (true == mdm.getIpAddr(0, ipAddrStr)) { |
JMF | 0:24d3eb812fd4 | 53 | debugUart.puts("Server IP: "); |
JMF | 0:24d3eb812fd4 | 54 | debugUart.puts(ipAddrStr); |
JMF | 0:24d3eb812fd4 | 55 | debugUart.puts("\r\n"); |
JMF | 0:24d3eb812fd4 | 56 | } |
JMF | 0:24d3eb812fd4 | 57 | |
JMF | 0:24d3eb812fd4 | 58 | // Open Socket #1, TCP=true resolved IP on port 80: |
JMF | 0:24d3eb812fd4 | 59 | if (true == mdm.openSocket(0, 80, true)) { |
JMF | 0:24d3eb812fd4 | 60 | // Write some data |
JMF | 0:24d3eb812fd4 | 61 | const uint8_t * dataStr = "GET /index.html HTTP/1.0\r\nFrom: someuser@someuser.com\r\nUser-Agent: HTTPTool/1.0\r\n\r\n"; |
JMF | 0:24d3eb812fd4 | 62 | if (true == mdm.write(0, dataStr, strlen((const char *)dataStr))) |
JMF | 0:24d3eb812fd4 | 63 | { |
JMF | 0:24d3eb812fd4 | 64 | const uint8_t * myBuf; |
JMF | 0:24d3eb812fd4 | 65 | mdm.setReadRetries(0, 20); |
JMF | 0:24d3eb812fd4 | 66 | uint32_t n = mdm.read(0, &myBuf); |
JMF | 0:24d3eb812fd4 | 67 | if (n > 0) |
JMF | 0:24d3eb812fd4 | 68 | debugUart.printf("Read %d chars: %s\r\n", n, myBuf); |
JMF | 0:24d3eb812fd4 | 69 | else |
JMF | 0:24d3eb812fd4 | 70 | debugUart.puts("No read data!\r\n"); |
JMF | 0:24d3eb812fd4 | 71 | } |
JMF | 0:24d3eb812fd4 | 72 | } |
JMF | 0:24d3eb812fd4 | 73 | } |
JMF | 0:24d3eb812fd4 | 74 | |
JMF | 0:24d3eb812fd4 | 75 | */ |
JMF | 0:24d3eb812fd4 | 76 | |
JMF | 0:24d3eb812fd4 | 77 | |
JMF | 0:24d3eb812fd4 | 78 | #include <cstdlib> |
JMF | 0:24d3eb812fd4 | 79 | #include <cctype> |
JMF | 0:24d3eb812fd4 | 80 | #include <string.h> |
JMF | 0:24d3eb812fd4 | 81 | #include "WncController.h" |
JMF | 0:24d3eb812fd4 | 82 | |
JMF | 0:24d3eb812fd4 | 83 | namespace WncController_fk { |
JMF | 0:24d3eb812fd4 | 84 | |
JMF | 0:24d3eb812fd4 | 85 | ///////////////////////////////////////////////////// |
JMF | 0:24d3eb812fd4 | 86 | // Static initializers |
JMF | 0:24d3eb812fd4 | 87 | ///////////////////////////////////////////////////// |
JMF | 0:24d3eb812fd4 | 88 | |
JMF | 0:24d3eb812fd4 | 89 | const char * const WncController::INVALID_IP_STR = ""; |
JMF | 0:24d3eb812fd4 | 90 | |
JMF | 0:24d3eb812fd4 | 91 | |
JMF | 0:24d3eb812fd4 | 92 | /** |
JMF | 0:24d3eb812fd4 | 93 | * C++ version 0.4 char* style "itoa": |
JMF | 0:24d3eb812fd4 | 94 | * Written by Lukás Chmela |
JMF | 0:24d3eb812fd4 | 95 | * Released under GPLv3. |
JMF | 0:24d3eb812fd4 | 96 | */ |
JMF | 0:24d3eb812fd4 | 97 | static char* itoa(int64_t value, char* result, int base) |
JMF | 0:24d3eb812fd4 | 98 | { |
JMF | 0:24d3eb812fd4 | 99 | // check that the base is valid |
JMF | 0:24d3eb812fd4 | 100 | if ( base < 2 || base > 36 ) { |
JMF | 0:24d3eb812fd4 | 101 | *result = '\0'; |
JMF | 0:24d3eb812fd4 | 102 | return result; |
JMF | 0:24d3eb812fd4 | 103 | } |
JMF | 0:24d3eb812fd4 | 104 | |
JMF | 0:24d3eb812fd4 | 105 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 0:24d3eb812fd4 | 106 | int64_t tmp_value; |
JMF | 0:24d3eb812fd4 | 107 | |
JMF | 0:24d3eb812fd4 | 108 | do { |
JMF | 0:24d3eb812fd4 | 109 | tmp_value = value; |
JMF | 0:24d3eb812fd4 | 110 | value /= base; |
JMF | 0:24d3eb812fd4 | 111 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 0:24d3eb812fd4 | 112 | } while ( value ); |
JMF | 0:24d3eb812fd4 | 113 | |
JMF | 0:24d3eb812fd4 | 114 | // Apply negative sign |
JMF | 0:24d3eb812fd4 | 115 | if ( tmp_value < 0 ) |
JMF | 0:24d3eb812fd4 | 116 | *ptr++ = '-'; |
JMF | 0:24d3eb812fd4 | 117 | |
JMF | 0:24d3eb812fd4 | 118 | *ptr-- = '\0'; |
JMF | 0:24d3eb812fd4 | 119 | |
JMF | 0:24d3eb812fd4 | 120 | while ( ptr1 < ptr ) { |
JMF | 0:24d3eb812fd4 | 121 | tmp_char = *ptr; |
JMF | 0:24d3eb812fd4 | 122 | *ptr-- = *ptr1; |
JMF | 0:24d3eb812fd4 | 123 | *ptr1++ = tmp_char; |
JMF | 0:24d3eb812fd4 | 124 | } |
JMF | 0:24d3eb812fd4 | 125 | |
JMF | 0:24d3eb812fd4 | 126 | return result; |
JMF | 0:24d3eb812fd4 | 127 | } |
JMF | 0:24d3eb812fd4 | 128 | |
JMF | 0:24d3eb812fd4 | 129 | const char * WncController::_to_string(int64_t value) |
JMF | 0:24d3eb812fd4 | 130 | { |
JMF | 0:24d3eb812fd4 | 131 | static char str[21]; // room for signed 64-bit + null |
JMF | 0:24d3eb812fd4 | 132 | itoa(value, str, 10); |
JMF | 0:24d3eb812fd4 | 133 | return (str); |
JMF | 0:24d3eb812fd4 | 134 | } |
JMF | 0:24d3eb812fd4 | 135 | |
JMF | 0:24d3eb812fd4 | 136 | const char * WncController::_to_hex_string(uint8_t value) |
JMF | 0:24d3eb812fd4 | 137 | { |
JMF | 0:24d3eb812fd4 | 138 | static char str[3]; // room for 8-bit + null |
JMF | 0:24d3eb812fd4 | 139 | itoa(value, str, 16); |
JMF | 0:24d3eb812fd4 | 140 | return (str); |
JMF | 0:24d3eb812fd4 | 141 | } |
JMF | 0:24d3eb812fd4 | 142 | |
JMF | 0:24d3eb812fd4 | 143 | WncController::WncController(void) : |
JMF | 0:24d3eb812fd4 | 144 | m_sState(WNC_OFF), |
JMF | 0:24d3eb812fd4 | 145 | m_sCmdTimeoutMs(WNC_CMD_TIMEOUT_MS), |
JMF | 0:24d3eb812fd4 | 146 | m_sApnStr("NULL"), |
JMF | 0:24d3eb812fd4 | 147 | m_sPowerUpTimeoutSecs(MAX_POWERUP_TIMEOUT), |
JMF | 0:24d3eb812fd4 | 148 | m_sDebugEnabled(false), |
JMF | 0:24d3eb812fd4 | 149 | m_sMoreDebugEnabled(false), |
JMF | 0:24d3eb812fd4 | 150 | m_sCheckNetStatus(false), // Turn on internet status check between every command |
JMF | 0:24d3eb812fd4 | 151 | m_sReadyForSMS(false) |
JMF | 0:24d3eb812fd4 | 152 | { |
JMF | 0:24d3eb812fd4 | 153 | static const WncController::WncSocketInfo_s defaultSockStruct = { 0, false, "192.168.0.1", 80, 0, 25, true, 30 }; |
JMF | 0:24d3eb812fd4 | 154 | |
JMF | 0:24d3eb812fd4 | 155 | for(unsigned i=0; i<MAX_NUM_WNC_SOCKETS; i++) |
JMF | 0:24d3eb812fd4 | 156 | m_sSock[i] = defaultSockStruct; |
JMF | 0:24d3eb812fd4 | 157 | } |
JMF | 0:24d3eb812fd4 | 158 | |
JMF | 0:24d3eb812fd4 | 159 | WncController::~WncController(void) {}; |
JMF | 0:24d3eb812fd4 | 160 | |
JMF | 0:24d3eb812fd4 | 161 | const char* WncController::getFirmRev(void) |
JMF | 0:24d3eb812fd4 | 162 | { |
JMF | 0:24d3eb812fd4 | 163 | return m_FirmwareRevision.c_str(); |
JMF | 0:24d3eb812fd4 | 164 | } |
JMF | 0:24d3eb812fd4 | 165 | |
JMF | 0:24d3eb812fd4 | 166 | void WncController::enableDebug(bool on, bool moreDebugOn) |
JMF | 0:24d3eb812fd4 | 167 | { |
JMF | 0:24d3eb812fd4 | 168 | m_sDebugEnabled = on; |
JMF | 0:24d3eb812fd4 | 169 | m_sMoreDebugEnabled = moreDebugOn; |
JMF | 0:24d3eb812fd4 | 170 | } |
JMF | 0:24d3eb812fd4 | 171 | |
JMF | 0:24d3eb812fd4 | 172 | WncController::WncState_e WncController::getWncStatus(void) |
JMF | 0:24d3eb812fd4 | 173 | { |
JMF | 0:24d3eb812fd4 | 174 | return (m_sState); |
JMF | 0:24d3eb812fd4 | 175 | } |
JMF | 0:24d3eb812fd4 | 176 | |
JMF | 0:24d3eb812fd4 | 177 | int16_t WncController::getDbmRssi(void) |
JMF | 0:24d3eb812fd4 | 178 | { |
JMF | 0:24d3eb812fd4 | 179 | int16_t rssi, ber; |
JMF | 0:24d3eb812fd4 | 180 | if (at_getrssiber_wnc(&rssi, &ber) == true) |
JMF | 0:24d3eb812fd4 | 181 | return (rssi); |
JMF | 0:24d3eb812fd4 | 182 | else |
JMF | 0:24d3eb812fd4 | 183 | return (99); |
JMF | 0:24d3eb812fd4 | 184 | } |
JMF | 0:24d3eb812fd4 | 185 | |
JMF | 0:24d3eb812fd4 | 186 | int16_t WncController::get3gBer(void) |
JMF | 0:24d3eb812fd4 | 187 | { |
JMF | 0:24d3eb812fd4 | 188 | int16_t rssi, ber; |
JMF | 0:24d3eb812fd4 | 189 | if (at_getrssiber_wnc(&rssi, &ber) == true) |
JMF | 0:24d3eb812fd4 | 190 | return (ber); |
JMF | 0:24d3eb812fd4 | 191 | else |
JMF | 0:24d3eb812fd4 | 192 | return (99); |
JMF | 0:24d3eb812fd4 | 193 | } |
JMF | 0:24d3eb812fd4 | 194 | |
JMF | 0:24d3eb812fd4 | 195 | bool WncController::powerWncOn(const char * const apn, uint8_t powerUpTimeoutSecs) |
JMF | 0:24d3eb812fd4 | 196 | { |
JMF | 0:24d3eb812fd4 | 197 | dbgPuts("Waiting for WNC to Initialize..."); |
JMF | 0:24d3eb812fd4 | 198 | m_sPowerUpTimeoutSecs = powerUpTimeoutSecs; |
JMF | 0:24d3eb812fd4 | 199 | m_sState = WNC_ON_NO_CELL_LINK; // Turn soft on to allow "AT" for init to be sent! |
JMF | 0:24d3eb812fd4 | 200 | if (initWncModem(powerUpTimeoutSecs) == true) { |
JMF | 0:24d3eb812fd4 | 201 | // Set the Apn |
JMF | 0:24d3eb812fd4 | 202 | setApnName(apn); |
JMF | 0:24d3eb812fd4 | 203 | if (false == softwareInitMdm()) { |
JMF | 0:24d3eb812fd4 | 204 | dbgPuts("Software init failed!"); |
JMF | 0:24d3eb812fd4 | 205 | m_sState = WNC_OFF; |
JMF | 0:24d3eb812fd4 | 206 | } |
JMF | 0:24d3eb812fd4 | 207 | } |
JMF | 0:24d3eb812fd4 | 208 | else { |
JMF | 0:24d3eb812fd4 | 209 | dbgPuts("Power up failed!"); |
JMF | 0:24d3eb812fd4 | 210 | m_sState = WNC_OFF; |
JMF | 0:24d3eb812fd4 | 211 | } |
JMF | 0:24d3eb812fd4 | 212 | |
JMF | 0:24d3eb812fd4 | 213 | return ((m_sState == WNC_ON) || (m_sState == WNC_ON_NO_CELL_LINK)); |
JMF | 0:24d3eb812fd4 | 214 | } |
JMF | 0:24d3eb812fd4 | 215 | |
JMF | 0:24d3eb812fd4 | 216 | size_t WncController::sendCustomCmd(const char * cmd, char * resp, size_t sizeRespBuf, int ms_timeout) |
JMF | 0:24d3eb812fd4 | 217 | { |
JMF | 0:24d3eb812fd4 | 218 | string * respStr; |
JMF | 0:24d3eb812fd4 | 219 | |
JMF | 0:24d3eb812fd4 | 220 | if (sizeRespBuf > 0) { |
JMF | 0:24d3eb812fd4 | 221 | at_send_wnc_cmd(cmd, &respStr, ms_timeout); |
JMF | 0:24d3eb812fd4 | 222 | strncpy(resp, respStr->c_str(), sizeRespBuf); |
JMF | 0:24d3eb812fd4 | 223 | if (respStr->size() > sizeRespBuf) |
JMF | 0:24d3eb812fd4 | 224 | dbgPuts("sendCustomCmd truncated!"); |
JMF | 0:24d3eb812fd4 | 225 | |
JMF | 0:24d3eb812fd4 | 226 | return (respStr->size()); |
JMF | 0:24d3eb812fd4 | 227 | } |
JMF | 0:24d3eb812fd4 | 228 | |
JMF | 0:24d3eb812fd4 | 229 | dbgPuts("sendCustomCmd: would have overrun!"); |
JMF | 0:24d3eb812fd4 | 230 | |
JMF | 0:24d3eb812fd4 | 231 | return (0); |
JMF | 0:24d3eb812fd4 | 232 | } |
JMF | 0:24d3eb812fd4 | 233 | |
JMF | 0:24d3eb812fd4 | 234 | bool WncController::pingUrl(const char * url) |
JMF | 0:24d3eb812fd4 | 235 | { |
JMF | 0:24d3eb812fd4 | 236 | string ipAddr; |
JMF | 0:24d3eb812fd4 | 237 | |
JMF | 0:24d3eb812fd4 | 238 | if (true == at_dnsresolve_wnc(url, &ipAddr)) |
JMF | 0:24d3eb812fd4 | 239 | return (pingIp(ipAddr.c_str())); |
JMF | 0:24d3eb812fd4 | 240 | else |
JMF | 0:24d3eb812fd4 | 241 | dbgPuts("pingUrl DNS resolve: failed!"); |
JMF | 0:24d3eb812fd4 | 242 | |
JMF | 0:24d3eb812fd4 | 243 | return (false); |
JMF | 0:24d3eb812fd4 | 244 | } |
JMF | 0:24d3eb812fd4 | 245 | |
JMF | 0:24d3eb812fd4 | 246 | bool WncController::pingIp(const char * ip) |
JMF | 0:24d3eb812fd4 | 247 | { |
JMF | 0:24d3eb812fd4 | 248 | if (true == at_ping_wnc(ip)) |
JMF | 0:24d3eb812fd4 | 249 | return (true); |
JMF | 0:24d3eb812fd4 | 250 | else |
JMF | 0:24d3eb812fd4 | 251 | dbgPuts("pingIp: failed!"); |
JMF | 0:24d3eb812fd4 | 252 | |
JMF | 0:24d3eb812fd4 | 253 | return (false); |
JMF | 0:24d3eb812fd4 | 254 | } |
JMF | 0:24d3eb812fd4 | 255 | |
JMF | 0:24d3eb812fd4 | 256 | bool WncController::getWncNetworkingStats(WncIpStats * s) |
JMF | 0:24d3eb812fd4 | 257 | { |
JMF | 0:24d3eb812fd4 | 258 | return (at_get_wnc_net_stats(s)); |
JMF | 0:24d3eb812fd4 | 259 | } |
JMF | 0:24d3eb812fd4 | 260 | |
JMF | 0:24d3eb812fd4 | 261 | bool WncController::getIpAddr(uint16_t numSock, char myIpAddr[MAX_LEN_IP_STR]) |
JMF | 0:24d3eb812fd4 | 262 | { |
JMF | 0:24d3eb812fd4 | 263 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 264 | strncpy(myIpAddr, m_sSock[numSock].myIpAddressStr.c_str(), MAX_LEN_IP_STR); |
JMF | 0:24d3eb812fd4 | 265 | myIpAddr[MAX_LEN_IP_STR - 1] = '\0'; |
JMF | 0:24d3eb812fd4 | 266 | return (true); |
JMF | 0:24d3eb812fd4 | 267 | } |
JMF | 0:24d3eb812fd4 | 268 | else { |
JMF | 0:24d3eb812fd4 | 269 | myIpAddr[0] = '\0'; |
JMF | 0:24d3eb812fd4 | 270 | return (false); |
JMF | 0:24d3eb812fd4 | 271 | } |
JMF | 0:24d3eb812fd4 | 272 | } |
JMF | 0:24d3eb812fd4 | 273 | |
JMF | 0:24d3eb812fd4 | 274 | bool WncController::setApnName(const char * const apnStr) |
JMF | 0:24d3eb812fd4 | 275 | { |
JMF | 0:24d3eb812fd4 | 276 | if (at_setapn_wnc(apnStr) == true) |
JMF | 0:24d3eb812fd4 | 277 | { |
JMF | 0:24d3eb812fd4 | 278 | m_sApnStr = apnStr; |
JMF | 0:24d3eb812fd4 | 279 | return (true); |
JMF | 0:24d3eb812fd4 | 280 | } |
JMF | 0:24d3eb812fd4 | 281 | else |
JMF | 0:24d3eb812fd4 | 282 | return (false); |
JMF | 0:24d3eb812fd4 | 283 | } |
JMF | 0:24d3eb812fd4 | 284 | |
JMF | 0:24d3eb812fd4 | 285 | bool WncController::resolveUrl(uint16_t numSock, const char * url) |
JMF | 0:24d3eb812fd4 | 286 | { |
JMF | 0:24d3eb812fd4 | 287 | bool cmdRes; |
JMF | 0:24d3eb812fd4 | 288 | |
JMF | 0:24d3eb812fd4 | 289 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 290 | if (strlen(url) > 0) { |
JMF | 0:24d3eb812fd4 | 291 | cmdRes = at_dnsresolve_wnc(url, &m_sSock[numSock].myIpAddressStr); |
JMF | 0:24d3eb812fd4 | 292 | if (cmdRes == false) |
JMF | 0:24d3eb812fd4 | 293 | dbgPuts("Cannot resolve URL!"); |
JMF | 0:24d3eb812fd4 | 294 | return (cmdRes); |
JMF | 0:24d3eb812fd4 | 295 | } |
JMF | 0:24d3eb812fd4 | 296 | else |
JMF | 0:24d3eb812fd4 | 297 | dbgPuts("Invalid URL"); |
JMF | 0:24d3eb812fd4 | 298 | } |
JMF | 0:24d3eb812fd4 | 299 | else |
JMF | 0:24d3eb812fd4 | 300 | dbgPuts("Invalid Sock num!"); |
JMF | 0:24d3eb812fd4 | 301 | |
JMF | 0:24d3eb812fd4 | 302 | return (false); |
JMF | 0:24d3eb812fd4 | 303 | } |
JMF | 0:24d3eb812fd4 | 304 | |
JMF | 0:24d3eb812fd4 | 305 | bool WncController::setIpAddr(uint16_t numSock, const char * ipStr) |
JMF | 0:24d3eb812fd4 | 306 | { |
JMF | 0:24d3eb812fd4 | 307 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 308 | m_sSock[numSock].myIpAddressStr = ipStr; |
JMF | 0:24d3eb812fd4 | 309 | return (true); |
JMF | 0:24d3eb812fd4 | 310 | } |
JMF | 0:24d3eb812fd4 | 311 | else { |
JMF | 0:24d3eb812fd4 | 312 | dbgPuts("Bad socket num!"); |
JMF | 0:24d3eb812fd4 | 313 | return (false); |
JMF | 0:24d3eb812fd4 | 314 | } |
JMF | 0:24d3eb812fd4 | 315 | } |
JMF | 0:24d3eb812fd4 | 316 | |
JMF | 0:24d3eb812fd4 | 317 | void WncController::setWncCmdTimeout(uint16_t toMs) |
JMF | 0:24d3eb812fd4 | 318 | { |
JMF | 0:24d3eb812fd4 | 319 | m_sCmdTimeoutMs = toMs; |
JMF | 0:24d3eb812fd4 | 320 | } |
JMF | 0:24d3eb812fd4 | 321 | |
JMF | 0:24d3eb812fd4 | 322 | bool WncController::openSocketUrl(uint16_t numSock, const char * url, uint16_t port, bool tcp, uint16_t timeOutSec) |
JMF | 0:24d3eb812fd4 | 323 | { |
JMF | 0:24d3eb812fd4 | 324 | if (resolveUrl(numSock, url) == true) |
JMF | 0:24d3eb812fd4 | 325 | return (openSocket(numSock, port, tcp, timeOutSec)); |
JMF | 0:24d3eb812fd4 | 326 | |
JMF | 0:24d3eb812fd4 | 327 | return (false); |
JMF | 0:24d3eb812fd4 | 328 | } |
JMF | 0:24d3eb812fd4 | 329 | |
JMF | 0:24d3eb812fd4 | 330 | bool WncController::openSocketIpAddr(uint16_t numSock, const char * ipAddr, uint16_t port, bool tcp, uint16_t timeOutSec) |
JMF | 0:24d3eb812fd4 | 331 | { |
JMF | 0:24d3eb812fd4 | 332 | if (setIpAddr(numSock, ipAddr) == true) |
JMF | 0:24d3eb812fd4 | 333 | return (openSocket(numSock, port, tcp, timeOutSec)); |
JMF | 0:24d3eb812fd4 | 334 | |
JMF | 0:24d3eb812fd4 | 335 | return (false); |
JMF | 0:24d3eb812fd4 | 336 | } |
JMF | 0:24d3eb812fd4 | 337 | |
JMF | 0:24d3eb812fd4 | 338 | bool WncController::openSocket(uint16_t numSock, uint16_t port, bool tcp, uint16_t timeOutSec) |
JMF | 0:24d3eb812fd4 | 339 | { |
JMF | 0:24d3eb812fd4 | 340 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 341 | // IPV4 ip addr sanity check! |
JMF | 0:24d3eb812fd4 | 342 | size_t lenIpStr = m_sSock[numSock].myIpAddressStr.size(); |
JMF | 0:24d3eb812fd4 | 343 | if (lenIpStr < 7 || lenIpStr > 15) { |
JMF | 0:24d3eb812fd4 | 344 | dbgPuts("Invalid IP Address!"); |
JMF | 0:24d3eb812fd4 | 345 | return (false); |
JMF | 0:24d3eb812fd4 | 346 | } |
JMF | 0:24d3eb812fd4 | 347 | |
JMF | 0:24d3eb812fd4 | 348 | // Already open ? Must close if want to re-open with new settings. |
JMF | 0:24d3eb812fd4 | 349 | if (m_sSock[numSock].open == true) { |
JMF | 0:24d3eb812fd4 | 350 | dbgPuts("Socket already open, close then re-open!"); |
JMF | 0:24d3eb812fd4 | 351 | if (true == at_sockclose_wnc(m_sSock[numSock].numWncSock)) |
JMF | 0:24d3eb812fd4 | 352 | m_sSock[numSock].open = false; |
JMF | 0:24d3eb812fd4 | 353 | else |
JMF | 0:24d3eb812fd4 | 354 | return (false); |
JMF | 0:24d3eb812fd4 | 355 | } |
JMF | 0:24d3eb812fd4 | 356 | |
JMF | 0:24d3eb812fd4 | 357 | m_sSock[numSock].myPort = port; |
JMF | 0:24d3eb812fd4 | 358 | m_sSock[numSock].isTcp = tcp; |
JMF | 0:24d3eb812fd4 | 359 | m_sSock[numSock].timeOutSec = timeOutSec; |
JMF | 0:24d3eb812fd4 | 360 | |
JMF | 0:24d3eb812fd4 | 361 | int16_t numWncSock = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), port, numSock, tcp, timeOutSec); |
JMF | 0:24d3eb812fd4 | 362 | m_sSock[numSock].numWncSock = numWncSock; |
JMF | 0:24d3eb812fd4 | 363 | if (numWncSock > 0 && numWncSock <= (uint16_t)MAX_NUM_WNC_SOCKETS) |
JMF | 0:24d3eb812fd4 | 364 | m_sSock[numSock].open = true; |
JMF | 0:24d3eb812fd4 | 365 | else { |
JMF | 0:24d3eb812fd4 | 366 | m_sSock[numSock].open = false; |
JMF | 0:24d3eb812fd4 | 367 | dbgPuts("Socket open fail!!!!"); |
JMF | 0:24d3eb812fd4 | 368 | |
JMF | 0:24d3eb812fd4 | 369 | // If the modem is not responding don't bother it. |
JMF | 0:24d3eb812fd4 | 370 | if (WNC_NO_RESPONSE != getWncStatus()) { |
JMF | 0:24d3eb812fd4 | 371 | // Work-around. If the sock open fails it needs to be told |
JMF | 0:24d3eb812fd4 | 372 | // to close. If 6 sock opens happen with a fail, it further |
JMF | 0:24d3eb812fd4 | 373 | // crashes the WNC. Not sure why the sock won't open. |
JMF | 0:24d3eb812fd4 | 374 | at_sockclose_wnc(m_sSock[numSock].numWncSock); |
JMF | 0:24d3eb812fd4 | 375 | } |
JMF | 0:24d3eb812fd4 | 376 | } |
JMF | 0:24d3eb812fd4 | 377 | } |
JMF | 0:24d3eb812fd4 | 378 | else { |
JMF | 0:24d3eb812fd4 | 379 | dbgPuts("Bad socket num or IP!"); |
JMF | 0:24d3eb812fd4 | 380 | return (false); |
JMF | 0:24d3eb812fd4 | 381 | } |
JMF | 0:24d3eb812fd4 | 382 | |
JMF | 0:24d3eb812fd4 | 383 | return (m_sSock[numSock].open); |
JMF | 0:24d3eb812fd4 | 384 | } |
JMF | 0:24d3eb812fd4 | 385 | |
JMF | 0:24d3eb812fd4 | 386 | bool WncController::sockWrite(const uint8_t * const s, uint16_t n, uint16_t numSock, bool isTcp) |
JMF | 0:24d3eb812fd4 | 387 | { |
JMF | 0:24d3eb812fd4 | 388 | bool result = true; |
JMF | 0:24d3eb812fd4 | 389 | |
JMF | 0:24d3eb812fd4 | 390 | AtCmdErr_e cmdRes = at_sockwrite_wnc(s, n, m_sSock[numSock].numWncSock, isTcp); |
JMF | 0:24d3eb812fd4 | 391 | if (cmdRes != WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 392 | if ((cmdRes == WNC_AT_CMD_ERREXT) || (cmdRes == WNC_AT_CMD_ERRCME)) |
JMF | 0:24d3eb812fd4 | 393 | { |
JMF | 0:24d3eb812fd4 | 394 | // This may throw away any data that hasn't been written out of the WNC |
JMF | 0:24d3eb812fd4 | 395 | // but at this point with the way the WNC currently works we have |
JMF | 0:24d3eb812fd4 | 396 | // no choice. |
JMF | 0:24d3eb812fd4 | 397 | closeOpenSocket(numSock); |
JMF | 0:24d3eb812fd4 | 398 | } |
JMF | 0:24d3eb812fd4 | 399 | result = false; |
JMF | 0:24d3eb812fd4 | 400 | } |
JMF | 0:24d3eb812fd4 | 401 | |
JMF | 0:24d3eb812fd4 | 402 | return (result); |
JMF | 0:24d3eb812fd4 | 403 | } |
JMF | 0:24d3eb812fd4 | 404 | |
JMF | 0:24d3eb812fd4 | 405 | bool WncController::write(uint16_t numSock, const uint8_t * s, uint32_t n) |
JMF | 0:24d3eb812fd4 | 406 | { |
JMF | 0:24d3eb812fd4 | 407 | bool result; |
JMF | 0:24d3eb812fd4 | 408 | |
JMF | 0:24d3eb812fd4 | 409 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 410 | if (m_sSock[numSock].open == true) { |
JMF | 0:24d3eb812fd4 | 411 | if (n <= MAX_WNC_WRITE_BYTES) { |
JMF | 0:24d3eb812fd4 | 412 | result = sockWrite(s, n, numSock, m_sSock[numSock].isTcp); |
JMF | 0:24d3eb812fd4 | 413 | } |
JMF | 0:24d3eb812fd4 | 414 | else { |
JMF | 0:24d3eb812fd4 | 415 | uint16_t rem = n % MAX_WNC_WRITE_BYTES; |
JMF | 0:24d3eb812fd4 | 416 | while (n >= MAX_WNC_WRITE_BYTES) { |
JMF | 0:24d3eb812fd4 | 417 | n -= MAX_WNC_WRITE_BYTES; |
JMF | 0:24d3eb812fd4 | 418 | result = sockWrite(s, MAX_WNC_WRITE_BYTES, numSock, m_sSock[numSock].isTcp); |
JMF | 0:24d3eb812fd4 | 419 | if (result == false) { |
JMF | 0:24d3eb812fd4 | 420 | n = 0; |
JMF | 0:24d3eb812fd4 | 421 | rem = 0; |
JMF | 0:24d3eb812fd4 | 422 | dbgPuts("Sock write fail!"); |
JMF | 0:24d3eb812fd4 | 423 | } |
JMF | 0:24d3eb812fd4 | 424 | else |
JMF | 0:24d3eb812fd4 | 425 | s += MAX_WNC_WRITE_BYTES; |
JMF | 0:24d3eb812fd4 | 426 | } |
JMF | 0:24d3eb812fd4 | 427 | if (rem > 0) |
JMF | 0:24d3eb812fd4 | 428 | result = sockWrite(s, rem, numSock, m_sSock[numSock].isTcp); |
JMF | 0:24d3eb812fd4 | 429 | } |
JMF | 0:24d3eb812fd4 | 430 | } |
JMF | 0:24d3eb812fd4 | 431 | else { |
JMF | 0:24d3eb812fd4 | 432 | dbgPuts("Socket is closed for write!"); |
JMF | 0:24d3eb812fd4 | 433 | result = false; |
JMF | 0:24d3eb812fd4 | 434 | } |
JMF | 0:24d3eb812fd4 | 435 | } |
JMF | 0:24d3eb812fd4 | 436 | else { |
JMF | 0:24d3eb812fd4 | 437 | dbgPuts("Bad socket num!"); |
JMF | 0:24d3eb812fd4 | 438 | result = false; |
JMF | 0:24d3eb812fd4 | 439 | } |
JMF | 0:24d3eb812fd4 | 440 | |
JMF | 0:24d3eb812fd4 | 441 | return (result); |
JMF | 0:24d3eb812fd4 | 442 | } |
JMF | 0:24d3eb812fd4 | 443 | |
JMF | 0:24d3eb812fd4 | 444 | size_t WncController::read(uint16_t numSock, const uint8_t ** readBuf) |
JMF | 0:24d3eb812fd4 | 445 | { |
JMF | 0:24d3eb812fd4 | 446 | static string theBuf; |
JMF | 0:24d3eb812fd4 | 447 | string readStr; |
JMF | 0:24d3eb812fd4 | 448 | |
JMF | 0:24d3eb812fd4 | 449 | theBuf.erase(); // Clean-up from last time |
JMF | 0:24d3eb812fd4 | 450 | |
JMF | 0:24d3eb812fd4 | 451 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 452 | if (m_sSock[numSock].open == true) { |
JMF | 0:24d3eb812fd4 | 453 | uint8_t i = m_sSock[numSock].readRetries; |
JMF | 0:24d3eb812fd4 | 454 | uint16_t to = m_sSock[numSock].readRetryWaitMs; |
JMF | 0:24d3eb812fd4 | 455 | bool foundData = false; |
JMF | 0:24d3eb812fd4 | 456 | do { |
JMF | 0:24d3eb812fd4 | 457 | AtCmdErr_e cmdRes; |
JMF | 0:24d3eb812fd4 | 458 | cmdRes = at_sockread_wnc(&readStr, m_sSock[numSock].numWncSock, m_sSock[numSock].isTcp); |
JMF | 0:24d3eb812fd4 | 459 | if (WNC_AT_CMD_OK == cmdRes) { |
JMF | 0:24d3eb812fd4 | 460 | // This will let this loop read until the socket data is |
JMF | 0:24d3eb812fd4 | 461 | // empty. If no data, then wait the retry amount of time. |
JMF | 0:24d3eb812fd4 | 462 | if (readStr.size() > 0) { |
JMF | 0:24d3eb812fd4 | 463 | theBuf += readStr; |
JMF | 0:24d3eb812fd4 | 464 | foundData = true; |
JMF | 0:24d3eb812fd4 | 465 | i = 1; |
JMF | 0:24d3eb812fd4 | 466 | } |
JMF | 0:24d3eb812fd4 | 467 | else { |
JMF | 0:24d3eb812fd4 | 468 | // Once data is found start returning it asap |
JMF | 0:24d3eb812fd4 | 469 | if (foundData == false) |
JMF | 0:24d3eb812fd4 | 470 | waitMs(to); |
JMF | 0:24d3eb812fd4 | 471 | } |
JMF | 0:24d3eb812fd4 | 472 | } |
JMF | 0:24d3eb812fd4 | 473 | else { |
JMF | 0:24d3eb812fd4 | 474 | theBuf += readStr; // Append what if any we got before it errored. |
JMF | 0:24d3eb812fd4 | 475 | dbgPuts("Sockread failed!"); |
JMF | 0:24d3eb812fd4 | 476 | if (WNC_NO_RESPONSE == getWncStatus()) { |
JMF | 0:24d3eb812fd4 | 477 | i = 0; |
JMF | 0:24d3eb812fd4 | 478 | } |
JMF | 0:24d3eb812fd4 | 479 | else if ((cmdRes == WNC_AT_CMD_ERREXT) || (cmdRes == WNC_AT_CMD_ERRCME)) |
JMF | 0:24d3eb812fd4 | 480 | { |
JMF | 0:24d3eb812fd4 | 481 | // This may throw away any data that hasn't been read out of the WNC |
JMF | 0:24d3eb812fd4 | 482 | // but at this point with the way the WNC currently works we have |
JMF | 0:24d3eb812fd4 | 483 | // no choice. |
JMF | 0:24d3eb812fd4 | 484 | closeOpenSocket(numSock); |
JMF | 0:24d3eb812fd4 | 485 | i = 0; |
JMF | 0:24d3eb812fd4 | 486 | } |
JMF | 0:24d3eb812fd4 | 487 | else |
JMF | 0:24d3eb812fd4 | 488 | waitMs(to); |
JMF | 0:24d3eb812fd4 | 489 | } |
JMF | 0:24d3eb812fd4 | 490 | } while (i-- > 0); |
JMF | 0:24d3eb812fd4 | 491 | } |
JMF | 0:24d3eb812fd4 | 492 | else { |
JMF | 0:24d3eb812fd4 | 493 | dbgPuts("Socket is closed for read"); |
JMF | 0:24d3eb812fd4 | 494 | } |
JMF | 0:24d3eb812fd4 | 495 | } |
JMF | 0:24d3eb812fd4 | 496 | else { |
JMF | 0:24d3eb812fd4 | 497 | dbgPuts("Bad socket num!"); |
JMF | 0:24d3eb812fd4 | 498 | } |
JMF | 0:24d3eb812fd4 | 499 | |
JMF | 0:24d3eb812fd4 | 500 | *readBuf = (const uint8_t *)theBuf.c_str(); |
JMF | 0:24d3eb812fd4 | 501 | |
JMF | 0:24d3eb812fd4 | 502 | return (theBuf.size()); |
JMF | 0:24d3eb812fd4 | 503 | } |
JMF | 0:24d3eb812fd4 | 504 | |
JMF | 0:24d3eb812fd4 | 505 | size_t WncController::read(uint16_t numSock, uint8_t * readBuf, uint32_t maxReadBufLen) |
JMF | 0:24d3eb812fd4 | 506 | { |
JMF | 0:24d3eb812fd4 | 507 | uint32_t numCopied = 0; |
JMF | 0:24d3eb812fd4 | 508 | |
JMF | 0:24d3eb812fd4 | 509 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 510 | if (m_sSock[numSock].open == true) { |
JMF | 0:24d3eb812fd4 | 511 | uint8_t i = m_sSock[numSock].readRetries; |
JMF | 0:24d3eb812fd4 | 512 | uint16_t to = m_sSock[numSock].readRetryWaitMs; |
JMF | 0:24d3eb812fd4 | 513 | bool foundData = false; |
JMF | 0:24d3eb812fd4 | 514 | uint16_t numRead; |
JMF | 0:24d3eb812fd4 | 515 | do { |
JMF | 0:24d3eb812fd4 | 516 | AtCmdErr_e cmdRes; |
JMF | 0:24d3eb812fd4 | 517 | if (maxReadBufLen < MAX_WNC_READ_BYTES) |
JMF | 0:24d3eb812fd4 | 518 | cmdRes = at_sockread_wnc(readBuf, &numRead, maxReadBufLen, m_sSock[numSock].numWncSock, m_sSock[numSock].isTcp); |
JMF | 0:24d3eb812fd4 | 519 | else |
JMF | 0:24d3eb812fd4 | 520 | cmdRes = at_sockread_wnc(readBuf, &numRead, MAX_WNC_READ_BYTES, m_sSock[numSock].numWncSock, m_sSock[numSock].isTcp); |
JMF | 0:24d3eb812fd4 | 521 | |
JMF | 0:24d3eb812fd4 | 522 | if (WNC_AT_CMD_OK == cmdRes) { |
JMF | 0:24d3eb812fd4 | 523 | // This will let this loop read until the socket data is |
JMF | 0:24d3eb812fd4 | 524 | // empty. If no data, then wait the retry amount of time. |
JMF | 0:24d3eb812fd4 | 525 | if (numRead > 0) { |
JMF | 0:24d3eb812fd4 | 526 | foundData = true; |
JMF | 0:24d3eb812fd4 | 527 | i = 1; |
JMF | 0:24d3eb812fd4 | 528 | if (numRead <= maxReadBufLen) { |
JMF | 0:24d3eb812fd4 | 529 | maxReadBufLen -= numRead; |
JMF | 0:24d3eb812fd4 | 530 | numCopied += numRead; |
JMF | 0:24d3eb812fd4 | 531 | readBuf += numRead; |
JMF | 0:24d3eb812fd4 | 532 | } |
JMF | 0:24d3eb812fd4 | 533 | else { |
JMF | 0:24d3eb812fd4 | 534 | i = 0; // No more room for data! |
JMF | 0:24d3eb812fd4 | 535 | dbgPutsNoTime("No more room for read data!"); |
JMF | 0:24d3eb812fd4 | 536 | } |
JMF | 0:24d3eb812fd4 | 537 | } |
JMF | 0:24d3eb812fd4 | 538 | else { |
JMF | 0:24d3eb812fd4 | 539 | // Once data is found start returning it asap |
JMF | 0:24d3eb812fd4 | 540 | if (foundData == false) |
JMF | 0:24d3eb812fd4 | 541 | waitMs(to); |
JMF | 0:24d3eb812fd4 | 542 | } |
JMF | 0:24d3eb812fd4 | 543 | } |
JMF | 0:24d3eb812fd4 | 544 | else { |
JMF | 0:24d3eb812fd4 | 545 | dbgPuts("Sockread failed!"); |
JMF | 0:24d3eb812fd4 | 546 | if (WNC_NO_RESPONSE == getWncStatus()) { |
JMF | 0:24d3eb812fd4 | 547 | i = 0; |
JMF | 0:24d3eb812fd4 | 548 | } |
JMF | 0:24d3eb812fd4 | 549 | else if ((cmdRes == WNC_AT_CMD_ERREXT) || (cmdRes == WNC_AT_CMD_ERRCME)) |
JMF | 0:24d3eb812fd4 | 550 | { |
JMF | 0:24d3eb812fd4 | 551 | // This may throw away any data that hasn't been read out of the WNC |
JMF | 0:24d3eb812fd4 | 552 | // but at this point with the way the WNC currently works we have |
JMF | 0:24d3eb812fd4 | 553 | // no choice. |
JMF | 0:24d3eb812fd4 | 554 | closeOpenSocket(numSock); |
JMF | 0:24d3eb812fd4 | 555 | i = 0; |
JMF | 0:24d3eb812fd4 | 556 | } |
JMF | 0:24d3eb812fd4 | 557 | else |
JMF | 0:24d3eb812fd4 | 558 | waitMs(to); |
JMF | 0:24d3eb812fd4 | 559 | } |
JMF | 0:24d3eb812fd4 | 560 | } while ((i-- > 0) && (maxReadBufLen > 0)); |
JMF | 0:24d3eb812fd4 | 561 | } |
JMF | 0:24d3eb812fd4 | 562 | else { |
JMF | 0:24d3eb812fd4 | 563 | dbgPuts("Socket is closed for read"); |
JMF | 0:24d3eb812fd4 | 564 | } |
JMF | 0:24d3eb812fd4 | 565 | } |
JMF | 0:24d3eb812fd4 | 566 | else { |
JMF | 0:24d3eb812fd4 | 567 | dbgPuts("Bad socket num!"); |
JMF | 0:24d3eb812fd4 | 568 | } |
JMF | 0:24d3eb812fd4 | 569 | |
JMF | 0:24d3eb812fd4 | 570 | return (numCopied); |
JMF | 0:24d3eb812fd4 | 571 | } |
JMF | 0:24d3eb812fd4 | 572 | |
JMF | 0:24d3eb812fd4 | 573 | void WncController::setReadRetries(uint16_t numSock, uint16_t retries) |
JMF | 0:24d3eb812fd4 | 574 | { |
JMF | 0:24d3eb812fd4 | 575 | if (numSock < MAX_NUM_WNC_SOCKETS) |
JMF | 0:24d3eb812fd4 | 576 | m_sSock[numSock].readRetries = retries; |
JMF | 0:24d3eb812fd4 | 577 | else |
JMF | 0:24d3eb812fd4 | 578 | dbgPuts("Bad socket num!"); |
JMF | 0:24d3eb812fd4 | 579 | } |
JMF | 0:24d3eb812fd4 | 580 | |
JMF | 0:24d3eb812fd4 | 581 | void WncController::setReadRetryWait(uint16_t numSock, uint16_t readRetryWaitMs) |
JMF | 0:24d3eb812fd4 | 582 | { |
JMF | 0:24d3eb812fd4 | 583 | if (numSock < MAX_NUM_WNC_SOCKETS) |
JMF | 0:24d3eb812fd4 | 584 | m_sSock[numSock].readRetryWaitMs = readRetryWaitMs; |
JMF | 0:24d3eb812fd4 | 585 | else |
JMF | 0:24d3eb812fd4 | 586 | dbgPuts("Bad socket num!"); |
JMF | 0:24d3eb812fd4 | 587 | } |
JMF | 0:24d3eb812fd4 | 588 | |
JMF | 0:24d3eb812fd4 | 589 | bool WncController::closeSocket(uint16_t numSock) |
JMF | 0:24d3eb812fd4 | 590 | { |
JMF | 0:24d3eb812fd4 | 591 | if (numSock < MAX_NUM_WNC_SOCKETS) { |
JMF | 0:24d3eb812fd4 | 592 | |
JMF | 0:24d3eb812fd4 | 593 | if (false == at_sockclose_wnc(m_sSock[numSock].numWncSock)) |
JMF | 0:24d3eb812fd4 | 594 | dbgPuts("Sock close may not have closed!"); |
JMF | 0:24d3eb812fd4 | 595 | |
JMF | 0:24d3eb812fd4 | 596 | // Even with an error the socket could have closed, |
JMF | 0:24d3eb812fd4 | 597 | // can't tell for sure so just soft close it for now. |
JMF | 0:24d3eb812fd4 | 598 | m_sSock[numSock].open = false; |
JMF | 0:24d3eb812fd4 | 599 | } |
JMF | 0:24d3eb812fd4 | 600 | else { |
JMF | 0:24d3eb812fd4 | 601 | dbgPuts("Bad socket num!"); |
JMF | 0:24d3eb812fd4 | 602 | } |
JMF | 0:24d3eb812fd4 | 603 | |
JMF | 0:24d3eb812fd4 | 604 | return (m_sSock[numSock].open == false); |
JMF | 0:24d3eb812fd4 | 605 | } |
JMF | 0:24d3eb812fd4 | 606 | |
JMF | 0:24d3eb812fd4 | 607 | size_t WncController::mdmGetline(string * buff, int timeout_ms) |
JMF | 0:24d3eb812fd4 | 608 | { |
JMF | 0:24d3eb812fd4 | 609 | char chin = '\0'; |
JMF | 0:24d3eb812fd4 | 610 | char chin_last; |
JMF | 0:24d3eb812fd4 | 611 | size_t len = 0; |
JMF | 0:24d3eb812fd4 | 612 | |
JMF | 0:24d3eb812fd4 | 613 | startTimerB(); |
JMF | 0:24d3eb812fd4 | 614 | while ((len <= MAX_LEN_WNC_CMD_RESPONSE) && (getTimerTicksB_mS() < timeout_ms)) { |
JMF | 0:24d3eb812fd4 | 615 | if (charReady()) { |
JMF | 0:24d3eb812fd4 | 616 | chin_last = chin; |
JMF | 0:24d3eb812fd4 | 617 | chin = getc(); |
JMF | 0:24d3eb812fd4 | 618 | if (isprint(chin)) { |
JMF | 0:24d3eb812fd4 | 619 | *buff += chin; |
JMF | 0:24d3eb812fd4 | 620 | len++; // Bound the copy length to something reaonable just in case |
JMF | 0:24d3eb812fd4 | 621 | continue; |
JMF | 0:24d3eb812fd4 | 622 | } |
JMF | 0:24d3eb812fd4 | 623 | else if ((('\r' == chin_last) && ('\n' == chin)) || (('\n' == chin_last) && ('\r' == chin))) { |
JMF | 0:24d3eb812fd4 | 624 | break; |
JMF | 0:24d3eb812fd4 | 625 | } |
JMF | 0:24d3eb812fd4 | 626 | } |
JMF | 0:24d3eb812fd4 | 627 | } |
JMF | 0:24d3eb812fd4 | 628 | stopTimerB(); |
JMF | 0:24d3eb812fd4 | 629 | |
JMF | 0:24d3eb812fd4 | 630 | if (len > MAX_LEN_WNC_CMD_RESPONSE) |
JMF | 0:24d3eb812fd4 | 631 | dbgPuts("Max cmd length reply exceeded!"); |
JMF | 0:24d3eb812fd4 | 632 | |
JMF | 0:24d3eb812fd4 | 633 | return (len); |
JMF | 0:24d3eb812fd4 | 634 | } |
JMF | 0:24d3eb812fd4 | 635 | |
JMF | 0:24d3eb812fd4 | 636 | bool WncController::softwareInitMdm(void) |
JMF | 0:24d3eb812fd4 | 637 | { |
JMF | 0:24d3eb812fd4 | 638 | static bool reportStatus = true; |
JMF | 0:24d3eb812fd4 | 639 | unsigned i; |
JMF | 0:24d3eb812fd4 | 640 | |
JMF | 0:24d3eb812fd4 | 641 | if (checkCellLink() == true) { |
JMF | 0:24d3eb812fd4 | 642 | if (reportStatus == false) { |
JMF | 0:24d3eb812fd4 | 643 | dbgPuts("Re-connected to cellular network!"); |
JMF | 0:24d3eb812fd4 | 644 | reportStatus = true; |
JMF | 0:24d3eb812fd4 | 645 | } |
JMF | 0:24d3eb812fd4 | 646 | |
JMF | 0:24d3eb812fd4 | 647 | // WNC has SIM and registered on network so |
JMF | 0:24d3eb812fd4 | 648 | // soft initialize the WNC. |
JMF | 0:24d3eb812fd4 | 649 | for (i = 0; i < WNC_SOFT_INIT_RETRY_COUNT; i++) |
JMF | 0:24d3eb812fd4 | 650 | if (at_init_wnc() == true) |
JMF | 0:24d3eb812fd4 | 651 | break; |
JMF | 0:24d3eb812fd4 | 652 | |
JMF | 0:24d3eb812fd4 | 653 | // If it did not respond try a hardware init |
JMF | 0:24d3eb812fd4 | 654 | if (i == WNC_SOFT_INIT_RETRY_COUNT) |
JMF | 0:24d3eb812fd4 | 655 | { |
JMF | 0:24d3eb812fd4 | 656 | at_reinitialize_mdm(); |
JMF | 0:24d3eb812fd4 | 657 | return (at_init_wnc(true)); // Hard reset occurred so make it go through the software init(); |
JMF | 0:24d3eb812fd4 | 658 | } |
JMF | 0:24d3eb812fd4 | 659 | else |
JMF | 0:24d3eb812fd4 | 660 | return (true); |
JMF | 0:24d3eb812fd4 | 661 | } |
JMF | 0:24d3eb812fd4 | 662 | else |
JMF | 0:24d3eb812fd4 | 663 | { |
JMF | 0:24d3eb812fd4 | 664 | if (reportStatus == true) { |
JMF | 0:24d3eb812fd4 | 665 | dbgPuts("Not connected to cellular network!"); |
JMF | 0:24d3eb812fd4 | 666 | reportStatus = false; |
JMF | 0:24d3eb812fd4 | 667 | } |
JMF | 0:24d3eb812fd4 | 668 | return (false); |
JMF | 0:24d3eb812fd4 | 669 | } |
JMF | 0:24d3eb812fd4 | 670 | } |
JMF | 0:24d3eb812fd4 | 671 | |
JMF | 0:24d3eb812fd4 | 672 | WncController::AtCmdErr_e WncController::sendWncCmd(const char * const s, string ** r, int ms_timeout) |
JMF | 0:24d3eb812fd4 | 673 | { |
JMF | 0:24d3eb812fd4 | 674 | if (checkCellLink() == false) { |
JMF | 0:24d3eb812fd4 | 675 | static string noRespStr; |
JMF | 0:24d3eb812fd4 | 676 | |
JMF | 0:24d3eb812fd4 | 677 | // Save some run-time! |
JMF | 0:24d3eb812fd4 | 678 | if (m_sDebugEnabled) |
JMF | 0:24d3eb812fd4 | 679 | { |
JMF | 0:24d3eb812fd4 | 680 | dbgPuts("FAIL send cmd: ", false); |
JMF | 0:24d3eb812fd4 | 681 | if (m_sMoreDebugEnabled && m_sDebugEnabled) { |
JMF | 0:24d3eb812fd4 | 682 | dbgPutsNoTime(s); |
JMF | 0:24d3eb812fd4 | 683 | } |
JMF | 0:24d3eb812fd4 | 684 | else { |
JMF | 0:24d3eb812fd4 | 685 | size_t n = strlen(s); |
JMF | 0:24d3eb812fd4 | 686 | if (n <= WNC_TRUNC_DEBUG_LENGTH) { |
JMF | 0:24d3eb812fd4 | 687 | dbgPutsNoTime(s); |
JMF | 0:24d3eb812fd4 | 688 | } |
JMF | 0:24d3eb812fd4 | 689 | else { |
JMF | 0:24d3eb812fd4 | 690 | string truncStr(s,WNC_TRUNC_DEBUG_LENGTH/2); |
JMF | 0:24d3eb812fd4 | 691 | truncStr += ".."; |
JMF | 0:24d3eb812fd4 | 692 | truncStr += &s[n-(WNC_TRUNC_DEBUG_LENGTH/2)]; |
JMF | 0:24d3eb812fd4 | 693 | dbgPutsNoTime(truncStr.c_str()); |
JMF | 0:24d3eb812fd4 | 694 | } |
JMF | 0:24d3eb812fd4 | 695 | } |
JMF | 0:24d3eb812fd4 | 696 | } |
JMF | 0:24d3eb812fd4 | 697 | |
JMF | 0:24d3eb812fd4 | 698 | noRespStr.erase(); |
JMF | 0:24d3eb812fd4 | 699 | *r = &noRespStr; |
JMF | 0:24d3eb812fd4 | 700 | |
JMF | 0:24d3eb812fd4 | 701 | return (WNC_AT_CMD_NO_CELL_LINK); |
JMF | 0:24d3eb812fd4 | 702 | } |
JMF | 0:24d3eb812fd4 | 703 | |
JMF | 0:24d3eb812fd4 | 704 | if (m_sCheckNetStatus) |
JMF | 0:24d3eb812fd4 | 705 | { |
JMF | 0:24d3eb812fd4 | 706 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 707 | dbgPuts("[---------- Network Status -------------"); |
JMF | 0:24d3eb812fd4 | 708 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 709 | at_send_wnc_cmd("AT@SOCKDIAL?", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 710 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 711 | dbgPuts("---------------------------------------]"); |
JMF | 0:24d3eb812fd4 | 712 | } |
JMF | 0:24d3eb812fd4 | 713 | |
JMF | 0:24d3eb812fd4 | 714 | // If WNC ready, send user command |
JMF | 0:24d3eb812fd4 | 715 | return (at_send_wnc_cmd(s, r, ms_timeout)); |
JMF | 0:24d3eb812fd4 | 716 | } |
JMF | 0:24d3eb812fd4 | 717 | |
JMF | 0:24d3eb812fd4 | 718 | WncController::AtCmdErr_e WncController::at_send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 0:24d3eb812fd4 | 719 | { |
JMF | 0:24d3eb812fd4 | 720 | // Save some run-time! |
JMF | 0:24d3eb812fd4 | 721 | if (m_sDebugEnabled) |
JMF | 0:24d3eb812fd4 | 722 | { |
JMF | 0:24d3eb812fd4 | 723 | if (m_sMoreDebugEnabled) { |
JMF | 0:24d3eb812fd4 | 724 | dbgPuts("TX: ", false); dbgPutsNoTime(s); |
JMF | 0:24d3eb812fd4 | 725 | } |
JMF | 0:24d3eb812fd4 | 726 | else { |
JMF | 0:24d3eb812fd4 | 727 | if (m_sDebugEnabled) { // Save some run-time! |
JMF | 0:24d3eb812fd4 | 728 | size_t n = strlen(s); |
JMF | 0:24d3eb812fd4 | 729 | if (n <= WNC_TRUNC_DEBUG_LENGTH) { |
JMF | 0:24d3eb812fd4 | 730 | dbgPuts("TX: ", false); dbgPutsNoTime(s); |
JMF | 0:24d3eb812fd4 | 731 | } |
JMF | 0:24d3eb812fd4 | 732 | else { |
JMF | 0:24d3eb812fd4 | 733 | string truncStr(s,WNC_TRUNC_DEBUG_LENGTH/2); |
JMF | 0:24d3eb812fd4 | 734 | truncStr += ".."; |
JMF | 0:24d3eb812fd4 | 735 | truncStr += &s[n - (WNC_TRUNC_DEBUG_LENGTH/2)]; |
JMF | 0:24d3eb812fd4 | 736 | dbgPuts("TX: ", false); dbgPutsNoTime(truncStr.c_str()); |
JMF | 0:24d3eb812fd4 | 737 | } |
JMF | 0:24d3eb812fd4 | 738 | } |
JMF | 0:24d3eb812fd4 | 739 | } |
JMF | 0:24d3eb812fd4 | 740 | } |
JMF | 0:24d3eb812fd4 | 741 | |
JMF | 0:24d3eb812fd4 | 742 | AtCmdErr_e atResult = mdmSendAtCmdRsp(s, ms_timeout, &m_sWncStr); |
JMF | 0:24d3eb812fd4 | 743 | *r = &m_sWncStr; // Return a pointer to the static string |
JMF | 0:24d3eb812fd4 | 744 | |
JMF | 0:24d3eb812fd4 | 745 | if (atResult != WNC_AT_CMD_TIMEOUT) { |
JMF | 0:24d3eb812fd4 | 746 | // If a prior command timed out but a new one works then |
JMF | 0:24d3eb812fd4 | 747 | // change the state back to ON. We don't know here in this |
JMF | 0:24d3eb812fd4 | 748 | // method if the Cell Link is good so assume it is. When a command |
JMF | 0:24d3eb812fd4 | 749 | // that depends on the cell link is made it will update the state. |
JMF | 0:24d3eb812fd4 | 750 | if (m_sState == WNC_NO_RESPONSE) |
JMF | 0:24d3eb812fd4 | 751 | m_sState = WNC_ON; |
JMF | 0:24d3eb812fd4 | 752 | |
JMF | 0:24d3eb812fd4 | 753 | // Save some run-time! |
JMF | 0:24d3eb812fd4 | 754 | if (m_sDebugEnabled) |
JMF | 0:24d3eb812fd4 | 755 | { |
JMF | 0:24d3eb812fd4 | 756 | dbgPuts("RX: ", false); |
JMF | 0:24d3eb812fd4 | 757 | if (m_sMoreDebugEnabled) { |
JMF | 0:24d3eb812fd4 | 758 | dbgPutsNoTime(m_sWncStr.c_str()); |
JMF | 0:24d3eb812fd4 | 759 | } |
JMF | 0:24d3eb812fd4 | 760 | else { |
JMF | 0:24d3eb812fd4 | 761 | if (m_sWncStr.size() <= WNC_TRUNC_DEBUG_LENGTH) { |
JMF | 0:24d3eb812fd4 | 762 | dbgPutsNoTime(m_sWncStr.c_str()); |
JMF | 0:24d3eb812fd4 | 763 | } |
JMF | 0:24d3eb812fd4 | 764 | else { |
JMF | 0:24d3eb812fd4 | 765 | string truncStr = m_sWncStr.substr(0,WNC_TRUNC_DEBUG_LENGTH/2) + ".."; |
JMF | 0:24d3eb812fd4 | 766 | truncStr += m_sWncStr.substr(m_sWncStr.size() - (WNC_TRUNC_DEBUG_LENGTH/2), WNC_TRUNC_DEBUG_LENGTH/2); |
JMF | 0:24d3eb812fd4 | 767 | dbgPutsNoTime(truncStr.c_str()); |
JMF | 0:24d3eb812fd4 | 768 | } |
JMF | 0:24d3eb812fd4 | 769 | } |
JMF | 0:24d3eb812fd4 | 770 | } |
JMF | 0:24d3eb812fd4 | 771 | } |
JMF | 0:24d3eb812fd4 | 772 | else { |
JMF | 0:24d3eb812fd4 | 773 | m_sState = WNC_NO_RESPONSE; |
JMF | 0:24d3eb812fd4 | 774 | dbgPuts("AT Cmd TIMEOUT!"); |
JMF | 0:24d3eb812fd4 | 775 | dbgPuts("RX: ", false); dbgPutsNoTime(m_sWncStr.c_str()); |
JMF | 0:24d3eb812fd4 | 776 | } |
JMF | 0:24d3eb812fd4 | 777 | |
JMF | 0:24d3eb812fd4 | 778 | return (atResult); |
JMF | 0:24d3eb812fd4 | 779 | } |
JMF | 0:24d3eb812fd4 | 780 | |
JMF | 0:24d3eb812fd4 | 781 | void WncController::closeOpenSocket(uint16_t numSock) |
JMF | 0:24d3eb812fd4 | 782 | { |
JMF | 0:24d3eb812fd4 | 783 | // Try to open and close the socket |
JMF | 0:24d3eb812fd4 | 784 | do { |
JMF | 0:24d3eb812fd4 | 785 | dbgPuts("Try to close and re-open socket"); |
JMF | 0:24d3eb812fd4 | 786 | if (false == at_sockclose_wnc(m_sSock[numSock].numWncSock)) { |
JMF | 0:24d3eb812fd4 | 787 | if (WNC_NO_RESPONSE == getWncStatus()) { |
JMF | 0:24d3eb812fd4 | 788 | dbgPuts("No response for closeOpenSocket1"); |
JMF | 0:24d3eb812fd4 | 789 | return ; |
JMF | 0:24d3eb812fd4 | 790 | } |
JMF | 0:24d3eb812fd4 | 791 | } |
JMF | 0:24d3eb812fd4 | 792 | |
JMF | 0:24d3eb812fd4 | 793 | int numWncSock = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), m_sSock[numSock].myPort, numSock, m_sSock[numSock].isTcp, m_sSock[numSock].timeOutSec); |
JMF | 0:24d3eb812fd4 | 794 | m_sSock[numSock].numWncSock = numWncSock; |
JMF | 0:24d3eb812fd4 | 795 | if (numWncSock > 0 && numWncSock <= (int)MAX_NUM_WNC_SOCKETS) |
JMF | 0:24d3eb812fd4 | 796 | m_sSock[numSock].open = true; |
JMF | 0:24d3eb812fd4 | 797 | else { |
JMF | 0:24d3eb812fd4 | 798 | m_sSock[numSock].open = false; |
JMF | 0:24d3eb812fd4 | 799 | dbgPuts("Failed to re-open socket!"); |
JMF | 0:24d3eb812fd4 | 800 | } |
JMF | 0:24d3eb812fd4 | 801 | |
JMF | 0:24d3eb812fd4 | 802 | if (WNC_NO_RESPONSE == getWncStatus()) { |
JMF | 0:24d3eb812fd4 | 803 | dbgPuts("No response for closeOpenSocket2"); |
JMF | 0:24d3eb812fd4 | 804 | return ; |
JMF | 0:24d3eb812fd4 | 805 | } |
JMF | 0:24d3eb812fd4 | 806 | } while (m_sSock[numSock].open == false); |
JMF | 0:24d3eb812fd4 | 807 | } |
JMF | 0:24d3eb812fd4 | 808 | |
JMF | 0:24d3eb812fd4 | 809 | bool WncController::getICCID(string * iccid) |
JMF | 0:24d3eb812fd4 | 810 | { |
JMF | 0:24d3eb812fd4 | 811 | if (at_geticcid_wnc(iccid) == false) { |
JMF | 0:24d3eb812fd4 | 812 | dbgPuts("getICCID error!"); |
JMF | 0:24d3eb812fd4 | 813 | return (false); |
JMF | 0:24d3eb812fd4 | 814 | } |
JMF | 0:24d3eb812fd4 | 815 | |
JMF | 0:24d3eb812fd4 | 816 | return (true); |
JMF | 0:24d3eb812fd4 | 817 | } |
JMF | 0:24d3eb812fd4 | 818 | |
JMF | 0:24d3eb812fd4 | 819 | bool WncController::at_geticcid_wnc(string * iccid) |
JMF | 0:24d3eb812fd4 | 820 | { |
JMF | 0:24d3eb812fd4 | 821 | string * respStr; |
JMF | 0:24d3eb812fd4 | 822 | |
JMF | 0:24d3eb812fd4 | 823 | iccid->erase(); |
JMF | 0:24d3eb812fd4 | 824 | |
JMF | 0:24d3eb812fd4 | 825 | AtCmdErr_e r = at_send_wnc_cmd("AT%CCID", &respStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 826 | |
JMF | 0:24d3eb812fd4 | 827 | if (r != WNC_AT_CMD_OK || respStr->size() == 0) |
JMF | 0:24d3eb812fd4 | 828 | return (false); |
JMF | 0:24d3eb812fd4 | 829 | |
JMF | 0:24d3eb812fd4 | 830 | // New Firmware versions respond to the %CCID command with "%CCID:" |
JMF | 0:24d3eb812fd4 | 831 | // but old version respond with "AT%CCID", so check to see which we have |
JMF | 0:24d3eb812fd4 | 832 | size_t pos = respStr->find(":"); |
JMF | 0:24d3eb812fd4 | 833 | if (pos == string::npos) |
JMF | 0:24d3eb812fd4 | 834 | pos = respStr->find("AT%CCID"); |
JMF | 0:24d3eb812fd4 | 835 | else |
JMF | 0:24d3eb812fd4 | 836 | pos = respStr->find("%CCID"); |
JMF | 0:24d3eb812fd4 | 837 | |
JMF | 0:24d3eb812fd4 | 838 | if (pos == string::npos) |
JMF | 0:24d3eb812fd4 | 839 | return (false); |
JMF | 0:24d3eb812fd4 | 840 | |
JMF | 0:24d3eb812fd4 | 841 | pos += 7; // Advanced to the number |
JMF | 0:24d3eb812fd4 | 842 | |
JMF | 0:24d3eb812fd4 | 843 | size_t posOK = respStr->rfind("OK"); |
JMF | 0:24d3eb812fd4 | 844 | if (posOK == string::npos) |
JMF | 0:24d3eb812fd4 | 845 | return (false); |
JMF | 0:24d3eb812fd4 | 846 | |
JMF | 0:24d3eb812fd4 | 847 | *iccid = respStr->substr(pos, posOK - pos); |
JMF | 0:24d3eb812fd4 | 848 | |
JMF | 0:24d3eb812fd4 | 849 | return (true); |
JMF | 0:24d3eb812fd4 | 850 | } |
JMF | 0:24d3eb812fd4 | 851 | |
JMF | 0:24d3eb812fd4 | 852 | bool WncController::convertICCIDtoMSISDN(const string & iccid, string * msisdn) |
JMF | 0:24d3eb812fd4 | 853 | { |
JMF | 0:24d3eb812fd4 | 854 | msisdn->erase(); |
JMF | 0:24d3eb812fd4 | 855 | |
JMF | 0:24d3eb812fd4 | 856 | if (iccid.size() != 20 && iccid.size() != 19) { |
JMF | 0:24d3eb812fd4 | 857 | dbgPuts("Invalid ICCID length!"); |
JMF | 0:24d3eb812fd4 | 858 | return (false); |
JMF | 0:24d3eb812fd4 | 859 | } |
JMF | 0:24d3eb812fd4 | 860 | |
JMF | 0:24d3eb812fd4 | 861 | *msisdn = "882350"; |
JMF | 0:24d3eb812fd4 | 862 | |
JMF | 0:24d3eb812fd4 | 863 | if (iccid.size() == 20) |
JMF | 0:24d3eb812fd4 | 864 | *msisdn += iccid.substr(10,iccid.size() - 11); |
JMF | 0:24d3eb812fd4 | 865 | else |
JMF | 0:24d3eb812fd4 | 866 | *msisdn += iccid.substr(10,iccid.size() - 10); |
JMF | 0:24d3eb812fd4 | 867 | |
JMF | 0:24d3eb812fd4 | 868 | return (true); |
JMF | 0:24d3eb812fd4 | 869 | } |
JMF | 0:24d3eb812fd4 | 870 | |
JMF | 0:24d3eb812fd4 | 871 | bool WncController::sendSMSText(const char * const phoneNum, const char * const text) |
JMF | 0:24d3eb812fd4 | 872 | { |
JMF | 0:24d3eb812fd4 | 873 | if (at_sendSMStext_wnc(phoneNum, text) == true) |
JMF | 0:24d3eb812fd4 | 874 | return (true); |
JMF | 0:24d3eb812fd4 | 875 | else { |
JMF | 0:24d3eb812fd4 | 876 | dbgPuts("sendSMSText: Failed!"); |
JMF | 0:24d3eb812fd4 | 877 | return (false); |
JMF | 0:24d3eb812fd4 | 878 | } |
JMF | 0:24d3eb812fd4 | 879 | } |
JMF | 0:24d3eb812fd4 | 880 | |
JMF | 0:24d3eb812fd4 | 881 | bool WncController::readSMSLog(struct WncSmsList * log) |
JMF | 0:24d3eb812fd4 | 882 | { |
JMF | 0:24d3eb812fd4 | 883 | string * logStr; |
JMF | 0:24d3eb812fd4 | 884 | uint16_t i; |
JMF | 0:24d3eb812fd4 | 885 | |
JMF | 0:24d3eb812fd4 | 886 | if (at_readSMSlog_wnc(&logStr) == false) { |
JMF | 0:24d3eb812fd4 | 887 | dbgPuts("readSMSLog: Failed!"); |
JMF | 0:24d3eb812fd4 | 888 | return (false); |
JMF | 0:24d3eb812fd4 | 889 | } |
JMF | 0:24d3eb812fd4 | 890 | |
JMF | 0:24d3eb812fd4 | 891 | // Clean slate |
JMF | 0:24d3eb812fd4 | 892 | log->msgCount = 0; |
JMF | 0:24d3eb812fd4 | 893 | |
JMF | 0:24d3eb812fd4 | 894 | if (logStr->size() == 0) |
JMF | 0:24d3eb812fd4 | 895 | return (false); |
JMF | 0:24d3eb812fd4 | 896 | |
JMF | 0:24d3eb812fd4 | 897 | // Pick out the stuff from the string and convert to struct |
JMF | 0:24d3eb812fd4 | 898 | string s; |
JMF | 0:24d3eb812fd4 | 899 | size_t pos2; |
JMF | 0:24d3eb812fd4 | 900 | size_t pos = logStr->find("+CMGL:"); |
JMF | 0:24d3eb812fd4 | 901 | |
JMF | 0:24d3eb812fd4 | 902 | for(i=0; i<MAX_WNC_SMS_MSG_SLOTS; i++) { |
JMF | 0:24d3eb812fd4 | 903 | // Start with a clean slate, let parsing fill out later. |
JMF | 0:24d3eb812fd4 | 904 | log->e[i].unread = false; |
JMF | 0:24d3eb812fd4 | 905 | log->e[i].incoming = false; |
JMF | 0:24d3eb812fd4 | 906 | log->e[i].unsent = false; |
JMF | 0:24d3eb812fd4 | 907 | log->e[i].pduMode = false; |
JMF | 0:24d3eb812fd4 | 908 | log->e[i].msgReceipt = false; |
JMF | 0:24d3eb812fd4 | 909 | |
JMF | 0:24d3eb812fd4 | 910 | log->e[i].idx = logStr->at(pos + 7); |
JMF | 0:24d3eb812fd4 | 911 | if (pos == string::npos) |
JMF | 0:24d3eb812fd4 | 912 | return (false); |
JMF | 0:24d3eb812fd4 | 913 | pos2 = logStr->find(",\"", pos); |
JMF | 0:24d3eb812fd4 | 914 | if (pos2 == string::npos) { |
JMF | 0:24d3eb812fd4 | 915 | // If the WNC acts wrong and receives a PDU mode |
JMF | 0:24d3eb812fd4 | 916 | // SMS there will not be any quotes in the response, |
JMF | 0:24d3eb812fd4 | 917 | // just take the whole reply and make it the message body for |
JMF | 0:24d3eb812fd4 | 918 | // now, mark it as an unread message, set the pdu flag! |
JMF | 0:24d3eb812fd4 | 919 | log->e[log->msgCount].unread = true; |
JMF | 0:24d3eb812fd4 | 920 | log->e[log->msgCount].pduMode = true; |
JMF | 0:24d3eb812fd4 | 921 | log->msgCount++; |
JMF | 0:24d3eb812fd4 | 922 | |
JMF | 0:24d3eb812fd4 | 923 | pos2 = logStr->find("+CMGL", pos + 5); |
JMF | 0:24d3eb812fd4 | 924 | if (pos2 == string::npos) { |
JMF | 0:24d3eb812fd4 | 925 | pos2 = logStr->find("OK", pos + 5); |
JMF | 0:24d3eb812fd4 | 926 | if (pos2 == string::npos) { |
JMF | 0:24d3eb812fd4 | 927 | dbgPuts("Strange SMS Log Ending!"); |
JMF | 0:24d3eb812fd4 | 928 | return (false); |
JMF | 0:24d3eb812fd4 | 929 | } |
JMF | 0:24d3eb812fd4 | 930 | i = MAX_WNC_SMS_MSG_SLOTS; |
JMF | 0:24d3eb812fd4 | 931 | } |
JMF | 0:24d3eb812fd4 | 932 | log->e[log->msgCount].msg = logStr->substr(0, pos2 - pos); |
JMF | 0:24d3eb812fd4 | 933 | pos = pos2; // for loop starts off expecting pos to point to next log msg |
JMF | 0:24d3eb812fd4 | 934 | continue; |
JMF | 0:24d3eb812fd4 | 935 | } |
JMF | 0:24d3eb812fd4 | 936 | pos += 2; // Advance to the text we want |
JMF | 0:24d3eb812fd4 | 937 | pos2 = logStr->find("\",", pos); |
JMF | 0:24d3eb812fd4 | 938 | if ((pos2 == string::npos) || (pos >= pos2)) |
JMF | 0:24d3eb812fd4 | 939 | return (false); |
JMF | 0:24d3eb812fd4 | 940 | |
JMF | 0:24d3eb812fd4 | 941 | // Setup attributes |
JMF | 0:24d3eb812fd4 | 942 | s = logStr->substr(pos, pos2 - pos); |
JMF | 0:24d3eb812fd4 | 943 | if (s.find("REC READ") != string::npos) |
JMF | 0:24d3eb812fd4 | 944 | log->e[i].incoming = true; |
JMF | 0:24d3eb812fd4 | 945 | if (s.find("REC UNREAD") != string::npos) { |
JMF | 0:24d3eb812fd4 | 946 | log->e[i].unread = true; |
JMF | 0:24d3eb812fd4 | 947 | log->e[i].incoming = true; |
JMF | 0:24d3eb812fd4 | 948 | } |
JMF | 0:24d3eb812fd4 | 949 | if (s.find("STO UNSENT") != string::npos) |
JMF | 0:24d3eb812fd4 | 950 | log->e[i].unsent = true; |
JMF | 0:24d3eb812fd4 | 951 | if (logStr->find(",,") == string::npos) |
JMF | 0:24d3eb812fd4 | 952 | log->e[i].msgReceipt = true; |
JMF | 0:24d3eb812fd4 | 953 | |
JMF | 0:24d3eb812fd4 | 954 | // Tele number |
JMF | 0:24d3eb812fd4 | 955 | pos2 = logStr->find(",\"", pos2); |
JMF | 0:24d3eb812fd4 | 956 | if (pos2 == string::npos) |
JMF | 0:24d3eb812fd4 | 957 | return (false); |
JMF | 0:24d3eb812fd4 | 958 | pos2 += 2; // Advance to next field |
JMF | 0:24d3eb812fd4 | 959 | pos = logStr->find("\",", pos2); |
JMF | 0:24d3eb812fd4 | 960 | if ((pos == string::npos) || (pos2 > pos)) |
JMF | 0:24d3eb812fd4 | 961 | return (false); |
JMF | 0:24d3eb812fd4 | 962 | if (pos == pos2) |
JMF | 0:24d3eb812fd4 | 963 | log->e[i].number.erase(); |
JMF | 0:24d3eb812fd4 | 964 | else |
JMF | 0:24d3eb812fd4 | 965 | log->e[i].number = logStr->substr(pos2, pos - pos2); |
JMF | 0:24d3eb812fd4 | 966 | |
JMF | 0:24d3eb812fd4 | 967 | // Date |
JMF | 0:24d3eb812fd4 | 968 | pos = logStr->find(",\"", pos); |
JMF | 0:24d3eb812fd4 | 969 | if (pos == string::npos) |
JMF | 0:24d3eb812fd4 | 970 | return (false); |
JMF | 0:24d3eb812fd4 | 971 | pos += 2; // Beginning of date field |
JMF | 0:24d3eb812fd4 | 972 | pos2 = logStr->find(",", pos); // End of timestamp field |
JMF | 0:24d3eb812fd4 | 973 | if ((pos2 == string::npos) || (pos > pos2)) |
JMF | 0:24d3eb812fd4 | 974 | return (false); |
JMF | 0:24d3eb812fd4 | 975 | if (pos == pos2) |
JMF | 0:24d3eb812fd4 | 976 | log->e[i].date.erase(); |
JMF | 0:24d3eb812fd4 | 977 | else |
JMF | 0:24d3eb812fd4 | 978 | log->e[i].date = logStr->substr(pos, pos2 - pos); |
JMF | 0:24d3eb812fd4 | 979 | |
JMF | 0:24d3eb812fd4 | 980 | // Timestamp |
JMF | 0:24d3eb812fd4 | 981 | pos = logStr->find("\",", pos2); // End of timestamp |
JMF | 0:24d3eb812fd4 | 982 | if (pos == string::npos) |
JMF | 0:24d3eb812fd4 | 983 | return (false); |
JMF | 0:24d3eb812fd4 | 984 | pos2 += 1; // Beginning of time field |
JMF | 0:24d3eb812fd4 | 985 | if (pos < pos2) |
JMF | 0:24d3eb812fd4 | 986 | return (false); |
JMF | 0:24d3eb812fd4 | 987 | if (pos == pos2) |
JMF | 0:24d3eb812fd4 | 988 | log->e[i].time.erase(); |
JMF | 0:24d3eb812fd4 | 989 | else |
JMF | 0:24d3eb812fd4 | 990 | log->e[i].time = logStr->substr(pos2, pos - pos2); |
JMF | 0:24d3eb812fd4 | 991 | |
JMF | 0:24d3eb812fd4 | 992 | // Message field |
JMF | 0:24d3eb812fd4 | 993 | |
JMF | 0:24d3eb812fd4 | 994 | // We don't know how many messages we have so the next search |
JMF | 0:24d3eb812fd4 | 995 | // could end with +CMGL or OK. |
JMF | 0:24d3eb812fd4 | 996 | pos += 2; // Advanced to message text |
JMF | 0:24d3eb812fd4 | 997 | pos2 = logStr->find("+CMGL", pos); |
JMF | 0:24d3eb812fd4 | 998 | if (pos2 == string::npos) { |
JMF | 0:24d3eb812fd4 | 999 | pos2 = logStr->find("OK", pos); |
JMF | 0:24d3eb812fd4 | 1000 | if (pos2 == string::npos) { |
JMF | 0:24d3eb812fd4 | 1001 | dbgPuts("Strange SMS Log Ending!"); |
JMF | 0:24d3eb812fd4 | 1002 | return (false); |
JMF | 0:24d3eb812fd4 | 1003 | } |
JMF | 0:24d3eb812fd4 | 1004 | i = MAX_WNC_SMS_MSG_SLOTS; // break |
JMF | 0:24d3eb812fd4 | 1005 | } |
JMF | 0:24d3eb812fd4 | 1006 | if (pos > pos2) |
JMF | 0:24d3eb812fd4 | 1007 | return (false); |
JMF | 0:24d3eb812fd4 | 1008 | if (pos == pos2) |
JMF | 0:24d3eb812fd4 | 1009 | log->e[log->msgCount].msg.erase(); |
JMF | 0:24d3eb812fd4 | 1010 | else |
JMF | 0:24d3eb812fd4 | 1011 | log->e[log->msgCount].msg = logStr->substr(pos, pos2 - pos); |
JMF | 0:24d3eb812fd4 | 1012 | |
JMF | 0:24d3eb812fd4 | 1013 | log->msgCount++; // Message complete |
JMF | 0:24d3eb812fd4 | 1014 | } |
JMF | 0:24d3eb812fd4 | 1015 | |
JMF | 0:24d3eb812fd4 | 1016 | return (true); |
JMF | 0:24d3eb812fd4 | 1017 | } |
JMF | 0:24d3eb812fd4 | 1018 | |
JMF | 0:24d3eb812fd4 | 1019 | bool WncController::readUnreadSMSText(struct WncSmsList * w, bool deleteRead) |
JMF | 0:24d3eb812fd4 | 1020 | { |
JMF | 0:24d3eb812fd4 | 1021 | struct WncController::WncSmsList tmp; |
JMF | 0:24d3eb812fd4 | 1022 | |
JMF | 0:24d3eb812fd4 | 1023 | if (readSMSLog(&tmp) == false) |
JMF | 0:24d3eb812fd4 | 1024 | return (false); |
JMF | 0:24d3eb812fd4 | 1025 | |
JMF | 0:24d3eb812fd4 | 1026 | w->msgCount = 0; |
JMF | 0:24d3eb812fd4 | 1027 | for(uint16_t i = 0; i < tmp.msgCount; i++) { |
JMF | 0:24d3eb812fd4 | 1028 | if (tmp.e[i].unread == true) { |
JMF | 0:24d3eb812fd4 | 1029 | w->e[w->msgCount] = tmp.e[i]; |
JMF | 0:24d3eb812fd4 | 1030 | w->msgCount++; |
JMF | 0:24d3eb812fd4 | 1031 | if (deleteRead == true) { |
JMF | 0:24d3eb812fd4 | 1032 | // Clean up message that was copied out and read |
JMF | 0:24d3eb812fd4 | 1033 | deleteSMSTextFromMem(w->e[i].idx); |
JMF | 0:24d3eb812fd4 | 1034 | } |
JMF | 0:24d3eb812fd4 | 1035 | } |
JMF | 0:24d3eb812fd4 | 1036 | } |
JMF | 0:24d3eb812fd4 | 1037 | |
JMF | 0:24d3eb812fd4 | 1038 | return (w->msgCount > 0); |
JMF | 0:24d3eb812fd4 | 1039 | } |
JMF | 0:24d3eb812fd4 | 1040 | |
JMF | 0:24d3eb812fd4 | 1041 | size_t WncController::getSignalQuality(const char ** log) |
JMF | 0:24d3eb812fd4 | 1042 | { |
JMF | 0:24d3eb812fd4 | 1043 | size_t n; |
JMF | 0:24d3eb812fd4 | 1044 | |
JMF | 0:24d3eb812fd4 | 1045 | n = at_getSignalQuality_wnc(log); |
JMF | 0:24d3eb812fd4 | 1046 | if (n == 0) |
JMF | 0:24d3eb812fd4 | 1047 | dbgPuts("readSMSText: Failed!"); |
JMF | 0:24d3eb812fd4 | 1048 | |
JMF | 0:24d3eb812fd4 | 1049 | return (n); |
JMF | 0:24d3eb812fd4 | 1050 | } |
JMF | 0:24d3eb812fd4 | 1051 | |
JMF | 0:24d3eb812fd4 | 1052 | size_t WncController::at_getSignalQuality_wnc(const char ** log) |
JMF | 0:24d3eb812fd4 | 1053 | { |
JMF | 0:24d3eb812fd4 | 1054 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1055 | static string logStr; |
JMF | 0:24d3eb812fd4 | 1056 | |
JMF | 0:24d3eb812fd4 | 1057 | logStr.erase(); |
JMF | 0:24d3eb812fd4 | 1058 | |
JMF | 0:24d3eb812fd4 | 1059 | if (at_send_wnc_cmd("AT%MEAS=\"0\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1060 | logStr = *pRespStr; |
JMF | 0:24d3eb812fd4 | 1061 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1062 | } |
JMF | 0:24d3eb812fd4 | 1063 | else |
JMF | 0:24d3eb812fd4 | 1064 | dbgPuts("AT%MEAS=0: failed!"); |
JMF | 0:24d3eb812fd4 | 1065 | |
JMF | 0:24d3eb812fd4 | 1066 | if (at_send_wnc_cmd("AT%MEAS=\"1\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1067 | logStr += *pRespStr; |
JMF | 0:24d3eb812fd4 | 1068 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1069 | } |
JMF | 0:24d3eb812fd4 | 1070 | else |
JMF | 0:24d3eb812fd4 | 1071 | dbgPuts("AT%MEAS=1: failed!"); |
JMF | 0:24d3eb812fd4 | 1072 | |
JMF | 0:24d3eb812fd4 | 1073 | if (at_send_wnc_cmd("AT%MEAS=\"2\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1074 | logStr += *pRespStr; |
JMF | 0:24d3eb812fd4 | 1075 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1076 | } |
JMF | 0:24d3eb812fd4 | 1077 | else |
JMF | 0:24d3eb812fd4 | 1078 | dbgPuts("AT%MEAS=2: failed!"); |
JMF | 0:24d3eb812fd4 | 1079 | |
JMF | 0:24d3eb812fd4 | 1080 | if (at_send_wnc_cmd("AT%MEAS=\"3\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1081 | logStr += *pRespStr; |
JMF | 0:24d3eb812fd4 | 1082 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1083 | } |
JMF | 0:24d3eb812fd4 | 1084 | else |
JMF | 0:24d3eb812fd4 | 1085 | dbgPuts("AT%MEAS=3: failed!"); |
JMF | 0:24d3eb812fd4 | 1086 | |
JMF | 0:24d3eb812fd4 | 1087 | if (at_send_wnc_cmd("AT%MEAS=\"4\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1088 | logStr += *pRespStr; |
JMF | 0:24d3eb812fd4 | 1089 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1090 | } |
JMF | 0:24d3eb812fd4 | 1091 | else |
JMF | 0:24d3eb812fd4 | 1092 | dbgPuts("AT%MEAS=4: failed!"); |
JMF | 0:24d3eb812fd4 | 1093 | |
JMF | 0:24d3eb812fd4 | 1094 | if (at_send_wnc_cmd("AT%MEAS=\"5\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1095 | logStr += *pRespStr; |
JMF | 0:24d3eb812fd4 | 1096 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1097 | } |
JMF | 0:24d3eb812fd4 | 1098 | else |
JMF | 0:24d3eb812fd4 | 1099 | dbgPuts("AT%MEAS=5: failed!"); |
JMF | 0:24d3eb812fd4 | 1100 | |
JMF | 0:24d3eb812fd4 | 1101 | if (at_send_wnc_cmd("AT%MEAS=\"8\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1102 | logStr += *pRespStr; |
JMF | 0:24d3eb812fd4 | 1103 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1104 | } |
JMF | 0:24d3eb812fd4 | 1105 | else |
JMF | 0:24d3eb812fd4 | 1106 | dbgPuts("AT%MEAS=8: failed!"); |
JMF | 0:24d3eb812fd4 | 1107 | |
JMF | 0:24d3eb812fd4 | 1108 | if (at_send_wnc_cmd("AT%MEAS=\"98\"", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1109 | logStr += *pRespStr; |
JMF | 0:24d3eb812fd4 | 1110 | logStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1111 | } |
JMF | 0:24d3eb812fd4 | 1112 | else |
JMF | 0:24d3eb812fd4 | 1113 | dbgPuts("AT%MEAS=98: failed!"); |
JMF | 0:24d3eb812fd4 | 1114 | |
JMF | 0:24d3eb812fd4 | 1115 | *log = logStr.c_str(); |
JMF | 0:24d3eb812fd4 | 1116 | |
JMF | 0:24d3eb812fd4 | 1117 | return (logStr.size()); |
JMF | 0:24d3eb812fd4 | 1118 | } |
JMF | 0:24d3eb812fd4 | 1119 | |
JMF | 0:24d3eb812fd4 | 1120 | bool WncController::getTimeDate(struct WncDateTime * tod) |
JMF | 0:24d3eb812fd4 | 1121 | { |
JMF | 0:24d3eb812fd4 | 1122 | if (at_gettimedate_wnc(tod) == true) |
JMF | 0:24d3eb812fd4 | 1123 | return (true); |
JMF | 0:24d3eb812fd4 | 1124 | else { |
JMF | 0:24d3eb812fd4 | 1125 | dbgPuts("Get time date failed!"); |
JMF | 0:24d3eb812fd4 | 1126 | return (false); |
JMF | 0:24d3eb812fd4 | 1127 | } |
JMF | 0:24d3eb812fd4 | 1128 | } |
JMF | 0:24d3eb812fd4 | 1129 | |
JMF | 0:24d3eb812fd4 | 1130 | bool WncController::at_ping_wnc(const char * ip) |
JMF | 0:24d3eb812fd4 | 1131 | { |
JMF | 0:24d3eb812fd4 | 1132 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1133 | string cmdStr = "AT@PINGREQ=\""; |
JMF | 0:24d3eb812fd4 | 1134 | cmdStr += ip; |
JMF | 0:24d3eb812fd4 | 1135 | cmdStr += "\""; |
JMF | 0:24d3eb812fd4 | 1136 | return (at_send_wnc_cmd(cmdStr.c_str(), &pRespStr, WNC_PING_CMD_TIMEOUT_MS) == WNC_AT_CMD_OK); |
JMF | 0:24d3eb812fd4 | 1137 | } |
JMF | 0:24d3eb812fd4 | 1138 | |
JMF | 0:24d3eb812fd4 | 1139 | bool WncController::at_gettimedate_wnc(struct WncDateTime * tod) |
JMF | 0:24d3eb812fd4 | 1140 | { |
JMF | 0:24d3eb812fd4 | 1141 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1142 | char * pEnd; |
JMF | 0:24d3eb812fd4 | 1143 | |
JMF | 0:24d3eb812fd4 | 1144 | if (at_send_wnc_cmd("AT+CCLK?", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1145 | if (pRespStr->size() > 0) { |
JMF | 0:24d3eb812fd4 | 1146 | size_t pos1 = pRespStr->find("+CCLK:"); |
JMF | 0:24d3eb812fd4 | 1147 | if (pos1 != string::npos) { |
JMF | 0:24d3eb812fd4 | 1148 | pEnd = (char *)pRespStr->c_str() + pos1 + 8; |
JMF | 0:24d3eb812fd4 | 1149 | tod->year = strtol(pEnd, &pEnd, 10); |
JMF | 0:24d3eb812fd4 | 1150 | tod->month = strtol(pEnd+1, &pEnd, 10); |
JMF | 0:24d3eb812fd4 | 1151 | tod->day = strtol(pEnd+1, &pEnd, 10); |
JMF | 0:24d3eb812fd4 | 1152 | tod->hour = strtol(pEnd+1, &pEnd, 10); |
JMF | 0:24d3eb812fd4 | 1153 | tod->min = strtol(pEnd+1, &pEnd, 10); |
JMF | 0:24d3eb812fd4 | 1154 | tod->sec = strtol(pEnd+1, &pEnd, 10); |
JMF | 0:24d3eb812fd4 | 1155 | return (true); |
JMF | 0:24d3eb812fd4 | 1156 | } |
JMF | 0:24d3eb812fd4 | 1157 | } |
JMF | 0:24d3eb812fd4 | 1158 | } |
JMF | 0:24d3eb812fd4 | 1159 | |
JMF | 0:24d3eb812fd4 | 1160 | return (false); |
JMF | 0:24d3eb812fd4 | 1161 | } |
JMF | 0:24d3eb812fd4 | 1162 | |
JMF | 0:24d3eb812fd4 | 1163 | bool WncController::at_get_wnc_net_stats(WncIpStats * s) |
JMF | 0:24d3eb812fd4 | 1164 | { |
JMF | 0:24d3eb812fd4 | 1165 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1166 | AtCmdErr_e cmdRes = at_send_wnc_cmd("AT+CGCONTRDP=1", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1167 | |
JMF | 0:24d3eb812fd4 | 1168 | if (WNC_AT_CMD_OK == cmdRes) { |
JMF | 0:24d3eb812fd4 | 1169 | if (pRespStr->size() > 0) { |
JMF | 0:24d3eb812fd4 | 1170 | memset((void*)s, '\0', sizeof(*s)); // Clean-up |
JMF | 0:24d3eb812fd4 | 1171 | string ss; |
JMF | 0:24d3eb812fd4 | 1172 | size_t pe; |
JMF | 0:24d3eb812fd4 | 1173 | size_t ps = pRespStr->rfind("\""); |
JMF | 0:24d3eb812fd4 | 1174 | if (ps != string::npos) { |
JMF | 0:24d3eb812fd4 | 1175 | ps += 2; // Skip the , after the " |
JMF | 0:24d3eb812fd4 | 1176 | pe = ps; |
JMF | 0:24d3eb812fd4 | 1177 | |
JMF | 0:24d3eb812fd4 | 1178 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1179 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1180 | return (false); |
JMF | 0:24d3eb812fd4 | 1181 | else |
JMF | 0:24d3eb812fd4 | 1182 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1183 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1184 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1185 | return (false); |
JMF | 0:24d3eb812fd4 | 1186 | else |
JMF | 0:24d3eb812fd4 | 1187 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1188 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1189 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1190 | return (false); |
JMF | 0:24d3eb812fd4 | 1191 | else |
JMF | 0:24d3eb812fd4 | 1192 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1193 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1194 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1195 | return (false); |
JMF | 0:24d3eb812fd4 | 1196 | else |
JMF | 0:24d3eb812fd4 | 1197 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1198 | |
JMF | 0:24d3eb812fd4 | 1199 | ss = pRespStr->substr(ps, pe - 1 - ps); |
JMF | 0:24d3eb812fd4 | 1200 | strncpy(s->ip, ss.c_str(), MAX_LEN_IP_STR); |
JMF | 0:24d3eb812fd4 | 1201 | s->ip[MAX_LEN_IP_STR - 1] = '\0'; |
JMF | 0:24d3eb812fd4 | 1202 | ps = pe; |
JMF | 0:24d3eb812fd4 | 1203 | |
JMF | 0:24d3eb812fd4 | 1204 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1205 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1206 | return (false); |
JMF | 0:24d3eb812fd4 | 1207 | else |
JMF | 0:24d3eb812fd4 | 1208 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1209 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1210 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1211 | return (false); |
JMF | 0:24d3eb812fd4 | 1212 | else |
JMF | 0:24d3eb812fd4 | 1213 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1214 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1215 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1216 | return (false); |
JMF | 0:24d3eb812fd4 | 1217 | else |
JMF | 0:24d3eb812fd4 | 1218 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1219 | pe = pRespStr->find(",", pe); |
JMF | 0:24d3eb812fd4 | 1220 | |
JMF | 0:24d3eb812fd4 | 1221 | ss = pRespStr->substr(ps, pe - ps); |
JMF | 0:24d3eb812fd4 | 1222 | strncpy(s->mask, ss.c_str(), MAX_LEN_IP_STR); |
JMF | 0:24d3eb812fd4 | 1223 | s->mask[MAX_LEN_IP_STR - 1] = '\0'; |
JMF | 0:24d3eb812fd4 | 1224 | ps = pe + 1; |
JMF | 0:24d3eb812fd4 | 1225 | |
JMF | 0:24d3eb812fd4 | 1226 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1227 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1228 | return (false); |
JMF | 0:24d3eb812fd4 | 1229 | else |
JMF | 0:24d3eb812fd4 | 1230 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1231 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1232 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1233 | return (false); |
JMF | 0:24d3eb812fd4 | 1234 | else |
JMF | 0:24d3eb812fd4 | 1235 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1236 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1237 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1238 | return (false); |
JMF | 0:24d3eb812fd4 | 1239 | else |
JMF | 0:24d3eb812fd4 | 1240 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1241 | pe = pRespStr->find(",", pe); |
JMF | 0:24d3eb812fd4 | 1242 | |
JMF | 0:24d3eb812fd4 | 1243 | ss = pRespStr->substr(ps, pe - ps); |
JMF | 0:24d3eb812fd4 | 1244 | strncpy(s->gateway, ss.c_str(), MAX_LEN_IP_STR); |
JMF | 0:24d3eb812fd4 | 1245 | s->gateway[MAX_LEN_IP_STR - 1] = '\0'; |
JMF | 0:24d3eb812fd4 | 1246 | ps = pe + 1; |
JMF | 0:24d3eb812fd4 | 1247 | |
JMF | 0:24d3eb812fd4 | 1248 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1249 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1250 | return (false); |
JMF | 0:24d3eb812fd4 | 1251 | else |
JMF | 0:24d3eb812fd4 | 1252 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1253 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1254 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1255 | return (false); |
JMF | 0:24d3eb812fd4 | 1256 | else |
JMF | 0:24d3eb812fd4 | 1257 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1258 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1259 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1260 | return (false); |
JMF | 0:24d3eb812fd4 | 1261 | else |
JMF | 0:24d3eb812fd4 | 1262 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1263 | pe = pRespStr->find(",", pe); |
JMF | 0:24d3eb812fd4 | 1264 | |
JMF | 0:24d3eb812fd4 | 1265 | |
JMF | 0:24d3eb812fd4 | 1266 | ss = pRespStr->substr(ps, pe - ps); |
JMF | 0:24d3eb812fd4 | 1267 | strncpy(s->dnsPrimary, ss.c_str(), MAX_LEN_IP_STR); |
JMF | 0:24d3eb812fd4 | 1268 | s->dnsPrimary[MAX_LEN_IP_STR - 1] = '\0'; |
JMF | 0:24d3eb812fd4 | 1269 | ps = pe + 1; |
JMF | 0:24d3eb812fd4 | 1270 | |
JMF | 0:24d3eb812fd4 | 1271 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1272 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1273 | return (false); |
JMF | 0:24d3eb812fd4 | 1274 | else |
JMF | 0:24d3eb812fd4 | 1275 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1276 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1277 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1278 | return (false); |
JMF | 0:24d3eb812fd4 | 1279 | else |
JMF | 0:24d3eb812fd4 | 1280 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1281 | pe = pRespStr->find(".", pe); |
JMF | 0:24d3eb812fd4 | 1282 | if (pe == string::npos) |
JMF | 0:24d3eb812fd4 | 1283 | return (false); |
JMF | 0:24d3eb812fd4 | 1284 | else |
JMF | 0:24d3eb812fd4 | 1285 | pe += 1; |
JMF | 0:24d3eb812fd4 | 1286 | pe = pRespStr->find(",", pe); |
JMF | 0:24d3eb812fd4 | 1287 | |
JMF | 0:24d3eb812fd4 | 1288 | |
JMF | 0:24d3eb812fd4 | 1289 | ss = pRespStr->substr(ps, pe - ps); |
JMF | 0:24d3eb812fd4 | 1290 | strncpy(s->dnsSecondary, ss.c_str(), MAX_LEN_IP_STR); |
JMF | 0:24d3eb812fd4 | 1291 | s->dnsSecondary[MAX_LEN_IP_STR - 1] = '\0'; |
JMF | 0:24d3eb812fd4 | 1292 | |
JMF | 0:24d3eb812fd4 | 1293 | dbgPuts("~~~~~~~~~~ WNC IP Stats ~~~~~~~~~~~~"); |
JMF | 0:24d3eb812fd4 | 1294 | dbgPuts("ip: ", false); dbgPutsNoTime(s->ip); |
JMF | 0:24d3eb812fd4 | 1295 | dbgPuts("mask: ", false); dbgPutsNoTime(s->mask); |
JMF | 0:24d3eb812fd4 | 1296 | dbgPuts("gateway: ", false); dbgPutsNoTime(s->gateway); |
JMF | 0:24d3eb812fd4 | 1297 | dbgPuts("dns pri: ", false); dbgPutsNoTime(s->dnsPrimary); |
JMF | 0:24d3eb812fd4 | 1298 | dbgPuts("dns sec: ", false); dbgPutsNoTime(s->dnsSecondary); |
JMF | 0:24d3eb812fd4 | 1299 | dbgPuts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); |
JMF | 0:24d3eb812fd4 | 1300 | |
JMF | 0:24d3eb812fd4 | 1301 | return (true); |
JMF | 0:24d3eb812fd4 | 1302 | } |
JMF | 0:24d3eb812fd4 | 1303 | } |
JMF | 0:24d3eb812fd4 | 1304 | } |
JMF | 0:24d3eb812fd4 | 1305 | |
JMF | 0:24d3eb812fd4 | 1306 | return (false); |
JMF | 0:24d3eb812fd4 | 1307 | } |
JMF | 0:24d3eb812fd4 | 1308 | |
JMF | 0:24d3eb812fd4 | 1309 | bool WncController::deleteSMSTextFromMem(char msgIdx) |
JMF | 0:24d3eb812fd4 | 1310 | { |
JMF | 0:24d3eb812fd4 | 1311 | const char * err = "deleteSMSTextFromMem: Failed!"; |
JMF | 0:24d3eb812fd4 | 1312 | |
JMF | 0:24d3eb812fd4 | 1313 | switch (msgIdx) |
JMF | 0:24d3eb812fd4 | 1314 | { |
JMF | 0:24d3eb812fd4 | 1315 | case '*': |
JMF | 0:24d3eb812fd4 | 1316 | at_deleteSMSTextFromMem_wnc('1'); |
JMF | 0:24d3eb812fd4 | 1317 | at_deleteSMSTextFromMem_wnc('2'); |
JMF | 0:24d3eb812fd4 | 1318 | at_deleteSMSTextFromMem_wnc('3'); |
JMF | 0:24d3eb812fd4 | 1319 | return (true); // WNC may error if slot empty, just ignore! |
JMF | 0:24d3eb812fd4 | 1320 | |
JMF | 0:24d3eb812fd4 | 1321 | case '1': |
JMF | 0:24d3eb812fd4 | 1322 | case '2': |
JMF | 0:24d3eb812fd4 | 1323 | case '3': |
JMF | 0:24d3eb812fd4 | 1324 | if (true == at_deleteSMSTextFromMem_wnc(msgIdx)) |
JMF | 0:24d3eb812fd4 | 1325 | return (true); |
JMF | 0:24d3eb812fd4 | 1326 | else { |
JMF | 0:24d3eb812fd4 | 1327 | dbgPuts(err); |
JMF | 0:24d3eb812fd4 | 1328 | return (false); |
JMF | 0:24d3eb812fd4 | 1329 | } |
JMF | 0:24d3eb812fd4 | 1330 | |
JMF | 0:24d3eb812fd4 | 1331 | default: |
JMF | 0:24d3eb812fd4 | 1332 | dbgPuts(err); |
JMF | 0:24d3eb812fd4 | 1333 | return (false); |
JMF | 0:24d3eb812fd4 | 1334 | } |
JMF | 0:24d3eb812fd4 | 1335 | } |
JMF | 0:24d3eb812fd4 | 1336 | |
JMF | 0:24d3eb812fd4 | 1337 | bool WncController::sendSMSTextFromMem(char msgIdx) |
JMF | 0:24d3eb812fd4 | 1338 | { |
JMF | 0:24d3eb812fd4 | 1339 | const char * err = "deleteSMSTextFromMem: Failed!"; |
JMF | 0:24d3eb812fd4 | 1340 | |
JMF | 0:24d3eb812fd4 | 1341 | switch (msgIdx) |
JMF | 0:24d3eb812fd4 | 1342 | { |
JMF | 0:24d3eb812fd4 | 1343 | case '*': |
JMF | 0:24d3eb812fd4 | 1344 | at_sendSMStextMem_wnc('1'); |
JMF | 0:24d3eb812fd4 | 1345 | at_sendSMStextMem_wnc('2'); |
JMF | 0:24d3eb812fd4 | 1346 | at_sendSMStextMem_wnc('3'); |
JMF | 0:24d3eb812fd4 | 1347 | return (true); // WNC may error if slot is empty, just ignore! |
JMF | 0:24d3eb812fd4 | 1348 | |
JMF | 0:24d3eb812fd4 | 1349 | case '1': |
JMF | 0:24d3eb812fd4 | 1350 | case '2': |
JMF | 0:24d3eb812fd4 | 1351 | case '3': |
JMF | 0:24d3eb812fd4 | 1352 | if (at_sendSMStextMem_wnc(msgIdx) == true) |
JMF | 0:24d3eb812fd4 | 1353 | return (true); |
JMF | 0:24d3eb812fd4 | 1354 | else { |
JMF | 0:24d3eb812fd4 | 1355 | dbgPuts(err); |
JMF | 0:24d3eb812fd4 | 1356 | return (false); |
JMF | 0:24d3eb812fd4 | 1357 | } |
JMF | 0:24d3eb812fd4 | 1358 | |
JMF | 0:24d3eb812fd4 | 1359 | default: |
JMF | 0:24d3eb812fd4 | 1360 | dbgPuts(err); |
JMF | 0:24d3eb812fd4 | 1361 | return (false); |
JMF | 0:24d3eb812fd4 | 1362 | } |
JMF | 0:24d3eb812fd4 | 1363 | } |
JMF | 0:24d3eb812fd4 | 1364 | |
JMF | 0:24d3eb812fd4 | 1365 | bool WncController::at_deleteSMSTextFromMem_wnc(char n) |
JMF | 0:24d3eb812fd4 | 1366 | { |
JMF | 0:24d3eb812fd4 | 1367 | string cmdStr, respStr; |
JMF | 0:24d3eb812fd4 | 1368 | // Message is stored in WNC, now send it! |
JMF | 0:24d3eb812fd4 | 1369 | cmdStr = "AT+CMGD="; |
JMF | 0:24d3eb812fd4 | 1370 | cmdStr += n; |
JMF | 0:24d3eb812fd4 | 1371 | cmdStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1372 | dbgPuts("TX: ", false); dbgPutsNoTime(cmdStr.c_str(), false); |
JMF | 0:24d3eb812fd4 | 1373 | AtCmdErr_e r = mdmSendAtCmdRsp(cmdStr.c_str(), m_sCmdTimeoutMs, &respStr); |
JMF | 0:24d3eb812fd4 | 1374 | dbgPuts("RX: ", false); dbgPutsNoTime(respStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1375 | return (r == WNC_AT_CMD_OK); |
JMF | 0:24d3eb812fd4 | 1376 | } |
JMF | 0:24d3eb812fd4 | 1377 | |
JMF | 0:24d3eb812fd4 | 1378 | bool WncController::at_sendSMStextMem_wnc(char n) |
JMF | 0:24d3eb812fd4 | 1379 | { |
JMF | 0:24d3eb812fd4 | 1380 | string cmdStr, respStr; |
JMF | 0:24d3eb812fd4 | 1381 | // Message is stored in WNC, now send it! |
JMF | 0:24d3eb812fd4 | 1382 | cmdStr = "AT+CMSS="; |
JMF | 0:24d3eb812fd4 | 1383 | cmdStr += n; |
JMF | 0:24d3eb812fd4 | 1384 | cmdStr += "\r\n"; |
JMF | 0:24d3eb812fd4 | 1385 | dbgPuts("TX: ", false); dbgPutsNoTime(cmdStr.c_str(), false); |
JMF | 0:24d3eb812fd4 | 1386 | AtCmdErr_e r = mdmSendAtCmdRsp(cmdStr.c_str(), m_sCmdTimeoutMs, &respStr); |
JMF | 0:24d3eb812fd4 | 1387 | dbgPuts("RX: ", false); dbgPutsNoTime(respStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1388 | return (r == WNC_AT_CMD_OK); |
JMF | 0:24d3eb812fd4 | 1389 | } |
JMF | 0:24d3eb812fd4 | 1390 | |
JMF | 0:24d3eb812fd4 | 1391 | bool WncController::at_sendSMStext_wnc(const char * const phoneNum, const char * const text) |
JMF | 0:24d3eb812fd4 | 1392 | { |
JMF | 0:24d3eb812fd4 | 1393 | string respStr; |
JMF | 0:24d3eb812fd4 | 1394 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1395 | size_t l = strlen(text); |
JMF | 0:24d3eb812fd4 | 1396 | |
JMF | 0:24d3eb812fd4 | 1397 | if (l <= MAX_WNC_SMS_LENGTH) |
JMF | 0:24d3eb812fd4 | 1398 | { |
JMF | 0:24d3eb812fd4 | 1399 | // Check to see if the SMS service is available |
JMF | 0:24d3eb812fd4 | 1400 | checkCellLink(); |
JMF | 0:24d3eb812fd4 | 1401 | if (m_sReadyForSMS == true) { |
JMF | 0:24d3eb812fd4 | 1402 | at_send_wnc_cmd("AT+CMGF=1", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1403 | string cmdStr("AT+CMGS=\""); |
JMF | 0:24d3eb812fd4 | 1404 | cmdStr += phoneNum; |
JMF | 0:24d3eb812fd4 | 1405 | cmdStr += "\""; |
JMF | 0:24d3eb812fd4 | 1406 | dbgPuts("TX: ", false); dbgPutsNoTime(cmdStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1407 | cmdStr += "\x0d"; // x0d = <ENTER> |
JMF | 0:24d3eb812fd4 | 1408 | // Send raw command with short timeout (the timeout will fail cause the WNC is not supposed to reply yet! |
JMF | 0:24d3eb812fd4 | 1409 | // And we want a delay before sending the actual text part of the string! |
JMF | 0:24d3eb812fd4 | 1410 | mdmSendAtCmdRsp(cmdStr.c_str(), 300, &respStr, false); // False turns off auto-addition of CR+LF (the WNC wants nothing here) |
JMF | 0:24d3eb812fd4 | 1411 | dbgPuts("RX: ", false); dbgPutsNoTime(respStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1412 | if ((respStr.size() > 0) && (respStr.find("ERROR") == string::npos)) { |
JMF | 0:24d3eb812fd4 | 1413 | // Part 2 of the text, this is the actual text part: |
JMF | 0:24d3eb812fd4 | 1414 | cmdStr = text; |
JMF | 0:24d3eb812fd4 | 1415 | dbgPuts("TX: ", false); dbgPutsNoTime(cmdStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1416 | cmdStr += "\x1A"; // <CTRL>-Z is what tells the WNC the message is complete to send! |
JMF | 0:24d3eb812fd4 | 1417 | AtCmdErr_e r = mdmSendAtCmdRsp(cmdStr.c_str(), 10000, &respStr); |
JMF | 0:24d3eb812fd4 | 1418 | dbgPuts("RX: ", false); dbgPutsNoTime(respStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1419 | if (respStr.size() == 0) |
JMF | 0:24d3eb812fd4 | 1420 | return (false); |
JMF | 0:24d3eb812fd4 | 1421 | else |
JMF | 0:24d3eb812fd4 | 1422 | return (r == WNC_AT_CMD_OK); |
JMF | 0:24d3eb812fd4 | 1423 | } |
JMF | 0:24d3eb812fd4 | 1424 | } |
JMF | 0:24d3eb812fd4 | 1425 | } |
JMF | 0:24d3eb812fd4 | 1426 | |
JMF | 0:24d3eb812fd4 | 1427 | return (false); |
JMF | 0:24d3eb812fd4 | 1428 | } |
JMF | 0:24d3eb812fd4 | 1429 | |
JMF | 0:24d3eb812fd4 | 1430 | bool WncController::saveSMSText(const char * const phoneNum, const char * const text, char * msgIdx) |
JMF | 0:24d3eb812fd4 | 1431 | { |
JMF | 0:24d3eb812fd4 | 1432 | if (at_saveSMStext_wnc(phoneNum, text, msgIdx) == true) |
JMF | 0:24d3eb812fd4 | 1433 | return (true); |
JMF | 0:24d3eb812fd4 | 1434 | else { |
JMF | 0:24d3eb812fd4 | 1435 | dbgPuts("saveSMSTextToMem: failed!\r\n"); |
JMF | 0:24d3eb812fd4 | 1436 | return (false); |
JMF | 0:24d3eb812fd4 | 1437 | } |
JMF | 0:24d3eb812fd4 | 1438 | } |
JMF | 0:24d3eb812fd4 | 1439 | |
JMF | 0:24d3eb812fd4 | 1440 | bool WncController::at_saveSMStext_wnc(const char * const phoneNum, const char * const text, char * msgIdx) |
JMF | 0:24d3eb812fd4 | 1441 | { |
JMF | 0:24d3eb812fd4 | 1442 | string respStr; |
JMF | 0:24d3eb812fd4 | 1443 | size_t l = strlen(text); |
JMF | 0:24d3eb812fd4 | 1444 | |
JMF | 0:24d3eb812fd4 | 1445 | if (l <= MAX_WNC_SMS_LENGTH) |
JMF | 0:24d3eb812fd4 | 1446 | { |
JMF | 0:24d3eb812fd4 | 1447 | // Check to see if the SMS service is available |
JMF | 0:24d3eb812fd4 | 1448 | checkCellLink(); |
JMF | 0:24d3eb812fd4 | 1449 | if (m_sReadyForSMS == true) { |
JMF | 0:24d3eb812fd4 | 1450 | string cmdStr("AT+CMGW=\""); |
JMF | 0:24d3eb812fd4 | 1451 | cmdStr += phoneNum; |
JMF | 0:24d3eb812fd4 | 1452 | cmdStr += "\""; |
JMF | 0:24d3eb812fd4 | 1453 | dbgPuts("TX: ", false); dbgPutsNoTime(cmdStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1454 | cmdStr += "\x0d"; // x0d = <ENTER> |
JMF | 0:24d3eb812fd4 | 1455 | // Send raw command with short timeout (the timeout will fail cause the WNC is not supposed to reply yet! |
JMF | 0:24d3eb812fd4 | 1456 | // And we want a delay before sending the actual text part of the string! |
JMF | 0:24d3eb812fd4 | 1457 | mdmSendAtCmdRsp(cmdStr.c_str(), 300, &respStr, false); // False turns off auto-addition of CR+LF (the WNC wants nothing here) |
JMF | 0:24d3eb812fd4 | 1458 | dbgPuts("RX: ", false); dbgPutsNoTime(respStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1459 | if ((respStr.size() > 0) && (respStr.find("ERROR") == string::npos)) { |
JMF | 0:24d3eb812fd4 | 1460 | // Part 2 of the text, this is the actual text part: |
JMF | 0:24d3eb812fd4 | 1461 | cmdStr = text; |
JMF | 0:24d3eb812fd4 | 1462 | dbgPuts("TX: ", false); dbgPutsNoTime(cmdStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1463 | cmdStr += "\x1A"; // <CTRL>-Z is what tells the WNC the message is complete to save! |
JMF | 0:24d3eb812fd4 | 1464 | mdmSendAtCmdRsp(cmdStr.c_str(), 10000, &respStr); |
JMF | 0:24d3eb812fd4 | 1465 | dbgPuts("RX: ", false); dbgPutsNoTime(respStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1466 | if (respStr.size() > 0) { |
JMF | 0:24d3eb812fd4 | 1467 | // respStr will have the SMS index |
JMF | 0:24d3eb812fd4 | 1468 | size_t pos1 = respStr.find("+CMGW: "); |
JMF | 0:24d3eb812fd4 | 1469 | size_t pos2 = respStr.rfind("OK"); |
JMF | 0:24d3eb812fd4 | 1470 | if (pos1 != string::npos && pos2 != string::npos) { |
JMF | 0:24d3eb812fd4 | 1471 | *msgIdx = *string(respStr.substr(pos1+7, 1)).c_str(); |
JMF | 0:24d3eb812fd4 | 1472 | return (true); |
JMF | 0:24d3eb812fd4 | 1473 | } |
JMF | 0:24d3eb812fd4 | 1474 | else { |
JMF | 0:24d3eb812fd4 | 1475 | *msgIdx = '!'; |
JMF | 0:24d3eb812fd4 | 1476 | } |
JMF | 0:24d3eb812fd4 | 1477 | } |
JMF | 0:24d3eb812fd4 | 1478 | } |
JMF | 0:24d3eb812fd4 | 1479 | } |
JMF | 0:24d3eb812fd4 | 1480 | } |
JMF | 0:24d3eb812fd4 | 1481 | |
JMF | 0:24d3eb812fd4 | 1482 | return (false); |
JMF | 0:24d3eb812fd4 | 1483 | } |
JMF | 0:24d3eb812fd4 | 1484 | |
JMF | 0:24d3eb812fd4 | 1485 | bool WncController::at_readSMSlog_wnc(string ** log) |
JMF | 0:24d3eb812fd4 | 1486 | { |
JMF | 0:24d3eb812fd4 | 1487 | return (at_send_wnc_cmd("AT+CMGL", log, m_sCmdTimeoutMs) == WNC_AT_CMD_OK); |
JMF | 0:24d3eb812fd4 | 1488 | } |
JMF | 0:24d3eb812fd4 | 1489 | |
JMF | 0:24d3eb812fd4 | 1490 | size_t WncController::at_readSMStext_wnc(const char n, const char ** log) |
JMF | 0:24d3eb812fd4 | 1491 | { |
JMF | 0:24d3eb812fd4 | 1492 | static string smsReadTxtStr; |
JMF | 0:24d3eb812fd4 | 1493 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1494 | string cmdStr; |
JMF | 0:24d3eb812fd4 | 1495 | |
JMF | 0:24d3eb812fd4 | 1496 | smsReadTxtStr.erase(); |
JMF | 0:24d3eb812fd4 | 1497 | cmdStr = "AT+CMGR"; |
JMF | 0:24d3eb812fd4 | 1498 | cmdStr += '1'; |
JMF | 0:24d3eb812fd4 | 1499 | if (at_send_wnc_cmd("AT+CMGR", &pRespStr, m_sCmdTimeoutMs) == WNC_AT_CMD_OK) |
JMF | 0:24d3eb812fd4 | 1500 | *log = pRespStr->c_str(); |
JMF | 0:24d3eb812fd4 | 1501 | else |
JMF | 0:24d3eb812fd4 | 1502 | *log = "\0"; |
JMF | 0:24d3eb812fd4 | 1503 | |
JMF | 0:24d3eb812fd4 | 1504 | return (pRespStr->size()); |
JMF | 0:24d3eb812fd4 | 1505 | } |
JMF | 0:24d3eb812fd4 | 1506 | |
JMF | 0:24d3eb812fd4 | 1507 | bool WncController::at_at_wnc(void) |
JMF | 0:24d3eb812fd4 | 1508 | { |
JMF | 0:24d3eb812fd4 | 1509 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1510 | return (WNC_AT_CMD_OK == at_send_wnc_cmd("AT", &pRespStr, WNC_QUICK_CMD_TIMEOUT_MS)); // Heartbeat? |
JMF | 0:24d3eb812fd4 | 1511 | } |
JMF | 0:24d3eb812fd4 | 1512 | |
JMF | 0:24d3eb812fd4 | 1513 | bool WncController::at_init_wnc(bool hardReset) |
JMF | 0:24d3eb812fd4 | 1514 | { |
JMF | 0:24d3eb812fd4 | 1515 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1516 | AtCmdErr_e cmdRes; |
JMF | 0:24d3eb812fd4 | 1517 | |
JMF | 0:24d3eb812fd4 | 1518 | if (hardReset == true) |
JMF | 0:24d3eb812fd4 | 1519 | dbgPuts("Hard Soft Reset!"); |
JMF | 0:24d3eb812fd4 | 1520 | |
JMF | 0:24d3eb812fd4 | 1521 | dbgPuts("Start AT init of WNC:"); |
JMF | 0:24d3eb812fd4 | 1522 | |
JMF | 0:24d3eb812fd4 | 1523 | // Kick it twice to perhaps remove cued responses from an incomplete |
JMF | 0:24d3eb812fd4 | 1524 | // power cycle. |
JMF | 0:24d3eb812fd4 | 1525 | at_send_wnc_cmd("AT", &pRespStr, WNC_QUICK_CMD_TIMEOUT_MS); |
JMF | 0:24d3eb812fd4 | 1526 | at_send_wnc_cmd("AT", &pRespStr, WNC_QUICK_CMD_TIMEOUT_MS); |
JMF | 0:24d3eb812fd4 | 1527 | |
JMF | 0:24d3eb812fd4 | 1528 | // Dump the firmware revision on the debug log: |
JMF | 0:24d3eb812fd4 | 1529 | at_send_wnc_cmd("AT+GMR", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1530 | |
JMF | 0:24d3eb812fd4 | 1531 | m_FirmwareRevision = pRespStr->c_str(); |
JMF | 0:24d3eb812fd4 | 1532 | |
JMF | 0:24d3eb812fd4 | 1533 | // Quick commands below do not need to check cellular connectivity |
JMF | 0:24d3eb812fd4 | 1534 | at_send_wnc_cmd("ATE0", &pRespStr, WNC_QUICK_CMD_TIMEOUT_MS); // Echo Off |
JMF | 0:24d3eb812fd4 | 1535 | at_send_wnc_cmd("AT+CMEE=2", &pRespStr, m_sCmdTimeoutMs); // 2 - verbose error, 1 - numeric error, 0 - just ERROR |
JMF | 0:24d3eb812fd4 | 1536 | |
JMF | 0:24d3eb812fd4 | 1537 | // Setup 3 memory slots in the WNC SIM for SMS usage. |
JMF | 0:24d3eb812fd4 | 1538 | at_send_wnc_cmd("AT+CMGF=1", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1539 | at_send_wnc_cmd("AT+CPMS=\"SM\",\"SM\",\"SM\"", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1540 | |
JMF | 0:24d3eb812fd4 | 1541 | cmdRes = at_send_wnc_cmd("AT", &pRespStr, WNC_QUICK_CMD_TIMEOUT_MS); // Heartbeat? |
JMF | 0:24d3eb812fd4 | 1542 | |
JMF | 0:24d3eb812fd4 | 1543 | // If the simple commands are not working, no chance of more complex. |
JMF | 0:24d3eb812fd4 | 1544 | // I have seen re-trying commands make it worse. |
JMF | 0:24d3eb812fd4 | 1545 | if (cmdRes != WNC_AT_CMD_OK) |
JMF | 0:24d3eb812fd4 | 1546 | return (false); |
JMF | 0:24d3eb812fd4 | 1547 | |
JMF | 0:24d3eb812fd4 | 1548 | // Disable unsolicited RRCSTATE responses. These are supposed to be off |
JMF | 0:24d3eb812fd4 | 1549 | // by default but have been found to be active. |
JMF | 0:24d3eb812fd4 | 1550 | // This problem introduced in: NQ_MPSS_IMA3_v10.58.174043 LTE-M firmware |
JMF | 0:24d3eb812fd4 | 1551 | cmdRes = at_send_wnc_cmd("AT%NOTIFYEV=\"ALL\",0", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1552 | if (cmdRes != WNC_AT_CMD_OK) |
JMF | 0:24d3eb812fd4 | 1553 | return (false); |
JMF | 0:24d3eb812fd4 | 1554 | |
JMF | 0:24d3eb812fd4 | 1555 | cmdRes = at_send_wnc_cmd("AT@INTERNET=1", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1556 | if (cmdRes != WNC_AT_CMD_OK) |
JMF | 0:24d3eb812fd4 | 1557 | return (false); |
JMF | 0:24d3eb812fd4 | 1558 | |
JMF | 0:24d3eb812fd4 | 1559 | cmdRes = at_send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1560 | if (cmdRes != WNC_AT_CMD_OK) |
JMF | 0:24d3eb812fd4 | 1561 | return (false); |
JMF | 0:24d3eb812fd4 | 1562 | |
JMF | 0:24d3eb812fd4 | 1563 | dbgPuts("SUCCESS: AT init of WNC!"); |
JMF | 0:24d3eb812fd4 | 1564 | |
JMF | 0:24d3eb812fd4 | 1565 | return (true); |
JMF | 0:24d3eb812fd4 | 1566 | } |
JMF | 0:24d3eb812fd4 | 1567 | |
JMF | 0:24d3eb812fd4 | 1568 | int16_t WncController::at_sockopen_wnc(const char * const ip, uint16_t port, uint16_t numSock, bool tcp, uint16_t timeOutSec) |
JMF | 0:24d3eb812fd4 | 1569 | { |
JMF | 0:24d3eb812fd4 | 1570 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1571 | string cmd_str("AT@SOCKCREAT="); |
JMF | 0:24d3eb812fd4 | 1572 | AtCmdErr_e res; |
JMF | 0:24d3eb812fd4 | 1573 | |
JMF | 0:24d3eb812fd4 | 1574 | if (tcp) cmd_str += "1"; // TCP |
JMF | 0:24d3eb812fd4 | 1575 | else cmd_str += "2"; // else UDP |
JMF | 0:24d3eb812fd4 | 1576 | |
JMF | 0:24d3eb812fd4 | 1577 | cmd_str += ",0"; |
JMF | 0:24d3eb812fd4 | 1578 | res = sendWncCmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1579 | if (res == WNC_AT_CMD_OK && pRespStr->size() > 0) |
JMF | 0:24d3eb812fd4 | 1580 | { |
JMF | 0:24d3eb812fd4 | 1581 | size_t pos1 = pRespStr->find("T:"); |
JMF | 0:24d3eb812fd4 | 1582 | size_t pos2 = pRespStr->rfind("OK"); |
JMF | 0:24d3eb812fd4 | 1583 | if ((pos1 != string::npos) && (pos2 != string::npos)) { |
JMF | 0:24d3eb812fd4 | 1584 | size_t numLen = pos2 - (pos1 + 2); |
JMF | 0:24d3eb812fd4 | 1585 | string sockStr = pRespStr->substr(pos1 + 2, numLen); |
JMF | 0:24d3eb812fd4 | 1586 | cmd_str = "AT@SOCKCONN="; |
JMF | 0:24d3eb812fd4 | 1587 | cmd_str += sockStr; |
JMF | 0:24d3eb812fd4 | 1588 | cmd_str += ",\""; |
JMF | 0:24d3eb812fd4 | 1589 | cmd_str += ip; |
JMF | 0:24d3eb812fd4 | 1590 | cmd_str += "\","; |
JMF | 0:24d3eb812fd4 | 1591 | cmd_str += _to_string(port); |
JMF | 0:24d3eb812fd4 | 1592 | cmd_str += ","; |
JMF | 0:24d3eb812fd4 | 1593 | if (timeOutSec < 30) |
JMF | 0:24d3eb812fd4 | 1594 | timeOutSec = 30; |
JMF | 0:24d3eb812fd4 | 1595 | else if (timeOutSec > 360) |
JMF | 0:24d3eb812fd4 | 1596 | timeOutSec = 360; |
JMF | 0:24d3eb812fd4 | 1597 | cmd_str += _to_string(timeOutSec); |
JMF | 0:24d3eb812fd4 | 1598 | res = sendWncCmd(cmd_str.c_str(), &pRespStr, 1000 * timeOutSec + 1000); |
JMF | 0:24d3eb812fd4 | 1599 | if (m_sMoreDebugEnabled) { |
JMF | 0:24d3eb812fd4 | 1600 | at_send_wnc_cmd("AT@SOCKCREAT?", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1601 | at_send_wnc_cmd("AT@SOCKCONN?", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1602 | } |
JMF | 0:24d3eb812fd4 | 1603 | return (strtol(sockStr.c_str(), NULL, 10)); |
JMF | 0:24d3eb812fd4 | 1604 | } |
JMF | 0:24d3eb812fd4 | 1605 | else { |
JMF | 0:24d3eb812fd4 | 1606 | dbgPuts("Invalid sockcreat response!"); |
JMF | 0:24d3eb812fd4 | 1607 | return (0); |
JMF | 0:24d3eb812fd4 | 1608 | } |
JMF | 0:24d3eb812fd4 | 1609 | } |
JMF | 0:24d3eb812fd4 | 1610 | else |
JMF | 0:24d3eb812fd4 | 1611 | return (0); |
JMF | 0:24d3eb812fd4 | 1612 | } |
JMF | 0:24d3eb812fd4 | 1613 | |
JMF | 0:24d3eb812fd4 | 1614 | bool WncController::at_sockclose_wnc(uint16_t numSock) |
JMF | 0:24d3eb812fd4 | 1615 | { |
JMF | 0:24d3eb812fd4 | 1616 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1617 | string cmd_str("AT@SOCKCLOSE="); |
JMF | 0:24d3eb812fd4 | 1618 | |
JMF | 0:24d3eb812fd4 | 1619 | cmd_str += _to_string(numSock); |
JMF | 0:24d3eb812fd4 | 1620 | |
JMF | 0:24d3eb812fd4 | 1621 | // Don't check the cell status to close the socket |
JMF | 0:24d3eb812fd4 | 1622 | AtCmdErr_e res = at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1623 | |
JMF | 0:24d3eb812fd4 | 1624 | if ((res != WNC_AT_CMD_TIMEOUT) && (res != WNC_AT_CMD_OK)) { |
JMF | 0:24d3eb812fd4 | 1625 | for (unsigned i = 0; i < WNC_SOCK_CLOSE_RETRY_CNT; i++) { |
JMF | 0:24d3eb812fd4 | 1626 | res = at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1627 | if ((res == WNC_AT_CMD_TIMEOUT) || (res == WNC_AT_CMD_OK)) |
JMF | 0:24d3eb812fd4 | 1628 | break; |
JMF | 0:24d3eb812fd4 | 1629 | } |
JMF | 0:24d3eb812fd4 | 1630 | } |
JMF | 0:24d3eb812fd4 | 1631 | |
JMF | 0:24d3eb812fd4 | 1632 | return (res == WNC_AT_CMD_OK); |
JMF | 0:24d3eb812fd4 | 1633 | } |
JMF | 0:24d3eb812fd4 | 1634 | |
JMF | 0:24d3eb812fd4 | 1635 | bool WncController::at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 0:24d3eb812fd4 | 1636 | { |
JMF | 0:24d3eb812fd4 | 1637 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1638 | string str(s); |
JMF | 0:24d3eb812fd4 | 1639 | AtCmdErr_e r; |
JMF | 0:24d3eb812fd4 | 1640 | |
JMF | 0:24d3eb812fd4 | 1641 | ipStr->erase(); // Clear out string until resolved! |
JMF | 0:24d3eb812fd4 | 1642 | str = "AT@DNSRESVDON=\"" + str; |
JMF | 0:24d3eb812fd4 | 1643 | str += "\""; |
JMF | 0:24d3eb812fd4 | 1644 | r = sendWncCmd(str.c_str(), &pRespStr, WNC_DNS_RESOLVE_WAIT_MS); |
JMF | 0:24d3eb812fd4 | 1645 | if (r == WNC_AT_CMD_OK && pRespStr->size() > 0) { |
JMF | 0:24d3eb812fd4 | 1646 | size_t pos_start = pRespStr->find("ON:\""); |
JMF | 0:24d3eb812fd4 | 1647 | size_t pos_end = pRespStr->find("\"", (pos_start + 4)); |
JMF | 0:24d3eb812fd4 | 1648 | if ((pos_start != string::npos) && (pos_end != string::npos)) { |
JMF | 0:24d3eb812fd4 | 1649 | pos_start += 4; |
JMF | 0:24d3eb812fd4 | 1650 | pos_end -= 1; |
JMF | 0:24d3eb812fd4 | 1651 | |
JMF | 0:24d3eb812fd4 | 1652 | if (pos_end > pos_start) { |
JMF | 0:24d3eb812fd4 | 1653 | // Make a copy for use later (the source string is re-used) |
JMF | 0:24d3eb812fd4 | 1654 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 0:24d3eb812fd4 | 1655 | return (true); |
JMF | 0:24d3eb812fd4 | 1656 | } |
JMF | 0:24d3eb812fd4 | 1657 | } |
JMF | 0:24d3eb812fd4 | 1658 | } |
JMF | 0:24d3eb812fd4 | 1659 | |
JMF | 0:24d3eb812fd4 | 1660 | *ipStr = INVALID_IP_STR; |
JMF | 0:24d3eb812fd4 | 1661 | |
JMF | 0:24d3eb812fd4 | 1662 | return (false); |
JMF | 0:24d3eb812fd4 | 1663 | } |
JMF | 0:24d3eb812fd4 | 1664 | |
JMF | 0:24d3eb812fd4 | 1665 | bool WncController::waitForPowerOnModemToRespond(uint8_t timeoutSecs) |
JMF | 0:24d3eb812fd4 | 1666 | { |
JMF | 0:24d3eb812fd4 | 1667 | // Now, give the modem x seconds to start responding by |
JMF | 0:24d3eb812fd4 | 1668 | // sending simple 'AT' commands to modem once per second. |
JMF | 0:24d3eb812fd4 | 1669 | if (timeoutSecs > 0) { |
JMF | 0:24d3eb812fd4 | 1670 | do { |
JMF | 0:24d3eb812fd4 | 1671 | timeoutSecs--; |
JMF | 0:24d3eb812fd4 | 1672 | dbgPutsNoTime("\rWaiting ", false); dbgPutsNoTime(_to_string(timeoutSecs), false); |
JMF | 0:24d3eb812fd4 | 1673 | dbgPutsNoTime(" ", false); |
JMF | 0:24d3eb812fd4 | 1674 | AtCmdErr_e rc = mdmSendAtCmdRsp("AT", 500, &m_sWncStr); |
JMF | 0:24d3eb812fd4 | 1675 | if (rc == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1676 | dbgPutsNoTime(""); // CR LF |
JMF | 0:24d3eb812fd4 | 1677 | return true; //timer.read(); |
JMF | 0:24d3eb812fd4 | 1678 | } |
JMF | 0:24d3eb812fd4 | 1679 | waitMs(500); |
JMF | 0:24d3eb812fd4 | 1680 | } |
JMF | 0:24d3eb812fd4 | 1681 | while (timeoutSecs > 0); |
JMF | 0:24d3eb812fd4 | 1682 | dbgPutsNoTime(""); // CR LF |
JMF | 0:24d3eb812fd4 | 1683 | } |
JMF | 0:24d3eb812fd4 | 1684 | |
JMF | 0:24d3eb812fd4 | 1685 | return (false); |
JMF | 0:24d3eb812fd4 | 1686 | } |
JMF | 0:24d3eb812fd4 | 1687 | |
JMF | 0:24d3eb812fd4 | 1688 | WncController::AtCmdErr_e WncController::at_sockwrite_wnc(const uint8_t * s, uint16_t n, uint16_t numSock, bool isTcp) |
JMF | 0:24d3eb812fd4 | 1689 | { |
JMF | 0:24d3eb812fd4 | 1690 | AtCmdErr_e result; |
JMF | 0:24d3eb812fd4 | 1691 | |
JMF | 0:24d3eb812fd4 | 1692 | if ((n > 0) && (n <= MAX_WNC_WRITE_BYTES)) { |
JMF | 0:24d3eb812fd4 | 1693 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1694 | const char * num2str; |
JMF | 0:24d3eb812fd4 | 1695 | string cmd_str; |
JMF | 0:24d3eb812fd4 | 1696 | |
JMF | 0:24d3eb812fd4 | 1697 | if (isTcp == true) |
JMF | 0:24d3eb812fd4 | 1698 | cmd_str="AT@SOCKWRITE="; |
JMF | 0:24d3eb812fd4 | 1699 | else |
JMF | 0:24d3eb812fd4 | 1700 | cmd_str="AT@SOCKWRITE="; // "AT@SOCKSEND="; |
JMF | 0:24d3eb812fd4 | 1701 | |
JMF | 0:24d3eb812fd4 | 1702 | cmd_str += _to_string(numSock); |
JMF | 0:24d3eb812fd4 | 1703 | cmd_str += ","; |
JMF | 0:24d3eb812fd4 | 1704 | cmd_str += _to_string(n); |
JMF | 0:24d3eb812fd4 | 1705 | cmd_str += ",\""; |
JMF | 0:24d3eb812fd4 | 1706 | while(n > 0) { |
JMF | 0:24d3eb812fd4 | 1707 | n--; |
JMF | 0:24d3eb812fd4 | 1708 | num2str = _to_hex_string(*s++); |
JMF | 0:24d3eb812fd4 | 1709 | // Always 2-digit ascii hex: |
JMF | 0:24d3eb812fd4 | 1710 | if (num2str[1] == '\0') |
JMF | 0:24d3eb812fd4 | 1711 | cmd_str += '0'; |
JMF | 0:24d3eb812fd4 | 1712 | cmd_str += num2str; |
JMF | 0:24d3eb812fd4 | 1713 | } |
JMF | 0:24d3eb812fd4 | 1714 | cmd_str += "\""; |
JMF | 0:24d3eb812fd4 | 1715 | result = sendWncCmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1716 | } |
JMF | 0:24d3eb812fd4 | 1717 | else { |
JMF | 0:24d3eb812fd4 | 1718 | dbgPuts("sockwrite Err, string len bad!"); |
JMF | 0:24d3eb812fd4 | 1719 | result = WNC_AT_CMD_ERR; |
JMF | 0:24d3eb812fd4 | 1720 | } |
JMF | 0:24d3eb812fd4 | 1721 | |
JMF | 0:24d3eb812fd4 | 1722 | return (result); |
JMF | 0:24d3eb812fd4 | 1723 | } |
JMF | 0:24d3eb812fd4 | 1724 | |
JMF | 0:24d3eb812fd4 | 1725 | WncController::AtCmdErr_e WncController::at_sockread_wnc(string * pS, uint16_t numSock, bool isTcp) |
JMF | 0:24d3eb812fd4 | 1726 | { |
JMF | 0:24d3eb812fd4 | 1727 | AtCmdErr_e result = WNC_AT_CMD_OK; |
JMF | 0:24d3eb812fd4 | 1728 | |
JMF | 0:24d3eb812fd4 | 1729 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1730 | string cmd_str; |
JMF | 0:24d3eb812fd4 | 1731 | size_t pos_start=0, pos_end=0; |
JMF | 0:24d3eb812fd4 | 1732 | int i; |
JMF | 0:24d3eb812fd4 | 1733 | |
JMF | 0:24d3eb812fd4 | 1734 | pS->erase(); // Start with a fresh string |
JMF | 0:24d3eb812fd4 | 1735 | |
JMF | 0:24d3eb812fd4 | 1736 | if (isTcp == true) |
JMF | 0:24d3eb812fd4 | 1737 | cmd_str="AT@SOCKREAD="; |
JMF | 0:24d3eb812fd4 | 1738 | else |
JMF | 0:24d3eb812fd4 | 1739 | cmd_str="AT@SOCKREAD="; // "AT@SOCKRECV="; |
JMF | 0:24d3eb812fd4 | 1740 | |
JMF | 0:24d3eb812fd4 | 1741 | cmd_str += _to_string(numSock); |
JMF | 0:24d3eb812fd4 | 1742 | cmd_str += ","; |
JMF | 0:24d3eb812fd4 | 1743 | cmd_str += _to_string(MAX_WNC_READ_BYTES); |
JMF | 0:24d3eb812fd4 | 1744 | |
JMF | 0:24d3eb812fd4 | 1745 | // Experimental: read should not need to check cell net status |
JMF | 0:24d3eb812fd4 | 1746 | result = at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1747 | if (result == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1748 | if (pRespStr->size() > 0) { |
JMF | 0:24d3eb812fd4 | 1749 | pos_start = pRespStr->find("\""); |
JMF | 0:24d3eb812fd4 | 1750 | pos_end = pRespStr->rfind("\""); |
JMF | 0:24d3eb812fd4 | 1751 | // Make sure search finds what it's looking for! |
JMF | 0:24d3eb812fd4 | 1752 | if (pos_start != string::npos && pos_end != string::npos) { |
JMF | 0:24d3eb812fd4 | 1753 | pos_start++; |
JMF | 0:24d3eb812fd4 | 1754 | i = pos_end - pos_start; // Num hex chars, 2 per byte |
JMF | 0:24d3eb812fd4 | 1755 | } |
JMF | 0:24d3eb812fd4 | 1756 | else |
JMF | 0:24d3eb812fd4 | 1757 | i = 0; |
JMF | 0:24d3eb812fd4 | 1758 | } |
JMF | 0:24d3eb812fd4 | 1759 | else |
JMF | 0:24d3eb812fd4 | 1760 | i = 0; |
JMF | 0:24d3eb812fd4 | 1761 | |
JMF | 0:24d3eb812fd4 | 1762 | if ((i < 0) || ((i % 2) == 1)) |
JMF | 0:24d3eb812fd4 | 1763 | dbgPuts("Invalid READ string!"); |
JMF | 0:24d3eb812fd4 | 1764 | |
JMF | 0:24d3eb812fd4 | 1765 | if (i > 2*MAX_WNC_READ_BYTES) { |
JMF | 0:24d3eb812fd4 | 1766 | i = 2*MAX_WNC_READ_BYTES; |
JMF | 0:24d3eb812fd4 | 1767 | dbgPuts("DANGER WNC read data does not match length!"); |
JMF | 0:24d3eb812fd4 | 1768 | } |
JMF | 0:24d3eb812fd4 | 1769 | |
JMF | 0:24d3eb812fd4 | 1770 | // If data, convert the hex string into byte values |
JMF | 0:24d3eb812fd4 | 1771 | while (i > 0) { |
JMF | 0:24d3eb812fd4 | 1772 | i -= 2; |
JMF | 0:24d3eb812fd4 | 1773 | *pS += (uint8_t)strtol(pRespStr->substr(pos_start, 2).c_str(), NULL, 16); |
JMF | 0:24d3eb812fd4 | 1774 | pos_start += 2; |
JMF | 0:24d3eb812fd4 | 1775 | } |
JMF | 0:24d3eb812fd4 | 1776 | } |
JMF | 0:24d3eb812fd4 | 1777 | |
JMF | 0:24d3eb812fd4 | 1778 | return (result); |
JMF | 0:24d3eb812fd4 | 1779 | } |
JMF | 0:24d3eb812fd4 | 1780 | |
JMF | 0:24d3eb812fd4 | 1781 | WncController::AtCmdErr_e WncController::at_sockread_wnc(uint8_t * pS, uint16_t * numRead, uint16_t n, uint16_t numSock, bool isTcp) |
JMF | 0:24d3eb812fd4 | 1782 | { |
JMF | 0:24d3eb812fd4 | 1783 | AtCmdErr_e result = WNC_AT_CMD_OK; |
JMF | 0:24d3eb812fd4 | 1784 | *numRead = 0; |
JMF | 0:24d3eb812fd4 | 1785 | |
JMF | 0:24d3eb812fd4 | 1786 | if ((n > 0) && (n <= MAX_WNC_READ_BYTES)) { |
JMF | 0:24d3eb812fd4 | 1787 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1788 | string cmd_str; |
JMF | 0:24d3eb812fd4 | 1789 | size_t pos_start=0, pos_end=0; |
JMF | 0:24d3eb812fd4 | 1790 | int i; |
JMF | 0:24d3eb812fd4 | 1791 | |
JMF | 0:24d3eb812fd4 | 1792 | if (isTcp == true) |
JMF | 0:24d3eb812fd4 | 1793 | cmd_str="AT@SOCKREAD="; |
JMF | 0:24d3eb812fd4 | 1794 | else |
JMF | 0:24d3eb812fd4 | 1795 | cmd_str="AT@SOCKREAD="; // "AT@SOCKRECV="; |
JMF | 0:24d3eb812fd4 | 1796 | |
JMF | 0:24d3eb812fd4 | 1797 | cmd_str += _to_string(numSock); |
JMF | 0:24d3eb812fd4 | 1798 | cmd_str += ","; |
JMF | 0:24d3eb812fd4 | 1799 | cmd_str += _to_string(n); |
JMF | 0:24d3eb812fd4 | 1800 | |
JMF | 0:24d3eb812fd4 | 1801 | // Experimental: read should not need to check cell net status |
JMF | 0:24d3eb812fd4 | 1802 | result = at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1803 | if (result == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1804 | if (pRespStr->size() > 0) { |
JMF | 0:24d3eb812fd4 | 1805 | pos_start = pRespStr->find("\""); |
JMF | 0:24d3eb812fd4 | 1806 | pos_end = pRespStr->rfind("\""); |
JMF | 0:24d3eb812fd4 | 1807 | // Make sure search finds what it's looking for! |
JMF | 0:24d3eb812fd4 | 1808 | if (pos_start != string::npos && pos_end != string::npos) { |
JMF | 0:24d3eb812fd4 | 1809 | pos_start++; |
JMF | 0:24d3eb812fd4 | 1810 | i = pos_end - pos_start; // Num hex chars, 2 per byte |
JMF | 0:24d3eb812fd4 | 1811 | } |
JMF | 0:24d3eb812fd4 | 1812 | else |
JMF | 0:24d3eb812fd4 | 1813 | i = 0; |
JMF | 0:24d3eb812fd4 | 1814 | } |
JMF | 0:24d3eb812fd4 | 1815 | else |
JMF | 0:24d3eb812fd4 | 1816 | i = 0; |
JMF | 0:24d3eb812fd4 | 1817 | |
JMF | 0:24d3eb812fd4 | 1818 | if ((i < 0) || ((i % 2) == 1)) |
JMF | 0:24d3eb812fd4 | 1819 | dbgPuts("Invalid READ string!"); |
JMF | 0:24d3eb812fd4 | 1820 | |
JMF | 0:24d3eb812fd4 | 1821 | if (i > 2*n) { |
JMF | 0:24d3eb812fd4 | 1822 | // Bound the ill formated WNC read string! |
JMF | 0:24d3eb812fd4 | 1823 | i = 2*n; |
JMF | 0:24d3eb812fd4 | 1824 | dbgPuts("TRUNCATING read data!"); |
JMF | 0:24d3eb812fd4 | 1825 | } |
JMF | 0:24d3eb812fd4 | 1826 | |
JMF | 0:24d3eb812fd4 | 1827 | // If data, convert the hex string into byte values |
JMF | 0:24d3eb812fd4 | 1828 | i /= 2; |
JMF | 0:24d3eb812fd4 | 1829 | *numRead = i; |
JMF | 0:24d3eb812fd4 | 1830 | while (i > 0) { |
JMF | 0:24d3eb812fd4 | 1831 | i--; |
JMF | 0:24d3eb812fd4 | 1832 | *pS++ = (uint8_t)strtol(pRespStr->substr(pos_start, 2).c_str(), NULL, 16); |
JMF | 0:24d3eb812fd4 | 1833 | pos_start += 2; |
JMF | 0:24d3eb812fd4 | 1834 | } |
JMF | 0:24d3eb812fd4 | 1835 | } |
JMF | 0:24d3eb812fd4 | 1836 | } |
JMF | 0:24d3eb812fd4 | 1837 | else { |
JMF | 0:24d3eb812fd4 | 1838 | dbgPuts("sockread Err, to many to read!"); |
JMF | 0:24d3eb812fd4 | 1839 | result = WNC_AT_CMD_ERR; |
JMF | 0:24d3eb812fd4 | 1840 | } |
JMF | 0:24d3eb812fd4 | 1841 | |
JMF | 0:24d3eb812fd4 | 1842 | return (result); |
JMF | 0:24d3eb812fd4 | 1843 | } |
JMF | 0:24d3eb812fd4 | 1844 | |
JMF | 0:24d3eb812fd4 | 1845 | bool WncController::at_reinitialize_mdm(void) |
JMF | 0:24d3eb812fd4 | 1846 | { |
JMF | 0:24d3eb812fd4 | 1847 | // Atempt to re-register |
JMF | 0:24d3eb812fd4 | 1848 | // string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1849 | // dbgPuts("Force re-register!"); |
JMF | 0:24d3eb812fd4 | 1850 | // at_send_wnc_cmd("AT+CFUN=0,0", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1851 | // waitMs(31000); |
JMF | 0:24d3eb812fd4 | 1852 | // at_send_wnc_cmd("AT+CFUN=1,0", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1853 | // waitMs(31000); |
JMF | 0:24d3eb812fd4 | 1854 | |
JMF | 0:24d3eb812fd4 | 1855 | // Initialize the modem |
JMF | 0:24d3eb812fd4 | 1856 | dbgPuts("Modem RE-initializing with SOFT Reset..."); |
JMF | 0:24d3eb812fd4 | 1857 | |
JMF | 0:24d3eb812fd4 | 1858 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1859 | at_send_wnc_cmd("AT@DMREBOOT", &pRespStr, m_sCmdTimeoutMs); |
JMF | 0:24d3eb812fd4 | 1860 | waitMs(5000); |
JMF | 0:24d3eb812fd4 | 1861 | |
JMF | 0:24d3eb812fd4 | 1862 | // Now, give the modem time to start responding by |
JMF | 0:24d3eb812fd4 | 1863 | // sending simple 'AT' commands to the modem once per second. |
JMF | 0:24d3eb812fd4 | 1864 | int timeoutSecs = WNC_REINIT_MAX_TIME_MS; |
JMF | 0:24d3eb812fd4 | 1865 | do { |
JMF | 0:24d3eb812fd4 | 1866 | dbgPuts("\rWaiting ", false); dbgPutsNoTime(_to_string(timeoutSecs), false); |
JMF | 0:24d3eb812fd4 | 1867 | AtCmdErr_e rc = mdmSendAtCmdRsp("AT", 500, &m_sWncStr); |
JMF | 0:24d3eb812fd4 | 1868 | if (rc == WNC_AT_CMD_OK) { |
JMF | 0:24d3eb812fd4 | 1869 | dbgPutsNoTime(""); // CR LF |
JMF | 0:24d3eb812fd4 | 1870 | break; |
JMF | 0:24d3eb812fd4 | 1871 | } |
JMF | 0:24d3eb812fd4 | 1872 | waitMs(500); |
JMF | 0:24d3eb812fd4 | 1873 | timeoutSecs--; |
JMF | 0:24d3eb812fd4 | 1874 | } |
JMF | 0:24d3eb812fd4 | 1875 | while (timeoutSecs > 0); |
JMF | 0:24d3eb812fd4 | 1876 | |
JMF | 0:24d3eb812fd4 | 1877 | if (timeoutSecs <= 0) |
JMF | 0:24d3eb812fd4 | 1878 | dbgPuts("\r\nModem RE-init FAILED!"); |
JMF | 0:24d3eb812fd4 | 1879 | else |
JMF | 0:24d3eb812fd4 | 1880 | dbgPuts("\r\nModem RE-init complete!"); |
JMF | 0:24d3eb812fd4 | 1881 | |
JMF | 0:24d3eb812fd4 | 1882 | return (timeoutSecs > 0); |
JMF | 0:24d3eb812fd4 | 1883 | } |
JMF | 0:24d3eb812fd4 | 1884 | |
JMF | 0:24d3eb812fd4 | 1885 | WncController::AtCmdErr_e WncController::mdmSendAtCmdRsp(const char *cmd, int timeout_ms, string * rsp, bool crLf) |
JMF | 0:24d3eb812fd4 | 1886 | { |
JMF | 0:24d3eb812fd4 | 1887 | rsp->erase(); // Clean up from possible prior cmd response |
JMF | 0:24d3eb812fd4 | 1888 | |
JMF | 0:24d3eb812fd4 | 1889 | // Don't bother the WNC if user hasn't turned it on. |
JMF | 0:24d3eb812fd4 | 1890 | if (m_sState == WNC_OFF) |
JMF | 0:24d3eb812fd4 | 1891 | return (WNC_AT_CMD_WNC_NOT_ON); |
JMF | 0:24d3eb812fd4 | 1892 | |
JMF | 0:24d3eb812fd4 | 1893 | size_t n = strlen(cmd); |
JMF | 0:24d3eb812fd4 | 1894 | |
JMF | 0:24d3eb812fd4 | 1895 | // Wait per WNC advise |
JMF | 0:24d3eb812fd4 | 1896 | waitMs(WNC_WAIT_FOR_AT_CMD_MS); |
JMF | 0:24d3eb812fd4 | 1897 | |
JMF | 0:24d3eb812fd4 | 1898 | if (cmd && n > 0) { |
JMF | 0:24d3eb812fd4 | 1899 | sendCmd(cmd, crLf); |
JMF | 0:24d3eb812fd4 | 1900 | // sendCmd(cmd, n, 1000, crLf); // 3rd arg is micro seconds between chars sent |
JMF | 0:24d3eb812fd4 | 1901 | } |
JMF | 0:24d3eb812fd4 | 1902 | |
JMF | 0:24d3eb812fd4 | 1903 | startTimerA(); |
JMF | 0:24d3eb812fd4 | 1904 | while (getTimerTicksA_mS() < timeout_ms) { |
JMF | 0:24d3eb812fd4 | 1905 | n = mdmGetline(rsp, timeout_ms - getTimerTicksA_mS()); |
JMF | 0:24d3eb812fd4 | 1906 | |
JMF | 0:24d3eb812fd4 | 1907 | if (n == 0) |
JMF | 0:24d3eb812fd4 | 1908 | continue; |
JMF | 0:24d3eb812fd4 | 1909 | |
JMF | 0:24d3eb812fd4 | 1910 | if (rsp->rfind("OK") != string::npos) { |
JMF | 0:24d3eb812fd4 | 1911 | stopTimerA(); |
JMF | 0:24d3eb812fd4 | 1912 | return (WNC_AT_CMD_OK); |
JMF | 0:24d3eb812fd4 | 1913 | } |
JMF | 0:24d3eb812fd4 | 1914 | |
JMF | 0:24d3eb812fd4 | 1915 | if (rsp->rfind("+CME ERROR") != string::npos) { |
JMF | 0:24d3eb812fd4 | 1916 | stopTimerA(); |
JMF | 0:24d3eb812fd4 | 1917 | return (WNC_AT_CMD_ERRCME); |
JMF | 0:24d3eb812fd4 | 1918 | } |
JMF | 0:24d3eb812fd4 | 1919 | |
JMF | 0:24d3eb812fd4 | 1920 | if (rsp->rfind("@EXTERR") != string::npos) { |
JMF | 0:24d3eb812fd4 | 1921 | stopTimerA(); |
JMF | 0:24d3eb812fd4 | 1922 | return (WNC_AT_CMD_ERREXT); |
JMF | 0:24d3eb812fd4 | 1923 | } |
JMF | 0:24d3eb812fd4 | 1924 | |
JMF | 0:24d3eb812fd4 | 1925 | if (rsp->rfind("ERROR") != string::npos) { |
JMF | 0:24d3eb812fd4 | 1926 | stopTimerA(); |
JMF | 0:24d3eb812fd4 | 1927 | return (WNC_AT_CMD_ERR); |
JMF | 0:24d3eb812fd4 | 1928 | } |
JMF | 0:24d3eb812fd4 | 1929 | } |
JMF | 0:24d3eb812fd4 | 1930 | stopTimerA(); |
JMF | 0:24d3eb812fd4 | 1931 | |
JMF | 0:24d3eb812fd4 | 1932 | return (WNC_AT_CMD_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 1933 | } |
JMF | 0:24d3eb812fd4 | 1934 | |
JMF | 0:24d3eb812fd4 | 1935 | bool WncController::at_setapn_wnc(const char * const apnStr) |
JMF | 0:24d3eb812fd4 | 1936 | { |
JMF | 0:24d3eb812fd4 | 1937 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1938 | |
JMF | 0:24d3eb812fd4 | 1939 | string cmd_str("AT%PDNSET=1,"); |
JMF | 0:24d3eb812fd4 | 1940 | cmd_str += apnStr; |
JMF | 0:24d3eb812fd4 | 1941 | cmd_str += ",IP"; |
JMF | 0:24d3eb812fd4 | 1942 | if (WNC_AT_CMD_OK == at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_APNSET_TIMEOUT_MS)) // Set APN, cmd seems to take a little longer sometimes |
JMF | 0:24d3eb812fd4 | 1943 | return (true); |
JMF | 0:24d3eb812fd4 | 1944 | else |
JMF | 0:24d3eb812fd4 | 1945 | return (false); |
JMF | 0:24d3eb812fd4 | 1946 | } |
JMF | 0:24d3eb812fd4 | 1947 | |
JMF | 0:24d3eb812fd4 | 1948 | bool WncController::at_getrssiber_wnc(int16_t * dBm, int16_t * ber) |
JMF | 0:24d3eb812fd4 | 1949 | { |
JMF | 0:24d3eb812fd4 | 1950 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 1951 | AtCmdErr_e cmdRes; |
JMF | 0:24d3eb812fd4 | 1952 | cmdRes = at_send_wnc_cmd("AT+CSQ", &pRespStr, m_sCmdTimeoutMs); // Check RSSI,BER |
JMF | 0:24d3eb812fd4 | 1953 | if (cmdRes != WNC_AT_CMD_OK) |
JMF | 0:24d3eb812fd4 | 1954 | return (false); |
JMF | 0:24d3eb812fd4 | 1955 | |
JMF | 0:24d3eb812fd4 | 1956 | if (pRespStr->size() == 0) { |
JMF | 0:24d3eb812fd4 | 1957 | dbgPuts("Strange RSSI result!"); |
JMF | 0:24d3eb812fd4 | 1958 | return (false); |
JMF | 0:24d3eb812fd4 | 1959 | } |
JMF | 0:24d3eb812fd4 | 1960 | else { |
JMF | 0:24d3eb812fd4 | 1961 | size_t pos1 = pRespStr->find("SQ:"); |
JMF | 0:24d3eb812fd4 | 1962 | size_t pos2 = pRespStr->rfind(","); |
JMF | 0:24d3eb812fd4 | 1963 | // Sanity check |
JMF | 0:24d3eb812fd4 | 1964 | if ((pos1 != string::npos) && (pos2 != string::npos) && (pos2 > pos1)) { |
JMF | 0:24d3eb812fd4 | 1965 | string subStr = pRespStr->substr(pos1 + 4, pos2 - pos1 ); |
JMF | 0:24d3eb812fd4 | 1966 | int rawRssi = atoi(subStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1967 | |
JMF | 0:24d3eb812fd4 | 1968 | // Convert WNC RSSI into dBm range: |
JMF | 0:24d3eb812fd4 | 1969 | // 0 - -113 dBm |
JMF | 0:24d3eb812fd4 | 1970 | // 1 - -111 dBm |
JMF | 0:24d3eb812fd4 | 1971 | // 2..30 - -109 to -53 dBm |
JMF | 0:24d3eb812fd4 | 1972 | // 31 - -51dBm or > |
JMF | 0:24d3eb812fd4 | 1973 | // 99 - not known or not detectable |
JMF | 0:24d3eb812fd4 | 1974 | if (rawRssi == 99) |
JMF | 0:24d3eb812fd4 | 1975 | *dBm = -199; |
JMF | 0:24d3eb812fd4 | 1976 | else if (rawRssi == 0) |
JMF | 0:24d3eb812fd4 | 1977 | *dBm = -113; |
JMF | 0:24d3eb812fd4 | 1978 | else if (rawRssi == 1) |
JMF | 0:24d3eb812fd4 | 1979 | *dBm = -111; |
JMF | 0:24d3eb812fd4 | 1980 | else if (rawRssi == 31) |
JMF | 0:24d3eb812fd4 | 1981 | *dBm = -51; |
JMF | 0:24d3eb812fd4 | 1982 | else if (rawRssi >= 2 && rawRssi <= 30) |
JMF | 0:24d3eb812fd4 | 1983 | *dBm = -113 + 2 * rawRssi; |
JMF | 0:24d3eb812fd4 | 1984 | else { |
JMF | 0:24d3eb812fd4 | 1985 | dbgPuts("Invalid RSSI!"); |
JMF | 0:24d3eb812fd4 | 1986 | return (false); |
JMF | 0:24d3eb812fd4 | 1987 | } |
JMF | 0:24d3eb812fd4 | 1988 | // Parse out BER: 0..7 as RXQUAL values in the table 3GPP TS 45.008 subclause 8.2.4 |
JMF | 0:24d3eb812fd4 | 1989 | // 99 - unknown or undetectable |
JMF | 0:24d3eb812fd4 | 1990 | subStr = pRespStr->substr(pos2 + 1, pRespStr->length() - (pos2 + 1)); |
JMF | 0:24d3eb812fd4 | 1991 | *ber = atoi(subStr.c_str()); |
JMF | 0:24d3eb812fd4 | 1992 | } |
JMF | 0:24d3eb812fd4 | 1993 | else { |
JMF | 0:24d3eb812fd4 | 1994 | dbgPuts("Strange RSSI result2!"); |
JMF | 0:24d3eb812fd4 | 1995 | return (false); |
JMF | 0:24d3eb812fd4 | 1996 | } |
JMF | 0:24d3eb812fd4 | 1997 | } |
JMF | 0:24d3eb812fd4 | 1998 | |
JMF | 0:24d3eb812fd4 | 1999 | return (true); |
JMF | 0:24d3eb812fd4 | 2000 | } |
JMF | 0:24d3eb812fd4 | 2001 | |
JMF | 0:24d3eb812fd4 | 2002 | bool WncController::checkCellLink(void) |
JMF | 0:24d3eb812fd4 | 2003 | { |
JMF | 0:24d3eb812fd4 | 2004 | string * pRespStr; |
JMF | 0:24d3eb812fd4 | 2005 | size_t pos; |
JMF | 0:24d3eb812fd4 | 2006 | int regSts; |
JMF | 0:24d3eb812fd4 | 2007 | int cmdRes1, cmdRes2; |
JMF | 0:24d3eb812fd4 | 2008 | |
JMF | 0:24d3eb812fd4 | 2009 | if (m_sState == WNC_OFF) |
JMF | 0:24d3eb812fd4 | 2010 | return (false); |
JMF | 0:24d3eb812fd4 | 2011 | |
JMF | 0:24d3eb812fd4 | 2012 | m_sState = WNC_ON_NO_CELL_LINK; |
JMF | 0:24d3eb812fd4 | 2013 | |
JMF | 0:24d3eb812fd4 | 2014 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 2015 | dbgPuts("<-------- Begin Cell Status ------------"); |
JMF | 0:24d3eb812fd4 | 2016 | |
JMF | 0:24d3eb812fd4 | 2017 | cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, m_sCmdTimeoutMs); // Check RSSI,BER |
JMF | 0:24d3eb812fd4 | 2018 | |
JMF | 0:24d3eb812fd4 | 2019 | // If no response, don't bother with more commands |
JMF | 0:24d3eb812fd4 | 2020 | if (cmdRes1 != WNC_AT_CMD_TIMEOUT) |
JMF | 0:24d3eb812fd4 | 2021 | cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, m_sCmdTimeoutMs); // Check if SIM locked |
JMF | 0:24d3eb812fd4 | 2022 | else { |
JMF | 0:24d3eb812fd4 | 2023 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 2024 | dbgPuts("------------ WNC No Response! --------->"); |
JMF | 0:24d3eb812fd4 | 2025 | |
JMF | 0:24d3eb812fd4 | 2026 | return (false); |
JMF | 0:24d3eb812fd4 | 2027 | } |
JMF | 0:24d3eb812fd4 | 2028 | |
JMF | 0:24d3eb812fd4 | 2029 | if ((cmdRes1 != WNC_AT_CMD_OK) || (cmdRes2 != WNC_AT_CMD_OK) || (pRespStr->size() == 0)) |
JMF | 0:24d3eb812fd4 | 2030 | { |
JMF | 0:24d3eb812fd4 | 2031 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 2032 | { |
JMF | 0:24d3eb812fd4 | 2033 | if ((cmdRes1 == WNC_AT_CMD_TIMEOUT) || (cmdRes2 == WNC_AT_CMD_TIMEOUT)) |
JMF | 0:24d3eb812fd4 | 2034 | dbgPuts("------------ WNC No Response! --------->"); |
JMF | 0:24d3eb812fd4 | 2035 | else |
JMF | 0:24d3eb812fd4 | 2036 | dbgPuts("------------ WNC Cmd Error! ----------->"); |
JMF | 0:24d3eb812fd4 | 2037 | } |
JMF | 0:24d3eb812fd4 | 2038 | |
JMF | 0:24d3eb812fd4 | 2039 | // If by a miracle it responds to the 2nd after the 1st, keep going |
JMF | 0:24d3eb812fd4 | 2040 | if ((cmdRes2 == WNC_AT_CMD_TIMEOUT) || (pRespStr->size() == 0)) |
JMF | 0:24d3eb812fd4 | 2041 | return (false); |
JMF | 0:24d3eb812fd4 | 2042 | } |
JMF | 0:24d3eb812fd4 | 2043 | |
JMF | 0:24d3eb812fd4 | 2044 | // If SIM Card not ready don't bother with commands! |
JMF | 0:24d3eb812fd4 | 2045 | if (pRespStr->find("CPIN: READY") == string::npos) |
JMF | 0:24d3eb812fd4 | 2046 | { |
JMF | 0:24d3eb812fd4 | 2047 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 2048 | dbgPuts("------------ WNC SIM Problem! --------->"); |
JMF | 0:24d3eb812fd4 | 2049 | |
JMF | 0:24d3eb812fd4 | 2050 | return (false); |
JMF | 0:24d3eb812fd4 | 2051 | } |
JMF | 0:24d3eb812fd4 | 2052 | |
JMF | 0:24d3eb812fd4 | 2053 | // SIM card OK, now check for signal and cellular network registration |
JMF | 0:24d3eb812fd4 | 2054 | cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, m_sCmdTimeoutMs); // Check if registered on network |
JMF | 0:24d3eb812fd4 | 2055 | if (cmdRes1 != WNC_AT_CMD_OK || pRespStr->size() == 0) |
JMF | 0:24d3eb812fd4 | 2056 | { |
JMF | 0:24d3eb812fd4 | 2057 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 2058 | dbgPuts("------------ WNC +CREG? Fail! --------->"); |
JMF | 0:24d3eb812fd4 | 2059 | |
JMF | 0:24d3eb812fd4 | 2060 | return (false); |
JMF | 0:24d3eb812fd4 | 2061 | } |
JMF | 0:24d3eb812fd4 | 2062 | else |
JMF | 0:24d3eb812fd4 | 2063 | { |
JMF | 0:24d3eb812fd4 | 2064 | pos = pRespStr->find("CREG: "); |
JMF | 0:24d3eb812fd4 | 2065 | if (pos != string::npos) |
JMF | 0:24d3eb812fd4 | 2066 | { |
JMF | 0:24d3eb812fd4 | 2067 | // The registration is the 2nd arg in the comma separated list |
JMF | 0:24d3eb812fd4 | 2068 | *pRespStr = pRespStr->substr(pos+8, 1); |
JMF | 0:24d3eb812fd4 | 2069 | regSts = atoi(pRespStr->c_str()); |
JMF | 0:24d3eb812fd4 | 2070 | switch (regSts) { |
JMF | 0:24d3eb812fd4 | 2071 | case 1: |
JMF | 0:24d3eb812fd4 | 2072 | case 5: |
JMF | 0:24d3eb812fd4 | 2073 | case 6: |
JMF | 0:24d3eb812fd4 | 2074 | case 7: |
JMF | 0:24d3eb812fd4 | 2075 | m_sReadyForSMS = true; |
JMF | 0:24d3eb812fd4 | 2076 | break; |
JMF | 0:24d3eb812fd4 | 2077 | default: |
JMF | 0:24d3eb812fd4 | 2078 | m_sReadyForSMS = false; |
JMF | 0:24d3eb812fd4 | 2079 | dbgPuts("SMS Service Down!"); |
JMF | 0:24d3eb812fd4 | 2080 | } |
JMF | 0:24d3eb812fd4 | 2081 | |
JMF | 0:24d3eb812fd4 | 2082 | // 1 - registered home, 5 - registered roaming |
JMF | 0:24d3eb812fd4 | 2083 | if ((regSts != 1) && (regSts != 5)) |
JMF | 0:24d3eb812fd4 | 2084 | { |
JMF | 0:24d3eb812fd4 | 2085 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 2086 | dbgPuts("------ WNC Cell Link Down for Data! --->"); |
JMF | 0:24d3eb812fd4 | 2087 | |
JMF | 0:24d3eb812fd4 | 2088 | return (false); |
JMF | 0:24d3eb812fd4 | 2089 | } |
JMF | 0:24d3eb812fd4 | 2090 | } |
JMF | 0:24d3eb812fd4 | 2091 | |
JMF | 0:24d3eb812fd4 | 2092 | if (m_sMoreDebugEnabled) |
JMF | 0:24d3eb812fd4 | 2093 | dbgPuts("------------ WNC Ready ---------------->"); |
JMF | 0:24d3eb812fd4 | 2094 | } |
JMF | 0:24d3eb812fd4 | 2095 | |
JMF | 0:24d3eb812fd4 | 2096 | // If we made it this far and the WNC did respond, keep the ON state |
JMF | 0:24d3eb812fd4 | 2097 | if (m_sState != WNC_NO_RESPONSE) |
JMF | 0:24d3eb812fd4 | 2098 | m_sState = WNC_ON; |
JMF | 0:24d3eb812fd4 | 2099 | |
JMF | 0:24d3eb812fd4 | 2100 | return (true); |
JMF | 0:24d3eb812fd4 | 2101 | } |
JMF | 0:24d3eb812fd4 | 2102 | |
JMF | 0:24d3eb812fd4 | 2103 | int WncController::dbgPutsNoTime(const char * s, bool crlf) |
JMF | 0:24d3eb812fd4 | 2104 | { |
JMF | 0:24d3eb812fd4 | 2105 | if (m_sDebugEnabled == true) { |
JMF | 0:24d3eb812fd4 | 2106 | int r = dbgWriteChars(s); |
JMF | 0:24d3eb812fd4 | 2107 | if (crlf == true) |
JMF | 0:24d3eb812fd4 | 2108 | return (dbgWriteChars("\r\n")); |
JMF | 0:24d3eb812fd4 | 2109 | else |
JMF | 0:24d3eb812fd4 | 2110 | return (r); |
JMF | 0:24d3eb812fd4 | 2111 | } |
JMF | 0:24d3eb812fd4 | 2112 | else |
JMF | 0:24d3eb812fd4 | 2113 | return 0; |
JMF | 0:24d3eb812fd4 | 2114 | }; |
JMF | 0:24d3eb812fd4 | 2115 | |
JMF | 0:24d3eb812fd4 | 2116 | int WncController::dbgPuts(const char * s, bool crlf) |
JMF | 0:24d3eb812fd4 | 2117 | { |
JMF | 0:24d3eb812fd4 | 2118 | dbgPutsNoTime("[*] ", false); |
JMF | 0:24d3eb812fd4 | 2119 | dbgPutsNoTime(_to_string(getLogTimerTicks()), false); |
JMF | 0:24d3eb812fd4 | 2120 | dbgPutsNoTime(" ", false); |
JMF | 0:24d3eb812fd4 | 2121 | |
JMF | 0:24d3eb812fd4 | 2122 | int r = dbgPutsNoTime(s, false); |
JMF | 0:24d3eb812fd4 | 2123 | |
JMF | 0:24d3eb812fd4 | 2124 | if (crlf == true) |
JMF | 0:24d3eb812fd4 | 2125 | return (dbgPutsNoTime("", true)); |
JMF | 0:24d3eb812fd4 | 2126 | else |
JMF | 0:24d3eb812fd4 | 2127 | return (r); |
JMF | 0:24d3eb812fd4 | 2128 | }; |
JMF | 0:24d3eb812fd4 | 2129 | |
JMF | 0:24d3eb812fd4 | 2130 | void WncController::sendCmd(const char * cmd, bool crLf) |
JMF | 0:24d3eb812fd4 | 2131 | { |
JMF | 0:24d3eb812fd4 | 2132 | puts(cmd); |
JMF | 0:24d3eb812fd4 | 2133 | if (crLf == true) |
JMF | 0:24d3eb812fd4 | 2134 | puts("\r\n"); |
JMF | 0:24d3eb812fd4 | 2135 | } |
JMF | 0:24d3eb812fd4 | 2136 | |
JMF | 0:24d3eb812fd4 | 2137 | void WncController::sendCmd(const char * cmd, unsigned n, unsigned wait_uS, bool crLf) |
JMF | 0:24d3eb812fd4 | 2138 | { |
JMF | 0:24d3eb812fd4 | 2139 | while (n--) { |
JMF | 0:24d3eb812fd4 | 2140 | putc(*cmd++); |
JMF | 0:24d3eb812fd4 | 2141 | waitUs(wait_uS); |
JMF | 0:24d3eb812fd4 | 2142 | }; |
JMF | 0:24d3eb812fd4 | 2143 | if (crLf == true) { |
JMF | 0:24d3eb812fd4 | 2144 | putc('\r'); |
JMF | 0:24d3eb812fd4 | 2145 | waitUs(wait_uS); |
JMF | 0:24d3eb812fd4 | 2146 | putc('\n'); |
JMF | 0:24d3eb812fd4 | 2147 | waitUs(wait_uS); |
JMF | 0:24d3eb812fd4 | 2148 | } |
JMF | 0:24d3eb812fd4 | 2149 | } |
JMF | 0:24d3eb812fd4 | 2150 | |
JMF | 0:24d3eb812fd4 | 2151 | }; // End namespace WncController_fk |
JMF | 0:24d3eb812fd4 | 2152 |