![](/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@85:0cf65ceb4492, 2019-04-29 (annotated)
- Committer:
- jwhammel
- Date:
- Mon Apr 29 04:24:38 2019 +0000
- Revision:
- 85:0cf65ceb4492
- Parent:
- 84:fc8c9b39723a
We have added an alarm trigger that gets sent to AT&T Flow.
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 | 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 | { |
jwhammel | 85:0cf65ceb4492 | 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 | { |
jwhammel | 85:0cf65ceb4492 | 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 | { |
jwhammel | 85:0cf65ceb4492 | 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 | { |
jwhammel | 85:0cf65ceb4492 | 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 | |
fkellermavnet | 77:c65eae5b9958 | 107 | void display_modem_firmware_version(void) |
fkellermavnet | 77:c65eae5b9958 | 108 | { |
fkellermavnet | 77:c65eae5b9958 | 109 | string * pRespStr; |
fkellermavnet | 77:c65eae5b9958 | 110 | |
jwhammel | 85:0cf65ceb4492 | 111 | PUTS("<-------- WNC Firmware Revision --------\r\n"); |
fkellermavnet | 77:c65eae5b9958 | 112 | at_send_wnc_cmd("ATI", &pRespStr, WNC_TIMEOUT_MS); |
jwhammel | 85:0cf65ceb4492 | 113 | PUTS(pRespStr->c_str()); |
jwhammel | 85:0cf65ceb4492 | 114 | PUTS("\r\n"); |
jwhammel | 85:0cf65ceb4492 | 115 | PUTS("--------------------------------------->\r\n"); |
fkellermavnet | 77:c65eae5b9958 | 116 | } |
fkellermavnet | 77:c65eae5b9958 | 117 | |
JMF | 2:0e2ef866af95 | 118 | void resolve_mdm(void) |
JMF | 2:0e2ef866af95 | 119 | { |
JMF | 2:0e2ef866af95 | 120 | do |
JMF | 2:0e2ef866af95 | 121 | { |
JMF | 2:0e2ef866af95 | 122 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 123 | at_dnsresolve_wnc(MY_SERVER_URL, &MyServerIpAddress); |
JMF | 2:0e2ef866af95 | 124 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 125 | { |
JMF | 2:0e2ef866af95 | 126 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 127 | } |
JMF | 2:0e2ef866af95 | 128 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 129 | { |
jwhammel | 85:0cf65ceb4492 | 130 | PUTS("Bad URL!!!!!!\r\n"); |
JMF | 2:0e2ef866af95 | 131 | } |
JMF | 2:0e2ef866af95 | 132 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 133 | |
jwhammel | 85:0cf65ceb4492 | 134 | PRINTF("My Server IP: %s\r\n", MyServerIpAddress.c_str()); |
JMF | 2:0e2ef866af95 | 135 | } |
JMF | 2:0e2ef866af95 | 136 | |
JMF | 2:0e2ef866af95 | 137 | void sockopen_mdm(void) |
JMF | 2:0e2ef866af95 | 138 | { |
JMF | 2:0e2ef866af95 | 139 | do |
JMF | 2:0e2ef866af95 | 140 | { |
JMF | 2:0e2ef866af95 | 141 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 142 | at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR); |
JMF | 2:0e2ef866af95 | 143 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 144 | { |
JMF | 2:0e2ef866af95 | 145 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 146 | } |
jwhammel | 85:0cf65ceb4492 | 147 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
jwhammel | 85:0cf65ceb4492 | 148 | PUTS("Socket open fail!!!!\r\n"); |
fkellermavnet | 10:df54436ecd38 | 149 | else |
fkellermavnet | 10:df54436ecd38 | 150 | socketOpen = 1; |
JMF | 2:0e2ef866af95 | 151 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 152 | } |
JMF | 2:0e2ef866af95 | 153 | |
JMF | 2:0e2ef866af95 | 154 | void sockwrite_mdm(const char * s) |
JMF | 2:0e2ef866af95 | 155 | { |
fkellermavnet | 10:df54436ecd38 | 156 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 157 | { |
JMF | 2:0e2ef866af95 | 158 | do |
JMF | 2:0e2ef866af95 | 159 | { |
JMF | 2:0e2ef866af95 | 160 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 161 | at_sockwrite_wnc(s); |
JMF | 2:0e2ef866af95 | 162 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 163 | { |
jwhammel | 85:0cf65ceb4492 | 164 | PUTS("Sock write no response!\r\n"); |
JMF | 2:0e2ef866af95 | 165 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 166 | } |
JMF | 2:0e2ef866af95 | 167 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 168 | { |
jwhammel | 85:0cf65ceb4492 | 169 | PUTS("Socket Write fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 170 | software_init_mdm(); |
fkellermavnet | 74:3e3ee15584e5 | 171 | }else if (WNC_MDM_ERR == WNC_EXTERR) |
fkellermavnet | 73:da723fedfdd2 | 172 | { |
jwhammel | 85:0cf65ceb4492 | 173 | PUTS("Socket Disconnected (broken) !!!\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 174 | sockclose_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 175 | sockopen_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 176 | //software_init_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 177 | } |
JMF | 2:0e2ef866af95 | 178 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 179 | } |
jwhammel | 85:0cf65ceb4492 | 180 | else |
jwhammel | 85:0cf65ceb4492 | 181 | PUTS("Socket is closed for write!\r\n"); |
JMF | 2:0e2ef866af95 | 182 | } |
JMF | 2:0e2ef866af95 | 183 | |
fkellermavnet | 30:33be8e2992f3 | 184 | unsigned sockread_mdm(string * sockData, int len, int retries) |
JMF | 2:0e2ef866af95 | 185 | { |
fkellermavnet | 30:33be8e2992f3 | 186 | unsigned n = 0; |
fkellermavnet | 30:33be8e2992f3 | 187 | |
fkellermavnet | 10:df54436ecd38 | 188 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 189 | { |
JMF | 2:0e2ef866af95 | 190 | do |
JMF | 2:0e2ef866af95 | 191 | { |
JMF | 2:0e2ef866af95 | 192 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 30:33be8e2992f3 | 193 | n = at_sockread_wnc(sockData, len, retries); |
JMF | 2:0e2ef866af95 | 194 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 195 | { |
fkellermavnet | 30:33be8e2992f3 | 196 | if (n == 0) |
fkellermavnet | 30:33be8e2992f3 | 197 | software_init_mdm(); |
jwhammel | 85:0cf65ceb4492 | 198 | else |
jwhammel | 85:0cf65ceb4492 | 199 | PUTS("Sock read partial data!!!\r\n"); |
JMF | 2:0e2ef866af95 | 200 | } |
jwhammel | 85:0cf65ceb4492 | 201 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
jwhammel | 85:0cf65ceb4492 | 202 | PUTS("Sock read fail!!!!\r\n"); |
fkellermavnet | 30:33be8e2992f3 | 203 | } while (WNC_MDM_ERR == WNC_NO_RESPONSE); |
fkellermavnet | 10:df54436ecd38 | 204 | } |
fkellermavnet | 10:df54436ecd38 | 205 | else |
fkellermavnet | 44:c95a85b5cf92 | 206 | { |
jwhammel | 85:0cf65ceb4492 | 207 | PUTS("Socket is closed for read\r\n"); |
fkellermavnet | 44:c95a85b5cf92 | 208 | sockData->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 209 | } |
fkellermavnet | 30:33be8e2992f3 | 210 | |
fkellermavnet | 30:33be8e2992f3 | 211 | return (n); |
JMF | 2:0e2ef866af95 | 212 | } |
JMF | 2:0e2ef866af95 | 213 | |
JMF | 2:0e2ef866af95 | 214 | void sockclose_mdm(void) |
JMF | 2:0e2ef866af95 | 215 | { |
JMF | 2:0e2ef866af95 | 216 | do |
JMF | 2:0e2ef866af95 | 217 | { |
JMF | 2:0e2ef866af95 | 218 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 219 | at_sockclose_wnc(); |
fkellermavnet | 10:df54436ecd38 | 220 | // Assume close happened even if it went bad |
fkellermavnet | 10:df54436ecd38 | 221 | // going bad will result in a re-init anyways and if close |
fkellermavnet | 10:df54436ecd38 | 222 | // fails we're pretty much in bad state and not much can do |
fkellermavnet | 10:df54436ecd38 | 223 | socketOpen = 0; |
JMF | 2:0e2ef866af95 | 224 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 225 | { |
JMF | 2:0e2ef866af95 | 226 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 227 | } |
jwhammel | 85:0cf65ceb4492 | 228 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
jwhammel | 85:0cf65ceb4492 | 229 | PUTS("Sock close fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 230 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 231 | } |
JMF | 2:0e2ef866af95 | 232 | |
JMF | 2:0e2ef866af95 | 233 | /** |
JMF | 2:0e2ef866af95 | 234 | * C++ version 0.4 char* style "itoa": |
fkellermavnet | 74:3e3ee15584e5 | 235 | * Written by Lukas Chmela |
JMF | 2:0e2ef866af95 | 236 | * Released under GPLv3. |
JMF | 2:0e2ef866af95 | 237 | */ |
JMF | 2:0e2ef866af95 | 238 | |
JMF | 2:0e2ef866af95 | 239 | char* itoa(int value, char* result, int base) |
JMF | 2:0e2ef866af95 | 240 | { |
JMF | 2:0e2ef866af95 | 241 | // check that the base if valid |
JMF | 2:0e2ef866af95 | 242 | if ( base < 2 || base > 36 ) { |
JMF | 2:0e2ef866af95 | 243 | *result = '\0'; |
JMF | 2:0e2ef866af95 | 244 | return result; |
JMF | 2:0e2ef866af95 | 245 | } |
JMF | 2:0e2ef866af95 | 246 | |
JMF | 2:0e2ef866af95 | 247 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 2:0e2ef866af95 | 248 | int tmp_value; |
JMF | 2:0e2ef866af95 | 249 | |
JMF | 2:0e2ef866af95 | 250 | do { |
JMF | 2:0e2ef866af95 | 251 | tmp_value = value; |
JMF | 2:0e2ef866af95 | 252 | value /= base; |
JMF | 2:0e2ef866af95 | 253 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 2:0e2ef866af95 | 254 | } while ( value ); |
JMF | 2:0e2ef866af95 | 255 | |
JMF | 2:0e2ef866af95 | 256 | // Apply negative sign |
JMF | 2:0e2ef866af95 | 257 | if ( tmp_value < 0 ) |
JMF | 2:0e2ef866af95 | 258 | *ptr++ = '-'; |
JMF | 2:0e2ef866af95 | 259 | *ptr-- = '\0'; |
JMF | 2:0e2ef866af95 | 260 | |
JMF | 2:0e2ef866af95 | 261 | while ( ptr1 < ptr ) { |
JMF | 2:0e2ef866af95 | 262 | tmp_char = *ptr; |
JMF | 2:0e2ef866af95 | 263 | *ptr-- = *ptr1; |
JMF | 2:0e2ef866af95 | 264 | *ptr1++ = tmp_char; |
JMF | 2:0e2ef866af95 | 265 | } |
JMF | 2:0e2ef866af95 | 266 | |
JMF | 2:0e2ef866af95 | 267 | return result; |
JMF | 2:0e2ef866af95 | 268 | } |
JMF | 2:0e2ef866af95 | 269 | |
JMF | 2:0e2ef866af95 | 270 | extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len); |
JMF | 2:0e2ef866af95 | 271 | |
fkellermavnet | 42:be4b9ee3a615 | 272 | int check_wnc_ready(void) |
fkellermavnet | 42:be4b9ee3a615 | 273 | { |
fkellermavnet | 43:6821a9c78c4b | 274 | string * pRespStr; |
fkellermavnet | 43:6821a9c78c4b | 275 | size_t pos; |
fkellermavnet | 43:6821a9c78c4b | 276 | int regSts; |
fkellermavnet | 46:da9d788f5d5a | 277 | int cmdRes1, cmdRes2; |
fkellermavnet | 42:be4b9ee3a615 | 278 | |
fkellermavnet | 53:dcccf2881fa0 | 279 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 280 | PUTS("<-------- Begin Cell Status ------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 281 | #endif |
fkellermavnet | 46:da9d788f5d5a | 282 | cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS); // Check RSSI,BER |
fkellermavnet | 46:da9d788f5d5a | 283 | cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS); // Check if SIM locked |
fkellermavnet | 46:da9d788f5d5a | 284 | |
fkellermavnet | 46:da9d788f5d5a | 285 | if ((cmdRes1 != 0) && (cmdRes2 != 0)) |
fkellermavnet | 43:6821a9c78c4b | 286 | { |
fkellermavnet | 53:dcccf2881fa0 | 287 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 288 | PUTS("------------ WNC No Response! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 289 | #endif |
fkellermavnet | 43:6821a9c78c4b | 290 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 291 | } |
fkellermavnet | 42:be4b9ee3a615 | 292 | |
fkellermavnet | 43:6821a9c78c4b | 293 | // If SIM Card not ready don't bother with commands! |
fkellermavnet | 43:6821a9c78c4b | 294 | if (pRespStr->find("CPIN: READY") == string::npos) |
fkellermavnet | 43:6821a9c78c4b | 295 | { |
fkellermavnet | 53:dcccf2881fa0 | 296 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 297 | PUTS("------------ WNC SIM Problem! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 298 | #endif |
fkellermavnet | 43:6821a9c78c4b | 299 | return (-1); |
fkellermavnet | 43:6821a9c78c4b | 300 | } |
fkellermavnet | 43:6821a9c78c4b | 301 | |
fkellermavnet | 43:6821a9c78c4b | 302 | // SIM card OK, now check for signal and cellular network registration |
fkellermavnet | 46:da9d788f5d5a | 303 | cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS); // Check if registered on network |
fkellermavnet | 73:da723fedfdd2 | 304 | if (pRespStr->size() > 0) |
fkellermavnet | 73:da723fedfdd2 | 305 | { |
fkellermavnet | 42:be4b9ee3a615 | 306 | pos = pRespStr->find("CREG: "); |
fkellermavnet | 42:be4b9ee3a615 | 307 | if (pos != string::npos) |
fkellermavnet | 42:be4b9ee3a615 | 308 | { |
fkellermavnet | 43:6821a9c78c4b | 309 | // The registration is the 2nd arg in the comma separated list |
fkellermavnet | 43:6821a9c78c4b | 310 | *pRespStr = pRespStr->substr(pos+8, 1); |
fkellermavnet | 43:6821a9c78c4b | 311 | regSts = atoi(pRespStr->c_str()); |
fkellermavnet | 43:6821a9c78c4b | 312 | // 1 - registered home, 5 - registered roaming |
fkellermavnet | 43:6821a9c78c4b | 313 | if ((regSts != 1) && (regSts != 5)) |
fkellermavnet | 43:6821a9c78c4b | 314 | { |
fkellermavnet | 53:dcccf2881fa0 | 315 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 316 | PUTS("------------ WNC Cell Link Down! ------>\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 317 | #endif |
fkellermavnet | 43:6821a9c78c4b | 318 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 319 | } |
fkellermavnet | 42:be4b9ee3a615 | 320 | } |
fkellermavnet | 43:6821a9c78c4b | 321 | |
fkellermavnet | 53:dcccf2881fa0 | 322 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 323 | PUTS("------------ WNC Ready ---------------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 324 | #endif |
fkellermavnet | 73:da723fedfdd2 | 325 | } |
fkellermavnet | 73:da723fedfdd2 | 326 | else |
fkellermavnet | 73:da723fedfdd2 | 327 | { |
fkellermavnet | 73:da723fedfdd2 | 328 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 329 | PUTS("------------ CREG No Reply !----------->\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 330 | #endif |
fkellermavnet | 73:da723fedfdd2 | 331 | return (-2); |
fkellermavnet | 73:da723fedfdd2 | 332 | } |
fkellermavnet | 73:da723fedfdd2 | 333 | |
fkellermavnet | 43:6821a9c78c4b | 334 | return (0); |
fkellermavnet | 42:be4b9ee3a615 | 335 | } |
fkellermavnet | 42:be4b9ee3a615 | 336 | |
JMF | 2:0e2ef866af95 | 337 | // Sets a global with failure or success, assumes 1 thread all the time |
JMF | 2:0e2ef866af95 | 338 | int send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 2:0e2ef866af95 | 339 | { |
fkellermavnet | 46:da9d788f5d5a | 340 | int cmdRes; |
fkellermavnet | 46:da9d788f5d5a | 341 | |
fkellermavnet | 42:be4b9ee3a615 | 342 | if (check_wnc_ready() < 0) |
fkellermavnet | 42:be4b9ee3a615 | 343 | { |
fkellermavnet | 45:a836eecd5d12 | 344 | static string noRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 345 | |
fkellermavnet | 53:dcccf2881fa0 | 346 | #ifdef WNC_CMD_DEBUG_ON |
jwhammel | 85:0cf65ceb4492 | 347 | PUTS("FAIL send cmd: "); |
fkellermavnet | 53:dcccf2881fa0 | 348 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 349 | PUTS(s); |
jwhammel | 85:0cf65ceb4492 | 350 | PUTS("\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 351 | #else |
fkellermavnet | 53:dcccf2881fa0 | 352 | string truncStr(s, 50); |
fkellermavnet | 53:dcccf2881fa0 | 353 | truncStr += "\r\n"; |
jwhammel | 85:0cf65ceb4492 | 354 | PUTS(truncStr.c_str()); |
fkellermavnet | 53:dcccf2881fa0 | 355 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 356 | #else |
jwhammel | 85:0cf65ceb4492 | 357 | PUTS("FAIL send cmd!\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 358 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 359 | |
fkellermavnet | 46:da9d788f5d5a | 360 | WNC_MDM_ERR = WNC_CELL_LINK_DOWN; |
fkellermavnet | 45:a836eecd5d12 | 361 | noRespStr.erase(); |
fkellermavnet | 44:c95a85b5cf92 | 362 | *r = &noRespStr; |
fkellermavnet | 46:da9d788f5d5a | 363 | return (-3); |
fkellermavnet | 42:be4b9ee3a615 | 364 | } |
fkellermavnet | 53:dcccf2881fa0 | 365 | |
fkellermavnet | 53:dcccf2881fa0 | 366 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 367 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 368 | PUTS("[---------- Network Status -------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 369 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 370 | string * pRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 371 | at_send_wnc_cmd("AT@SOCKDIAL?", &pRespStr, 5000); |
fkellermavnet | 53:dcccf2881fa0 | 372 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 373 | PUTS("---------------------------------------]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 374 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 375 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 376 | |
fkellermavnet | 42:be4b9ee3a615 | 377 | // If WNC ready, send user command |
fkellermavnet | 46:da9d788f5d5a | 378 | cmdRes = at_send_wnc_cmd(s, r, ms_timeout); |
fkellermavnet | 46:da9d788f5d5a | 379 | |
fkellermavnet | 46:da9d788f5d5a | 380 | if (cmdRes == -1) |
fkellermavnet | 46:da9d788f5d5a | 381 | WNC_MDM_ERR = WNC_CMD_ERR; |
fkellermavnet | 46:da9d788f5d5a | 382 | |
fkellermavnet | 46:da9d788f5d5a | 383 | if (cmdRes == -2) |
fkellermavnet | 46:da9d788f5d5a | 384 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 74:3e3ee15584e5 | 385 | |
fkellermavnet | 74:3e3ee15584e5 | 386 | if (cmdRes == -3) { |
fkellermavnet | 73:da723fedfdd2 | 387 | WNC_MDM_ERR = WNC_EXTERR; |
jwhammel | 85:0cf65ceb4492 | 388 | PRINTF("[[WNC_MDM_ERR = WNC_EXTERR]] \r\n"); |
fkellermavnet | 74:3e3ee15584e5 | 389 | } |
fkellermavnet | 73:da723fedfdd2 | 390 | |
fkellermavnet | 73:da723fedfdd2 | 391 | if (cmdRes == 0) |
fkellermavnet | 46:da9d788f5d5a | 392 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 46:da9d788f5d5a | 393 | |
fkellermavnet | 46:da9d788f5d5a | 394 | return (cmdRes); |
fkellermavnet | 42:be4b9ee3a615 | 395 | } |
fkellermavnet | 42:be4b9ee3a615 | 396 | |
fkellermavnet | 42:be4b9ee3a615 | 397 | int at_send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
fkellermavnet | 42:be4b9ee3a615 | 398 | { |
fkellermavnet | 74:3e3ee15584e5 | 399 | //Eaddy |
fkellermavnet | 75:8cc98a3b9c62 | 400 | static const char * rsp_lst[] = { "OK", "ERROR","@EXTERR", "+CME", NULL }; |
JMF | 2:0e2ef866af95 | 401 | int len; |
fkellermavnet | 53:dcccf2881fa0 | 402 | |
fkellermavnet | 53:dcccf2881fa0 | 403 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 404 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
fkellermavnet | 53:dcccf2881fa0 | 405 | |
fkellermavnet | 53:dcccf2881fa0 | 406 | #else |
fkellermavnet | 42:be4b9ee3a615 | 407 | if (strlen(s) > 60) |
fkellermavnet | 42:be4b9ee3a615 | 408 | { |
fkellermavnet | 42:be4b9ee3a615 | 409 | string truncStr(s,57); |
fkellermavnet | 42:be4b9ee3a615 | 410 | truncStr += "..."; |
jwhammel | 85:0cf65ceb4492 | 411 | PRINTF("Send: <<%s>>\r\n",truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 412 | } |
jwhammel | 85:0cf65ceb4492 | 413 | else |
fkellermavnet | 53:dcccf2881fa0 | 414 | #endif |
jwhammel | 85:0cf65ceb4492 | 415 | PRINTF("Send: <<%s>>\r\n",s); |
fkellermavnet | 53:dcccf2881fa0 | 416 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 417 | |
JMF | 2:0e2ef866af95 | 418 | int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len); |
JMF | 2:0e2ef866af95 | 419 | *r = &wncStr; // Return a pointer to the static string |
JMF | 2:0e2ef866af95 | 420 | |
JMF | 2:0e2ef866af95 | 421 | if (res >= 0) |
fkellermavnet | 53:dcccf2881fa0 | 422 | { |
fkellermavnet | 53:dcccf2881fa0 | 423 | |
fkellermavnet | 53:dcccf2881fa0 | 424 | #ifdef WNC_CMD_DEBUG_ON |
jwhammel | 85:0cf65ceb4492 | 425 | PUTS("["); |
fkellermavnet | 53:dcccf2881fa0 | 426 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
jwhammel | 85:0cf65ceb4492 | 427 | PUTS(wncStr.c_str()); |
jwhammel | 85:0cf65ceb4492 | 428 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 429 | #else |
jwhammel | 85:0cf65ceb4492 | 430 | if (wncStr.size() < 51) |
jwhammel | 85:0cf65ceb4492 | 431 | PUTS(wncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 432 | else |
fkellermavnet | 42:be4b9ee3a615 | 433 | { |
fkellermavnet | 42:be4b9ee3a615 | 434 | string truncStr = wncStr.substr(0,50) + "..."; |
jwhammel | 85:0cf65ceb4492 | 435 | PUTS(truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 436 | } |
jwhammel | 85:0cf65ceb4492 | 437 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 438 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 439 | #endif |
fkellermavnet | 42:be4b9ee3a615 | 440 | |
fkellermavnet | 74:3e3ee15584e5 | 441 | #if 0 |
JMF | 2:0e2ef866af95 | 442 | if (res > 0) |
JMF | 2:0e2ef866af95 | 443 | return -1; |
JMF | 2:0e2ef866af95 | 444 | else |
JMF | 2:0e2ef866af95 | 445 | return 0; |
fkellermavnet | 74:3e3ee15584e5 | 446 | #else |
fkellermavnet | 74:3e3ee15584e5 | 447 | //Eaddy added |
fkellermavnet | 74:3e3ee15584e5 | 448 | if (res == 0) { |
fkellermavnet | 73:da723fedfdd2 | 449 | /* OK */ |
fkellermavnet | 73:da723fedfdd2 | 450 | return 0; |
fkellermavnet | 73:da723fedfdd2 | 451 | } else if (res == 2) { |
fkellermavnet | 73:da723fedfdd2 | 452 | /* @EXTERR */ |
jwhammel | 85:0cf65ceb4492 | 453 | PRINTF("@EXTERR and res = %d \r\n", res); |
fkellermavnet | 73:da723fedfdd2 | 454 | return -3; |
fkellermavnet | 73:da723fedfdd2 | 455 | } else |
JMF | 2:0e2ef866af95 | 456 | return -1; |
fkellermavnet | 74:3e3ee15584e5 | 457 | #endif |
JMF | 2:0e2ef866af95 | 458 | } |
JMF | 2:0e2ef866af95 | 459 | else |
JMF | 2:0e2ef866af95 | 460 | { |
jwhammel | 85:0cf65ceb4492 | 461 | PUTS("No response from WNC!\n\r"); |
JMF | 2:0e2ef866af95 | 462 | return -2; |
JMF | 2:0e2ef866af95 | 463 | } |
JMF | 2:0e2ef866af95 | 464 | } |
JMF | 2:0e2ef866af95 | 465 | |
fkellermavnet | 42:be4b9ee3a615 | 466 | |
fkellermavnet | 19:f89baed3bd6f | 467 | void at_at_wnc(void) |
fkellermavnet | 19:f89baed3bd6f | 468 | { |
fkellermavnet | 19:f89baed3bd6f | 469 | string * pRespStr; |
fkellermavnet | 19:f89baed3bd6f | 470 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 19:f89baed3bd6f | 471 | } |
fkellermavnet | 19:f89baed3bd6f | 472 | |
fkellermavnet | 42:be4b9ee3a615 | 473 | void at_init_wnc(bool hardReset) |
JMF | 2:0e2ef866af95 | 474 | { |
fkellermavnet | 36:d4782eabff43 | 475 | static bool pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 476 | static bool intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 477 | static bool sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 478 | string * pRespStr; |
fkellermavnet | 46:da9d788f5d5a | 479 | int cmdRes; |
fkellermavnet | 36:d4782eabff43 | 480 | |
fkellermavnet | 42:be4b9ee3a615 | 481 | if (hardReset == true) |
fkellermavnet | 42:be4b9ee3a615 | 482 | { |
jwhammel | 85:0cf65ceb4492 | 483 | PUTS("Hard Reset!\r\n"); |
fkellermavnet | 42:be4b9ee3a615 | 484 | pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 485 | intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 486 | sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 487 | } |
fkellermavnet | 42:be4b9ee3a615 | 488 | |
jwhammel | 85:0cf65ceb4492 | 489 | PUTS("Start AT init of WNC:\r\n"); |
fkellermavnet | 43:6821a9c78c4b | 490 | // Quick commands below do not need to check cellular connectivity |
fkellermavnet | 46:da9d788f5d5a | 491 | cmdRes = at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 46:da9d788f5d5a | 492 | cmdRes += at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS); // Echo Off |
fkellermavnet | 46:da9d788f5d5a | 493 | cmdRes += at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS); // 2 - verbose error, 1 - numeric error, 0 - just ERROR |
kevinkeryk | 82:65cde5d7070c | 494 | /* Disable unsolicited RRCSTATE responses from interfering with DNS |
kevinkeryk | 82:65cde5d7070c | 495 | * resolution which was introduced with release of the |
kevinkeryk | 82:65cde5d7070c | 496 | * NQ_MPSS_IMA3_v10.58.174043 LTE-M firmware and is a more elegant |
kevinkeryk | 82:65cde5d7070c | 497 | * solution than disabling ALL unsoliceted responses as WNC suggested |
kevinkeryk | 82:65cde5d7070c | 498 | * in ticket 2017090085. |
kevinkeryk | 82:65cde5d7070c | 499 | */ |
kevinkeryk | 82:65cde5d7070c | 500 | //cmdRes += at_send_wnc_cmd("AT%NOTIFYEV=\"RRCSTATE\",0", &pRespStr, WNC_TIMEOUT_MS); |
kevinkeryk | 82:65cde5d7070c | 501 | cmdRes += at_send_wnc_cmd("AT%NOTIFYEV=\"ALL\",0", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 43:6821a9c78c4b | 502 | |
fkellermavnet | 43:6821a9c78c4b | 503 | // If the simple commands are not working no chance of more complex. |
fkellermavnet | 43:6821a9c78c4b | 504 | // I have seen re-trying commands make it worse. |
fkellermavnet | 46:da9d788f5d5a | 505 | if (cmdRes < 0) |
fkellermavnet | 46:da9d788f5d5a | 506 | { |
fkellermavnet | 46:da9d788f5d5a | 507 | // Since I used the at_send_wnc_cmd I am setting the error state based upon |
fkellermavnet | 46:da9d788f5d5a | 508 | // the responses. And since these are simple commands, even if the WNC |
fkellermavnet | 46:da9d788f5d5a | 509 | // is saying ERROR, treat it like a no response. |
fkellermavnet | 46:da9d788f5d5a | 510 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 43:6821a9c78c4b | 511 | return ; |
fkellermavnet | 46:da9d788f5d5a | 512 | } |
fkellermavnet | 42:be4b9ee3a615 | 513 | |
fkellermavnet | 46:da9d788f5d5a | 514 | if (intSet == false) |
fkellermavnet | 46:da9d788f5d5a | 515 | cmdRes = send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 516 | |
fkellermavnet | 46:da9d788f5d5a | 517 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 518 | intSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 519 | else |
fkellermavnet | 42:be4b9ee3a615 | 520 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 521 | |
fkellermavnet | 36:d4782eabff43 | 522 | if (pdnSet == false) |
fkellermavnet | 36:d4782eabff43 | 523 | { |
fkellermavnet | 36:d4782eabff43 | 524 | string cmd_str("AT%PDNSET=1,"); |
fkellermavnet | 36:d4782eabff43 | 525 | cmd_str += MY_APN_STR; |
fkellermavnet | 36:d4782eabff43 | 526 | cmd_str += ",IP"; |
fkellermavnet | 46:da9d788f5d5a | 527 | 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 | 528 | } |
fkellermavnet | 42:be4b9ee3a615 | 529 | |
fkellermavnet | 46:da9d788f5d5a | 530 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 531 | pdnSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 532 | else |
fkellermavnet | 42:be4b9ee3a615 | 533 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 534 | |
fkellermavnet | 42:be4b9ee3a615 | 535 | if (sockDialSet == false) |
fkellermavnet | 46:da9d788f5d5a | 536 | cmdRes = send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 537 | |
fkellermavnet | 46:da9d788f5d5a | 538 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 539 | sockDialSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 540 | else |
fkellermavnet | 42:be4b9ee3a615 | 541 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 542 | |
jwhammel | 85:0cf65ceb4492 | 543 | PUTS("SUCCESS: AT init of WNC!\r\n"); |
JMF | 2:0e2ef866af95 | 544 | } |
JMF | 2:0e2ef866af95 | 545 | |
JMF | 2:0e2ef866af95 | 546 | void at_sockopen_wnc(const string & ipStr, const char * port ) |
JMF | 2:0e2ef866af95 | 547 | { |
JMF | 2:0e2ef866af95 | 548 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 549 | send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 550 | string cmd_str("AT@SOCKCONN=1,\""); |
JMF | 2:0e2ef866af95 | 551 | cmd_str += ipStr; |
JMF | 2:0e2ef866af95 | 552 | cmd_str += "\","; |
JMF | 2:0e2ef866af95 | 553 | cmd_str += port; |
fkellermavnet | 38:564b312a719f | 554 | cmd_str += ",30"; |
fkellermavnet | 73:da723fedfdd2 | 555 | int cmd = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000); |
fkellermavnet | 73:da723fedfdd2 | 556 | if (cmd != WNC_OK) { |
fkellermavnet | 73:da723fedfdd2 | 557 | // Per WNC: re-close even if open fails! |
fkellermavnet | 73:da723fedfdd2 | 558 | at_sockclose_wnc(); |
fkellermavnet | 73:da723fedfdd2 | 559 | } |
JMF | 2:0e2ef866af95 | 560 | } |
JMF | 2:0e2ef866af95 | 561 | |
JMF | 2:0e2ef866af95 | 562 | void at_sockclose_wnc(void) |
JMF | 2:0e2ef866af95 | 563 | { |
JMF | 2:0e2ef866af95 | 564 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 565 | send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 566 | } |
JMF | 2:0e2ef866af95 | 567 | |
JMF | 2:0e2ef866af95 | 568 | int at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 2:0e2ef866af95 | 569 | { |
JMF | 2:0e2ef866af95 | 570 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 571 | string str(s); |
fkellermavnet | 43:6821a9c78c4b | 572 | str = "AT@DNSRESVDON=\"" + str + "\""; |
fkellermavnet | 79:ff3014b3637e | 573 | if (send_wnc_cmd(str.c_str(), &pRespStr, 60000) == 0) |
JMF | 2:0e2ef866af95 | 574 | { |
kevinkeryk | 82:65cde5d7070c | 575 | size_t pos_start = pRespStr->find("ON:\""); // See there are 4 characters in the search string |
kevinkeryk | 82:65cde5d7070c | 576 | size_t pos_end = pRespStr->find("\"", (pos_start + 4)); |
fkellermavnet | 43:6821a9c78c4b | 577 | if ((pos_start != string::npos) && (pos_end != string::npos)) |
JMF | 2:0e2ef866af95 | 578 | { |
kevinkeryk | 82:65cde5d7070c | 579 | /* The substring identified was indeed located within the returned |
kevinkeryk | 82:65cde5d7070c | 580 | * response. Adjust the start position ahead by 2 and move the end |
kevinkeryk | 82:65cde5d7070c | 581 | * position back by l to narrow the answer down to to the response |
kevinkeryk | 82:65cde5d7070c | 582 | * string contained within the quote bookends --> ON:"<response>". |
kevinkeryk | 82:65cde5d7070c | 583 | */ |
kevinkeryk | 82:65cde5d7070c | 584 | pos_start += 4; |
kevinkeryk | 82:65cde5d7070c | 585 | pos_end -= 1; |
kevinkeryk | 82:65cde5d7070c | 586 | |
JMF | 2:0e2ef866af95 | 587 | if (pos_end > pos_start) |
JMF | 2:0e2ef866af95 | 588 | { |
JMF | 2:0e2ef866af95 | 589 | // Make a copy for use later (the source string is re-used) |
JMF | 2:0e2ef866af95 | 590 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 2:0e2ef866af95 | 591 | return 1; |
JMF | 2:0e2ef866af95 | 592 | } |
jwhammel | 85:0cf65ceb4492 | 593 | else |
jwhammel | 85:0cf65ceb4492 | 594 | PUTS("URL Resolve fail, substr Err\r\n"); |
JMF | 2:0e2ef866af95 | 595 | } |
jwhammel | 85:0cf65ceb4492 | 596 | else |
jwhammel | 85:0cf65ceb4492 | 597 | PUTS("URL Resolve fail, no quotes\r\n"); |
JMF | 2:0e2ef866af95 | 598 | } |
JMF | 2:0e2ef866af95 | 599 | else |
jwhammel | 85:0cf65ceb4492 | 600 | PUTS("URL Resolve fail, WNC cmd fail\r\n"); |
JMF | 2:0e2ef866af95 | 601 | |
fkellermavnet | 43:6821a9c78c4b | 602 | *ipStr = "192.168.0.1"; |
fkellermavnet | 43:6821a9c78c4b | 603 | |
JMF | 2:0e2ef866af95 | 604 | return -1; |
JMF | 2:0e2ef866af95 | 605 | } |
JMF | 2:0e2ef866af95 | 606 | |
JMF | 2:0e2ef866af95 | 607 | void at_sockwrite_wnc(const char * s) |
JMF | 2:0e2ef866af95 | 608 | { |
JMF | 2:0e2ef866af95 | 609 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 610 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 611 | size_t sLen = strlen(s); |
fkellermavnet | 73:da723fedfdd2 | 612 | int res; |
fkellermavnet | 39:1996eaec02d6 | 613 | if (sLen <= 1500) |
JMF | 2:0e2ef866af95 | 614 | { |
JMF | 2:0e2ef866af95 | 615 | string cmd_str("AT@SOCKWRITE=1,"); |
JMF | 2:0e2ef866af95 | 616 | itoa(sLen, num2str, 10); |
JMF | 2:0e2ef866af95 | 617 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 618 | cmd_str += ",\""; |
JMF | 2:0e2ef866af95 | 619 | while(*s != '\0') |
JMF | 2:0e2ef866af95 | 620 | { |
JMF | 2:0e2ef866af95 | 621 | itoa((int)*s++, num2str, 16); |
JMF | 2:0e2ef866af95 | 622 | // Always 2-digit ascii hex: |
JMF | 2:0e2ef866af95 | 623 | if (strlen(num2str) == 1) |
JMF | 2:0e2ef866af95 | 624 | { |
JMF | 2:0e2ef866af95 | 625 | num2str[2] = '\0'; |
JMF | 2:0e2ef866af95 | 626 | num2str[1] = num2str[0]; |
JMF | 2:0e2ef866af95 | 627 | num2str[0] = '0'; |
JMF | 2:0e2ef866af95 | 628 | } |
JMF | 2:0e2ef866af95 | 629 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 630 | } |
JMF | 2:0e2ef866af95 | 631 | cmd_str += "\""; |
fkellermavnet | 73:da723fedfdd2 | 632 | res = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 120000); |
jwhammel | 85:0cf65ceb4492 | 633 | if (res == -3) |
jwhammel | 85:0cf65ceb4492 | 634 | PUTS("sockwrite is disconnect \r\n"); |
JMF | 2:0e2ef866af95 | 635 | } |
jwhammel | 85:0cf65ceb4492 | 636 | else |
jwhammel | 85:0cf65ceb4492 | 637 | PUTS("sockwrite Err, string to long\r\n"); |
JMF | 2:0e2ef866af95 | 638 | } |
JMF | 2:0e2ef866af95 | 639 | |
JMF | 2:0e2ef866af95 | 640 | unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0) |
JMF | 2:0e2ef866af95 | 641 | { |
fkellermavnet | 34:1a4498e3580e | 642 | unsigned i, numBytes = 0; |
JMF | 2:0e2ef866af95 | 643 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 644 | string cmd_str("AT@SOCKREAD=1,"); |
fkellermavnet | 30:33be8e2992f3 | 645 | |
fkellermavnet | 44:c95a85b5cf92 | 646 | // Clean slate |
fkellermavnet | 44:c95a85b5cf92 | 647 | pS->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 648 | |
JMF | 2:0e2ef866af95 | 649 | if (n <= 1500) |
JMF | 2:0e2ef866af95 | 650 | { |
JMF | 2:0e2ef866af95 | 651 | char num2str[6]; |
fkellermavnet | 22:41e6c417ace1 | 652 | |
JMF | 2:0e2ef866af95 | 653 | itoa(n, num2str, 10); |
JMF | 2:0e2ef866af95 | 654 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 655 | retries += 1; |
JMF | 2:0e2ef866af95 | 656 | while (retries--) |
JMF | 2:0e2ef866af95 | 657 | { |
fkellermavnet | 22:41e6c417ace1 | 658 | // Assuming someone is sending then calling this to receive response, invoke |
fkellermavnet | 22:41e6c417ace1 | 659 | // a pause to give the response some time to come back and then also |
fkellermavnet | 22:41e6c417ace1 | 660 | // between each retry. |
fkellermavnet | 22:41e6c417ace1 | 661 | wait_ms(10); |
fkellermavnet | 22:41e6c417ace1 | 662 | |
fkellermavnet | 73:da723fedfdd2 | 663 | if (send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0) |
fkellermavnet | 73:da723fedfdd2 | 664 | { |
fkellermavnet | 73:da723fedfdd2 | 665 | size_t pos_start = pRespStr->find("\"") + 1; |
fkellermavnet | 73:da723fedfdd2 | 666 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 43:6821a9c78c4b | 667 | |
fkellermavnet | 73:da723fedfdd2 | 668 | // Make sure search finds what it's looking for! |
fkellermavnet | 73:da723fedfdd2 | 669 | if (pos_start != string::npos && pos_end != string::npos) |
fkellermavnet | 43:6821a9c78c4b | 670 | i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte |
fkellermavnet | 73:da723fedfdd2 | 671 | else |
fkellermavnet | 43:6821a9c78c4b | 672 | i = 0; |
fkellermavnet | 43:6821a9c78c4b | 673 | |
fkellermavnet | 73:da723fedfdd2 | 674 | if (i > 0) |
fkellermavnet | 73:da723fedfdd2 | 675 | { |
fkellermavnet | 73:da723fedfdd2 | 676 | retries = 1; // If any data found retry 1 more time to catch data that might be in another |
fkellermavnet | 73:da723fedfdd2 | 677 | // WNC payload |
fkellermavnet | 73:da723fedfdd2 | 678 | string byte; |
fkellermavnet | 73:da723fedfdd2 | 679 | while (pos_start < pos_end) |
fkellermavnet | 73:da723fedfdd2 | 680 | { |
fkellermavnet | 73:da723fedfdd2 | 681 | byte = pRespStr->substr(pos_start, 2); |
fkellermavnet | 73:da723fedfdd2 | 682 | *pS += (char)strtol(byte.c_str(), NULL, 16); |
fkellermavnet | 73:da723fedfdd2 | 683 | pos_start += 2; |
fkellermavnet | 73:da723fedfdd2 | 684 | } |
fkellermavnet | 73:da723fedfdd2 | 685 | numBytes += i/2; |
fkellermavnet | 73:da723fedfdd2 | 686 | } |
fkellermavnet | 73:da723fedfdd2 | 687 | } |
fkellermavnet | 73:da723fedfdd2 | 688 | else |
JMF | 2:0e2ef866af95 | 689 | { |
jwhammel | 85:0cf65ceb4492 | 690 | PUTS("no readsock reply!\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 691 | return (0); |
JMF | 2:0e2ef866af95 | 692 | } |
JMF | 2:0e2ef866af95 | 693 | } |
JMF | 2:0e2ef866af95 | 694 | } |
JMF | 2:0e2ef866af95 | 695 | else |
jwhammel | 85:0cf65ceb4492 | 696 | PUTS("sockread Err, to many to read\r\n"); |
JMF | 2:0e2ef866af95 | 697 | |
fkellermavnet | 43:6821a9c78c4b | 698 | return (numBytes); |
JMF | 2:0e2ef866af95 | 699 | } |