Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z revert by Axeda Corp

Committer:
AxedaCorp
Date:
Tue Jul 01 21:31:54 2014 +0000
Revision:
0:65004368569c
Made initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 /* Universal Socket Modem Interface Library
AxedaCorp 0:65004368569c 2 * Copyright (c) 2013 Multi-Tech Systems
AxedaCorp 0:65004368569c 3 *
AxedaCorp 0:65004368569c 4 * Licensed under the Apache License, Version 2.0 (the "License");
AxedaCorp 0:65004368569c 5 * you may not use this file except in compliance with the License.
AxedaCorp 0:65004368569c 6 * You may obtain a copy of the License at
AxedaCorp 0:65004368569c 7 *
AxedaCorp 0:65004368569c 8 * http://www.apache.org/licenses/LICENSE-2.0
AxedaCorp 0:65004368569c 9 *
AxedaCorp 0:65004368569c 10 * Unless required by applicable law or agreed to in writing, software
AxedaCorp 0:65004368569c 11 * distributed under the License is distributed on an "AS IS" BASIS,
AxedaCorp 0:65004368569c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AxedaCorp 0:65004368569c 13 * See the License for the specific language governing permissions and
AxedaCorp 0:65004368569c 14 * limitations under the License.
AxedaCorp 0:65004368569c 15 */
AxedaCorp 0:65004368569c 16
AxedaCorp 0:65004368569c 17 #include "Wifi.h"
AxedaCorp 0:65004368569c 18 #include "MTSText.h"
AxedaCorp 0:65004368569c 19
AxedaCorp 0:65004368569c 20 #if 0
AxedaCorp 0:65004368569c 21 //Enable debug
AxedaCorp 0:65004368569c 22 #include <cstdio>
AxedaCorp 0:65004368569c 23 #define DBG(x, ...) std::printf("Line: %d %s \t[Wifi : DBG]"x"\r\n", __LINE__, __FILE__, ##__VA_ARGS__);
AxedaCorp 0:65004368569c 24 #else
AxedaCorp 0:65004368569c 25 #define DBG(x, ...)
AxedaCorp 0:65004368569c 26 #endif
AxedaCorp 0:65004368569c 27
AxedaCorp 0:65004368569c 28 Wifi* Wifi::instance = NULL;
AxedaCorp 0:65004368569c 29
AxedaCorp 0:65004368569c 30 Wifi* Wifi::getInstance()
AxedaCorp 0:65004368569c 31 {
AxedaCorp 0:65004368569c 32 if(instance == NULL) {
AxedaCorp 0:65004368569c 33 instance = new Wifi(NULL);
AxedaCorp 0:65004368569c 34 }
AxedaCorp 0:65004368569c 35 return instance;
AxedaCorp 0:65004368569c 36 }
AxedaCorp 0:65004368569c 37
AxedaCorp 0:65004368569c 38 bool Wifi::sortInterfaceMode(void)
AxedaCorp 0:65004368569c 39 {
AxedaCorp 0:65004368569c 40 //Check initial state of command mode
AxedaCorp 0:65004368569c 41 std::string response = sendCommand("", 1000, ">");
AxedaCorp 0:65004368569c 42 if(response.find(">") != string::npos) {
AxedaCorp 0:65004368569c 43 cmdOn = true;
AxedaCorp 0:65004368569c 44 }
AxedaCorp 0:65004368569c 45
AxedaCorp 0:65004368569c 46 //Set device into command mode
AxedaCorp 0:65004368569c 47 if (!setCmdMode(true)) {
AxedaCorp 0:65004368569c 48 return false;
AxedaCorp 0:65004368569c 49 }
AxedaCorp 0:65004368569c 50
AxedaCorp 0:65004368569c 51 return true;
AxedaCorp 0:65004368569c 52 }
AxedaCorp 0:65004368569c 53
AxedaCorp 0:65004368569c 54 bool Wifi::init(MTSBufferedIO* io)
AxedaCorp 0:65004368569c 55 {
AxedaCorp 0:65004368569c 56 if (io == NULL) {
AxedaCorp 0:65004368569c 57 return false;
AxedaCorp 0:65004368569c 58 }
AxedaCorp 0:65004368569c 59 instance->io = io;
AxedaCorp 0:65004368569c 60
AxedaCorp 0:65004368569c 61 // start from the same place each time
AxedaCorp 0:65004368569c 62 reset();
AxedaCorp 0:65004368569c 63
AxedaCorp 0:65004368569c 64 //Secure interface mode
AxedaCorp 0:65004368569c 65 if(!sortInterfaceMode()) {
AxedaCorp 0:65004368569c 66 return false;
AxedaCorp 0:65004368569c 67 }
AxedaCorp 0:65004368569c 68
AxedaCorp 0:65004368569c 69 //Set device to non-echo mode
AxedaCorp 0:65004368569c 70 while (sendBasicCommand("set uart mode 1", 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 71 printf("[ERROR] Failed to set to non-echo mode\n\r");
AxedaCorp 0:65004368569c 72 //return false;
AxedaCorp 0:65004368569c 73 }
AxedaCorp 0:65004368569c 74 // do this twice because the module response doesnt seem to always take
AxedaCorp 0:65004368569c 75 while (sendBasicCommand("set uart mode 1", 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 76 printf("[ERROR] Failed to set to non-echo mode\n\r");
AxedaCorp 0:65004368569c 77 //return false;
AxedaCorp 0:65004368569c 78 }
AxedaCorp 0:65004368569c 79
AxedaCorp 0:65004368569c 80 //Set device to manual infrastructure mode
AxedaCorp 0:65004368569c 81 if (sendBasicCommand("set wlan join 0", 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 82 printf("[ERROR] Failed to set join mode\n\r");
AxedaCorp 0:65004368569c 83 return false;
AxedaCorp 0:65004368569c 84 }
AxedaCorp 0:65004368569c 85
AxedaCorp 0:65004368569c 86 //Set device to channel auto-scanning mode
AxedaCorp 0:65004368569c 87 if (sendBasicCommand("set wlan channel 0", 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 88 printf("[ERROR] Failed to set auto-scanning mode\n\r");
AxedaCorp 0:65004368569c 89 return false;
AxedaCorp 0:65004368569c 90 }
AxedaCorp 0:65004368569c 91
AxedaCorp 0:65004368569c 92 //Set device so no data is transmitted immediately following a socket connection
AxedaCorp 0:65004368569c 93 if (sendBasicCommand("set comm remote 0", 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 94 printf("[ERROR] Failed to set remote transmit mode\n\r");
AxedaCorp 0:65004368569c 95 return false;
AxedaCorp 0:65004368569c 96 }
AxedaCorp 0:65004368569c 97
AxedaCorp 0:65004368569c 98 //Set device into DHCP mode by default
AxedaCorp 0:65004368569c 99 if (sendBasicCommand("set ip dhcp 1", 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 100 printf("[ERROR] Failed to set to default DHCP mode\n\r");
AxedaCorp 0:65004368569c 101 return false;
AxedaCorp 0:65004368569c 102 }
AxedaCorp 0:65004368569c 103
AxedaCorp 0:65004368569c 104 return true;
AxedaCorp 0:65004368569c 105 }
AxedaCorp 0:65004368569c 106
AxedaCorp 0:65004368569c 107 Wifi::Wifi(MTSBufferedIO* io)
AxedaCorp 0:65004368569c 108 : io(io)
AxedaCorp 0:65004368569c 109 , wifiConnected(false)
AxedaCorp 0:65004368569c 110 , _ssid("")
AxedaCorp 0:65004368569c 111 , mode(TCP)
AxedaCorp 0:65004368569c 112 , socketOpened(false)
AxedaCorp 0:65004368569c 113 , socketCloseable(true)
AxedaCorp 0:65004368569c 114 , local_port(0)
AxedaCorp 0:65004368569c 115 , local_address("")
AxedaCorp 0:65004368569c 116 , host_port(0)
AxedaCorp 0:65004368569c 117 , cmdOn(false)
AxedaCorp 0:65004368569c 118 {
AxedaCorp 0:65004368569c 119
AxedaCorp 0:65004368569c 120 }
AxedaCorp 0:65004368569c 121
AxedaCorp 0:65004368569c 122 Wifi::~Wifi()
AxedaCorp 0:65004368569c 123 {
AxedaCorp 0:65004368569c 124 }
AxedaCorp 0:65004368569c 125
AxedaCorp 0:65004368569c 126 bool Wifi::connect()
AxedaCorp 0:65004368569c 127 {
AxedaCorp 0:65004368569c 128 //Check if socket is open
AxedaCorp 0:65004368569c 129 if(socketOpened) {
AxedaCorp 0:65004368569c 130 return true;
AxedaCorp 0:65004368569c 131 }
AxedaCorp 0:65004368569c 132
AxedaCorp 0:65004368569c 133 //Run Test first to validate a good state
AxedaCorp 0:65004368569c 134 if(isConnected()) {
AxedaCorp 0:65004368569c 135 return true;
AxedaCorp 0:65004368569c 136 }
AxedaCorp 0:65004368569c 137
AxedaCorp 0:65004368569c 138 if (_ssid.size() == 0) {
AxedaCorp 0:65004368569c 139 printf("[ERROR] No SSID has been set\n\r");
AxedaCorp 0:65004368569c 140 return false;
AxedaCorp 0:65004368569c 141 }
AxedaCorp 0:65004368569c 142
AxedaCorp 0:65004368569c 143 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 144 return false;
AxedaCorp 0:65004368569c 145 }
AxedaCorp 0:65004368569c 146
AxedaCorp 0:65004368569c 147 //Possibly add a scan command here and look for the network....
AxedaCorp 0:65004368569c 148
AxedaCorp 0:65004368569c 149 //join my_network
AxedaCorp 0:65004368569c 150 printf("[DEBUG] Making SSID Connection Attempt. SSID[%s]\r\n", _ssid.c_str());
AxedaCorp 0:65004368569c 151 std::string result = sendCommand("join " + _ssid, 15000, "GW=");
AxedaCorp 0:65004368569c 152 //printf("Connect Status: %s\n\r", result.c_str());
AxedaCorp 0:65004368569c 153
AxedaCorp 0:65004368569c 154 //Check whether connection was successful
AxedaCorp 0:65004368569c 155 if(result.find("Associated!") != string::npos) {
AxedaCorp 0:65004368569c 156 if(result.find("Static") == string::npos) {
AxedaCorp 0:65004368569c 157 int start = result.find("IP=");
AxedaCorp 0:65004368569c 158 int stop = result.find(":", start);
AxedaCorp 0:65004368569c 159 local_address = result.substr(start + 3, stop - start - 3);
AxedaCorp 0:65004368569c 160 }
AxedaCorp 0:65004368569c 161 printf("[INFO] WiFi Connection Established: IP[%s]\r\n", local_address.c_str());
AxedaCorp 0:65004368569c 162 wifiConnected = true;
AxedaCorp 0:65004368569c 163
AxedaCorp 0:65004368569c 164 //Report Signal Strength of new connection
AxedaCorp 0:65004368569c 165 wait(1); //Needed for signal strength to be available
AxedaCorp 0:65004368569c 166 int rssi = getSignalStrength();
AxedaCorp 0:65004368569c 167 printf("[DEBUG] Signal strength (dBm): %d\r\n", rssi);
AxedaCorp 0:65004368569c 168 } else {
AxedaCorp 0:65004368569c 169 wifiConnected = false;
AxedaCorp 0:65004368569c 170 }
AxedaCorp 0:65004368569c 171
AxedaCorp 0:65004368569c 172 return wifiConnected;
AxedaCorp 0:65004368569c 173 }
AxedaCorp 0:65004368569c 174
AxedaCorp 0:65004368569c 175 void Wifi::disconnect()
AxedaCorp 0:65004368569c 176 {
AxedaCorp 0:65004368569c 177 wait(5.0f);
AxedaCorp 0:65004368569c 178 printf("[DEBUG] Disconnecting from network\r\n");
AxedaCorp 0:65004368569c 179
AxedaCorp 0:65004368569c 180 if(socketOpened) {
AxedaCorp 0:65004368569c 181 close();
AxedaCorp 0:65004368569c 182 }
AxedaCorp 0:65004368569c 183
AxedaCorp 0:65004368569c 184 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 185 printf("[ERROR] Failed in disconnecting from network. Continuing ...\r\n");
AxedaCorp 0:65004368569c 186 }
AxedaCorp 0:65004368569c 187
AxedaCorp 0:65004368569c 188 std::string response = sendCommand("leave", 10000, "<4.00>");
AxedaCorp 0:65004368569c 189 response = sendCommand("show net", 5000, "Links");
AxedaCorp 0:65004368569c 190 //printf("Response: %s\n\r", response.c_str());
AxedaCorp 0:65004368569c 191 if (response.find("Assoc=FAIL") != string::npos) {
AxedaCorp 0:65004368569c 192 printf("[DEBUG] Successfully disconnected from network\r\n");
AxedaCorp 0:65004368569c 193 } else {
AxedaCorp 0:65004368569c 194 printf("[ERROR] Failed in disconnecting from network. Continuing ...\r\n");
AxedaCorp 0:65004368569c 195 }
AxedaCorp 0:65004368569c 196
AxedaCorp 0:65004368569c 197 wifiConnected = false;
AxedaCorp 0:65004368569c 198 }
AxedaCorp 0:65004368569c 199
AxedaCorp 0:65004368569c 200 bool Wifi::isConnected()
AxedaCorp 0:65004368569c 201 {
AxedaCorp 0:65004368569c 202 //1) Check if SSID was set
AxedaCorp 0:65004368569c 203 if(_ssid.size() == 0) {
AxedaCorp 0:65004368569c 204 printf("[DEBUG] SSID is not set\r\n");
AxedaCorp 0:65004368569c 205 return false;
AxedaCorp 0:65004368569c 206 }
AxedaCorp 0:65004368569c 207
AxedaCorp 0:65004368569c 208 //1) Check that we do not have a live connection up
AxedaCorp 0:65004368569c 209 if(isOpen()) {
AxedaCorp 0:65004368569c 210 printf("[DEBUG] Socket is opened\r\n");
AxedaCorp 0:65004368569c 211 return true;
AxedaCorp 0:65004368569c 212 }
AxedaCorp 0:65004368569c 213
AxedaCorp 0:65004368569c 214 //Check command mode.
AxedaCorp 0:65004368569c 215 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 216 return false;
AxedaCorp 0:65004368569c 217 }
AxedaCorp 0:65004368569c 218
AxedaCorp 0:65004368569c 219 //2) Query the wifi module
AxedaCorp 0:65004368569c 220 wifiConnected = false;
AxedaCorp 0:65004368569c 221 std::string result = sendCommand("show net", 5000, "Links");
AxedaCorp 0:65004368569c 222 //printf("netResult: %s\n\r", result);
AxedaCorp 0:65004368569c 223 if(result.find("Assoc=OK") != std::string::npos) {
AxedaCorp 0:65004368569c 224 wifiConnected = true;
AxedaCorp 0:65004368569c 225 }
AxedaCorp 0:65004368569c 226
AxedaCorp 0:65004368569c 227 return wifiConnected;
AxedaCorp 0:65004368569c 228 }
AxedaCorp 0:65004368569c 229
AxedaCorp 0:65004368569c 230 bool Wifi::bind(unsigned int port)
AxedaCorp 0:65004368569c 231 {
AxedaCorp 0:65004368569c 232 if(socketOpened) {
AxedaCorp 0:65004368569c 233 printf("[ERROR] socket is open. Can not set local port\r\n");
AxedaCorp 0:65004368569c 234 return false;
AxedaCorp 0:65004368569c 235 }
AxedaCorp 0:65004368569c 236 if(port > 65535) {
AxedaCorp 0:65004368569c 237 printf("[ERROR] port out of range (0-65535)\r\n");
AxedaCorp 0:65004368569c 238 return false;
AxedaCorp 0:65004368569c 239 }
AxedaCorp 0:65004368569c 240 local_port = port;
AxedaCorp 0:65004368569c 241 return true;
AxedaCorp 0:65004368569c 242 }
AxedaCorp 0:65004368569c 243
AxedaCorp 0:65004368569c 244 bool Wifi::open(const std::string& address, unsigned int port, Mode mode)
AxedaCorp 0:65004368569c 245 {
AxedaCorp 0:65004368569c 246 char buffer[256] = {0};
AxedaCorp 0:65004368569c 247 printf("[DEBUG] Attempting to Open Socket\r\n");
AxedaCorp 0:65004368569c 248
AxedaCorp 0:65004368569c 249 //1) Check that we do not have a live connection up
AxedaCorp 0:65004368569c 250 if(socketOpened) {
AxedaCorp 0:65004368569c 251 //Check that the address, port, and mode match
AxedaCorp 0:65004368569c 252 if(host_address != address || host_port != port || this->mode != mode) {
AxedaCorp 0:65004368569c 253 if(this->mode == TCP) {
AxedaCorp 0:65004368569c 254 printf("[ERROR] TCP socket already opened (%s:%d)\r\n", host_address.c_str(), host_port);
AxedaCorp 0:65004368569c 255 } else {
AxedaCorp 0:65004368569c 256 printf("[ERROR] UDP socket already opened (%s:%d)\r\n", host_address.c_str(), host_port);
AxedaCorp 0:65004368569c 257 }
AxedaCorp 0:65004368569c 258 return false;
AxedaCorp 0:65004368569c 259 }
AxedaCorp 0:65004368569c 260
AxedaCorp 0:65004368569c 261 printf("[DEBUG] Socket already opened\r\n");
AxedaCorp 0:65004368569c 262 return true;
AxedaCorp 0:65004368569c 263 }
AxedaCorp 0:65004368569c 264
AxedaCorp 0:65004368569c 265 //2) Check Parameters
AxedaCorp 0:65004368569c 266 if(port > 65535) {
AxedaCorp 0:65004368569c 267 printf("[ERROR] port out of range (0-65535)\r\n");
AxedaCorp 0:65004368569c 268 return false;
AxedaCorp 0:65004368569c 269 }
AxedaCorp 0:65004368569c 270
AxedaCorp 0:65004368569c 271
AxedaCorp 0:65004368569c 272 //3) Check Wifi network connection
AxedaCorp 0:65004368569c 273 if(!isConnected()) {
AxedaCorp 0:65004368569c 274 printf("[ERROR] Wifi network not connected. Attempting to connect\r\n");
AxedaCorp 0:65004368569c 275 if(!connect()) {
AxedaCorp 0:65004368569c 276 printf("[ERROR] Wifi network connection failed\r\n");
AxedaCorp 0:65004368569c 277 return false;
AxedaCorp 0:65004368569c 278 } else {
AxedaCorp 0:65004368569c 279 printf("[DEBUG] Wifi connection established\r\n");
AxedaCorp 0:65004368569c 280 }
AxedaCorp 0:65004368569c 281 }
AxedaCorp 0:65004368569c 282
AxedaCorp 0:65004368569c 283 //Check command mode
AxedaCorp 0:65004368569c 284 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 285 return false;
AxedaCorp 0:65004368569c 286 }
AxedaCorp 0:65004368569c 287
AxedaCorp 0:65004368569c 288 //Set Local Port
AxedaCorp 0:65004368569c 289 if(local_port != 0) {
AxedaCorp 0:65004368569c 290 //Attempt to set local port
AxedaCorp 0:65004368569c 291 sprintf(buffer, "set ip localport %d", local_port);
AxedaCorp 0:65004368569c 292 Code code = sendBasicCommand(buffer, 1000);
AxedaCorp 0:65004368569c 293 if(code != SUCCESS) {
AxedaCorp 0:65004368569c 294 printf("[WARNING] Unable to set local port (%d) [%d]. Continuing...\r\n", local_port, (int) code);
AxedaCorp 0:65004368569c 295 }
AxedaCorp 0:65004368569c 296 }
AxedaCorp 0:65004368569c 297
AxedaCorp 0:65004368569c 298 //Set TCP/UDP parameters
AxedaCorp 0:65004368569c 299 sprintf(buffer, "set ip remote %d", port);
AxedaCorp 0:65004368569c 300 if(sendBasicCommand(buffer, 1000) == SUCCESS) {
AxedaCorp 0:65004368569c 301 host_port = port;
AxedaCorp 0:65004368569c 302 } else {
AxedaCorp 0:65004368569c 303 printf("[ERROR] Host port could not be set\r\n");
AxedaCorp 0:65004368569c 304 }
AxedaCorp 0:65004368569c 305
AxedaCorp 0:65004368569c 306 //Check if address of URL
AxedaCorp 0:65004368569c 307 std::vector<std::string> tmp = Text::split(address, '.');
AxedaCorp 0:65004368569c 308 if(tmp.size() != 4) {
AxedaCorp 0:65004368569c 309 std::string ip = getHostByName(address);
AxedaCorp 0:65004368569c 310 if(ip.size() != 0) {
AxedaCorp 0:65004368569c 311 host_address = ip;
AxedaCorp 0:65004368569c 312 } else {
AxedaCorp 0:65004368569c 313 return false;
AxedaCorp 0:65004368569c 314 }
AxedaCorp 0:65004368569c 315 } else {
AxedaCorp 0:65004368569c 316 host_address = address;
AxedaCorp 0:65004368569c 317 }
AxedaCorp 0:65004368569c 318
AxedaCorp 0:65004368569c 319 //Set Address
AxedaCorp 0:65004368569c 320 printf("[DEBUG] Host address: %s\n\r", host_address.c_str());
AxedaCorp 0:65004368569c 321 if(sendBasicCommand("set ip host " + host_address, 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 322 printf("[ERROR] Host address could not be set\r\n");
AxedaCorp 0:65004368569c 323 return false;
AxedaCorp 0:65004368569c 324 }
AxedaCorp 0:65004368569c 325
AxedaCorp 0:65004368569c 326 if(sendBasicCommand("set ip protocol 8", 1000) != SUCCESS) {
AxedaCorp 0:65004368569c 327 printf("[ERROR] Failed to set TCP mode\r\n");
AxedaCorp 0:65004368569c 328 return false;
AxedaCorp 0:65004368569c 329 }
AxedaCorp 0:65004368569c 330
AxedaCorp 0:65004368569c 331 // Try and Connect
AxedaCorp 0:65004368569c 332 std::string sMode;
AxedaCorp 0:65004368569c 333 std::string sOpenSocketCmd;
AxedaCorp 0:65004368569c 334 if(mode == TCP) {
AxedaCorp 0:65004368569c 335 sOpenSocketCmd = "open";
AxedaCorp 0:65004368569c 336 sMode = "TCP";
AxedaCorp 0:65004368569c 337 } else {
AxedaCorp 0:65004368569c 338 //TODO
AxedaCorp 0:65004368569c 339 //sOpenSocketCmd = "AT#OUDP";
AxedaCorp 0:65004368569c 340 //sMode = "UDP";
AxedaCorp 0:65004368569c 341 }
AxedaCorp 0:65004368569c 342 string response = sendCommand(sOpenSocketCmd, 10000, "OPEN");
AxedaCorp 0:65004368569c 343 if (response.find("OPEN") != string::npos) {
AxedaCorp 0:65004368569c 344 printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), host_address.c_str(), port);
AxedaCorp 0:65004368569c 345 socketOpened = true;
AxedaCorp 0:65004368569c 346 cmdOn = false;
AxedaCorp 0:65004368569c 347 } else {
AxedaCorp 0:65004368569c 348 printf("[WARNING] Unable to open %s Socket [%s:%d]\r\n", sMode.c_str(), host_address.c_str(), port);
AxedaCorp 0:65004368569c 349 socketOpened = false;
AxedaCorp 0:65004368569c 350 }
AxedaCorp 0:65004368569c 351
AxedaCorp 0:65004368569c 352 return socketOpened;
AxedaCorp 0:65004368569c 353 }
AxedaCorp 0:65004368569c 354
AxedaCorp 0:65004368569c 355 bool Wifi::isOpen()
AxedaCorp 0:65004368569c 356 {
AxedaCorp 0:65004368569c 357 if(io->readable()) {
AxedaCorp 0:65004368569c 358 printf("[DEBUG] Assuming open, data available to read.\n\r");
AxedaCorp 0:65004368569c 359 return true;
AxedaCorp 0:65004368569c 360 }
AxedaCorp 0:65004368569c 361 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 362 printf("[ERROR] Failed to properly check if TCP connection is open.\r\n");
AxedaCorp 0:65004368569c 363 return socketOpened;
AxedaCorp 0:65004368569c 364 }
AxedaCorp 0:65004368569c 365 std::string response = sendCommand("show connection", 2000, "\n");
AxedaCorp 0:65004368569c 366 int start = response.find("f");
AxedaCorp 0:65004368569c 367 if(start != string::npos && response.size() >= (start + 3)) {
AxedaCorp 0:65004368569c 368 if(response[start + 3] == '1') {
AxedaCorp 0:65004368569c 369 socketOpened = true;
AxedaCorp 0:65004368569c 370 } else {
AxedaCorp 0:65004368569c 371 socketOpened = false;
AxedaCorp 0:65004368569c 372 }
AxedaCorp 0:65004368569c 373 } else {
AxedaCorp 0:65004368569c 374 printf("[WARNING] Trouble checking TCP Connection status.\n\r");
AxedaCorp 0:65004368569c 375 }
AxedaCorp 0:65004368569c 376 return socketOpened;
AxedaCorp 0:65004368569c 377 }
AxedaCorp 0:65004368569c 378
AxedaCorp 0:65004368569c 379 bool Wifi::close()
AxedaCorp 0:65004368569c 380 {
AxedaCorp 0:65004368569c 381 wait(1);
AxedaCorp 0:65004368569c 382 if(io == NULL) {
AxedaCorp 0:65004368569c 383 printf("[ERROR] MTSBufferedIO not set\r\n");
AxedaCorp 0:65004368569c 384 return false;
AxedaCorp 0:65004368569c 385 }
AxedaCorp 0:65004368569c 386
AxedaCorp 0:65004368569c 387 if(!socketOpened) {
AxedaCorp 0:65004368569c 388 printf("[WARNING] Socket close() called, but socket was not open\r\n");
AxedaCorp 0:65004368569c 389 return true;
AxedaCorp 0:65004368569c 390 }
AxedaCorp 0:65004368569c 391
AxedaCorp 0:65004368569c 392 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 393 printf("[ERROR] Failed to close socket\r\n");
AxedaCorp 0:65004368569c 394 return false;
AxedaCorp 0:65004368569c 395 }
AxedaCorp 0:65004368569c 396
AxedaCorp 0:65004368569c 397 if(isOpen()) {
AxedaCorp 0:65004368569c 398 std::string response = sendCommand("close", 3000, "CLOS");
AxedaCorp 0:65004368569c 399 if(response.find("CLOS") == string::npos) {
AxedaCorp 0:65004368569c 400 printf("[WARNING] Failed to successfully close socket...\r\n");
AxedaCorp 0:65004368569c 401 return false;
AxedaCorp 0:65004368569c 402 }
AxedaCorp 0:65004368569c 403 }
AxedaCorp 0:65004368569c 404
AxedaCorp 0:65004368569c 405 wait(1); //Wait so the subsequent isOpen calls return correctly.
AxedaCorp 0:65004368569c 406 io->rxClear();
AxedaCorp 0:65004368569c 407 io->txClear();
AxedaCorp 0:65004368569c 408
AxedaCorp 0:65004368569c 409 return true;
AxedaCorp 0:65004368569c 410 }
AxedaCorp 0:65004368569c 411
AxedaCorp 0:65004368569c 412 int Wifi::read(char* data, int max, int timeout)
AxedaCorp 0:65004368569c 413 {
AxedaCorp 0:65004368569c 414 if(io == NULL) {
AxedaCorp 0:65004368569c 415 printf("[ERROR] MTSBufferedIO not set\r\n");
AxedaCorp 0:65004368569c 416 return -1;
AxedaCorp 0:65004368569c 417 }
AxedaCorp 0:65004368569c 418
AxedaCorp 0:65004368569c 419 //Check that nothing is in the rx buffer
AxedaCorp 0:65004368569c 420 if(!socketOpened && !io->readable()) {
AxedaCorp 0:65004368569c 421 printf("[ERROR] Socket is not open\r\n");
AxedaCorp 0:65004368569c 422 return -1;
AxedaCorp 0:65004368569c 423 }
AxedaCorp 0:65004368569c 424
AxedaCorp 0:65004368569c 425 //Check for data mode
AxedaCorp 0:65004368569c 426 if(!setCmdMode(false)) {
AxedaCorp 0:65004368569c 427 printf("[ERROR] Failed to read data due to mode\r\n");
AxedaCorp 0:65004368569c 428 return -1;
AxedaCorp 0:65004368569c 429 }
AxedaCorp 0:65004368569c 430
AxedaCorp 0:65004368569c 431 int bytesRead = 0;
AxedaCorp 0:65004368569c 432
AxedaCorp 0:65004368569c 433 if(timeout >= 0) {
AxedaCorp 0:65004368569c 434 bytesRead = io->read(data, max, static_cast<unsigned int>(timeout));
AxedaCorp 0:65004368569c 435 } else {
AxedaCorp 0:65004368569c 436 bytesRead = io->read(data, max);
AxedaCorp 0:65004368569c 437 }
AxedaCorp 0:65004368569c 438
AxedaCorp 0:65004368569c 439 return bytesRead;
AxedaCorp 0:65004368569c 440 }
AxedaCorp 0:65004368569c 441
AxedaCorp 0:65004368569c 442 int Wifi::write(const char* data, int length, int timeout)
AxedaCorp 0:65004368569c 443 {
AxedaCorp 0:65004368569c 444 if(io == NULL) {
AxedaCorp 0:65004368569c 445 printf("[ERROR] MTSBufferedIO not set\r\n");
AxedaCorp 0:65004368569c 446 return -1;
AxedaCorp 0:65004368569c 447 }
AxedaCorp 0:65004368569c 448
AxedaCorp 0:65004368569c 449 if(!socketOpened) {
AxedaCorp 0:65004368569c 450 printf("[ERROR] Socket is not open\r\n");
AxedaCorp 0:65004368569c 451 return -1;
AxedaCorp 0:65004368569c 452 }
AxedaCorp 0:65004368569c 453
AxedaCorp 0:65004368569c 454 //Check for data mode
AxedaCorp 0:65004368569c 455 if(!setCmdMode(false)) {
AxedaCorp 0:65004368569c 456 printf("[ERROR] Failed to write data due to mode\r\n");
AxedaCorp 0:65004368569c 457 return -1;
AxedaCorp 0:65004368569c 458 }
AxedaCorp 0:65004368569c 459
AxedaCorp 0:65004368569c 460 int bytesWritten = 0;
AxedaCorp 0:65004368569c 461
AxedaCorp 0:65004368569c 462 if(timeout >= 0) {
AxedaCorp 0:65004368569c 463 bytesWritten = io->write(data, length, static_cast<unsigned int>(timeout));
AxedaCorp 0:65004368569c 464 } else {
AxedaCorp 0:65004368569c 465 bytesWritten = io->write(data, length);
AxedaCorp 0:65004368569c 466 }
AxedaCorp 0:65004368569c 467
AxedaCorp 0:65004368569c 468 return bytesWritten;
AxedaCorp 0:65004368569c 469 }
AxedaCorp 0:65004368569c 470
AxedaCorp 0:65004368569c 471 unsigned int Wifi::readable()
AxedaCorp 0:65004368569c 472 {
AxedaCorp 0:65004368569c 473 if(io == NULL) {
AxedaCorp 0:65004368569c 474 printf("[ERROR] MTSBufferedIO not set\r\n");
AxedaCorp 0:65004368569c 475 return 0;
AxedaCorp 0:65004368569c 476 }
AxedaCorp 0:65004368569c 477 if(!socketOpened) {
AxedaCorp 0:65004368569c 478 printf("[ERROR] Socket is not open\r\n");
AxedaCorp 0:65004368569c 479 return 0;
AxedaCorp 0:65004368569c 480 }
AxedaCorp 0:65004368569c 481 return io->readable();
AxedaCorp 0:65004368569c 482 }
AxedaCorp 0:65004368569c 483
AxedaCorp 0:65004368569c 484 unsigned int Wifi::writeable()
AxedaCorp 0:65004368569c 485 {
AxedaCorp 0:65004368569c 486 if(io == NULL) {
AxedaCorp 0:65004368569c 487 printf("[ERROR] MTSBufferedIO not set\r\n");
AxedaCorp 0:65004368569c 488 return 0;
AxedaCorp 0:65004368569c 489 }
AxedaCorp 0:65004368569c 490 if(!socketOpened) {
AxedaCorp 0:65004368569c 491 printf("[ERROR] Socket is not open\r\n");
AxedaCorp 0:65004368569c 492 return 0;
AxedaCorp 0:65004368569c 493 }
AxedaCorp 0:65004368569c 494
AxedaCorp 0:65004368569c 495 return io->writeable();
AxedaCorp 0:65004368569c 496 }
AxedaCorp 0:65004368569c 497
AxedaCorp 0:65004368569c 498 void Wifi::reset()
AxedaCorp 0:65004368569c 499 {
AxedaCorp 0:65004368569c 500 if(!sortInterfaceMode()) {
AxedaCorp 0:65004368569c 501 return;
AxedaCorp 0:65004368569c 502 }
AxedaCorp 0:65004368569c 503
AxedaCorp 0:65004368569c 504 sendCommand("factory RESET", 2000, "Set Factory Default"); // <ver> comes out about 1 sec later
AxedaCorp 0:65004368569c 505 wait(0.5f);
AxedaCorp 0:65004368569c 506 sendCommand("reboot", 2000, "*READY*");
AxedaCorp 0:65004368569c 507
AxedaCorp 0:65004368569c 508 wifiConnected = false;
AxedaCorp 0:65004368569c 509 _ssid = "";
AxedaCorp 0:65004368569c 510 mode = TCP;
AxedaCorp 0:65004368569c 511 socketOpened = false;
AxedaCorp 0:65004368569c 512 socketCloseable = true;
AxedaCorp 0:65004368569c 513 local_port = 0;
AxedaCorp 0:65004368569c 514 local_address = "";
AxedaCorp 0:65004368569c 515 host_port = 0;
AxedaCorp 0:65004368569c 516 cmdOn = false;
AxedaCorp 0:65004368569c 517 wait(1);
AxedaCorp 0:65004368569c 518 // if(!init(io)) {
AxedaCorp 0:65004368569c 519 // printf("[ERROR] Failed to reinitialize after reset.\n\r");
AxedaCorp 0:65004368569c 520 // }
AxedaCorp 0:65004368569c 521 }
AxedaCorp 0:65004368569c 522
AxedaCorp 0:65004368569c 523 Code Wifi::setDeviceIP(std::string address)
AxedaCorp 0:65004368569c 524 {
AxedaCorp 0:65004368569c 525 //Check for command mode
AxedaCorp 0:65004368569c 526 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 527 printf("[ERROR] Failed to set IP due to mode issue\r\n");
AxedaCorp 0:65004368569c 528 return FAILURE;
AxedaCorp 0:65004368569c 529 }
AxedaCorp 0:65004368569c 530
AxedaCorp 0:65004368569c 531 //Set to DHCP mode
AxedaCorp 0:65004368569c 532 if(address.compare("DHCP") == 0) {
AxedaCorp 0:65004368569c 533 return sendBasicCommand("set ip dhcp 1", 1000);
AxedaCorp 0:65004368569c 534 }
AxedaCorp 0:65004368569c 535
AxedaCorp 0:65004368569c 536 //Set to static mode and set address
AxedaCorp 0:65004368569c 537 Code code = sendBasicCommand("set ip address " + address, 1000);
AxedaCorp 0:65004368569c 538 if(code != SUCCESS) {
AxedaCorp 0:65004368569c 539 return code;
AxedaCorp 0:65004368569c 540 }
AxedaCorp 0:65004368569c 541 code = sendBasicCommand("set ip dhcp 0", 1000);
AxedaCorp 0:65004368569c 542 if(code != SUCCESS) {
AxedaCorp 0:65004368569c 543 return code;
AxedaCorp 0:65004368569c 544 }
AxedaCorp 0:65004368569c 545 local_address = address;
AxedaCorp 0:65004368569c 546 return SUCCESS;
AxedaCorp 0:65004368569c 547 }
AxedaCorp 0:65004368569c 548
AxedaCorp 0:65004368569c 549 std::string Wifi::getDeviceIP()
AxedaCorp 0:65004368569c 550 {
AxedaCorp 0:65004368569c 551 return local_address;
AxedaCorp 0:65004368569c 552 }
AxedaCorp 0:65004368569c 553
AxedaCorp 0:65004368569c 554 Code Wifi::setNetwork(const std::string& ssid, SecurityType type, const std::string& key)
AxedaCorp 0:65004368569c 555 {
AxedaCorp 0:65004368569c 556 //Check the command mode
AxedaCorp 0:65004368569c 557 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 558 return FAILURE;
AxedaCorp 0:65004368569c 559 }
AxedaCorp 0:65004368569c 560
AxedaCorp 0:65004368569c 561 Code code;
AxedaCorp 0:65004368569c 562
AxedaCorp 0:65004368569c 563 //Set the appropraite SSID
AxedaCorp 0:65004368569c 564 code = sendBasicCommand("set wlan ssid " + ssid, 1000);
AxedaCorp 0:65004368569c 565 if (code != SUCCESS) {
AxedaCorp 0:65004368569c 566 return code;
AxedaCorp 0:65004368569c 567 }
AxedaCorp 0:65004368569c 568
AxedaCorp 0:65004368569c 569 //Set the security key
AxedaCorp 0:65004368569c 570 if (type == WEP64 || type == WEP128) {
AxedaCorp 0:65004368569c 571 //Set the WEP key if using WEP encryption
AxedaCorp 0:65004368569c 572 code = sendBasicCommand("set wlan key " + key, 1000);
AxedaCorp 0:65004368569c 573 if (code != SUCCESS) {
AxedaCorp 0:65004368569c 574 return code;
AxedaCorp 0:65004368569c 575 }
AxedaCorp 0:65004368569c 576 } else if (type == WPA || type == WPA2) {
AxedaCorp 0:65004368569c 577 //Set the WPA key if using WPA encryption
AxedaCorp 0:65004368569c 578 code = sendBasicCommand("set wlan phrase " + key, 1000);
AxedaCorp 0:65004368569c 579 if (code != SUCCESS) {
AxedaCorp 0:65004368569c 580 return code;
AxedaCorp 0:65004368569c 581 }
AxedaCorp 0:65004368569c 582 }
AxedaCorp 0:65004368569c 583
AxedaCorp 0:65004368569c 584 _ssid = ssid;
AxedaCorp 0:65004368569c 585 return SUCCESS;
AxedaCorp 0:65004368569c 586 }
AxedaCorp 0:65004368569c 587
AxedaCorp 0:65004368569c 588 Code Wifi::setDNS(const std::string& dnsName)
AxedaCorp 0:65004368569c 589 {
AxedaCorp 0:65004368569c 590 //Check the command mode
AxedaCorp 0:65004368569c 591 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 592 return FAILURE;
AxedaCorp 0:65004368569c 593 }
AxedaCorp 0:65004368569c 594
AxedaCorp 0:65004368569c 595 return sendBasicCommand("set dns name " + dnsName, 1000);
AxedaCorp 0:65004368569c 596 }
AxedaCorp 0:65004368569c 597
AxedaCorp 0:65004368569c 598 int Wifi::getSignalStrength()
AxedaCorp 0:65004368569c 599 {
AxedaCorp 0:65004368569c 600 //Signal strength does not report correctly if not connected
AxedaCorp 0:65004368569c 601 if(!wifiConnected) {
AxedaCorp 0:65004368569c 602 printf("[ERROR] Could not get RSSI, Wifi network not connected.\n\r");
AxedaCorp 0:65004368569c 603 return 99;
AxedaCorp 0:65004368569c 604 }
AxedaCorp 0:65004368569c 605
AxedaCorp 0:65004368569c 606 //Check the command mode
AxedaCorp 0:65004368569c 607 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 608 printf("[ERROR] Could not get RSSI\n\r");
AxedaCorp 0:65004368569c 609 return 99;
AxedaCorp 0:65004368569c 610 }
AxedaCorp 0:65004368569c 611
AxedaCorp 0:65004368569c 612 string response = sendCommand("show rssi", 2000, "dBm");
AxedaCorp 0:65004368569c 613 if (response.find("RSSI") == string::npos) {
AxedaCorp 0:65004368569c 614 printf("[ERROR] Could not get RSSI\n\r");
AxedaCorp 0:65004368569c 615 return 99;
AxedaCorp 0:65004368569c 616 }
AxedaCorp 0:65004368569c 617 int start = response.find('(');
AxedaCorp 0:65004368569c 618 int stop = response.find(')', start);
AxedaCorp 0:65004368569c 619 string signal = response.substr(start + 1, stop - start - 1);
AxedaCorp 0:65004368569c 620 int value;
AxedaCorp 0:65004368569c 621 sscanf(signal.c_str(), "%d", &value);
AxedaCorp 0:65004368569c 622 return value;
AxedaCorp 0:65004368569c 623 }
AxedaCorp 0:65004368569c 624
AxedaCorp 0:65004368569c 625 bool Wifi::ping(const std::string& address)
AxedaCorp 0:65004368569c 626 {
AxedaCorp 0:65004368569c 627 //Check the command mode
AxedaCorp 0:65004368569c 628 if(!setCmdMode(true)) {
AxedaCorp 0:65004368569c 629 printf("[ERROR] Could not send ping command\n\r");
AxedaCorp 0:65004368569c 630 return false;
AxedaCorp 0:65004368569c 631 }
AxedaCorp 0:65004368569c 632
AxedaCorp 0:65004368569c 633 std::string response;
AxedaCorp 0:65004368569c 634 for (int i = 0; i < PINGNUM; i++) {
AxedaCorp 0:65004368569c 635 response = sendCommand("ping " + address, PINGDELAY * 1000, "reply");
AxedaCorp 0:65004368569c 636 if (response.find("reply") != std::string::npos) {
AxedaCorp 0:65004368569c 637 return true;
AxedaCorp 0:65004368569c 638 }
AxedaCorp 0:65004368569c 639 }
AxedaCorp 0:65004368569c 640 return false;
AxedaCorp 0:65004368569c 641 }
AxedaCorp 0:65004368569c 642
AxedaCorp 0:65004368569c 643 bool Wifi::setCmdMode(bool on)
AxedaCorp 0:65004368569c 644 {
AxedaCorp 0:65004368569c 645 if (on) {
AxedaCorp 0:65004368569c 646 if (cmdOn) {
AxedaCorp 0:65004368569c 647 return true;
AxedaCorp 0:65004368569c 648 }
AxedaCorp 0:65004368569c 649 wait(.5);
AxedaCorp 0:65004368569c 650 std::string response = sendCommand("$$", 2000, "CMD", '$');
AxedaCorp 0:65004368569c 651 if (response.find("CMD") != string::npos) {
AxedaCorp 0:65004368569c 652 cmdOn = true;
AxedaCorp 0:65004368569c 653 wait(.5);
AxedaCorp 0:65004368569c 654 return true;
AxedaCorp 0:65004368569c 655 }
AxedaCorp 0:65004368569c 656 printf("[ERROR] Failed to enter command mode\n\r");
AxedaCorp 0:65004368569c 657 return false;
AxedaCorp 0:65004368569c 658 } else {
AxedaCorp 0:65004368569c 659 if (!cmdOn) {
AxedaCorp 0:65004368569c 660 return true;
AxedaCorp 0:65004368569c 661 }
AxedaCorp 0:65004368569c 662 std::string response = sendCommand("exit", 2000, "EXIT");
AxedaCorp 0:65004368569c 663 if (response.find("EXIT") != string::npos) {
AxedaCorp 0:65004368569c 664 cmdOn = false;
AxedaCorp 0:65004368569c 665 return true;
AxedaCorp 0:65004368569c 666 }
AxedaCorp 0:65004368569c 667 printf("[ERROR] Failed to exit command mode\n\r");
AxedaCorp 0:65004368569c 668 return false;
AxedaCorp 0:65004368569c 669 }
AxedaCorp 0:65004368569c 670 }
AxedaCorp 0:65004368569c 671
AxedaCorp 0:65004368569c 672 std::string Wifi::getHostByName(std::string url)
AxedaCorp 0:65004368569c 673 {
AxedaCorp 0:65004368569c 674 std::string response = sendCommand("lookup " + url, 3000, "<4.00>");
AxedaCorp 0:65004368569c 675 int start = response.find("=");
AxedaCorp 0:65004368569c 676 int stop = response.find("\r");
AxedaCorp 0:65004368569c 677 if(start == string::npos || stop == string::npos) {
AxedaCorp 0:65004368569c 678 printf("[ERROR] Failed to resolve URL [%s]", response.c_str());
AxedaCorp 0:65004368569c 679 return "";
AxedaCorp 0:65004368569c 680 }
AxedaCorp 0:65004368569c 681 std::string ip = response.substr(start + 1, stop - start - 1);
AxedaCorp 0:65004368569c 682 //printf("Data: %s\n\r", ip);
AxedaCorp 0:65004368569c 683 return ip;
AxedaCorp 0:65004368569c 684 }
AxedaCorp 0:65004368569c 685
AxedaCorp 0:65004368569c 686 Code Wifi::sendBasicCommand(string command, int timeoutMillis, char esc)
AxedaCorp 0:65004368569c 687 {
AxedaCorp 0:65004368569c 688 if(socketOpened) {
AxedaCorp 0:65004368569c 689 printf("[ERROR] socket is open. Can not send AT commands\r\n");
AxedaCorp 0:65004368569c 690 return ERROR;
AxedaCorp 0:65004368569c 691 }
AxedaCorp 0:65004368569c 692
AxedaCorp 0:65004368569c 693 string response = sendCommand(command, timeoutMillis, "AOK", esc);
AxedaCorp 0:65004368569c 694 //printf("Response: %s\n\r", response.c_str());
AxedaCorp 0:65004368569c 695 if (response.size() == 0) {
AxedaCorp 0:65004368569c 696 return NO_RESPONSE;
AxedaCorp 0:65004368569c 697 } else if (response.find("AOK") != string::npos) {
AxedaCorp 0:65004368569c 698 return SUCCESS;
AxedaCorp 0:65004368569c 699 } else if (response.find("ERR") != string::npos) {
AxedaCorp 0:65004368569c 700 return ERROR;
AxedaCorp 0:65004368569c 701 } else {
AxedaCorp 0:65004368569c 702 return FAILURE;
AxedaCorp 0:65004368569c 703 }
AxedaCorp 0:65004368569c 704 }
AxedaCorp 0:65004368569c 705
AxedaCorp 0:65004368569c 706 string Wifi::sendCommand(string command, int timeoutMillis, std::string response, char esc)
AxedaCorp 0:65004368569c 707 {
AxedaCorp 0:65004368569c 708 if(io == NULL) {
AxedaCorp 0:65004368569c 709 printf("[ERROR] MTSBufferedIO not set\r\n");
AxedaCorp 0:65004368569c 710 return "";
AxedaCorp 0:65004368569c 711 }
AxedaCorp 0:65004368569c 712 //if(socketOpened && command.compare("$$") != 0 && command.compare("exit") != 0 && command.compare("close") != 0) {
AxedaCorp 0:65004368569c 713 // printf("[ERROR] socket is open. Can not send AT commands\r\n");
AxedaCorp 0:65004368569c 714 // return "";
AxedaCorp 0:65004368569c 715 //}
AxedaCorp 0:65004368569c 716
AxedaCorp 0:65004368569c 717 io->rxClear();
AxedaCorp 0:65004368569c 718 io->txClear();
AxedaCorp 0:65004368569c 719 std::string result;
AxedaCorp 0:65004368569c 720
AxedaCorp 0:65004368569c 721 //Attempt to write command
AxedaCorp 0:65004368569c 722 if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) {
AxedaCorp 0:65004368569c 723 //Failed to write command
AxedaCorp 0:65004368569c 724 printf("[ERROR] failed to send command to radio within %d milliseconds\r\n", timeoutMillis);
AxedaCorp 0:65004368569c 725 return "";
AxedaCorp 0:65004368569c 726 }
AxedaCorp 0:65004368569c 727 //Send Escape Character
AxedaCorp 0:65004368569c 728 if (esc != 0x00) {
AxedaCorp 0:65004368569c 729 if(io->write(esc, timeoutMillis) != 1) {
AxedaCorp 0:65004368569c 730 printf("[ERROR] failed to send '%c' to radio within %d milliseconds\r\n", esc, timeoutMillis);
AxedaCorp 0:65004368569c 731 return "";
AxedaCorp 0:65004368569c 732 }
AxedaCorp 0:65004368569c 733 }
AxedaCorp 0:65004368569c 734 DBG("Sending: %s%c", command.data(), esc);
AxedaCorp 0:65004368569c 735
AxedaCorp 0:65004368569c 736 int timer = 0;
AxedaCorp 0:65004368569c 737 size_t previous = 0;
AxedaCorp 0:65004368569c 738 char tmp[256];
AxedaCorp 0:65004368569c 739 tmp[255] = 0;
AxedaCorp 0:65004368569c 740 bool done = false;
AxedaCorp 0:65004368569c 741 do {
AxedaCorp 0:65004368569c 742 wait(.2);
AxedaCorp 0:65004368569c 743 timer = timer + 200;
AxedaCorp 0:65004368569c 744 previous = result.size();
AxedaCorp 0:65004368569c 745 int size = io->read(tmp, 255, 0); //1 less than allocated
AxedaCorp 0:65004368569c 746 if(size > 0) {
AxedaCorp 0:65004368569c 747 result.append(tmp, size);
AxedaCorp 0:65004368569c 748 if (response.size() != 0) {
AxedaCorp 0:65004368569c 749 if (result.find(response) != string::npos) {
AxedaCorp 0:65004368569c 750 goto exit_func;
AxedaCorp 0:65004368569c 751 //return result;
AxedaCorp 0:65004368569c 752 }
AxedaCorp 0:65004368569c 753 } else {
AxedaCorp 0:65004368569c 754 done = (result.size() == previous);
AxedaCorp 0:65004368569c 755 }
AxedaCorp 0:65004368569c 756 }
AxedaCorp 0:65004368569c 757 if(timer >= timeoutMillis) {
AxedaCorp 0:65004368569c 758 if(!(command.compare("reboot") == 0 || command.compare("") == 0)) {
AxedaCorp 0:65004368569c 759 printf("[WARNING] sendCommand [%s] timed out after %d milliseconds\r\n", command.c_str(), timeoutMillis);
AxedaCorp 0:65004368569c 760 }
AxedaCorp 0:65004368569c 761 done = true;
AxedaCorp 0:65004368569c 762 }
AxedaCorp 0:65004368569c 763 } while (!done);
AxedaCorp 0:65004368569c 764
AxedaCorp 0:65004368569c 765 exit_func:
AxedaCorp 0:65004368569c 766 DBG("Result: %s\n\r", result.c_str());
AxedaCorp 0:65004368569c 767 return result;
AxedaCorp 0:65004368569c 768 }
AxedaCorp 0:65004368569c 769