![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A simple project for sending button input up to AT&T Flow
Dependencies: FXOS8700CQ MODSERIAL mbed
For instructions on using this program, see the AT&T Starter Kit tutorial on it.
wnc_control.cpp@10:df54436ecd38, 2016-07-11 (annotated)
- Committer:
- fkellermavnet
- Date:
- Mon Jul 11 23:54:24 2016 +0000
- Revision:
- 10:df54436ecd38
- Parent:
- 2:0e2ef866af95
- Child:
- 19:f89baed3bd6f
Changed wnc control code so that it can't do a read or a write to a socket if the socket is not opened successfully.
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 | { |
JMF | 2:0e2ef866af95 | 65 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 66 | at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR); |
JMF | 2:0e2ef866af95 | 67 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 68 | { |
JMF | 2:0e2ef866af95 | 69 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 70 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 71 | } |
JMF | 2:0e2ef866af95 | 72 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 73 | pc.puts("Socket open fail!!!!\r\n"); |
fkellermavnet | 10:df54436ecd38 | 74 | else |
fkellermavnet | 10:df54436ecd38 | 75 | socketOpen = 1; |
JMF | 2:0e2ef866af95 | 76 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 77 | } |
JMF | 2:0e2ef866af95 | 78 | |
JMF | 2:0e2ef866af95 | 79 | void sockwrite_mdm(const char * s) |
JMF | 2:0e2ef866af95 | 80 | { |
fkellermavnet | 10:df54436ecd38 | 81 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 82 | { |
JMF | 2:0e2ef866af95 | 83 | do |
JMF | 2:0e2ef866af95 | 84 | { |
JMF | 2:0e2ef866af95 | 85 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 86 | at_sockwrite_wnc(s); |
JMF | 2:0e2ef866af95 | 87 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 88 | { |
JMF | 2:0e2ef866af95 | 89 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 90 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 91 | } |
JMF | 2:0e2ef866af95 | 92 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 93 | { |
JMF | 2:0e2ef866af95 | 94 | pc.puts("Socket Write fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 95 | // Have seen when write fails modem gets stuck in bad state, try to recover |
JMF | 2:0e2ef866af95 | 96 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 97 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 98 | } |
JMF | 2:0e2ef866af95 | 99 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 100 | } |
fkellermavnet | 10:df54436ecd38 | 101 | else |
fkellermavnet | 10:df54436ecd38 | 102 | puts("Socket is closed for write!\r\n"); |
JMF | 2:0e2ef866af95 | 103 | } |
JMF | 2:0e2ef866af95 | 104 | |
JMF | 2:0e2ef866af95 | 105 | void sockread_mdm(string * sockData, int len, int retries) |
JMF | 2:0e2ef866af95 | 106 | { |
fkellermavnet | 10:df54436ecd38 | 107 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 108 | { |
JMF | 2:0e2ef866af95 | 109 | do |
JMF | 2:0e2ef866af95 | 110 | { |
JMF | 2:0e2ef866af95 | 111 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 112 | at_sockread_wnc(sockData, len, retries); |
JMF | 2:0e2ef866af95 | 113 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 114 | { |
JMF | 2:0e2ef866af95 | 115 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 116 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 117 | } |
JMF | 2:0e2ef866af95 | 118 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 119 | puts("Sock read fail!!!!\r\n"); |
JMF | 2:0e2ef866af95 | 120 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 121 | } |
fkellermavnet | 10:df54436ecd38 | 122 | else |
fkellermavnet | 10:df54436ecd38 | 123 | puts("Socket is closed for read\r\n"); |
JMF | 2:0e2ef866af95 | 124 | } |
JMF | 2:0e2ef866af95 | 125 | |
JMF | 2:0e2ef866af95 | 126 | void sockclose_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_sockclose_wnc(); |
fkellermavnet | 10:df54436ecd38 | 132 | // Assume close happened even if it went bad |
fkellermavnet | 10:df54436ecd38 | 133 | // going bad will result in a re-init anyways and if close |
fkellermavnet | 10:df54436ecd38 | 134 | // fails we're pretty much in bad state and not much can do |
fkellermavnet | 10:df54436ecd38 | 135 | socketOpen = 0; |
JMF | 2:0e2ef866af95 | 136 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 137 | { |
JMF | 2:0e2ef866af95 | 138 | reinitialize_mdm(); |
JMF | 2:0e2ef866af95 | 139 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 140 | } |
JMF | 2:0e2ef866af95 | 141 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 142 | puts("Sock close fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 143 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 144 | } |
JMF | 2:0e2ef866af95 | 145 | |
JMF | 2:0e2ef866af95 | 146 | /** |
JMF | 2:0e2ef866af95 | 147 | * C++ version 0.4 char* style "itoa": |
JMF | 2:0e2ef866af95 | 148 | * Written by Lukás Chmela |
JMF | 2:0e2ef866af95 | 149 | * Released under GPLv3. |
JMF | 2:0e2ef866af95 | 150 | */ |
JMF | 2:0e2ef866af95 | 151 | |
JMF | 2:0e2ef866af95 | 152 | char* itoa(int value, char* result, int base) |
JMF | 2:0e2ef866af95 | 153 | { |
JMF | 2:0e2ef866af95 | 154 | // check that the base if valid |
JMF | 2:0e2ef866af95 | 155 | if ( base < 2 || base > 36 ) { |
JMF | 2:0e2ef866af95 | 156 | *result = '\0'; |
JMF | 2:0e2ef866af95 | 157 | return result; |
JMF | 2:0e2ef866af95 | 158 | } |
JMF | 2:0e2ef866af95 | 159 | |
JMF | 2:0e2ef866af95 | 160 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 2:0e2ef866af95 | 161 | int tmp_value; |
JMF | 2:0e2ef866af95 | 162 | |
JMF | 2:0e2ef866af95 | 163 | do { |
JMF | 2:0e2ef866af95 | 164 | tmp_value = value; |
JMF | 2:0e2ef866af95 | 165 | value /= base; |
JMF | 2:0e2ef866af95 | 166 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 2:0e2ef866af95 | 167 | } while ( value ); |
JMF | 2:0e2ef866af95 | 168 | |
JMF | 2:0e2ef866af95 | 169 | // Apply negative sign |
JMF | 2:0e2ef866af95 | 170 | if ( tmp_value < 0 ) |
JMF | 2:0e2ef866af95 | 171 | *ptr++ = '-'; |
JMF | 2:0e2ef866af95 | 172 | *ptr-- = '\0'; |
JMF | 2:0e2ef866af95 | 173 | |
JMF | 2:0e2ef866af95 | 174 | while ( ptr1 < ptr ) { |
JMF | 2:0e2ef866af95 | 175 | tmp_char = *ptr; |
JMF | 2:0e2ef866af95 | 176 | *ptr-- = *ptr1; |
JMF | 2:0e2ef866af95 | 177 | *ptr1++ = tmp_char; |
JMF | 2:0e2ef866af95 | 178 | } |
JMF | 2:0e2ef866af95 | 179 | |
JMF | 2:0e2ef866af95 | 180 | return result; |
JMF | 2:0e2ef866af95 | 181 | } |
JMF | 2:0e2ef866af95 | 182 | |
JMF | 2:0e2ef866af95 | 183 | extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len); |
JMF | 2:0e2ef866af95 | 184 | |
JMF | 2:0e2ef866af95 | 185 | // Sets a global with failure or success, assumes 1 thread all the time |
JMF | 2:0e2ef866af95 | 186 | int send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 2:0e2ef866af95 | 187 | { |
JMF | 2:0e2ef866af95 | 188 | static const char * rsp_lst[] = { "OK", "ERROR", NULL }; |
JMF | 2:0e2ef866af95 | 189 | int len; |
JMF | 2:0e2ef866af95 | 190 | |
JMF | 2:0e2ef866af95 | 191 | pc.printf("Send: %s\r\n",s); |
JMF | 2:0e2ef866af95 | 192 | int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len); |
JMF | 2:0e2ef866af95 | 193 | *r = &wncStr; // Return a pointer to the static string |
JMF | 2:0e2ef866af95 | 194 | |
JMF | 2:0e2ef866af95 | 195 | if (res >= 0) |
JMF | 2:0e2ef866af95 | 196 | { |
JMF | 2:0e2ef866af95 | 197 | pc.puts("["); |
JMF | 2:0e2ef866af95 | 198 | pc.puts(wncStr.data()); |
JMF | 2:0e2ef866af95 | 199 | pc.puts("]\n\r"); |
JMF | 2:0e2ef866af95 | 200 | if (res > 0) |
JMF | 2:0e2ef866af95 | 201 | { |
JMF | 2:0e2ef866af95 | 202 | if (WNC_MDM_ERR != WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 203 | WNC_MDM_ERR = WNC_CMD_ERR; |
JMF | 2:0e2ef866af95 | 204 | return -1; |
JMF | 2:0e2ef866af95 | 205 | } |
JMF | 2:0e2ef866af95 | 206 | else |
JMF | 2:0e2ef866af95 | 207 | return 0; |
JMF | 2:0e2ef866af95 | 208 | } |
JMF | 2:0e2ef866af95 | 209 | else |
JMF | 2:0e2ef866af95 | 210 | { |
JMF | 2:0e2ef866af95 | 211 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
JMF | 2:0e2ef866af95 | 212 | pc.puts("No response from WNC!\n\r"); |
JMF | 2:0e2ef866af95 | 213 | return -2; |
JMF | 2:0e2ef866af95 | 214 | } |
JMF | 2:0e2ef866af95 | 215 | } |
JMF | 2:0e2ef866af95 | 216 | |
JMF | 2:0e2ef866af95 | 217 | void at_init_wnc(void) |
JMF | 2:0e2ef866af95 | 218 | { |
JMF | 2:0e2ef866af95 | 219 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 220 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
JMF | 2:0e2ef866af95 | 221 | send_wnc_cmd("ATE1", &pRespStr, WNC_TIMEOUT_MS); // Echo ON |
JMF | 2:0e2ef866af95 | 222 | string cmd_str("AT%PDNSET=1,"); |
JMF | 2:0e2ef866af95 | 223 | cmd_str += MY_APN_STR; |
JMF | 2:0e2ef866af95 | 224 | cmd_str += ",IP"; |
JMF | 2:0e2ef866af95 | 225 | send_wnc_cmd(cmd_str.data(), &pRespStr, 2*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes |
JMF | 2:0e2ef866af95 | 226 | send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); // Internet services enabled |
JMF | 2:0e2ef866af95 | 227 | send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 228 | } |
JMF | 2:0e2ef866af95 | 229 | |
JMF | 2:0e2ef866af95 | 230 | void at_sockopen_wnc(const string & ipStr, const char * port ) |
JMF | 2:0e2ef866af95 | 231 | { |
JMF | 2:0e2ef866af95 | 232 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 233 | send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 234 | string cmd_str("AT@SOCKCONN=1,\""); |
JMF | 2:0e2ef866af95 | 235 | cmd_str += ipStr; |
JMF | 2:0e2ef866af95 | 236 | cmd_str += "\","; |
JMF | 2:0e2ef866af95 | 237 | cmd_str += port; |
JMF | 2:0e2ef866af95 | 238 | send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 239 | } |
JMF | 2:0e2ef866af95 | 240 | |
JMF | 2:0e2ef866af95 | 241 | void at_sockclose_wnc(void) |
JMF | 2:0e2ef866af95 | 242 | { |
JMF | 2:0e2ef866af95 | 243 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 244 | send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 245 | } |
JMF | 2:0e2ef866af95 | 246 | |
JMF | 2:0e2ef866af95 | 247 | int at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 2:0e2ef866af95 | 248 | { |
JMF | 2:0e2ef866af95 | 249 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 250 | string str(s); |
JMF | 2:0e2ef866af95 | 251 | str = "AT@DNSRESVDON=\"" + str; |
JMF | 2:0e2ef866af95 | 252 | str += "\"\r\n"; |
JMF | 2:0e2ef866af95 | 253 | if (send_wnc_cmd(str.data(), &pRespStr, WNC_TIMEOUT_MS) == 0) |
JMF | 2:0e2ef866af95 | 254 | { |
JMF | 2:0e2ef866af95 | 255 | size_t pos_start = pRespStr->find(":\"") + 2; |
JMF | 2:0e2ef866af95 | 256 | if (pos_start != string::npos) |
JMF | 2:0e2ef866af95 | 257 | { |
JMF | 2:0e2ef866af95 | 258 | size_t pos_end = pRespStr->rfind("\"") - 1; |
JMF | 2:0e2ef866af95 | 259 | if (pos_end != string::npos) |
JMF | 2:0e2ef866af95 | 260 | { |
JMF | 2:0e2ef866af95 | 261 | if (pos_end > pos_start) |
JMF | 2:0e2ef866af95 | 262 | { |
JMF | 2:0e2ef866af95 | 263 | // Make a copy for use later (the source string is re-used) |
JMF | 2:0e2ef866af95 | 264 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 2:0e2ef866af95 | 265 | return 1; |
JMF | 2:0e2ef866af95 | 266 | } |
JMF | 2:0e2ef866af95 | 267 | else |
JMF | 2:0e2ef866af95 | 268 | pc.puts("URL Resolve fail, substr Err\r\n"); |
JMF | 2:0e2ef866af95 | 269 | } |
JMF | 2:0e2ef866af95 | 270 | else |
JMF | 2:0e2ef866af95 | 271 | pc.puts("URL Resolve fail, no 2nd quote\r\n"); |
JMF | 2:0e2ef866af95 | 272 | } |
JMF | 2:0e2ef866af95 | 273 | else |
JMF | 2:0e2ef866af95 | 274 | pc.puts("URL Resolve fail, no quotes\r\n"); |
JMF | 2:0e2ef866af95 | 275 | } |
JMF | 2:0e2ef866af95 | 276 | else |
JMF | 2:0e2ef866af95 | 277 | pc.puts("URL Resolve fail, WNC cmd fail\r\n"); |
JMF | 2:0e2ef866af95 | 278 | |
JMF | 2:0e2ef866af95 | 279 | return -1; |
JMF | 2:0e2ef866af95 | 280 | } |
JMF | 2:0e2ef866af95 | 281 | |
JMF | 2:0e2ef866af95 | 282 | void at_sockwrite_wnc(const char * s) |
JMF | 2:0e2ef866af95 | 283 | { |
JMF | 2:0e2ef866af95 | 284 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 285 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 286 | size_t sLen = strlen(s); |
JMF | 2:0e2ef866af95 | 287 | if (sLen <= 99999) |
JMF | 2:0e2ef866af95 | 288 | { |
JMF | 2:0e2ef866af95 | 289 | string cmd_str("AT@SOCKWRITE=1,"); |
JMF | 2:0e2ef866af95 | 290 | itoa(sLen, num2str, 10); |
JMF | 2:0e2ef866af95 | 291 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 292 | cmd_str += ",\""; |
JMF | 2:0e2ef866af95 | 293 | while(*s != '\0') |
JMF | 2:0e2ef866af95 | 294 | { |
JMF | 2:0e2ef866af95 | 295 | itoa((int)*s++, num2str, 16); |
JMF | 2:0e2ef866af95 | 296 | // Always 2-digit ascii hex: |
JMF | 2:0e2ef866af95 | 297 | if (strlen(num2str) == 1) |
JMF | 2:0e2ef866af95 | 298 | { |
JMF | 2:0e2ef866af95 | 299 | num2str[2] = '\0'; |
JMF | 2:0e2ef866af95 | 300 | num2str[1] = num2str[0]; |
JMF | 2:0e2ef866af95 | 301 | num2str[0] = '0'; |
JMF | 2:0e2ef866af95 | 302 | } |
JMF | 2:0e2ef866af95 | 303 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 304 | } |
JMF | 2:0e2ef866af95 | 305 | cmd_str += "\""; |
JMF | 2:0e2ef866af95 | 306 | send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 307 | } |
JMF | 2:0e2ef866af95 | 308 | else |
JMF | 2:0e2ef866af95 | 309 | pc.puts("sockwrite Err, string to long\r\n"); |
JMF | 2:0e2ef866af95 | 310 | } |
JMF | 2:0e2ef866af95 | 311 | |
JMF | 2:0e2ef866af95 | 312 | unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0) |
JMF | 2:0e2ef866af95 | 313 | { |
JMF | 2:0e2ef866af95 | 314 | unsigned i; |
JMF | 2:0e2ef866af95 | 315 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 316 | string cmd_str("AT@SOCKREAD=1,"); |
JMF | 2:0e2ef866af95 | 317 | if (n <= 1500) |
JMF | 2:0e2ef866af95 | 318 | { |
JMF | 2:0e2ef866af95 | 319 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 320 | itoa(n, num2str, 10); |
JMF | 2:0e2ef866af95 | 321 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 322 | retries += 1; |
JMF | 2:0e2ef866af95 | 323 | while (retries--) |
JMF | 2:0e2ef866af95 | 324 | { |
JMF | 2:0e2ef866af95 | 325 | send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 326 | size_t pos_start = pRespStr->find("\"") + 1; |
JMF | 2:0e2ef866af95 | 327 | size_t pos_end = pRespStr->rfind("\"") - 1; |
JMF | 2:0e2ef866af95 | 328 | i = pos_end - pos_start + 1; |
JMF | 2:0e2ef866af95 | 329 | if (i > 0) |
JMF | 2:0e2ef866af95 | 330 | { |
JMF | 2:0e2ef866af95 | 331 | retries = 0; // If any data found stop retrying |
JMF | 2:0e2ef866af95 | 332 | string byte; |
JMF | 2:0e2ef866af95 | 333 | pS->erase(); |
JMF | 2:0e2ef866af95 | 334 | while (pos_start < pos_end) |
JMF | 2:0e2ef866af95 | 335 | { |
JMF | 2:0e2ef866af95 | 336 | byte = pRespStr->substr(pos_start, 2); |
JMF | 2:0e2ef866af95 | 337 | *pS += (char)strtol(byte.data(), NULL, 16); |
JMF | 2:0e2ef866af95 | 338 | pos_start += 2; |
JMF | 2:0e2ef866af95 | 339 | } |
JMF | 2:0e2ef866af95 | 340 | return i; |
JMF | 2:0e2ef866af95 | 341 | } |
JMF | 2:0e2ef866af95 | 342 | } |
JMF | 2:0e2ef866af95 | 343 | } |
JMF | 2:0e2ef866af95 | 344 | else |
JMF | 2:0e2ef866af95 | 345 | pc.puts("sockread Err, to many to read\r\n"); |
JMF | 2:0e2ef866af95 | 346 | |
JMF | 2:0e2ef866af95 | 347 | return 0; |
JMF | 2:0e2ef866af95 | 348 | } |