-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathCatM1ConnectionHandler.cpp
More file actions
145 lines (124 loc) · 4.44 KB
/
CatM1ConnectionHandler.cpp
File metadata and controls
145 lines (124 loc) · 4.44 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
/*
This file is part of the Arduino_ConnectionHandler library.
Copyright (c) 2023 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_CATM1_NBIOT /* Only compile if the board has CatM1 BN-IoT */
#include "CatM1ConnectionHandler.h"
/******************************************************************************
CTOR/DTOR
******************************************************************************/
CatM1ConnectionHandler::CatM1ConnectionHandler()
: ConnectionHandler(true, NetworkAdapter::CATM1, true) { }
CatM1ConnectionHandler::CatM1ConnectionHandler(
const char * pin, const char * apn, const char * login, const char * pass,
RadioAccessTechnologyType rat, uint32_t band, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::CATM1, true}
{
_settings.type = NetworkAdapter::CATM1;
// To keep the backward compatibility, the user can call enableCheckInternetAvailability(false) for disabling the check
_flags.check_internet_availability = true;
strncpy(_settings.catm1.pin, pin, sizeof(_settings.catm1.pin)-1);
strncpy(_settings.catm1.apn, apn, sizeof(_settings.catm1.apn)-1);
strncpy(_settings.catm1.login, login, sizeof(_settings.catm1.login)-1);
strncpy(_settings.catm1.pass, pass, sizeof(_settings.catm1.pass)-1);
_settings.catm1.rat = static_cast<uint8_t>(rat);
_settings.catm1.band = band;
_reset = false;
_flags.settings_provided = true;
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
unsigned long CatM1ConnectionHandler::getTime()
{
/* It is not safe to call GSM.getTime() since we don't know if modem internal
* RTC is in sync with current time.
*/
return 0;
}
/******************************************************************************
PROTECTED MEMBER FUNCTIONS
******************************************************************************/
NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
{
#if defined (ARDUINO_EDGE_CONTROL)
/* Power on module */
pinMode(ON_MKR2, OUTPUT);
digitalWrite(ON_MKR2, HIGH);
#endif
if(!GSM.begin(
_settings.catm1.pin,
_settings.catm1.apn,
_settings.catm1.login,
_settings.catm1.pass,
static_cast<RadioAccessTechnologyType>(_settings.catm1.rat) ,
_settings.catm1.band,
_reset))
{
DEBUG_ERROR(F("The board was not able to register to the network..."));
_reset = true;
return NetworkConnectionState::DISCONNECTED;
}
_reset = false;
return NetworkConnectionState::CONNECTING;
}
NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
{
if (!GSM.isConnected())
{
DEBUG_ERROR(F("GSM connection not alive... disconnecting"));
return NetworkConnectionState::DISCONNECTED;
}
if(!_flags.check_internet_availability){
return NetworkConnectionState::CONNECTED;
}
DEBUG_INFO(F("Sending PING to outer space..."));
int const ping_result = GSM.ping("time.arduino.cc");
DEBUG_INFO(F("GSM.ping(): %d"), ping_result);
if (ping_result < 0)
{
DEBUG_ERROR(F("Internet check failed"));
DEBUG_INFO(F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
else
{
DEBUG_INFO(F("Connected to Internet"));
return NetworkConnectionState::CONNECTED;
}
}
NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()
{
int const is_gsm_access_alive = GSM.isConnected();
if (is_gsm_access_alive != 1)
{
DEBUG_ERROR(F("GSM connection not alive... disconnecting"));
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
}
NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnecting()
{
GSM.disconnect();
return NetworkConnectionState::DISCONNECTED;
}
NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnected()
{
GSM.end();
if (_flags.keep_alive)
{
return NetworkConnectionState::INIT;
}
else
{
return NetworkConnectionState::CLOSED;
}
}
#endif /* #ifdef BOARD_HAS_CATM1_NBIOT */