![](/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@19:f89baed3bd6f, 2016-07-13 (annotated)
- Committer:
- fkellermavnet
- Date:
- Wed Jul 13 17:35:14 2016 +0000
- Revision:
- 19:f89baed3bd6f
- Parent:
- 10:df54436ecd38
- Child:
- 22:41e6c417ace1
Right before the SOCKCONN during the WNC software initialization once in a while the WNC seems to be dropping a char that is sent to it, 'A' specifically. So I put in 2 "AT" commands right before the SOCKCONN and that seems to clean it up.
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(); |
JMF | 2:0e2ef866af95 | 34 | if (WNC_MDM_ERR != WNC_OK) |
JMF | 2:0e2ef866af95 | 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 | { |
JMF | 2:0e2ef866af95 | 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 | |
JMF | 2:0e2ef866af95 | 58 | pc.printf("My Server IP: %s\r\n", MyServerIpAddress.data()); |
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 | { |
JMF | 2:0e2ef866af95 | 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 | { |
JMF | 2:0e2ef866af95 | 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 |
JMF | 2:0e2ef866af95 | 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 | |
JMF | 2:0e2ef866af95 | 107 | void sockread_mdm(string * sockData, int len, int retries) |
JMF | 2:0e2ef866af95 | 108 | { |
fkellermavnet | 10:df54436ecd38 | 109 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 110 | { |
JMF | 2:0e2ef866af95 | 111 | do |
JMF | 2:0e2ef866af95 | 112 | { |
JMF | 2:0e2ef866af95 | 113 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 114 | at_sockread_wnc(sockData, len, retries); |
JMF | 2:0e2ef866af95 | 115 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 116 | { |
JMF | 2:0e2ef866af95 | 117 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 118 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 119 | } |
JMF | 2:0e2ef866af95 | 120 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 121 | puts("Sock read fail!!!!\r\n"); |
JMF | 2:0e2ef866af95 | 122 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 123 | } |
fkellermavnet | 10:df54436ecd38 | 124 | else |
fkellermavnet | 10:df54436ecd38 | 125 | puts("Socket is closed for read\r\n"); |
JMF | 2:0e2ef866af95 | 126 | } |
JMF | 2:0e2ef866af95 | 127 | |
JMF | 2:0e2ef866af95 | 128 | void sockclose_mdm(void) |
JMF | 2:0e2ef866af95 | 129 | { |
JMF | 2:0e2ef866af95 | 130 | do |
JMF | 2:0e2ef866af95 | 131 | { |
JMF | 2:0e2ef866af95 | 132 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 133 | at_sockclose_wnc(); |
fkellermavnet | 10:df54436ecd38 | 134 | // Assume close happened even if it went bad |
fkellermavnet | 10:df54436ecd38 | 135 | // going bad will result in a re-init anyways and if close |
fkellermavnet | 10:df54436ecd38 | 136 | // fails we're pretty much in bad state and not much can do |
fkellermavnet | 10:df54436ecd38 | 137 | socketOpen = 0; |
JMF | 2:0e2ef866af95 | 138 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 139 | { |
JMF | 2:0e2ef866af95 | 140 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 141 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 142 | } |
JMF | 2:0e2ef866af95 | 143 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 144 | puts("Sock close fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 145 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 146 | } |
JMF | 2:0e2ef866af95 | 147 | |
JMF | 2:0e2ef866af95 | 148 | /** |
JMF | 2:0e2ef866af95 | 149 | * C++ version 0.4 char* style "itoa": |
JMF | 2:0e2ef866af95 | 150 | * Written by Lukás Chmela |
JMF | 2:0e2ef866af95 | 151 | * Released under GPLv3. |
JMF | 2:0e2ef866af95 | 152 | */ |
JMF | 2:0e2ef866af95 | 153 | |
JMF | 2:0e2ef866af95 | 154 | char* itoa(int value, char* result, int base) |
JMF | 2:0e2ef866af95 | 155 | { |
JMF | 2:0e2ef866af95 | 156 | // check that the base if valid |
JMF | 2:0e2ef866af95 | 157 | if ( base < 2 || base > 36 ) { |
JMF | 2:0e2ef866af95 | 158 | *result = '\0'; |
JMF | 2:0e2ef866af95 | 159 | return result; |
JMF | 2:0e2ef866af95 | 160 | } |
JMF | 2:0e2ef866af95 | 161 | |
JMF | 2:0e2ef866af95 | 162 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 2:0e2ef866af95 | 163 | int tmp_value; |
JMF | 2:0e2ef866af95 | 164 | |
JMF | 2:0e2ef866af95 | 165 | do { |
JMF | 2:0e2ef866af95 | 166 | tmp_value = value; |
JMF | 2:0e2ef866af95 | 167 | value /= base; |
JMF | 2:0e2ef866af95 | 168 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 2:0e2ef866af95 | 169 | } while ( value ); |
JMF | 2:0e2ef866af95 | 170 | |
JMF | 2:0e2ef866af95 | 171 | // Apply negative sign |
JMF | 2:0e2ef866af95 | 172 | if ( tmp_value < 0 ) |
JMF | 2:0e2ef866af95 | 173 | *ptr++ = '-'; |
JMF | 2:0e2ef866af95 | 174 | *ptr-- = '\0'; |
JMF | 2:0e2ef866af95 | 175 | |
JMF | 2:0e2ef866af95 | 176 | while ( ptr1 < ptr ) { |
JMF | 2:0e2ef866af95 | 177 | tmp_char = *ptr; |
JMF | 2:0e2ef866af95 | 178 | *ptr-- = *ptr1; |
JMF | 2:0e2ef866af95 | 179 | *ptr1++ = tmp_char; |
JMF | 2:0e2ef866af95 | 180 | } |
JMF | 2:0e2ef866af95 | 181 | |
JMF | 2:0e2ef866af95 | 182 | return result; |
JMF | 2:0e2ef866af95 | 183 | } |
JMF | 2:0e2ef866af95 | 184 | |
JMF | 2:0e2ef866af95 | 185 | extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len); |
JMF | 2:0e2ef866af95 | 186 | |
JMF | 2:0e2ef866af95 | 187 | // Sets a global with failure or success, assumes 1 thread all the time |
JMF | 2:0e2ef866af95 | 188 | int send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 2:0e2ef866af95 | 189 | { |
JMF | 2:0e2ef866af95 | 190 | static const char * rsp_lst[] = { "OK", "ERROR", NULL }; |
JMF | 2:0e2ef866af95 | 191 | int len; |
JMF | 2:0e2ef866af95 | 192 | |
JMF | 2:0e2ef866af95 | 193 | pc.printf("Send: %s\r\n",s); |
JMF | 2:0e2ef866af95 | 194 | int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len); |
JMF | 2:0e2ef866af95 | 195 | *r = &wncStr; // Return a pointer to the static string |
JMF | 2:0e2ef866af95 | 196 | |
JMF | 2:0e2ef866af95 | 197 | if (res >= 0) |
JMF | 2:0e2ef866af95 | 198 | { |
JMF | 2:0e2ef866af95 | 199 | pc.puts("["); |
JMF | 2:0e2ef866af95 | 200 | pc.puts(wncStr.data()); |
JMF | 2:0e2ef866af95 | 201 | pc.puts("]\n\r"); |
JMF | 2:0e2ef866af95 | 202 | if (res > 0) |
JMF | 2:0e2ef866af95 | 203 | { |
JMF | 2:0e2ef866af95 | 204 | if (WNC_MDM_ERR != WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 205 | WNC_MDM_ERR = WNC_CMD_ERR; |
JMF | 2:0e2ef866af95 | 206 | return -1; |
JMF | 2:0e2ef866af95 | 207 | } |
JMF | 2:0e2ef866af95 | 208 | else |
JMF | 2:0e2ef866af95 | 209 | return 0; |
JMF | 2:0e2ef866af95 | 210 | } |
JMF | 2:0e2ef866af95 | 211 | else |
JMF | 2:0e2ef866af95 | 212 | { |
JMF | 2:0e2ef866af95 | 213 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
JMF | 2:0e2ef866af95 | 214 | pc.puts("No response from WNC!\n\r"); |
JMF | 2:0e2ef866af95 | 215 | return -2; |
JMF | 2:0e2ef866af95 | 216 | } |
JMF | 2:0e2ef866af95 | 217 | } |
JMF | 2:0e2ef866af95 | 218 | |
fkellermavnet | 19:f89baed3bd6f | 219 | void at_at_wnc(void) |
fkellermavnet | 19:f89baed3bd6f | 220 | { |
fkellermavnet | 19:f89baed3bd6f | 221 | string * pRespStr; |
fkellermavnet | 19:f89baed3bd6f | 222 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 19:f89baed3bd6f | 223 | } |
fkellermavnet | 19:f89baed3bd6f | 224 | |
JMF | 2:0e2ef866af95 | 225 | void at_init_wnc(void) |
JMF | 2:0e2ef866af95 | 226 | { |
JMF | 2:0e2ef866af95 | 227 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 228 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
JMF | 2:0e2ef866af95 | 229 | send_wnc_cmd("ATE1", &pRespStr, WNC_TIMEOUT_MS); // Echo ON |
JMF | 2:0e2ef866af95 | 230 | string cmd_str("AT%PDNSET=1,"); |
JMF | 2:0e2ef866af95 | 231 | cmd_str += MY_APN_STR; |
JMF | 2:0e2ef866af95 | 232 | cmd_str += ",IP"; |
JMF | 2:0e2ef866af95 | 233 | send_wnc_cmd(cmd_str.data(), &pRespStr, 2*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes |
JMF | 2:0e2ef866af95 | 234 | send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); // Internet services enabled |
JMF | 2:0e2ef866af95 | 235 | send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 236 | } |
JMF | 2:0e2ef866af95 | 237 | |
JMF | 2:0e2ef866af95 | 238 | void at_sockopen_wnc(const string & ipStr, const char * port ) |
JMF | 2:0e2ef866af95 | 239 | { |
JMF | 2:0e2ef866af95 | 240 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 241 | send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 242 | string cmd_str("AT@SOCKCONN=1,\""); |
JMF | 2:0e2ef866af95 | 243 | cmd_str += ipStr; |
JMF | 2:0e2ef866af95 | 244 | cmd_str += "\","; |
JMF | 2:0e2ef866af95 | 245 | cmd_str += port; |
JMF | 2:0e2ef866af95 | 246 | send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 247 | } |
JMF | 2:0e2ef866af95 | 248 | |
JMF | 2:0e2ef866af95 | 249 | void at_sockclose_wnc(void) |
JMF | 2:0e2ef866af95 | 250 | { |
JMF | 2:0e2ef866af95 | 251 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 252 | send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 253 | } |
JMF | 2:0e2ef866af95 | 254 | |
JMF | 2:0e2ef866af95 | 255 | int at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 2:0e2ef866af95 | 256 | { |
JMF | 2:0e2ef866af95 | 257 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 258 | string str(s); |
JMF | 2:0e2ef866af95 | 259 | str = "AT@DNSRESVDON=\"" + str; |
JMF | 2:0e2ef866af95 | 260 | str += "\"\r\n"; |
JMF | 2:0e2ef866af95 | 261 | if (send_wnc_cmd(str.data(), &pRespStr, WNC_TIMEOUT_MS) == 0) |
JMF | 2:0e2ef866af95 | 262 | { |
JMF | 2:0e2ef866af95 | 263 | size_t pos_start = pRespStr->find(":\"") + 2; |
JMF | 2:0e2ef866af95 | 264 | if (pos_start != string::npos) |
JMF | 2:0e2ef866af95 | 265 | { |
JMF | 2:0e2ef866af95 | 266 | size_t pos_end = pRespStr->rfind("\"") - 1; |
JMF | 2:0e2ef866af95 | 267 | if (pos_end != string::npos) |
JMF | 2:0e2ef866af95 | 268 | { |
JMF | 2:0e2ef866af95 | 269 | if (pos_end > pos_start) |
JMF | 2:0e2ef866af95 | 270 | { |
JMF | 2:0e2ef866af95 | 271 | // Make a copy for use later (the source string is re-used) |
JMF | 2:0e2ef866af95 | 272 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 2:0e2ef866af95 | 273 | return 1; |
JMF | 2:0e2ef866af95 | 274 | } |
JMF | 2:0e2ef866af95 | 275 | else |
JMF | 2:0e2ef866af95 | 276 | pc.puts("URL Resolve fail, substr Err\r\n"); |
JMF | 2:0e2ef866af95 | 277 | } |
JMF | 2:0e2ef866af95 | 278 | else |
JMF | 2:0e2ef866af95 | 279 | pc.puts("URL Resolve fail, no 2nd quote\r\n"); |
JMF | 2:0e2ef866af95 | 280 | } |
JMF | 2:0e2ef866af95 | 281 | else |
JMF | 2:0e2ef866af95 | 282 | pc.puts("URL Resolve fail, no quotes\r\n"); |
JMF | 2:0e2ef866af95 | 283 | } |
JMF | 2:0e2ef866af95 | 284 | else |
JMF | 2:0e2ef866af95 | 285 | pc.puts("URL Resolve fail, WNC cmd fail\r\n"); |
JMF | 2:0e2ef866af95 | 286 | |
JMF | 2:0e2ef866af95 | 287 | return -1; |
JMF | 2:0e2ef866af95 | 288 | } |
JMF | 2:0e2ef866af95 | 289 | |
JMF | 2:0e2ef866af95 | 290 | void at_sockwrite_wnc(const char * s) |
JMF | 2:0e2ef866af95 | 291 | { |
JMF | 2:0e2ef866af95 | 292 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 293 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 294 | size_t sLen = strlen(s); |
JMF | 2:0e2ef866af95 | 295 | if (sLen <= 99999) |
JMF | 2:0e2ef866af95 | 296 | { |
JMF | 2:0e2ef866af95 | 297 | string cmd_str("AT@SOCKWRITE=1,"); |
JMF | 2:0e2ef866af95 | 298 | itoa(sLen, num2str, 10); |
JMF | 2:0e2ef866af95 | 299 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 300 | cmd_str += ",\""; |
JMF | 2:0e2ef866af95 | 301 | while(*s != '\0') |
JMF | 2:0e2ef866af95 | 302 | { |
JMF | 2:0e2ef866af95 | 303 | itoa((int)*s++, num2str, 16); |
JMF | 2:0e2ef866af95 | 304 | // Always 2-digit ascii hex: |
JMF | 2:0e2ef866af95 | 305 | if (strlen(num2str) == 1) |
JMF | 2:0e2ef866af95 | 306 | { |
JMF | 2:0e2ef866af95 | 307 | num2str[2] = '\0'; |
JMF | 2:0e2ef866af95 | 308 | num2str[1] = num2str[0]; |
JMF | 2:0e2ef866af95 | 309 | num2str[0] = '0'; |
JMF | 2:0e2ef866af95 | 310 | } |
JMF | 2:0e2ef866af95 | 311 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 312 | } |
JMF | 2:0e2ef866af95 | 313 | cmd_str += "\""; |
JMF | 2:0e2ef866af95 | 314 | send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 315 | } |
JMF | 2:0e2ef866af95 | 316 | else |
JMF | 2:0e2ef866af95 | 317 | pc.puts("sockwrite Err, string to long\r\n"); |
JMF | 2:0e2ef866af95 | 318 | } |
JMF | 2:0e2ef866af95 | 319 | |
JMF | 2:0e2ef866af95 | 320 | unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0) |
JMF | 2:0e2ef866af95 | 321 | { |
JMF | 2:0e2ef866af95 | 322 | unsigned i; |
JMF | 2:0e2ef866af95 | 323 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 324 | string cmd_str("AT@SOCKREAD=1,"); |
JMF | 2:0e2ef866af95 | 325 | if (n <= 1500) |
JMF | 2:0e2ef866af95 | 326 | { |
JMF | 2:0e2ef866af95 | 327 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 328 | itoa(n, num2str, 10); |
JMF | 2:0e2ef866af95 | 329 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 330 | retries += 1; |
JMF | 2:0e2ef866af95 | 331 | while (retries--) |
JMF | 2:0e2ef866af95 | 332 | { |
JMF | 2:0e2ef866af95 | 333 | send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 334 | size_t pos_start = pRespStr->find("\"") + 1; |
JMF | 2:0e2ef866af95 | 335 | size_t pos_end = pRespStr->rfind("\"") - 1; |
JMF | 2:0e2ef866af95 | 336 | i = pos_end - pos_start + 1; |
JMF | 2:0e2ef866af95 | 337 | if (i > 0) |
JMF | 2:0e2ef866af95 | 338 | { |
JMF | 2:0e2ef866af95 | 339 | retries = 0; // If any data found stop retrying |
JMF | 2:0e2ef866af95 | 340 | string byte; |
JMF | 2:0e2ef866af95 | 341 | pS->erase(); |
JMF | 2:0e2ef866af95 | 342 | while (pos_start < pos_end) |
JMF | 2:0e2ef866af95 | 343 | { |
JMF | 2:0e2ef866af95 | 344 | byte = pRespStr->substr(pos_start, 2); |
JMF | 2:0e2ef866af95 | 345 | *pS += (char)strtol(byte.data(), NULL, 16); |
JMF | 2:0e2ef866af95 | 346 | pos_start += 2; |
JMF | 2:0e2ef866af95 | 347 | } |
JMF | 2:0e2ef866af95 | 348 | return i; |
JMF | 2:0e2ef866af95 | 349 | } |
JMF | 2:0e2ef866af95 | 350 | } |
JMF | 2:0e2ef866af95 | 351 | } |
JMF | 2:0e2ef866af95 | 352 | else |
JMF | 2:0e2ef866af95 | 353 | pc.puts("sockread Err, to many to read\r\n"); |
JMF | 2:0e2ef866af95 | 354 | |
JMF | 2:0e2ef866af95 | 355 | return 0; |
JMF | 2:0e2ef866af95 | 356 | } |