|
| 1 | +/* |
| 2 | + This file is part of ArduinoIoTCloud. |
| 3 | +
|
| 4 | + Copyright 2020 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_EthernetConnectionHandler.h" |
| 23 | + |
| 24 | +#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */ |
| 25 | + |
| 26 | +/****************************************************************************** |
| 27 | + CTOR/DTOR |
| 28 | + ******************************************************************************/ |
| 29 | + |
| 30 | +EthernetConnectionHandler::EthernetConnectionHandler(uint8_t * mac, bool const keep_alive) |
| 31 | +: ConnectionHandler{keep_alive} |
| 32 | +, _mac{mac} |
| 33 | +{ |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +/****************************************************************************** |
| 38 | + PROTECTED MEMBER FUNCTIONS |
| 39 | + ******************************************************************************/ |
| 40 | + |
| 41 | +NetworkConnectionState EthernetConnectionHandler::update_handleInit() |
| 42 | +{ |
| 43 | + if (Ethernet.begin(const_cast<uint8_t *>(_mac)) == 0) { |
| 44 | + Debug.print(DBG_ERROR, F("Failed to configure Ethernet using DHCP")); |
| 45 | + |
| 46 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 47 | + Debug.print(DBG_ERROR, F("Error, ethernet shield was not found.")); |
| 48 | + return NetworkConnectionState::ERROR; |
| 49 | + } |
| 50 | + |
| 51 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 52 | + Debug.print(DBG_ERROR, F("Error, ethernet cable is not connected.")); |
| 53 | + return NetworkConnectionState::ERROR; |
| 54 | + } |
| 55 | + |
| 56 | + return NetworkConnectionState::ERROR; |
| 57 | + } |
| 58 | + |
| 59 | + return NetworkConnectionState::CONNECTING; |
| 60 | +} |
| 61 | + |
| 62 | +NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() |
| 63 | +{ |
| 64 | + return NetworkConnectionState::CONNECTED; |
| 65 | +} |
| 66 | + |
| 67 | +NetworkConnectionState EthernetConnectionHandler::update_handleConnected() |
| 68 | +{ |
| 69 | + if (Ethernet.linkStatus() == LinkON) |
| 70 | + return NetworkConnectionState::CONNECTED; |
| 71 | + else |
| 72 | + return NetworkConnectionState::DISCONNECTED; |
| 73 | +} |
| 74 | + |
| 75 | +NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting() |
| 76 | +{ |
| 77 | + return NetworkConnectionState::DISCONNECTED; |
| 78 | +} |
| 79 | + |
| 80 | +NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected() |
| 81 | +{ |
| 82 | + return NetworkConnectionState::INIT; |
| 83 | +} |
| 84 | + |
| 85 | +#endif /* #ifdef BOARD_HAS_ETHERNET */ |
0 commit comments