1+ /*
2+ This file is part of ArduinoIoTCloud.
3+
4+ Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
6+ This software is released under the GNU General Public License version 3,
7+ which covers the main part of arduino-cli.
8+ The terms of this license can be found at:
9+ https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+ You can be released from the requirements of the above licenses by purchasing
12+ a commercial license. Buying such a license is mandatory if you want to modify or
13+ otherwise use the software for commercial activities involving the Arduino
14+ software without disclosing the source code of your own applications. To purchase
15+ a commercial license, send an email to [email protected] . 16+ */
17+
18+ /* *****************************************************************************
19+ INCLUDE
20+ ******************************************************************************/
21+
22+ /*
23+ static int const DBG_NONE = -1;
24+ static int const DBG_ERROR = 0;
25+ static int const DBG_WARNING = 1;
26+ static int const DBG_INFO = 2;
27+ static int const DBG_DEBUG = 3;
28+ static int const DBG_VERBOSE = 4;
29+ */
30+
31+ #include " Arduino_NBConnectionHandler.h"
32+
33+ #ifdef BOARD_HAS_NB /* Only compile if this is a board with NB */
34+
35+ /* *****************************************************************************
36+ CONSTANTS
37+ ******************************************************************************/
38+
39+ static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000 ;
40+
41+ /* *****************************************************************************
42+ CTOR/DTOR
43+ ******************************************************************************/
44+
45+ NBConnectionHandler::NBConnectionHandler (const char *pin, bool _keepAlive) :
46+ pin(pin),
47+ lastConnectionTickTime(millis()),
48+ connectionTickTimeInterval(CHECK_INTERVAL_IDLE),
49+ keepAlive(_keepAlive),
50+ _on_connect_event_callback(NULL ),
51+ _on_disconnect_event_callback(NULL ),
52+ _on_error_event_callback(NULL ) {
53+ }
54+
55+ /* *****************************************************************************
56+ PUBLIC MEMBER FUNCTIONS
57+ ******************************************************************************/
58+
59+ void NBConnectionHandler::init () {
60+ char msgBuffer[120 ];
61+ if (nbAccess.begin (pin) == NB_READY) {
62+ Debug.print (DBG_INFO, " SIM card ok" );
63+ nbAccess.setTimeout (CHECK_INTERVAL_RETRYING);
64+ changeConnectionState (NetworkConnectionState::CONNECTING);
65+ } else {
66+ Debug.print (DBG_ERROR, " SIM not present or wrong PIN" );
67+ while (1 );
68+ }
69+ }
70+
71+ void NBConnectionHandler::addCallback (NetworkConnectionEvent const event, OnNetworkEventCallback callback) {
72+ switch (event) {
73+ case NetworkConnectionEvent::CONNECTED: _on_connect_event_callback = callback; break ;
74+ case NetworkConnectionEvent::DISCONNECTED: _on_disconnect_event_callback = callback; break ;
75+ case NetworkConnectionEvent::ERROR: _on_error_event_callback = callback; break ;
76+ case NetworkConnectionEvent::INIT: ; break ;
77+ case NetworkConnectionEvent::CONNECTING: ; break ;
78+ case NetworkConnectionEvent::DISCONNECTING: ; break ;
79+ case NetworkConnectionEvent::CLOSED: ; break ;
80+ }
81+ }
82+
83+ void NBConnectionHandler::addConnectCallback (OnNetworkEventCallback callback) {
84+ _on_connect_event_callback = callback;
85+ }
86+ void NBConnectionHandler::addDisconnectCallback (OnNetworkEventCallback callback) {
87+ _on_disconnect_event_callback = callback;
88+ }
89+ void NBConnectionHandler::addErrorCallback (OnNetworkEventCallback callback) {
90+ _on_error_event_callback = callback;
91+ }
92+
93+ void NBConnectionHandler::execNetworkEventCallback (OnNetworkEventCallback & callback, void * callback_arg) {
94+ if (callback) {
95+ (*callback)(callback_arg);
96+ }
97+ }
98+
99+ unsigned long NBConnectionHandler::getTime () {
100+ return nbAccess.getTime ();
101+ }
102+
103+ void NBConnectionHandler::update () {
104+ unsigned long const now = millis ();
105+ int nbAccessAlive;
106+ if (now - lastConnectionTickTime > connectionTickTimeInterval) {
107+ switch (netConnectionState) {
108+ case NetworkConnectionState::INIT: {
109+ init ();
110+ }
111+ break ;
112+
113+ case NetworkConnectionState::CONNECTING: {
114+ // NOTE: Blocking Call when 4th parameter == true
115+ NB_NetworkStatus_t networkStatus;
116+ networkStatus = gprs.attachGPRS ();
117+ Debug.print (DBG_DEBUG, " GPRS.attachGPRS(): %d" , networkStatus);
118+ if (networkStatus == NB_NetworkStatus_t::ERROR) {
119+ // NO FURTHER ACTION WILL FOLLOW THIS
120+ changeConnectionState (NetworkConnectionState::ERROR);
121+ return ;
122+ }
123+ Debug.print (DBG_INFO, " Sending PING to outer space..." );
124+ int pingResult;
125+ // pingResult = gprs.ping("time.arduino.cc");
126+ // Debug.print(DBG_INFO, "NB.ping(): %d", pingResult);
127+ // if (pingResult < 0) {
128+ if (pingResult < 0 ) {
129+ Debug.print (DBG_ERROR, " PING failed" );
130+ Debug.print (DBG_INFO, " Retrying in \" %d\" milliseconds" , connectionTickTimeInterval);
131+ return ;
132+ } else {
133+ Debug.print (DBG_INFO, " Connected to GPRS Network" );
134+ changeConnectionState (NetworkConnectionState::CONNECTED);
135+ return ;
136+ }
137+ }
138+ break ;
139+ case NetworkConnectionState::CONNECTED: {
140+ nbAccessAlive = nbAccess.isAccessAlive ();
141+ Debug.print (DBG_VERBOSE, " GPRS.isAccessAlive(): %d" , nbAccessAlive);
142+ if (nbAccessAlive != 1 ) {
143+ changeConnectionState (NetworkConnectionState::DISCONNECTED);
144+ return ;
145+ }
146+ Debug.print (DBG_VERBOSE, " Connected to Cellular Network" );
147+ }
148+ break ;
149+ case NetworkConnectionState::DISCONNECTED: {
150+ // gprs.detachGPRS();
151+ if (keepAlive) {
152+ Debug.print (DBG_VERBOSE, " keep alive > INIT" );
153+ changeConnectionState (NetworkConnectionState::INIT);
154+ } else {
155+ changeConnectionState (NetworkConnectionState::CLOSED);
156+ }
157+ // changeConnectionState(NetworkConnectionState::CONNECTING);
158+ }
159+ break ;
160+ }
161+ lastConnectionTickTime = now;
162+ }
163+ }
164+
165+ /* *****************************************************************************
166+ PRIVATE MEMBER FUNCTIONS
167+ ******************************************************************************/
168+
169+ void NBConnectionHandler::changeConnectionState (NetworkConnectionState _newState) {
170+ int newInterval = CHECK_INTERVAL_IDLE;
171+ switch (_newState) {
172+ case NetworkConnectionState::INIT: {
173+ newInterval = CHECK_INTERVAL_INIT;
174+ }
175+ break ;
176+ case NetworkConnectionState::CONNECTING: {
177+ Debug.print (DBG_INFO, " Connecting to Cellular Network" );
178+ newInterval = CHECK_INTERVAL_CONNECTING;
179+ }
180+ break ;
181+ case NetworkConnectionState::CONNECTED: {
182+ execNetworkEventCallback (_on_connect_event_callback, 0 );
183+ newInterval = CHECK_INTERVAL_CONNECTED;
184+ }
185+ break ;
186+ case NetworkConnectionState::GETTIME: {
187+ }
188+ break ;
189+ case NetworkConnectionState::DISCONNECTING: {
190+ Debug.print (DBG_VERBOSE, " Disconnecting from Cellular Network" );
191+ nbAccess.shutdown ();
192+ }
193+ case NetworkConnectionState::DISCONNECTED: {
194+ if (netConnectionState == NetworkConnectionState::CONNECTED) {
195+ execNetworkEventCallback (_on_disconnect_event_callback, 0 );
196+ Debug.print (DBG_ERROR, " Disconnected from Cellular Network" );
197+ Debug.print (DBG_ERROR, " Attempting reconnection" );
198+ if (keepAlive) {
199+ Debug.print (DBG_ERROR, " Attempting reconnection" );
200+ }
201+ }
202+ newInterval = CHECK_INTERVAL_DISCONNECTED;
203+ }
204+ break ;
205+ case NetworkConnectionState::ERROR: {
206+ execNetworkEventCallback (_on_error_event_callback, 0 );
207+ Debug.print (DBG_ERROR, " GPRS attach failed\n\r Make sure the antenna is connected and reset your board." );
208+ }
209+ break ;
210+ }
211+ connectionTickTimeInterval = newInterval;
212+ lastConnectionTickTime = millis ();
213+ netConnectionState = _newState;
214+ }
215+
216+
217+ void NBConnectionHandler::connect () {
218+ if (netConnectionState == NetworkConnectionState::INIT || netConnectionState == NetworkConnectionState::CONNECTING) {
219+ return ;
220+ }
221+ keepAlive = true ;
222+ changeConnectionState (NetworkConnectionState::INIT);
223+
224+ }
225+ void NBConnectionHandler::disconnect () {
226+ // WiFi.end();
227+
228+ changeConnectionState (NetworkConnectionState::DISCONNECTING);
229+ keepAlive = false ;
230+ }
231+
232+ #endif /* #ifdef BOARD_HAS_NB */
0 commit comments