|
| 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 | +/****************************************************************************** |
| 13 | + INCLUDE |
| 14 | + ******************************************************************************/ |
| 15 | + |
| 16 | +#include "Arduino_CellularConnectionHandler.h" |
| 17 | + |
| 18 | +#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */ |
| 19 | + |
| 20 | +/****************************************************************************** |
| 21 | + CTOR/DTOR |
| 22 | + ******************************************************************************/ |
| 23 | + |
| 24 | +CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive) |
| 25 | +: ConnectionHandler{keep_alive, NetworkAdapter::CELL} |
| 26 | +, _pin(pin) |
| 27 | +, _apn(apn) |
| 28 | +, _login(login) |
| 29 | +, _pass(pass) |
| 30 | +{ |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +/****************************************************************************** |
| 35 | + PUBLIC MEMBER FUNCTIONS |
| 36 | + ******************************************************************************/ |
| 37 | + |
| 38 | +unsigned long CellularConnectionHandler::getTime() |
| 39 | +{ |
| 40 | + return _cellular.getCellularTime().getUNIXTimestamp(); |
| 41 | +} |
| 42 | + |
| 43 | +UDP & CellularConnectionHandler::getUDP() |
| 44 | +{ |
| 45 | + Debug.print(DBG_ERROR, F("CellularConnectionHandler has no UDP support")); |
| 46 | + while(1) {}; |
| 47 | +} |
| 48 | + |
| 49 | +/****************************************************************************** |
| 50 | + PROTECTED MEMBER FUNCTIONS |
| 51 | + ******************************************************************************/ |
| 52 | + |
| 53 | +NetworkConnectionState CellularConnectionHandler::update_handleInit() |
| 54 | +{ |
| 55 | + _cellular.begin(); |
| 56 | + _cellular.setDebugStream(Serial); |
| 57 | + if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) { |
| 58 | + Debug.print(DBG_ERROR, F("SIM not present or wrong PIN")); |
| 59 | + return NetworkConnectionState::ERROR; |
| 60 | + } |
| 61 | + return NetworkConnectionState::CONNECTING; |
| 62 | +} |
| 63 | + |
| 64 | +NetworkConnectionState CellularConnectionHandler::update_handleConnecting() |
| 65 | +{ |
| 66 | + if (!_cellular.connect(_apn, _login, _pass)) { |
| 67 | + Debug.print(DBG_ERROR, F("The board was not able to register to the network...")); |
| 68 | + return NetworkConnectionState::ERROR; |
| 69 | + } |
| 70 | + Debug.print(DBG_INFO, F("Connected to Network")); |
| 71 | + return NetworkConnectionState::CONNECTED; |
| 72 | +} |
| 73 | + |
| 74 | +NetworkConnectionState CellularConnectionHandler::update_handleConnected() |
| 75 | +{ |
| 76 | + if (!_cellular.isConnectedToInternet()) { |
| 77 | + return NetworkConnectionState::DISCONNECTED; |
| 78 | + } |
| 79 | + return NetworkConnectionState::CONNECTED; |
| 80 | +} |
| 81 | + |
| 82 | +NetworkConnectionState CellularConnectionHandler::update_handleDisconnecting() |
| 83 | +{ |
| 84 | + return NetworkConnectionState::DISCONNECTED; |
| 85 | +} |
| 86 | + |
| 87 | +NetworkConnectionState CellularConnectionHandler::update_handleDisconnected() |
| 88 | +{ |
| 89 | + if (_keep_alive) { |
| 90 | + return NetworkConnectionState::INIT; |
| 91 | + } |
| 92 | + return NetworkConnectionState::CLOSED; |
| 93 | +} |
| 94 | + |
| 95 | +#endif /* #ifdef BOARD_HAS_CELLULAR */ |
0 commit comments