Pubnub demo for AT&T IoT Starter Kit. Functionally similar to the Flow demo.
Dependencies: FXOS8700CQ MODSERIAL mbed
Pubnub demo for AT&T IoT Starter Kit
This demo is functionally similar to the Flow demo, so you can find general information here: https://developer.mbed.org/users/JMF/code/Avnet_ATT_Cellular_IOT/.
The only difference is that we use Pubnub to publish the measurements and subscribe to receiving the instructions to set the LED.
Settings
Pubnub related settings are:
Pubnub settings in `config_me.h`
PUBNUB_SUBSCRIBE_KEY PUBNUB_PUBLISH_KEY PUBNUB_CHANNEL
All are documented in their respective comments.
Pubnub context class
Similar to Pubnub SDKs, we provide a Pubnub context class. It is defined in pubnub.h
header file and implemented in pubnub.cpp
.
It provides only the fundamental "publish" and "subscribe" methods. They are documented in the header file.
This class is reusable in other code (it is not specific to this demo), it has a very narrow interface to the AT&T IoT cellular modem code. For example of use, you can look at the main()
(in main.c
).
Sample of published data
Published message w/measurement data
{"serial":"vstarterkit001","temp":89.61,"humidity":35,"accelX":0.97,"accelY":0.013,"accelZ":-0.038}
Don't worry, nobody got burnt, the temperature is in degrees Fahrenheit. :)
Publish a message (from, say, the Pubnub console http://pubnub.com/console) of the form {"LED":<name-of-the-color>}
on the channel that this demo listens to (default is hello_world
) to turn the LED to that color on the Starter Kit:
Turn LED to red
{"LED":"Red"}
Turn LED to green
{"LED":"Green"}
Turn LED to blue
{"LED":"Blue"}
wnc_control.cpp@75:8cc98a3b9c62, 2016-08-13 (annotated)
- Committer:
- fkellermavnet
- Date:
- Sat Aug 13 17:46:30 2016 +0000
- Revision:
- 75:8cc98a3b9c62
- Parent:
- 74:3e3ee15584e5
- Parent:
- 73:da723fedfdd2
- Child:
- 77:c65eae5b9958
Re-merged in Stefan's branch with Eaddy branch.
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 | { |
JMF | 67:11db02bb93e1 | 56 | PUTS("SET APN STRING!\r\n"); |
fkellermavnet | 66:b2425419b0cc | 57 | string * pRespStr; |
fkellermavnet | 66:b2425419b0cc | 58 | string cmd_str("AT%PDNSET=1,"); |
fkellermavnet | 66:b2425419b0cc | 59 | cmd_str += MY_APN_STR; |
fkellermavnet | 66:b2425419b0cc | 60 | cmd_str += ",IP"; |
fkellermavnet | 66:b2425419b0cc | 61 | at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes |
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 | { |
fkellermavnet | 73:da723fedfdd2 | 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 | { |
JMF | 67:11db02bb93e1 | 72 | PUTS("Re-connected to cellular network!\n\r"); |
fkellermavnet | 42:be4b9ee3a615 | 73 | reportStatus = true; |
fkellermavnet | 42:be4b9ee3a615 | 74 | } |
fkellermavnet | 42:be4b9ee3a615 | 75 | |
fkellermavnet | 42:be4b9ee3a615 | 76 | // WNC has SIM and registered on network |
fkellermavnet | 42:be4b9ee3a615 | 77 | do |
fkellermavnet | 42:be4b9ee3a615 | 78 | { |
fkellermavnet | 42:be4b9ee3a615 | 79 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 42:be4b9ee3a615 | 80 | at_init_wnc(); |
fkellermavnet | 42:be4b9ee3a615 | 81 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
fkellermavnet | 42:be4b9ee3a615 | 82 | { |
fkellermavnet | 42:be4b9ee3a615 | 83 | reinitialize_mdm(); |
fkellermavnet | 42:be4b9ee3a615 | 84 | at_init_wnc(true); // Hard reset occurred so make it go through the software init(); |
fkellermavnet | 42:be4b9ee3a615 | 85 | } |
fkellermavnet | 42:be4b9ee3a615 | 86 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 42:be4b9ee3a615 | 87 | } |
fkellermavnet | 42:be4b9ee3a615 | 88 | else |
fkellermavnet | 42:be4b9ee3a615 | 89 | { |
fkellermavnet | 42:be4b9ee3a615 | 90 | if (reportStatus == true) |
fkellermavnet | 42:be4b9ee3a615 | 91 | { |
JMF | 67:11db02bb93e1 | 92 | PUTS("Not connected to cellular network!\n\r"); |
fkellermavnet | 42:be4b9ee3a615 | 93 | reportStatus = false; |
fkellermavnet | 42:be4b9ee3a615 | 94 | } |
fkellermavnet | 42:be4b9ee3a615 | 95 | // Atempt to re-register |
fkellermavnet | 42:be4b9ee3a615 | 96 | // string * pRespStr; |
JMF | 67:11db02bb93e1 | 97 | // PUTS("Force re-register!\r\n"); |
fkellermavnet | 42:be4b9ee3a615 | 98 | // at_send_wnc_cmd("AT+CFUN=0,0", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 99 | // wait_ms(31000); |
fkellermavnet | 42:be4b9ee3a615 | 100 | // at_send_wnc_cmd("AT+CFUN=1,0", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 101 | // wait_ms(31000); |
fkellermavnet | 46:da9d788f5d5a | 102 | WNC_MDM_ERR = WNC_CELL_LINK_DOWN; |
fkellermavnet | 42:be4b9ee3a615 | 103 | } |
fkellermavnet | 42:be4b9ee3a615 | 104 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 105 | } |
JMF | 2:0e2ef866af95 | 106 | |
JMF | 2:0e2ef866af95 | 107 | void resolve_mdm(void) |
JMF | 2:0e2ef866af95 | 108 | { |
JMF | 2:0e2ef866af95 | 109 | do |
JMF | 2:0e2ef866af95 | 110 | { |
JMF | 2:0e2ef866af95 | 111 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 112 | at_dnsresolve_wnc(MY_SERVER_URL, &MyServerIpAddress); |
JMF | 2:0e2ef866af95 | 113 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 114 | { |
JMF | 2:0e2ef866af95 | 115 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 116 | } |
JMF | 2:0e2ef866af95 | 117 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 118 | { |
JMF | 67:11db02bb93e1 | 119 | PUTS("Bad URL!!!!!!\r\n"); |
JMF | 2:0e2ef866af95 | 120 | } |
JMF | 2:0e2ef866af95 | 121 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 122 | |
stefanrousseau | 64:09004cd610df | 123 | PRINTF("My Server IP: %s\r\n", MyServerIpAddress.c_str()); |
JMF | 2:0e2ef866af95 | 124 | } |
JMF | 2:0e2ef866af95 | 125 | |
JMF | 2:0e2ef866af95 | 126 | void sockopen_mdm(void) |
JMF | 2:0e2ef866af95 | 127 | { |
JMF | 2:0e2ef866af95 | 128 | do |
JMF | 2:0e2ef866af95 | 129 | { |
JMF | 2:0e2ef866af95 | 130 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 131 | at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR); |
JMF | 2:0e2ef866af95 | 132 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 133 | { |
JMF | 2:0e2ef866af95 | 134 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 135 | } |
JMF | 2:0e2ef866af95 | 136 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 137 | PUTS("Socket open fail!!!!\r\n"); |
fkellermavnet | 10:df54436ecd38 | 138 | else |
fkellermavnet | 10:df54436ecd38 | 139 | socketOpen = 1; |
JMF | 2:0e2ef866af95 | 140 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 141 | } |
JMF | 2:0e2ef866af95 | 142 | |
JMF | 2:0e2ef866af95 | 143 | void sockwrite_mdm(const char * s) |
JMF | 2:0e2ef866af95 | 144 | { |
fkellermavnet | 10:df54436ecd38 | 145 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 146 | { |
JMF | 2:0e2ef866af95 | 147 | do |
JMF | 2:0e2ef866af95 | 148 | { |
JMF | 2:0e2ef866af95 | 149 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 150 | at_sockwrite_wnc(s); |
JMF | 2:0e2ef866af95 | 151 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 152 | { |
JMF | 67:11db02bb93e1 | 153 | PUTS("Sock write no response!\r\n"); |
JMF | 2:0e2ef866af95 | 154 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 155 | } |
JMF | 2:0e2ef866af95 | 156 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 2:0e2ef866af95 | 157 | { |
JMF | 67:11db02bb93e1 | 158 | PUTS("Socket Write fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 159 | software_init_mdm(); |
fkellermavnet | 74:3e3ee15584e5 | 160 | }else if (WNC_MDM_ERR == WNC_EXTERR) |
fkellermavnet | 73:da723fedfdd2 | 161 | { |
fkellermavnet | 74:3e3ee15584e5 | 162 | PUTS("Socket Disconnected (broken) !!!\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 163 | sockclose_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 164 | sockopen_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 165 | //software_init_mdm(); |
fkellermavnet | 73:da723fedfdd2 | 166 | } |
JMF | 2:0e2ef866af95 | 167 | } while (WNC_MDM_ERR != WNC_OK); |
fkellermavnet | 10:df54436ecd38 | 168 | } |
fkellermavnet | 10:df54436ecd38 | 169 | else |
JMF | 67:11db02bb93e1 | 170 | PUTS("Socket is closed for write!\r\n"); |
JMF | 2:0e2ef866af95 | 171 | } |
JMF | 2:0e2ef866af95 | 172 | |
fkellermavnet | 30:33be8e2992f3 | 173 | unsigned sockread_mdm(string * sockData, int len, int retries) |
JMF | 2:0e2ef866af95 | 174 | { |
fkellermavnet | 30:33be8e2992f3 | 175 | unsigned n = 0; |
fkellermavnet | 30:33be8e2992f3 | 176 | |
fkellermavnet | 10:df54436ecd38 | 177 | if (socketOpen == 1) |
fkellermavnet | 10:df54436ecd38 | 178 | { |
JMF | 2:0e2ef866af95 | 179 | do |
JMF | 2:0e2ef866af95 | 180 | { |
JMF | 2:0e2ef866af95 | 181 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 30:33be8e2992f3 | 182 | n = at_sockread_wnc(sockData, len, retries); |
JMF | 2:0e2ef866af95 | 183 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 184 | { |
fkellermavnet | 30:33be8e2992f3 | 185 | if (n == 0) |
fkellermavnet | 30:33be8e2992f3 | 186 | software_init_mdm(); |
fkellermavnet | 30:33be8e2992f3 | 187 | else |
JMF | 67:11db02bb93e1 | 188 | PUTS("Sock read partial data!!!\r\n"); |
JMF | 2:0e2ef866af95 | 189 | } |
JMF | 2:0e2ef866af95 | 190 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 191 | PUTS("Sock read fail!!!!\r\n"); |
fkellermavnet | 30:33be8e2992f3 | 192 | } while (WNC_MDM_ERR == WNC_NO_RESPONSE); |
fkellermavnet | 10:df54436ecd38 | 193 | } |
fkellermavnet | 10:df54436ecd38 | 194 | else |
fkellermavnet | 44:c95a85b5cf92 | 195 | { |
JMF | 67:11db02bb93e1 | 196 | PUTS("Socket is closed for read\r\n"); |
fkellermavnet | 44:c95a85b5cf92 | 197 | sockData->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 198 | } |
fkellermavnet | 30:33be8e2992f3 | 199 | |
fkellermavnet | 30:33be8e2992f3 | 200 | return (n); |
JMF | 2:0e2ef866af95 | 201 | } |
JMF | 2:0e2ef866af95 | 202 | |
JMF | 2:0e2ef866af95 | 203 | void sockclose_mdm(void) |
JMF | 2:0e2ef866af95 | 204 | { |
JMF | 2:0e2ef866af95 | 205 | do |
JMF | 2:0e2ef866af95 | 206 | { |
JMF | 2:0e2ef866af95 | 207 | WNC_MDM_ERR = WNC_OK; |
JMF | 2:0e2ef866af95 | 208 | at_sockclose_wnc(); |
fkellermavnet | 10:df54436ecd38 | 209 | // Assume close happened even if it went bad |
fkellermavnet | 10:df54436ecd38 | 210 | // going bad will result in a re-init anyways and if close |
fkellermavnet | 10:df54436ecd38 | 211 | // fails we're pretty much in bad state and not much can do |
fkellermavnet | 10:df54436ecd38 | 212 | socketOpen = 0; |
JMF | 2:0e2ef866af95 | 213 | if (WNC_MDM_ERR == WNC_NO_RESPONSE) |
JMF | 2:0e2ef866af95 | 214 | { |
JMF | 2:0e2ef866af95 | 215 | software_init_mdm(); |
JMF | 2:0e2ef866af95 | 216 | } |
JMF | 2:0e2ef866af95 | 217 | else if (WNC_MDM_ERR == WNC_CMD_ERR) |
JMF | 67:11db02bb93e1 | 218 | PUTS("Sock close fail!!!\r\n"); |
JMF | 2:0e2ef866af95 | 219 | } while (WNC_MDM_ERR != WNC_OK); |
JMF | 2:0e2ef866af95 | 220 | } |
JMF | 2:0e2ef866af95 | 221 | |
JMF | 2:0e2ef866af95 | 222 | /** |
JMF | 2:0e2ef866af95 | 223 | * C++ version 0.4 char* style "itoa": |
fkellermavnet | 74:3e3ee15584e5 | 224 | * Written by Lukas Chmela |
JMF | 2:0e2ef866af95 | 225 | * Released under GPLv3. |
JMF | 2:0e2ef866af95 | 226 | */ |
JMF | 2:0e2ef866af95 | 227 | |
JMF | 2:0e2ef866af95 | 228 | char* itoa(int value, char* result, int base) |
JMF | 2:0e2ef866af95 | 229 | { |
JMF | 2:0e2ef866af95 | 230 | // check that the base if valid |
JMF | 2:0e2ef866af95 | 231 | if ( base < 2 || base > 36 ) { |
JMF | 2:0e2ef866af95 | 232 | *result = '\0'; |
JMF | 2:0e2ef866af95 | 233 | return result; |
JMF | 2:0e2ef866af95 | 234 | } |
JMF | 2:0e2ef866af95 | 235 | |
JMF | 2:0e2ef866af95 | 236 | char* ptr = result, *ptr1 = result, tmp_char; |
JMF | 2:0e2ef866af95 | 237 | int tmp_value; |
JMF | 2:0e2ef866af95 | 238 | |
JMF | 2:0e2ef866af95 | 239 | do { |
JMF | 2:0e2ef866af95 | 240 | tmp_value = value; |
JMF | 2:0e2ef866af95 | 241 | value /= base; |
JMF | 2:0e2ef866af95 | 242 | *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]; |
JMF | 2:0e2ef866af95 | 243 | } while ( value ); |
JMF | 2:0e2ef866af95 | 244 | |
JMF | 2:0e2ef866af95 | 245 | // Apply negative sign |
JMF | 2:0e2ef866af95 | 246 | if ( tmp_value < 0 ) |
JMF | 2:0e2ef866af95 | 247 | *ptr++ = '-'; |
JMF | 2:0e2ef866af95 | 248 | *ptr-- = '\0'; |
JMF | 2:0e2ef866af95 | 249 | |
JMF | 2:0e2ef866af95 | 250 | while ( ptr1 < ptr ) { |
JMF | 2:0e2ef866af95 | 251 | tmp_char = *ptr; |
JMF | 2:0e2ef866af95 | 252 | *ptr-- = *ptr1; |
JMF | 2:0e2ef866af95 | 253 | *ptr1++ = tmp_char; |
JMF | 2:0e2ef866af95 | 254 | } |
JMF | 2:0e2ef866af95 | 255 | |
JMF | 2:0e2ef866af95 | 256 | return result; |
JMF | 2:0e2ef866af95 | 257 | } |
JMF | 2:0e2ef866af95 | 258 | |
JMF | 2:0e2ef866af95 | 259 | extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len); |
JMF | 2:0e2ef866af95 | 260 | |
fkellermavnet | 42:be4b9ee3a615 | 261 | int check_wnc_ready(void) |
fkellermavnet | 42:be4b9ee3a615 | 262 | { |
fkellermavnet | 43:6821a9c78c4b | 263 | string * pRespStr; |
fkellermavnet | 43:6821a9c78c4b | 264 | size_t pos; |
fkellermavnet | 43:6821a9c78c4b | 265 | int regSts; |
fkellermavnet | 46:da9d788f5d5a | 266 | int cmdRes1, cmdRes2; |
fkellermavnet | 42:be4b9ee3a615 | 267 | |
fkellermavnet | 53:dcccf2881fa0 | 268 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 269 | PUTS("<-------- Begin Cell Status ------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 270 | #endif |
fkellermavnet | 46:da9d788f5d5a | 271 | cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS); // Check RSSI,BER |
fkellermavnet | 46:da9d788f5d5a | 272 | cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS); // Check if SIM locked |
fkellermavnet | 46:da9d788f5d5a | 273 | |
fkellermavnet | 46:da9d788f5d5a | 274 | if ((cmdRes1 != 0) && (cmdRes2 != 0)) |
fkellermavnet | 43:6821a9c78c4b | 275 | { |
fkellermavnet | 53:dcccf2881fa0 | 276 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 277 | PUTS("------------ WNC No Response! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 278 | #endif |
fkellermavnet | 43:6821a9c78c4b | 279 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 280 | } |
fkellermavnet | 42:be4b9ee3a615 | 281 | |
fkellermavnet | 43:6821a9c78c4b | 282 | // If SIM Card not ready don't bother with commands! |
fkellermavnet | 43:6821a9c78c4b | 283 | if (pRespStr->find("CPIN: READY") == string::npos) |
fkellermavnet | 43:6821a9c78c4b | 284 | { |
fkellermavnet | 53:dcccf2881fa0 | 285 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 286 | PUTS("------------ WNC SIM Problem! --------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 287 | #endif |
fkellermavnet | 43:6821a9c78c4b | 288 | return (-1); |
fkellermavnet | 43:6821a9c78c4b | 289 | } |
fkellermavnet | 43:6821a9c78c4b | 290 | |
fkellermavnet | 43:6821a9c78c4b | 291 | // SIM card OK, now check for signal and cellular network registration |
fkellermavnet | 46:da9d788f5d5a | 292 | cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS); // Check if registered on network |
fkellermavnet | 73:da723fedfdd2 | 293 | if (pRespStr->size() > 0) |
fkellermavnet | 73:da723fedfdd2 | 294 | { |
fkellermavnet | 42:be4b9ee3a615 | 295 | pos = pRespStr->find("CREG: "); |
fkellermavnet | 42:be4b9ee3a615 | 296 | if (pos != string::npos) |
fkellermavnet | 42:be4b9ee3a615 | 297 | { |
fkellermavnet | 43:6821a9c78c4b | 298 | // The registration is the 2nd arg in the comma separated list |
fkellermavnet | 43:6821a9c78c4b | 299 | *pRespStr = pRespStr->substr(pos+8, 1); |
fkellermavnet | 43:6821a9c78c4b | 300 | regSts = atoi(pRespStr->c_str()); |
fkellermavnet | 43:6821a9c78c4b | 301 | // 1 - registered home, 5 - registered roaming |
fkellermavnet | 43:6821a9c78c4b | 302 | if ((regSts != 1) && (regSts != 5)) |
fkellermavnet | 43:6821a9c78c4b | 303 | { |
fkellermavnet | 53:dcccf2881fa0 | 304 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 305 | PUTS("------------ WNC Cell Link Down! ------>\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 306 | #endif |
fkellermavnet | 43:6821a9c78c4b | 307 | return (-2); |
fkellermavnet | 43:6821a9c78c4b | 308 | } |
fkellermavnet | 42:be4b9ee3a615 | 309 | } |
fkellermavnet | 43:6821a9c78c4b | 310 | |
fkellermavnet | 53:dcccf2881fa0 | 311 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 312 | PUTS("------------ WNC Ready ---------------->\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 313 | #endif |
fkellermavnet | 73:da723fedfdd2 | 314 | } |
fkellermavnet | 73:da723fedfdd2 | 315 | else |
fkellermavnet | 73:da723fedfdd2 | 316 | { |
fkellermavnet | 73:da723fedfdd2 | 317 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
fkellermavnet | 73:da723fedfdd2 | 318 | PUTS("------------ CREG No Reply !----------->\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 319 | #endif |
fkellermavnet | 73:da723fedfdd2 | 320 | return (-2); |
fkellermavnet | 73:da723fedfdd2 | 321 | } |
fkellermavnet | 73:da723fedfdd2 | 322 | |
fkellermavnet | 43:6821a9c78c4b | 323 | return (0); |
fkellermavnet | 42:be4b9ee3a615 | 324 | } |
fkellermavnet | 42:be4b9ee3a615 | 325 | |
JMF | 2:0e2ef866af95 | 326 | // Sets a global with failure or success, assumes 1 thread all the time |
JMF | 2:0e2ef866af95 | 327 | int send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
JMF | 2:0e2ef866af95 | 328 | { |
fkellermavnet | 46:da9d788f5d5a | 329 | int cmdRes; |
fkellermavnet | 46:da9d788f5d5a | 330 | |
fkellermavnet | 42:be4b9ee3a615 | 331 | if (check_wnc_ready() < 0) |
fkellermavnet | 42:be4b9ee3a615 | 332 | { |
fkellermavnet | 45:a836eecd5d12 | 333 | static string noRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 334 | |
fkellermavnet | 53:dcccf2881fa0 | 335 | #ifdef WNC_CMD_DEBUG_ON |
JMF | 67:11db02bb93e1 | 336 | PUTS("FAIL send cmd: "); |
fkellermavnet | 53:dcccf2881fa0 | 337 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 338 | PUTS(s); |
fkellermavnet | 73:da723fedfdd2 | 339 | |
fkellermavnet | 53:dcccf2881fa0 | 340 | #else |
fkellermavnet | 53:dcccf2881fa0 | 341 | string truncStr(s, 50); |
fkellermavnet | 53:dcccf2881fa0 | 342 | truncStr += "\r\n"; |
JMF | 67:11db02bb93e1 | 343 | PUTS(truncStr.c_str()); |
fkellermavnet | 53:dcccf2881fa0 | 344 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 345 | #else |
JMF | 67:11db02bb93e1 | 346 | PUTS("FAIL send cmd!\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 347 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 348 | |
fkellermavnet | 46:da9d788f5d5a | 349 | WNC_MDM_ERR = WNC_CELL_LINK_DOWN; |
fkellermavnet | 45:a836eecd5d12 | 350 | noRespStr.erase(); |
fkellermavnet | 44:c95a85b5cf92 | 351 | *r = &noRespStr; |
fkellermavnet | 46:da9d788f5d5a | 352 | return (-3); |
fkellermavnet | 42:be4b9ee3a615 | 353 | } |
fkellermavnet | 53:dcccf2881fa0 | 354 | |
fkellermavnet | 53:dcccf2881fa0 | 355 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 356 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 357 | PUTS("[---------- Network Status -------------\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 358 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 359 | string * pRespStr; |
fkellermavnet | 53:dcccf2881fa0 | 360 | at_send_wnc_cmd("AT@SOCKDIAL?", &pRespStr, 5000); |
fkellermavnet | 53:dcccf2881fa0 | 361 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 362 | PUTS("---------------------------------------]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 363 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 364 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 365 | |
fkellermavnet | 42:be4b9ee3a615 | 366 | // If WNC ready, send user command |
fkellermavnet | 46:da9d788f5d5a | 367 | cmdRes = at_send_wnc_cmd(s, r, ms_timeout); |
fkellermavnet | 46:da9d788f5d5a | 368 | |
fkellermavnet | 46:da9d788f5d5a | 369 | if (cmdRes == -1) |
fkellermavnet | 46:da9d788f5d5a | 370 | WNC_MDM_ERR = WNC_CMD_ERR; |
fkellermavnet | 46:da9d788f5d5a | 371 | |
fkellermavnet | 46:da9d788f5d5a | 372 | if (cmdRes == -2) |
fkellermavnet | 46:da9d788f5d5a | 373 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 74:3e3ee15584e5 | 374 | |
fkellermavnet | 74:3e3ee15584e5 | 375 | if (cmdRes == -3) { |
fkellermavnet | 73:da723fedfdd2 | 376 | WNC_MDM_ERR = WNC_EXTERR; |
fkellermavnet | 74:3e3ee15584e5 | 377 | pc.printf("[[WNC_MDM_ERR = WNC_EXTERR]] \r\n"); |
fkellermavnet | 74:3e3ee15584e5 | 378 | } |
fkellermavnet | 73:da723fedfdd2 | 379 | |
fkellermavnet | 73:da723fedfdd2 | 380 | if (cmdRes == 0) |
fkellermavnet | 46:da9d788f5d5a | 381 | WNC_MDM_ERR = WNC_OK; |
fkellermavnet | 46:da9d788f5d5a | 382 | |
fkellermavnet | 46:da9d788f5d5a | 383 | return (cmdRes); |
fkellermavnet | 42:be4b9ee3a615 | 384 | } |
fkellermavnet | 42:be4b9ee3a615 | 385 | |
fkellermavnet | 42:be4b9ee3a615 | 386 | int at_send_wnc_cmd(const char * s, string ** r, int ms_timeout) |
fkellermavnet | 42:be4b9ee3a615 | 387 | { |
fkellermavnet | 74:3e3ee15584e5 | 388 | //Eaddy |
fkellermavnet | 75:8cc98a3b9c62 | 389 | static const char * rsp_lst[] = { "OK", "ERROR","@EXTERR", "+CME", NULL }; |
JMF | 2:0e2ef866af95 | 390 | int len; |
fkellermavnet | 53:dcccf2881fa0 | 391 | |
fkellermavnet | 53:dcccf2881fa0 | 392 | #ifdef WNC_CMD_DEBUG_ON |
fkellermavnet | 53:dcccf2881fa0 | 393 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
fkellermavnet | 53:dcccf2881fa0 | 394 | |
fkellermavnet | 53:dcccf2881fa0 | 395 | #else |
fkellermavnet | 42:be4b9ee3a615 | 396 | if (strlen(s) > 60) |
fkellermavnet | 42:be4b9ee3a615 | 397 | { |
fkellermavnet | 42:be4b9ee3a615 | 398 | string truncStr(s,57); |
fkellermavnet | 42:be4b9ee3a615 | 399 | truncStr += "..."; |
stefanrousseau | 64:09004cd610df | 400 | PRINTF("Send: <<%s>>\r\n",truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 401 | } |
fkellermavnet | 42:be4b9ee3a615 | 402 | else |
fkellermavnet | 53:dcccf2881fa0 | 403 | #endif |
stefanrousseau | 64:09004cd610df | 404 | PRINTF("Send: <<%s>>\r\n",s); |
fkellermavnet | 53:dcccf2881fa0 | 405 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 406 | |
JMF | 2:0e2ef866af95 | 407 | int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len); |
JMF | 2:0e2ef866af95 | 408 | *r = &wncStr; // Return a pointer to the static string |
JMF | 2:0e2ef866af95 | 409 | |
JMF | 2:0e2ef866af95 | 410 | if (res >= 0) |
fkellermavnet | 53:dcccf2881fa0 | 411 | { |
fkellermavnet | 53:dcccf2881fa0 | 412 | |
fkellermavnet | 53:dcccf2881fa0 | 413 | #ifdef WNC_CMD_DEBUG_ON |
JMF | 67:11db02bb93e1 | 414 | PUTS("["); |
fkellermavnet | 53:dcccf2881fa0 | 415 | #ifdef WNC_CMD_DEBUG_ON_VERBOSE |
JMF | 67:11db02bb93e1 | 416 | PUTS(wncStr.c_str()); |
JMF | 67:11db02bb93e1 | 417 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 418 | #else |
fkellermavnet | 42:be4b9ee3a615 | 419 | if (wncStr.size() < 51) |
JMF | 67:11db02bb93e1 | 420 | PUTS(wncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 421 | else |
fkellermavnet | 42:be4b9ee3a615 | 422 | { |
fkellermavnet | 42:be4b9ee3a615 | 423 | string truncStr = wncStr.substr(0,50) + "..."; |
JMF | 67:11db02bb93e1 | 424 | PUTS(truncStr.c_str()); |
fkellermavnet | 42:be4b9ee3a615 | 425 | } |
JMF | 67:11db02bb93e1 | 426 | PUTS("]\r\n"); |
fkellermavnet | 53:dcccf2881fa0 | 427 | #endif |
fkellermavnet | 53:dcccf2881fa0 | 428 | #endif |
fkellermavnet | 42:be4b9ee3a615 | 429 | |
fkellermavnet | 74:3e3ee15584e5 | 430 | #if 0 |
JMF | 2:0e2ef866af95 | 431 | if (res > 0) |
JMF | 2:0e2ef866af95 | 432 | return -1; |
JMF | 2:0e2ef866af95 | 433 | else |
JMF | 2:0e2ef866af95 | 434 | return 0; |
fkellermavnet | 74:3e3ee15584e5 | 435 | #else |
fkellermavnet | 74:3e3ee15584e5 | 436 | //Eaddy added |
fkellermavnet | 74:3e3ee15584e5 | 437 | if (res == 0) { |
fkellermavnet | 73:da723fedfdd2 | 438 | /* OK */ |
fkellermavnet | 73:da723fedfdd2 | 439 | return 0; |
fkellermavnet | 73:da723fedfdd2 | 440 | } else if (res == 2) { |
fkellermavnet | 73:da723fedfdd2 | 441 | /* @EXTERR */ |
fkellermavnet | 75:8cc98a3b9c62 | 442 | PRINTF("@EXTERR and res = %d \r\n", res); |
fkellermavnet | 73:da723fedfdd2 | 443 | return -3; |
fkellermavnet | 73:da723fedfdd2 | 444 | } else |
JMF | 2:0e2ef866af95 | 445 | return -1; |
fkellermavnet | 74:3e3ee15584e5 | 446 | #endif |
JMF | 2:0e2ef866af95 | 447 | } |
JMF | 2:0e2ef866af95 | 448 | else |
JMF | 2:0e2ef866af95 | 449 | { |
JMF | 67:11db02bb93e1 | 450 | PUTS("No response from WNC!\n\r"); |
JMF | 2:0e2ef866af95 | 451 | return -2; |
JMF | 2:0e2ef866af95 | 452 | } |
JMF | 2:0e2ef866af95 | 453 | } |
JMF | 2:0e2ef866af95 | 454 | |
fkellermavnet | 42:be4b9ee3a615 | 455 | |
fkellermavnet | 19:f89baed3bd6f | 456 | void at_at_wnc(void) |
fkellermavnet | 19:f89baed3bd6f | 457 | { |
fkellermavnet | 19:f89baed3bd6f | 458 | string * pRespStr; |
fkellermavnet | 19:f89baed3bd6f | 459 | send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 19:f89baed3bd6f | 460 | } |
fkellermavnet | 19:f89baed3bd6f | 461 | |
fkellermavnet | 42:be4b9ee3a615 | 462 | void at_init_wnc(bool hardReset) |
JMF | 2:0e2ef866af95 | 463 | { |
fkellermavnet | 36:d4782eabff43 | 464 | static bool pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 465 | static bool intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 466 | static bool sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 467 | string * pRespStr; |
fkellermavnet | 46:da9d788f5d5a | 468 | int cmdRes; |
fkellermavnet | 36:d4782eabff43 | 469 | |
fkellermavnet | 42:be4b9ee3a615 | 470 | if (hardReset == true) |
fkellermavnet | 42:be4b9ee3a615 | 471 | { |
JMF | 67:11db02bb93e1 | 472 | PUTS("Hard Reset!\r\n"); |
fkellermavnet | 42:be4b9ee3a615 | 473 | pdnSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 474 | intSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 475 | sockDialSet = false; |
fkellermavnet | 42:be4b9ee3a615 | 476 | } |
fkellermavnet | 42:be4b9ee3a615 | 477 | |
JMF | 67:11db02bb93e1 | 478 | PUTS("Start AT init of WNC:\r\n"); |
fkellermavnet | 43:6821a9c78c4b | 479 | // Quick commands below do not need to check cellular connectivity |
fkellermavnet | 46:da9d788f5d5a | 480 | cmdRes = at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat? |
fkellermavnet | 46:da9d788f5d5a | 481 | cmdRes += at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS); // Echo Off |
fkellermavnet | 46:da9d788f5d5a | 482 | cmdRes += at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS); // 2 - verbose error, 1 - numeric error, 0 - just ERROR |
fkellermavnet | 43:6821a9c78c4b | 483 | |
fkellermavnet | 43:6821a9c78c4b | 484 | // If the simple commands are not working no chance of more complex. |
fkellermavnet | 43:6821a9c78c4b | 485 | // I have seen re-trying commands make it worse. |
fkellermavnet | 46:da9d788f5d5a | 486 | if (cmdRes < 0) |
fkellermavnet | 46:da9d788f5d5a | 487 | { |
fkellermavnet | 46:da9d788f5d5a | 488 | // Since I used the at_send_wnc_cmd I am setting the error state based upon |
fkellermavnet | 46:da9d788f5d5a | 489 | // the responses. And since these are simple commands, even if the WNC |
fkellermavnet | 46:da9d788f5d5a | 490 | // is saying ERROR, treat it like a no response. |
fkellermavnet | 46:da9d788f5d5a | 491 | WNC_MDM_ERR = WNC_NO_RESPONSE; |
fkellermavnet | 43:6821a9c78c4b | 492 | return ; |
fkellermavnet | 46:da9d788f5d5a | 493 | } |
fkellermavnet | 42:be4b9ee3a615 | 494 | |
fkellermavnet | 46:da9d788f5d5a | 495 | if (intSet == false) |
fkellermavnet | 46:da9d788f5d5a | 496 | cmdRes = send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 497 | |
fkellermavnet | 46:da9d788f5d5a | 498 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 499 | intSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 500 | else |
fkellermavnet | 42:be4b9ee3a615 | 501 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 502 | |
fkellermavnet | 36:d4782eabff43 | 503 | if (pdnSet == false) |
fkellermavnet | 36:d4782eabff43 | 504 | { |
fkellermavnet | 36:d4782eabff43 | 505 | string cmd_str("AT%PDNSET=1,"); |
fkellermavnet | 36:d4782eabff43 | 506 | cmd_str += MY_APN_STR; |
fkellermavnet | 36:d4782eabff43 | 507 | cmd_str += ",IP"; |
fkellermavnet | 46:da9d788f5d5a | 508 | 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 | 509 | } |
fkellermavnet | 42:be4b9ee3a615 | 510 | |
fkellermavnet | 46:da9d788f5d5a | 511 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 512 | pdnSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 513 | else |
fkellermavnet | 42:be4b9ee3a615 | 514 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 515 | |
fkellermavnet | 42:be4b9ee3a615 | 516 | if (sockDialSet == false) |
fkellermavnet | 46:da9d788f5d5a | 517 | cmdRes = send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS); |
fkellermavnet | 42:be4b9ee3a615 | 518 | |
fkellermavnet | 46:da9d788f5d5a | 519 | if (cmdRes == 0) |
fkellermavnet | 42:be4b9ee3a615 | 520 | sockDialSet = true; |
fkellermavnet | 42:be4b9ee3a615 | 521 | else |
fkellermavnet | 42:be4b9ee3a615 | 522 | return ; |
fkellermavnet | 42:be4b9ee3a615 | 523 | |
JMF | 67:11db02bb93e1 | 524 | PUTS("SUCCESS: AT init of WNC!\r\n"); |
JMF | 2:0e2ef866af95 | 525 | } |
JMF | 2:0e2ef866af95 | 526 | |
JMF | 2:0e2ef866af95 | 527 | void at_sockopen_wnc(const string & ipStr, const char * port ) |
JMF | 2:0e2ef866af95 | 528 | { |
JMF | 2:0e2ef866af95 | 529 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 530 | send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 531 | string cmd_str("AT@SOCKCONN=1,\""); |
JMF | 2:0e2ef866af95 | 532 | cmd_str += ipStr; |
JMF | 2:0e2ef866af95 | 533 | cmd_str += "\","; |
JMF | 2:0e2ef866af95 | 534 | cmd_str += port; |
fkellermavnet | 38:564b312a719f | 535 | cmd_str += ",30"; |
fkellermavnet | 73:da723fedfdd2 | 536 | int cmd = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000); |
fkellermavnet | 73:da723fedfdd2 | 537 | if (cmd != WNC_OK) { |
fkellermavnet | 73:da723fedfdd2 | 538 | // Per WNC: re-close even if open fails! |
fkellermavnet | 73:da723fedfdd2 | 539 | at_sockclose_wnc(); |
fkellermavnet | 73:da723fedfdd2 | 540 | } |
JMF | 2:0e2ef866af95 | 541 | } |
JMF | 2:0e2ef866af95 | 542 | |
JMF | 2:0e2ef866af95 | 543 | void at_sockclose_wnc(void) |
JMF | 2:0e2ef866af95 | 544 | { |
JMF | 2:0e2ef866af95 | 545 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 546 | send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS); |
JMF | 2:0e2ef866af95 | 547 | } |
JMF | 2:0e2ef866af95 | 548 | |
JMF | 2:0e2ef866af95 | 549 | int at_dnsresolve_wnc(const char * s, string * ipStr) |
JMF | 2:0e2ef866af95 | 550 | { |
JMF | 2:0e2ef866af95 | 551 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 552 | string str(s); |
fkellermavnet | 43:6821a9c78c4b | 553 | str = "AT@DNSRESVDON=\"" + str + "\""; |
fkellermavnet | 46:da9d788f5d5a | 554 | if (send_wnc_cmd(str.c_str(), &pRespStr, 15000) == 0) |
JMF | 2:0e2ef866af95 | 555 | { |
JMF | 2:0e2ef866af95 | 556 | size_t pos_start = pRespStr->find(":\"") + 2; |
fkellermavnet | 43:6821a9c78c4b | 557 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 43:6821a9c78c4b | 558 | if ((pos_start != string::npos) && (pos_end != string::npos)) |
JMF | 2:0e2ef866af95 | 559 | { |
JMF | 2:0e2ef866af95 | 560 | if (pos_end > pos_start) |
JMF | 2:0e2ef866af95 | 561 | { |
JMF | 2:0e2ef866af95 | 562 | // Make a copy for use later (the source string is re-used) |
JMF | 2:0e2ef866af95 | 563 | *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1); |
JMF | 2:0e2ef866af95 | 564 | return 1; |
JMF | 2:0e2ef866af95 | 565 | } |
JMF | 2:0e2ef866af95 | 566 | else |
JMF | 67:11db02bb93e1 | 567 | PUTS("URL Resolve fail, substr Err\r\n"); |
JMF | 2:0e2ef866af95 | 568 | } |
JMF | 2:0e2ef866af95 | 569 | else |
JMF | 67:11db02bb93e1 | 570 | PUTS("URL Resolve fail, no quotes\r\n"); |
JMF | 2:0e2ef866af95 | 571 | } |
JMF | 2:0e2ef866af95 | 572 | else |
JMF | 67:11db02bb93e1 | 573 | PUTS("URL Resolve fail, WNC cmd fail\r\n"); |
JMF | 2:0e2ef866af95 | 574 | |
fkellermavnet | 43:6821a9c78c4b | 575 | *ipStr = "192.168.0.1"; |
fkellermavnet | 43:6821a9c78c4b | 576 | |
JMF | 2:0e2ef866af95 | 577 | return -1; |
JMF | 2:0e2ef866af95 | 578 | } |
JMF | 2:0e2ef866af95 | 579 | |
JMF | 2:0e2ef866af95 | 580 | void at_sockwrite_wnc(const char * s) |
JMF | 2:0e2ef866af95 | 581 | { |
JMF | 2:0e2ef866af95 | 582 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 583 | char num2str[6]; |
JMF | 2:0e2ef866af95 | 584 | size_t sLen = strlen(s); |
fkellermavnet | 73:da723fedfdd2 | 585 | int res; |
fkellermavnet | 39:1996eaec02d6 | 586 | if (sLen <= 1500) |
JMF | 2:0e2ef866af95 | 587 | { |
JMF | 2:0e2ef866af95 | 588 | string cmd_str("AT@SOCKWRITE=1,"); |
JMF | 2:0e2ef866af95 | 589 | itoa(sLen, num2str, 10); |
JMF | 2:0e2ef866af95 | 590 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 591 | cmd_str += ",\""; |
JMF | 2:0e2ef866af95 | 592 | while(*s != '\0') |
JMF | 2:0e2ef866af95 | 593 | { |
JMF | 2:0e2ef866af95 | 594 | itoa((int)*s++, num2str, 16); |
JMF | 2:0e2ef866af95 | 595 | // Always 2-digit ascii hex: |
JMF | 2:0e2ef866af95 | 596 | if (strlen(num2str) == 1) |
JMF | 2:0e2ef866af95 | 597 | { |
JMF | 2:0e2ef866af95 | 598 | num2str[2] = '\0'; |
JMF | 2:0e2ef866af95 | 599 | num2str[1] = num2str[0]; |
JMF | 2:0e2ef866af95 | 600 | num2str[0] = '0'; |
JMF | 2:0e2ef866af95 | 601 | } |
JMF | 2:0e2ef866af95 | 602 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 603 | } |
JMF | 2:0e2ef866af95 | 604 | cmd_str += "\""; |
fkellermavnet | 73:da723fedfdd2 | 605 | res = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 120000); |
fkellermavnet | 73:da723fedfdd2 | 606 | if (res == -3) |
fkellermavnet | 73:da723fedfdd2 | 607 | PUTS("sockwrite is disconnect \r\n"); |
JMF | 2:0e2ef866af95 | 608 | } |
JMF | 2:0e2ef866af95 | 609 | else |
JMF | 67:11db02bb93e1 | 610 | PUTS("sockwrite Err, string to long\r\n"); |
JMF | 2:0e2ef866af95 | 611 | } |
JMF | 2:0e2ef866af95 | 612 | |
JMF | 2:0e2ef866af95 | 613 | unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0) |
JMF | 2:0e2ef866af95 | 614 | { |
fkellermavnet | 34:1a4498e3580e | 615 | unsigned i, numBytes = 0; |
JMF | 2:0e2ef866af95 | 616 | string * pRespStr; |
JMF | 2:0e2ef866af95 | 617 | string cmd_str("AT@SOCKREAD=1,"); |
fkellermavnet | 30:33be8e2992f3 | 618 | |
fkellermavnet | 44:c95a85b5cf92 | 619 | // Clean slate |
fkellermavnet | 44:c95a85b5cf92 | 620 | pS->erase(); |
fkellermavnet | 44:c95a85b5cf92 | 621 | |
JMF | 2:0e2ef866af95 | 622 | if (n <= 1500) |
JMF | 2:0e2ef866af95 | 623 | { |
JMF | 2:0e2ef866af95 | 624 | char num2str[6]; |
fkellermavnet | 22:41e6c417ace1 | 625 | |
JMF | 2:0e2ef866af95 | 626 | itoa(n, num2str, 10); |
JMF | 2:0e2ef866af95 | 627 | cmd_str += num2str; |
JMF | 2:0e2ef866af95 | 628 | retries += 1; |
JMF | 2:0e2ef866af95 | 629 | while (retries--) |
JMF | 2:0e2ef866af95 | 630 | { |
fkellermavnet | 22:41e6c417ace1 | 631 | // Assuming someone is sending then calling this to receive response, invoke |
fkellermavnet | 22:41e6c417ace1 | 632 | // a pause to give the response some time to come back and then also |
fkellermavnet | 22:41e6c417ace1 | 633 | // between each retry. |
fkellermavnet | 22:41e6c417ace1 | 634 | wait_ms(10); |
fkellermavnet | 22:41e6c417ace1 | 635 | |
fkellermavnet | 73:da723fedfdd2 | 636 | if (send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0) |
fkellermavnet | 73:da723fedfdd2 | 637 | { |
fkellermavnet | 73:da723fedfdd2 | 638 | size_t pos_start = pRespStr->find("\"") + 1; |
fkellermavnet | 73:da723fedfdd2 | 639 | size_t pos_end = pRespStr->rfind("\"") - 1; |
fkellermavnet | 43:6821a9c78c4b | 640 | |
fkellermavnet | 73:da723fedfdd2 | 641 | // Make sure search finds what it's looking for! |
fkellermavnet | 73:da723fedfdd2 | 642 | if (pos_start != string::npos && pos_end != string::npos) |
fkellermavnet | 43:6821a9c78c4b | 643 | i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte |
fkellermavnet | 73:da723fedfdd2 | 644 | else |
fkellermavnet | 43:6821a9c78c4b | 645 | i = 0; |
fkellermavnet | 43:6821a9c78c4b | 646 | |
fkellermavnet | 73:da723fedfdd2 | 647 | if (i > 0) |
fkellermavnet | 73:da723fedfdd2 | 648 | { |
fkellermavnet | 73:da723fedfdd2 | 649 | retries = 1; // If any data found retry 1 more time to catch data that might be in another |
fkellermavnet | 73:da723fedfdd2 | 650 | // WNC payload |
fkellermavnet | 73:da723fedfdd2 | 651 | string byte; |
fkellermavnet | 73:da723fedfdd2 | 652 | while (pos_start < pos_end) |
fkellermavnet | 73:da723fedfdd2 | 653 | { |
fkellermavnet | 73:da723fedfdd2 | 654 | byte = pRespStr->substr(pos_start, 2); |
fkellermavnet | 73:da723fedfdd2 | 655 | *pS += (char)strtol(byte.c_str(), NULL, 16); |
fkellermavnet | 73:da723fedfdd2 | 656 | pos_start += 2; |
fkellermavnet | 73:da723fedfdd2 | 657 | } |
fkellermavnet | 73:da723fedfdd2 | 658 | numBytes += i/2; |
fkellermavnet | 73:da723fedfdd2 | 659 | } |
fkellermavnet | 73:da723fedfdd2 | 660 | } |
fkellermavnet | 73:da723fedfdd2 | 661 | else |
JMF | 2:0e2ef866af95 | 662 | { |
fkellermavnet | 73:da723fedfdd2 | 663 | PUTS("no readsock reply!\r\n"); |
fkellermavnet | 73:da723fedfdd2 | 664 | return (0); |
JMF | 2:0e2ef866af95 | 665 | } |
JMF | 2:0e2ef866af95 | 666 | } |
JMF | 2:0e2ef866af95 | 667 | } |
JMF | 2:0e2ef866af95 | 668 | else |
JMF | 67:11db02bb93e1 | 669 | PUTS("sockread Err, to many to read\r\n"); |
JMF | 2:0e2ef866af95 | 670 | |
fkellermavnet | 43:6821a9c78c4b | 671 | return (numBytes); |
JMF | 2:0e2ef866af95 | 672 | } |