|
| 1 | +/* |
| 2 | + This file is part of the Arduino_ConnectionHandler library. |
| 3 | +
|
| 4 | + Copyright (c) 2024 Arduino SA |
| 5 | +
|
| 6 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | +*/ |
| 10 | + |
| 11 | +/****************************************************************************** |
| 12 | + INCLUDE |
| 13 | + ******************************************************************************/ |
| 14 | + |
| 15 | +#include "GenericConnectionHandler.h" |
| 16 | +#include "Arduino_ConnectionHandler.h" |
| 17 | + |
| 18 | +static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter); |
| 19 | + |
| 20 | +bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) { |
| 21 | + if(_ch != nullptr && _ch->_current_net_connection_state != NetworkConnectionState::INIT) { |
| 22 | + // If the internal connection handler is already being used and not in INIT phase we cannot update the settings |
| 23 | + return false; |
| 24 | + } else if(_ch != nullptr && _ch->_current_net_connection_state == NetworkConnectionState::INIT && _interface != s.type) { |
| 25 | + // If the internal connection handler is already being used and in INIT phase and the interface type is being changed |
| 26 | + // -> we need to deallocate the previously allocated handler |
| 27 | + |
| 28 | + // if interface type is not being changed -> we just need to call updateSettings |
| 29 | + delete _ch; |
| 30 | + _ch = nullptr; |
| 31 | + } |
| 32 | + |
| 33 | + if(_ch == nullptr) { |
| 34 | + _ch = instantiate_handler(s.type); |
| 35 | + } |
| 36 | + |
| 37 | + if(_ch != nullptr) { |
| 38 | + _interface = s.type; |
| 39 | + _ch->setKeepAlive(_keep_alive); |
| 40 | + return _ch->updateSetting(s); |
| 41 | + } else { |
| 42 | + _interface = NetworkAdapter::NONE; |
| 43 | + |
| 44 | + return false; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +NetworkConnectionState GenericConnectionHandler::updateConnectionState() { |
| 49 | + return _ch != nullptr ? _ch->updateConnectionState() : NetworkConnectionState::INIT; |
| 50 | +} |
| 51 | + |
| 52 | +NetworkConnectionState GenericConnectionHandler::update_handleInit() { |
| 53 | + return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::INIT; |
| 54 | +} |
| 55 | + |
| 56 | +NetworkConnectionState GenericConnectionHandler::update_handleConnecting() { |
| 57 | + return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::INIT; |
| 58 | +} |
| 59 | + |
| 60 | +NetworkConnectionState GenericConnectionHandler::update_handleConnected() { |
| 61 | + return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::INIT; |
| 62 | +} |
| 63 | + |
| 64 | +NetworkConnectionState GenericConnectionHandler::update_handleDisconnecting() { |
| 65 | + return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::INIT; |
| 66 | +} |
| 67 | + |
| 68 | +NetworkConnectionState GenericConnectionHandler::update_handleDisconnected() { |
| 69 | + return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::INIT; |
| 70 | +} |
| 71 | + |
| 72 | +#if not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD)) |
| 73 | +unsigned long GenericConnectionHandler::getTime() { |
| 74 | + return _ch != nullptr ? _ch->getTime() : 0; |
| 75 | +} |
| 76 | + |
| 77 | +Client & GenericConnectionHandler::getClient() { |
| 78 | + return _ch->getClient(); // NOTE _ch may be nullptr |
| 79 | +} |
| 80 | + |
| 81 | +UDP & GenericConnectionHandler::getUDP() { |
| 82 | + return _ch->getUDP(); // NOTE _ch may be nullptr |
| 83 | +} |
| 84 | + |
| 85 | +#endif // not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD)) |
| 86 | + |
| 87 | +void GenericConnectionHandler::connect() { |
| 88 | + if(_ch!=nullptr) { |
| 89 | + _ch->connect(); |
| 90 | + } |
| 91 | + ConnectionHandler::connect(); |
| 92 | +} |
| 93 | + |
| 94 | +void GenericConnectionHandler::disconnect() { |
| 95 | + if(_ch!=nullptr) { |
| 96 | + _ch->disconnect(); |
| 97 | + } |
| 98 | + ConnectionHandler::disconnect(); |
| 99 | +} |
| 100 | + |
| 101 | +void GenericConnectionHandler::setKeepAlive(bool keep_alive) { |
| 102 | + _keep_alive = keep_alive; |
| 103 | + |
| 104 | + if(_ch!=nullptr) { |
| 105 | + _ch->setKeepAlive(keep_alive); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter) { |
| 110 | + switch(adapter) { |
| 111 | + #if defined(BOARD_HAS_WIFI) |
| 112 | + case NetworkAdapter::WIFI: |
| 113 | + return new WiFiConnectionHandler(); |
| 114 | + break; |
| 115 | + #endif |
| 116 | + |
| 117 | + #if defined(BOARD_HAS_ETHERNET) |
| 118 | + case NetworkAdapter::ETHERNET: |
| 119 | + return new EthernetConnectionHandler(); |
| 120 | + break; |
| 121 | + #endif |
| 122 | + |
| 123 | + #if defined(BOARD_HAS_NB) |
| 124 | + case NetworkAdapter::NB: |
| 125 | + return new NBConnectionHandler(); |
| 126 | + break; |
| 127 | + #endif |
| 128 | + |
| 129 | + #if defined(BOARD_HAS_GSM) |
| 130 | + case NetworkAdapter::GSM: |
| 131 | + return new GSMConnectionHandler(); |
| 132 | + break; |
| 133 | + #endif |
| 134 | + |
| 135 | + #if defined(BOARD_HAS_CATM1_NBIOT) |
| 136 | + case NetworkAdapter::CATM1: |
| 137 | + return new CatM1ConnectionHandler(); |
| 138 | + break; |
| 139 | + #endif |
| 140 | + |
| 141 | + #if defined(BOARD_HAS_CELLULAR) |
| 142 | + case NetworkAdapter::CELL: |
| 143 | + return new CellularConnectionHandler(); |
| 144 | + break; |
| 145 | + #endif |
| 146 | + |
| 147 | + default: |
| 148 | + Debug.print(DBG_ERROR, "Network adapter not supported by this platform: %d", adapter); |
| 149 | + return nullptr; |
| 150 | + } |
| 151 | +} |
0 commit comments