A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Committer:
mfiore
Date:
Thu Aug 28 20:17:36 2014 +0000
Revision:
151:483208276759
Parent:
150:95093b517872
Parent:
148:df9feef182b4
Child:
152:9a2c7ed27744
merge

Who changed what in which revision?

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