![](/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@40:aec4d09fde23, 2016-07-23 (annotated)
- Committer:
- fkellermavnet
- Date:
- Sat Jul 23 20:13:01 2016 +0000
- Revision:
- 40:aec4d09fde23
- Parent:
- 39:1996eaec02d6
- Child:
- 42:be4b9ee3a615
Turned back on hardware init. Without it really stops responding completely. Hardware init does not always make it work but it seems to sometimes and so is better without it. YMMV. Still have not found the root cause of WNC no response.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 2:0e2ef866af95 | 1 | #include "mbed.h" |
JMF | 2:0e2ef866af95 | 2 | #include <cctype> |
JMF | 2:0e2ef866af95 | 3 | #include <string> |
JMF | 2:0e2ef866af95 | 4 | #include "config_me.h" |
JMF | 2:0e2ef866af95 | 5 | #include "SerialBuffered.h" |
JMF | 2:0e2ef866af95 | 6 | #include "wnc_control.h" |
JMF | 2:0e2ef866af95 | 7 | |
JMF | 2:0e2ef866af95 | 8 | extern Serial pc; |
JMF | 2:0e2ef866af95 | 9 | extern Serial mdm; |
JMF | 2:0e2ef866af95 | 10 | extern string MyServerIpAddress; |
JMF | 2:0e2ef866af95 | 11 | extern string MySocketData; |
JMF | 2:0e2ef866af95 | 12 | |
JMF | 2:0e2ef866af95 | 13 | int reinitialize_mdm(void); |
JMF | 2:0e2ef866af95 | 14 | |
JMF | 2:0e2ef866af95 | 15 | enum WNC_ERR_e { |
JMF | 2:0e2ef866af95 | 16 | WNC_OK =0, |
JMF | 2:0e2ef866af95 | 17 | WNC_CMD_ERR = -1, |
JMF | 2:0e2ef866af95 | 18 | WNC_NO_RESPONSE = -2 |
JMF | 2:0e2ef866af95 | 19 | }; |
JMF | 2:0e2ef866af95 | 20 | |
JMF | 2:0e2ef866af95 | 21 | // Contains result of last call to send_wnc_cmd(..) |
JMF | 2:0e2ef866af95 | 22 | WNC_ERR_e WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 23 | |
JMF | 2:0e2ef866af95 | 24 | // Contains the RAW WNC UART responses |
JMF | 2:0e2ef866af95 | 25 | static string wncStr; |
fkellermavnet | 10:df54436ecd38 | 26 | static int socketOpen = 0; |
JMF | 2:0e2ef866af95 | 27 | |
JMF | 2:0e2ef866af95 | 28 | void software_init_mdm(void) |
JMF | 2:0e2ef866af95 | 29 | { |
JMF | 2:0e2ef866af95 | 30 | do |
JMF | 2:0e2ef866af95 | 31 | { |
JMF | 2:0e2ef866af95 | 32 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 33 | at_init_wnc(); |
fkellermavnet | 40:aec4d09fde23 | 34 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
fkellermavnet | 40:aec4d09fde23 | 35 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 36 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 37 | } |
JMF | 2:0e2ef866af95 | 38 | |
JMF | 2:0e2ef866af95 | 39 | void resolve_mdm(void) |
JMF | 2:0e2ef866af95 | 40 | { |
JMF | 2:0e2ef866af95 | 41 | do |
JMF | 2:0e2ef866af95 | 42 | { |
JMF | 2:0e2ef866af95 | 43 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 44 | at_dnsresolve_wnc(MY_SERVER_URL, &MyServerIpAddress); |
JMF | 2:0e2ef866af95 | 45 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 46 | { |
fkellermavnet | 40:aec4d09fde23 | 47 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 48 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 49 | } |
JMF | 2:0e2ef866af95 | 50 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 51 | { |
JMF | 2:0e2ef866af95 | 52 | pc.puts("Bad URL!!!!!!\r\n"); |
JMF | 2:0e2ef866af95 | 53 | MyServerIpAddress = "192.168.0.1"; |
JMF | 2:0e2ef866af95 | 54 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 55 | } |
JMF | 2:0e2ef866af95 | 56 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 57 | |
fkellermavnet | 30:33be8e2992f3 | 58 | pc.printf("My Server IP: %s\r\n", MyServerIpAddress.c_str()); |
JMF | 2:0e2ef866af95 | 59 | } |
JMF | 2:0e2ef866af95 | 60 | |
JMF | 2:0e2ef866af95 | 61 | void sockopen_mdm(void) |
JMF | 2:0e2ef866af95 | 62 | { |
JMF | 2:0e2ef866af95 | 63 | do |
JMF | 2:0e2ef866af95 | 64 | { |
fkellermavnet | 19:f89baed3bd6f | 65 | at_at_wnc(); |
fkellermavnet | 19:f89baed3bd6f | 66 | at_at_wnc(); |
JMF | 2:0e2ef866af95 | 67 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 68 | at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR); |
JMF | 2:0e2ef866af95 | 69 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 70 | { |
fkellermavnet | 40:aec4d09fde23 | 71 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 72 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 73 | } |
JMF | 2:0e2ef866af95 | 74 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 75 | pc.puts("Socket open fail!!!!\r\n"); |
fkellermavnet | 10:df54436ecd38 | 76 | else |
fkellermavnet | 10:df54436ecd38 | 77 | socketOpen = 1; |
JMF | 2:0e2ef866af95 | 78 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 79 | } |
JMF | 2:0e2ef866af95 | 80 | |
JMF | 2:0e2ef866af95 | 81 | void sockwrite_mdm(const char * s) |
JMF | 2:0e2ef866af95 | 82 | { |
fkellermavnet | 10:df54436ecd38 | 83 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 84 | { |
JMF | 2:0e2ef866af95 | 85 | do |
JMF | 2:0e2ef866af95 | 86 | { |
JMF | 2:0e2ef866af95 | 87 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 88 | at_sockwrite_wnc(s); |
JMF | 2:0e2ef866af95 | 89 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 90 | { |
fkellermavnet | 40:aec4d09fde23 | 91 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 92 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 93 | } |
JMF | 2:0e2ef866af95 | 94 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 95 | { |
JMF | 2:0e2ef866af95 | 96 | pc.puts("Socket Write fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 97 | // Have seen when write fails modem gets stuck in bad state, try to recover |
fkellermavnet | 40:aec4d09fde23 | 98 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 99 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 100 | } |
JMF | 2:0e2ef866af95 | 101 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 102 | } |
fkellermavnet | 10:df54436ecd38 | 103 | else |
fkellermavnet | 10:df54436ecd38 | 104 | puts("Socket is closed for write!\r\n"); |
JMF | 2:0e2ef866af95 | 105 | } |
JMF | 2:0e2ef866af95 | 106 | |
fkellermavnet | 30:33be8e2992f3 | 107 | unsigned sockread_mdm(string * sockData, int len, int retries) |
JMF | 2:0e2ef866af95 | 108 | { |
fkellermavnet | 30:33be8e2992f3 | 109 | unsigned n = 0; |
fkellermavnet | 30:33be8e2992f3 | 110 | |
fkellermavnet | 30:33be8e2992f3 | 111 | // Clean slate: |
fkellermavnet | 30:33be8e2992f3 | 112 | sockData->erase(); |
fkellermavnet | 30:33be8e2992f3 | 113 | |
fkellermavnet | 10:df54436ecd38 | 114 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 115 | { |
JMF | 2:0e2ef866af95 | 116 | do |
JMF | 2:0e2ef866af95 | 117 | { |
JMF | 2:0e2ef866af95 | 118 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 30:33be8e2992f3 | 119 | n = at_sockread_wnc(sockData, len, retries); |
JMF | 2:0e2ef866af95 | 120 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 121 | { |
fkellermavnet | 30:33be8e2992f3 | 122 | if (n == 0) |
fkellermavnet | 30:33be8e2992f3 | 123 | { |
fkellermavnet | 40:aec4d09fde23 | 124 | reinitialize_mdm(); |
fkellermavnet | 30:33be8e2992f3 | 125 | software_init_mdm(); |
fkellermavnet | 30:33be8e2992f3 | 126 | } |
fkellermavnet | 30:33be8e2992f3 | 127 | else |
fkellermavnet | 30:33be8e2992f3 | 128 | puts("Sock read partial data!!!\r\n"); |
JMF | 2:0e2ef866af95 | 129 | } |
JMF | 2:0e2ef866af95 | 130 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 131 | puts("Sock read fail!!!!\r\n"); |
fkellermavnet | 30:33be8e2992f3 | 132 | } while (WNC_MDM_ERR == WNC_NO_RESPONSE); |
fkellermavnet | 10:df54436ecd38 | 133 | } |
fkellermavnet | 10:df54436ecd38 | 134 | else |
fkellermavnet | 10:df54436ecd38 | 135 | puts("Socket is closed for read\r\n"); |
fkellermavnet | 30:33be8e2992f3 | 136 | |
fkellermavnet | 30:33be8e2992f3 | 137 | return (n); |
JMF | 2:0e2ef866af95 | 138 | } |
JMF | 2:0e2ef866af95 | 139 | |
JMF | 2:0e2ef866af95 | 140 | void sockclose_mdm(void) |
JMF | 2:0e2ef866af95 | 141 | { |
JMF | 2:0e2ef866af95 | 142 | do |
JMF | 2:0e2ef866af95 | 143 | { |
JMF | 2:0e2ef866af95 | 144 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 145 | at_sockclose_wnc(); |
fkellermavnet | 10:df54436ecd38 | 146 | // Assume close happened even if it went bad |
fkellermavnet | 10:df54436ecd38 | 147 | // going bad will result in a re-init anyways and if close |
fkellermavnet | 10:df54436ecd38 | 148 | // fails we're pretty much in bad state and not much can do |
fkellermavnet | 10:df54436ecd38 | 149 | socketOpen = 0; |
JMF | 2:0e2ef866af95 | 150 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 151 | { |
fkellermavnet | 40:aec4d09fde23 | 152 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 153 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 154 | } |
JMF | 2:0e2ef866af95 | 155 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 156 | puts("Sock close fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 157 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 158 | } |
JMF | 2:0e2ef866af95 | 159 | |
JMF | 2:0e2ef866af95 | 160 | /** |
JMF | 2:0e2ef866af95 | 161 | * C++ version 0.4 char* style "itoa": |
JMF | 2:0e2ef866af95 | 162 | * Written by Lukás Chmela |
JMF | 2:0e2ef866af95 | 163 | * Released under GPLv3. |
JMF | 2:0e2ef866af95 | 164 | */ |
JMF | 2:0e2ef866af95 | 165 | |
JMF | 2:0e2ef866af95 | 166 | char* itoa(int value, char* result, int base) |
JMF | 2:0e2ef866af95 | 167 | { |
JMF | 2:0e2ef866af95 | 168 | // check that the base if valid |
JMF | 2:0e2ef866af95 | 169 | if ( base < 2 || base > 36 ) { |
JMF | 2:0e2ef866af95 | 170 | *result = '\0'; |
JMF | 2:0e2ef866af95 | 171 | return result; |
JMF | 2:0e2ef866af95 | 172 | } |
JMF | 2:0e2ef866af95 | 173 | |
JMF | 2:0e2ef866af95 | 174 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 2:0e2ef866af95 | 175 | int tmp_value; |
JMF | 2:0e2ef866af95 | 176 | |
JMF | 2:0e2ef866af95 | 177 | do { |
JMF | 2:0e2ef866af95 | 178 | tmp_value = value; |
JMF | 2:0e2ef866af95 | 179 | value /= base; |
JMF | 2:0e2ef866af95 | 180 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 2:0e2ef866af95 | 181 | } while ( value ); |
JMF | 2:0e2ef866af95 | 182 | |
JMF | 2:0e2ef866af95 | 183 | // Apply negative sign |
JMF | 2:0e2ef866af95 | 184 | if ( tmp_value < 0 ) |
JMF | 2:0e2ef866af95 | 185 | *ptr++ = '-'; |
JMF | 2:0e2ef866af95 | 186 | *ptr-- = '\0'; |
JMF | 2:0e2ef866af95 | 187 | |
JMF | 2:0e2ef866af95 | 188 | while ( ptr1 < ptr ) { |
JMF | 2:0e2ef866af95 | 189 | tmp_char = *ptr; |
JMF | 2:0e2ef866af95 | 190 | *ptr-- = *ptr1; |
JMF | 2:0e2ef866af95 | 191 | *ptr1++ = tmp_char; |
JMF | 2:0e2ef866af95 | 192 | } |
JMF | 2:0e2ef866af95 | 193 | |
JMF | 2:0e2ef866af95 | 194 | return result; |
JMF | 2:0e2ef866af95 | 195 | } |
JMF | 2:0e2ef866af95 | 196 | |
JMF | 2:0e2ef866af95 | 197 | extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len); |
JMF | 2:0e2ef866af95 | 198 | |
JMF | 2:0e2ef866af95 | 199 | // Sets a global with failure or success, assumes 1 thread all the time |
JMF | 2:0e2ef866af95 | 200 | int send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 2:0e2ef866af95 | 201 | { |
JMF | 2:0e2ef866af95 | 202 | static const char * rsp_lst[] = { "OK", "ERROR", NULL }; |
JMF | 2:0e2ef866af95 | 203 | int len; |
JMF | 2:0e2ef866af95 | 204 | |
JMF | 2:0e2ef866af95 | 205 | pc.printf("Send: %s\r\n",s); |
JMF | 2:0e2ef866af95 | 206 | int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len); |
JMF | 2:0e2ef866af95 | 207 | *r = &wncStr; // Return a pointer to the static string |
JMF | 2:0e2ef866af95 | 208 | |
JMF | 2:0e2ef866af95 | 209 | if (res >= 0) |
JMF | 2:0e2ef866af95 | 210 | { |
JMF | 2:0e2ef866af95 | 211 | pc.puts("["); |
fkellermavnet | 30:33be8e2992f3 | 212 | pc.puts(wncStr.c_str()); |
JMF | 2:0e2ef866af95 | 213 | pc.puts("]\n\r"); |
JMF | 2:0e2ef866af95 | 214 | if (res > 0) |
JMF | 2:0e2ef866af95 | 215 | { |
JMF | 2:0e2ef866af95 | 216 | if (WNC_MDM_ERR != WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 217 | WNC_MDM_ERR = WNC_CMD_ERR; |
JMF | 2:0e2ef866af95 | 218 | return -1; |
JMF | 2:0e2ef866af95 | 219 | } |
JMF | 2:0e2ef866af95 | 220 | else |
JMF | 2:0e2ef866af95 | 221 | return 0; |
JMF | 2:0e2ef866af95 | 222 | } |
JMF | 2:0e2ef866af95 | 223 | else |
JMF | 2:0e2ef866af95 | 224 | { |
JMF | 2:0e2ef866af95 | 225 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
JMF | 2:0e2ef866af95 | 226 | pc.puts("No response from WNC!\n\r"); |
JMF | 2:0e2ef866af95 | 227 | return -2; |
JMF | 2:0e2ef866af95 | 228 | } |
JMF | 2:0e2ef866af95 | 229 | } |
JMF | 2:0e2ef866af95 | 230 | |
fkellermavnet | 19:f89baed3bd6f | 231 | void at_at_wnc(void) |
fkellermavnet | 19:f89baed3bd6f | 232 | { |
fkellermavnet | 19:f89baed3bd6f | 233 | string * pRespStr; |
fkellermavnet | 19:f89baed3bd6f | 234 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 19:f89baed3bd6f | 235 | } |
fkellermavnet | 19:f89baed3bd6f | 236 | |
JMF | 2:0e2ef866af95 | 237 | void at_init_wnc(void) |
JMF | 2:0e2ef866af95 | 238 | { |
fkellermavnet | 36:d4782eabff43 | 239 | static bool pdnSet = false; |
fkellermavnet | 36:d4782eabff43 | 240 | |
JMF | 2:0e2ef866af95 | 241 | string * pRespStr; |
fkellermavnet | 36:d4782eabff43 | 242 | pc.puts("Start AT init of WNC:\r\n"); |
fkellermavnet | 36:d4782eabff43 | 243 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 36:d4782eabff43 | 244 | send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS); // Echo ON |
JMF | 2:0e2ef866af95 | 245 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 36:d4782eabff43 | 246 | send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS); // 2 - verbose error, 1 - numeric error, 0 - just ERROR |
fkellermavnet | 36:d4782eabff43 | 247 | do { |
fkellermavnet | 36:d4782eabff43 | 248 | if (pdnSet == false) |
fkellermavnet | 36:d4782eabff43 | 249 | { |
fkellermavnet | 36:d4782eabff43 | 250 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 36:d4782eabff43 | 251 | send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 36:d4782eabff43 | 252 | string cmd_str("AT%PDNSET=1,"); |
fkellermavnet | 36:d4782eabff43 | 253 | cmd_str += MY_APN_STR; |
fkellermavnet | 36:d4782eabff43 | 254 | cmd_str += ",IP"; |
fkellermavnet | 36:d4782eabff43 | 255 | send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes |
fkellermavnet | 36:d4782eabff43 | 256 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 36:d4782eabff43 | 257 | send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 36:d4782eabff43 | 258 | } |
fkellermavnet | 36:d4782eabff43 | 259 | } |
fkellermavnet | 36:d4782eabff43 | 260 | while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 36:d4782eabff43 | 261 | // One time during power up to set the APN |
fkellermavnet | 36:d4782eabff43 | 262 | pdnSet = true; |
fkellermavnet | 36:d4782eabff43 | 263 | |
fkellermavnet | 36:d4782eabff43 | 264 | // send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 36:d4782eabff43 | 265 | // send_wnc_cmd("AT+CFUN=0", &pRespStr, WNC_TIMEOUT_MS); // Try to force on network pt1 |
fkellermavnet | 36:d4782eabff43 | 266 | // send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 36:d4782eabff43 | 267 | // send_wnc_cmd("AT+CFUN=1", &pRespStr, WNC_TIMEOUT_MS); // Try to force on network pt2 |
fkellermavnet | 36:d4782eabff43 | 268 | // send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 36:d4782eabff43 | 269 | pc.puts("WNC Status:\r\n"); |
fkellermavnet | 38:564b312a719f | 270 | send_wnc_cmd("AT@SOCKDIAL?", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 36:d4782eabff43 | 271 | send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS); // Check if SIM locked |
fkellermavnet | 36:d4782eabff43 | 272 | send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS); // Check if registered on network |
fkellermavnet | 36:d4782eabff43 | 273 | send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS); // Check RSSI |
fkellermavnet | 36:d4782eabff43 | 274 | pc.puts("End AT init of WNC\r\n"); |
JMF | 2:0e2ef866af95 | 275 | } |
JMF | 2:0e2ef866af95 | 276 | |
JMF | 2:0e2ef866af95 | 277 | void at_sockopen_wnc(const string & ipStr, const char * port ) |
JMF | 2:0e2ef866af95 | 278 | { |
JMF | 2:0e2ef866af95 | 279 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 280 | send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 281 | string cmd_str("AT@SOCKCONN=1,\""); |
JMF | 2:0e2ef866af95 | 282 | cmd_str += ipStr; |
JMF | 2:0e2ef866af95 | 283 | cmd_str += "\","; |
JMF | 2:0e2ef866af95 | 284 | cmd_str += port; |
fkellermavnet | 38:564b312a719f | 285 | cmd_str += ",30"; |
fkellermavnet | 39:1996eaec02d6 | 286 | send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000); |
fkellermavnet | 36:d4782eabff43 | 287 | send_wnc_cmd("AT@SOCKCREAT?", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 36:d4782eabff43 | 288 | send_wnc_cmd("AT@SOCKCONN?", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 289 | } |
JMF | 2:0e2ef866af95 | 290 | |
JMF | 2:0e2ef866af95 | 291 | void at_sockclose_wnc(void) |
JMF | 2:0e2ef866af95 | 292 | { |
JMF | 2:0e2ef866af95 | 293 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 294 | send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 295 | } |
JMF | 2:0e2ef866af95 | 296 | |
JMF | 2:0e2ef866af95 | 297 | int at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 2:0e2ef866af95 | 298 | { |
JMF | 2:0e2ef866af95 | 299 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 300 | string str(s); |
JMF | 2:0e2ef866af95 | 301 | str = "AT@DNSRESVDON=\"" + str; |
JMF | 2:0e2ef866af95 | 302 | str += "\"\r\n"; |
fkellermavnet | 30:33be8e2992f3 | 303 | if (send_wnc_cmd(str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0) |
JMF | 2:0e2ef866af95 | 304 | { |
JMF | 2:0e2ef866af95 | 305 | size_t pos_start = pRespStr->find(":\"") + 2; |
JMF | 2:0e2ef866af95 | 306 | if (pos_start != string::npos) |
JMF | 2:0e2ef866af95 | 307 | { |
JMF | 2:0e2ef866af95 | 308 | size_t pos_end = pRespStr->rfind("\"") - 1; |
JMF | 2:0e2ef866af95 | 309 | if (pos_end != string::npos) |
JMF | 2:0e2ef866af95 | 310 | { |
JMF | 2:0e2ef866af95 | 311 | if (pos_end > pos_start) |
JMF | 2:0e2ef866af95 | 312 | { |
JMF | 2:0e2ef866af95 | 313 | // Make a copy for use later (the source string is re-used) |
JMF | 2:0e2ef866af95 | 314 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 2:0e2ef866af95 | 315 | return 1; |
JMF | 2:0e2ef866af95 | 316 | } |
JMF | 2:0e2ef866af95 | 317 | else |
JMF | 2:0e2ef866af95 | 318 | pc.puts("URL Resolve fail, substr Err\r\n"); |
JMF | 2:0e2ef866af95 | 319 | } |
JMF | 2:0e2ef866af95 | 320 | else |
JMF | 2:0e2ef866af95 | 321 | pc.puts("URL Resolve fail, no 2nd quote\r\n"); |
JMF | 2:0e2ef866af95 | 322 | } |
JMF | 2:0e2ef866af95 | 323 | else |
JMF | 2:0e2ef866af95 | 324 | pc.puts("URL Resolve fail, no quotes\r\n"); |
JMF | 2:0e2ef866af95 | 325 | } |
JMF | 2:0e2ef866af95 | 326 | else |
JMF | 2:0e2ef866af95 | 327 | pc.puts("URL Resolve fail, WNC cmd fail\r\n"); |
JMF | 2:0e2ef866af95 | 328 | |
JMF | 2:0e2ef866af95 | 329 | return -1; |
JMF | 2:0e2ef866af95 | 330 | } |
JMF | 2:0e2ef866af95 | 331 | |
JMF | 2:0e2ef866af95 | 332 | void at_sockwrite_wnc(const char * s) |
JMF | 2:0e2ef866af95 | 333 | { |
JMF | 2:0e2ef866af95 | 334 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 335 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 336 | size_t sLen = strlen(s); |
fkellermavnet | 39:1996eaec02d6 | 337 | if (sLen <= 1500) |
JMF | 2:0e2ef866af95 | 338 | { |
JMF | 2:0e2ef866af95 | 339 | string cmd_str("AT@SOCKWRITE=1,"); |
JMF | 2:0e2ef866af95 | 340 | itoa(sLen, num2str, 10); |
JMF | 2:0e2ef866af95 | 341 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 342 | cmd_str += ",\""; |
JMF | 2:0e2ef866af95 | 343 | while(*s != '\0') |
JMF | 2:0e2ef866af95 | 344 | { |
JMF | 2:0e2ef866af95 | 345 | itoa((int)*s++, num2str, 16); |
JMF | 2:0e2ef866af95 | 346 | // Always 2-digit ascii hex: |
JMF | 2:0e2ef866af95 | 347 | if (strlen(num2str) == 1) |
JMF | 2:0e2ef866af95 | 348 | { |
JMF | 2:0e2ef866af95 | 349 | num2str[2] = '\0'; |
JMF | 2:0e2ef866af95 | 350 | num2str[1] = num2str[0]; |
JMF | 2:0e2ef866af95 | 351 | num2str[0] = '0'; |
JMF | 2:0e2ef866af95 | 352 | } |
JMF | 2:0e2ef866af95 | 353 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 354 | } |
JMF | 2:0e2ef866af95 | 355 | cmd_str += "\""; |
fkellermavnet | 39:1996eaec02d6 | 356 | send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000); |
JMF | 2:0e2ef866af95 | 357 | } |
JMF | 2:0e2ef866af95 | 358 | else |
JMF | 2:0e2ef866af95 | 359 | pc.puts("sockwrite Err, string to long\r\n"); |
JMF | 2:0e2ef866af95 | 360 | } |
JMF | 2:0e2ef866af95 | 361 | |
JMF | 2:0e2ef866af95 | 362 | unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0) |
JMF | 2:0e2ef866af95 | 363 | { |
fkellermavnet | 34:1a4498e3580e | 364 | unsigned i, numBytes = 0; |
JMF | 2:0e2ef866af95 | 365 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 366 | string cmd_str("AT@SOCKREAD=1,"); |
fkellermavnet | 30:33be8e2992f3 | 367 | |
JMF | 2:0e2ef866af95 | 368 | if (n <= 1500) |
JMF | 2:0e2ef866af95 | 369 | { |
JMF | 2:0e2ef866af95 | 370 | char num2str[6]; |
fkellermavnet | 22:41e6c417ace1 | 371 | |
JMF | 2:0e2ef866af95 | 372 | itoa(n, num2str, 10); |
JMF | 2:0e2ef866af95 | 373 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 374 | retries += 1; |
JMF | 2:0e2ef866af95 | 375 | while (retries--) |
JMF | 2:0e2ef866af95 | 376 | { |
fkellermavnet | 22:41e6c417ace1 | 377 | // Assuming someone is sending then calling this to receive response, invoke |
fkellermavnet | 22:41e6c417ace1 | 378 | // a pause to give the response some time to come back and then also |
fkellermavnet | 22:41e6c417ace1 | 379 | // between each retry. |
fkellermavnet | 22:41e6c417ace1 | 380 | wait_ms(10); |
fkellermavnet | 22:41e6c417ace1 | 381 | |
fkellermavnet | 30:33be8e2992f3 | 382 | send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 383 | size_t pos_start = pRespStr->find("\"") + 1; |
JMF | 2:0e2ef866af95 | 384 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 34:1a4498e3580e | 385 | i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte |
JMF | 2:0e2ef866af95 | 386 | if (i > 0) |
JMF | 2:0e2ef866af95 | 387 | { |
fkellermavnet | 34:1a4498e3580e | 388 | retries = 1; // If any data found retry 1 more time to catch data that might be in another |
fkellermavnet | 34:1a4498e3580e | 389 | // WNC payload |
JMF | 2:0e2ef866af95 | 390 | string byte; |
JMF | 2:0e2ef866af95 | 391 | while (pos_start < pos_end) |
JMF | 2:0e2ef866af95 | 392 | { |
JMF | 2:0e2ef866af95 | 393 | byte = pRespStr->substr(pos_start, 2); |
fkellermavnet | 30:33be8e2992f3 | 394 | *pS += (char)strtol(byte.c_str(), NULL, 16); |
JMF | 2:0e2ef866af95 | 395 | pos_start += 2; |
JMF | 2:0e2ef866af95 | 396 | } |
fkellermavnet | 34:1a4498e3580e | 397 | numBytes += i/2; |
JMF | 2:0e2ef866af95 | 398 | } |
JMF | 2:0e2ef866af95 | 399 | } |
fkellermavnet | 34:1a4498e3580e | 400 | return numBytes; |
JMF | 2:0e2ef866af95 | 401 | } |
JMF | 2:0e2ef866af95 | 402 | else |
JMF | 2:0e2ef866af95 | 403 | pc.puts("sockread Err, to many to read\r\n"); |
JMF | 2:0e2ef866af95 | 404 | |
JMF | 2:0e2ef866af95 | 405 | return 0; |
JMF | 2:0e2ef866af95 | 406 | } |