|
| 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 "Arduino_EthernetConnectionHandler.h" |
| 20 | + |
| 21 | +#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */ |
| 22 | + |
| 23 | +/****************************************************************************** |
| 24 | + CTOR/DTOR |
| 25 | + ******************************************************************************/ |
| 26 | + |
| 27 | +EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) |
| 28 | +: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} |
| 29 | +,_ip{INADDR_NONE} |
| 30 | +,_dns{INADDR_NONE} |
| 31 | +,_gateway{INADDR_NONE} |
| 32 | +,_netmask{INADDR_NONE} |
| 33 | +{ |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +EthernetConnectionHandler::EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, bool const keep_alive) |
| 38 | +: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} |
| 39 | +,_ip{ip} |
| 40 | +,_dns{dns} |
| 41 | +,_gateway{gateway} |
| 42 | +,_netmask{netmask} |
| 43 | +{ |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +EthernetConnectionHandler::EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, bool const keep_alive) |
| 48 | +: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} |
| 49 | +,_ip{INADDR_NONE} |
| 50 | +,_dns{INADDR_NONE} |
| 51 | +,_gateway{INADDR_NONE} |
| 52 | +,_netmask{INADDR_NONE} |
| 53 | +{ |
| 54 | + if(!_ip.fromString(ip)) { |
| 55 | + _ip = INADDR_NONE; |
| 56 | + } |
| 57 | + if(!_dns.fromString(dns)) { |
| 58 | + _dns = INADDR_NONE; |
| 59 | + } |
| 60 | + if(!_gateway.fromString(gateway)) { |
| 61 | + _gateway = INADDR_NONE; |
| 62 | + } |
| 63 | + if(!_netmask.fromString(netmask)) { |
| 64 | + _netmask = INADDR_NONE; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/****************************************************************************** |
| 69 | + PROTECTED MEMBER FUNCTIONS |
| 70 | + ******************************************************************************/ |
| 71 | + |
| 72 | +NetworkConnectionState EthernetConnectionHandler::update_handleInit() |
| 73 | +{ |
| 74 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 75 | + Debug.print(DBG_ERROR, F("Error, ethernet shield was not found.")); |
| 76 | + return NetworkConnectionState::ERROR; |
| 77 | + } |
| 78 | + return NetworkConnectionState::CONNECTING; |
| 79 | +} |
| 80 | + |
| 81 | +NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() |
| 82 | +{ |
| 83 | + if (_ip != INADDR_NONE) { |
| 84 | + if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _netmask, 15000, 4000) == 0) { |
| 85 | + Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection")); |
| 86 | + return NetworkConnectionState::CONNECTING; |
| 87 | + } |
| 88 | + } else { |
| 89 | + if (Ethernet.begin(nullptr, 15000, 4000) == 0) { |
| 90 | + Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection")); |
| 91 | + return NetworkConnectionState::CONNECTING; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return NetworkConnectionState::CONNECTED; |
| 96 | +} |
| 97 | + |
| 98 | +NetworkConnectionState EthernetConnectionHandler::update_handleConnected() |
| 99 | +{ |
| 100 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 101 | + Debug.print(DBG_ERROR, F("Ethernet link OFF, connection lost.")); |
| 102 | + if (_keep_alive) |
| 103 | + { |
| 104 | + Debug.print(DBG_ERROR, F("Attempting reconnection")); |
| 105 | + } |
| 106 | + return NetworkConnectionState::DISCONNECTED; |
| 107 | + } |
| 108 | + return NetworkConnectionState::CONNECTED; |
| 109 | +} |
| 110 | + |
| 111 | +NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting() |
| 112 | +{ |
| 113 | + Ethernet.disconnect(); |
| 114 | + return NetworkConnectionState::DISCONNECTED; |
| 115 | +} |
| 116 | + |
| 117 | +NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected() |
| 118 | +{ |
| 119 | + if (_keep_alive) |
| 120 | + { |
| 121 | + return NetworkConnectionState::INIT; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + return NetworkConnectionState::CLOSED; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#endif /* #ifdef BOARD_HAS_ETHERNET */ |
0 commit comments