├── .codespellrc ├── .github ├── dependabot.yml └── workflows │ ├── report-size-deltas.yml │ ├── spell-check.yml │ ├── check-arduino.yml │ ├── sync-labels.yml │ └── compile-examples.yml ├── library.properties ├── examples ├── ConnectionHandlerTimeoutTable │ ├── arduino_secrets.h │ └── ConnectionHandlerTimeoutTable.ino ├── GenericConnectionHandlerDemo │ ├── arduino_secrets.h │ └── GenericConnectionHandlerDemo.ino ├── ConnectionHandlerDemo │ ├── arduino_secrets.h │ └── ConnectionHandlerDemo.ino └── CheckInternetAvailabilityDemo │ ├── arduino_secrets.h │ └── CheckInternetAvailabilityDemo.ino ├── keywords.txt ├── src ├── Arduino_ConnectionHandler.h ├── GSMConnectionHandler.h ├── CellularConnectionHandler.h ├── CatM1ConnectionHandler.h ├── NBConnectionHandler.h ├── connectionHandlerModels │ ├── settings_default.h │ └── settings.h ├── EthernetConnectionHandler.h ├── GenericConnectionHandler.h ├── WiFiConnectionHandler.h ├── LoRaConnectionHandler.h ├── CellularConnectionHandler.cpp ├── CatM1ConnectionHandler.cpp ├── ConnectionHandlerInterface.h ├── NBConnectionHandler.cpp ├── GSMConnectionHandler.cpp ├── GenericConnectionHandler.cpp ├── EthernetConnectionHandler.cpp ├── ConnectionHandlerInterface.cpp ├── ConnectionHandlerDefinitions.h ├── WiFiConnectionHandler.cpp └── LoRaConnectionHandler.cpp ├── README.md └── LICENSE /.codespellrc: -------------------------------------------------------------------------------- 1 | # See: https://github.com/codespell-project/codespell#using-a-config-file 2 | [codespell] 3 | # In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: 4 | ignore-words-list = wan 5 | check-filenames = 6 | check-hidden = 7 | skip = ./.git 8 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # See: https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates#about-the-dependabotyml-file 2 | version: 2 3 | 4 | updates: 5 | # Configure check for outdated GitHub Actions actions in workflows. 6 | # See: https://docs.github.com/en/github/administering-a-repository/keeping-your-actions-up-to-date-with-dependabot 7 | - package-ecosystem: github-actions 8 | directory: / # Check the repository's workflows under /.github/workflows/ 9 | schedule: 10 | interval: daily 11 | labels: 12 | - "topic: infrastructure" 13 | -------------------------------------------------------------------------------- /.github/workflows/report-size-deltas.yml: -------------------------------------------------------------------------------- 1 | name: Report PR Size Deltas 2 | 3 | on: 4 | push: 5 | paths: 6 | - ".github/workflows/report-size-deltas.yml" 7 | schedule: 8 | - cron: '*/5 * * * *' 9 | workflow_dispatch: 10 | repository_dispatch: 11 | 12 | jobs: 13 | report: 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - name: Comment size deltas reports to PRs 18 | uses: arduino/report-size-deltas@v1 19 | with: 20 | # Regex matching the names of the workflow artifacts created by the "Compile Examples" workflow 21 | sketches-reports-source: ^sketches-report-.+ 22 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Arduino_ConnectionHandler 2 | version=1.2.0 3 | author=Ubi de Feo, Cristian Maglie, Andrea Catozzi, Alexander Entinger et al. 4 | maintainer=Arduino 5 | sentence=Arduino Library for network connection management (WiFi, GSM, NB, [Ethernet]) 6 | paragraph=Originally part of ArduinoIoTCloud 7 | category=Communication 8 | url=https://github.com/arduino-libraries/Arduino_ConnectionHandler 9 | architectures=samd,esp32,esp8266,mbed,megaavr,mbed_nano,mbed_portenta,mbed_nicla,mbed_opta,mbed_giga,renesas_portenta,renesas_uno,mbed_edge,stm32,rp2040 10 | depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN 11 | -------------------------------------------------------------------------------- /.github/workflows/spell-check.yml: -------------------------------------------------------------------------------- 1 | name: Spell Check 2 | 3 | # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | pull_request: 7 | schedule: 8 | # Run every Tuesday at 8 AM UTC to catch new misspelling detections resulting from dictionary updates. 9 | - cron: "0 8 * * TUE" 10 | workflow_dispatch: 11 | repository_dispatch: 12 | 13 | jobs: 14 | spellcheck: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v6 20 | 21 | - name: Spell check 22 | uses: codespell-project/actions-codespell@master 23 | -------------------------------------------------------------------------------- /examples/ConnectionHandlerTimeoutTable/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | // Required for WiFiConnectionHandler 2 | const char SECRET_WIFI_SSID[] = "SSID"; 3 | const char SECRET_WIFI_PASS[] = "PASSWORD"; 4 | 5 | // Required for GSMConnectionHandler 6 | const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS"; 7 | const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler 8 | const char SECRET_GSM_USER[] = "GSM USERNAME"; 9 | const char SECRET_GSM_PASS[] = "GSM PASSWORD"; 10 | 11 | // Required for LoRaConnectionHandler 12 | const char SECRET_APP_EUI[] = "APP_EUI"; 13 | const char SECRET_APP_KEY[] = "APP_KEY"; 14 | 15 | // Required for EthernetConnectionHandler (without DHCP mode) 16 | const char SECRET_IP[] = "IP ADDRESS"; 17 | const char SECRET_DNS[] = "DNS ADDRESS"; 18 | const char SECRET_GATEWAY[] = "GATEWAY ADDRESS"; 19 | const char SECRET_NETMASK[] = "NETWORK MASK"; 20 | -------------------------------------------------------------------------------- /examples/GenericConnectionHandlerDemo/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | // Required for WiFiConnectionHandler 2 | const char SECRET_WIFI_SSID[] = "SSID"; 3 | const char SECRET_WIFI_PASS[] = "PASSWORD"; 4 | 5 | // Required for GSMConnectionHandler 6 | const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS"; 7 | const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler 8 | const char SECRET_GSM_USER[] = "GSM USERNAME"; 9 | const char SECRET_GSM_PASS[] = "GSM PASSWORD"; 10 | 11 | // Required for LoRaConnectionHandler 12 | const char SECRET_APP_EUI[] = "APP_EUI"; 13 | const char SECRET_APP_KEY[] = "APP_KEY"; 14 | 15 | // Required for EthernetConnectionHandler (without DHCP mode) 16 | const char SECRET_IP[] = "IP ADDRESS"; 17 | const char SECRET_DNS[] = "DNS ADDRESS"; 18 | const char SECRET_GATEWAY[] = "GATEWAY ADDRESS"; 19 | const char SECRET_NETMASK[] = "NETWORK MASK"; 20 | -------------------------------------------------------------------------------- /examples/ConnectionHandlerDemo/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | // Required for WiFiConnectionHandler 2 | const char SECRET_WIFI_SSID[] = "NETWORK NAME"; 3 | const char SECRET_WIFI_PASS[] = "NETWORK PASSWORD"; 4 | 5 | // Required for GSMConnectionHandler 6 | const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS"; 7 | const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler 8 | const char SECRET_GSM_USER[] = "GSM USERNAME"; 9 | const char SECRET_GSM_PASS[] = "GSM PASSWORD"; 10 | 11 | // Required for LoRaConnectionHandler 12 | const char SECRET_APP_EUI[] = "APP_EUI"; 13 | const char SECRET_APP_KEY[] = "APP_KEY"; 14 | 15 | // Required for EthernetConnectionHandler (without DHCP mode) 16 | const char SECRET_IP[] = "IP ADDRESS"; 17 | const char SECRET_DNS[] = "DNS ADDRESS"; 18 | const char SECRET_GATEWAY[] = "GATEWAY ADDRESS"; 19 | const char SECRET_NETMASK[] = "NETWORK MASK"; 20 | -------------------------------------------------------------------------------- /.github/workflows/check-arduino.yml: -------------------------------------------------------------------------------- 1 | name: Check Arduino 2 | 3 | # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | pull_request: 7 | schedule: 8 | # Run every Tuesday at 8 AM UTC to catch breakage caused by new rules added to Arduino Lint. 9 | - cron: "0 8 * * TUE" 10 | workflow_dispatch: 11 | repository_dispatch: 12 | 13 | jobs: 14 | lint: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v6 20 | 21 | - name: Arduino Lint 22 | uses: arduino/arduino-lint-action@v2 23 | with: 24 | compliance: specification 25 | library-manager: update 26 | # Always use this setting for official repositories. Remove for 3rd party projects. 27 | official: true 28 | project-type: library 29 | -------------------------------------------------------------------------------- /examples/CheckInternetAvailabilityDemo/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | // Required for WiFiConnectionHandler 2 | const char SECRET_WIFI_SSID[] = "NETWORK NAME"; 3 | const char SECRET_WIFI_PASS[] = "NETWORK PASSWORD"; 4 | 5 | // Required for GSMConnectionHandler 6 | const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS"; 7 | const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler 8 | const char SECRET_GSM_USER[] = "GSM USERNAME"; 9 | const char SECRET_GSM_PASS[] = "GSM PASSWORD"; 10 | 11 | // Required for LoRaConnectionHandler 12 | const char SECRET_APP_EUI[] = "APP_EUI"; 13 | const char SECRET_APP_KEY[] = "APP_KEY"; 14 | 15 | // Required for EthernetConnectionHandler (without DHCP mode) 16 | const char SECRET_IP[] = "IP ADDRESS"; 17 | const char SECRET_DNS[] = "DNS ADDRESS"; 18 | const char SECRET_GATEWAY[] = "GATEWAY ADDRESS"; 19 | const char SECRET_NETMASK[] = "NETWORK MASK"; 20 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | #################################################### 2 | # Syntax Coloring Map For Arduino_ConnectionHandler 3 | #################################################### 4 | 5 | #################################################### 6 | # Datatypes (KEYWORD1) 7 | #################################################### 8 | ConnectionHandler KEYWORD1 9 | WiFiConnectionHandler KEYWORD1 10 | GSMConnectionHandler KEYWORD1 11 | NBConnectionHandler KEYWORD1 12 | LoRaConnectionHandler KEYWORD1 13 | EthernetConnectionHandler KEYWORD1 14 | CatM1ConnectionHandler KEYWORD1 15 | 16 | #################################################### 17 | # Methods and Functions (KEYWORD2) 18 | #################################################### 19 | 20 | ConnectionHandler KEYWORD2 21 | available KEYWORD2 22 | read KEYWORD2 23 | write KEYWORD2 24 | check KEYWORD2 25 | connect KEYWORD2 26 | disconnect KEYWORD2 27 | addCallback KEYWORD2 28 | getTime KEYWORD2 29 | getClient KEYWORD2 30 | getUDP KEYWORD2 31 | 32 | #################################################### 33 | # Constants (LITERAL1) 34 | #################################################### 35 | 36 | -------------------------------------------------------------------------------- /src/Arduino_ConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | #ifndef ARDUINO_CONNECTION_HANDLER_H_ 12 | #define ARDUINO_CONNECTION_HANDLER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDES 16 | ******************************************************************************/ 17 | 18 | #if !defined(__AVR__) 19 | # include 20 | #endif 21 | 22 | #include 23 | #include "ConnectionHandlerDefinitions.h" 24 | 25 | #if defined(BOARD_HAS_WIFI) 26 | #include "WiFiConnectionHandler.h" 27 | #endif 28 | 29 | #if defined(BOARD_HAS_GSM) 30 | #include "GSMConnectionHandler.h" 31 | #endif 32 | 33 | #if defined(BOARD_HAS_NB) 34 | #include "NBConnectionHandler.h" 35 | #endif 36 | 37 | #if defined(BOARD_HAS_LORA) 38 | #include "LoRaConnectionHandler.h" 39 | #endif 40 | 41 | #if defined(BOARD_HAS_ETHERNET) 42 | #include "EthernetConnectionHandler.h" 43 | #endif 44 | 45 | #if defined(BOARD_HAS_CATM1_NBIOT) 46 | #include "CatM1ConnectionHandler.h" 47 | #endif 48 | 49 | #if defined(BOARD_HAS_CELLULAR) 50 | #include "CellularConnectionHandler.h" 51 | #endif 52 | 53 | #endif /* CONNECTION_HANDLER_H_ */ 54 | -------------------------------------------------------------------------------- /src/GSMConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | #ifndef GSM_CONNECTION_MANAGER_H_ 12 | #define GSM_CONNECTION_MANAGER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "ConnectionHandlerInterface.h" 19 | 20 | #if defined(ARDUINO_SAMD_MKRGSM1400) 21 | #include 22 | #endif 23 | 24 | #ifndef BOARD_HAS_GSM 25 | #error "Board doesn't support GSM" 26 | #endif 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class GSMConnectionHandler : public ConnectionHandler 33 | { 34 | public: 35 | GSMConnectionHandler(); 36 | GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true); 37 | 38 | 39 | virtual unsigned long getTime() override; 40 | virtual Client & getClient() override { return _gsm_client; }; 41 | virtual UDP & getUDP() override { return _gsm_udp; }; 42 | 43 | 44 | protected: 45 | 46 | virtual NetworkConnectionState update_handleInit () override; 47 | virtual NetworkConnectionState update_handleConnecting () override; 48 | virtual NetworkConnectionState update_handleConnected () override; 49 | virtual NetworkConnectionState update_handleDisconnecting() override; 50 | virtual NetworkConnectionState update_handleDisconnected () override; 51 | 52 | 53 | private: 54 | 55 | GSM _gsm; 56 | GPRS _gprs; 57 | GSMUDP _gsm_udp; 58 | GSMClient _gsm_client; 59 | }; 60 | 61 | #endif /* #ifndef GSM_CONNECTION_MANAGER_H_ */ 62 | -------------------------------------------------------------------------------- /src/CellularConnectionHandler.h: -------------------------------------------------------------------------------- 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 | #ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ 13 | #define ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ 14 | 15 | /****************************************************************************** 16 | INCLUDE 17 | ******************************************************************************/ 18 | 19 | #include "ConnectionHandlerInterface.h" 20 | 21 | #if defined(ARDUINO_PORTENTA_C33) || defined(ARDUINO_PORTENTA_H7_M7) 22 | #include 23 | #endif 24 | 25 | #ifndef BOARD_HAS_CELLULAR 26 | #error "Board doesn't support CELLULAR" 27 | #endif 28 | 29 | /****************************************************************************** 30 | CLASS DECLARATION 31 | ******************************************************************************/ 32 | 33 | class CellularConnectionHandler : public ConnectionHandler 34 | { 35 | public: 36 | CellularConnectionHandler(); 37 | CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true); 38 | 39 | 40 | virtual unsigned long getTime() override; 41 | virtual Client & getClient() override { return _gsm_client; }; 42 | virtual UDP & getUDP() override; 43 | 44 | 45 | protected: 46 | 47 | virtual NetworkConnectionState update_handleInit () override; 48 | virtual NetworkConnectionState update_handleConnecting () override; 49 | virtual NetworkConnectionState update_handleConnected () override; 50 | virtual NetworkConnectionState update_handleDisconnecting() override; 51 | virtual NetworkConnectionState update_handleDisconnected () override; 52 | 53 | 54 | private: 55 | 56 | ArduinoCellular _cellular; 57 | TinyGsmClient _gsm_client = _cellular.getNetworkClient(); 58 | }; 59 | 60 | #endif /* #ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ */ 61 | -------------------------------------------------------------------------------- /src/CatM1ConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2023 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 | #ifndef ARDUINO_CATM1_CONNECTION_HANDLER_H_ 12 | #define ARDUINO_CATM1_CONNECTION_HANDLER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "ConnectionHandlerInterface.h" 19 | 20 | #if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_EDGE_CONTROL) 21 | #include 22 | #endif 23 | 24 | #ifndef BOARD_HAS_CATM1_NBIOT 25 | #error "Board doesn't support CATM1_NBIOT" 26 | #endif 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class CatM1ConnectionHandler : public ConnectionHandler 33 | { 34 | public: 35 | 36 | CatM1ConnectionHandler(); 37 | CatM1ConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, RadioAccessTechnologyType rat = CATM1, uint32_t band = BAND_3 | BAND_20 | BAND_19, bool const keep_alive = true); 38 | 39 | 40 | virtual unsigned long getTime() override; 41 | virtual Client & getClient() override { return _gsm_client; }; 42 | virtual UDP & getUDP() override { return _gsm_udp; }; 43 | 44 | 45 | protected: 46 | 47 | virtual NetworkConnectionState update_handleInit () override; 48 | virtual NetworkConnectionState update_handleConnecting () override; 49 | virtual NetworkConnectionState update_handleConnected () override; 50 | virtual NetworkConnectionState update_handleDisconnecting() override; 51 | virtual NetworkConnectionState update_handleDisconnected () override; 52 | 53 | 54 | private: 55 | 56 | bool _reset; 57 | 58 | GSMUDP _gsm_udp; 59 | GSMClient _gsm_client; 60 | }; 61 | 62 | #endif /* #ifndef ARDUINO_CATM1_CONNECTION_HANDLER_H_ */ 63 | -------------------------------------------------------------------------------- /src/NBConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | #ifndef NB_CONNECTION_MANAGER_H_ 12 | #define NB_CONNECTION_MANAGER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "ConnectionHandlerInterface.h" 19 | 20 | #ifdef ARDUINO_SAMD_MKRNB1500 21 | #include 22 | #endif 23 | 24 | #ifndef BOARD_HAS_NB 25 | #error "Board doesn't support NB" 26 | #endif 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class NBConnectionHandler : public ConnectionHandler 33 | { 34 | public: 35 | NBConnectionHandler(); 36 | NBConnectionHandler(char const * pin, bool const keep_alive = true); 37 | NBConnectionHandler(char const * pin, char const * apn, bool const keep_alive = true); 38 | NBConnectionHandler(char const * pin, char const * apn, char const * login, char const * pass, bool const keep_alive = true); 39 | 40 | 41 | virtual unsigned long getTime() override; 42 | virtual Client & getClient() override { return _nb_client; }; 43 | virtual UDP & getUDP() override { return _nb_udp; }; 44 | 45 | 46 | protected: 47 | 48 | virtual NetworkConnectionState update_handleInit () override; 49 | virtual NetworkConnectionState update_handleConnecting () override; 50 | virtual NetworkConnectionState update_handleConnected () override; 51 | virtual NetworkConnectionState update_handleDisconnecting() override; 52 | virtual NetworkConnectionState update_handleDisconnected () override; 53 | 54 | 55 | private: 56 | 57 | void changeConnectionState(NetworkConnectionState _newState); 58 | 59 | NB _nb; 60 | GPRS _nb_gprs; 61 | NBUDP _nb_udp; 62 | NBClient _nb_client; 63 | }; 64 | 65 | #endif /* #ifndef NB_CONNECTION_MANAGER_H_ */ 66 | -------------------------------------------------------------------------------- /src/connectionHandlerModels/settings_default.h: -------------------------------------------------------------------------------- 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 | #pragma once 12 | #include "settings.h" 13 | 14 | namespace models { 15 | 16 | /* 17 | * if the cpp version is older than cpp14 then a constexpr function cannot include 18 | * other than a simple return statement, thsu we can define it only as inline 19 | */ 20 | #if __cplusplus > 201103L 21 | constexpr NetworkSetting settingsDefault(NetworkAdapter type) { 22 | #else 23 | inline NetworkSetting settingsDefault(NetworkAdapter type) { 24 | #endif 25 | 26 | NetworkSetting res = {type, {}}; 27 | 28 | switch(type) { 29 | #if defined(BOARD_HAS_ETHERNET) 30 | case NetworkAdapter::ETHERNET: 31 | res.eth.timeout = 15000; 32 | res.eth.response_timeout = 4000; 33 | break; 34 | #endif //defined(BOARD_HAS_ETHERNET) 35 | 36 | #if defined(BOARD_HAS_CATM1_NBIOT) 37 | case NetworkAdapter::CATM1: 38 | res.catm1.rat = 7; // CATM1 39 | res.catm1.band = 0x04 | 0x80000 | 0x40000; // BAND_3 | BAND_20 | BAND_19 40 | break; 41 | #endif //defined(BOARD_HAS_CATM1_NBIOT) 42 | 43 | #if defined(BOARD_HAS_LORA) 44 | case NetworkAdapter::LORA: 45 | res.lora.band = 5; // _lora_band::EU868 46 | res.lora.channelMask[0] = '\0'; 47 | res.lora.deviceClass = 'A'; // _lora_class::CLASS_A 48 | break; 49 | #endif //defined(BOARD_HAS_LORA) 50 | 51 | #if defined(BOARD_HAS_WIFI) 52 | case NetworkAdapter::WIFI: // nothing todo, default optional values are fine with 0 53 | #endif //defined(BOARD_HAS_WIFI) 54 | 55 | #if defined(BOARD_HAS_NB) 56 | case NetworkAdapter::NB: // nothing todo, default optional values are fine with 0 57 | #endif //defined(BOARD_HAS_NB) 58 | 59 | #if defined(BOARD_HAS_GSM) 60 | case NetworkAdapter::GSM: // nothing todo, default optional values are fine with 0 61 | #endif //defined(BOARD_HAS_GSM) 62 | 63 | #if defined(BOARD_HAS_CELLULAR) 64 | case NetworkAdapter::CELL: // nothing todo, default optional values are fine with 0 65 | #endif //defined(BOARD_HAS_CELLULAR) 66 | default: 67 | (void) 0; 68 | } 69 | 70 | return res; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/EthernetConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2020 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 | #ifndef ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ 12 | #define ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "ConnectionHandlerInterface.h" 19 | 20 | #if defined(ARDUINO_PORTENTA_H7_M7) 21 | #include 22 | #include 23 | #elif defined(ARDUINO_PORTENTA_C33) 24 | #include 25 | #include 26 | #elif defined(ARDUINO_OPTA) 27 | #include 28 | #include 29 | #endif 30 | 31 | #ifndef BOARD_HAS_ETHERNET 32 | #error "Board doesn't support ETHERNET" 33 | #endif 34 | 35 | /****************************************************************************** 36 | CLASS DECLARATION 37 | ******************************************************************************/ 38 | 39 | class EthernetConnectionHandler : public ConnectionHandler 40 | { 41 | public: 42 | 43 | EthernetConnectionHandler( 44 | unsigned long const timeout = 15000, 45 | unsigned long const responseTimeout = 4000, 46 | bool const keep_alive = true); 47 | 48 | EthernetConnectionHandler( 49 | const IPAddress ip, 50 | const IPAddress dns, 51 | const IPAddress gateway, 52 | const IPAddress netmask, 53 | unsigned long const timeout = 15000, 54 | unsigned long const responseTimeout = 4000, 55 | bool const keep_alive = true); 56 | 57 | virtual unsigned long getTime() override { return 0; } 58 | virtual Client & getClient() override{ return _eth_client; } 59 | virtual UDP & getUDP() override { return _eth_udp; } 60 | 61 | protected: 62 | 63 | virtual NetworkConnectionState update_handleInit () override; 64 | virtual NetworkConnectionState update_handleConnecting () override; 65 | virtual NetworkConnectionState update_handleConnected () override; 66 | virtual NetworkConnectionState update_handleDisconnecting() override; 67 | virtual NetworkConnectionState update_handleDisconnected () override; 68 | 69 | private: 70 | 71 | EthernetUDP _eth_udp; 72 | EthernetClient _eth_client; 73 | 74 | }; 75 | 76 | #endif /* ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ */ 77 | -------------------------------------------------------------------------------- /src/GenericConnectionHandler.h: -------------------------------------------------------------------------------- 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 | #ifndef ARDUINO_GENERIC_CONNECTION_HANDLER_H_ 12 | #define ARDUINO_GENERIC_CONNECTION_HANDLER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "ConnectionHandlerInterface.h" 19 | 20 | /****************************************************************************** 21 | CLASS DECLARATION 22 | ******************************************************************************/ 23 | 24 | /** GenericConnectionHandler class 25 | * This class aims to wrap a connectionHandler and provide a generic way to 26 | * instantiate a specific connectionHandler type 27 | */ 28 | class GenericConnectionHandler : public ConnectionHandler 29 | { 30 | public: 31 | 32 | GenericConnectionHandler(bool const keep_alive=true): ConnectionHandler(keep_alive), _ch(nullptr) {} 33 | 34 | #if defined(BOARD_HAS_NOTECARD) || defined(BOARD_HAS_LORA) 35 | virtual bool available() = 0; 36 | virtual int read() = 0; 37 | virtual int write(const uint8_t *buf, size_t size) = 0; 38 | #else 39 | unsigned long getTime() override; 40 | 41 | /* 42 | * NOTE: The following functions have a huge risk of returning a reference to a non existing memory location 43 | * It is important to make sure that the internal connection handler is already allocated before calling them 44 | * When updateSettings is called and the internal connectionHandler is reallocated the references to TCP and UDP 45 | * handles should be deleted. 46 | */ 47 | Client & getClient() override; 48 | UDP & getUDP() override; 49 | #endif 50 | 51 | bool updateSetting(const models::NetworkSetting& s) override; 52 | void getSetting(models::NetworkSetting& s) override; 53 | 54 | void connect() override; 55 | void disconnect() override; 56 | 57 | void setKeepAlive(bool keep_alive=true) override; 58 | 59 | protected: 60 | 61 | NetworkConnectionState updateConnectionState() override; 62 | 63 | NetworkConnectionState update_handleInit () override; 64 | NetworkConnectionState update_handleConnecting () override; 65 | NetworkConnectionState update_handleConnected () override; 66 | NetworkConnectionState update_handleDisconnecting() override; 67 | NetworkConnectionState update_handleDisconnected () override; 68 | 69 | private: 70 | 71 | ConnectionHandler* _ch; 72 | }; 73 | 74 | #endif /* ARDUINO_GENERIC_CONNECTION_HANDLER_H_ */ 75 | -------------------------------------------------------------------------------- /src/WiFiConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | #ifndef ARDUINO_WIFI_CONNECTION_HANDLER_H_ 12 | #define ARDUINO_WIFI_CONNECTION_HANDLER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "ConnectionHandlerInterface.h" 19 | 20 | #ifdef ARDUINO_SAMD_MKR1000 21 | #include 22 | #include 23 | #elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ 24 | defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined (ARDUINO_NANO_RP2040_CONNECT) 25 | #include 26 | #include 27 | #elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M7) || \ 28 | defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) 29 | #include 30 | #include 31 | #elif defined(ARDUINO_PORTENTA_C33) 32 | #include 33 | #include 34 | #elif defined(ARDUINO_ARCH_ESP8266) 35 | #include 36 | #include 37 | #elif defined(ARDUINO_ARCH_ESP32) 38 | #include 39 | #include 40 | #elif defined(ARDUINO_UNOR4_WIFI) 41 | #include 42 | #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) 43 | #include 44 | #include 45 | #endif 46 | 47 | #ifndef BOARD_HAS_WIFI 48 | #error "Board doesn't support WIFI" 49 | #endif 50 | 51 | /****************************************************************************** 52 | CLASS DECLARATION 53 | ******************************************************************************/ 54 | 55 | class WiFiConnectionHandler : public ConnectionHandler 56 | { 57 | public: 58 | WiFiConnectionHandler(); 59 | WiFiConnectionHandler(char const * ssid, char const * pass, bool const keep_alive = true); 60 | 61 | 62 | virtual unsigned long getTime() override; 63 | virtual Client & getClient() override { return _wifi_client; } 64 | virtual UDP & getUDP() override { return _wifi_udp; } 65 | 66 | 67 | protected: 68 | 69 | virtual NetworkConnectionState update_handleInit () override; 70 | virtual NetworkConnectionState update_handleConnecting () override; 71 | virtual NetworkConnectionState update_handleConnected () override; 72 | virtual NetworkConnectionState update_handleDisconnecting() override; 73 | virtual NetworkConnectionState update_handleDisconnected () override; 74 | 75 | private: 76 | WiFiUDP _wifi_udp; 77 | WiFiClient _wifi_client; 78 | }; 79 | 80 | #endif /* ARDUINO_WIFI_CONNECTION_HANDLER_H_ */ 81 | -------------------------------------------------------------------------------- /src/LoRaConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | #ifndef ARDUINO_LORA_CONNECTION_HANDLER_H_ 12 | #define ARDUINO_LORA_CONNECTION_HANDLER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "ConnectionHandlerInterface.h" 19 | 20 | #if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) 21 | #include 22 | #endif 23 | 24 | #ifndef BOARD_HAS_LORA 25 | #error "Board doesn't support LORA" 26 | #endif 27 | 28 | 29 | /****************************************************************************** 30 | CLASS DECLARATION 31 | ******************************************************************************/ 32 | 33 | class LoRaConnectionHandler : public ConnectionHandler 34 | { 35 | public: 36 | 37 | LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band = _lora_band::EU868, char const * channelMask = NULL, _lora_class const device_class = _lora_class::CLASS_A); 38 | 39 | virtual int write(const uint8_t *buf, size_t size) override; 40 | virtual int read() override; 41 | virtual bool available() override; 42 | 43 | inline String getVersion() { return _modem.version(); } 44 | inline String getDeviceEUI() { return _modem.deviceEUI(); } 45 | inline int getChannelMaskSize(_lora_band band) { return _modem.getChannelMaskSize(band); } 46 | inline String getChannelMask() { return _modem.getChannelMask(); } 47 | inline int isChannelEnabled(int pos) { return _modem.isChannelEnabled(pos); } 48 | inline int getDataRate() { return _modem.getDataRate(); } 49 | inline int getADR() { return _modem.getADR(); } 50 | inline String getDevAddr() { return _modem.getDevAddr(); } 51 | inline String getNwkSKey() { return _modem.getNwkSKey(); } 52 | inline String getAppSKey() { return _modem.getAppSKey(); } 53 | inline int getRX2DR() { return _modem.getRX2DR(); } 54 | inline uint32_t getRX2Freq() { return _modem.getRX2Freq(); } 55 | inline int32_t getFCU() { return _modem.getFCU(); } 56 | inline int32_t getFCD() { return _modem.getFCD(); } 57 | 58 | protected: 59 | 60 | virtual NetworkConnectionState update_handleInit () override; 61 | virtual NetworkConnectionState update_handleConnecting () override; 62 | virtual NetworkConnectionState update_handleConnected () override; 63 | virtual NetworkConnectionState update_handleDisconnecting() override; 64 | virtual NetworkConnectionState update_handleDisconnected () override; 65 | 66 | 67 | private: 68 | 69 | LoRaModem _modem; 70 | }; 71 | 72 | #endif /* ARDUINO_LORA_CONNECTION_HANDLER_H_ */ 73 | -------------------------------------------------------------------------------- /examples/ConnectionHandlerTimeoutTable/ConnectionHandlerTimeoutTable.ino: -------------------------------------------------------------------------------- 1 | /* SECRET_ fields are in `arduino_secrets.h` (included below) 2 | * 3 | * This example is a lightly modified version of ConnectionHandlerDemo to showcase 4 | * the possibility of changing the timeout values for the different states a connectionhandler have 5 | */ 6 | 7 | #include 8 | 9 | #include "arduino_secrets.h" 10 | 11 | #if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \ 12 | defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)) 13 | #error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md" 14 | #endif 15 | 16 | #if defined(BOARD_HAS_ETHERNET) 17 | EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); 18 | #elif defined(BOARD_HAS_WIFI) 19 | WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 20 | #elif defined(BOARD_HAS_GSM) 21 | GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 22 | #elif defined(BOARD_HAS_NB) 23 | NBConnectionHandler conMan(SECRET_PIN); 24 | #elif defined(BOARD_HAS_LORA) 25 | LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY); 26 | #elif defined(BOARD_HAS_CATM1_NBIOT) 27 | CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 28 | #elif defined(BOARD_HAS_CELLULAR) 29 | CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 30 | #endif 31 | 32 | bool attemptConnect = false; 33 | uint32_t lastConnToggleMs = 0; 34 | 35 | void setup() { 36 | /* Initialize serial debug port and wait up to 5 seconds for port to open */ 37 | Serial.begin(9600); 38 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 39 | 40 | #ifndef __AVR__ 41 | /* Set the debug message level: 42 | * - DBG_ERROR: Only show error messages 43 | * - DBG_WARNING: Show warning and error messages 44 | * - DBG_INFO: Show info, warning, and error messages 45 | * - DBG_DEBUG: Show debug, info, warning, and error messages 46 | * - DBG_VERBOSE: Show all messages 47 | */ 48 | setDebugMessageLevel(DBG_INFO); 49 | #endif 50 | 51 | /* Add callbacks to the ConnectionHandler object to get notified of network 52 | * connection events. */ 53 | conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); 54 | conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); 55 | conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); 56 | 57 | /* By using updateTimeoutInterval I can change the timeout value for a specific 58 | * state of the connection handler 59 | */ 60 | conMan.updateTimeoutInterval(NetworkConnectionState::INIT, 8000); 61 | conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTING, 1000); 62 | conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTED, 20000); 63 | conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTING, 200); 64 | conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTED, 2000); 65 | conMan.updateTimeoutInterval(NetworkConnectionState::CLOSED, 2000); 66 | conMan.updateTimeoutInterval(NetworkConnectionState::ERROR, 2000); 67 | } 68 | 69 | void loop() { 70 | conMan.check(); 71 | } 72 | 73 | void onNetworkConnect() { 74 | Serial.println(">>>> CONNECTED to network"); 75 | } 76 | 77 | void onNetworkDisconnect() { 78 | Serial.println(">>>> DISCONNECTED from network"); 79 | } 80 | 81 | void onNetworkError() { 82 | Serial.println(">>>> ERROR"); 83 | } 84 | -------------------------------------------------------------------------------- /src/connectionHandlerModels/settings.h: -------------------------------------------------------------------------------- 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 | #pragma once 12 | 13 | #include "ConnectionHandlerDefinitions.h" 14 | #include 15 | #include 16 | 17 | namespace models { 18 | constexpr size_t WifiSsidLength = 33; // Max length of wifi ssid is 32 + \0 19 | constexpr size_t WifiPwdLength = 64; // Max length of wifi password is 63 + \0 20 | 21 | constexpr size_t CellularPinLength = 9; 22 | constexpr size_t CellularApnLength = 101; // Max length of apn is 100 + \0 23 | constexpr size_t CellularLoginLength = 65; 24 | constexpr size_t CellularPassLength = 65; 25 | 26 | constexpr size_t LoraAppeuiLength = 17; // appeui is 8 octets * 2 (hex format) + \0 27 | constexpr size_t LoraAppkeyLength = 33; // appeui is 16 octets * 2 (hex format) + \0 28 | constexpr size_t LoraChannelMaskLength = 13; 29 | 30 | #if defined(BOARD_HAS_WIFI) 31 | struct WiFiSetting { 32 | char ssid[WifiSsidLength]; 33 | char pwd[WifiPwdLength]; 34 | }; 35 | #endif //defined(BOARD_HAS_WIFI) 36 | 37 | #if defined(BOARD_HAS_ETHERNET) 38 | // this struct represents an ip address in its simplest form. 39 | // FIXME this should be available from ArduinoCore-api IPAddress 40 | struct ip_addr { 41 | IPType type; 42 | union { 43 | uint8_t bytes[16]; 44 | uint32_t dword[4]; 45 | }; 46 | }; 47 | 48 | struct EthernetSetting { 49 | ip_addr ip; 50 | ip_addr dns; 51 | ip_addr gateway; 52 | ip_addr netmask; 53 | unsigned long timeout; 54 | unsigned long response_timeout; 55 | }; 56 | #endif // BOARD_HAS_ETHERNET 57 | 58 | #if defined(BOARD_HAS_NB) || defined(BOARD_HAS_GSM) ||defined(BOARD_HAS_CELLULAR) 59 | struct CellularSetting { 60 | char pin[CellularPinLength]; 61 | char apn[CellularApnLength]; 62 | char login[CellularLoginLength]; 63 | char pass[CellularPassLength]; 64 | }; 65 | #endif // defined(BOARD_HAS_NB) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR) 66 | 67 | #if defined(BOARD_HAS_GSM) 68 | typedef CellularSetting GSMSetting; 69 | #endif //defined(BOARD_HAS_GSM) 70 | 71 | #if defined(BOARD_HAS_NB) 72 | typedef CellularSetting NBSetting; 73 | #endif //defined(BOARD_HAS_NB) 74 | 75 | #if defined(BOARD_HAS_CATM1_NBIOT) 76 | struct CATM1Setting { 77 | char pin[CellularPinLength]; 78 | char apn[CellularApnLength]; 79 | char login[CellularLoginLength]; 80 | char pass[CellularPassLength]; 81 | uint32_t band; 82 | uint8_t rat; 83 | }; 84 | #endif //defined(BOARD_HAS_CATM1_NBIOT) 85 | 86 | #if defined(BOARD_HAS_LORA) 87 | struct LoraSetting { 88 | char appeui[LoraAppeuiLength]; 89 | char appkey[LoraAppkeyLength]; 90 | uint8_t band; 91 | char channelMask[LoraChannelMaskLength]; 92 | uint8_t deviceClass; 93 | }; 94 | #endif 95 | 96 | struct NetworkSetting { 97 | NetworkAdapter type; 98 | union { 99 | #if defined(BOARD_HAS_WIFI) 100 | WiFiSetting wifi; 101 | #endif 102 | 103 | #if defined(BOARD_HAS_ETHERNET) 104 | EthernetSetting eth; 105 | #endif 106 | 107 | #if defined(BOARD_HAS_NB) 108 | NBSetting nb; 109 | #endif 110 | 111 | #if defined(BOARD_HAS_GSM) 112 | GSMSetting gsm; 113 | #endif 114 | 115 | #if defined(BOARD_HAS_CATM1_NBIOT) 116 | CATM1Setting catm1; 117 | #endif 118 | 119 | #if defined(BOARD_HAS_CELLULAR) 120 | CellularSetting cell; 121 | #endif 122 | 123 | #if defined(BOARD_HAS_LORA) 124 | LoraSetting lora; 125 | #endif 126 | }; 127 | }; 128 | } 129 | 130 | #include "settings_default.h" 131 | -------------------------------------------------------------------------------- /src/CellularConnectionHandler.cpp: -------------------------------------------------------------------------------- 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 "ConnectionHandlerDefinitions.h" 17 | 18 | #ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */ 19 | #include "CellularConnectionHandler.h" 20 | 21 | /****************************************************************************** 22 | CTOR/DTOR 23 | ******************************************************************************/ 24 | CellularConnectionHandler::CellularConnectionHandler() 25 | : ConnectionHandler(true, NetworkAdapter::CELL) {} 26 | 27 | CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive) 28 | : ConnectionHandler{keep_alive, NetworkAdapter::CELL} 29 | { 30 | _settings.type = NetworkAdapter::CELL; 31 | strncpy(_settings.cell.pin, pin, sizeof(_settings.cell.pin)-1); 32 | strncpy(_settings.cell.apn, apn, sizeof(_settings.cell.apn)-1); 33 | strncpy(_settings.cell.login, login, sizeof(_settings.cell.login)-1); 34 | strncpy(_settings.cell.pass, pass, sizeof(_settings.cell.pass)-1); 35 | 36 | } 37 | 38 | /****************************************************************************** 39 | PUBLIC MEMBER FUNCTIONS 40 | ******************************************************************************/ 41 | 42 | unsigned long CellularConnectionHandler::getTime() 43 | { 44 | return _cellular.getCellularTime().getUNIXTimestamp(); 45 | } 46 | 47 | UDP & CellularConnectionHandler::getUDP() 48 | { 49 | DEBUG_ERROR(F("CellularConnectionHandler has no UDP support")); 50 | while(1) {}; 51 | } 52 | 53 | /****************************************************************************** 54 | PROTECTED MEMBER FUNCTIONS 55 | ******************************************************************************/ 56 | 57 | NetworkConnectionState CellularConnectionHandler::update_handleInit() 58 | { 59 | _cellular.begin(); 60 | _cellular.setDebugStream(Serial); 61 | if (strlen(_settings.cell.pin) > 0 && !_cellular.unlockSIM(_settings.cell.pin)) { 62 | DEBUG_ERROR(F("SIM not present or wrong PIN")); 63 | return NetworkConnectionState::ERROR; 64 | } 65 | 66 | if (!_cellular.connect(String(_settings.cell.apn), String(_settings.cell.login), String(_settings.cell.pass))) { 67 | DEBUG_ERROR(F("The board was not able to register to the network...")); 68 | return NetworkConnectionState::ERROR; 69 | } 70 | DEBUG_INFO(F("Connected to Network")); 71 | return NetworkConnectionState::CONNECTING; 72 | } 73 | 74 | NetworkConnectionState CellularConnectionHandler::update_handleConnecting() 75 | { 76 | if (!_cellular.isConnectedToInternet()) { 77 | return NetworkConnectionState::INIT; 78 | } 79 | 80 | if (!_check_internet_availability) { 81 | return NetworkConnectionState::CONNECTED; 82 | } 83 | 84 | if(getTime() == 0){ 85 | DEBUG_ERROR(F("Internet check failed")); 86 | DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting); 87 | return NetworkConnectionState::CONNECTING; 88 | } 89 | 90 | return NetworkConnectionState::CONNECTED; 91 | } 92 | 93 | NetworkConnectionState CellularConnectionHandler::update_handleConnected() 94 | { 95 | if (!_cellular.isConnectedToInternet()) { 96 | return NetworkConnectionState::DISCONNECTED; 97 | } 98 | return NetworkConnectionState::CONNECTED; 99 | } 100 | 101 | NetworkConnectionState CellularConnectionHandler::update_handleDisconnecting() 102 | { 103 | return NetworkConnectionState::DISCONNECTED; 104 | } 105 | 106 | NetworkConnectionState CellularConnectionHandler::update_handleDisconnected() 107 | { 108 | if (_keep_alive) { 109 | return NetworkConnectionState::INIT; 110 | } 111 | return NetworkConnectionState::CLOSED; 112 | } 113 | 114 | #endif /* #ifdef BOARD_HAS_CELLULAR */ 115 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Arduino Library for network connections management 2 | ================================================== 3 | 4 | [![Check Arduino status](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/check-arduino.yml/badge.svg)](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/check-arduino.yml) 5 | [![Compile Examples status](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/compile-examples.yml/badge.svg)](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/compile-examples.yml) 6 | [![Spell Check status](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/spell-check.yml/badge.svg)](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/spell-check.yml) 7 | 8 | Library for handling and managing network connections by providing keep-alive functionality and automatic reconnection in case of connection-loss. It supports the following boards: 9 | 10 | * **WiFi**: [`MKR 1000`](https://store.arduino.cc/arduino-mkr1000-wifi), [`MKR WiFi 1010`](https://store.arduino.cc/arduino-mkr-wifi-1010), [`Nano 33 IoT`](https://store.arduino.cc/arduino-nano-33-iot), [`Portenta H7`](https://store.arduino.cc/products/portenta-h7), [`Nano RP2040 Connect`](https://store.arduino.cc/products/arduino-nano-rp2040-connect), [`Nicla Vision`](https://store.arduino.cc/products/nicla-vision), [`OPTA WiFi`](https://store.arduino.cc/products/opta-wifi), [`GIGA R1 WiFi`](https://store.arduino.cc/products/giga-r1-wifi), [`Portenta C33`](https://store.arduino.cc/products/portenta-c33), [`UNO R4 WiFi`](https://store.arduino.cc/products/uno-r4-wifi), [`Nano ESP32`](https://store.arduino.cc/products/nano-esp32), [`ESP8266`](https://github.com/esp8266/Arduino/releases/tag/2.5.0), [`ESP32`](https://github.com/espressif/arduino-esp32) 11 | * **GSM**: [`MKR GSM 1400`](https://store.arduino.cc/arduino-mkr-gsm-1400-1415) 12 | * **5G**: [`MKR NB 1500`](https://store.arduino.cc/arduino-mkr-nb-1500-1413) 13 | * **LoRa**: [`MKR WAN 1300/1310`](https://store.arduino.cc/mkr-wan-1310) 14 | * **Ethernet**: [`Portenta H7`](https://store.arduino.cc/products/portenta-h7) + [`Vision Shield Ethernet`](https://store.arduino.cc/products/arduino-portenta-vision-shield-ethernet), [`Max Carrier`](https://store.arduino.cc/products/portenta-max-carrier), [`Breakout`](https://store.arduino.cc/products/arduino-portenta-breakout), [`Portenta Machine Control`](https://store.arduino.cc/products/arduino-portenta-machine-control), [`OPTA WiFi`](https://store.arduino.cc/products/opta-wifi), [`OPTA RS485`](https://store.arduino.cc/products/opta-rs485), [`OPTA Lite`](https://store.arduino.cc/products/opta-lite), [`Portenta C33`](https://store.arduino.cc/products/portenta-c33) + [`Vision Shield Ethernet`](https://store.arduino.cc/products/arduino-portenta-vision-shield-ethernet) 15 | 16 | ### How-to-use 17 | 18 | ```C++ 19 | #include 20 | /* ... */ 21 | #if defined(BOARD_HAS_ETHERNET) 22 | EthernetConnectionHandler conMan; 23 | #elif defined(BOARD_HAS_WIFI) 24 | WiFiConnectionHandler conMan("SECRET_WIFI_SSID", "SECRET_WIFI_PASS"); 25 | #elif defined(BOARD_HAS_GSM) 26 | GSMConnectionHandler conMan("SECRET_PIN", "SECRET_APN", "SECRET_GSM_LOGIN", "SECRET_GSM_PASS"); 27 | #elif defined(BOARD_HAS_NB) 28 | NBConnectionHandler conMan("SECRET_PIN", "SECRET_APN", "SECRET_GSM_LOGIN", "SECRET_GSM_PASS"); 29 | #elif defined(BOARD_HAS_LORA) 30 | LoRaConnectionHandler conMan("SECRET_APP_EUI", "SECRET_APP_KEY"); 31 | #endif 32 | /* ... */ 33 | void setup() { 34 | Serial.begin(9600); 35 | while(!Serial) { } 36 | 37 | setDebugMessageLevel(DBG_INFO); 38 | 39 | conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); 40 | conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); 41 | conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); 42 | } 43 | 44 | void loop() { 45 | /* The following code keeps on running connection workflows on our 46 | * ConnectionHandler object, hence allowing reconnection in case of failure 47 | * and notification of connect/disconnect event if enabled (see 48 | * addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within 49 | * the loop or methods called from it will delay the execution of .check(), 50 | * which might not guarantee the correct functioning of the ConnectionHandler 51 | * object. 52 | */ 53 | conMan.check(); 54 | } 55 | /* ... */ 56 | void onNetworkConnect() { 57 | Serial.println(">>>> CONNECTED to network"); 58 | } 59 | 60 | void onNetworkDisconnect() { 61 | Serial.println(">>>> DISCONNECTED from network"); 62 | } 63 | 64 | void onNetworkError() { 65 | Serial.println(">>>> ERROR"); 66 | } 67 | ``` 68 | -------------------------------------------------------------------------------- /src/CatM1ConnectionHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2023 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "ConnectionHandlerDefinitions.h" 16 | 17 | #ifdef BOARD_HAS_CATM1_NBIOT /* Only compile if the board has CatM1 BN-IoT */ 18 | #include "CatM1ConnectionHandler.h" 19 | 20 | /****************************************************************************** 21 | CTOR/DTOR 22 | ******************************************************************************/ 23 | 24 | CatM1ConnectionHandler::CatM1ConnectionHandler() 25 | : ConnectionHandler(true, NetworkAdapter::CATM1) { } 26 | 27 | CatM1ConnectionHandler::CatM1ConnectionHandler( 28 | const char * pin, const char * apn, const char * login, const char * pass, 29 | RadioAccessTechnologyType rat, uint32_t band, bool const keep_alive) 30 | : ConnectionHandler{keep_alive, NetworkAdapter::CATM1} 31 | { 32 | _settings.type = NetworkAdapter::CATM1; 33 | // To keep the backward compatibility, the user can call enableCheckInternetAvailability(false) for disabling the check 34 | _check_internet_availability = true; 35 | strncpy(_settings.catm1.pin, pin, sizeof(_settings.catm1.pin)-1); 36 | strncpy(_settings.catm1.apn, apn, sizeof(_settings.catm1.apn)-1); 37 | strncpy(_settings.catm1.login, login, sizeof(_settings.catm1.login)-1); 38 | strncpy(_settings.catm1.pass, pass, sizeof(_settings.catm1.pass)-1); 39 | _settings.catm1.rat = static_cast(rat); 40 | _settings.catm1.band = band; 41 | _reset = false; 42 | } 43 | 44 | /****************************************************************************** 45 | PUBLIC MEMBER FUNCTIONS 46 | ******************************************************************************/ 47 | 48 | unsigned long CatM1ConnectionHandler::getTime() 49 | { 50 | /* It is not safe to call GSM.getTime() since we don't know if modem internal 51 | * RTC is in sync with current time. 52 | */ 53 | return 0; 54 | } 55 | 56 | /****************************************************************************** 57 | PROTECTED MEMBER FUNCTIONS 58 | ******************************************************************************/ 59 | 60 | NetworkConnectionState CatM1ConnectionHandler::update_handleInit() 61 | { 62 | #if defined (ARDUINO_EDGE_CONTROL) 63 | /* Power on module */ 64 | pinMode(ON_MKR2, OUTPUT); 65 | digitalWrite(ON_MKR2, HIGH); 66 | #endif 67 | 68 | if(!GSM.begin( 69 | _settings.catm1.pin, 70 | _settings.catm1.apn, 71 | _settings.catm1.login, 72 | _settings.catm1.pass, 73 | static_cast(_settings.catm1.rat) , 74 | _settings.catm1.band, 75 | _reset)) 76 | { 77 | DEBUG_ERROR(F("The board was not able to register to the network...")); 78 | _reset = true; 79 | return NetworkConnectionState::DISCONNECTED; 80 | } 81 | _reset = false; 82 | return NetworkConnectionState::CONNECTING; 83 | } 84 | 85 | NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting() 86 | { 87 | if (!GSM.isConnected()) 88 | { 89 | DEBUG_ERROR(F("GSM connection not alive... disconnecting")); 90 | return NetworkConnectionState::DISCONNECTED; 91 | } 92 | 93 | if(!_check_internet_availability){ 94 | return NetworkConnectionState::CONNECTED; 95 | } 96 | 97 | DEBUG_INFO(F("Sending PING to outer space...")); 98 | int const ping_result = GSM.ping("time.arduino.cc"); 99 | DEBUG_INFO(F("GSM.ping(): %d"), ping_result); 100 | if (ping_result < 0) 101 | { 102 | DEBUG_ERROR(F("Internet check failed")); 103 | DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting); 104 | return NetworkConnectionState::CONNECTING; 105 | } 106 | else 107 | { 108 | DEBUG_INFO(F("Connected to Internet")); 109 | return NetworkConnectionState::CONNECTED; 110 | } 111 | } 112 | 113 | NetworkConnectionState CatM1ConnectionHandler::update_handleConnected() 114 | { 115 | int const is_gsm_access_alive = GSM.isConnected(); 116 | if (is_gsm_access_alive != 1) 117 | { 118 | DEBUG_ERROR(F("GSM connection not alive... disconnecting")); 119 | return NetworkConnectionState::DISCONNECTED; 120 | } 121 | return NetworkConnectionState::CONNECTED; 122 | } 123 | 124 | NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnecting() 125 | { 126 | GSM.disconnect(); 127 | return NetworkConnectionState::DISCONNECTED; 128 | } 129 | 130 | NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnected() 131 | { 132 | GSM.end(); 133 | if (_keep_alive) 134 | { 135 | return NetworkConnectionState::INIT; 136 | } 137 | else 138 | { 139 | return NetworkConnectionState::CLOSED; 140 | } 141 | } 142 | 143 | #endif /* #ifdef BOARD_HAS_CATM1_NBIOT */ 144 | -------------------------------------------------------------------------------- /examples/GenericConnectionHandlerDemo/GenericConnectionHandlerDemo.ino: -------------------------------------------------------------------------------- 1 | /* SECRET_ fields are in `arduino_secrets.h` (included below) 2 | * 3 | * If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO 4 | * WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding 5 | * Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the 6 | * arduino_secrets.h file (or Secrets tab in Create Web Editor). 7 | * 8 | * WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 9 | * 10 | * If using a MKR GSM 1400 or other GSM boards supporting the same API you'll 11 | * need a GSMConnectionHandler object as follows 12 | * 13 | * GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 14 | * 15 | * If using a MKR NB1500 you'll need a NBConnectionHandler object as follows 16 | * 17 | * NBConnectionHandler conMan(SECRET_PIN); 18 | * 19 | * If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows: 20 | * 21 | * DHCP mode 22 | * EthernetConnectionHandler conMan; 23 | * 24 | * Manual configuration 25 | * EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); 26 | * 27 | * Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE. 28 | * 29 | */ 30 | 31 | #include 32 | 33 | #include "arduino_secrets.h" 34 | 35 | #define CONN_TOGGLE_MS 60000 36 | 37 | #if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \ 38 | defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)) 39 | #error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md" 40 | #endif 41 | 42 | GenericConnectionHandler conMan; 43 | 44 | 45 | bool attemptConnect = false; 46 | uint32_t lastConnToggleMs = 0; 47 | 48 | void setup() { 49 | /* Initialize serial debug port and wait up to 5 seconds for port to open */ 50 | Serial.begin(9600); 51 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 52 | 53 | #ifndef __AVR__ 54 | /* Set the debug message level: 55 | * - DBG_ERROR: Only show error messages 56 | * - DBG_WARNING: Show warning and error messages 57 | * - DBG_INFO: Show info, warning, and error messages 58 | * - DBG_DEBUG: Show debug, info, warning, and error messages 59 | * - DBG_VERBOSE: Show all messages 60 | */ 61 | setDebugMessageLevel(DBG_INFO); 62 | #endif 63 | 64 | models::NetworkSetting setting = models::settingsDefault(NetworkAdapter::WIFI); 65 | 66 | strcpy(setting.wifi.ssid, SECRET_WIFI_SSID); 67 | strcpy(setting.wifi.pwd, SECRET_WIFI_PASS); 68 | 69 | /* Add callbacks to the ConnectionHandler object to get notified of network 70 | * connection events. */ 71 | conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); 72 | conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); 73 | conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); 74 | 75 | conMan.updateSetting(setting); 76 | Serial.print("Network Adapter Interface: "); 77 | switch (conMan.getInterface()) { 78 | case NetworkAdapter::WIFI: 79 | Serial.println("Wi-Fi"); 80 | break; 81 | case NetworkAdapter::ETHERNET: 82 | Serial.println("Ethernet"); 83 | break; 84 | case NetworkAdapter::NB: 85 | Serial.println("Narrowband"); 86 | break; 87 | case NetworkAdapter::GSM: 88 | Serial.println("GSM"); 89 | break; 90 | case NetworkAdapter::LORA: 91 | Serial.println("LoRa"); 92 | break; 93 | case NetworkAdapter::CATM1: 94 | Serial.println("Category M1"); 95 | break; 96 | case NetworkAdapter::CELL: 97 | Serial.println("Cellular"); 98 | break; 99 | default: 100 | Serial.println("Unknown"); 101 | break; 102 | } 103 | } 104 | 105 | void loop() { 106 | /* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */ 107 | if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) { 108 | Serial.println("Toggling connection..."); 109 | if (attemptConnect) { 110 | conMan.connect(); 111 | } else { 112 | conMan.disconnect(); 113 | } 114 | attemptConnect = !attemptConnect; 115 | lastConnToggleMs = millis(); 116 | } 117 | 118 | /* The following code keeps on running connection workflows on our 119 | * ConnectionHandler object, hence allowing reconnection in case of failure 120 | * and notification of connect/disconnect event if enabled (see 121 | * addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within 122 | * the loop or methods called from it will delay the execution of .update(), 123 | * which might not guarantee the correct functioning of the ConnectionHandler 124 | * object. 125 | */ 126 | conMan.check(); 127 | } 128 | 129 | void onNetworkConnect() { 130 | Serial.println(">>>> CONNECTED to network"); 131 | } 132 | 133 | void onNetworkDisconnect() { 134 | Serial.println(">>>> DISCONNECTED from network"); 135 | } 136 | 137 | void onNetworkError() { 138 | Serial.println(">>>> ERROR"); 139 | } 140 | -------------------------------------------------------------------------------- /src/ConnectionHandlerInterface.h: -------------------------------------------------------------------------------- 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 | #pragma once 12 | 13 | /****************************************************************************** 14 | INCLUDES 15 | ******************************************************************************/ 16 | 17 | #if !defined(__AVR__) 18 | # include 19 | #endif 20 | 21 | #include 22 | #include 23 | #include 24 | #include "ConnectionHandlerDefinitions.h" 25 | #include "connectionHandlerModels/settings.h" 26 | 27 | #include 28 | 29 | /****************************************************************************** 30 | TYPEDEFS 31 | ******************************************************************************/ 32 | 33 | typedef void (*OnNetworkEventCallback)(); 34 | 35 | /****************************************************************************** 36 | CLASS DECLARATION 37 | ******************************************************************************/ 38 | 39 | // forward declaration FIXME 40 | class GenericConnectionHandler; 41 | 42 | class ConnectionHandler { 43 | public: 44 | 45 | ConnectionHandler(bool const keep_alive=true, NetworkAdapter interface=NetworkAdapter::NONE); 46 | 47 | virtual ~ConnectionHandler() {} 48 | 49 | virtual NetworkConnectionState check(); 50 | 51 | #if not defined(BOARD_HAS_LORA) 52 | virtual unsigned long getTime() = 0; 53 | #endif 54 | 55 | #if defined(BOARD_HAS_NOTECARD) || defined(BOARD_HAS_LORA) 56 | virtual bool available() = 0; 57 | virtual int read() = 0; 58 | virtual int write(const uint8_t *buf, size_t size) = 0; 59 | #else 60 | virtual Client &getClient() = 0; 61 | virtual UDP &getUDP() = 0; 62 | #endif 63 | 64 | NetworkConnectionState getStatus() __attribute__((deprecated)) { 65 | return _current_net_connection_state; 66 | } 67 | 68 | NetworkAdapter getInterface() { 69 | return _interface; 70 | } 71 | 72 | virtual void connect(); 73 | virtual void disconnect(); 74 | void enableCheckInternetAvailability(bool enable) { 75 | _check_internet_availability = enable; 76 | } 77 | 78 | virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback); 79 | void addConnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); 80 | void addDisconnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); 81 | void addErrorCallback(OnNetworkEventCallback callback) __attribute__((deprecated)); 82 | 83 | /** 84 | * Update the interface settings. This can be performed only when the interface is 85 | * in INIT state. otherwise nothing is performed. The type of the interface should match 86 | * the type of the settings provided 87 | * 88 | * @return true if the update is successful, false otherwise 89 | */ 90 | virtual bool updateSetting(const models::NetworkSetting& s) { 91 | if(_current_net_connection_state == NetworkConnectionState::INIT && s.type == _interface) { 92 | memcpy(&_settings, &s, sizeof(s)); 93 | return true; 94 | } 95 | 96 | return false; 97 | } 98 | 99 | virtual void getSetting(models::NetworkSetting& s) { 100 | memcpy(&s, &_settings, sizeof(s)); 101 | return; 102 | } 103 | 104 | virtual void setKeepAlive(bool keep_alive=true) { this->_keep_alive = keep_alive; } 105 | 106 | inline void updateTimeoutTable(const TimeoutTable& t) { _timeoutTable = t; } 107 | inline void updateTimeoutTable(TimeoutTable&& t) { _timeoutTable = std::move(t); } 108 | inline void updateTimeoutInterval(NetworkConnectionState state, uint32_t interval) { 109 | _timeoutTable.intervals[static_cast(state)] = interval; 110 | } 111 | protected: 112 | 113 | virtual NetworkConnectionState updateConnectionState(); 114 | virtual void updateCallback(NetworkConnectionState next_net_connection_state); 115 | 116 | bool _keep_alive; 117 | bool _check_internet_availability; 118 | NetworkAdapter _interface; 119 | 120 | virtual NetworkConnectionState update_handleInit () = 0; 121 | virtual NetworkConnectionState update_handleConnecting () = 0; 122 | virtual NetworkConnectionState update_handleConnected () = 0; 123 | virtual NetworkConnectionState update_handleDisconnecting() = 0; 124 | virtual NetworkConnectionState update_handleDisconnected () = 0; 125 | 126 | models::NetworkSetting _settings; 127 | 128 | TimeoutTable _timeoutTable; 129 | private: 130 | 131 | unsigned long _lastConnectionTickTime; 132 | NetworkConnectionState _current_net_connection_state; 133 | OnNetworkEventCallback _on_connect_event_callback = NULL, 134 | _on_disconnect_event_callback = NULL, 135 | _on_error_event_callback = NULL; 136 | 137 | friend GenericConnectionHandler; 138 | }; 139 | -------------------------------------------------------------------------------- /src/NBConnectionHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "ConnectionHandlerDefinitions.h" 16 | 17 | #ifdef BOARD_HAS_NB /* Only compile if this is a board with NB */ 18 | #include "NBConnectionHandler.h" 19 | 20 | /****************************************************************************** 21 | CONSTANTS 22 | ******************************************************************************/ 23 | 24 | static int const NB_TIMEOUT = 30000; 25 | 26 | /****************************************************************************** 27 | FUNCTION DEFINITION 28 | ******************************************************************************/ 29 | 30 | __attribute__((weak)) void mkr_nb_feed_watchdog() 31 | { 32 | /* This function can be overwritten by a "strong" implementation 33 | * in a higher level application, such as the ArduinoIoTCloud 34 | * firmware stack. 35 | */ 36 | } 37 | 38 | /****************************************************************************** 39 | CTOR/DTOR 40 | ******************************************************************************/ 41 | 42 | NBConnectionHandler::NBConnectionHandler() 43 | : ConnectionHandler(true, NetworkAdapter::NB) {} 44 | 45 | NBConnectionHandler::NBConnectionHandler(char const * pin, bool const keep_alive) 46 | : NBConnectionHandler(pin, "", keep_alive) 47 | { 48 | 49 | } 50 | 51 | NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, bool const keep_alive) 52 | : NBConnectionHandler(pin, apn, "", "", keep_alive) 53 | { 54 | 55 | } 56 | 57 | NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, char const * login, char const * pass, bool const keep_alive) 58 | : ConnectionHandler{keep_alive, NetworkAdapter::NB} 59 | { 60 | _settings.type = NetworkAdapter::NB; 61 | strncpy(_settings.nb.pin, pin, sizeof(_settings.nb.pin)-1); 62 | strncpy(_settings.nb.apn, apn, sizeof(_settings.nb.apn)-1); 63 | strncpy(_settings.nb.login, login, sizeof(_settings.nb.login)-1); 64 | strncpy(_settings.nb.pass, pass, sizeof(_settings.nb.pass)-1); 65 | } 66 | 67 | /****************************************************************************** 68 | PUBLIC MEMBER FUNCTIONS 69 | ******************************************************************************/ 70 | 71 | unsigned long NBConnectionHandler::getTime() 72 | { 73 | return _nb.getTime(); 74 | } 75 | 76 | /****************************************************************************** 77 | PRIVATE MEMBER FUNCTIONS 78 | ******************************************************************************/ 79 | 80 | NetworkConnectionState NBConnectionHandler::update_handleInit() 81 | { 82 | mkr_nb_feed_watchdog(); 83 | 84 | if (_nb.begin(_settings.nb.pin, 85 | _settings.nb.apn, 86 | _settings.nb.login, 87 | _settings.nb.pass) == NB_READY) 88 | { 89 | DEBUG_INFO(F("SIM card ok")); 90 | _nb.setTimeout(NB_TIMEOUT); 91 | return NetworkConnectionState::CONNECTING; 92 | } 93 | else 94 | { 95 | DEBUG_ERROR(F("SIM not present or wrong PIN")); 96 | return NetworkConnectionState::ERROR; 97 | } 98 | } 99 | 100 | NetworkConnectionState NBConnectionHandler::update_handleConnecting() 101 | { 102 | NB_NetworkStatus_t const network_status = _nb_gprs.attachGPRS(true); 103 | DEBUG_DEBUG(F("GPRS.attachGPRS(): %d"), network_status); 104 | if (network_status == NB_NetworkStatus_t::NB_ERROR) 105 | { 106 | DEBUG_ERROR(F("GPRS.attachGPRS() failed")); 107 | return NetworkConnectionState::ERROR; 108 | } 109 | else 110 | { 111 | DEBUG_INFO(F("Connected to GPRS Network")); 112 | return NetworkConnectionState::CONNECTED; 113 | } 114 | } 115 | 116 | NetworkConnectionState NBConnectionHandler::update_handleConnected() 117 | { 118 | int const nb_is_access_alive = _nb.isAccessAlive(); 119 | DEBUG_VERBOSE(F("GPRS.isAccessAlive(): %d"), nb_is_access_alive); 120 | if (nb_is_access_alive != 1) 121 | { 122 | DEBUG_INFO(F("Disconnected from cellular network")); 123 | return NetworkConnectionState::DISCONNECTED; 124 | } 125 | else 126 | { 127 | DEBUG_VERBOSE(F("Connected to Cellular Network")); 128 | return NetworkConnectionState::CONNECTED; 129 | } 130 | } 131 | 132 | NetworkConnectionState NBConnectionHandler::update_handleDisconnecting() 133 | { 134 | DEBUG_VERBOSE(F("Disconnecting from Cellular Network")); 135 | _nb.shutdown(); 136 | return NetworkConnectionState::DISCONNECTED; 137 | } 138 | 139 | NetworkConnectionState NBConnectionHandler::update_handleDisconnected() 140 | { 141 | if (_keep_alive) 142 | { 143 | return NetworkConnectionState::INIT; 144 | } 145 | else 146 | { 147 | return NetworkConnectionState::CLOSED; 148 | } 149 | } 150 | 151 | #endif /* #ifdef BOARD_HAS_NB */ 152 | -------------------------------------------------------------------------------- /.github/workflows/sync-labels.yml: -------------------------------------------------------------------------------- 1 | # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/sync-labels.md 2 | name: Sync Labels 3 | 4 | # See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows 5 | on: 6 | push: 7 | paths: 8 | - ".github/workflows/sync-labels.ya?ml" 9 | - ".github/label-configuration-files/*.ya?ml" 10 | pull_request: 11 | paths: 12 | - ".github/workflows/sync-labels.ya?ml" 13 | - ".github/label-configuration-files/*.ya?ml" 14 | schedule: 15 | # Run daily at 8 AM UTC to sync with changes to shared label configurations. 16 | - cron: "0 8 * * *" 17 | workflow_dispatch: 18 | repository_dispatch: 19 | 20 | env: 21 | CONFIGURATIONS_FOLDER: .github/label-configuration-files 22 | CONFIGURATIONS_ARTIFACT: label-configuration-files 23 | 24 | jobs: 25 | check: 26 | runs-on: ubuntu-latest 27 | 28 | steps: 29 | - name: Checkout repository 30 | uses: actions/checkout@v6 31 | 32 | - name: Download JSON schema for labels configuration file 33 | id: download-schema 34 | uses: carlosperate/download-file-action@v2 35 | with: 36 | file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json 37 | location: ${{ runner.temp }}/label-configuration-schema 38 | 39 | - name: Install JSON schema validator 40 | run: | 41 | sudo npm install \ 42 | --global \ 43 | ajv-cli \ 44 | ajv-formats 45 | 46 | - name: Validate local labels configuration 47 | run: | 48 | # See: https://github.com/ajv-validator/ajv-cli#readme 49 | ajv validate \ 50 | --all-errors \ 51 | -c ajv-formats \ 52 | -s "${{ steps.download-schema.outputs.file-path }}" \ 53 | -d "${{ env.CONFIGURATIONS_FOLDER }}/*.{yml,yaml}" 54 | 55 | download: 56 | needs: check 57 | runs-on: ubuntu-latest 58 | 59 | strategy: 60 | matrix: 61 | filename: 62 | # Filenames of the shared configurations to apply to the repository in addition to the local configuration. 63 | # https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/sync-labels 64 | - universal.yml 65 | 66 | steps: 67 | - name: Download 68 | uses: carlosperate/download-file-action@v2 69 | with: 70 | file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/${{ matrix.filename }} 71 | 72 | - name: Pass configuration files to next job via workflow artifact 73 | uses: actions/upload-artifact@v6 74 | with: 75 | path: | 76 | *.yaml 77 | *.yml 78 | if-no-files-found: error 79 | name: ${{ env.CONFIGURATIONS_ARTIFACT }} 80 | 81 | sync: 82 | needs: download 83 | runs-on: ubuntu-latest 84 | 85 | steps: 86 | - name: Set environment variables 87 | run: | 88 | # See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable 89 | echo "MERGED_CONFIGURATION_PATH=${{ runner.temp }}/labels.yml" >> "$GITHUB_ENV" 90 | 91 | - name: Determine whether to dry run 92 | id: dry-run 93 | if: > 94 | github.event_name == 'pull_request' || 95 | ( 96 | ( 97 | github.event_name == 'push' || 98 | github.event_name == 'workflow_dispatch' 99 | ) && 100 | github.ref != format('refs/heads/{0}', github.event.repository.default_branch) 101 | ) 102 | run: | 103 | # Use of this flag in the github-label-sync command will cause it to only check the validity of the 104 | # configuration. 105 | echo "::set-output name=flag::--dry-run" 106 | 107 | - name: Checkout repository 108 | uses: actions/checkout@v6 109 | 110 | - name: Download configuration files artifact 111 | uses: actions/download-artifact@v7 112 | with: 113 | name: ${{ env.CONFIGURATIONS_ARTIFACT }} 114 | path: ${{ env.CONFIGURATIONS_FOLDER }} 115 | 116 | - name: Remove unneeded artifact 117 | uses: geekyeggo/delete-artifact@v5 118 | with: 119 | name: ${{ env.CONFIGURATIONS_ARTIFACT }} 120 | 121 | - name: Merge label configuration files 122 | run: | 123 | # Merge all configuration files 124 | shopt -s extglob 125 | cat "${{ env.CONFIGURATIONS_FOLDER }}"/*.@(yml|yaml) > "${{ env.MERGED_CONFIGURATION_PATH }}" 126 | 127 | - name: Install github-label-sync 128 | run: sudo npm install --global github-label-sync 129 | 130 | - name: Sync labels 131 | env: 132 | GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} 133 | run: | 134 | # See: https://github.com/Financial-Times/github-label-sync 135 | github-label-sync \ 136 | --labels "${{ env.MERGED_CONFIGURATION_PATH }}" \ 137 | ${{ steps.dry-run.outputs.flag }} \ 138 | ${{ github.repository }} 139 | -------------------------------------------------------------------------------- /src/GSMConnectionHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "ConnectionHandlerDefinitions.h" 16 | 17 | #ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */ 18 | #include "GSMConnectionHandler.h" 19 | 20 | /****************************************************************************** 21 | CONSTANTS 22 | ******************************************************************************/ 23 | 24 | static int const GSM_TIMEOUT = 30000; 25 | static int const GPRS_TIMEOUT = 30000; 26 | 27 | /****************************************************************************** 28 | FUNCTION DEFINITION 29 | ******************************************************************************/ 30 | 31 | __attribute__((weak)) void mkr_gsm_feed_watchdog() 32 | { 33 | /* This function can be overwritten by a "strong" implementation 34 | * in a higher level application, such as the ArduinoIoTCloud 35 | * firmware stack. 36 | */ 37 | } 38 | 39 | /****************************************************************************** 40 | CTOR/DTOR 41 | ******************************************************************************/ 42 | GSMConnectionHandler::GSMConnectionHandler() 43 | : ConnectionHandler(true, NetworkAdapter::GSM) {} 44 | 45 | GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive) 46 | : ConnectionHandler{keep_alive, NetworkAdapter::GSM} 47 | { 48 | _settings.type = NetworkAdapter::GSM; 49 | // To keep the backward compatibility, the user can call enableCheckInternetAvailability(false) for disabling the check 50 | _check_internet_availability = true; 51 | strncpy(_settings.gsm.pin, pin, sizeof(_settings.gsm.pin)-1); 52 | strncpy(_settings.gsm.apn, apn, sizeof(_settings.gsm.apn)-1); 53 | strncpy(_settings.gsm.login, login, sizeof(_settings.gsm.login)-1); 54 | strncpy(_settings.gsm.pass, pass, sizeof(_settings.gsm.pass)-1); 55 | } 56 | 57 | /****************************************************************************** 58 | PUBLIC MEMBER FUNCTIONS 59 | ******************************************************************************/ 60 | 61 | unsigned long GSMConnectionHandler::getTime() 62 | { 63 | return _gsm.getTime(); 64 | } 65 | 66 | /****************************************************************************** 67 | PROTECTED MEMBER FUNCTIONS 68 | ******************************************************************************/ 69 | 70 | NetworkConnectionState GSMConnectionHandler::update_handleInit() 71 | { 72 | mkr_gsm_feed_watchdog(); 73 | 74 | if (_gsm.begin(_settings.gsm.pin) != GSM_READY) 75 | { 76 | DEBUG_ERROR(F("SIM not present or wrong PIN")); 77 | return NetworkConnectionState::ERROR; 78 | } 79 | 80 | mkr_gsm_feed_watchdog(); 81 | 82 | DEBUG_INFO(F("SIM card ok")); 83 | _gsm.setTimeout(GSM_TIMEOUT); 84 | _gprs.setTimeout(GPRS_TIMEOUT); 85 | 86 | mkr_gsm_feed_watchdog(); 87 | 88 | GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS( 89 | _settings.gsm.apn, _settings.gsm.login, _settings.gsm.pass, true); 90 | DEBUG_DEBUG(F("GPRS.attachGPRS(): %d"), network_status); 91 | if (network_status == GSM3_NetworkStatus_t::ERROR) 92 | { 93 | DEBUG_ERROR(F("GPRS attach failed")); 94 | DEBUG_ERROR(F("Make sure the antenna is connected and reset your board.")); 95 | return NetworkConnectionState::ERROR; 96 | } 97 | 98 | return NetworkConnectionState::CONNECTING; 99 | } 100 | 101 | NetworkConnectionState GSMConnectionHandler::update_handleConnecting() 102 | { 103 | if(!_check_internet_availability){ 104 | return NetworkConnectionState::CONNECTED; 105 | } 106 | 107 | DEBUG_INFO(F("Sending PING to outer space...")); 108 | int const ping_result = _gprs.ping("time.arduino.cc"); 109 | DEBUG_INFO(F("GPRS.ping(): %d"), ping_result); 110 | if (ping_result < 0) 111 | { 112 | DEBUG_ERROR(F("PING failed")); 113 | DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting); 114 | return NetworkConnectionState::CONNECTING; 115 | } 116 | else 117 | { 118 | DEBUG_INFO(F("Connected to GPRS Network")); 119 | return NetworkConnectionState::CONNECTED; 120 | } 121 | } 122 | 123 | NetworkConnectionState GSMConnectionHandler::update_handleConnected() 124 | { 125 | int const is_gsm_access_alive = _gsm.isAccessAlive(); 126 | if (is_gsm_access_alive != 1) 127 | { 128 | return NetworkConnectionState::DISCONNECTED; 129 | } 130 | return NetworkConnectionState::CONNECTED; 131 | } 132 | 133 | NetworkConnectionState GSMConnectionHandler::update_handleDisconnecting() 134 | { 135 | _gsm.shutdown(); 136 | return NetworkConnectionState::DISCONNECTED; 137 | } 138 | 139 | NetworkConnectionState GSMConnectionHandler::update_handleDisconnected() 140 | { 141 | if (_keep_alive) 142 | { 143 | return NetworkConnectionState::INIT; 144 | } 145 | else 146 | { 147 | return NetworkConnectionState::CLOSED; 148 | } 149 | } 150 | 151 | #endif /* #ifdef BOARD_HAS_GSM */ 152 | -------------------------------------------------------------------------------- /examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino: -------------------------------------------------------------------------------- 1 | /* SECRET_ fields are in `arduino_secrets.h` (included below) 2 | * 3 | * If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO 4 | * WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding 5 | * Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the 6 | * arduino_secrets.h file (or Secrets tab in Create Web Editor). 7 | * 8 | * WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 9 | * 10 | * If using a MKR GSM 1400 or other GSM boards supporting the same API you'll 11 | * need a GSMConnectionHandler object as follows 12 | * 13 | * GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 14 | * 15 | * If using a MKR NB1500 you'll need a NBConnectionHandler object as follows 16 | * 17 | * NBConnectionHandler conMan(SECRET_PIN); 18 | * 19 | * If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows: 20 | * 21 | * DHCP mode 22 | * EthernetConnectionHandler conMan; 23 | * 24 | * Manual configuration 25 | * EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); 26 | * 27 | * Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE. 28 | * 29 | */ 30 | 31 | #include 32 | 33 | #include "arduino_secrets.h" 34 | 35 | #define CONN_TOGGLE_MS 60000 36 | 37 | #if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \ 38 | defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)) 39 | #error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md" 40 | #endif 41 | 42 | #if defined(BOARD_HAS_ETHERNET) 43 | EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); 44 | #elif defined(BOARD_HAS_WIFI) 45 | WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 46 | #elif defined(BOARD_HAS_GSM) 47 | GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 48 | #elif defined(BOARD_HAS_NB) 49 | NBConnectionHandler conMan(SECRET_PIN); 50 | #elif defined(BOARD_HAS_LORA) 51 | LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY); 52 | #elif defined(BOARD_HAS_CATM1_NBIOT) 53 | CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 54 | #elif defined(BOARD_HAS_CELLULAR) 55 | CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 56 | #endif 57 | 58 | bool attemptConnect = false; 59 | uint32_t lastConnToggleMs = 0; 60 | 61 | void setup() { 62 | /* Initialize serial debug port and wait up to 5 seconds for port to open */ 63 | Serial.begin(9600); 64 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 65 | 66 | #ifndef __AVR__ 67 | /* Set the debug message level: 68 | * - DBG_ERROR: Only show error messages 69 | * - DBG_WARNING: Show warning and error messages 70 | * - DBG_INFO: Show info, warning, and error messages 71 | * - DBG_DEBUG: Show debug, info, warning, and error messages 72 | * - DBG_VERBOSE: Show all messages 73 | */ 74 | setDebugMessageLevel(DBG_INFO); 75 | #endif 76 | 77 | /* Add callbacks to the ConnectionHandler object to get notified of network 78 | * connection events. */ 79 | conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); 80 | conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); 81 | conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); 82 | 83 | Serial.print("Network Adapter Interface: "); 84 | switch (conMan.getInterface()) { 85 | case NetworkAdapter::WIFI: 86 | Serial.println("Wi-Fi"); 87 | break; 88 | case NetworkAdapter::ETHERNET: 89 | Serial.println("Ethernet"); 90 | break; 91 | case NetworkAdapter::NB: 92 | Serial.println("Narrowband"); 93 | break; 94 | case NetworkAdapter::GSM: 95 | Serial.println("GSM"); 96 | break; 97 | case NetworkAdapter::LORA: 98 | Serial.println("LoRa"); 99 | break; 100 | case NetworkAdapter::CATM1: 101 | Serial.println("Category M1"); 102 | break; 103 | case NetworkAdapter::CELL: 104 | Serial.println("Cellular"); 105 | break; 106 | default: 107 | Serial.println("Unknown"); 108 | break; 109 | } 110 | } 111 | 112 | void loop() { 113 | /* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */ 114 | if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) { 115 | Serial.println("Toggling connection..."); 116 | if (attemptConnect) { 117 | conMan.connect(); 118 | } else { 119 | conMan.disconnect(); 120 | } 121 | attemptConnect = !attemptConnect; 122 | lastConnToggleMs = millis(); 123 | } 124 | 125 | /* The following code keeps on running connection workflows on our 126 | * ConnectionHandler object, hence allowing reconnection in case of failure 127 | * and notification of connect/disconnect event if enabled (see 128 | * addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within 129 | * the loop or methods called from it will delay the execution of .update(), 130 | * which might not guarantee the correct functioning of the ConnectionHandler 131 | * object. 132 | */ 133 | conMan.check(); 134 | } 135 | 136 | void onNetworkConnect() { 137 | Serial.println(">>>> CONNECTED to network"); 138 | } 139 | 140 | void onNetworkDisconnect() { 141 | Serial.println(">>>> DISCONNECTED from network"); 142 | } 143 | 144 | void onNetworkError() { 145 | Serial.println(">>>> ERROR"); 146 | } 147 | -------------------------------------------------------------------------------- /src/GenericConnectionHandler.cpp: -------------------------------------------------------------------------------- 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "GenericConnectionHandler.h" 16 | #include "Arduino_ConnectionHandler.h" 17 | 18 | static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter); 19 | 20 | bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) { 21 | if(_ch != nullptr && _ch->_current_net_connection_state != NetworkConnectionState::INIT) { 22 | // If the internal connection handler is already being used and not in INIT phase we cannot update the settings 23 | return false; 24 | } else if(_ch != nullptr && _ch->_current_net_connection_state == NetworkConnectionState::INIT && _interface != s.type) { 25 | // If the internal connection handler is already being used and in INIT phase and the interface type is being changed 26 | // -> we need to deallocate the previously allocated handler 27 | 28 | // if interface type is not being changed -> we just need to call updateSettings 29 | delete _ch; 30 | _ch = nullptr; 31 | } 32 | 33 | if(_ch == nullptr) { 34 | _ch = instantiate_handler(s.type); 35 | } 36 | 37 | if(_ch != nullptr) { 38 | _interface = s.type; 39 | _ch->setKeepAlive(_keep_alive); 40 | _ch->enableCheckInternetAvailability(_check_internet_availability); 41 | return _ch->updateSetting(s); 42 | } else { 43 | _interface = NetworkAdapter::NONE; 44 | 45 | return false; 46 | } 47 | } 48 | 49 | void GenericConnectionHandler::getSetting(models::NetworkSetting& s) { 50 | if(_ch != nullptr) { 51 | _ch->getSetting(s); 52 | } else { 53 | s.type = NetworkAdapter::NONE; 54 | } 55 | } 56 | 57 | NetworkConnectionState GenericConnectionHandler::updateConnectionState() { 58 | return _ch != nullptr ? _ch->updateConnectionState() : NetworkConnectionState::INIT; 59 | } 60 | 61 | NetworkConnectionState GenericConnectionHandler::update_handleInit() { 62 | return _ch != nullptr ? _ch->update_handleInit() : NetworkConnectionState::INIT; 63 | } 64 | 65 | NetworkConnectionState GenericConnectionHandler::update_handleConnecting() { 66 | return _ch != nullptr ? _ch->update_handleConnecting() : NetworkConnectionState::INIT; 67 | } 68 | 69 | NetworkConnectionState GenericConnectionHandler::update_handleConnected() { 70 | return _ch != nullptr ? _ch->update_handleConnected() : NetworkConnectionState::INIT; 71 | } 72 | 73 | NetworkConnectionState GenericConnectionHandler::update_handleDisconnecting() { 74 | return _ch != nullptr ? _ch->update_handleDisconnecting() : NetworkConnectionState::INIT; 75 | } 76 | 77 | NetworkConnectionState GenericConnectionHandler::update_handleDisconnected() { 78 | return _ch != nullptr ? _ch->update_handleDisconnected() : NetworkConnectionState::INIT; 79 | } 80 | 81 | #if not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD)) 82 | unsigned long GenericConnectionHandler::getTime() { 83 | return _ch != nullptr ? _ch->getTime() : 0; 84 | } 85 | 86 | Client & GenericConnectionHandler::getClient() { 87 | return _ch->getClient(); // NOTE _ch may be nullptr 88 | } 89 | 90 | UDP & GenericConnectionHandler::getUDP() { 91 | return _ch->getUDP(); // NOTE _ch may be nullptr 92 | } 93 | 94 | #endif // not (defined(BOARD_HAS_LORA) or defined(BOARD_HAS_NOTECARD)) 95 | 96 | void GenericConnectionHandler::connect() { 97 | if(_ch!=nullptr) { 98 | _ch->connect(); 99 | } 100 | ConnectionHandler::connect(); 101 | } 102 | 103 | void GenericConnectionHandler::disconnect() { 104 | if(_ch!=nullptr) { 105 | _ch->disconnect(); 106 | } 107 | ConnectionHandler::disconnect(); 108 | } 109 | 110 | void GenericConnectionHandler::setKeepAlive(bool keep_alive) { 111 | _keep_alive = keep_alive; 112 | 113 | if(_ch!=nullptr) { 114 | _ch->setKeepAlive(keep_alive); 115 | } 116 | } 117 | 118 | static inline ConnectionHandler* instantiate_handler(NetworkAdapter adapter) { 119 | switch(adapter) { 120 | #if defined(BOARD_HAS_WIFI) 121 | case NetworkAdapter::WIFI: 122 | return new WiFiConnectionHandler(); 123 | break; 124 | #endif 125 | 126 | #if defined(BOARD_HAS_ETHERNET) 127 | case NetworkAdapter::ETHERNET: 128 | return new EthernetConnectionHandler(); 129 | break; 130 | #endif 131 | 132 | #if defined(BOARD_HAS_NB) 133 | case NetworkAdapter::NB: 134 | return new NBConnectionHandler(); 135 | break; 136 | #endif 137 | 138 | #if defined(BOARD_HAS_GSM) 139 | case NetworkAdapter::GSM: 140 | return new GSMConnectionHandler(); 141 | break; 142 | #endif 143 | 144 | #if defined(BOARD_HAS_CATM1_NBIOT) 145 | case NetworkAdapter::CATM1: 146 | return new CatM1ConnectionHandler(); 147 | break; 148 | #endif 149 | 150 | #if defined(BOARD_HAS_CELLULAR) 151 | case NetworkAdapter::CELL: 152 | return new CellularConnectionHandler(); 153 | break; 154 | #endif 155 | 156 | default: 157 | DEBUG_ERROR("Network adapter not supported by this platform: %d", adapter); 158 | return nullptr; 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /examples/CheckInternetAvailabilityDemo/CheckInternetAvailabilityDemo.ino: -------------------------------------------------------------------------------- 1 | /* SECRET_ fields are in `arduino_secrets.h` (included below) 2 | * 3 | * If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO 4 | * WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding 5 | * Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the 6 | * arduino_secrets.h file (or Secrets tab in Create Web Editor). 7 | * 8 | * WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 9 | * 10 | * If using a MKR GSM 1400 or other GSM boards supporting the same API you'll 11 | * need a GSMConnectionHandler object as follows 12 | * 13 | * GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 14 | * 15 | * If using a MKR NB1500 you'll need a NBConnectionHandler object as follows 16 | * 17 | * NBConnectionHandler conMan(SECRET_PIN); 18 | * 19 | * If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows: 20 | * 21 | * DHCP mode 22 | * EthernetConnectionHandler conMan; 23 | * 24 | * Manual configuration 25 | * EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); 26 | * 27 | * Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE. 28 | * 29 | * This sketch enables the ConnectionHandler to check for internet availability (only for IP based connectivity) 30 | * before reporting the Connected state. By default the check is disabled. 31 | * 32 | */ 33 | 34 | #include 35 | 36 | #include "arduino_secrets.h" 37 | 38 | #define CONN_TOGGLE_MS 60000 39 | 40 | #if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \ 41 | defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)) 42 | #error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md" 43 | #endif 44 | 45 | #if defined(BOARD_HAS_ETHERNET) 46 | EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK); 47 | #elif defined(BOARD_HAS_WIFI) 48 | WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 49 | #elif defined(BOARD_HAS_GSM) 50 | GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 51 | #elif defined(BOARD_HAS_NB) 52 | NBConnectionHandler conMan(SECRET_PIN); 53 | #elif defined(BOARD_HAS_LORA) 54 | LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY); 55 | #elif defined(BOARD_HAS_CATM1_NBIOT) 56 | CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 57 | #elif defined(BOARD_HAS_CELLULAR) 58 | CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS); 59 | #endif 60 | 61 | bool attemptConnect = false; 62 | uint32_t lastConnToggleMs = 0; 63 | 64 | void setup() { 65 | /* Initialize serial debug port and wait up to 5 seconds for port to open */ 66 | Serial.begin(9600); 67 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 68 | 69 | #ifndef __AVR__ 70 | /* Set the debug message level: 71 | * - DBG_ERROR: Only show error messages 72 | * - DBG_WARNING: Show warning and error messages 73 | * - DBG_INFO: Show info, warning, and error messages 74 | * - DBG_DEBUG: Show debug, info, warning, and error messages 75 | * - DBG_VERBOSE: Show all messages 76 | */ 77 | setDebugMessageLevel(DBG_INFO); 78 | #endif 79 | /* Enable the connection handler to check for internet availability. 80 | * By default is disabled. 81 | */ 82 | conMan.enableCheckInternetAvailability(true); 83 | /* Add callbacks to the ConnectionHandler object to get notified of network 84 | * connection events. */ 85 | conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect); 86 | conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect); 87 | conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError); 88 | 89 | Serial.print("Network Adapter Interface: "); 90 | switch (conMan.getInterface()) { 91 | case NetworkAdapter::WIFI: 92 | Serial.println("Wi-Fi"); 93 | break; 94 | case NetworkAdapter::ETHERNET: 95 | Serial.println("Ethernet"); 96 | break; 97 | case NetworkAdapter::NB: 98 | Serial.println("Narrowband"); 99 | break; 100 | case NetworkAdapter::GSM: 101 | Serial.println("GSM"); 102 | break; 103 | case NetworkAdapter::LORA: 104 | Serial.println("LoRa"); 105 | break; 106 | case NetworkAdapter::CATM1: 107 | Serial.println("Category M1"); 108 | break; 109 | case NetworkAdapter::CELL: 110 | Serial.println("Cellular"); 111 | break; 112 | default: 113 | Serial.println("Unknown"); 114 | break; 115 | } 116 | } 117 | 118 | void loop() { 119 | /* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */ 120 | if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) { 121 | Serial.println("Toggling connection..."); 122 | if (attemptConnect) { 123 | conMan.connect(); 124 | } else { 125 | conMan.disconnect(); 126 | } 127 | attemptConnect = !attemptConnect; 128 | lastConnToggleMs = millis(); 129 | } 130 | 131 | /* The following code keeps on running connection workflows on our 132 | * ConnectionHandler object, hence allowing reconnection in case of failure 133 | * and notification of connect/disconnect event if enabled (see 134 | * addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within 135 | * the loop or methods called from it will delay the execution of .update(), 136 | * which might not guarantee the correct functioning of the ConnectionHandler 137 | * object. 138 | */ 139 | conMan.check(); 140 | } 141 | 142 | void onNetworkConnect() { 143 | Serial.println(">>>> CONNECTED to network"); 144 | } 145 | 146 | void onNetworkDisconnect() { 147 | Serial.println(">>>> DISCONNECTED from network"); 148 | } 149 | 150 | void onNetworkError() { 151 | Serial.println(">>>> ERROR"); 152 | } 153 | -------------------------------------------------------------------------------- /src/EthernetConnectionHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2020 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "ConnectionHandlerDefinitions.h" 16 | 17 | #ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */ 18 | #include "EthernetConnectionHandler.h" 19 | 20 | /****************************************************************************** 21 | CTOR/DTOR 22 | ******************************************************************************/ 23 | 24 | static inline void fromIPAddress(const IPAddress src, models::ip_addr& dst) { 25 | if(src.type() == IPv4) { 26 | dst.dword[IPADDRESS_V4_DWORD_INDEX] = (uint32_t)src; 27 | } else if(src.type() == IPv6) { 28 | for(uint8_t i=0; i static ip configuration 76 | if (ip != INADDR_NONE) { 77 | if (Ethernet.begin(nullptr, ip, 78 | IPAddress(_settings.eth.dns.type, _settings.eth.dns.bytes), 79 | IPAddress(_settings.eth.gateway.type, _settings.eth.gateway.bytes), 80 | IPAddress(_settings.eth.netmask.type, _settings.eth.netmask.bytes), 81 | _settings.eth.timeout, 82 | _settings.eth.response_timeout) == 0) { 83 | 84 | DEBUG_ERROR(F("Failed to configure Ethernet, check cable connection")); 85 | DEBUG_VERBOSE("timeout: %d, response timeout: %d", 86 | _settings.eth.timeout, _settings.eth.response_timeout); 87 | return NetworkConnectionState::INIT; 88 | } 89 | // An ip address is not provided -> dhcp configuration 90 | } else { 91 | if (Ethernet.begin(nullptr, _settings.eth.timeout, _settings.eth.response_timeout) == 0) { 92 | DEBUG_ERROR(F("Waiting Ethernet configuration from DHCP server, check cable connection")); 93 | DEBUG_VERBOSE("timeout: %d, response timeout: %d", 94 | _settings.eth.timeout, _settings.eth.response_timeout); 95 | 96 | return NetworkConnectionState::INIT; 97 | } 98 | } 99 | 100 | return NetworkConnectionState::CONNECTING; 101 | } 102 | 103 | NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() 104 | { 105 | if (Ethernet.linkStatus() == LinkOFF) { 106 | return NetworkConnectionState::INIT; 107 | } 108 | 109 | if (!_check_internet_availability) { 110 | return NetworkConnectionState::CONNECTED; 111 | } 112 | 113 | int ping_result = Ethernet.ping("time.arduino.cc"); 114 | DEBUG_INFO(F("Ethernet.ping(): %d"), ping_result); 115 | if (ping_result < 0) 116 | { 117 | DEBUG_ERROR(F("Internet check failed")); 118 | DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting); 119 | return NetworkConnectionState::CONNECTING; 120 | } 121 | else 122 | { 123 | DEBUG_INFO(F("Connected to Internet")); 124 | return NetworkConnectionState::CONNECTED; 125 | } 126 | 127 | } 128 | 129 | NetworkConnectionState EthernetConnectionHandler::update_handleConnected() 130 | { 131 | if (Ethernet.linkStatus() == LinkOFF) { 132 | DEBUG_ERROR(F("Ethernet link OFF, connection lost.")); 133 | if (_keep_alive) 134 | { 135 | DEBUG_ERROR(F("Attempting reconnection")); 136 | } 137 | return NetworkConnectionState::DISCONNECTED; 138 | } 139 | return NetworkConnectionState::CONNECTED; 140 | } 141 | 142 | NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting() 143 | { 144 | Ethernet.disconnect(); 145 | return NetworkConnectionState::DISCONNECTED; 146 | } 147 | 148 | NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected() 149 | { 150 | if (_keep_alive) 151 | { 152 | return NetworkConnectionState::INIT; 153 | } 154 | else 155 | { 156 | return NetworkConnectionState::CLOSED; 157 | } 158 | } 159 | 160 | #endif /* #ifdef BOARD_HAS_ETHERNET */ 161 | -------------------------------------------------------------------------------- /src/ConnectionHandlerInterface.cpp: -------------------------------------------------------------------------------- 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "ConnectionHandlerInterface.h" 16 | 17 | /****************************************************************************** 18 | CONSTRUCTOR/DESTRUCTOR 19 | ******************************************************************************/ 20 | 21 | ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter interface) 22 | : _keep_alive{keep_alive} 23 | , _check_internet_availability{false} 24 | , _interface{interface} 25 | , _lastConnectionTickTime{millis()} 26 | , _current_net_connection_state{NetworkConnectionState::INIT} 27 | , _timeoutTable(DefaultTimeoutTable) 28 | { 29 | 30 | } 31 | 32 | /****************************************************************************** 33 | PUBLIC MEMBER FUNCTIONS 34 | ******************************************************************************/ 35 | 36 | NetworkConnectionState ConnectionHandler::check() 37 | { 38 | unsigned long const now = millis(); 39 | unsigned int const connectionTickTimeInterval = 40 | _timeoutTable.intervals[static_cast(_current_net_connection_state)]; 41 | 42 | if((now - _lastConnectionTickTime) > connectionTickTimeInterval) 43 | { 44 | _lastConnectionTickTime = now; 45 | 46 | NetworkConnectionState old_net_connection_state = _current_net_connection_state; 47 | NetworkConnectionState next_net_connection_state = updateConnectionState(); 48 | 49 | /* Here we are determining whether a state transition from one state to the next has 50 | * occurred - and if it has, we call eventually registered callbacks. 51 | */ 52 | 53 | if(old_net_connection_state != next_net_connection_state) { 54 | updateCallback(next_net_connection_state); 55 | 56 | /* It may happen that the local _current_net_connection_state 57 | * is not updated by the updateConnectionState() call. This is the case for GenericConnection handler 58 | * where the call of updateConnectionState() is replaced by the inner ConnectionHandler call 59 | * that updates its state, but not the outer one. For this reason it is required to perform this call twice 60 | */ 61 | _current_net_connection_state = next_net_connection_state; 62 | } 63 | } 64 | 65 | return _current_net_connection_state; 66 | } 67 | 68 | NetworkConnectionState ConnectionHandler::updateConnectionState() { 69 | NetworkConnectionState next_net_connection_state = _current_net_connection_state; 70 | 71 | /* While the state machine is implemented here, the concrete implementation of the 72 | * states is done in the derived connection handlers. 73 | */ 74 | switch (_current_net_connection_state) 75 | { 76 | case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break; 77 | case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break; 78 | case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break; 79 | case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting(); break; 80 | case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break; 81 | case NetworkConnectionState::ERROR: break; 82 | case NetworkConnectionState::CLOSED: break; 83 | } 84 | 85 | /* Assign new state to the member variable holding the state */ 86 | _current_net_connection_state = next_net_connection_state; 87 | 88 | return next_net_connection_state; 89 | } 90 | 91 | void ConnectionHandler::updateCallback(NetworkConnectionState next_net_connection_state) { 92 | 93 | /* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */ 94 | if(next_net_connection_state == NetworkConnectionState::CONNECTED) 95 | { 96 | if(_on_connect_event_callback) _on_connect_event_callback(); 97 | } 98 | if(next_net_connection_state == NetworkConnectionState::DISCONNECTED) 99 | { 100 | if(_on_disconnect_event_callback) _on_disconnect_event_callback(); 101 | } 102 | if(next_net_connection_state == NetworkConnectionState::ERROR) 103 | { 104 | if(_on_error_event_callback) _on_error_event_callback(); 105 | } 106 | } 107 | 108 | void ConnectionHandler::connect() 109 | { 110 | if (_current_net_connection_state != NetworkConnectionState::INIT && _current_net_connection_state != NetworkConnectionState::CONNECTING) 111 | { 112 | _keep_alive = true; 113 | _current_net_connection_state = NetworkConnectionState::INIT; 114 | } 115 | } 116 | 117 | void ConnectionHandler::disconnect() 118 | { 119 | _keep_alive = false; 120 | _current_net_connection_state = NetworkConnectionState::DISCONNECTING; 121 | } 122 | 123 | void ConnectionHandler::addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback) 124 | { 125 | switch (event) 126 | { 127 | case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break; 128 | case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break; 129 | case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break; 130 | } 131 | } 132 | 133 | void ConnectionHandler::addConnectCallback(OnNetworkEventCallback callback) { 134 | _on_connect_event_callback = callback; 135 | } 136 | void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) { 137 | _on_disconnect_event_callback = callback; 138 | } 139 | void ConnectionHandler::addErrorCallback(OnNetworkEventCallback callback) { 140 | _on_error_event_callback = callback; 141 | } 142 | -------------------------------------------------------------------------------- /src/ConnectionHandlerDefinitions.h: -------------------------------------------------------------------------------- 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 | #pragma once 12 | 13 | /****************************************************************************** 14 | INCLUDES 15 | ******************************************************************************/ 16 | 17 | #include 18 | 19 | #ifdef ARDUINO_SAMD_MKR1000 20 | #define BOARD_HAS_WIFI 21 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 22 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 23 | #define NETWORK_CONNECTED WL_CONNECTED 24 | #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED 25 | #endif 26 | 27 | #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ 28 | defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined (ARDUINO_NANO_RP2040_CONNECT) 29 | 30 | #define BOARD_HAS_WIFI 31 | #define NETWORK_HARDWARE_ERROR WL_NO_MODULE 32 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 33 | #define NETWORK_CONNECTED WL_CONNECTED 34 | #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION 35 | #endif 36 | 37 | #if defined(ARDUINO_PORTENTA_H7_M7) 38 | #define BOARD_HAS_WIFI 39 | #define BOARD_HAS_ETHERNET 40 | #define BOARD_HAS_CATM1_NBIOT 41 | #define BOARD_HAS_CELLULAR 42 | #define BOARD_HAS_PORTENTA_CATM1_NBIOT_SHIELD 43 | #define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET 44 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 45 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 46 | #define NETWORK_CONNECTED WL_CONNECTED 47 | #endif 48 | 49 | #if defined(ARDUINO_PORTENTA_C33) 50 | #define BOARD_HAS_WIFI 51 | #define BOARD_HAS_ETHERNET 52 | #define BOARD_HAS_CELLULAR 53 | #define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET 54 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 55 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 56 | #define NETWORK_CONNECTED WL_CONNECTED 57 | #endif 58 | 59 | #if defined(ARDUINO_NICLA_VISION) 60 | #define BOARD_HAS_WIFI 61 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 62 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 63 | #define NETWORK_CONNECTED WL_CONNECTED 64 | #endif 65 | 66 | #if defined(ARDUINO_OPTA) 67 | #define BOARD_HAS_WIFI 68 | #define BOARD_HAS_ETHERNET 69 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 70 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 71 | #define NETWORK_CONNECTED WL_CONNECTED 72 | #endif 73 | 74 | #if defined(ARDUINO_GIGA) 75 | 76 | #define BOARD_HAS_WIFI 77 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 78 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 79 | #define NETWORK_CONNECTED WL_CONNECTED 80 | #endif 81 | 82 | #ifdef ARDUINO_SAMD_MKRGSM1400 83 | #define BOARD_HAS_GSM 84 | #define NETWORK_HARDWARE_ERROR GPRS_PING_ERROR 85 | #define NETWORK_IDLE_STATUS GSM3_NetworkStatus_t::IDLE 86 | #define NETWORK_CONNECTED GSM3_NetworkStatus_t::GPRS_READY 87 | #endif 88 | 89 | #ifdef ARDUINO_SAMD_MKRNB1500 90 | #define BOARD_HAS_NB 91 | #define NETWORK_HARDWARE_ERROR 92 | #define NETWORK_IDLE_STATUS NB_NetworkStatus_t::IDLE 93 | #define NETWORK_CONNECTED NB_NetworkStatus_t::GPRS_READY 94 | #endif 95 | 96 | #if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) 97 | #define BOARD_HAS_LORA 98 | #endif 99 | 100 | #if defined(ARDUINO_ARCH_ESP8266) 101 | 102 | #define BOARD_HAS_WIFI 103 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 104 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 105 | #define NETWORK_CONNECTED WL_CONNECTED 106 | #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED 107 | #endif 108 | 109 | #if defined(ARDUINO_ARCH_ESP32) 110 | #define BOARD_HAS_WIFI 111 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 112 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 113 | #define NETWORK_CONNECTED WL_CONNECTED 114 | #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED 115 | #endif 116 | 117 | #if defined(ARDUINO_UNOR4_WIFI) 118 | 119 | #define BOARD_HAS_WIFI 120 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 121 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 122 | #define NETWORK_CONNECTED WL_CONNECTED 123 | #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION 124 | #endif 125 | 126 | #ifdef ARDUINO_EDGE_CONTROL 127 | #define BOARD_HAS_CATM1_NBIOT 128 | #define BOARD_HAS_PORTENTA_CATM1_NBIOT_SHIELD 129 | #define NETWORK_HARDWARE_ERROR 130 | #endif 131 | 132 | #if defined(ARDUINO_RASPBERRY_PI_PICO_W) 133 | #define BOARD_HAS_WIFI 134 | #define NETWORK_HARDWARE_ERROR WL_NO_SHIELD 135 | #define NETWORK_IDLE_STATUS WL_IDLE_STATUS 136 | #define NETWORK_CONNECTED WL_CONNECTED 137 | #endif 138 | 139 | /****************************************************************************** 140 | TYPEDEFS 141 | ******************************************************************************/ 142 | 143 | enum class NetworkConnectionState : unsigned int { 144 | INIT = 0, 145 | CONNECTING = 1, 146 | CONNECTED = 2, 147 | DISCONNECTING = 3, 148 | DISCONNECTED = 4, 149 | CLOSED = 5, 150 | ERROR = 6 151 | }; 152 | 153 | enum class NetworkConnectionEvent { 154 | CONNECTED, 155 | DISCONNECTED, 156 | ERROR 157 | }; 158 | 159 | enum class NetworkAdapter { 160 | NONE, 161 | WIFI, 162 | ETHERNET, 163 | NB, 164 | GSM, 165 | LORA, 166 | CATM1, 167 | CELL 168 | }; 169 | 170 | union TimeoutTable { 171 | struct { 172 | // Note: order of the following values must be preserved 173 | // and match against NetworkConnectionState values 174 | uint32_t init; 175 | uint32_t connecting; 176 | uint32_t connected; 177 | uint32_t disconnecting; 178 | uint32_t disconnected; 179 | uint32_t closed; 180 | uint32_t error; 181 | } timeout; 182 | uint32_t intervals[sizeof(timeout) / sizeof(uint32_t)]; 183 | }; 184 | 185 | /****************************************************************************** 186 | CONSTANTS 187 | ******************************************************************************/ 188 | 189 | constexpr TimeoutTable DefaultTimeoutTable { 190 | #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) 191 | 4000, // init 192 | #else 193 | 500, // init 194 | #endif 195 | 500, // connecting 196 | 10000, // connected 197 | 100, // disconnecting 198 | 1000, // disconnected 199 | 1000, // closed 200 | 1000, // error 201 | }; 202 | -------------------------------------------------------------------------------- /src/WiFiConnectionHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "ConnectionHandlerDefinitions.h" 16 | 17 | #ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ 18 | #include "WiFiConnectionHandler.h" 19 | 20 | /****************************************************************************** 21 | CONSTANTS 22 | ******************************************************************************/ 23 | #if defined(ARDUINO_ARCH_ESP8266) 24 | static int const ESP_WIFI_CONNECTION_TIMEOUT = 3000; 25 | #endif 26 | 27 | /****************************************************************************** 28 | CTOR/DTOR 29 | ******************************************************************************/ 30 | 31 | WiFiConnectionHandler::WiFiConnectionHandler() 32 | : ConnectionHandler(true, NetworkAdapter::WIFI) { 33 | } 34 | 35 | WiFiConnectionHandler::WiFiConnectionHandler(char const * ssid, char const * pass, bool const keep_alive) 36 | : ConnectionHandler{keep_alive, NetworkAdapter::WIFI} 37 | { 38 | _settings.type = NetworkAdapter::WIFI; 39 | strncpy(_settings.wifi.ssid, ssid, sizeof(_settings.wifi.ssid)-1); 40 | strncpy(_settings.wifi.pwd, pass, sizeof(_settings.wifi.pwd)-1); 41 | } 42 | 43 | /****************************************************************************** 44 | PUBLIC MEMBER FUNCTIONS 45 | ******************************************************************************/ 46 | 47 | unsigned long WiFiConnectionHandler::getTime() 48 | { 49 | #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) 50 | return WiFi.getTime(); 51 | #else 52 | return 0; 53 | #endif 54 | } 55 | 56 | /****************************************************************************** 57 | PROTECTED MEMBER FUNCTIONS 58 | ******************************************************************************/ 59 | 60 | NetworkConnectionState WiFiConnectionHandler::update_handleInit() 61 | { 62 | #if !defined(__AVR__) 63 | DEBUG_INFO(F("WiFi.status(): %d"), WiFi.status()); 64 | #endif 65 | 66 | #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) 67 | if (WiFi.status() == NETWORK_HARDWARE_ERROR) 68 | { 69 | #if !defined(__AVR__) 70 | DEBUG_ERROR(F("WiFi Hardware failure.\nMake sure you are using a WiFi enabled board/shield.")); 71 | DEBUG_ERROR(F("Then reset and retry.")); 72 | #endif 73 | return NetworkConnectionState::ERROR; 74 | } 75 | #if !defined(__AVR__) 76 | DEBUG_INFO(F("Current WiFi Firmware: %s"), WiFi.firmwareVersion()); 77 | #endif 78 | 79 | #if defined(WIFI_FIRMWARE_VERSION_REQUIRED) 80 | if (String(WiFi.firmwareVersion()) < String(WIFI_FIRMWARE_VERSION_REQUIRED)) 81 | { 82 | #if !defined(__AVR__) 83 | DEBUG_ERROR(F("Latest WiFi Firmware: %s"), WIFI_FIRMWARE_VERSION_REQUIRED); 84 | DEBUG_ERROR(F("Please update to the latest version for best performance.")); 85 | #endif 86 | delay(5000); 87 | } 88 | #endif 89 | #else 90 | WiFi.mode(WIFI_STA); 91 | #endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */ 92 | 93 | if (WiFi.status() != WL_CONNECTED) 94 | { 95 | WiFi.begin(_settings.wifi.ssid, _settings.wifi.pwd); 96 | #if defined(ARDUINO_ARCH_ESP8266) 97 | /* Wait connection otherwise board won't connect */ 98 | unsigned long start = millis(); 99 | while((WiFi.status() != WL_CONNECTED) && (millis() - start) < ESP_WIFI_CONNECTION_TIMEOUT) { 100 | delay(100); 101 | } 102 | #endif 103 | 104 | } 105 | 106 | if (WiFi.status() != NETWORK_CONNECTED) 107 | { 108 | #if !defined(__AVR__) 109 | DEBUG_ERROR(F("Connection to \"%s\" failed"), _settings.wifi.ssid); 110 | DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.init); 111 | #endif 112 | return NetworkConnectionState::INIT; 113 | } 114 | else 115 | { 116 | #if !defined(__AVR__) 117 | DEBUG_INFO(F("Connected to \"%s\""), _settings.wifi.ssid); 118 | #endif 119 | #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) 120 | configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); 121 | #endif 122 | return NetworkConnectionState::CONNECTING; 123 | } 124 | } 125 | 126 | NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() 127 | { 128 | if (WiFi.status() != WL_CONNECTED){ 129 | return NetworkConnectionState::INIT; 130 | } 131 | 132 | if(!_check_internet_availability){ 133 | return NetworkConnectionState::CONNECTED; 134 | } 135 | 136 | #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) 137 | int ping_result = WiFi.ping("time.arduino.cc"); 138 | DEBUG_INFO(F("WiFi.ping(): %d"), ping_result); 139 | if (ping_result < 0) 140 | { 141 | DEBUG_ERROR(F("Internet check failed")); 142 | DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting); 143 | return NetworkConnectionState::CONNECTING; 144 | } 145 | #endif 146 | DEBUG_INFO(F("Connected to Internet")); 147 | return NetworkConnectionState::CONNECTED; 148 | 149 | } 150 | 151 | NetworkConnectionState WiFiConnectionHandler::update_handleConnected() 152 | { 153 | if (WiFi.status() != WL_CONNECTED) 154 | { 155 | #if !defined(__AVR__) 156 | DEBUG_VERBOSE(F("WiFi.status(): %d"), WiFi.status()); 157 | DEBUG_ERROR(F("Connection to \"%s\" lost."), _settings.wifi.ssid); 158 | #endif 159 | if (_keep_alive) 160 | { 161 | #if !defined(__AVR__) 162 | DEBUG_INFO(F("Attempting reconnection")); 163 | #endif 164 | } 165 | 166 | return NetworkConnectionState::DISCONNECTED; 167 | } 168 | return NetworkConnectionState::CONNECTED; 169 | } 170 | 171 | NetworkConnectionState WiFiConnectionHandler::update_handleDisconnecting() 172 | { 173 | WiFi.disconnect(); 174 | return NetworkConnectionState::DISCONNECTED; 175 | } 176 | 177 | NetworkConnectionState WiFiConnectionHandler::update_handleDisconnected() 178 | { 179 | #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) 180 | WiFi.end(); 181 | #endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */ 182 | if (_keep_alive) 183 | { 184 | return NetworkConnectionState::INIT; 185 | } 186 | else 187 | { 188 | return NetworkConnectionState::CLOSED; 189 | } 190 | } 191 | 192 | #endif /* #ifdef BOARD_HAS_WIFI */ 193 | -------------------------------------------------------------------------------- /src/LoRaConnectionHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_ConnectionHandler library. 3 | 4 | Copyright (c) 2019 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 | INCLUDE 13 | ******************************************************************************/ 14 | 15 | #include "ConnectionHandlerDefinitions.h" 16 | 17 | #if defined(BOARD_HAS_LORA) /* Only compile if the board has LoRa */ 18 | #include "LoRaConnectionHandler.h" 19 | 20 | /****************************************************************************** 21 | TYPEDEF 22 | ******************************************************************************/ 23 | 24 | typedef enum 25 | { 26 | LORA_ERROR_ACK_NOT_RECEIVED = -1, 27 | LORA_ERROR_GENERIC = -2, 28 | LORA_ERROR_WRONG_PARAM = -3, 29 | LORA_ERROR_COMMUNICATION_BUSY = -4, 30 | LORA_ERROR_MESSAGE_OVERFLOW = -5, 31 | LORA_ERROR_NO_NETWORK_AVAILABLE = -6, 32 | LORA_ERROR_RX_PACKET = -7, 33 | LORA_ERROR_REASON_UNKNOWN = -8, 34 | LORA_ERROR_MAX_PACKET_SIZE = -20 35 | } LoRaCommunicationError; 36 | 37 | /****************************************************************************** 38 | CTOR/DTOR 39 | ******************************************************************************/ 40 | LoRaConnectionHandler::LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band, char const * channelMask, _lora_class const device_class) 41 | : ConnectionHandler{false, NetworkAdapter::LORA} 42 | { 43 | _settings.type = NetworkAdapter::LORA; 44 | strncpy(_settings.lora.appeui, appeui, sizeof(_settings.lora.appeui)-1); 45 | strncpy(_settings.lora.appkey, appkey, sizeof(_settings.lora.appkey)-1); 46 | _settings.lora.band = band; 47 | strncpy(_settings.lora.channelMask, channelMask, sizeof(_settings.lora.channelMask)-1); 48 | _settings.lora.deviceClass = device_class; 49 | } 50 | 51 | /****************************************************************************** 52 | PUBLIC MEMBER FUNCTIONS 53 | ******************************************************************************/ 54 | 55 | int LoRaConnectionHandler::write(const uint8_t * buf, size_t size) 56 | { 57 | _modem.beginPacket(); 58 | _modem.write(buf, size); 59 | int const err = _modem.endPacket(true); 60 | 61 | if (err != size) 62 | { 63 | switch (err) 64 | { 65 | case LoRaCommunicationError::LORA_ERROR_ACK_NOT_RECEIVED: 66 | DEBUG_ERROR(F("Message ack was not received, the message could not be delivered")); 67 | break; 68 | case LoRaCommunicationError::LORA_ERROR_GENERIC: 69 | DEBUG_ERROR(F("LoRa generic error (LORA_ERROR)")); 70 | break; 71 | case LoRaCommunicationError::LORA_ERROR_WRONG_PARAM: 72 | DEBUG_ERROR(F("LoRa malformed param error (LORA_ERROR_PARAM")); 73 | break; 74 | case LoRaCommunicationError::LORA_ERROR_COMMUNICATION_BUSY: 75 | DEBUG_ERROR(F("LoRa chip is busy (LORA_ERROR_BUSY)")); 76 | break; 77 | case LoRaCommunicationError::LORA_ERROR_MESSAGE_OVERFLOW: 78 | DEBUG_ERROR(F("LoRa chip overflow error (LORA_ERROR_OVERFLOW)")); 79 | break; 80 | case LoRaCommunicationError::LORA_ERROR_NO_NETWORK_AVAILABLE: 81 | DEBUG_ERROR(F("LoRa no network error (LORA_ERROR_NO_NETWORK)")); 82 | break; 83 | case LoRaCommunicationError::LORA_ERROR_RX_PACKET: 84 | DEBUG_ERROR(F("LoRa rx error (LORA_ERROR_RX)")); 85 | break; 86 | case LoRaCommunicationError::LORA_ERROR_REASON_UNKNOWN: 87 | DEBUG_ERROR(F("LoRa unknown error (LORA_ERROR_UNKNOWN)")); 88 | break; 89 | case LoRaCommunicationError::LORA_ERROR_MAX_PACKET_SIZE: 90 | DEBUG_ERROR(F("Message length is bigger than max LoRa packet!")); 91 | break; 92 | } 93 | } 94 | else 95 | { 96 | DEBUG_INFO(F("Message sent correctly!")); 97 | } 98 | return err; 99 | } 100 | 101 | int LoRaConnectionHandler::read() 102 | { 103 | return _modem.read(); 104 | } 105 | 106 | bool LoRaConnectionHandler::available() 107 | { 108 | return _modem.available(); 109 | } 110 | 111 | /****************************************************************************** 112 | PROTECTED MEMBER FUNCTIONS 113 | ******************************************************************************/ 114 | 115 | NetworkConnectionState LoRaConnectionHandler::update_handleInit() 116 | { 117 | if (!_modem.begin((_lora_band)_settings.lora.band)) 118 | { 119 | DEBUG_ERROR(F("Something went wrong; are you indoor? Move near a window, then reset and retry.")); 120 | return NetworkConnectionState::ERROR; 121 | } 122 | // Set channelmask based on configuration 123 | if (_settings.lora.channelMask) { 124 | _modem.sendMask(_settings.lora.channelMask); 125 | } 126 | //A delay is required between _modem.begin(band) and _modem.joinOTAA(appeui, appkey) in order to let the chip to be correctly initialized before the connection attempt 127 | delay(100); 128 | _modem.configureClass((_lora_class)_settings.lora.deviceClass); 129 | delay(100); 130 | DEBUG_INFO(F("Connecting to the network")); 131 | return NetworkConnectionState::CONNECTING; 132 | } 133 | 134 | NetworkConnectionState LoRaConnectionHandler::update_handleConnecting() 135 | { 136 | bool const network_status = _modem.joinOTAA(_settings.lora.appeui, _settings.lora.appkey); 137 | if (network_status != true) 138 | { 139 | DEBUG_ERROR(F("Connection to the network failed")); 140 | DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting); 141 | return NetworkConnectionState::INIT; 142 | } 143 | else 144 | { 145 | DEBUG_INFO(F("Connected to the network")); 146 | return NetworkConnectionState::CONNECTED; 147 | } 148 | } 149 | 150 | NetworkConnectionState LoRaConnectionHandler::update_handleConnected() 151 | { 152 | bool const network_status = _modem.connected(); 153 | if (network_status != true) 154 | { 155 | DEBUG_ERROR(F("Connection to the network lost.")); 156 | if (_keep_alive) 157 | { 158 | DEBUG_ERROR(F("Attempting reconnection")); 159 | } 160 | return NetworkConnectionState::DISCONNECTED; 161 | } 162 | return NetworkConnectionState::CONNECTED; 163 | } 164 | 165 | NetworkConnectionState LoRaConnectionHandler::update_handleDisconnecting() 166 | { 167 | DEBUG_ERROR(F("Connection to the network lost.")); 168 | if (_keep_alive) 169 | { 170 | DEBUG_ERROR(F("Attempting reconnection")); 171 | } 172 | return NetworkConnectionState::DISCONNECTED; 173 | } 174 | 175 | NetworkConnectionState LoRaConnectionHandler::update_handleDisconnected() 176 | { 177 | if (_keep_alive) 178 | { 179 | return NetworkConnectionState::INIT; 180 | } 181 | else 182 | { 183 | return NetworkConnectionState::CLOSED; 184 | } 185 | } 186 | 187 | #endif 188 | -------------------------------------------------------------------------------- /.github/workflows/compile-examples.yml: -------------------------------------------------------------------------------- 1 | name: Compile Examples 2 | 3 | on: 4 | pull_request: 5 | paths: 6 | - ".github/workflows/compile-examples.yml" 7 | - "library.properties" 8 | - "examples/**" 9 | - "src/**" 10 | push: 11 | paths: 12 | - ".github/workflows/compile-examples.yml" 13 | - "library.properties" 14 | - "examples/**" 15 | - "src/**" 16 | schedule: 17 | # Run every Tuesday at 8 AM UTC to catch breakage caused by changes to external resources (libraries, platforms). 18 | - cron: "0 8 * * TUE" 19 | workflow_dispatch: 20 | repository_dispatch: 21 | 22 | jobs: 23 | build: 24 | name: ${{ matrix.board.fqbn }} 25 | runs-on: ubuntu-latest 26 | 27 | env: 28 | LIBRARIES: | 29 | # Install the Arduino_ConnectionHandler library from the repository 30 | - source-path: ./ 31 | - name: Arduino_DebugUtils 32 | - name: WiFi101 33 | - name: WiFiNINA 34 | - name: MKRGSM 35 | - name: MKRNB 36 | - name: MKRWAN 37 | - name: Arduino_Cellular 38 | SKETCH_PATHS: | 39 | - examples/ConnectionHandlerDemo 40 | - examples/CheckInternetAvailabilityDemo 41 | ARDUINOCORE_MBED_STAGING_PATH: extras/ArduinoCore-mbed 42 | ARDUINOCORE_API_STAGING_PATH: extras/ArduinoCore-API 43 | SKETCHES_REPORTS_PATH: sketches-reports 44 | strategy: 45 | matrix: 46 | board: 47 | - fqbn: "arduino:samd:mkr1000" 48 | platform-name: arduino:samd 49 | artifact-name-suffix: arduino-samd-mkr1000 50 | - fqbn: "arduino:samd:mkrwifi1010" 51 | platform-name: arduino:samd 52 | artifact-name-suffix: arduino-samd-mkrwifi1010 53 | - fqbn: "arduino:samd:nano_33_iot" 54 | platform-name: arduino:samd 55 | artifact-name-suffix: arduino-samd-nano_33_iot 56 | - fqbn: "arduino:samd:mkrgsm1400" 57 | platform-name: arduino:samd 58 | artifact-name-suffix: arduino-samd-mkrgsm1400 59 | - fqbn: "arduino:samd:mkrnb1500" 60 | platform-name: arduino:samd 61 | artifact-name-suffix: arduino-samd-mkrnb1500 62 | - fqbn: "arduino:samd:mkrwan1300" 63 | platform-name: arduino:samd 64 | artifact-name-suffix: arduino-samd-mkrwan1300 65 | - fqbn: "arduino:samd:mkrwan1310" 66 | platform-name: arduino:samd 67 | artifact-name-suffix: arduino-samd-mkrwan1310 68 | - fqbn: "arduino:mbed:envie_m7" 69 | platform-name: arduino:mbed 70 | artifact-name-suffix: arduino-mbed-envie_m7 71 | - fqbn: "arduino:mbed_portenta:envie_m7" 72 | platform-name: arduino:mbed_portenta 73 | artifact-name-suffix: arduino-mbed_portenta-envie_m7 74 | - fqbn: "esp8266:esp8266:huzzah" 75 | platform-name: esp8266:esp8266 76 | artifact-name-suffix: esp8266-esp8266-huzzah 77 | - fqbn: "esp32:esp32:esp32" 78 | platform-name: esp32:esp32 79 | artifact-name-suffix: esp32-esp32-esp32 80 | - fqbn: arduino:mbed_nano:nanorp2040connect 81 | platform-name: arduino:mbed_nano 82 | artifact-name-suffix: arduino-mbed_nano-nanorp2040connect 83 | - fqbn: arduino:mbed_nicla:nicla_vision 84 | platform-name: arduino:mbed_nicla 85 | artifact-name-suffix: arduino-mbed_nicla-nicla_vision 86 | - fqbn: arduino:mbed_opta:opta 87 | platform-name: arduino:mbed_opta 88 | artifact-name-suffix: arduino-mbed_opta-opta 89 | - fqbn: arduino:mbed_giga:giga 90 | platform-name: arduino:mbed_giga 91 | artifact-name-suffix: arduino-mbed_giga-giga 92 | - fqbn: arduino:renesas_portenta:portenta_c33 93 | platform-name: arduino:renesas_portenta 94 | artifact-name-suffix: arduino-renesas_portenta-portenta_c33 95 | - fqbn: arduino:renesas_uno:unor4wifi 96 | platform-name: arduino:renesas_uno 97 | artifact-name-suffix: arduino-renesas_uno-unor4wifi 98 | - fqbn: arduino:esp32:nano_nora 99 | platform-name: arduino:esp32 100 | artifact-name-suffix: arduino-esp32-nano_nora 101 | - fqbn: arduino:mbed_edge:edge_control 102 | platform-name: arduino:mbed_edge 103 | artifact-name-suffix: arduino-mbed_edge-edge_control 104 | - fqbn: "rp2040:rp2040:rpipicow" 105 | platform-name: rp2040:rp2040 106 | artifact-name-suffix: rp2040-rp2040-rpipicow 107 | 108 | # Make board type-specific customizations to the matrix jobs 109 | include: 110 | - board: 111 | platform-name: arduino:samd 112 | platforms: | 113 | # Install Arduino SAMD Boards via Boards Manager 114 | - name: arduino:samd 115 | - board: 116 | platform-name: arduino:mbed 117 | platforms: | 118 | # Install Arduino mbed-Enabled Boards via Boards Manager for the toolchain 119 | - name: arduino:mbed 120 | # Overwrite the Arduino mbed-Enabled Boards release version with version from the tip of the default branch (located in local path because of the need to first install ArduinoCore-API) 121 | - source-path: extras/ArduinoCore-mbed 122 | name: arduino:mbed 123 | - board: 124 | platform-name: arduino:renesas_portenta 125 | platforms: | 126 | # Install Arduino Renesas portenta Boards via Boards Manager 127 | - name: arduino:renesas_portenta 128 | - board: 129 | platform-name: arduino:renesas_uno 130 | platforms: | 131 | # Install Arduino Renesas uno Boards via Boards Manager 132 | - name: arduino:renesas_uno 133 | - board: 134 | platform-name: arduino:esp32 135 | platforms: | 136 | # Install Arduino ESP32 Boards via Boards Manager 137 | - name: arduino:esp32 138 | - board: 139 | platform-name: esp8266:esp8266 140 | platforms: | 141 | # Install ESP8266 platform via Boards Manager 142 | - name: esp8266:esp8266 143 | source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json 144 | version: 2.5.0 145 | - board: 146 | platform-name: esp32:esp32 147 | platforms: | 148 | # Install ESP32 platform via Boards Manager 149 | - name: esp32:esp32 150 | source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json 151 | - board: 152 | platform-name: rp2040:rp2040 153 | platforms: | 154 | # Install rp2040 platform via Boards Manager 155 | - name: rp2040:rp2040 156 | source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json 157 | 158 | steps: 159 | - uses: actions/checkout@v6 160 | 161 | # It's necessary to checkout the platform before installing it so that the ArduinoCore-API dependency can be added 162 | - name: Checkout ArduinoCore-mbed 163 | # this step only needed when the Arduino mbed-Enabled Boards platform sourced from the repository is being used 164 | if: matrix.board.platform-name == 'arduino:mbed' 165 | uses: actions/checkout@v6 166 | with: 167 | repository: arduino/ArduinoCore-mbed 168 | # The arduino/actions/libraries/compile-examples action will install the platform from this path 169 | path: ${{ env.ARDUINOCORE_MBED_STAGING_PATH }} 170 | 171 | - name: Checkout ArduinoCore-API 172 | # This step only needed when the Arduino mbed-Enabled Boards platform sourced from the repository is being used 173 | if: matrix.board.platform-name == 'arduino:mbed' 174 | uses: actions/checkout@v6 175 | with: 176 | repository: arduino/ArduinoCore-API 177 | path: ${{ env.ARDUINOCORE_API_STAGING_PATH }} 178 | 179 | - name: Install ArduinoCore-API 180 | # This step only needed when the Arduino mbed-Enabled Boards platform sourced from the repository is being used 181 | if: matrix.board.platform-name == 'arduino:mbed' 182 | run: | 183 | mv "${{ env.ARDUINOCORE_API_STAGING_PATH }}/api" "${{ env.ARDUINOCORE_MBED_STAGING_PATH }}/cores/arduino" 184 | 185 | - name: Install ESP32 platform dependencies 186 | if: matrix.board.platform-name == 'esp32:esp32' 187 | run: pip3 install pyserial 188 | 189 | - name: Compile examples 190 | uses: arduino/compile-sketches@v1 191 | with: 192 | platforms: ${{ matrix.platforms }} 193 | fqbn: ${{ matrix.board.fqbn }} 194 | libraries: ${{ env.LIBRARIES }} 195 | sketch-paths: | 196 | ${{ env.SKETCH_PATHS }} 197 | ${{ matrix.sketch-paths }} 198 | enable-deltas-report: 'true' 199 | sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }} 200 | 201 | - name: Save memory usage change report as artifact 202 | if: github.event_name == 'pull_request' 203 | uses: actions/upload-artifact@v6 204 | with: 205 | if-no-files-found: error 206 | name: sketches-report-${{ matrix.board.artifact-name-suffix }} 207 | path: ${{ env.SKETCHES_REPORTS_PATH }} 208 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at https://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. --------------------------------------------------------------------------------