-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathCellularConnectionHandler.cpp
143 lines (121 loc) · 4.12 KB
/
CellularConnectionHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
This file is part of the Arduino_ConnectionHandler library.
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "ConnectionHandlerDefinitions.h"
#ifdef BOARD_HAS_CELLULAR /* Only compile if the board has Cellular */
#include "CellularConnectionHandler.h"
/******************************************************************************
CTOR/DTOR
******************************************************************************/
CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::CELL}
, _pin(pin)
, _apn(apn)
, _login(login)
, _pass(pass)
{
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
unsigned long CellularConnectionHandler::getTime()
{
return _cellular.getCellularTime().getUNIXTimestamp();
}
UDP & CellularConnectionHandler::getUDP()
{
Debug.print(DBG_ERROR, F("CellularConnectionHandler has no UDP support"));
while(1) {};
}
/******************************************************************************
PROTECTED MEMBER FUNCTIONS
******************************************************************************/
#if defined(ARDUINO_OPTA)
CellularExpansion ce;
static void beginOptaCellular() {
static bool first_call = true;
if(first_call) {
first_call = false;
OptaController.registerCustomExpansion(CellularExpansion::getProduct(),
CellularExpansion::makeExpansion,
CellularExpansion::startUp);
OptaController.begin();
delay(500);
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
ce = OptaController.getExpansion(i);
if(ce) {
ce.ctrlModem(true);
delay(100);
break;
}
}
}
else {
OptaController.update();
}
}
#endif
NetworkConnectionState CellularConnectionHandler::update_handleInit()
{
#if defined(ARDUINO_OPTA)
beginOptaCellular();
#else
_cellular.begin();
#endif
_cellular.setDebugStream(Serial);
#if defined(ARDUINO_OPTA)
/* in case Opta Cellular is not wired this check on ce prevent the call
to unlockSIM that cause a crash in the Opta (deep due to the missing
communication with the modem) */
if(ce) {
if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) {
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
return NetworkConnectionState::ERROR;
}
}
else {
return NetworkConnectionState::ERROR;
}
#else
if (String(_pin).length() > 0 && !_cellular.unlockSIM(_pin)) {
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
return NetworkConnectionState::ERROR;
}
#endif
return NetworkConnectionState::CONNECTING;
}
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
{
if (!_cellular.connect(_apn, _login, _pass)) {
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
}
Debug.print(DBG_INFO, F("Connected to Network"));
return NetworkConnectionState::CONNECTED;
}
NetworkConnectionState CellularConnectionHandler::update_handleConnected()
{
if (!_cellular.isConnectedToInternet()) {
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
}
NetworkConnectionState CellularConnectionHandler::update_handleDisconnecting()
{
return NetworkConnectionState::DISCONNECTED;
}
NetworkConnectionState CellularConnectionHandler::update_handleDisconnected()
{
if (_keep_alive) {
return NetworkConnectionState::INIT;
}
return NetworkConnectionState::CLOSED;
}
#endif /* #ifdef BOARD_HAS_CELLULAR */