Skip to content

Commit 1e88a7c

Browse files
committed
Add CellularConnectionHandler
1 parent 53db2d5 commit 1e88a7c

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
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_ */

0 commit comments

Comments
 (0)