From f4551983ceb167744527fa3b67567dfed9d5c840 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 2 May 2024 16:34:27 +0200 Subject: [PATCH 1/5] Add CellularConnectionHandler --- src/Arduino_CellularConnectionHandler.cpp | 89 +++++++++++++++++++++++ src/Arduino_CellularConnectionHandler.h | 62 ++++++++++++++++ src/Arduino_ConnectionHandler.h | 16 ++-- 3 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 src/Arduino_CellularConnectionHandler.cpp create mode 100644 src/Arduino_CellularConnectionHandler.h diff --git a/src/Arduino_CellularConnectionHandler.cpp b/src/Arduino_CellularConnectionHandler.cpp new file mode 100644 index 00000000..372bdd24 --- /dev/null +++ b/src/Arduino_CellularConnectionHandler.cpp @@ -0,0 +1,89 @@ +/* + 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(); +} + +/****************************************************************************** + 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..6270d188 --- /dev/null +++ b/src/Arduino_CellularConnectionHandler.h @@ -0,0 +1,62 @@ +/* + 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 { } __attribute__((error("CellularConnectionHandler has no UDP support"))); + + + 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..d3230a01 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -57,6 +57,7 @@ #include #include #include + #include #define BOARD_HAS_WIFI #define BOARD_HAS_ETHERNET @@ -150,7 +151,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 +202,8 @@ enum class NetworkAdapter { NB, GSM, LORA, - CATM1 + CATM1, + CELL }; typedef void (*OnNetworkEventCallback)(); @@ -237,13 +239,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 +304,8 @@ class ConnectionHandler { #include "Arduino_CatM1ConnectionHandler.h" #endif +#if defined(BOARD_HAS_CELLULAR) + #include "Arduino_CellularConnectionHandler.h" +#endif + #endif /* CONNECTION_HANDLER_H_ */ From eb559db9772d123ddba8767008b413e5cc22f3dd Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 24 May 2024 14:25:56 +0200 Subject: [PATCH 2/5] Add Cellular support to PORTENTA_H7 through Portenta Mid Carrier --- src/Arduino_ConnectionHandler.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index d3230a01..89c12c03 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -62,6 +62,7 @@ #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 From 353fd2534672943b2968862b9b34b8cfbbe81216 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 2 May 2024 16:37:00 +0200 Subject: [PATCH 3/5] Add Cellular support to PORTENTA_C33 through Portenta Mid Carrier --- src/Arduino_ConnectionHandler.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 89c12c03..b4f8e315 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -75,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 From 3ac5c054577481f5cc99c36bc87467b2b998d144 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 2 May 2024 16:43:19 +0200 Subject: [PATCH 4/5] Examples: add Cellular support --- .github/workflows/compile-examples.yml | 1 + examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino | 2 ++ 2 files changed, 3 insertions(+) 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() { From 075c689a1c00d3eb18eb175aa736db9868ec0786 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 3 Jun 2024 16:15:11 +0200 Subject: [PATCH 5/5] CellularConnectionHandler: Generate runtime error if getUDP() is called __attribute__((error)) not working on virtual functions --- src/Arduino_CellularConnectionHandler.cpp | 6 ++++++ src/Arduino_CellularConnectionHandler.h | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Arduino_CellularConnectionHandler.cpp b/src/Arduino_CellularConnectionHandler.cpp index 372bdd24..7e543b0e 100644 --- a/src/Arduino_CellularConnectionHandler.cpp +++ b/src/Arduino_CellularConnectionHandler.cpp @@ -40,6 +40,12 @@ 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 ******************************************************************************/ diff --git a/src/Arduino_CellularConnectionHandler.h b/src/Arduino_CellularConnectionHandler.h index 6270d188..0c4d5f89 100644 --- a/src/Arduino_CellularConnectionHandler.h +++ b/src/Arduino_CellularConnectionHandler.h @@ -18,7 +18,6 @@ #include "Arduino_ConnectionHandler.h" - #ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */ /****************************************************************************** @@ -34,7 +33,7 @@ class CellularConnectionHandler : public ConnectionHandler virtual unsigned long getTime() override; virtual Client & getClient() override { return _gsm_client; }; - virtual UDP & getUDP() override { } __attribute__((error("CellularConnectionHandler has no UDP support"))); + virtual UDP & getUDP() override; protected: