|
| 1 | +/* |
| 2 | + This file is part of ArduinoIoTCloud. |
| 3 | + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | + This software is released under the GNU General Public License version 3, |
| 5 | + which covers the main part of arduino-cli. |
| 6 | + The terms of this license can be found at: |
| 7 | + https://www.gnu.org/licenses/gpl-3.0.en.html |
| 8 | + You can be released from the requirements of the above licenses by purchasing |
| 9 | + a commercial license. Buying such a license is mandatory if you want to modify or |
| 10 | + otherwise use the software for commercial activities involving the Arduino |
| 11 | + software without disclosing the source code of your own applications. To purchase |
| 12 | + a commercial license, send an email to [email protected]. |
| 13 | +*/ |
| 14 | + |
| 15 | +/****************************************************************************** |
| 16 | + INCLUDE |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | +#include "GenericConnectionHandler.h" |
| 20 | +#include "Arduino_ConnectionHandler.h" |
| 21 | + |
| 22 | +static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter); |
| 23 | + |
| 24 | +bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) { |
| 25 | + |
| 26 | + if(_ch != nullptr && _ch->_current_net_connection_state != NetworkConnectionState::INIT) { |
| 27 | + // If the internal connection handler is already being used and not in INIT phase we cannot update the settings |
| 28 | + return false; |
| 29 | + } else if(_ch != nullptr && _ch->_current_net_connection_state == NetworkConnectionState::INIT && _interface != s.type) { |
| 30 | + // If the internal connection handler is already being used and in INIT phase and the interface type is being changed |
| 31 | + // -> we need to deallocate the previously allocated handler |
| 32 | + |
| 33 | + // if interface type is not being changed -> we just need to call updateSettings |
| 34 | + delete _ch; |
| 35 | + _ch = nullptr; |
| 36 | + } |
| 37 | + |
| 38 | + if(_ch == nullptr) { |
| 39 | + _ch = instantiate_handler(s.type); |
| 40 | + } |
| 41 | + |
| 42 | + if(_ch != nullptr) { |
| 43 | + _interface = s.type; |
| 44 | + _ch->setKeepAlive(_keep_alive); |
| 45 | + return _ch->updateSetting(s); |
| 46 | + } else { |
| 47 | + _interface = NetworkAdapter::NONE; |
| 48 | + |
| 49 | + return false; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +NetworkConnectionState GenericConnectionHandler::update_handleInit() { |
| 54 | + return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::INIT; |
| 55 | +} |
| 56 | + |
| 57 | +NetworkConnectionState GenericConnectionHandler::update_handleConnecting() { |
| 58 | + return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::INIT; |
| 59 | +} |
| 60 | + |
| 61 | +NetworkConnectionState GenericConnectionHandler::update_handleConnected() { |
| 62 | + return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::INIT; |
| 63 | +} |
| 64 | + |
| 65 | +NetworkConnectionState GenericConnectionHandler::update_handleDisconnecting() { |
| 66 | + return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::INIT; |
| 67 | +} |
| 68 | + |
| 69 | +NetworkConnectionState GenericConnectionHandler::update_handleDisconnected() { |
| 70 | + return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::INIT; |
| 71 | +} |
| 72 | + |
| 73 | +#if not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD)) |
| 74 | +unsigned long GenericConnectionHandler::getTime() { |
| 75 | + return _ch != nullptr ? _ch->getTime() : 0; |
| 76 | +} |
| 77 | + |
| 78 | +Client & GenericConnectionHandler::getClient() { |
| 79 | + return _ch->getClient(); // NOTE _ch may be nullptr |
| 80 | +} |
| 81 | + |
| 82 | +UDP & GenericConnectionHandler::getUDP() { |
| 83 | + return _ch->getUDP(); // NOTE _ch may be nullptr |
| 84 | +} |
| 85 | + |
| 86 | +#endif // not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD)) |
| 87 | + |
| 88 | +void GenericConnectionHandler::connect() { |
| 89 | + if(_ch!=nullptr) { |
| 90 | + _ch->connect(); |
| 91 | + } |
| 92 | + ConnectionHandler::connect(); |
| 93 | +} |
| 94 | + |
| 95 | +void GenericConnectionHandler::disconnect() { |
| 96 | + if(_ch!=nullptr) { |
| 97 | + _ch->disconnect(); |
| 98 | + } |
| 99 | + ConnectionHandler::disconnect(); |
| 100 | +} |
| 101 | + |
| 102 | +void GenericConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) { |
| 103 | + if(_ch!=nullptr) { |
| 104 | + _ch->addCallback(event, callback); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void GenericConnectionHandler::setKeepAlive(bool keep_alive) { |
| 109 | + _keep_alive = keep_alive; |
| 110 | + |
| 111 | + if(_ch!=nullptr) { |
| 112 | + _ch->setKeepAlive(keep_alive); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter) { |
| 117 | + switch(adapter) { |
| 118 | + #if defined(BOARD_HAS_WIFI) |
| 119 | + case NetworkAdapter::WIFI: |
| 120 | + return new WiFiConnectionHandler(); |
| 121 | + break; |
| 122 | + #endif |
| 123 | + |
| 124 | + #if defined(BOARD_HAS_ETHERNET) |
| 125 | + case NetworkAdapter::ETHERNET: |
| 126 | + return new EthernetConnectionHandler(); |
| 127 | + break; |
| 128 | + #endif |
| 129 | + |
| 130 | + #if defined(BOARD_HAS_NB) |
| 131 | + case NetworkAdapter::NB: |
| 132 | + return new NBConnectionHandler(); |
| 133 | + break; |
| 134 | + #endif |
| 135 | + |
| 136 | + #if defined(BOARD_HAS_GSM) |
| 137 | + case NetworkAdapter::GSM: |
| 138 | + return new GSMConnectionHandler(); |
| 139 | + break; |
| 140 | + #endif |
| 141 | + |
| 142 | + #if defined(BOARD_HAS_CATM1_NBIOT) |
| 143 | + case NetworkAdapter::CATM1: |
| 144 | + return new CatM1ConnectionHandler(); |
| 145 | + break; |
| 146 | + #endif |
| 147 | + |
| 148 | + #if defined(BOARD_HAS_CELLULAR) |
| 149 | + case NetworkAdapter::CELL: |
| 150 | + return new CellularConnectionHandler(); |
| 151 | + break; |
| 152 | + #endif |
| 153 | + |
| 154 | + default: |
| 155 | + Debug.print(DBG_ERROR, "Network adapter not supported by this platform: %d", adapter); |
| 156 | + return nullptr; |
| 157 | + } |
| 158 | +} |
0 commit comments