-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathCellularConnectionHandler.cpp
More file actions
161 lines (135 loc) · 4.89 KB
/
CellularConnectionHandler.cpp
File metadata and controls
161 lines (135 loc) · 4.89 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
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()
: ConnectionHandler(true, NetworkAdapter::CELL) {}
CellularConnectionHandler::CellularConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::CELL}
{
_settings.type = NetworkAdapter::CELL;
strncpy(_settings.cell.pin, pin, sizeof(_settings.cell.pin)-1);
strncpy(_settings.cell.apn, apn, sizeof(_settings.cell.apn)-1);
strncpy(_settings.cell.login, login, sizeof(_settings.cell.login)-1);
strncpy(_settings.cell.pass, pass, sizeof(_settings.cell.pass)-1);
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
unsigned long CellularConnectionHandler::getTime()
{
return _cellular.getCellularTime().getUNIXTimestamp();
}
UDP & CellularConnectionHandler::getUDP()
{
DEBUG_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 (strlen(_settings.cell.pin) > 0 && !_cellular.unlockSIM(_settings.cell.pin)) {
DEBUG_ERROR(F("SIM not present or wrong PIN"));
return NetworkConnectionState::ERROR;
}
if (!_cellular.connect(String(_settings.cell.apn), String(_settings.cell.login), String(_settings.cell.pass))) {
DEBUG_ERROR(F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
}
#endif
DEBUG_INFO(F("Connected to Network"));
return NetworkConnectionState::CONNECTING;
}
NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
{
if (!_cellular.isConnectedToInternet()) {
return NetworkConnectionState::INIT;
}
if (!_check_internet_availability) {
return NetworkConnectionState::CONNECTED;
}
if(getTime() == 0){
DEBUG_ERROR(F("Internet check failed"));
DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
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 */