![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.
Dependencies: mbed FXOS8700CQ MODSERIAL
wnc_control.cpp@74:3e3ee15584e5, 2016-08-13 (annotated)
- Committer:
- fkellermavnet
- Date:
- Sat Aug 13 15:08:11 2016 +0000
- Revision:
- 74:3e3ee15584e5
- Parent:
- 68:6e311c747045
- Child:
- 75:8cc98a3b9c62
Merged in latest code from Eaddy. This adds in more checks if the socket disconnects and attempts to reconnect it and also to search for the +CME error condition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fkellermavnet | 68:6e311c747045 | 1 | /* =================================================================== |
fkellermavnet | 74:3e3ee15584e5 | 2 | Copyright c 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 | 74:3e3ee15584e5 | 40 | WNC_CELL_LINK_DOWN = -3, |
fkellermavnet | 74:3e3ee15584e5 | 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 |
JMF | 67:11db02bb93e1 | 62 | PUTS(cmd_str.c_str()); |
fkellermavnet | 66:b2425419b0cc | 63 | } |
fkellermavnet | 66:b2425419b0cc | 64 | |
fkellermavnet | 42:be4b9ee3a615 | 65 | static bool reportStatus = true; |
JMF | 2:0e2ef866af95 | 66 | do |
JMF | 2:0e2ef866af95 | 67 | { |
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(); |
fkellermavnet | 74:3e3ee15584e5 | 160 | }else if (WNC_MDM_ERR == WNC_EXTERR) |
fkellermavnet | 74:3e3ee15584e5 | 161 | { |
fkellermavnet | 74:3e3ee15584e5 | 162 | PUTS("Socket Disconnected (broken) !!!\r\n"); |
fkellermavnet | 74:3e3ee15584e5 | 163 | sockclose_mdm(); |
fkellermavnet | 74:3e3ee15584e5 | 164 | sockopen_mdm(); |
fkellermavnet | 74:3e3ee15584e5 | 165 | //software_init_mdm(); |
JMF | 2:0e2ef866af95 | 166 | } |
JMF | 2:0e2ef866af95 | 167 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 168 | } |
fkellermavnet | 10:df54436ecd38 | 169 | else |
JMF | 67:11db02bb93e1 | 170 | PUTS("Socket is closed for write!\r\n"); |
JMF | 2:0e2ef866af95 | 171 | } |
JMF | 2:0e2ef866af95 | 172 | |
fkellermavnet | 30:33be8e2992f3 | 173 | unsigned sockread_mdm(string * sockData, int len, int retries) |
JMF | 2:0e2ef866af95 | 174 | { |
fkellermavnet | 30:33be8e2992f3 | 175 | unsigned n = 0; |
fkellermavnet | 30:33be8e2992f3 | 176 | |
fkellermavnet | 10:df54436ecd38 | 177 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 178 | { |
JMF | 2:0e2ef866af95 | 179 | do |
JMF | 2:0e2ef866af95 | 180 | { |
JMF | 2:0e2ef866af95 | 181 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 30:33be8e2992f3 | 182 | n = at_sockread_wnc(sockData, len, retries); |
JMF | 2:0e2ef866af95 | 183 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 184 | { |
fkellermavnet | 30:33be8e2992f3 | 185 | if (n == 0) |
fkellermavnet | 30:33be8e2992f3 | 186 | software_init_mdm(); |
fkellermavnet | 30:33be8e2992f3 | 187 | else |
JMF | 67:11db02bb93e1 | 188 | PUTS("Sock read partial data!!!\r\n"); |
JMF | 2:0e2ef866af95 | 189 | } |
JMF | 2:0e2ef866af95 | 190 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 191 | PUTS("Sock read fail!!!!\r\n"); |
fkellermavnet | 30:33be8e2992f3 | 192 | } while (WNC_MDM_ERR == WNC_NO_RESPONSE); |
fkellermavnet | 10:df54436ecd38 | 193 | } |
fkellermavnet | 10:df54436ecd38 | 194 | else |
fkellermavnet | 44:c95a85b5cf92 | 195 | { |
JMF | 67:11db02bb93e1 | 196 | PUTS("Socket is closed for read\r\n"); |
fkellermavnet | 44:c95a85b5cf92 | 197 | sockData->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 198 | } |
fkellermavnet | 30:33be8e2992f3 | 199 | |
fkellermavnet | 30:33be8e2992f3 | 200 | return (n); |
JMF | 2:0e2ef866af95 | 201 | } |
JMF | 2:0e2ef866af95 | 202 | |
JMF | 2:0e2ef866af95 | 203 | void sockclose_mdm(void) |
JMF | 2:0e2ef866af95 | 204 | { |
JMF | 2:0e2ef866af95 | 205 | do |
JMF | 2:0e2ef866af95 | 206 | { |
JMF | 2:0e2ef866af95 | 207 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 208 | at_sockclose_wnc(); |
fkellermavnet | 10:df54436ecd38 | 209 | // Assume close happened even if it went bad |
fkellermavnet | 10:df54436ecd38 | 210 | // going bad will result in a re-init anyways and if close |
fkellermavnet | 10:df54436ecd38 | 211 | // fails we're pretty much in bad state and not much can do |
fkellermavnet | 10:df54436ecd38 | 212 | socketOpen = 0; |
JMF | 2:0e2ef866af95 | 213 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 214 | { |
JMF | 2:0e2ef866af95 | 215 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 216 | } |
JMF | 2:0e2ef866af95 | 217 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 218 | PUTS("Sock close fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 219 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 220 | } |
JMF | 2:0e2ef866af95 | 221 | |
JMF | 2:0e2ef866af95 | 222 | /** |
JMF | 2:0e2ef866af95 | 223 | * C++ version 0.4 char* style "itoa": |
fkellermavnet | 74:3e3ee15584e5 | 224 | * Written by Lukas Chmela |
JMF | 2:0e2ef866af95 | 225 | * Released under GPLv3. |
JMF | 2:0e2ef866af95 | 226 | */ |
JMF | 2:0e2ef866af95 | 227 | |
JMF | 2:0e2ef866af95 | 228 | char* itoa(int value, char* result, int base) |
JMF | 2:0e2ef866af95 | 229 | { |
JMF | 2:0e2ef866af95 | 230 | // check that the base if valid |
JMF | 2:0e2ef866af95 | 231 | if ( base < 2 || base > 36 ) { |
JMF | 2:0e2ef866af95 | 232 | *result = '\0'; |
JMF | 2:0e2ef866af95 | 233 | return result; |
JMF | 2:0e2ef866af95 | 234 | } |
JMF | 2:0e2ef866af95 | 235 | |
JMF | 2:0e2ef866af95 | 236 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 2:0e2ef866af95 | 237 | int tmp_value; |
JMF | 2:0e2ef866af95 | 238 | |
JMF | 2:0e2ef866af95 | 239 | do { |
JMF | 2:0e2ef866af95 | 240 | tmp_value = value; |
JMF | 2:0e2ef866af95 | 241 | value /= base; |
JMF | 2:0e2ef866af95 | 242 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 2:0e2ef866af95 | 243 | } while ( value ); |
JMF | 2:0e2ef866af95 | 244 | |
JMF | 2:0e2ef866af95 | 245 | // Apply negative sign |
JMF | 2:0e2ef866af95 | 246 | if ( tmp_value < 0 ) |
JMF | 2:0e2ef866af95 | 247 | *ptr++ = '-'; |
JMF | 2:0e2ef866af95 | 248 | *ptr-- = '\0'; |
JMF | 2:0e2ef866af95 | 249 | |
JMF | 2:0e2ef866af95 | 250 | while ( ptr1 < ptr ) { |
JMF | 2:0e2ef866af95 | 251 | tmp_char = *ptr; |
JMF | 2:0e2ef866af95 | 252 | *ptr-- = *ptr1; |
JMF | 2:0e2ef866af95 | 253 | *ptr1++ = tmp_char; |
JMF | 2:0e2ef866af95 | 254 | } |
JMF | 2:0e2ef866af95 | 255 | |
JMF | 2:0e2ef866af95 | 256 | return result; |
JMF | 2:0e2ef866af95 | 257 | } |
JMF | 2:0e2ef866af95 | 258 | |
JMF | 2:0e2ef866af95 | 259 | extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len); |
JMF | 2:0e2ef866af95 | 260 | |
fkellermavnet | 42:be4b9ee3a615 | 261 | int check_wnc_ready(void) |
fkellermavnet | 42:be4b9ee3a615 | 262 | { |
fkellermavnet | 43:6821a9c78c4b | 263 | string * pRespStr; |
fkellermavnet | 43:6821a9c78c4b | 264 | size_t pos; |
fkellermavnet | 43:6821a9c78c4b | 265 | int regSts; |
fkellermavnet | 46:da9d788f5d5a | 266 | int cmdRes1, cmdRes2; |
fkellermavnet | 42:be4b9ee3a615 | 267 | |
fkellermavnet | 53:dcccf2881fa0 | 268 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 269 | PUTS("<-------- Begin Cell Status ------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 270 | #endif |
fkellermavnet | 46:da9d788f5d5a | 271 | cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS); // Check RSSI,BER |
fkellermavnet | 46:da9d788f5d5a | 272 | cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS); // Check if SIM locked |
fkellermavnet | 46:da9d788f5d5a | 273 | |
fkellermavnet | 46:da9d788f5d5a | 274 | if ((cmdRes1 != 0) && (cmdRes2 != 0)) |
fkellermavnet | 43:6821a9c78c4b | 275 | { |
fkellermavnet | 53:dcccf2881fa0 | 276 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 277 | PUTS("------------ WNC No Response! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 278 | #endif |
fkellermavnet | 43:6821a9c78c4b | 279 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 280 | } |
fkellermavnet | 42:be4b9ee3a615 | 281 | |
fkellermavnet | 43:6821a9c78c4b | 282 | // If SIM Card not ready don't bother with commands! |
fkellermavnet | 43:6821a9c78c4b | 283 | if (pRespStr->find("CPIN: READY") == string::npos) |
fkellermavnet | 43:6821a9c78c4b | 284 | { |
fkellermavnet | 53:dcccf2881fa0 | 285 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 286 | PUTS("------------ WNC SIM Problem! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 287 | #endif |
fkellermavnet | 43:6821a9c78c4b | 288 | return (-1); |
fkellermavnet | 43:6821a9c78c4b | 289 | } |
fkellermavnet | 43:6821a9c78c4b | 290 | |
fkellermavnet | 43:6821a9c78c4b | 291 | // SIM card OK, now check for signal and cellular network registration |
fkellermavnet | 46:da9d788f5d5a | 292 | cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS); // Check if registered on network |
fkellermavnet | 42:be4b9ee3a615 | 293 | pos = pRespStr->find("CREG: "); |
fkellermavnet | 42:be4b9ee3a615 | 294 | if (pos != string::npos) |
fkellermavnet | 42:be4b9ee3a615 | 295 | { |
fkellermavnet | 43:6821a9c78c4b | 296 | // The registration is the 2nd arg in the comma separated list |
fkellermavnet | 43:6821a9c78c4b | 297 | *pRespStr = pRespStr->substr(pos+8, 1); |
fkellermavnet | 43:6821a9c78c4b | 298 | regSts = atoi(pRespStr->c_str()); |
fkellermavnet | 43:6821a9c78c4b | 299 | // 1 - registered home, 5 - registered roaming |
fkellermavnet | 43:6821a9c78c4b | 300 | if ((regSts != 1) && (regSts != 5)) |
fkellermavnet | 43:6821a9c78c4b | 301 | { |
fkellermavnet | 53:dcccf2881fa0 | 302 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 303 | PUTS("------------ WNC Cell Link Down! ------>\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 304 | #endif |
fkellermavnet | 43:6821a9c78c4b | 305 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 306 | } |
fkellermavnet | 42:be4b9ee3a615 | 307 | } |
fkellermavnet | 43:6821a9c78c4b | 308 | |
fkellermavnet | 53:dcccf2881fa0 | 309 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 310 | PUTS("------------ WNC Ready ---------------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 311 | #endif |
fkellermavnet | 42:be4b9ee3a615 | 312 | |
fkellermavnet | 43:6821a9c78c4b | 313 | return (0); |
fkellermavnet | 42:be4b9ee3a615 | 314 | } |
fkellermavnet | 42:be4b9ee3a615 | 315 | |
JMF | 2:0e2ef866af95 | 316 | // Sets a global with failure or success, assumes 1 thread all the time |
JMF | 2:0e2ef866af95 | 317 | int send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 2:0e2ef866af95 | 318 | { |
fkellermavnet | 46:da9d788f5d5a | 319 | int cmdRes; |
fkellermavnet | 46:da9d788f5d5a | 320 | |
fkellermavnet | 42:be4b9ee3a615 | 321 | if (check_wnc_ready() < 0) |
fkellermavnet | 42:be4b9ee3a615 | 322 | { |
fkellermavnet | 45:a836eecd5d12 | 323 | static string noRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 324 | |
fkellermavnet | 53:dcccf2881fa0 | 325 | #ifdef WNC_CMD_DEBUG_ON |
JMF | 67:11db02bb93e1 | 326 | PUTS("FAIL send cmd: "); |
fkellermavnet | 53:dcccf2881fa0 | 327 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 328 | PUTS(s); |
fkellermavnet | 53:dcccf2881fa0 | 329 | #else |
fkellermavnet | 53:dcccf2881fa0 | 330 | string truncStr(s, 50); |
fkellermavnet | 53:dcccf2881fa0 | 331 | truncStr += "\r\n"; |
JMF | 67:11db02bb93e1 | 332 | PUTS(truncStr.c_str()); |
fkellermavnet | 53:dcccf2881fa0 | 333 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 334 | #else |
JMF | 67:11db02bb93e1 | 335 | PUTS("FAIL send cmd!\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 336 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 337 | |
fkellermavnet | 46:da9d788f5d5a | 338 | WNC_MDM_ERR = WNC_CELL_LINK_DOWN; |
fkellermavnet | 45:a836eecd5d12 | 339 | noRespStr.erase(); |
fkellermavnet | 44:c95a85b5cf92 | 340 | *r = &noRespStr; |
fkellermavnet | 46:da9d788f5d5a | 341 | return (-3); |
fkellermavnet | 42:be4b9ee3a615 | 342 | } |
fkellermavnet | 53:dcccf2881fa0 | 343 | |
fkellermavnet | 53:dcccf2881fa0 | 344 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 345 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 346 | PUTS("[---------- Network Status -------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 347 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 348 | string * pRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 349 | at_send_wnc_cmd("AT@SOCKDIAL?", &pRespStr, 5000); |
fkellermavnet | 53:dcccf2881fa0 | 350 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 351 | PUTS("---------------------------------------]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 352 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 353 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 354 | |
fkellermavnet | 42:be4b9ee3a615 | 355 | // If WNC ready, send user command |
fkellermavnet | 46:da9d788f5d5a | 356 | cmdRes = at_send_wnc_cmd(s, r, ms_timeout); |
fkellermavnet | 46:da9d788f5d5a | 357 | |
fkellermavnet | 46:da9d788f5d5a | 358 | if (cmdRes == -1) |
fkellermavnet | 46:da9d788f5d5a | 359 | WNC_MDM_ERR = WNC_CMD_ERR; |
fkellermavnet | 46:da9d788f5d5a | 360 | |
fkellermavnet | 46:da9d788f5d5a | 361 | if (cmdRes == -2) |
fkellermavnet | 46:da9d788f5d5a | 362 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 74:3e3ee15584e5 | 363 | |
fkellermavnet | 74:3e3ee15584e5 | 364 | if (cmdRes == -3) { |
fkellermavnet | 74:3e3ee15584e5 | 365 | WNC_MDM_ERR = WNC_EXTERR; |
fkellermavnet | 74:3e3ee15584e5 | 366 | pc.printf("[[WNC_MDM_ERR = WNC_EXTERR]] \r\n"); |
fkellermavnet | 74:3e3ee15584e5 | 367 | } |
fkellermavnet | 46:da9d788f5d5a | 368 | |
fkellermavnet | 46:da9d788f5d5a | 369 | if (cmdRes == 0) |
fkellermavnet | 46:da9d788f5d5a | 370 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 46:da9d788f5d5a | 371 | |
fkellermavnet | 46:da9d788f5d5a | 372 | return (cmdRes); |
fkellermavnet | 42:be4b9ee3a615 | 373 | } |
fkellermavnet | 42:be4b9ee3a615 | 374 | |
fkellermavnet | 42:be4b9ee3a615 | 375 | int at_send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
fkellermavnet | 42:be4b9ee3a615 | 376 | { |
fkellermavnet | 74:3e3ee15584e5 | 377 | //Eaddy |
fkellermavnet | 74:3e3ee15584e5 | 378 | static const char * rsp_lst[] = { "OK", "ERROR","@EXTERR", "+CME",NULL }; |
fkellermavnet | 74:3e3ee15584e5 | 379 | //static const char * rsp_lst[] = { "OK", "ERROR", NULL }; |
JMF | 2:0e2ef866af95 | 380 | int len; |
fkellermavnet | 53:dcccf2881fa0 | 381 | |
fkellermavnet | 53:dcccf2881fa0 | 382 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 383 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
fkellermavnet | 53:dcccf2881fa0 | 384 | |
fkellermavnet | 53:dcccf2881fa0 | 385 | #else |
fkellermavnet | 42:be4b9ee3a615 | 386 | if (strlen(s) > 60) |
fkellermavnet | 42:be4b9ee3a615 | 387 | { |
fkellermavnet | 42:be4b9ee3a615 | 388 | string truncStr(s,57); |
fkellermavnet | 42:be4b9ee3a615 | 389 | truncStr += "..."; |
stefanrousseau | 64:09004cd610df | 390 | PRINTF("Send: <<%s>>\r\n",truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 391 | } |
fkellermavnet | 42:be4b9ee3a615 | 392 | else |
fkellermavnet | 53:dcccf2881fa0 | 393 | #endif |
stefanrousseau | 64:09004cd610df | 394 | PRINTF("Send: <<%s>>\r\n",s); |
fkellermavnet | 53:dcccf2881fa0 | 395 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 396 | |
JMF | 2:0e2ef866af95 | 397 | int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len); |
JMF | 2:0e2ef866af95 | 398 | *r = &wncStr; // Return a pointer to the static string |
JMF | 2:0e2ef866af95 | 399 | |
JMF | 2:0e2ef866af95 | 400 | if (res >= 0) |
fkellermavnet | 53:dcccf2881fa0 | 401 | { |
fkellermavnet | 53:dcccf2881fa0 | 402 | |
fkellermavnet | 53:dcccf2881fa0 | 403 | #ifdef WNC_CMD_DEBUG_ON |
JMF | 67:11db02bb93e1 | 404 | PUTS("["); |
fkellermavnet | 53:dcccf2881fa0 | 405 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 406 | PUTS(wncStr.c_str()); |
JMF | 67:11db02bb93e1 | 407 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 408 | #else |
fkellermavnet | 42:be4b9ee3a615 | 409 | if (wncStr.size() < 51) |
JMF | 67:11db02bb93e1 | 410 | PUTS(wncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 411 | else |
fkellermavnet | 42:be4b9ee3a615 | 412 | { |
fkellermavnet | 42:be4b9ee3a615 | 413 | string truncStr = wncStr.substr(0,50) + "..."; |
JMF | 67:11db02bb93e1 | 414 | PUTS(truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 415 | } |
JMF | 67:11db02bb93e1 | 416 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 417 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 418 | #endif |
fkellermavnet | 74:3e3ee15584e5 | 419 | #if 0 |
JMF | 2:0e2ef866af95 | 420 | if (res > 0) |
JMF | 2:0e2ef866af95 | 421 | return -1; |
JMF | 2:0e2ef866af95 | 422 | else |
JMF | 2:0e2ef866af95 | 423 | return 0; |
fkellermavnet | 74:3e3ee15584e5 | 424 | #else |
fkellermavnet | 74:3e3ee15584e5 | 425 | //Eaddy added |
fkellermavnet | 74:3e3ee15584e5 | 426 | if (res == 0) { |
fkellermavnet | 74:3e3ee15584e5 | 427 | /* OK */ |
fkellermavnet | 74:3e3ee15584e5 | 428 | return 0; |
fkellermavnet | 74:3e3ee15584e5 | 429 | } else if (res == 2) { |
fkellermavnet | 74:3e3ee15584e5 | 430 | /* @EXTERR */ |
fkellermavnet | 74:3e3ee15584e5 | 431 | pc.printf("@EXTERR and res = %d \r\n", res); |
fkellermavnet | 74:3e3ee15584e5 | 432 | return -3; |
fkellermavnet | 74:3e3ee15584e5 | 433 | } else |
fkellermavnet | 74:3e3ee15584e5 | 434 | return -1; |
fkellermavnet | 74:3e3ee15584e5 | 435 | #endif |
JMF | 2:0e2ef866af95 | 436 | } |
JMF | 2:0e2ef866af95 | 437 | else |
JMF | 2:0e2ef866af95 | 438 | { |
JMF | 67:11db02bb93e1 | 439 | PUTS("No response from WNC!\n\r"); |
JMF | 2:0e2ef866af95 | 440 | return -2; |
JMF | 2:0e2ef866af95 | 441 | } |
JMF | 2:0e2ef866af95 | 442 | } |
JMF | 2:0e2ef866af95 | 443 | |
fkellermavnet | 42:be4b9ee3a615 | 444 | |
fkellermavnet | 19:f89baed3bd6f | 445 | void at_at_wnc(void) |
fkellermavnet | 19:f89baed3bd6f | 446 | { |
fkellermavnet | 19:f89baed3bd6f | 447 | string * pRespStr; |
fkellermavnet | 19:f89baed3bd6f | 448 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 19:f89baed3bd6f | 449 | } |
fkellermavnet | 19:f89baed3bd6f | 450 | |
fkellermavnet | 42:be4b9ee3a615 | 451 | void at_init_wnc(bool hardReset) |
JMF | 2:0e2ef866af95 | 452 | { |
fkellermavnet | 36:d4782eabff43 | 453 | static bool pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 454 | static bool intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 455 | static bool sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 456 | string * pRespStr; |
fkellermavnet | 46:da9d788f5d5a | 457 | int cmdRes; |
fkellermavnet | 36:d4782eabff43 | 458 | |
fkellermavnet | 42:be4b9ee3a615 | 459 | if (hardReset == true) |
fkellermavnet | 42:be4b9ee3a615 | 460 | { |
JMF | 67:11db02bb93e1 | 461 | PUTS("Hard Reset!\r\n"); |
fkellermavnet | 42:be4b9ee3a615 | 462 | pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 463 | intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 464 | sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 465 | } |
fkellermavnet | 42:be4b9ee3a615 | 466 | |
JMF | 67:11db02bb93e1 | 467 | PUTS("Start AT init of WNC:\r\n"); |
fkellermavnet | 43:6821a9c78c4b | 468 | // Quick commands below do not need to check cellular connectivity |
fkellermavnet | 46:da9d788f5d5a | 469 | cmdRes = at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 46:da9d788f5d5a | 470 | cmdRes += at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS); // Echo Off |
fkellermavnet | 46:da9d788f5d5a | 471 | cmdRes += at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS); // 2 - verbose error, 1 - numeric error, 0 - just ERROR |
fkellermavnet | 43:6821a9c78c4b | 472 | |
fkellermavnet | 43:6821a9c78c4b | 473 | // If the simple commands are not working no chance of more complex. |
fkellermavnet | 43:6821a9c78c4b | 474 | // I have seen re-trying commands make it worse. |
fkellermavnet | 46:da9d788f5d5a | 475 | if (cmdRes < 0) |
fkellermavnet | 46:da9d788f5d5a | 476 | { |
fkellermavnet | 46:da9d788f5d5a | 477 | // Since I used the at_send_wnc_cmd I am setting the error state based upon |
fkellermavnet | 46:da9d788f5d5a | 478 | // the responses. And since these are simple commands, even if the WNC |
fkellermavnet | 46:da9d788f5d5a | 479 | // is saying ERROR, treat it like a no response. |
fkellermavnet | 46:da9d788f5d5a | 480 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 43:6821a9c78c4b | 481 | return ; |
fkellermavnet | 46:da9d788f5d5a | 482 | } |
fkellermavnet | 42:be4b9ee3a615 | 483 | |
fkellermavnet | 46:da9d788f5d5a | 484 | if (intSet == false) |
fkellermavnet | 46:da9d788f5d5a | 485 | cmdRes = send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 486 | |
fkellermavnet | 46:da9d788f5d5a | 487 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 488 | intSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 489 | else |
fkellermavnet | 42:be4b9ee3a615 | 490 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 491 | |
fkellermavnet | 36:d4782eabff43 | 492 | if (pdnSet == false) |
fkellermavnet | 36:d4782eabff43 | 493 | { |
fkellermavnet | 36:d4782eabff43 | 494 | string cmd_str("AT%PDNSET=1,"); |
fkellermavnet | 36:d4782eabff43 | 495 | cmd_str += MY_APN_STR; |
fkellermavnet | 36:d4782eabff43 | 496 | cmd_str += ",IP"; |
fkellermavnet | 46:da9d788f5d5a | 497 | 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 | 498 | } |
fkellermavnet | 42:be4b9ee3a615 | 499 | |
fkellermavnet | 46:da9d788f5d5a | 500 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 501 | pdnSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 502 | else |
fkellermavnet | 42:be4b9ee3a615 | 503 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 504 | |
fkellermavnet | 42:be4b9ee3a615 | 505 | if (sockDialSet == false) |
fkellermavnet | 46:da9d788f5d5a | 506 | cmdRes = send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 507 | |
fkellermavnet | 46:da9d788f5d5a | 508 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 509 | sockDialSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 510 | else |
fkellermavnet | 42:be4b9ee3a615 | 511 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 512 | |
JMF | 67:11db02bb93e1 | 513 | PUTS("SUCCESS: AT init of WNC!\r\n"); |
JMF | 2:0e2ef866af95 | 514 | } |
JMF | 2:0e2ef866af95 | 515 | |
JMF | 2:0e2ef866af95 | 516 | void at_sockopen_wnc(const string & ipStr, const char * port ) |
JMF | 2:0e2ef866af95 | 517 | { |
JMF | 2:0e2ef866af95 | 518 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 519 | send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 520 | string cmd_str("AT@SOCKCONN=1,\""); |
JMF | 2:0e2ef866af95 | 521 | cmd_str += ipStr; |
JMF | 2:0e2ef866af95 | 522 | cmd_str += "\","; |
JMF | 2:0e2ef866af95 | 523 | cmd_str += port; |
fkellermavnet | 38:564b312a719f | 524 | cmd_str += ",30"; |
fkellermavnet | 39:1996eaec02d6 | 525 | send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000); |
JMF | 2:0e2ef866af95 | 526 | } |
JMF | 2:0e2ef866af95 | 527 | |
JMF | 2:0e2ef866af95 | 528 | void at_sockclose_wnc(void) |
JMF | 2:0e2ef866af95 | 529 | { |
JMF | 2:0e2ef866af95 | 530 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 531 | send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 532 | } |
JMF | 2:0e2ef866af95 | 533 | |
JMF | 2:0e2ef866af95 | 534 | int at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 2:0e2ef866af95 | 535 | { |
JMF | 2:0e2ef866af95 | 536 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 537 | string str(s); |
fkellermavnet | 43:6821a9c78c4b | 538 | str = "AT@DNSRESVDON=\"" + str + "\""; |
fkellermavnet | 46:da9d788f5d5a | 539 | if (send_wnc_cmd(str.c_str(), &pRespStr, 15000) == 0) |
JMF | 2:0e2ef866af95 | 540 | { |
JMF | 2:0e2ef866af95 | 541 | size_t pos_start = pRespStr->find(":\"") + 2; |
fkellermavnet | 43:6821a9c78c4b | 542 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 43:6821a9c78c4b | 543 | if ((pos_start != string::npos) && (pos_end != string::npos)) |
JMF | 2:0e2ef866af95 | 544 | { |
JMF | 2:0e2ef866af95 | 545 | if (pos_end > pos_start) |
JMF | 2:0e2ef866af95 | 546 | { |
JMF | 2:0e2ef866af95 | 547 | // Make a copy for use later (the source string is re-used) |
JMF | 2:0e2ef866af95 | 548 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 2:0e2ef866af95 | 549 | return 1; |
JMF | 2:0e2ef866af95 | 550 | } |
JMF | 2:0e2ef866af95 | 551 | else |
JMF | 67:11db02bb93e1 | 552 | PUTS("URL Resolve fail, substr Err\r\n"); |
JMF | 2:0e2ef866af95 | 553 | } |
JMF | 2:0e2ef866af95 | 554 | else |
JMF | 67:11db02bb93e1 | 555 | PUTS("URL Resolve fail, no quotes\r\n"); |
JMF | 2:0e2ef866af95 | 556 | } |
JMF | 2:0e2ef866af95 | 557 | else |
JMF | 67:11db02bb93e1 | 558 | PUTS("URL Resolve fail, WNC cmd fail\r\n"); |
JMF | 2:0e2ef866af95 | 559 | |
fkellermavnet | 43:6821a9c78c4b | 560 | *ipStr = "192.168.0.1"; |
fkellermavnet | 43:6821a9c78c4b | 561 | |
JMF | 2:0e2ef866af95 | 562 | return -1; |
JMF | 2:0e2ef866af95 | 563 | } |
JMF | 2:0e2ef866af95 | 564 | |
JMF | 2:0e2ef866af95 | 565 | void at_sockwrite_wnc(const char * s) |
JMF | 2:0e2ef866af95 | 566 | { |
JMF | 2:0e2ef866af95 | 567 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 568 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 569 | size_t sLen = strlen(s); |
fkellermavnet | 74:3e3ee15584e5 | 570 | int res; |
fkellermavnet | 39:1996eaec02d6 | 571 | if (sLen <= 1500) |
JMF | 2:0e2ef866af95 | 572 | { |
JMF | 2:0e2ef866af95 | 573 | string cmd_str("AT@SOCKWRITE=1,"); |
JMF | 2:0e2ef866af95 | 574 | itoa(sLen, num2str, 10); |
JMF | 2:0e2ef866af95 | 575 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 576 | cmd_str += ",\""; |
JMF | 2:0e2ef866af95 | 577 | while(*s != '\0') |
JMF | 2:0e2ef866af95 | 578 | { |
JMF | 2:0e2ef866af95 | 579 | itoa((int)*s++, num2str, 16); |
JMF | 2:0e2ef866af95 | 580 | // Always 2-digit ascii hex: |
JMF | 2:0e2ef866af95 | 581 | if (strlen(num2str) == 1) |
JMF | 2:0e2ef866af95 | 582 | { |
JMF | 2:0e2ef866af95 | 583 | num2str[2] = '\0'; |
JMF | 2:0e2ef866af95 | 584 | num2str[1] = num2str[0]; |
JMF | 2:0e2ef866af95 | 585 | num2str[0] = '0'; |
JMF | 2:0e2ef866af95 | 586 | } |
JMF | 2:0e2ef866af95 | 587 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 588 | } |
JMF | 2:0e2ef866af95 | 589 | cmd_str += "\""; |
fkellermavnet | 74:3e3ee15584e5 | 590 | res = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 120000); |
fkellermavnet | 74:3e3ee15584e5 | 591 | |
fkellermavnet | 74:3e3ee15584e5 | 592 | if (res == -3) |
fkellermavnet | 74:3e3ee15584e5 | 593 | PUTS("sockwrite is disconnect \r\n"); |
JMF | 2:0e2ef866af95 | 594 | } |
JMF | 2:0e2ef866af95 | 595 | else |
JMF | 67:11db02bb93e1 | 596 | PUTS("sockwrite Err, string to long\r\n"); |
JMF | 2:0e2ef866af95 | 597 | } |
JMF | 2:0e2ef866af95 | 598 | |
JMF | 2:0e2ef866af95 | 599 | unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0) |
JMF | 2:0e2ef866af95 | 600 | { |
fkellermavnet | 34:1a4498e3580e | 601 | unsigned i, numBytes = 0; |
JMF | 2:0e2ef866af95 | 602 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 603 | string cmd_str("AT@SOCKREAD=1,"); |
fkellermavnet | 30:33be8e2992f3 | 604 | |
fkellermavnet | 44:c95a85b5cf92 | 605 | // Clean slate |
fkellermavnet | 44:c95a85b5cf92 | 606 | pS->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 607 | |
JMF | 2:0e2ef866af95 | 608 | if (n <= 1500) |
JMF | 2:0e2ef866af95 | 609 | { |
JMF | 2:0e2ef866af95 | 610 | char num2str[6]; |
fkellermavnet | 22:41e6c417ace1 | 611 | |
JMF | 2:0e2ef866af95 | 612 | itoa(n, num2str, 10); |
JMF | 2:0e2ef866af95 | 613 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 614 | retries += 1; |
JMF | 2:0e2ef866af95 | 615 | while (retries--) |
JMF | 2:0e2ef866af95 | 616 | { |
fkellermavnet | 22:41e6c417ace1 | 617 | // Assuming someone is sending then calling this to receive response, invoke |
fkellermavnet | 22:41e6c417ace1 | 618 | // a pause to give the response some time to come back and then also |
fkellermavnet | 22:41e6c417ace1 | 619 | // between each retry. |
fkellermavnet | 22:41e6c417ace1 | 620 | wait_ms(10); |
fkellermavnet | 22:41e6c417ace1 | 621 | |
fkellermavnet | 30:33be8e2992f3 | 622 | send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 43:6821a9c78c4b | 623 | |
JMF | 2:0e2ef866af95 | 624 | size_t pos_start = pRespStr->find("\"") + 1; |
JMF | 2:0e2ef866af95 | 625 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 43:6821a9c78c4b | 626 | |
fkellermavnet | 43:6821a9c78c4b | 627 | // Make sure search finds what it's looking for! |
fkellermavnet | 43:6821a9c78c4b | 628 | if (pos_start != string::npos && pos_end != string::npos) |
fkellermavnet | 43:6821a9c78c4b | 629 | i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte |
fkellermavnet | 43:6821a9c78c4b | 630 | else |
fkellermavnet | 43:6821a9c78c4b | 631 | i = 0; |
fkellermavnet | 43:6821a9c78c4b | 632 | |
JMF | 2:0e2ef866af95 | 633 | if (i > 0) |
JMF | 2:0e2ef866af95 | 634 | { |
fkellermavnet | 34:1a4498e3580e | 635 | retries = 1; // If any data found retry 1 more time to catch data that might be in another |
fkellermavnet | 34:1a4498e3580e | 636 | // WNC payload |
JMF | 2:0e2ef866af95 | 637 | string byte; |
JMF | 2:0e2ef866af95 | 638 | while (pos_start < pos_end) |
JMF | 2:0e2ef866af95 | 639 | { |
JMF | 2:0e2ef866af95 | 640 | byte = pRespStr->substr(pos_start, 2); |
fkellermavnet | 30:33be8e2992f3 | 641 | *pS += (char)strtol(byte.c_str(), NULL, 16); |
JMF | 2:0e2ef866af95 | 642 | pos_start += 2; |
JMF | 2:0e2ef866af95 | 643 | } |
fkellermavnet | 34:1a4498e3580e | 644 | numBytes += i/2; |
JMF | 2:0e2ef866af95 | 645 | } |
JMF | 2:0e2ef866af95 | 646 | } |
JMF | 2:0e2ef866af95 | 647 | } |
JMF | 2:0e2ef866af95 | 648 | else |
JMF | 67:11db02bb93e1 | 649 | PUTS("sockread Err, to many to read\r\n"); |
JMF | 2:0e2ef866af95 | 650 | |
fkellermavnet | 43:6821a9c78c4b | 651 | return (numBytes); |
JMF | 2:0e2ef866af95 | 652 | } |