-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathGSMConnectionHandler.cpp
149 lines (121 loc) · 4.65 KB
/
GSMConnectionHandler.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
144
145
146
147
148
149
/*
This file is part of ArduinoIoTCloud.
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "definitions/ConnectionHandlerDefinitions.h"
#ifdef BOARD_HAS_GSM /* Only compile if this is a board with GSM */
#include "handlers/GSMConnectionHandler.h"
/******************************************************************************
CONSTANTS
******************************************************************************/
static int const GSM_TIMEOUT = 30000;
static int const GPRS_TIMEOUT = 30000;
/******************************************************************************
FUNCTION DEFINITION
******************************************************************************/
__attribute__((weak)) void mkr_gsm_feed_watchdog()
{
/* This function can be overwritten by a "strong" implementation
* in a higher level application, such as the ArduinoIoTCloud
* firmware stack.
*/
}
/******************************************************************************
CTOR/DTOR
******************************************************************************/
GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
: ConnectionHandler{keep_alive, NetworkAdapter::GSM}
, _pin(pin)
, _apn(apn)
, _login(login)
, _pass(pass)
{
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
unsigned long GSMConnectionHandler::getTime()
{
return _gsm.getTime();
}
/******************************************************************************
PROTECTED MEMBER FUNCTIONS
******************************************************************************/
NetworkConnectionState GSMConnectionHandler::update_handleInit()
{
mkr_gsm_feed_watchdog();
if (_gsm.begin(_pin) != GSM_READY)
{
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
return NetworkConnectionState::ERROR;
}
mkr_gsm_feed_watchdog();
Debug.print(DBG_INFO, F("SIM card ok"));
_gsm.setTimeout(GSM_TIMEOUT);
_gprs.setTimeout(GPRS_TIMEOUT);
mkr_gsm_feed_watchdog();
GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(_apn, _login, _pass, true);
Debug.print(DBG_DEBUG, F("GPRS.attachGPRS(): %d"), network_status);
if (network_status == GSM3_NetworkStatus_t::ERROR)
{
Debug.print(DBG_ERROR, F("GPRS attach failed"));
Debug.print(DBG_ERROR, F("Make sure the antenna is connected and reset your board."));
return NetworkConnectionState::ERROR;
}
return NetworkConnectionState::CONNECTING;
}
NetworkConnectionState GSMConnectionHandler::update_handleConnecting()
{
Debug.print(DBG_INFO, F("Sending PING to outer space..."));
int const ping_result = _gprs.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("GPRS.ping(): %d"), ping_result);
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("PING failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
return NetworkConnectionState::CONNECTING;
}
else
{
Debug.print(DBG_INFO, F("Connected to GPRS Network"));
return NetworkConnectionState::CONNECTED;
}
}
NetworkConnectionState GSMConnectionHandler::update_handleConnected()
{
int const is_gsm_access_alive = _gsm.isAccessAlive();
if (is_gsm_access_alive != 1)
{
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
}
NetworkConnectionState GSMConnectionHandler::update_handleDisconnecting()
{
_gsm.shutdown();
return NetworkConnectionState::DISCONNECTED;
}
NetworkConnectionState GSMConnectionHandler::update_handleDisconnected()
{
if (_keep_alive)
{
return NetworkConnectionState::INIT;
}
else
{
return NetworkConnectionState::CLOSED;
}
}
#endif /* #ifdef BOARD_HAS_GSM */