forked from u-blox/XPLR-IOT-1-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_cell_saraR5.c
689 lines (503 loc) · 20.1 KB
/
x_cell_saraR5.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
* Copyright 2022 u-blox Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file
* @brief This file contains the function implementation of SARA-R5
* cellular module used in Sensor Aggregation use case (XPLR-IOT-1)
*/
#include "x_cell_saraR5.h"
#include "stdio.h"
#include "stddef.h"
#include "stdint.h"
#include "stdbool.h"
// Zephyr/SDK related
#include <zephyr.h>
#include <hal/nrf_gpio.h>
#include <logging/log.h>
//ubxlib related
#include "u_cfg_sw.h"
#include "u_cfg_app_platform_specific.h"
#include "u_error_common.h"
#include "u_port.h"
#include "u_port_debug.h"
#include "u_port_uart.h"
#include "u_at_client.h"
#include "u_cell.h"
#include "u_cell_net.h"
#include "u_cell_pwr.h"
#include "u_cell_cfg.h"
#include "u_network.h"
#include "u_network_config_cell.h"
//Sensor aggregation related
#include "x_logging.h"
#include "x_module_common.h"
#include "x_system_conf.h"
#include "x_module_common.h"
#include "x_cell_mqttsn.h"
#include "x_led.h"
#include "x_wifi_ninaW156.h"
/* ----------------------------------------------------------------
* STATIC FUNCTION DECLARATION
* -------------------------------------------------------------- */
/** Thread called by xCellSaraInit to configure/initialize the SARA-R5 module */
void xCellSaraInitThread(void);
/** Thread called by xCellSaraConnect to connect SARA-R5 module to a network*/
void xCellSaraConnectThread(void);
/** Thread called by xCellSaraDeinit to disconect, deinitialize and power off
* SARA-R5 module
*/
void xCellSaraDeinitThread(void);
/** Function called to configure the cellular module depending on the
* active Thingstream plan (Flex, Anywhere). This is supposed to be tweaked
* by the user, especially in the case of Flex plan.
*
* @return zero on success else negative error code.
*/
static int32_t xCellSaraRegistrationConfig(void);
/** Handle error happening in a thread
*/
static void saraErrorHandle(err_code err_code);
/** Disconnect from network
*/
static void saraDisconnect(void);
/** Callback triggered on changes of connection status
*/
static void saraConnectCb(bool isConnected, void *pParameter);
/** Callback triggered on changes of registration status
*/
static void saraRegisterCb(uCellNetRegDomain_t domain, uCellNetStatus_t reg_status, void *pParameter);
/* ----------------------------------------------------------------
* ZEPHYR RELATED DEFINITIONS/DECLARATIONS
* -------------------------------------------------------------- */
LOG_MODULE_REGISTER(LOGMOD_NAME_SARAR5, LOG_LEVEL_DBG);
// Semamphores definition
K_SEM_DEFINE(xCellSaraInit_semaphore, 0, 1);
K_SEM_DEFINE(xCellSaraConnect_semaphore, 0, 1);
K_SEM_DEFINE(xCellSaraDeinit_semaphore, 0, 1);
// Threads definition
K_THREAD_DEFINE(xCellSaraInitThreadId, SARAR5_STACK_SIZE, xCellSaraInitThread, NULL, NULL, NULL,
SARAR5_PRIORITY, 0, 0);
K_THREAD_DEFINE(xCellSaraConnectThreadId, SARAR5_STACK_SIZE, xCellSaraConnectThread, NULL, NULL, NULL,
SARAR5_PRIORITY, 0, 0);
K_THREAD_DEFINE(xCellSaraDeinitThreadId, SARAR5_STACK_SIZE, xCellSaraDeinitThread, NULL, NULL, NULL,
SARAR5_PRIORITY, 0, 0);
/* ----------------------------------------------------------------
* GLOBALS
* -------------------------------------------------------------- */
/** Holds SARA status (as regards Sensor Aggregation application)*/
xCellSaraStatus_t gSaraStatus={
.pinsConfigured = false,
.poweredUp = false,
.uStatus = uPortNotInitialized,
.isRegistered = false,
.isConnected = false,
.isReadyToConnect = false,
.isReadyForMqttSN = false
};
/** Holds the result of the last operation performed by this module (refers to operations that need to report their result
* to other modules of the application and cannot report their results directly e.g. a thread operation) */
static err_code gLastOperationResult = X_ERR_SUCCESS;
/** Network Handle returned and used by ubxlib functions */
int32_t gNetHandle;
xCellMqttSnPlan_t gMqttSnActivePlan = MQTTSN_DEFAULT_PLAN;
// Configure depending on the SIM card used (MQTT Flex plan in Thingstream portal)
const uNetworkConfigurationCell_t gCellSaraFlexConfig = {U_NETWORK_TYPE_CELL,
U_CELL_MODULE_TYPE_SARA_R5,
NULL, /* SIM pin */
"iot.1nce.net", /* APN: accept default */
300, /* Connection timeout in seconds (240)*/
2,
-1,
-1,
-1,
-1,
-1,
-1,
-1};
// Preconfigured for Thingstream SIM cards (MQTT Anywhere plan in Thingstream portal)
const uNetworkConfigurationCell_t gCellSaraAnywhereConfig = {U_NETWORK_TYPE_CELL,
U_CELL_MODULE_TYPE_SARA_R5,
NULL, /* SIM pin */
"TSUDP", /* APN: accept default */
300, /* Connection timeout in seconds (240)*/
2,
-1,
-1,
-1,
-1,
-1,
-1,
-1};
/* ----------------------------------------------------------------
* CALLBACKS
* -------------------------------------------------------------- */
static void saraConnectCb(bool isConnected, void *pParameter){
gSaraStatus.isConnected = isConnected;
if(gSaraStatus.isConnected){
LOG_INF("Cell connected \r\n");
}
else{
LOG_WRN("Cell disconnected \r\n");
}
}
static void saraRegisterCb(uCellNetRegDomain_t domain, uCellNetStatus_t reg_status, void *pParameter){
if( reg_status <= U_CELL_NET_STATUS_NOT_REGISTERED ){
gSaraStatus.isRegistered = false;
LOG_WRN("Cell not Registered \r\n");
}
else{
gSaraStatus.isRegistered = true;
LOG_INF("Cell Registered \r\n");
}
}
/* ----------------------------------------------------------------
* STATIC FUNCTION IMPLEMENTATION
* -------------------------------------------------------------- */
static void saraErrorHandle(err_code err_code){
gLastOperationResult = err_code;
xLedBlink( ERROR_LEDCOL, ERROR_LED_DELAY_ON, ERROR_LED_DELAY_OFF, ERROR_LED_BLINKS);
}
static int32_t xCellSaraRegistrationConfig(void){
int32_t ret;
// if uNetworkAdd has not been added we cannot use AtClient to send AT commands
if(gSaraStatus.uStatus<uNetAdded){
LOG_WRN("Config Reg to MNO: Initialize first\r\n");
return -1; //err code invalid state
}
if( gSaraStatus.isRegistered || gSaraStatus.isConnected ){
LOG_WRN("Config Reg to MNO: Disconnect(deinit) and initialize again\r\n");
return -1; //err_code invalid state
}
// two situations have been programmed one when using a Thingstream card and one for
// using a 1nce SIM card over an NB-IoT newtork.
// The user may change this according to needs
if( gMqttSnActivePlan == FLEX){
// just check the URAT configuration (if its NB-IoT)
uCellNetRat_t rat = uCellCfgGetRat(gNetHandle, 0);
if( rat == U_CELL_NET_RAT_NB1 ){
LOG_INF("Urat is ok: %d", rat);
}
else{
LOG_INF("Urat is not ok: %d", rat);
// will change urat to NB-IoT
LOG_INF("Setting urat to: %d", U_CELL_NET_RAT_NB1);
ret = uCellCfgSetRat(gNetHandle, U_CELL_NET_RAT_NB1);
if( ret == 0 ){
LOG_INF("Rebooting module \r\n");
ret = uCellPwrReboot(gNetHandle, NULL);
if(ret<0){
return ret;
}
LOG_INF("Module restarted\r\n");
}
else if(ret<0){
return ret;
}
}
}
if( gMqttSnActivePlan == ANYWHERE ){
// according to thingstream documentation for MQTT Anywhere URAT should be <LTE Cat M1> or <GPRS>
// (this is implied by command: AT+URAT=9,7)
// in SARA-R5 only AT+URAT=9 is NOT supported, so we set it to 7 <LTE Cat M1>
uCellNetRat_t rat = uCellCfgGetRat(gNetHandle, 0);
if( rat == U_CELL_NET_RAT_CATM1 ){
LOG_INF("Urat is ok: %d", rat);
}
else{
LOG_INF("Urat is not ok: %d", rat);
// will change urat to NB-IoT
LOG_INF("Setting urat to: %d", U_CELL_NET_RAT_CATM1);
ret = uCellCfgSetRat(gNetHandle, U_CELL_NET_RAT_CATM1);
if( ret == 0 ){
LOG_INF("Rebooting module \r\n");
ret = uCellPwrReboot(gNetHandle, NULL);
if(ret<0){
return ret;
}
LOG_INF("Module restarted\r\n");
}
else if(ret<0){
return ret;
}
}
}
return 0;
}
void xCellSaraInitThread(void){
// needed to avoid thread overflows when using ubxlib functions within a thread
k_thread_system_pool_assign(k_current_get());
while(1){
// Semaphore given by xCellSaraInit()
k_sem_take( &xCellSaraInit_semaphore, K_FOREVER );
// if WiFi is using ubxlib, abort
//( Wifi should be deinitiliazed fisrt )
if( xCommonUPortIsInit() ){
xWifiNinaStatus_t ninaStatus = xWifiNinaGetModuleStatus();
if( ninaStatus.uStatus >= uPortInitialized ){
// deinitialize previous uPort to config SARA
LOG_ERR("uPort is used by WiFi\r\n");
saraErrorHandle( X_ERR_INVALID_STATE );
continue;
}
}
//optical indication
xLedFade(CELL_ACTIVATING_LEDCOL, CELL_ACTIVATING_LED_DELAY_ON, CELL_ACTIVATING_LED_DELAY_OFF, 0);
// Power On SARA if necessary
if(!gSaraStatus.poweredUp){
xCellSaraPowerOn();
}
// Configure uart
xCommonUartCfg(COMMON_UART_SARA);
// Initialize ubxlib for use with Zephyr
if( !xCommonUPortIsInit() ){
gLastOperationResult = xCommonUPortInit();
if( gLastOperationResult != X_ERR_SUCCESS ){
LOG_ERR("Could not initialize SARA \r\n");
saraErrorHandle( gLastOperationResult );
continue;
}
}
gSaraStatus.uStatus = uPortInitialized;
LOG_INF("Port Initialized\r\n");
// Network Initialization - Configuration
gLastOperationResult = uNetworkInit();
if( gLastOperationResult != X_ERR_SUCCESS ){
saraErrorHandle( gLastOperationResult );
continue; //err
}
gSaraStatus.uStatus = uNetInitialized;
// add approprtiate configuration according to plan selected
if( gMqttSnActivePlan == FLEX ){
gNetHandle = uNetworkAdd(U_NETWORK_TYPE_CELL, (void *) &gCellSaraFlexConfig);
}
else if( gMqttSnActivePlan == ANYWHERE ){
gNetHandle = uNetworkAdd(U_NETWORK_TYPE_CELL, (void *) &gCellSaraAnywhereConfig);
}
if( gNetHandle < 0 ){
LOG_ERR("networkAdd error: %d\r\n",gNetHandle);
saraErrorHandle( gNetHandle );
continue;
}
else{
LOG_INF("Network Added\r\n");
gSaraStatus.uStatus = uNetAdded;
}
// Cellular Configuration that might be needed before connecting to a
// mobile network provider
gLastOperationResult = xCellSaraRegistrationConfig();
if( gLastOperationResult != X_ERR_SUCCESS ){
saraErrorHandle( gLastOperationResult );
continue;
}
gSaraStatus.isReadyToConnect = true;
xLedOff();
}
}
void xCellSaraDeinitThread(void){
while(1){
// Semaphore given by xCellSaraDeinit()
k_sem_take( &xCellSaraDeinit_semaphore, K_FOREVER );
LOG_INF("SARA deinit request\r\n");
xCommonUPortDeinit();
// Wait for deinitialization to complete
while( xCommonUPortIsInit() ){
k_sleep(K_MSEC(500));
}
LOG_INF("Module Deinitialized\r\n");
gSaraStatus.uStatus = uPortNotInitialized;
// already off at this point (powered down by ubxlib)
xCellSaraPowerOff();
}
}
void xCellSaraConnectThread(void){
while(1){
// Semaphore given by xCellSaraConnect
k_sem_take( &xCellSaraConnect_semaphore, K_FOREVER );
// if not ready to connect, prepare module for connection request
gLastOperationResult = X_ERR_SUCCESS;
if( !gSaraStatus.isReadyToConnect ){
xCellSaraInit();
while( ( !gSaraStatus.isReadyToConnect ) && ( gLastOperationResult == X_ERR_SUCCESS ) ){
k_sleep(K_MSEC(1000));
}
if( gLastOperationResult != X_ERR_SUCCESS ){
continue;
}
}
// optical indication
xLedFade(CELL_ACTIVATING_LEDCOL, CELL_ACTIVATING_LED_DELAY_ON, CELL_ACTIVATING_LED_DELAY_OFF, 0);
//register callbacks
uCellNetSetRegistrationStatusCallback(gNetHandle,saraRegisterCb,NULL);
uCellNetSetBaseStationConnectionStatusCallback(gNetHandle, saraConnectCb,NULL);
// Bring up the network layer
LOG_INF("Connecting to network \r\n");
gLastOperationResult = uNetworkUp( gNetHandle );
if( gLastOperationResult != X_ERR_SUCCESS ){
LOG_WRN("Connect to network err code: %d \r\n", gLastOperationResult );
saraErrorHandle( gLastOperationResult );
continue;
}
LOG_INF("Connected to network \r\n");
gSaraStatus.isReadyForMqttSN = true;
xLedOff();
}
}
static void saraDisconnect(void){
LOG_INF("Cell Network Down request\r\n");
xLedFade(CELL_DEACTIVATING_LEDCOL, CELL_ACTIVATING_LED_DELAY_ON, CELL_ACTIVATING_LED_DELAY_OFF, 0);
// this function also powers off the module, so we should also deinit after using it
int32_t ret = uNetworkDown(gNetHandle);
if( ret != X_ERR_SUCCESS ){
LOG_ERR("Error while bringing network down: %d \r\n", ret);
}
LOG_INF("Cell Netowrk Down\r\n" );
gSaraStatus.isConnected = false;
gSaraStatus.isRegistered = false;
gSaraStatus.isReadyForMqttSN = false;
xLedOff();
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTION IMPLEMENTATION
* -------------------------------------------------------------- */
void xCellSaraActivateMqttFlexPlan(void){
if( gSaraStatus.uStatus > uPortNotInitialized ){
LOG_ERR("Cannot Change Plan when module is initialized. SARAR5 must be deinitialized fisrt \r\n");
return;
}
gMqttSnActivePlan = FLEX;
}
void xCellSaraActivateMqttAnywherePlan(void){
if( gSaraStatus.uStatus > uPortNotInitialized ){
LOG_ERR("Cannot Change Plan when module is initialized. SARAR5 must be deinitialized fisrt \r\n");
return;
}
gMqttSnActivePlan = ANYWHERE;
}
xCellMqttSnPlan_t xCellSaraGetActiveMqttPlan(void){
return gMqttSnActivePlan;
}
xCellSaraStatus_t xCellSaraGetModuleStatus(){
return gSaraStatus;
}
void xCellSaraConfigPins(void){
nrf_gpio_cfg_output((uint32_t) NORA_EN_SARA_PIN);
nrf_gpio_cfg_output((uint32_t) SARA_PWR_ON_PIN);
nrf_gpio_cfg_output((uint32_t) SARA_RST_PIN);
nrf_gpio_cfg_output((uint32_t) SARA_INT_PIN);
nrf_gpio_pin_clear((uint32_t) SARA_INT_PIN);
gSaraStatus.pinsConfigured = true;
LOG_INF("ouput pins configured \r\n");
}
void xCellSaraPowerOn(void)
{
if(gSaraStatus.poweredUp){
LOG_INF("Already powered up\r\n");
return; //success
}
if( !gSaraStatus.pinsConfigured ){
xCellSaraConfigPins();
}
// Power on sequence
// Apply power to the module and wait a little time
nrf_gpio_pin_set((uint32_t) NORA_EN_SARA_PIN);
k_sleep(K_MSEC(100));
//assert POWER_ON pin for a valid time
nrf_gpio_pin_set((uint32_t) SARA_PWR_ON_PIN);
k_sleep(K_MSEC(1200));
//deassert POWER_ON pin
nrf_gpio_pin_clear((uint32_t) SARA_PWR_ON_PIN);
gSaraStatus.poweredUp = true;
LOG_INF("Module powered up\r\n");
}
err_code xCellSaraPowerOff(void){
if(!gSaraStatus.pinsConfigured){
xCellSaraConfigPins();
}
if(!gSaraStatus.poweredUp){
LOG_INF("Already powered down\r\n");
return X_ERR_SUCCESS;
}
if( gSaraStatus.uStatus > uPortNotInitialized ){
LOG_ERR("Module network is initialized. Cannot power down. Use deinit command\r\n");
return X_ERR_INVALID_STATE;
}
nrf_gpio_pin_clear((uint32_t) SARA_PWR_ON_PIN);
nrf_gpio_pin_clear((uint32_t) NORA_EN_SARA_PIN);
LOG_INF("Module powered down\r\n");
gSaraStatus.poweredUp = false;
return X_ERR_SUCCESS;
}
void xCellSaraInit(void){
k_sem_give( &xCellSaraInit_semaphore );
}
void xCellSaraConnect(void){
k_sem_give( &xCellSaraConnect_semaphore );
}
void xCellSaraDeinit(void){
k_sem_give( &xCellSaraDeinit_semaphore );
}
void xCellSaraNetworkDeinit(void){
LOG_INF("SARA Network Deinit request\r\n");
// check Mqtt-SN status and close if necessary prior to
// deinitializing the network
xClientStatus_t mqttsn_stat = xCellMqttSnClientGetStatus();
if( mqttsn_stat > ClientClosed ){
xCellMqttSnClientClose();
// wait for Mqtt-SN to close
while( mqttsn_stat > ClientClosed ){
k_sleep( K_MSEC( 1000 ) );
mqttsn_stat = xCellMqttSnClientGetStatus();
// we do not check for errors here, continue anyway
}
}
// if network is up (connected)
if( gSaraStatus.isReadyForMqttSN ){
LOG_WRN("SARA Network is up. Brigning Network Down now\r\n");
saraDisconnect();
}
while( gSaraStatus.isReadyForMqttSN ){
//wait for disconnection
}
xLedFade(CELL_DEACTIVATING_LEDCOL, CELL_ACTIVATING_LED_DELAY_ON, CELL_ACTIVATING_LED_DELAY_OFF, 0);
uNetworkDeinit(); //you can also just remove the network
gSaraStatus.isReadyToConnect = false;
gSaraStatus.uStatus = uPortInitialized;
LOG_INF("Net Deinitialized \r\n");
xLedOff();
}
int32_t xCellSaraGetHandle( void ){
if( gSaraStatus.uStatus < uNetAdded ){
LOG_WRN("GetNetworkHandle error: Net not Added yet");
return -1; //to do: return negative err code?
}
else{
return gNetHandle;
}
}
err_code xCellSaraGetLastOperationResult( void ){
return gLastOperationResult;
}
/* ----------------------------------------------------------------
* SHELL COMMANDS IMPLEMENTATION
* -------------------------------------------------------------- */
void xCellSaraGetActiveMqttPlanCmd( const struct shell *shell, size_t argc, char **argv ){
if( gMqttSnActivePlan == FLEX )
shell_print( shell, "Active Plan is MQTT Flex \r\n" );
else if( gMqttSnActivePlan == ANYWHERE )
shell_print( shell, "Active Plan is MQTT Anywhere \r\n" );
else{
shell_error( shell, "Active Plan error \r\n" );
}
}