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
wifi/Wifi.cpp@152:9a2c7ed27744, 2014-09-02 (annotated)
- Committer:
- mfiore
- Date:
- Tue Sep 02 18:38:55 2014 +0000
- Revision:
- 152:9a2c7ed27744
- Parent:
- 151:483208276759
Wifi: fix compatibility break with old shields by checking for both old and new style responses to "show connection" command
Who changed what in which revision?
User | Revision | Line number | New 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 | 152:9a2c7ed27744 | 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"); |
mfiore | 152:9a2c7ed27744 | 369 | // but for some reason the old wifi sheilds give back a response starting with "f" |
mfiore | 152:9a2c7ed27744 | 370 | // perhaps a different firmware version |
mfiore | 152:9a2c7ed27744 | 371 | if (start == string::npos) { |
mfiore | 152:9a2c7ed27744 | 372 | start = response.find("f"); |
mfiore | 152:9a2c7ed27744 | 373 | } |
jengbrecht | 114:cd34b1d64360 | 374 | if(start != string::npos && response.size() >= (start + 3)) { |
jengbrecht | 114:cd34b1d64360 | 375 | if(response[start + 3] == '1') { |
jengbrecht | 114:cd34b1d64360 | 376 | socketOpened = true; |
jengbrecht | 114:cd34b1d64360 | 377 | } else { |
jengbrecht | 114:cd34b1d64360 | 378 | socketOpened = false; |
jengbrecht | 114:cd34b1d64360 | 379 | } |
jengbrecht | 114:cd34b1d64360 | 380 | } else { |
jengbrecht | 114:cd34b1d64360 | 381 | printf("[WARNING] Trouble checking TCP Connection status.\n\r"); |
jengbrecht | 114:cd34b1d64360 | 382 | } |
jengbrecht | 95:4fdf968b5b37 | 383 | return socketOpened; |
jengbrecht | 69:f3e696bbb0d5 | 384 | } |
jengbrecht | 69:f3e696bbb0d5 | 385 | |
jengbrecht | 69:f3e696bbb0d5 | 386 | bool Wifi::close() |
jengbrecht | 69:f3e696bbb0d5 | 387 | { |
jengbrecht | 114:cd34b1d64360 | 388 | wait(1); |
jengbrecht | 95:4fdf968b5b37 | 389 | if(io == NULL) { |
jengbrecht | 95:4fdf968b5b37 | 390 | printf("[ERROR] MTSBufferedIO not set\r\n"); |
jengbrecht | 95:4fdf968b5b37 | 391 | return false; |
jengbrecht | 95:4fdf968b5b37 | 392 | } |
jengbrecht | 95:4fdf968b5b37 | 393 | |
jengbrecht | 95:4fdf968b5b37 | 394 | if(!socketOpened) { |
jengbrecht | 95:4fdf968b5b37 | 395 | printf("[WARNING] Socket close() called, but socket was not open\r\n"); |
jengbrecht | 95:4fdf968b5b37 | 396 | return true; |
jengbrecht | 95:4fdf968b5b37 | 397 | } |
jengbrecht | 95:4fdf968b5b37 | 398 | |
jengbrecht | 98:dbeac735109d | 399 | if(!setCmdMode(true)) { |
jengbrecht | 98:dbeac735109d | 400 | printf("[ERROR] Failed to close socket\r\n"); |
jengbrecht | 98:dbeac735109d | 401 | return false; |
jengbrecht | 95:4fdf968b5b37 | 402 | } |
jengbrecht | 95:4fdf968b5b37 | 403 | |
jengbrecht | 114:cd34b1d64360 | 404 | if(isOpen()) { |
jengbrecht | 114:cd34b1d64360 | 405 | std::string response = sendCommand("close", 3000, "CLOS"); |
jengbrecht | 114:cd34b1d64360 | 406 | if(response.find("CLOS") == string::npos) { |
jengbrecht | 114:cd34b1d64360 | 407 | printf("[WARNING] Failed to successfully close socket...\r\n"); |
jengbrecht | 114:cd34b1d64360 | 408 | return false; |
jengbrecht | 114:cd34b1d64360 | 409 | } |
jengbrecht | 95:4fdf968b5b37 | 410 | } |
jengbrecht | 121:5a7fba896c98 | 411 | |
jengbrecht | 119:544578fd290c | 412 | wait(1); //Wait so the subsequent isOpen calls return correctly. |
jengbrecht | 113:7238f9b8db17 | 413 | io->rxClear(); |
jengbrecht | 113:7238f9b8db17 | 414 | io->txClear(); |
jengbrecht | 114:cd34b1d64360 | 415 | |
jengbrecht | 69:f3e696bbb0d5 | 416 | return true; |
jengbrecht | 69:f3e696bbb0d5 | 417 | } |
jengbrecht | 69:f3e696bbb0d5 | 418 | |
jengbrecht | 69:f3e696bbb0d5 | 419 | int Wifi::read(char* data, int max, int timeout) |
jengbrecht | 69:f3e696bbb0d5 | 420 | { |
jengbrecht | 98:dbeac735109d | 421 | if(io == NULL) { |
jengbrecht | 98:dbeac735109d | 422 | printf("[ERROR] MTSBufferedIO not set\r\n"); |
jengbrecht | 98:dbeac735109d | 423 | return -1; |
jengbrecht | 98:dbeac735109d | 424 | } |
jengbrecht | 98:dbeac735109d | 425 | |
jengbrecht | 98:dbeac735109d | 426 | //Check that nothing is in the rx buffer |
jengbrecht | 98:dbeac735109d | 427 | if(!socketOpened && !io->readable()) { |
jengbrecht | 98:dbeac735109d | 428 | printf("[ERROR] Socket is not open\r\n"); |
jengbrecht | 98:dbeac735109d | 429 | return -1; |
jengbrecht | 98:dbeac735109d | 430 | } |
jengbrecht | 98:dbeac735109d | 431 | |
jengbrecht | 98:dbeac735109d | 432 | //Check for data mode |
jengbrecht | 98:dbeac735109d | 433 | if(!setCmdMode(false)) { |
jengbrecht | 98:dbeac735109d | 434 | printf("[ERROR] Failed to read data due to mode\r\n"); |
jengbrecht | 98:dbeac735109d | 435 | return -1; |
jengbrecht | 98:dbeac735109d | 436 | } |
jengbrecht | 98:dbeac735109d | 437 | |
jengbrecht | 98:dbeac735109d | 438 | int bytesRead = 0; |
jengbrecht | 98:dbeac735109d | 439 | |
jengbrecht | 98:dbeac735109d | 440 | if(timeout >= 0) { |
jengbrecht | 98:dbeac735109d | 441 | bytesRead = io->read(data, max, static_cast<unsigned int>(timeout)); |
jengbrecht | 98:dbeac735109d | 442 | } else { |
jengbrecht | 98:dbeac735109d | 443 | bytesRead = io->read(data, max); |
jengbrecht | 98:dbeac735109d | 444 | } |
jengbrecht | 98:dbeac735109d | 445 | |
jengbrecht | 98:dbeac735109d | 446 | return bytesRead; |
jengbrecht | 69:f3e696bbb0d5 | 447 | } |
jengbrecht | 69:f3e696bbb0d5 | 448 | |
jengbrecht | 69:f3e696bbb0d5 | 449 | int Wifi::write(const char* data, int length, int timeout) |
jengbrecht | 69:f3e696bbb0d5 | 450 | { |
jengbrecht | 98:dbeac735109d | 451 | if(io == NULL) { |
jengbrecht | 98:dbeac735109d | 452 | printf("[ERROR] MTSBufferedIO not set\r\n"); |
jengbrecht | 98:dbeac735109d | 453 | return -1; |
jengbrecht | 98:dbeac735109d | 454 | } |
jengbrecht | 98:dbeac735109d | 455 | |
jengbrecht | 98:dbeac735109d | 456 | if(!socketOpened) { |
jengbrecht | 98:dbeac735109d | 457 | printf("[ERROR] Socket is not open\r\n"); |
jengbrecht | 98:dbeac735109d | 458 | return -1; |
jengbrecht | 98:dbeac735109d | 459 | } |
jengbrecht | 98:dbeac735109d | 460 | |
jengbrecht | 98:dbeac735109d | 461 | //Check for data mode |
jengbrecht | 98:dbeac735109d | 462 | if(!setCmdMode(false)) { |
jengbrecht | 98:dbeac735109d | 463 | printf("[ERROR] Failed to write data due to mode\r\n"); |
jengbrecht | 98:dbeac735109d | 464 | return -1; |
jengbrecht | 98:dbeac735109d | 465 | } |
jengbrecht | 98:dbeac735109d | 466 | |
jengbrecht | 98:dbeac735109d | 467 | int bytesWritten = 0; |
jengbrecht | 103:da58d27c15d7 | 468 | |
jengbrecht | 98:dbeac735109d | 469 | if(timeout >= 0) { |
jengbrecht | 100:9d96b4391151 | 470 | bytesWritten = io->write(data, length, static_cast<unsigned int>(timeout)); |
jengbrecht | 98:dbeac735109d | 471 | } else { |
jengbrecht | 100:9d96b4391151 | 472 | bytesWritten = io->write(data, length); |
jengbrecht | 98:dbeac735109d | 473 | } |
jengbrecht | 103:da58d27c15d7 | 474 | |
jengbrecht | 98:dbeac735109d | 475 | return bytesWritten; |
jengbrecht | 69:f3e696bbb0d5 | 476 | } |
jengbrecht | 69:f3e696bbb0d5 | 477 | |
jengbrecht | 69:f3e696bbb0d5 | 478 | unsigned int Wifi::readable() |
jengbrecht | 69:f3e696bbb0d5 | 479 | { |
jengbrecht | 69:f3e696bbb0d5 | 480 | if(io == NULL) { |
jengbrecht | 69:f3e696bbb0d5 | 481 | printf("[ERROR] MTSBufferedIO not set\r\n"); |
jengbrecht | 69:f3e696bbb0d5 | 482 | return 0; |
jengbrecht | 69:f3e696bbb0d5 | 483 | } |
jengbrecht | 69:f3e696bbb0d5 | 484 | if(!socketOpened) { |
jengbrecht | 69:f3e696bbb0d5 | 485 | printf("[ERROR] Socket is not open\r\n"); |
jengbrecht | 69:f3e696bbb0d5 | 486 | return 0; |
jengbrecht | 69:f3e696bbb0d5 | 487 | } |
jengbrecht | 69:f3e696bbb0d5 | 488 | return io->readable(); |
jengbrecht | 69:f3e696bbb0d5 | 489 | } |
jengbrecht | 69:f3e696bbb0d5 | 490 | |
jengbrecht | 69:f3e696bbb0d5 | 491 | unsigned int Wifi::writeable() |
jengbrecht | 69:f3e696bbb0d5 | 492 | { |
jengbrecht | 69:f3e696bbb0d5 | 493 | if(io == NULL) { |
jengbrecht | 69:f3e696bbb0d5 | 494 | printf("[ERROR] MTSBufferedIO not set\r\n"); |
jengbrecht | 69:f3e696bbb0d5 | 495 | return 0; |
jengbrecht | 69:f3e696bbb0d5 | 496 | } |
jengbrecht | 69:f3e696bbb0d5 | 497 | if(!socketOpened) { |
jengbrecht | 69:f3e696bbb0d5 | 498 | printf("[ERROR] Socket is not open\r\n"); |
jengbrecht | 69:f3e696bbb0d5 | 499 | return 0; |
jengbrecht | 69:f3e696bbb0d5 | 500 | } |
jengbrecht | 69:f3e696bbb0d5 | 501 | |
jengbrecht | 69:f3e696bbb0d5 | 502 | return io->writeable(); |
jengbrecht | 69:f3e696bbb0d5 | 503 | } |
jengbrecht | 69:f3e696bbb0d5 | 504 | |
jengbrecht | 69:f3e696bbb0d5 | 505 | void Wifi::reset() |
jengbrecht | 69:f3e696bbb0d5 | 506 | { |
sam_grove | 131:da8f0e1c264a | 507 | if(!sortInterfaceMode()) { |
sam_grove | 131:da8f0e1c264a | 508 | return; |
sam_grove | 131:da8f0e1c264a | 509 | } |
jengbrecht | 138:a233b9775f93 | 510 | |
sam_grove | 131:da8f0e1c264a | 511 | sendCommand("factory RESET", 2000, "Set Factory Default"); // <ver> comes out about 1 sec later |
sam_grove | 131:da8f0e1c264a | 512 | wait(0.5f); |
sam_grove | 131:da8f0e1c264a | 513 | sendCommand("reboot", 2000, "*READY*"); |
jengbrecht | 138:a233b9775f93 | 514 | |
jengbrecht | 121:5a7fba896c98 | 515 | wifiConnected = false; |
jengbrecht | 121:5a7fba896c98 | 516 | _ssid = ""; |
jengbrecht | 121:5a7fba896c98 | 517 | mode = TCP; |
jengbrecht | 121:5a7fba896c98 | 518 | socketOpened = false; |
jengbrecht | 121:5a7fba896c98 | 519 | socketCloseable = true; |
jengbrecht | 121:5a7fba896c98 | 520 | local_port = 0; |
jengbrecht | 121:5a7fba896c98 | 521 | local_address = ""; |
jengbrecht | 121:5a7fba896c98 | 522 | host_port = 0; |
jengbrecht | 121:5a7fba896c98 | 523 | cmdOn = false; |
sam_grove | 131:da8f0e1c264a | 524 | wait(1); |
sam_grove | 131:da8f0e1c264a | 525 | // if(!init(io)) { |
sam_grove | 131:da8f0e1c264a | 526 | // printf("[ERROR] Failed to reinitialize after reset.\n\r"); |
sam_grove | 131:da8f0e1c264a | 527 | // } |
jengbrecht | 69:f3e696bbb0d5 | 528 | } |
jengbrecht | 69:f3e696bbb0d5 | 529 | |
jengbrecht | 106:358972176b89 | 530 | Code Wifi::setDeviceIP(std::string address) |
jengbrecht | 106:358972176b89 | 531 | { |
jengbrecht | 108:554585370b4a | 532 | //Check for command mode |
jengbrecht | 108:554585370b4a | 533 | if(!setCmdMode(true)) { |
jengbrecht | 108:554585370b4a | 534 | printf("[ERROR] Failed to set IP due to mode issue\r\n"); |
mfiore | 148:df9feef182b4 | 535 | return MTS_FAILURE; |
jengbrecht | 108:554585370b4a | 536 | } |
jengbrecht | 113:7238f9b8db17 | 537 | |
jengbrecht | 106:358972176b89 | 538 | //Set to DHCP mode |
jengbrecht | 106:358972176b89 | 539 | if(address.compare("DHCP") == 0) { |
jengbrecht | 106:358972176b89 | 540 | return sendBasicCommand("set ip dhcp 1", 1000); |
jengbrecht | 106:358972176b89 | 541 | } |
jengbrecht | 106:358972176b89 | 542 | |
jengbrecht | 106:358972176b89 | 543 | //Set to static mode and set address |
jengbrecht | 106:358972176b89 | 544 | Code code = sendBasicCommand("set ip address " + address, 1000); |
mfiore | 148:df9feef182b4 | 545 | if(code != MTS_SUCCESS) { |
jengbrecht | 106:358972176b89 | 546 | return code; |
jengbrecht | 106:358972176b89 | 547 | } |
jengbrecht | 106:358972176b89 | 548 | code = sendBasicCommand("set ip dhcp 0", 1000); |
mfiore | 148:df9feef182b4 | 549 | if(code != MTS_SUCCESS) { |
jengbrecht | 106:358972176b89 | 550 | return code; |
jengbrecht | 106:358972176b89 | 551 | } |
jengbrecht | 106:358972176b89 | 552 | local_address = address; |
mfiore | 148:df9feef182b4 | 553 | return MTS_SUCCESS; |
jengbrecht | 106:358972176b89 | 554 | } |
jengbrecht | 106:358972176b89 | 555 | |
jengbrecht | 106:358972176b89 | 556 | std::string Wifi::getDeviceIP() |
jengbrecht | 106:358972176b89 | 557 | { |
jengbrecht | 106:358972176b89 | 558 | return local_address; |
jengbrecht | 106:358972176b89 | 559 | } |
jengbrecht | 106:358972176b89 | 560 | |
jengbrecht | 103:da58d27c15d7 | 561 | Code Wifi::setNetwork(const std::string& ssid, SecurityType type, const std::string& key) |
jengbrecht | 69:f3e696bbb0d5 | 562 | { |
jengbrecht | 98:dbeac735109d | 563 | //Check the command mode |
jengbrecht | 98:dbeac735109d | 564 | if(!setCmdMode(true)) { |
mfiore | 148:df9feef182b4 | 565 | return MTS_FAILURE; |
jengbrecht | 98:dbeac735109d | 566 | } |
jengbrecht | 98:dbeac735109d | 567 | |
sgodinez | 73:bb5bbca971ae | 568 | Code code; |
jengbrecht | 69:f3e696bbb0d5 | 569 | |
jengbrecht | 69:f3e696bbb0d5 | 570 | //Set the appropraite SSID |
jengbrecht | 69:f3e696bbb0d5 | 571 | code = sendBasicCommand("set wlan ssid " + ssid, 1000); |
mfiore | 148:df9feef182b4 | 572 | if (code != MTS_SUCCESS) { |
jengbrecht | 69:f3e696bbb0d5 | 573 | return code; |
jengbrecht | 69:f3e696bbb0d5 | 574 | } |
jengbrecht | 69:f3e696bbb0d5 | 575 | |
jengbrecht | 69:f3e696bbb0d5 | 576 | //Set the security key |
jengbrecht | 69:f3e696bbb0d5 | 577 | if (type == WEP64 || type == WEP128) { |
jengbrecht | 69:f3e696bbb0d5 | 578 | //Set the WEP key if using WEP encryption |
jengbrecht | 69:f3e696bbb0d5 | 579 | code = sendBasicCommand("set wlan key " + key, 1000); |
mfiore | 148:df9feef182b4 | 580 | if (code != MTS_SUCCESS) { |
jengbrecht | 69:f3e696bbb0d5 | 581 | return code; |
jengbrecht | 69:f3e696bbb0d5 | 582 | } |
jengbrecht | 69:f3e696bbb0d5 | 583 | } else if (type == WPA || type == WPA2) { |
jengbrecht | 69:f3e696bbb0d5 | 584 | //Set the WPA key if using WPA encryption |
jengbrecht | 69:f3e696bbb0d5 | 585 | code = sendBasicCommand("set wlan phrase " + key, 1000); |
mfiore | 148:df9feef182b4 | 586 | if (code != MTS_SUCCESS) { |
jengbrecht | 69:f3e696bbb0d5 | 587 | return code; |
jengbrecht | 69:f3e696bbb0d5 | 588 | } |
jengbrecht | 69:f3e696bbb0d5 | 589 | } |
jengbrecht | 69:f3e696bbb0d5 | 590 | |
jengbrecht | 69:f3e696bbb0d5 | 591 | _ssid = ssid; |
mfiore | 148:df9feef182b4 | 592 | return MTS_SUCCESS; |
jengbrecht | 69:f3e696bbb0d5 | 593 | } |
jengbrecht | 69:f3e696bbb0d5 | 594 | |
jengbrecht | 94:1baa587e89ae | 595 | Code Wifi::setDNS(const std::string& dnsName) |
jengbrecht | 94:1baa587e89ae | 596 | { |
jengbrecht | 98:dbeac735109d | 597 | //Check the command mode |
jengbrecht | 98:dbeac735109d | 598 | if(!setCmdMode(true)) { |
mfiore | 148:df9feef182b4 | 599 | return MTS_FAILURE; |
jengbrecht | 98:dbeac735109d | 600 | } |
jengbrecht | 98:dbeac735109d | 601 | |
jengbrecht | 94:1baa587e89ae | 602 | return sendBasicCommand("set dns name " + dnsName, 1000); |
jengbrecht | 94:1baa587e89ae | 603 | } |
jengbrecht | 94:1baa587e89ae | 604 | |
jengbrecht | 69:f3e696bbb0d5 | 605 | int Wifi::getSignalStrength() |
jengbrecht | 69:f3e696bbb0d5 | 606 | { |
jengbrecht | 103:da58d27c15d7 | 607 | //Signal strength does not report correctly if not connected |
jengbrecht | 103:da58d27c15d7 | 608 | if(!wifiConnected) { |
jengbrecht | 103:da58d27c15d7 | 609 | printf("[ERROR] Could not get RSSI, Wifi network not connected.\n\r"); |
jengbrecht | 103:da58d27c15d7 | 610 | return 99; |
jengbrecht | 103:da58d27c15d7 | 611 | } |
jengbrecht | 103:da58d27c15d7 | 612 | |
jengbrecht | 98:dbeac735109d | 613 | //Check the command mode |
jengbrecht | 98:dbeac735109d | 614 | if(!setCmdMode(true)) { |
jengbrecht | 98:dbeac735109d | 615 | printf("[ERROR] Could not get RSSI\n\r"); |
jengbrecht | 103:da58d27c15d7 | 616 | return 99; |
jengbrecht | 98:dbeac735109d | 617 | } |
jengbrecht | 98:dbeac735109d | 618 | |
jengbrecht | 93:aa7a48e65974 | 619 | string response = sendCommand("show rssi", 2000, "dBm"); |
jengbrecht | 74:9f87bd22c222 | 620 | if (response.find("RSSI") == string::npos) { |
jengbrecht | 98:dbeac735109d | 621 | printf("[ERROR] Could not get RSSI\n\r"); |
jengbrecht | 103:da58d27c15d7 | 622 | return 99; |
jengbrecht | 74:9f87bd22c222 | 623 | } |
jengbrecht | 74:9f87bd22c222 | 624 | int start = response.find('('); |
jengbrecht | 74:9f87bd22c222 | 625 | int stop = response.find(')', start); |
jengbrecht | 74:9f87bd22c222 | 626 | string signal = response.substr(start + 1, stop - start - 1); |
jengbrecht | 74:9f87bd22c222 | 627 | int value; |
jengbrecht | 74:9f87bd22c222 | 628 | sscanf(signal.c_str(), "%d", &value); |
jengbrecht | 74:9f87bd22c222 | 629 | return value; |
jengbrecht | 69:f3e696bbb0d5 | 630 | } |
jengbrecht | 69:f3e696bbb0d5 | 631 | |
jengbrecht | 95:4fdf968b5b37 | 632 | bool Wifi::ping(const std::string& address) |
jengbrecht | 95:4fdf968b5b37 | 633 | { |
jengbrecht | 98:dbeac735109d | 634 | //Check the command mode |
jengbrecht | 98:dbeac735109d | 635 | if(!setCmdMode(true)) { |
jengbrecht | 98:dbeac735109d | 636 | printf("[ERROR] Could not send ping command\n\r"); |
jengbrecht | 98:dbeac735109d | 637 | return false; |
jengbrecht | 98:dbeac735109d | 638 | } |
jengbrecht | 98:dbeac735109d | 639 | |
jengbrecht | 95:4fdf968b5b37 | 640 | std::string response; |
jengbrecht | 95:4fdf968b5b37 | 641 | for (int i = 0; i < PINGNUM; i++) { |
jengbrecht | 95:4fdf968b5b37 | 642 | response = sendCommand("ping " + address, PINGDELAY * 1000, "reply"); |
jengbrecht | 95:4fdf968b5b37 | 643 | if (response.find("reply") != std::string::npos) { |
jengbrecht | 95:4fdf968b5b37 | 644 | return true; |
jengbrecht | 95:4fdf968b5b37 | 645 | } |
jengbrecht | 95:4fdf968b5b37 | 646 | } |
jengbrecht | 95:4fdf968b5b37 | 647 | return false; |
jengbrecht | 95:4fdf968b5b37 | 648 | } |
jengbrecht | 95:4fdf968b5b37 | 649 | |
jengbrecht | 79:f356009dbc12 | 650 | bool Wifi::setCmdMode(bool on) |
jengbrecht | 74:9f87bd22c222 | 651 | { |
jengbrecht | 79:f356009dbc12 | 652 | if (on) { |
jengbrecht | 79:f356009dbc12 | 653 | if (cmdOn) { |
jengbrecht | 79:f356009dbc12 | 654 | return true; |
jengbrecht | 79:f356009dbc12 | 655 | } |
jengbrecht | 79:f356009dbc12 | 656 | wait(.5); |
jengbrecht | 93:aa7a48e65974 | 657 | std::string response = sendCommand("$$", 2000, "CMD", '$'); |
jengbrecht | 79:f356009dbc12 | 658 | if (response.find("CMD") != string::npos) { |
jengbrecht | 79:f356009dbc12 | 659 | cmdOn = true; |
jengbrecht | 113:7238f9b8db17 | 660 | wait(.5); |
jengbrecht | 79:f356009dbc12 | 661 | return true; |
jengbrecht | 79:f356009dbc12 | 662 | } |
jengbrecht | 98:dbeac735109d | 663 | printf("[ERROR] Failed to enter command mode\n\r"); |
jengbrecht | 79:f356009dbc12 | 664 | return false; |
jengbrecht | 79:f356009dbc12 | 665 | } else { |
jengbrecht | 79:f356009dbc12 | 666 | if (!cmdOn) { |
jengbrecht | 79:f356009dbc12 | 667 | return true; |
jengbrecht | 79:f356009dbc12 | 668 | } |
jengbrecht | 93:aa7a48e65974 | 669 | std::string response = sendCommand("exit", 2000, "EXIT"); |
jengbrecht | 79:f356009dbc12 | 670 | if (response.find("EXIT") != string::npos) { |
jengbrecht | 79:f356009dbc12 | 671 | cmdOn = false; |
jengbrecht | 79:f356009dbc12 | 672 | return true; |
jengbrecht | 79:f356009dbc12 | 673 | } |
jengbrecht | 98:dbeac735109d | 674 | printf("[ERROR] Failed to exit command mode\n\r"); |
jengbrecht | 79:f356009dbc12 | 675 | return false; |
jengbrecht | 74:9f87bd22c222 | 676 | } |
jengbrecht | 74:9f87bd22c222 | 677 | } |
jengbrecht | 69:f3e696bbb0d5 | 678 | |
jengbrecht | 138:a233b9775f93 | 679 | std::string Wifi::getHostByName(std::string url) |
jengbrecht | 138:a233b9775f93 | 680 | { |
jengbrecht | 138:a233b9775f93 | 681 | std::string response = sendCommand("lookup " + url, 3000, "<4.00>"); |
jengbrecht | 138:a233b9775f93 | 682 | int start = response.find("="); |
jengbrecht | 138:a233b9775f93 | 683 | int stop = response.find("\r"); |
jengbrecht | 138:a233b9775f93 | 684 | if(start == string::npos || stop == string::npos) { |
jengbrecht | 138:a233b9775f93 | 685 | printf("[ERROR] Failed to resolve URL [%s]", response.c_str()); |
jengbrecht | 138:a233b9775f93 | 686 | return ""; |
jengbrecht | 138:a233b9775f93 | 687 | } |
jengbrecht | 138:a233b9775f93 | 688 | std::string ip = response.substr(start + 1, stop - start - 1); |
jengbrecht | 138:a233b9775f93 | 689 | //printf("Data: %s\n\r", ip); |
jengbrecht | 138:a233b9775f93 | 690 | return ip; |
jengbrecht | 138:a233b9775f93 | 691 | } |
jengbrecht | 138:a233b9775f93 | 692 | |
sgodinez | 73:bb5bbca971ae | 693 | Code Wifi::sendBasicCommand(string command, int timeoutMillis, char esc) |
jengbrecht | 69:f3e696bbb0d5 | 694 | { |
jengbrecht | 69:f3e696bbb0d5 | 695 | if(socketOpened) { |
jengbrecht | 69:f3e696bbb0d5 | 696 | printf("[ERROR] socket is open. Can not send AT commands\r\n"); |
mfiore | 148:df9feef182b4 | 697 | return MTS_ERROR; |
jengbrecht | 69:f3e696bbb0d5 | 698 | } |
jengbrecht | 69:f3e696bbb0d5 | 699 | |
jengbrecht | 93:aa7a48e65974 | 700 | string response = sendCommand(command, timeoutMillis, "AOK", esc); |
jengbrecht | 69:f3e696bbb0d5 | 701 | //printf("Response: %s\n\r", response.c_str()); |
jengbrecht | 69:f3e696bbb0d5 | 702 | if (response.size() == 0) { |
mfiore | 148:df9feef182b4 | 703 | return MTS_NO_RESPONSE; |
jengbrecht | 69:f3e696bbb0d5 | 704 | } else if (response.find("AOK") != string::npos) { |
mfiore | 148:df9feef182b4 | 705 | return MTS_SUCCESS; |
jengbrecht | 69:f3e696bbb0d5 | 706 | } else if (response.find("ERR") != string::npos) { |
mfiore | 148:df9feef182b4 | 707 | return MTS_ERROR; |
jengbrecht | 69:f3e696bbb0d5 | 708 | } else { |
mfiore | 148:df9feef182b4 | 709 | return MTS_FAILURE; |
jengbrecht | 69:f3e696bbb0d5 | 710 | } |
jengbrecht | 69:f3e696bbb0d5 | 711 | } |
jengbrecht | 69:f3e696bbb0d5 | 712 | |
jengbrecht | 93:aa7a48e65974 | 713 | string Wifi::sendCommand(string command, int timeoutMillis, std::string response, char esc) |
jengbrecht | 69:f3e696bbb0d5 | 714 | { |
jengbrecht | 69:f3e696bbb0d5 | 715 | if(io == NULL) { |
jengbrecht | 69:f3e696bbb0d5 | 716 | printf("[ERROR] MTSBufferedIO not set\r\n"); |
jengbrecht | 69:f3e696bbb0d5 | 717 | return ""; |
jengbrecht | 69:f3e696bbb0d5 | 718 | } |
jengbrecht | 106:358972176b89 | 719 | //if(socketOpened && command.compare("$$") != 0 && command.compare("exit") != 0 && command.compare("close") != 0) { |
jengbrecht | 106:358972176b89 | 720 | // printf("[ERROR] socket is open. Can not send AT commands\r\n"); |
jengbrecht | 106:358972176b89 | 721 | // return ""; |
jengbrecht | 106:358972176b89 | 722 | //} |
jengbrecht | 69:f3e696bbb0d5 | 723 | |
jengbrecht | 69:f3e696bbb0d5 | 724 | io->rxClear(); |
jengbrecht | 69:f3e696bbb0d5 | 725 | io->txClear(); |
jengbrecht | 69:f3e696bbb0d5 | 726 | std::string result; |
jengbrecht | 79:f356009dbc12 | 727 | |
sgodinez | 73:bb5bbca971ae | 728 | //Attempt to write command |
sgodinez | 73:bb5bbca971ae | 729 | if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) { |
sgodinez | 73:bb5bbca971ae | 730 | //Failed to write command |
sgodinez | 73:bb5bbca971ae | 731 | printf("[ERROR] failed to send command to radio within %d milliseconds\r\n", timeoutMillis); |
sgodinez | 73:bb5bbca971ae | 732 | return ""; |
sgodinez | 73:bb5bbca971ae | 733 | } |
sgodinez | 73:bb5bbca971ae | 734 | //Send Escape Character |
sgodinez | 73:bb5bbca971ae | 735 | if (esc != 0x00) { |
sgodinez | 73:bb5bbca971ae | 736 | if(io->write(esc, timeoutMillis) != 1) { |
sgodinez | 73:bb5bbca971ae | 737 | printf("[ERROR] failed to send '%c' to radio within %d milliseconds\r\n", esc, timeoutMillis); |
sgodinez | 73:bb5bbca971ae | 738 | return ""; |
sgodinez | 73:bb5bbca971ae | 739 | } |
sgodinez | 73:bb5bbca971ae | 740 | } |
sam_grove | 131:da8f0e1c264a | 741 | DBG("Sending: %s%c", command.data(), esc); |
jengbrecht | 138:a233b9775f93 | 742 | |
jengbrecht | 69:f3e696bbb0d5 | 743 | int timer = 0; |
sgodinez | 73:bb5bbca971ae | 744 | size_t previous = 0; |
jengbrecht | 69:f3e696bbb0d5 | 745 | char tmp[256]; |
jengbrecht | 69:f3e696bbb0d5 | 746 | tmp[255] = 0; |
jengbrecht | 69:f3e696bbb0d5 | 747 | bool done = false; |
jengbrecht | 69:f3e696bbb0d5 | 748 | do { |
jengbrecht | 93:aa7a48e65974 | 749 | wait(.2); |
jengbrecht | 93:aa7a48e65974 | 750 | timer = timer + 200; |
sgodinez | 73:bb5bbca971ae | 751 | previous = result.size(); |
jengbrecht | 93:aa7a48e65974 | 752 | int size = io->read(tmp, 255, 0); //1 less than allocated |
jengbrecht | 69:f3e696bbb0d5 | 753 | if(size > 0) { |
jengbrecht | 69:f3e696bbb0d5 | 754 | result.append(tmp, size); |
jengbrecht | 93:aa7a48e65974 | 755 | if (response.size() != 0) { |
jengbrecht | 93:aa7a48e65974 | 756 | if (result.find(response) != string::npos) { |
sam_grove | 131:da8f0e1c264a | 757 | goto exit_func; |
sam_grove | 131:da8f0e1c264a | 758 | //return result; |
jengbrecht | 93:aa7a48e65974 | 759 | } |
jengbrecht | 93:aa7a48e65974 | 760 | } else { |
jengbrecht | 93:aa7a48e65974 | 761 | done = (result.size() == previous); |
jengbrecht | 69:f3e696bbb0d5 | 762 | } |
jengbrecht | 69:f3e696bbb0d5 | 763 | } |
jengbrecht | 69:f3e696bbb0d5 | 764 | if(timer >= timeoutMillis) { |
jengbrecht | 127:526003734633 | 765 | if(!(command.compare("reboot") == 0 || command.compare("") == 0)) { |
jengbrecht | 121:5a7fba896c98 | 766 | printf("[WARNING] sendCommand [%s] timed out after %d milliseconds\r\n", command.c_str(), timeoutMillis); |
jengbrecht | 121:5a7fba896c98 | 767 | } |
jengbrecht | 69:f3e696bbb0d5 | 768 | done = true; |
jengbrecht | 69:f3e696bbb0d5 | 769 | } |
jengbrecht | 69:f3e696bbb0d5 | 770 | } while (!done); |
jengbrecht | 79:f356009dbc12 | 771 | |
sam_grove | 131:da8f0e1c264a | 772 | exit_func: |
sam_grove | 131:da8f0e1c264a | 773 | DBG("Result: %s\n\r", result.c_str()); |
jengbrecht | 69:f3e696bbb0d5 | 774 | return result; |
jengbrecht | 69:f3e696bbb0d5 | 775 | } |
kranjan | 141:571e0ef6c8dc | 776 |