diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index dc94e2ab..878d5669 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -34,6 +34,7 @@ jobs: - name: MKRGSM - name: MKRNB - name: MKRWAN + - name: Arduino_Cellular ARDUINOCORE_MBED_STAGING_PATH: extras/ArduinoCore-mbed ARDUINOCORE_API_STAGING_PATH: extras/ArduinoCore-API SKETCHES_REPORTS_PATH: sketches-reports diff --git a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino index a5be6a7e..960bfe00 100644 --- a/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino +++ b/examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino @@ -43,6 +43,8 @@ NBConnectionHandler conMan(SECRET_PIN); LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY); #elif defined(BOARD_HAS_CATM1_NBIOT) CatM1ConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_PASS); +#elif defined(BOARD_HAS_CELLULAR) +CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); #endif void setup() { diff --git a/src/Arduino_CellularConnectionHandler.cpp b/src/Arduino_CellularConnectionHandler.cpp new file mode 100644 index 00000000..7e543b0e --- /dev/null +++ b/src/Arduino_CellularConnectionHandler.cpp @@ -0,0 +1,95 @@ +/* + This file is part of the Arduino_ConnectionHandler library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "Arduino_CellularConnectionHandler.h" + +#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */ + +/****************************************************************************** + CTOR/DTOR + ******************************************************************************/ + +CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive) +: ConnectionHandler{keep_alive, NetworkAdapter::CELL} +, _pin(pin) +, _apn(apn) +, _login(login) +, _pass(pass) +{ + +} + +/****************************************************************************** + PUBLIC MEMBER FUNCTIONS + ******************************************************************************/ + +unsigned long CellularConnectionHandler::getTime() +{ + return _cellular.getCellularTime().getUNIXTimestamp(); +} + +UDP & CellularConnectionHandler::getUDP() +{ + Debug.print(DBG_ERROR, F("CellularConnectionHandler has no UDP support")); + while(1) {}; +} + +/****************************************************************************** + PROTECTED MEMBER FUNCTIONS + ******************************************************************************/ + +NetworkConnectionState CellularConnectionHandler::update_handleInit() +{ + _cellular.begin(); + _cellular.setDebugStream(Serial); + if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) { + Debug.print(DBG_ERROR, F("SIM not present or wrong PIN")); + return NetworkConnectionState::ERROR; + } + return NetworkConnectionState::CONNECTING; +} + +NetworkConnectionState CellularConnectionHandler::update_handleConnecting() +{ + if (!_cellular.connect(_apn, _login, _pass)) { + Debug.print(DBG_ERROR, F("The board was not able to register to the network...")); + return NetworkConnectionState::ERROR; + } + Debug.print(DBG_INFO, F("Connected to Network")); + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState CellularConnectionHandler::update_handleConnected() +{ + if (!_cellular.isConnectedToInternet()) { + return NetworkConnectionState::DISCONNECTED; + } + return NetworkConnectionState::CONNECTED; +} + +NetworkConnectionState CellularConnectionHandler::update_handleDisconnecting() +{ + return NetworkConnectionState::DISCONNECTED; +} + +NetworkConnectionState CellularConnectionHandler::update_handleDisconnected() +{ + if (_keep_alive) { + return NetworkConnectionState::INIT; + } + return NetworkConnectionState::CLOSED; +} + +#endif /* #ifdef BOARD_HAS_CELLULAR */ diff --git a/src/Arduino_CellularConnectionHandler.h b/src/Arduino_CellularConnectionHandler.h new file mode 100644 index 00000000..0c4d5f89 --- /dev/null +++ b/src/Arduino_CellularConnectionHandler.h @@ -0,0 +1,61 @@ +/* + This file is part of the Arduino_ConnectionHandler library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + + +#ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ +#define ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "Arduino_ConnectionHandler.h" + +#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */ + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class CellularConnectionHandler : public ConnectionHandler +{ + public: + + CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true); + + + virtual unsigned long getTime() override; + virtual Client & getClient() override { return _gsm_client; }; + virtual UDP & getUDP() override; + + + protected: + + virtual NetworkConnectionState update_handleInit () override; + virtual NetworkConnectionState update_handleConnecting () override; + virtual NetworkConnectionState update_handleConnected () override; + virtual NetworkConnectionState update_handleDisconnecting() override; + virtual NetworkConnectionState update_handleDisconnected () override; + + + private: + + const char * _pin; + const char * _apn; + const char * _login; + const char * _pass; + + ArduinoCellular _cellular; + TinyGsmClient _gsm_client = _cellular.getNetworkClient(); +}; + +#endif /* #ifdef BOARD_HAS_CELLULAR */ + +#endif /* #ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ */ diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 37f99fe0..b4f8e315 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -57,10 +57,12 @@ #include #include #include + #include #define BOARD_HAS_WIFI #define BOARD_HAS_ETHERNET #define BOARD_HAS_CATM1_NBIOT + #define BOARD_HAS_CELLULAR #define BOARD_HAS_PORTENTA_CATM1_NBIOT_SHIELD #define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD @@ -73,9 +75,11 @@ #include #include #include + #include #define BOARD_HAS_WIFI #define BOARD_HAS_ETHERNET + #define BOARD_HAS_CELLULAR #define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD #define NETWORK_IDLE_STATUS WL_IDLE_STATUS @@ -150,7 +154,7 @@ #if defined(ARDUINO_ARCH_ESP32) #include #include - + #define BOARD_HAS_WIFI #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD #define NETWORK_IDLE_STATUS WL_IDLE_STATUS @@ -201,7 +205,8 @@ enum class NetworkAdapter { NB, GSM, LORA, - CATM1 + CATM1, + CELL }; typedef void (*OnNetworkEventCallback)(); @@ -237,13 +242,11 @@ class ConnectionHandler { NetworkConnectionState check(); - #if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT) + #if !defined(BOARD_HAS_LORA) virtual unsigned long getTime() = 0; virtual Client &getClient() = 0; virtual UDP &getUDP() = 0; - #endif - - #if defined(BOARD_HAS_LORA) + #else virtual int write(const uint8_t *buf, size_t size) = 0; virtual int read() = 0; virtual bool available() = 0; @@ -304,4 +307,8 @@ class ConnectionHandler { #include "Arduino_CatM1ConnectionHandler.h" #endif +#if defined(BOARD_HAS_CELLULAR) + #include "Arduino_CellularConnectionHandler.h" +#endif + #endif /* CONNECTION_HANDLER_H_ */