|
| 1 | +/* |
| 2 | + This file is part of ArduinoIoTCloud. |
| 3 | +
|
| 4 | + Copyright 2019 ARDUINO SA (http://www.arduino.cc/) |
| 5 | +
|
| 6 | + This software is released under the GNU General Public License version 3, |
| 7 | + which covers the main part of arduino-cli. |
| 8 | + The terms of this license can be found at: |
| 9 | + https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | +
|
| 11 | + You can be released from the requirements of the above licenses by purchasing |
| 12 | + a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | + otherwise use the software for commercial activities involving the Arduino |
| 14 | + software without disclosing the source code of your own applications. To purchase |
| 15 | + a commercial license, send an email to [email protected]. |
| 16 | +*/ |
| 17 | + |
| 18 | +/****************************************************************************** |
| 19 | + INCLUDE |
| 20 | + ******************************************************************************/ |
| 21 | + |
| 22 | +#include "Arduino_WiFiConnectionHandler.h" |
| 23 | + |
| 24 | +#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ |
| 25 | + |
| 26 | +/****************************************************************************** |
| 27 | + CONSTANTS |
| 28 | + ******************************************************************************/ |
| 29 | + |
| 30 | +static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED */ |
| 31 | + |
| 32 | +/****************************************************************************** |
| 33 | + CTOR/DTOR |
| 34 | + ******************************************************************************/ |
| 35 | + |
| 36 | +Arduino_WiFiConnectionHandler::Arduino_WiFiConnectionHandler(const char *_ssid, const char *_pass, bool _keepAlive) : |
| 37 | + ssid(_ssid), |
| 38 | + pass(_pass), |
| 39 | + lastConnectionTickTime(millis()), |
| 40 | + connectionTickTimeInterval(CHECK_INTERVAL_IDLE), |
| 41 | + keepAlive(_keepAlive), |
| 42 | + _on_connect_event_callback(NULL), |
| 43 | + _on_disconnect_event_callback(NULL), |
| 44 | + _on_error_event_callback(NULL){ |
| 45 | +} |
| 46 | + |
| 47 | +/****************************************************************************** |
| 48 | + PUBLIC MEMBER FUNCTIONS |
| 49 | + ******************************************************************************/ |
| 50 | + |
| 51 | +void Arduino_WiFiConnectionHandler::init() { |
| 52 | +} |
| 53 | + |
| 54 | +void Arduino_WiFiConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback){ |
| 55 | + switch (event) { |
| 56 | + case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break; |
| 57 | + case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break; |
| 58 | + case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void Arduino_WiFiConnectionHandler::execNetworkEventCallback(OnNetworkEventCallback & callback, void * callback_arg){ |
| 63 | + if(callback){ |
| 64 | + (*callback)(callback_arg); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +unsigned long Arduino_WiFiConnectionHandler::getTime() { |
| 69 | +#ifdef GETTIME_MISSING |
| 70 | + return 0; |
| 71 | +#else |
| 72 | + return WiFi.getTime(); |
| 73 | +#endif |
| 74 | +} |
| 75 | + |
| 76 | +void Arduino_WiFiConnectionHandler::update() { |
| 77 | + |
| 78 | + unsigned long const now = millis(); |
| 79 | + int networkStatus = 0; |
| 80 | + if (now - lastConnectionTickTime > connectionTickTimeInterval) { /* time bracket */ |
| 81 | + |
| 82 | + switch (netConnectionState) { |
| 83 | + case NetworkConnectionState::INIT: { |
| 84 | + debugMessage(DebugLevel::Verbose, "::INIT"); |
| 85 | +#if !defined(ARDUINO_ESP8266_ESP12) && !defined(ARDUINO_ARCH_ESP32) |
| 86 | + networkStatus = WiFi.status(); |
| 87 | + |
| 88 | + debugMessage(DebugLevel::Info, "WiFi.status(): %d", networkStatus); |
| 89 | + if (networkStatus == NETWORK_HARDWARE_ERROR) { |
| 90 | + // NO FURTHER ACTION WILL FOLLOW THIS |
| 91 | + changeConnectionState(NetworkConnectionState::ERROR); |
| 92 | + lastConnectionTickTime = now; |
| 93 | + return; |
| 94 | + } |
| 95 | + debugMessage(DebugLevel::Error, "Current WiFi Firmware: %s", WiFi.firmwareVersion()); |
| 96 | + if (WiFi.firmwareVersion() < WIFI_FIRMWARE_VERSION_REQUIRED) { |
| 97 | + debugMessage(DebugLevel::Error, "Latest WiFi Firmware: %s", WIFI_FIRMWARE_VERSION_REQUIRED); |
| 98 | + debugMessage(DebugLevel::Error, "Please update to the latest version for best performance."); |
| 99 | + delay(5000); |
| 100 | + } |
| 101 | +#else |
| 102 | + debugMessage(DebugLevel::Error, "WiFi status ESP: %d", WiFi.status()); |
| 103 | + WiFi.disconnect(); |
| 104 | + networkStatus = WiFi.begin(ssid, pass); |
| 105 | +#endif |
| 106 | + |
| 107 | + changeConnectionState(NetworkConnectionState::CONNECTING); |
| 108 | + } |
| 109 | + break; |
| 110 | + case NetworkConnectionState::CONNECTING: { |
| 111 | + debugMessage(DebugLevel::Verbose, "::CONNECTING"); |
| 112 | + networkStatus = WiFi.status(); |
| 113 | + |
| 114 | +#if !defined(ARDUINO_ESP8266_ESP12) && !defined(ARDUINO_ARCH_ESP32) |
| 115 | + |
| 116 | + if (networkStatus != WL_CONNECTED) { |
| 117 | + networkStatus = WiFi.begin(ssid, pass); |
| 118 | + } |
| 119 | + |
| 120 | +#else |
| 121 | + |
| 122 | + networkStatus = WiFi.status(); |
| 123 | + |
| 124 | +#endif |
| 125 | + |
| 126 | + debugMessage(DebugLevel::Verbose, "WiFi.status(): %d", networkStatus); |
| 127 | + if (networkStatus != NETWORK_CONNECTED) { |
| 128 | + debugMessage(DebugLevel::Error, "Connection to \"%s\" failed", ssid); |
| 129 | + debugMessage(DebugLevel::Info, "Retrying in \"%d\" milliseconds", connectionTickTimeInterval); |
| 130 | + |
| 131 | + return; |
| 132 | + } else { |
| 133 | + debugMessage(DebugLevel::Info, "Connected to \"%s\"", ssid); |
| 134 | + changeConnectionState(NetworkConnectionState::CONNECTED); |
| 135 | + return; |
| 136 | + } |
| 137 | + } |
| 138 | + break; |
| 139 | + case NetworkConnectionState::CONNECTED: { |
| 140 | + |
| 141 | + networkStatus = WiFi.status(); |
| 142 | + debugMessage(DebugLevel::Verbose, "WiFi.status(): %d", networkStatus); |
| 143 | + if (networkStatus != WL_CONNECTED) { |
| 144 | + changeConnectionState(NetworkConnectionState::DISCONNECTED); |
| 145 | + return; |
| 146 | + } |
| 147 | + debugMessage(DebugLevel::Verbose, "Connected to \"%s\"", ssid); |
| 148 | + } |
| 149 | + break; |
| 150 | + case NetworkConnectionState::GETTIME: { |
| 151 | + |
| 152 | + } |
| 153 | + break; |
| 154 | + case NetworkConnectionState::DISCONNECTING: { |
| 155 | + if (networkStatus != WL_CONNECTED) { |
| 156 | + changeConnectionState(NetworkConnectionState::DISCONNECTED); |
| 157 | + } |
| 158 | + } |
| 159 | + break; |
| 160 | + case NetworkConnectionState::DISCONNECTED: { |
| 161 | +#if !defined(ARDUINO_ESP8266_ESP12) && !defined(ARDUINO_ARCH_ESP32) |
| 162 | + WiFi.end(); |
| 163 | +#endif |
| 164 | + if (keepAlive) { |
| 165 | + changeConnectionState(NetworkConnectionState::INIT); |
| 166 | + }else{ |
| 167 | + changeConnectionState(NetworkConnectionState::CLOSED); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + break; |
| 172 | + case NetworkConnectionState::ERROR: { |
| 173 | + |
| 174 | + } |
| 175 | + break; |
| 176 | + case NetworkConnectionState::CLOSED: { |
| 177 | + |
| 178 | + } |
| 179 | + break; |
| 180 | + } |
| 181 | + lastConnectionTickTime = now; |
| 182 | + |
| 183 | + } /* time bracket */ |
| 184 | +} |
| 185 | + |
| 186 | +/****************************************************************************** |
| 187 | + PRIVATE MEMBER FUNCTIONS |
| 188 | + ******************************************************************************/ |
| 189 | + |
| 190 | +void Arduino_WiFiConnectionHandler::changeConnectionState(NetworkConnectionState _newState) { |
| 191 | + if(_newState == netConnectionState) return; |
| 192 | + int newInterval = CHECK_INTERVAL_INIT; |
| 193 | + switch (_newState) { |
| 194 | + case NetworkConnectionState::INIT: { |
| 195 | + debugMessage(DebugLevel::Verbose, "CHANGING STATE TO ::INIT"); |
| 196 | + newInterval = CHECK_INTERVAL_INIT; |
| 197 | + } |
| 198 | + break; |
| 199 | + case NetworkConnectionState::CONNECTING: { |
| 200 | + debugMessage(DebugLevel::Info, "Connecting to \"%s\"", ssid); |
| 201 | + newInterval = CHECK_INTERVAL_CONNECTING; |
| 202 | + } |
| 203 | + break; |
| 204 | + case NetworkConnectionState::CONNECTED: { |
| 205 | + execNetworkEventCallback(_on_connect_event_callback,0); |
| 206 | + newInterval = CHECK_INTERVAL_CONNECTED; |
| 207 | + } |
| 208 | + break; |
| 209 | + case NetworkConnectionState::GETTIME: { |
| 210 | + } |
| 211 | + break; |
| 212 | + case NetworkConnectionState::DISCONNECTING: { |
| 213 | + debugMessage(DebugLevel::Verbose, "Disconnecting from \"%s\"", ssid); |
| 214 | + WiFi.disconnect(); |
| 215 | + } |
| 216 | + break; |
| 217 | + case NetworkConnectionState::DISCONNECTED: { |
| 218 | + execNetworkEventCallback(_on_disconnect_event_callback,0); |
| 219 | + debugMessage(DebugLevel::Verbose, "WiFi.status(): %d", WiFi.status()); |
| 220 | + |
| 221 | + debugMessage(DebugLevel::Error, "Connection to \"%s\" lost.", ssid); |
| 222 | + debugMessage(DebugLevel::Error, "Attempting reconnection"); |
| 223 | + newInterval = CHECK_INTERVAL_DISCONNECTED; |
| 224 | + } |
| 225 | + break; |
| 226 | + case NetworkConnectionState::CLOSED: { |
| 227 | + |
| 228 | + #if !defined(ARDUINO_ESP8266_ESP12) && !defined(ARDUINO_ARCH_ESP32) |
| 229 | + WiFi.end(); |
| 230 | + #endif |
| 231 | + |
| 232 | + debugMessage(DebugLevel::Verbose, "Connection to \"%s\" closed", ssid); |
| 233 | + } |
| 234 | + break; |
| 235 | + case NetworkConnectionState::ERROR: { |
| 236 | + execNetworkEventCallback(_on_error_event_callback,0); |
| 237 | + debugMessage(DebugLevel::Error, "WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield."); |
| 238 | + debugMessage(DebugLevel::Error, "Then reset and retry."); |
| 239 | + } |
| 240 | + break; |
| 241 | + } |
| 242 | + connectionTickTimeInterval = newInterval; |
| 243 | + lastConnectionTickTime = millis(); |
| 244 | + netConnectionState = _newState; |
| 245 | + connectionStateChanged(netConnectionState); |
| 246 | +} |
| 247 | + |
| 248 | +void Arduino_WiFiConnectionHandler::connect() { |
| 249 | + if(netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING){ |
| 250 | + return; |
| 251 | + } |
| 252 | + keepAlive = true; |
| 253 | + changeConnectionState(NetworkConnectionState::INIT); |
| 254 | + |
| 255 | +} |
| 256 | +void Arduino_WiFiConnectionHandler::disconnect() { |
| 257 | + //WiFi.end(); |
| 258 | + |
| 259 | + changeConnectionState(NetworkConnectionState::DISCONNECTING); |
| 260 | + keepAlive = false; |
| 261 | +} |
| 262 | +#endif /* #ifdef BOARD_HAS_WIFI */ |
0 commit comments