Skip to content

Commit f455198

Browse files
committed
Add CellularConnectionHandler
1 parent 53db2d5 commit f455198

3 files changed

+161
-6
lines changed
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
This file is part of the Arduino_ConnectionHandler library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
12+
/******************************************************************************
13+
INCLUDE
14+
******************************************************************************/
15+
16+
#include "Arduino_CellularConnectionHandler.h"
17+
18+
#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */
19+
20+
/******************************************************************************
21+
CTOR/DTOR
22+
******************************************************************************/
23+
24+
CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
25+
: ConnectionHandler{keep_alive, NetworkAdapter::CELL}
26+
, _pin(pin)
27+
, _apn(apn)
28+
, _login(login)
29+
, _pass(pass)
30+
{
31+
32+
}
33+
34+
/******************************************************************************
35+
PUBLIC MEMBER FUNCTIONS
36+
******************************************************************************/
37+
38+
unsigned long CellularConnectionHandler::getTime()
39+
{
40+
return _cellular.getCellularTime().getUNIXTimestamp();
41+
}
42+
43+
/******************************************************************************
44+
PROTECTED MEMBER FUNCTIONS
45+
******************************************************************************/
46+
47+
NetworkConnectionState CellularConnectionHandler::update_handleInit()
48+
{
49+
_cellular.begin();
50+
_cellular.setDebugStream(Serial);
51+
if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) {
52+
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
53+
return NetworkConnectionState::ERROR;
54+
}
55+
return NetworkConnectionState::CONNECTING;
56+
}
57+
58+
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
59+
{
60+
if (!_cellular.connect(_apn, _login, _pass)) {
61+
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
62+
return NetworkConnectionState::ERROR;
63+
}
64+
Debug.print(DBG_INFO, F("Connected to Network"));
65+
return NetworkConnectionState::CONNECTED;
66+
}
67+
68+
NetworkConnectionState CellularConnectionHandler::update_handleConnected()
69+
{
70+
if (!_cellular.isConnectedToInternet()) {
71+
return NetworkConnectionState::DISCONNECTED;
72+
}
73+
return NetworkConnectionState::CONNECTED;
74+
}
75+
76+
NetworkConnectionState CellularConnectionHandler::update_handleDisconnecting()
77+
{
78+
return NetworkConnectionState::DISCONNECTED;
79+
}
80+
81+
NetworkConnectionState CellularConnectionHandler::update_handleDisconnected()
82+
{
83+
if (_keep_alive) {
84+
return NetworkConnectionState::INIT;
85+
}
86+
return NetworkConnectionState::CLOSED;
87+
}
88+
89+
#endif /* #ifdef BOARD_HAS_CELLULAR */
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 "Arduino_ConnectionHandler.h"
20+
21+
22+
#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */
23+
24+
/******************************************************************************
25+
CLASS DECLARATION
26+
******************************************************************************/
27+
28+
class CellularConnectionHandler : public ConnectionHandler
29+
{
30+
public:
31+
32+
CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive = true);
33+
34+
35+
virtual unsigned long getTime() override;
36+
virtual Client & getClient() override { return _gsm_client; };
37+
virtual UDP & getUDP() override { } __attribute__((error("CellularConnectionHandler has no UDP support")));
38+
39+
40+
protected:
41+
42+
virtual NetworkConnectionState update_handleInit () override;
43+
virtual NetworkConnectionState update_handleConnecting () override;
44+
virtual NetworkConnectionState update_handleConnected () override;
45+
virtual NetworkConnectionState update_handleDisconnecting() override;
46+
virtual NetworkConnectionState update_handleDisconnected () override;
47+
48+
49+
private:
50+
51+
const char * _pin;
52+
const char * _apn;
53+
const char * _login;
54+
const char * _pass;
55+
56+
ArduinoCellular _cellular;
57+
TinyGsmClient _gsm_client = _cellular.getNetworkClient();
58+
};
59+
60+
#endif /* #ifdef BOARD_HAS_CELLULAR */
61+
62+
#endif /* #ifndef ARDUINO_CELLULAR_CONNECTION_HANDLER_H_ */

src/Arduino_ConnectionHandler.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <Ethernet.h>
5858
#include <PortentaEthernet.h>
5959
#include <GSM.h>
60+
#include <Arduino_Cellular.h>
6061

6162
#define BOARD_HAS_WIFI
6263
#define BOARD_HAS_ETHERNET
@@ -150,7 +151,7 @@
150151
#if defined(ARDUINO_ARCH_ESP32)
151152
#include <WiFi.h>
152153
#include <WiFiUdp.h>
153-
154+
154155
#define BOARD_HAS_WIFI
155156
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
156157
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
@@ -201,7 +202,8 @@ enum class NetworkAdapter {
201202
NB,
202203
GSM,
203204
LORA,
204-
CATM1
205+
CATM1,
206+
CELL
205207
};
206208

207209
typedef void (*OnNetworkEventCallback)();
@@ -237,13 +239,11 @@ class ConnectionHandler {
237239

238240
NetworkConnectionState check();
239241

240-
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)
242+
#if !defined(BOARD_HAS_LORA)
241243
virtual unsigned long getTime() = 0;
242244
virtual Client &getClient() = 0;
243245
virtual UDP &getUDP() = 0;
244-
#endif
245-
246-
#if defined(BOARD_HAS_LORA)
246+
#else
247247
virtual int write(const uint8_t *buf, size_t size) = 0;
248248
virtual int read() = 0;
249249
virtual bool available() = 0;
@@ -304,4 +304,8 @@ class ConnectionHandler {
304304
#include "Arduino_CatM1ConnectionHandler.h"
305305
#endif
306306

307+
#if defined(BOARD_HAS_CELLULAR)
308+
#include "Arduino_CellularConnectionHandler.h"
309+
#endif
310+
307311
#endif /* CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)