![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork with SeeedStudio Xadow GPS v2 enabled
Dependencies: FXOS8700CQ MODSERIAL XadowGPS mbed
Fork of Avnet_ATT_Cellular_IOT by
wnc_control.cpp@73:da723fedfdd2, 2016-08-12 (annotated)
- Committer:
- fkellermavnet
- Date:
- Fri Aug 12 19:47:29 2016 +0000
- Revision:
- 73:da723fedfdd2
- Parent:
- 68:6e311c747045
- Child:
- 75:8cc98a3b9c62
Merged in WNC suggestions for: Check for EXTERR and close and reopen the socket if it occurs. If sockopen fails send command to close the socket. Don't use find() on empty strings.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fkellermavnet | 68:6e311c747045 | 1 | /* =================================================================== |
fkellermavnet | 68:6e311c747045 | 2 | Copyright © 2016, AVNET Inc. |
fkellermavnet | 68:6e311c747045 | 3 | |
fkellermavnet | 68:6e311c747045 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
fkellermavnet | 68:6e311c747045 | 5 | you may not use this file except in compliance with the License. |
fkellermavnet | 68:6e311c747045 | 6 | You may obtain a copy of the License at |
fkellermavnet | 68:6e311c747045 | 7 | |
fkellermavnet | 68:6e311c747045 | 8 | http://www.apache.org/licenses/LICENSE-2.0 |
fkellermavnet | 68:6e311c747045 | 9 | |
fkellermavnet | 68:6e311c747045 | 10 | Unless required by applicable law or agreed to in writing, |
fkellermavnet | 68:6e311c747045 | 11 | software distributed under the License is distributed on an |
fkellermavnet | 68:6e311c747045 | 12 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
fkellermavnet | 68:6e311c747045 | 13 | either express or implied. See the License for the specific |
fkellermavnet | 68:6e311c747045 | 14 | language governing permissions and limitations under the License. |
fkellermavnet | 68:6e311c747045 | 15 | |
fkellermavnet | 68:6e311c747045 | 16 | ======================================================================== */ |
fkellermavnet | 68:6e311c747045 | 17 | |
JMF | 2:0e2ef866af95 | 18 | #include "mbed.h" |
JMF | 2:0e2ef866af95 | 19 | #include <cctype> |
JMF | 2:0e2ef866af95 | 20 | #include <string> |
JMF | 2:0e2ef866af95 | 21 | #include "config_me.h" |
JMF | 2:0e2ef866af95 | 22 | #include "wnc_control.h" |
stefanrousseau | 62:73c564e883e9 | 23 | #include "hardware.h" |
JMF | 2:0e2ef866af95 | 24 | |
fkellermavnet | 53:dcccf2881fa0 | 25 | // Outputs detailed WNC command info |
fkellermavnet | 53:dcccf2881fa0 | 26 | #define WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 27 | |
fkellermavnet | 53:dcccf2881fa0 | 28 | // Full debug output, longer cmds and extra cellular status checking |
fkellermavnet | 53:dcccf2881fa0 | 29 | #undef WNC_CMD_DEBUG_ON_VERBOSE |
fkellermavnet | 53:dcccf2881fa0 | 30 | |
JMF | 2:0e2ef866af95 | 31 | extern string MyServerIpAddress; |
JMF | 2:0e2ef866af95 | 32 | extern string MySocketData; |
JMF | 2:0e2ef866af95 | 33 | |
JMF | 2:0e2ef866af95 | 34 | int reinitialize_mdm(void); |
JMF | 2:0e2ef866af95 | 35 | |
JMF | 2:0e2ef866af95 | 36 | enum WNC_ERR_e { |
JMF | 2:0e2ef866af95 | 37 | WNC_OK =0, |
JMF | 2:0e2ef866af95 | 38 | WNC_CMD_ERR = -1, |
fkellermavnet | 46:da9d788f5d5a | 39 | WNC_NO_RESPONSE = -2, |
fkellermavnet | 73:da723fedfdd2 | 40 | WNC_CELL_LINK_DOWN = -3, |
fkellermavnet | 73:da723fedfdd2 | 41 | WNC_EXTERR = -4 |
JMF | 2:0e2ef866af95 | 42 | }; |
JMF | 2:0e2ef866af95 | 43 | |
JMF | 2:0e2ef866af95 | 44 | // Contains result of last call to send_wnc_cmd(..) |
JMF | 2:0e2ef866af95 | 45 | WNC_ERR_e WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 46 | |
JMF | 2:0e2ef866af95 | 47 | // Contains the RAW WNC UART responses |
JMF | 2:0e2ef866af95 | 48 | static string wncStr; |
fkellermavnet | 10:df54436ecd38 | 49 | static int socketOpen = 0; |
JMF | 2:0e2ef866af95 | 50 | |
JMF | 2:0e2ef866af95 | 51 | void software_init_mdm(void) |
JMF | 2:0e2ef866af95 | 52 | { |
fkellermavnet | 66:b2425419b0cc | 53 | // Temp put here to fix new boards needing init, |
fkellermavnet | 66:b2425419b0cc | 54 | // the check for on the cellular network was preventing the PDNSET from happening!!!! |
fkellermavnet | 66:b2425419b0cc | 55 | { |
JMF | 67:11db02bb93e1 | 56 | PUTS("SET APN STRING!\r\n"); |
fkellermavnet | 66:b2425419b0cc | 57 | string * pRespStr; |
fkellermavnet | 66:b2425419b0cc | 58 | string cmd_str("AT%PDNSET=1,"); |
fkellermavnet | 66:b2425419b0cc | 59 | cmd_str += MY_APN_STR; |
fkellermavnet | 66:b2425419b0cc | 60 | cmd_str += ",IP"; |
fkellermavnet | 66:b2425419b0cc | 61 | at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes |
fkellermavnet | 66:b2425419b0cc | 62 | } |
fkellermavnet | 66:b2425419b0cc | 63 | |
fkellermavnet | 42:be4b9ee3a615 | 64 | static bool reportStatus = true; |
JMF | 2:0e2ef866af95 | 65 | do |
JMF | 2:0e2ef866af95 | 66 | { |
fkellermavnet | 73:da723fedfdd2 | 67 | PUTS("------------ software_init_mdm! --------->\r\n"); |
fkellermavnet | 42:be4b9ee3a615 | 68 | if (check_wnc_ready() == 0) |
fkellermavnet | 42:be4b9ee3a615 | 69 | { |
fkellermavnet | 42:be4b9ee3a615 | 70 | if (reportStatus == false) |
fkellermavnet | 42:be4b9ee3a615 | 71 | { |
JMF | 67:11db02bb93e1 | 72 | PUTS("Re-connected to cellular network!\n\r"); |
fkellermavnet | 42:be4b9ee3a615 | 73 | reportStatus = true; |
fkellermavnet | 42:be4b9ee3a615 | 74 | } |
fkellermavnet | 42:be4b9ee3a615 | 75 | |
fkellermavnet | 42:be4b9ee3a615 | 76 | // WNC has SIM and registered on network |
fkellermavnet | 42:be4b9ee3a615 | 77 | do |
fkellermavnet | 42:be4b9ee3a615 | 78 | { |
fkellermavnet | 42:be4b9ee3a615 | 79 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 42:be4b9ee3a615 | 80 | at_init_wnc(); |
fkellermavnet | 42:be4b9ee3a615 | 81 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
fkellermavnet | 42:be4b9ee3a615 | 82 | { |
fkellermavnet | 42:be4b9ee3a615 | 83 | reinitialize_mdm(); |
fkellermavnet | 42:be4b9ee3a615 | 84 | at_init_wnc(true); // Hard reset occurred so make it go through the software init(); |
fkellermavnet | 42:be4b9ee3a615 | 85 | } |
fkellermavnet | 42:be4b9ee3a615 | 86 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 42:be4b9ee3a615 | 87 | } |
fkellermavnet | 42:be4b9ee3a615 | 88 | else |
fkellermavnet | 42:be4b9ee3a615 | 89 | { |
fkellermavnet | 42:be4b9ee3a615 | 90 | if (reportStatus == true) |
fkellermavnet | 42:be4b9ee3a615 | 91 | { |
JMF | 67:11db02bb93e1 | 92 | PUTS("Not connected to cellular network!\n\r"); |
fkellermavnet | 42:be4b9ee3a615 | 93 | reportStatus = false; |
fkellermavnet | 42:be4b9ee3a615 | 94 | } |
fkellermavnet | 42:be4b9ee3a615 | 95 | // Atempt to re-register |
fkellermavnet | 42:be4b9ee3a615 | 96 | // string * pRespStr; |
JMF | 67:11db02bb93e1 | 97 | // PUTS("Force re-register!\r\n"); |
fkellermavnet | 42:be4b9ee3a615 | 98 | // at_send_wnc_cmd("AT+CFUN=0,0", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 99 | // wait_ms(31000); |
fkellermavnet | 42:be4b9ee3a615 | 100 | // at_send_wnc_cmd("AT+CFUN=1,0", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 101 | // wait_ms(31000); |
fkellermavnet | 46:da9d788f5d5a | 102 | WNC_MDM_ERR = WNC_CELL_LINK_DOWN; |
fkellermavnet | 42:be4b9ee3a615 | 103 | } |
fkellermavnet | 42:be4b9ee3a615 | 104 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 105 | } |
JMF | 2:0e2ef866af95 | 106 | |
JMF | 2:0e2ef866af95 | 107 | void resolve_mdm(void) |
JMF | 2:0e2ef866af95 | 108 | { |
JMF | 2:0e2ef866af95 | 109 | do |
JMF | 2:0e2ef866af95 | 110 | { |
JMF | 2:0e2ef866af95 | 111 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 112 | at_dnsresolve_wnc(MY_SERVER_URL, &MyServerIpAddress); |
JMF | 2:0e2ef866af95 | 113 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 114 | { |
JMF | 2:0e2ef866af95 | 115 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 116 | } |
JMF | 2:0e2ef866af95 | 117 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 118 | { |
JMF | 67:11db02bb93e1 | 119 | PUTS("Bad URL!!!!!!\r\n"); |
JMF | 2:0e2ef866af95 | 120 | } |
JMF | 2:0e2ef866af95 | 121 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 122 | |
stefanrousseau | 64:09004cd610df | 123 | PRINTF("My Server IP: %s\r\n", MyServerIpAddress.c_str()); |
JMF | 2:0e2ef866af95 | 124 | } |
JMF | 2:0e2ef866af95 | 125 | |
JMF | 2:0e2ef866af95 | 126 | void sockopen_mdm(void) |
JMF | 2:0e2ef866af95 | 127 | { |
JMF | 2:0e2ef866af95 | 128 | do |
JMF | 2:0e2ef866af95 | 129 | { |
JMF | 2:0e2ef866af95 | 130 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 131 | at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR); |
JMF | 2:0e2ef866af95 | 132 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 133 | { |
JMF | 2:0e2ef866af95 | 134 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 135 | } |
JMF | 2:0e2ef866af95 | 136 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 137 | PUTS("Socket open fail!!!!\r\n"); |
fkellermavnet | 10:df54436ecd38 | 138 | else |
fkellermavnet | 10:df54436ecd38 | 139 | socketOpen = 1; |
JMF | 2:0e2ef866af95 | 140 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 141 | } |
JMF | 2:0e2ef866af95 | 142 | |
JMF | 2:0e2ef866af95 | 143 | void sockwrite_mdm(const char * s) |
JMF | 2:0e2ef866af95 | 144 | { |
fkellermavnet | 10:df54436ecd38 | 145 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 146 | { |
JMF | 2:0e2ef866af95 | 147 | do |
JMF | 2:0e2ef866af95 | 148 | { |
JMF | 2:0e2ef866af95 | 149 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 150 | at_sockwrite_wnc(s); |
JMF | 2:0e2ef866af95 | 151 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 152 | { |
JMF | 67:11db02bb93e1 | 153 | PUTS("Sock write no response!\r\n"); |
JMF | 2:0e2ef866af95 | 154 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 155 | } |
JMF | 2:0e2ef866af95 | 156 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 157 | { |
JMF | 67:11db02bb93e1 | 158 | PUTS("Socket Write fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 159 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 160 | } |
fkellermavnet | 73:da723fedfdd2 | 161 | else if (WNC_MDM_ERR == WNC_EXTERR) |
fkellermavnet | 73:da723fedfdd2 | 162 | { |
fkellermavnet | 73:da723fedfdd2 | 163 | pc.puts("Socket Disconnected (broken) !!!\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 164 | sockclose_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 165 | sockopen_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 166 | //software_init_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 167 | } |
JMF | 2:0e2ef866af95 | 168 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 169 | } |
fkellermavnet | 10:df54436ecd38 | 170 | else |
JMF | 67:11db02bb93e1 | 171 | PUTS("Socket is closed for write!\r\n"); |
JMF | 2:0e2ef866af95 | 172 | } |
JMF | 2:0e2ef866af95 | 173 | |
fkellermavnet | 30:33be8e2992f3 | 174 | unsigned sockread_mdm(string * sockData, int len, int retries) |
JMF | 2:0e2ef866af95 | 175 | { |
fkellermavnet | 30:33be8e2992f3 | 176 | unsigned n = 0; |
fkellermavnet | 30:33be8e2992f3 | 177 | |
fkellermavnet | 10:df54436ecd38 | 178 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 179 | { |
JMF | 2:0e2ef866af95 | 180 | do |
JMF | 2:0e2ef866af95 | 181 | { |
JMF | 2:0e2ef866af95 | 182 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 30:33be8e2992f3 | 183 | n = at_sockread_wnc(sockData, len, retries); |
JMF | 2:0e2ef866af95 | 184 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 185 | { |
fkellermavnet | 30:33be8e2992f3 | 186 | if (n == 0) |
fkellermavnet | 30:33be8e2992f3 | 187 | software_init_mdm(); |
fkellermavnet | 30:33be8e2992f3 | 188 | else |
JMF | 67:11db02bb93e1 | 189 | PUTS("Sock read partial data!!!\r\n"); |
JMF | 2:0e2ef866af95 | 190 | } |
JMF | 2:0e2ef866af95 | 191 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 192 | PUTS("Sock read fail!!!!\r\n"); |
fkellermavnet | 30:33be8e2992f3 | 193 | } while (WNC_MDM_ERR == WNC_NO_RESPONSE); |
fkellermavnet | 10:df54436ecd38 | 194 | } |
fkellermavnet | 10:df54436ecd38 | 195 | else |
fkellermavnet | 44:c95a85b5cf92 | 196 | { |
JMF | 67:11db02bb93e1 | 197 | PUTS("Socket is closed for read\r\n"); |
fkellermavnet | 44:c95a85b5cf92 | 198 | sockData->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 199 | } |
fkellermavnet | 30:33be8e2992f3 | 200 | |
fkellermavnet | 30:33be8e2992f3 | 201 | return (n); |
JMF | 2:0e2ef866af95 | 202 | } |
JMF | 2:0e2ef866af95 | 203 | |
JMF | 2:0e2ef866af95 | 204 | void sockclose_mdm(void) |
JMF | 2:0e2ef866af95 | 205 | { |
JMF | 2:0e2ef866af95 | 206 | do |
JMF | 2:0e2ef866af95 | 207 | { |
JMF | 2:0e2ef866af95 | 208 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 209 | at_sockclose_wnc(); |
fkellermavnet | 10:df54436ecd38 | 210 | // Assume close happened even if it went bad |
fkellermavnet | 10:df54436ecd38 | 211 | // going bad will result in a re-init anyways and if close |
fkellermavnet | 10:df54436ecd38 | 212 | // fails we're pretty much in bad state and not much can do |
fkellermavnet | 10:df54436ecd38 | 213 | socketOpen = 0; |
JMF | 2:0e2ef866af95 | 214 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 215 | { |
JMF | 2:0e2ef866af95 | 216 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 217 | } |
JMF | 2:0e2ef866af95 | 218 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 219 | PUTS("Sock close fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 220 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 221 | } |
JMF | 2:0e2ef866af95 | 222 | |
JMF | 2:0e2ef866af95 | 223 | /** |
JMF | 2:0e2ef866af95 | 224 | * C++ version 0.4 char* style "itoa": |
JMF | 2:0e2ef866af95 | 225 | * Written by Lukás Chmela |
JMF | 2:0e2ef866af95 | 226 | * Released under GPLv3. |
JMF | 2:0e2ef866af95 | 227 | */ |
JMF | 2:0e2ef866af95 | 228 | |
JMF | 2:0e2ef866af95 | 229 | char* itoa(int value, char* result, int base) |
JMF | 2:0e2ef866af95 | 230 | { |
JMF | 2:0e2ef866af95 | 231 | // check that the base if valid |
JMF | 2:0e2ef866af95 | 232 | if ( base < 2 || base > 36 ) { |
JMF | 2:0e2ef866af95 | 233 | *result = '\0'; |
JMF | 2:0e2ef866af95 | 234 | return result; |
JMF | 2:0e2ef866af95 | 235 | } |
JMF | 2:0e2ef866af95 | 236 | |
JMF | 2:0e2ef866af95 | 237 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 2:0e2ef866af95 | 238 | int tmp_value; |
JMF | 2:0e2ef866af95 | 239 | |
JMF | 2:0e2ef866af95 | 240 | do { |
JMF | 2:0e2ef866af95 | 241 | tmp_value = value; |
JMF | 2:0e2ef866af95 | 242 | value /= base; |
JMF | 2:0e2ef866af95 | 243 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 2:0e2ef866af95 | 244 | } while ( value ); |
JMF | 2:0e2ef866af95 | 245 | |
JMF | 2:0e2ef866af95 | 246 | // Apply negative sign |
JMF | 2:0e2ef866af95 | 247 | if ( tmp_value < 0 ) |
JMF | 2:0e2ef866af95 | 248 | *ptr++ = '-'; |
JMF | 2:0e2ef866af95 | 249 | *ptr-- = '\0'; |
JMF | 2:0e2ef866af95 | 250 | |
JMF | 2:0e2ef866af95 | 251 | while ( ptr1 < ptr ) { |
JMF | 2:0e2ef866af95 | 252 | tmp_char = *ptr; |
JMF | 2:0e2ef866af95 | 253 | *ptr-- = *ptr1; |
JMF | 2:0e2ef866af95 | 254 | *ptr1++ = tmp_char; |
JMF | 2:0e2ef866af95 | 255 | } |
JMF | 2:0e2ef866af95 | 256 | |
JMF | 2:0e2ef866af95 | 257 | return result; |
JMF | 2:0e2ef866af95 | 258 | } |
JMF | 2:0e2ef866af95 | 259 | |
JMF | 2:0e2ef866af95 | 260 | extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len); |
JMF | 2:0e2ef866af95 | 261 | |
fkellermavnet | 42:be4b9ee3a615 | 262 | int check_wnc_ready(void) |
fkellermavnet | 42:be4b9ee3a615 | 263 | { |
fkellermavnet | 43:6821a9c78c4b | 264 | string * pRespStr; |
fkellermavnet | 43:6821a9c78c4b | 265 | size_t pos; |
fkellermavnet | 43:6821a9c78c4b | 266 | int regSts; |
fkellermavnet | 46:da9d788f5d5a | 267 | int cmdRes1, cmdRes2; |
fkellermavnet | 42:be4b9ee3a615 | 268 | |
fkellermavnet | 53:dcccf2881fa0 | 269 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 270 | PUTS("<-------- Begin Cell Status ------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 271 | #endif |
fkellermavnet | 46:da9d788f5d5a | 272 | cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS); // Check RSSI,BER |
fkellermavnet | 46:da9d788f5d5a | 273 | cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS); // Check if SIM locked |
fkellermavnet | 46:da9d788f5d5a | 274 | |
fkellermavnet | 46:da9d788f5d5a | 275 | if ((cmdRes1 != 0) && (cmdRes2 != 0)) |
fkellermavnet | 43:6821a9c78c4b | 276 | { |
fkellermavnet | 53:dcccf2881fa0 | 277 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 278 | PUTS("------------ WNC No Response! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 279 | #endif |
fkellermavnet | 43:6821a9c78c4b | 280 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 281 | } |
fkellermavnet | 42:be4b9ee3a615 | 282 | |
fkellermavnet | 43:6821a9c78c4b | 283 | // If SIM Card not ready don't bother with commands! |
fkellermavnet | 43:6821a9c78c4b | 284 | if (pRespStr->find("CPIN: READY") == string::npos) |
fkellermavnet | 43:6821a9c78c4b | 285 | { |
fkellermavnet | 53:dcccf2881fa0 | 286 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 287 | PUTS("------------ WNC SIM Problem! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 288 | #endif |
fkellermavnet | 43:6821a9c78c4b | 289 | return (-1); |
fkellermavnet | 43:6821a9c78c4b | 290 | } |
fkellermavnet | 43:6821a9c78c4b | 291 | |
fkellermavnet | 43:6821a9c78c4b | 292 | // SIM card OK, now check for signal and cellular network registration |
fkellermavnet | 46:da9d788f5d5a | 293 | cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS); // Check if registered on network |
fkellermavnet | 73:da723fedfdd2 | 294 | if (pRespStr->size() > 0) |
fkellermavnet | 73:da723fedfdd2 | 295 | { |
fkellermavnet | 42:be4b9ee3a615 | 296 | pos = pRespStr->find("CREG: "); |
fkellermavnet | 42:be4b9ee3a615 | 297 | if (pos != string::npos) |
fkellermavnet | 42:be4b9ee3a615 | 298 | { |
fkellermavnet | 43:6821a9c78c4b | 299 | // The registration is the 2nd arg in the comma separated list |
fkellermavnet | 43:6821a9c78c4b | 300 | *pRespStr = pRespStr->substr(pos+8, 1); |
fkellermavnet | 43:6821a9c78c4b | 301 | regSts = atoi(pRespStr->c_str()); |
fkellermavnet | 43:6821a9c78c4b | 302 | // 1 - registered home, 5 - registered roaming |
fkellermavnet | 43:6821a9c78c4b | 303 | if ((regSts != 1) && (regSts != 5)) |
fkellermavnet | 43:6821a9c78c4b | 304 | { |
fkellermavnet | 53:dcccf2881fa0 | 305 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 306 | PUTS("------------ WNC Cell Link Down! ------>\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 307 | #endif |
fkellermavnet | 73:da723fedfdd2 | 308 | wait_ms(3000); |
fkellermavnet | 43:6821a9c78c4b | 309 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 310 | } |
fkellermavnet | 42:be4b9ee3a615 | 311 | } |
fkellermavnet | 43:6821a9c78c4b | 312 | |
fkellermavnet | 53:dcccf2881fa0 | 313 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 314 | PUTS("------------ WNC Ready ---------------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 315 | #endif |
fkellermavnet | 73:da723fedfdd2 | 316 | } |
fkellermavnet | 73:da723fedfdd2 | 317 | else |
fkellermavnet | 73:da723fedfdd2 | 318 | { |
fkellermavnet | 73:da723fedfdd2 | 319 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
fkellermavnet | 73:da723fedfdd2 | 320 | PUTS("------------ CREG No Reply !----------->\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 321 | #endif |
fkellermavnet | 73:da723fedfdd2 | 322 | return (-2); |
fkellermavnet | 73:da723fedfdd2 | 323 | } |
fkellermavnet | 73:da723fedfdd2 | 324 | |
fkellermavnet | 43:6821a9c78c4b | 325 | return (0); |
fkellermavnet | 42:be4b9ee3a615 | 326 | } |
fkellermavnet | 42:be4b9ee3a615 | 327 | |
JMF | 2:0e2ef866af95 | 328 | // Sets a global with failure or success, assumes 1 thread all the time |
JMF | 2:0e2ef866af95 | 329 | int send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 2:0e2ef866af95 | 330 | { |
fkellermavnet | 46:da9d788f5d5a | 331 | int cmdRes; |
fkellermavnet | 46:da9d788f5d5a | 332 | |
fkellermavnet | 42:be4b9ee3a615 | 333 | if (check_wnc_ready() < 0) |
fkellermavnet | 42:be4b9ee3a615 | 334 | { |
fkellermavnet | 45:a836eecd5d12 | 335 | static string noRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 336 | |
fkellermavnet | 53:dcccf2881fa0 | 337 | #ifdef WNC_CMD_DEBUG_ON |
JMF | 67:11db02bb93e1 | 338 | PUTS("FAIL send cmd: "); |
fkellermavnet | 53:dcccf2881fa0 | 339 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 340 | PUTS(s); |
fkellermavnet | 73:da723fedfdd2 | 341 | |
fkellermavnet | 53:dcccf2881fa0 | 342 | #else |
fkellermavnet | 53:dcccf2881fa0 | 343 | string truncStr(s, 50); |
fkellermavnet | 53:dcccf2881fa0 | 344 | truncStr += "\r\n"; |
JMF | 67:11db02bb93e1 | 345 | PUTS(truncStr.c_str()); |
fkellermavnet | 53:dcccf2881fa0 | 346 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 347 | #else |
JMF | 67:11db02bb93e1 | 348 | PUTS("FAIL send cmd!\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 349 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 350 | |
fkellermavnet | 46:da9d788f5d5a | 351 | WNC_MDM_ERR = WNC_CELL_LINK_DOWN; |
fkellermavnet | 45:a836eecd5d12 | 352 | noRespStr.erase(); |
fkellermavnet | 44:c95a85b5cf92 | 353 | *r = &noRespStr; |
fkellermavnet | 46:da9d788f5d5a | 354 | return (-3); |
fkellermavnet | 42:be4b9ee3a615 | 355 | } |
fkellermavnet | 53:dcccf2881fa0 | 356 | |
fkellermavnet | 53:dcccf2881fa0 | 357 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 358 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 359 | PUTS("[---------- Network Status -------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 360 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 361 | string * pRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 362 | at_send_wnc_cmd("AT@SOCKDIAL?", &pRespStr, 5000); |
fkellermavnet | 53:dcccf2881fa0 | 363 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 364 | PUTS("---------------------------------------]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 365 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 366 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 367 | |
fkellermavnet | 42:be4b9ee3a615 | 368 | // If WNC ready, send user command |
fkellermavnet | 46:da9d788f5d5a | 369 | cmdRes = at_send_wnc_cmd(s, r, ms_timeout); |
fkellermavnet | 46:da9d788f5d5a | 370 | |
fkellermavnet | 46:da9d788f5d5a | 371 | if (cmdRes == -1) |
fkellermavnet | 46:da9d788f5d5a | 372 | WNC_MDM_ERR = WNC_CMD_ERR; |
fkellermavnet | 46:da9d788f5d5a | 373 | |
fkellermavnet | 46:da9d788f5d5a | 374 | if (cmdRes == -2) |
fkellermavnet | 46:da9d788f5d5a | 375 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 46:da9d788f5d5a | 376 | |
fkellermavnet | 73:da723fedfdd2 | 377 | if (cmdRes == -3) |
fkellermavnet | 73:da723fedfdd2 | 378 | WNC_MDM_ERR = WNC_EXTERR; |
fkellermavnet | 73:da723fedfdd2 | 379 | |
fkellermavnet | 73:da723fedfdd2 | 380 | if (cmdRes == 0) |
fkellermavnet | 46:da9d788f5d5a | 381 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 46:da9d788f5d5a | 382 | |
fkellermavnet | 46:da9d788f5d5a | 383 | return (cmdRes); |
fkellermavnet | 42:be4b9ee3a615 | 384 | } |
fkellermavnet | 42:be4b9ee3a615 | 385 | |
fkellermavnet | 42:be4b9ee3a615 | 386 | int at_send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
fkellermavnet | 42:be4b9ee3a615 | 387 | { |
fkellermavnet | 73:da723fedfdd2 | 388 | static const char * rsp_lst[] = { "OK", "ERROR", "@EXTERR", NULL }; |
JMF | 2:0e2ef866af95 | 389 | int len; |
fkellermavnet | 53:dcccf2881fa0 | 390 | |
fkellermavnet | 53:dcccf2881fa0 | 391 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 392 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
fkellermavnet | 53:dcccf2881fa0 | 393 | |
fkellermavnet | 53:dcccf2881fa0 | 394 | #else |
fkellermavnet | 42:be4b9ee3a615 | 395 | if (strlen(s) > 60) |
fkellermavnet | 42:be4b9ee3a615 | 396 | { |
fkellermavnet | 42:be4b9ee3a615 | 397 | string truncStr(s,57); |
fkellermavnet | 42:be4b9ee3a615 | 398 | truncStr += "..."; |
stefanrousseau | 64:09004cd610df | 399 | PRINTF("Send: <<%s>>\r\n",truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 400 | } |
fkellermavnet | 42:be4b9ee3a615 | 401 | else |
fkellermavnet | 53:dcccf2881fa0 | 402 | #endif |
stefanrousseau | 64:09004cd610df | 403 | PRINTF("Send: <<%s>>\r\n",s); |
fkellermavnet | 53:dcccf2881fa0 | 404 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 405 | |
JMF | 2:0e2ef866af95 | 406 | int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len); |
JMF | 2:0e2ef866af95 | 407 | *r = &wncStr; // Return a pointer to the static string |
JMF | 2:0e2ef866af95 | 408 | |
JMF | 2:0e2ef866af95 | 409 | if (res >= 0) |
fkellermavnet | 53:dcccf2881fa0 | 410 | { |
fkellermavnet | 53:dcccf2881fa0 | 411 | |
fkellermavnet | 53:dcccf2881fa0 | 412 | #ifdef WNC_CMD_DEBUG_ON |
JMF | 67:11db02bb93e1 | 413 | PUTS("["); |
fkellermavnet | 53:dcccf2881fa0 | 414 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 415 | PUTS(wncStr.c_str()); |
JMF | 67:11db02bb93e1 | 416 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 417 | #else |
fkellermavnet | 42:be4b9ee3a615 | 418 | if (wncStr.size() < 51) |
JMF | 67:11db02bb93e1 | 419 | PUTS(wncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 420 | else |
fkellermavnet | 42:be4b9ee3a615 | 421 | { |
fkellermavnet | 42:be4b9ee3a615 | 422 | string truncStr = wncStr.substr(0,50) + "..."; |
JMF | 67:11db02bb93e1 | 423 | PUTS(truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 424 | } |
JMF | 67:11db02bb93e1 | 425 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 426 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 427 | #endif |
fkellermavnet | 42:be4b9ee3a615 | 428 | |
fkellermavnet | 73:da723fedfdd2 | 429 | if (res == 0) { |
fkellermavnet | 73:da723fedfdd2 | 430 | /* OK */ |
fkellermavnet | 73:da723fedfdd2 | 431 | return 0; |
fkellermavnet | 73:da723fedfdd2 | 432 | } else if (res == 2) { |
fkellermavnet | 73:da723fedfdd2 | 433 | /* @EXTERR */ |
fkellermavnet | 73:da723fedfdd2 | 434 | return -3; |
fkellermavnet | 73:da723fedfdd2 | 435 | } else |
JMF | 2:0e2ef866af95 | 436 | return -1; |
JMF | 2:0e2ef866af95 | 437 | } |
JMF | 2:0e2ef866af95 | 438 | else |
JMF | 2:0e2ef866af95 | 439 | { |
JMF | 67:11db02bb93e1 | 440 | PUTS("No response from WNC!\n\r"); |
JMF | 2:0e2ef866af95 | 441 | return -2; |
JMF | 2:0e2ef866af95 | 442 | } |
JMF | 2:0e2ef866af95 | 443 | } |
JMF | 2:0e2ef866af95 | 444 | |
fkellermavnet | 42:be4b9ee3a615 | 445 | |
fkellermavnet | 19:f89baed3bd6f | 446 | void at_at_wnc(void) |
fkellermavnet | 19:f89baed3bd6f | 447 | { |
fkellermavnet | 19:f89baed3bd6f | 448 | string * pRespStr; |
fkellermavnet | 19:f89baed3bd6f | 449 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 19:f89baed3bd6f | 450 | } |
fkellermavnet | 19:f89baed3bd6f | 451 | |
fkellermavnet | 42:be4b9ee3a615 | 452 | void at_init_wnc(bool hardReset) |
JMF | 2:0e2ef866af95 | 453 | { |
fkellermavnet | 36:d4782eabff43 | 454 | static bool pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 455 | static bool intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 456 | static bool sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 457 | string * pRespStr; |
fkellermavnet | 46:da9d788f5d5a | 458 | int cmdRes; |
fkellermavnet | 36:d4782eabff43 | 459 | |
fkellermavnet | 42:be4b9ee3a615 | 460 | if (hardReset == true) |
fkellermavnet | 42:be4b9ee3a615 | 461 | { |
JMF | 67:11db02bb93e1 | 462 | PUTS("Hard Reset!\r\n"); |
fkellermavnet | 42:be4b9ee3a615 | 463 | pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 464 | intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 465 | sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 466 | } |
fkellermavnet | 42:be4b9ee3a615 | 467 | |
JMF | 67:11db02bb93e1 | 468 | PUTS("Start AT init of WNC:\r\n"); |
fkellermavnet | 43:6821a9c78c4b | 469 | // Quick commands below do not need to check cellular connectivity |
fkellermavnet | 46:da9d788f5d5a | 470 | cmdRes = at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 46:da9d788f5d5a | 471 | cmdRes += at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS); // Echo Off |
fkellermavnet | 46:da9d788f5d5a | 472 | cmdRes += at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS); // 2 - verbose error, 1 - numeric error, 0 - just ERROR |
fkellermavnet | 43:6821a9c78c4b | 473 | |
fkellermavnet | 43:6821a9c78c4b | 474 | // If the simple commands are not working no chance of more complex. |
fkellermavnet | 43:6821a9c78c4b | 475 | // I have seen re-trying commands make it worse. |
fkellermavnet | 46:da9d788f5d5a | 476 | if (cmdRes < 0) |
fkellermavnet | 46:da9d788f5d5a | 477 | { |
fkellermavnet | 46:da9d788f5d5a | 478 | // Since I used the at_send_wnc_cmd I am setting the error state based upon |
fkellermavnet | 46:da9d788f5d5a | 479 | // the responses. And since these are simple commands, even if the WNC |
fkellermavnet | 46:da9d788f5d5a | 480 | // is saying ERROR, treat it like a no response. |
fkellermavnet | 46:da9d788f5d5a | 481 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 43:6821a9c78c4b | 482 | return ; |
fkellermavnet | 46:da9d788f5d5a | 483 | } |
fkellermavnet | 42:be4b9ee3a615 | 484 | |
fkellermavnet | 46:da9d788f5d5a | 485 | if (intSet == false) |
fkellermavnet | 46:da9d788f5d5a | 486 | cmdRes = send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 487 | |
fkellermavnet | 46:da9d788f5d5a | 488 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 489 | intSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 490 | else |
fkellermavnet | 42:be4b9ee3a615 | 491 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 492 | |
fkellermavnet | 36:d4782eabff43 | 493 | if (pdnSet == false) |
fkellermavnet | 36:d4782eabff43 | 494 | { |
fkellermavnet | 36:d4782eabff43 | 495 | string cmd_str("AT%PDNSET=1,"); |
fkellermavnet | 36:d4782eabff43 | 496 | cmd_str += MY_APN_STR; |
fkellermavnet | 36:d4782eabff43 | 497 | cmd_str += ",IP"; |
fkellermavnet | 46:da9d788f5d5a | 498 | cmdRes = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes |
fkellermavnet | 42:be4b9ee3a615 | 499 | } |
fkellermavnet | 42:be4b9ee3a615 | 500 | |
fkellermavnet | 46:da9d788f5d5a | 501 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 502 | pdnSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 503 | else |
fkellermavnet | 42:be4b9ee3a615 | 504 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 505 | |
fkellermavnet | 42:be4b9ee3a615 | 506 | if (sockDialSet == false) |
fkellermavnet | 46:da9d788f5d5a | 507 | cmdRes = send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 508 | |
fkellermavnet | 46:da9d788f5d5a | 509 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 510 | sockDialSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 511 | else |
fkellermavnet | 42:be4b9ee3a615 | 512 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 513 | |
JMF | 67:11db02bb93e1 | 514 | PUTS("SUCCESS: AT init of WNC!\r\n"); |
JMF | 2:0e2ef866af95 | 515 | } |
JMF | 2:0e2ef866af95 | 516 | |
JMF | 2:0e2ef866af95 | 517 | void at_sockopen_wnc(const string & ipStr, const char * port ) |
JMF | 2:0e2ef866af95 | 518 | { |
JMF | 2:0e2ef866af95 | 519 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 520 | send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 521 | string cmd_str("AT@SOCKCONN=1,\""); |
JMF | 2:0e2ef866af95 | 522 | cmd_str += ipStr; |
JMF | 2:0e2ef866af95 | 523 | cmd_str += "\","; |
JMF | 2:0e2ef866af95 | 524 | cmd_str += port; |
fkellermavnet | 38:564b312a719f | 525 | cmd_str += ",30"; |
fkellermavnet | 73:da723fedfdd2 | 526 | int cmd = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000); |
fkellermavnet | 73:da723fedfdd2 | 527 | if (cmd != WNC_OK) { |
fkellermavnet | 73:da723fedfdd2 | 528 | // Per WNC: re-close even if open fails! |
fkellermavnet | 73:da723fedfdd2 | 529 | at_sockclose_wnc(); |
fkellermavnet | 73:da723fedfdd2 | 530 | } |
JMF | 2:0e2ef866af95 | 531 | } |
JMF | 2:0e2ef866af95 | 532 | |
JMF | 2:0e2ef866af95 | 533 | void at_sockclose_wnc(void) |
JMF | 2:0e2ef866af95 | 534 | { |
JMF | 2:0e2ef866af95 | 535 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 536 | send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 537 | } |
JMF | 2:0e2ef866af95 | 538 | |
JMF | 2:0e2ef866af95 | 539 | int at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 2:0e2ef866af95 | 540 | { |
JMF | 2:0e2ef866af95 | 541 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 542 | string str(s); |
fkellermavnet | 43:6821a9c78c4b | 543 | str = "AT@DNSRESVDON=\"" + str + "\""; |
fkellermavnet | 46:da9d788f5d5a | 544 | if (send_wnc_cmd(str.c_str(), &pRespStr, 15000) == 0) |
JMF | 2:0e2ef866af95 | 545 | { |
JMF | 2:0e2ef866af95 | 546 | size_t pos_start = pRespStr->find(":\"") + 2; |
fkellermavnet | 43:6821a9c78c4b | 547 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 43:6821a9c78c4b | 548 | if ((pos_start != string::npos) && (pos_end != string::npos)) |
JMF | 2:0e2ef866af95 | 549 | { |
JMF | 2:0e2ef866af95 | 550 | if (pos_end > pos_start) |
JMF | 2:0e2ef866af95 | 551 | { |
JMF | 2:0e2ef866af95 | 552 | // Make a copy for use later (the source string is re-used) |
JMF | 2:0e2ef866af95 | 553 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 2:0e2ef866af95 | 554 | return 1; |
JMF | 2:0e2ef866af95 | 555 | } |
JMF | 2:0e2ef866af95 | 556 | else |
JMF | 67:11db02bb93e1 | 557 | PUTS("URL Resolve fail, substr Err\r\n"); |
JMF | 2:0e2ef866af95 | 558 | } |
JMF | 2:0e2ef866af95 | 559 | else |
JMF | 67:11db02bb93e1 | 560 | PUTS("URL Resolve fail, no quotes\r\n"); |
JMF | 2:0e2ef866af95 | 561 | } |
JMF | 2:0e2ef866af95 | 562 | else |
JMF | 67:11db02bb93e1 | 563 | PUTS("URL Resolve fail, WNC cmd fail\r\n"); |
JMF | 2:0e2ef866af95 | 564 | |
fkellermavnet | 43:6821a9c78c4b | 565 | *ipStr = "192.168.0.1"; |
fkellermavnet | 43:6821a9c78c4b | 566 | |
JMF | 2:0e2ef866af95 | 567 | return -1; |
JMF | 2:0e2ef866af95 | 568 | } |
JMF | 2:0e2ef866af95 | 569 | |
JMF | 2:0e2ef866af95 | 570 | void at_sockwrite_wnc(const char * s) |
JMF | 2:0e2ef866af95 | 571 | { |
JMF | 2:0e2ef866af95 | 572 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 573 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 574 | size_t sLen = strlen(s); |
fkellermavnet | 73:da723fedfdd2 | 575 | int res; |
fkellermavnet | 39:1996eaec02d6 | 576 | if (sLen <= 1500) |
JMF | 2:0e2ef866af95 | 577 | { |
JMF | 2:0e2ef866af95 | 578 | string cmd_str("AT@SOCKWRITE=1,"); |
JMF | 2:0e2ef866af95 | 579 | itoa(sLen, num2str, 10); |
JMF | 2:0e2ef866af95 | 580 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 581 | cmd_str += ",\""; |
JMF | 2:0e2ef866af95 | 582 | while(*s != '\0') |
JMF | 2:0e2ef866af95 | 583 | { |
JMF | 2:0e2ef866af95 | 584 | itoa((int)*s++, num2str, 16); |
JMF | 2:0e2ef866af95 | 585 | // Always 2-digit ascii hex: |
JMF | 2:0e2ef866af95 | 586 | if (strlen(num2str) == 1) |
JMF | 2:0e2ef866af95 | 587 | { |
JMF | 2:0e2ef866af95 | 588 | num2str[2] = '\0'; |
JMF | 2:0e2ef866af95 | 589 | num2str[1] = num2str[0]; |
JMF | 2:0e2ef866af95 | 590 | num2str[0] = '0'; |
JMF | 2:0e2ef866af95 | 591 | } |
JMF | 2:0e2ef866af95 | 592 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 593 | } |
JMF | 2:0e2ef866af95 | 594 | cmd_str += "\""; |
fkellermavnet | 73:da723fedfdd2 | 595 | res = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 120000); |
fkellermavnet | 73:da723fedfdd2 | 596 | if (res == -3) |
fkellermavnet | 73:da723fedfdd2 | 597 | PUTS("sockwrite is disconnect \r\n"); |
JMF | 2:0e2ef866af95 | 598 | } |
JMF | 2:0e2ef866af95 | 599 | else |
JMF | 67:11db02bb93e1 | 600 | PUTS("sockwrite Err, string to long\r\n"); |
JMF | 2:0e2ef866af95 | 601 | } |
JMF | 2:0e2ef866af95 | 602 | |
JMF | 2:0e2ef866af95 | 603 | unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0) |
JMF | 2:0e2ef866af95 | 604 | { |
fkellermavnet | 34:1a4498e3580e | 605 | unsigned i, numBytes = 0; |
JMF | 2:0e2ef866af95 | 606 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 607 | string cmd_str("AT@SOCKREAD=1,"); |
fkellermavnet | 30:33be8e2992f3 | 608 | |
fkellermavnet | 44:c95a85b5cf92 | 609 | // Clean slate |
fkellermavnet | 44:c95a85b5cf92 | 610 | pS->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 611 | |
JMF | 2:0e2ef866af95 | 612 | if (n <= 1500) |
JMF | 2:0e2ef866af95 | 613 | { |
JMF | 2:0e2ef866af95 | 614 | char num2str[6]; |
fkellermavnet | 22:41e6c417ace1 | 615 | |
JMF | 2:0e2ef866af95 | 616 | itoa(n, num2str, 10); |
JMF | 2:0e2ef866af95 | 617 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 618 | retries += 1; |
JMF | 2:0e2ef866af95 | 619 | while (retries--) |
JMF | 2:0e2ef866af95 | 620 | { |
fkellermavnet | 22:41e6c417ace1 | 621 | // Assuming someone is sending then calling this to receive response, invoke |
fkellermavnet | 22:41e6c417ace1 | 622 | // a pause to give the response some time to come back and then also |
fkellermavnet | 22:41e6c417ace1 | 623 | // between each retry. |
fkellermavnet | 22:41e6c417ace1 | 624 | wait_ms(10); |
fkellermavnet | 22:41e6c417ace1 | 625 | |
fkellermavnet | 73:da723fedfdd2 | 626 | if (send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0) |
fkellermavnet | 73:da723fedfdd2 | 627 | { |
fkellermavnet | 73:da723fedfdd2 | 628 | size_t pos_start = pRespStr->find("\"") + 1; |
fkellermavnet | 73:da723fedfdd2 | 629 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 43:6821a9c78c4b | 630 | |
fkellermavnet | 73:da723fedfdd2 | 631 | // Make sure search finds what it's looking for! |
fkellermavnet | 73:da723fedfdd2 | 632 | if (pos_start != string::npos && pos_end != string::npos) |
fkellermavnet | 43:6821a9c78c4b | 633 | i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte |
fkellermavnet | 73:da723fedfdd2 | 634 | else |
fkellermavnet | 43:6821a9c78c4b | 635 | i = 0; |
fkellermavnet | 43:6821a9c78c4b | 636 | |
fkellermavnet | 73:da723fedfdd2 | 637 | if (i > 0) |
fkellermavnet | 73:da723fedfdd2 | 638 | { |
fkellermavnet | 73:da723fedfdd2 | 639 | retries = 1; // If any data found retry 1 more time to catch data that might be in another |
fkellermavnet | 73:da723fedfdd2 | 640 | // WNC payload |
fkellermavnet | 73:da723fedfdd2 | 641 | string byte; |
fkellermavnet | 73:da723fedfdd2 | 642 | while (pos_start < pos_end) |
fkellermavnet | 73:da723fedfdd2 | 643 | { |
fkellermavnet | 73:da723fedfdd2 | 644 | byte = pRespStr->substr(pos_start, 2); |
fkellermavnet | 73:da723fedfdd2 | 645 | *pS += (char)strtol(byte.c_str(), NULL, 16); |
fkellermavnet | 73:da723fedfdd2 | 646 | pos_start += 2; |
fkellermavnet | 73:da723fedfdd2 | 647 | } |
fkellermavnet | 73:da723fedfdd2 | 648 | numBytes += i/2; |
fkellermavnet | 73:da723fedfdd2 | 649 | } |
fkellermavnet | 73:da723fedfdd2 | 650 | } |
fkellermavnet | 73:da723fedfdd2 | 651 | else |
JMF | 2:0e2ef866af95 | 652 | { |
fkellermavnet | 73:da723fedfdd2 | 653 | PUTS("no readsock reply!\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 654 | return (0); |
JMF | 2:0e2ef866af95 | 655 | } |
JMF | 2:0e2ef866af95 | 656 | } |
JMF | 2:0e2ef866af95 | 657 | } |
JMF | 2:0e2ef866af95 | 658 | else |
JMF | 67:11db02bb93e1 | 659 | PUTS("sockread Err, to many to read\r\n"); |
JMF | 2:0e2ef866af95 | 660 | |
fkellermavnet | 43:6821a9c78c4b | 661 | return (numBytes); |
JMF | 2:0e2ef866af95 | 662 | } |