-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp_lwgsm.c
602 lines (505 loc) · 16.8 KB
/
esp_lwgsm.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
/**
* @file esp_lwgsm.c
* @author Rafael de la Rosa Vidal ([email protected])
* @brief API functions over LWGSM library adapted to SIM7080G
* @version 0.1
* @date 2022-12-17
*
* @copyright Copyright (c) 2022
*
*/
/* LWGSM includes */
#include "lwgsm/lwgsm_netconn.h"
#include "lwgsm/lwgsm_network_api.h"
#include "lwgsm/lwgsm_certs.h"
#include "lwgsm/lwgsm_mem.h"
/* ESP IDF includes */
#include "esp_log.h"
/* Utils */
#include "sim_manager.h"
#include "network_utils.h"
#include "operator_utils.h"
/* AWS provisioning */
#include "aws_provisioning.h"
/* Own include */
#include "esp_lwgsm.h"
/*******************************************************************************
*
* Defines
*
*******************************************************************************/
#define ESP_LWGSM_MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045
#define ESP_LWGSM_MBEDTLS_ERR_NET_SEND_FAILED -0x004E
#define ESP_LWGSM_MBEDTLS_ERR_NET_RECV_FAILED -0x004C
#define ESP_LWGSM_MBEDTLS_ERR_SSL_WANT_READ -0x6900
#define CHECK_LWGSMOK(x) do{ if((x) != lwgsmOK){ return -1; }} while(0)
/*******************************************************************************
*
* Typedefs, enums and structs
*
*******************************************************************************/
typedef struct esp_lwgsm_mbedtls_net_context
{
int fd; /**< The underlying file descriptor */
} esp_lwgsm_mbedtls_net_context_t;
/*******************************************************************************
*
* Static variables
*
*******************************************************************************/
static const char* TAG = "ESP_LWGSM";
static uint8_t initFlag = 0;
static lwgsm_sim_state_t simState;
static lwgsm_netconn_p pClient;
static lwgsm_pbuf_p pbuf;
/*******************************************************************************
*
* Private function prototypes
*
*******************************************************************************/
static lwgsmr_t esp_lwgsm_event_cb(lwgsm_evt_t* evt);
static lwgsm_evt_fn esp_lwgsm_user_cb;
static esp_err_t prv_esp_lwgsm_init(lwgsm_evt_fn evt_func, uint8_t reinit);
/*******************************************************************************
*
* Public function bodies
*
*******************************************************************************/
/**
* \brief Initialize the LWGSM stack, reset the GSM module, unlock SIM card and attach to operator network
* \return \ref lwgsmOK on success, member of \ref lwgsmr_t otherwise
*/
esp_err_t esp_lwgsm_init(lwgsm_evt_fn evt_func)
{
return prv_esp_lwgsm_init(evt_func, 0);
}
/**
* \brief Reset the GSM module, unlock SIM card and attach to operator network
* \return \ref lwgsmOK on success, member of \ref lwgsmr_t otherwise
*/
esp_err_t esp_lwgsm_reinit()
{
return prv_esp_lwgsm_init(NULL, 1);
}
/**
* @brief Reset the GSM module
*
* @return esp_err_t ESP_OK if success, ESP_FAIL otherwise
*/
esp_err_t esp_lwgsm_reset_sw()
{
lwgsmr_t ret = lwgsmOK;
if(lwgsm_network_is_attached()){
ret = lwgsm_network_request_detach();
}
if(ret == lwgsmOK){
ret = lwgsm_reset(NULL, NULL, 1);
do{
ret = lwgsm_check_at(1);
}while(ret != lwgsmOK);
ret = lwgsm_config_module(NULL, NULL, 1);
}
return ret == lwgsmOK ? ESP_OK : ESP_FAIL;
}
/**
* \brief Creates a TCP connection to a specified host and port
* \param[in] fd: Socket handler. The number of the 'lwgsm_conn_t' to use
* \param[in] host: The host address
* \param[in] port: The connection port
* \param[in] block: Status whether command should be blocking or not
* \return \ref ESP_OK on success, member of \ref esp_err_t otherwise
*/
esp_err_t esp_lwgsm_connect(int* fd, const char* host, int port, uint8_t block)
{
esp_err_t res;
lwgsmr_t ret = lwgsmERR;
pClient = lwgsm_netconn_new(LWGSM_NETCONN_TYPE_TCP);
if (pClient != NULL) {
ESP_LOGD(TAG, "Connecting to %s:%d", host, port);
if(block){
ret = lwgsm_netconn_connect(pClient, host, port);
if(ret == lwgsmOK){
ESP_LOGD(TAG, "TCP connection creation success.");
}else{
ESP_LOGE(TAG, "TCP connection creation error. Code %d", ret);
}
}else{
ret = lwgsm_netconn_connect_async(pClient, host, port);
if(ret == lwgsmOK){
ESP_LOGD(TAG, "Connection request sended.");
}else{
ESP_LOGE(TAG, "Connection request sending error. Code %d", ret);
}
}
}
if(ret != lwgsmOK){
ESP_LOGE(TAG, "Error connecting server. Code %d", ret);
}
*fd = lwgsm_netconn_getconnnum(pClient);
res = ret == lwgsmOK ? ESP_OK : ESP_FAIL;
return res;
}
/**
* \brief Close a connection
* \param[in] fd: Socket handler. The number of the 'lwgsm_conn_t' to use
* \return \ref ESP_OK on success, member of \ref esp_err_t otherwise
*/
esp_err_t esp_lwgsm_close(int fd)
{
lwgsmr_t ret1, ret2;
ESP_LOGI(TAG, "On connection closing...");
if(lwgsm_netconn_getconnnum(pClient) != fd){
return -1;
}
ret1 = lwgsm_netconn_close(pClient);
if(ret1 != lwgsmOK){
ESP_LOGE(TAG, "Connection close error. Code %d", ret1);
}else{
ESP_LOGI(TAG, "Connection closed.");
}
ret2 = lwgsm_netconn_delete(pClient);
if(ret2 != lwgsmOK){
ESP_LOGE(TAG, "Connection delete error. Code %d", ret2);
}
pClient = NULL;
return (ret1 == lwgsmOK && ret2 == lwgsmOK) ? 0 : -1;
}
/**
* \brief Send data over a connection stablished
* \param[in] fd: Socket handler. The number of the 'lwgsm_conn_t' to use
* \param[in] data: Pointer to data to send
* \param[in] datalen: Number of bytes to send
* \param[in] flags: (Not used) Added for compatibility purposes
* \return \ref Number of bytes sent, -1 in case of error or timeout
*/
int esp_lwgsm_send(int fd, const char* data, size_t datalen, int flags)
{
lwgsmr_t ret;
(void) flags;
if(lwgsm_netconn_getconnnum(pClient) != fd){
return -1;
}
if(!lwgsm_netconn_is_connected(pClient)){
return -1;
}
ret = lwgsm_netconn_write(pClient, data, datalen);
CHECK_LWGSMOK(ret);
ret = lwgsm_netconn_flush(pClient);
CHECK_LWGSMOK(ret);
return datalen;
}
/**
* \brief Read data from a stablished connection
* \param[in] fd: Socket handler. The number of the 'lwgsm_conn_t' to use
* \param[in] data: Pointer to a buffer where the data is going to be stored
* \param[in] datalen: Number of bytes to receive at most
* \param[in] flags: (Not used) Added for compatibility purposes
* \return \ref Number of bytes read, -1 in case of error or timeout
*/
int esp_lwgsm_recv(int fd, char* data, size_t datalen, int flags)
{
lwgsmr_t ret;
int data_recv = -1;
int to_copy = 0;
static int pbuf_tot_len;
static int offset = 0;
(void) flags;
if(lwgsm_netconn_getconnnum(pClient) != fd){
ESP_LOGE(TAG, "Error getting the connection number.");
return -1;
}
if(!lwgsm_netconn_is_connected(pClient)){
ESP_LOGE(TAG, "The client is disconnected.");
return -1;
}
if(pbuf == NULL){
ret = lwgsm_netconn_receive_manual(pClient, &pbuf, datalen);
if(ret == lwgsmTIMEOUT){
return 0;
}
else if(ret != lwgsmOK){
ESP_LOGE(TAG, "lwgsm_netconn_receive_manual returns with code(%d).", ret);
return -1;
}
pbuf_tot_len = lwgsm_pbuf_length(pbuf, 1);
}
if(pbuf != NULL){
to_copy = LWGSM_MIN(pbuf_tot_len, datalen);
data_recv = lwgsm_pbuf_copy(pbuf, data, to_copy, offset);
offset += data_recv;
if(offset == pbuf_tot_len){
lwgsm_pbuf_free(pbuf);
offset = 0;
pbuf = NULL;
}
}
if(data_recv != to_copy){
ESP_LOGD(TAG, "RECV: %d - QUERY: %d) ", data_recv, to_copy);
}
ESP_LOGD(TAG, "Returned %d / %d", data_recv, datalen);
return data_recv;
}
/**
* \brief esp_lwgsm_send wrapper to be compatible with esp_mbedtls
* \param[in] ctx: Network context, see \ref 'esp_lwgsm_mbedtls_net_context_t'
* \param[in] data: Pointer to data to send
* \param[in] datalen: Number of bytes to receive at most
* \return \ref Number of bytes read, -1 in case of error or timeout
*/
int esp_lwgsm_mbedtls_send(void* ctx, const unsigned char* data, size_t datalen)
{
int ret;
int fd = ((esp_lwgsm_mbedtls_net_context_t *) ctx)->fd;
if ( fd < 0 ) {
return ( ESP_LWGSM_MBEDTLS_ERR_NET_INVALID_CONTEXT );
}
ret = esp_lwgsm_send(fd, (const char*) data, datalen, 0);
if(ret < 0){
return ( ESP_LWGSM_MBEDTLS_ERR_NET_SEND_FAILED );
}
return ( ret );
}
/**
* \brief esp_lwgsm_recv wrapper to be compatible with esp_mbedtls
* \param[in] ctx: Network context, see \ref 'esp_lwgsm_mbedtls_net_context_t'
* \param[in] data: Pointer to a buffer where the data is going to be stored
* \param[in] datalen: Number of bytes to receive at most
* \return \ref Number of bytes read, -1 in case of error or timeout
*/
int esp_lwgsm_mbedtls_recv(void* ctx, unsigned char* data, size_t datalen)
{
int ret;
int fd = ((esp_lwgsm_mbedtls_net_context_t *) ctx)->fd;
if ( fd < 0 ) {
return ( ESP_LWGSM_MBEDTLS_ERR_NET_INVALID_CONTEXT );
}
ret = esp_lwgsm_recv(fd, (char*) data, datalen, 0);
if(ret < 0){
return ( ESP_LWGSM_MBEDTLS_ERR_NET_RECV_FAILED );
}
else if(ret == 0){
return ( ESP_LWGSM_MBEDTLS_ERR_SSL_WANT_READ );
}
return ( ret );
}
/**
* \brief Check if connection has been stablished for a time period
* \param[in] fd: Socket handler. The number of the 'lwgsm_conn_t' to use
* \param[in] timeout: Maximum time checking the connection
* \return \ref 1 if connected, 0 otherwise
*/
uint8_t esp_lwgsm_is_connected(int fd, uint32_t timeout)
{
uint8_t connected;
uint32_t attempts = 10;
uint32_t interval = 0;
if(lwgsm_netconn_getconnnum(pClient) != fd){
return -1;
}
connected = lwgsm_netconn_is_connected(pClient);
if(timeout > 0){
interval = timeout / attempts;
}
else{
return connected;
}
while(!connected && (attempts > 0)){
connected = lwgsm_netconn_is_connected(pClient);
lwgsm_delay(interval);
--attempts;
}
return connected;
}
/**
* @brief Perform an operator scan
*
* @return esp_err_t ESP_OK if sucess, ESP_FAIL otherwise
*/
esp_err_t esp_lwgsm_oper_scan()
{
lwgsmr_t ret;
lwgsm_operator_t* operators;
uint16_t operator_array_len = 10;
size_t operators_len;
operators = lwgsm_mem_malloc(operator_array_len*sizeof(lwgsm_operator_t));
ret = lwgsm_operator_scan(operators, operator_array_len, &operators_len, NULL, NULL, 1);
lwgsm_mem_free(operators);
return ret == lwgsmOK ? ESP_OK : ESP_FAIL;
}
/**
* @brief Set receive timeout
*
* @param fd Socket handler. The number of the 'lwgsm_conn_t' to use
* @param timeout Timeout to wait a for receive
* @return int
*/
int esp_lwgsm_set_recv_timeout(int fd, uint32_t timeout)
{
if(lwgsm_netconn_getconnnum(pClient) != fd){
return -1;
}
lwgsm_netconn_set_receive_timeout(pClient, timeout);
return 0;
}
/**
* @brief Get the RSSI
*
* @param rssi Pointer to return RSSI value
* @return ESP_OK if success, ESP_FAIL otherwise
*/
esp_err_t esp_lwgsm_get_rssi(int16_t* rssi)
{
lwgsmr_t err;
esp_err_t ret = ESP_OK;
err = lwgsm_network_rssi(rssi, NULL, NULL, pdTRUE);
if(err != lwgsmOK){
ret = ESP_FAIL;
}
return ret;
}
esp_err_t esp_lwgsm_get_device_revision_cached(char** rev)
{
lwgsmr_t err;
esp_err_t ret = ESP_OK;
err = lwgsm_device_get_cached_revision(rev);
ESP_LOGI(TAG, "Device Identified %s", *rev);
if(err != lwgsmOK){
ret = ESP_FAIL;
}
return ret;
}
/*******************************************************************************
*
* Private function bodies
*
*******************************************************************************/
/**
* \brief Event callback function for GSM stack
* \param[in] evt: Event information with data
* \return \ref lwgsmOK on success, member of \ref lwgsmr_t otherwise
*/
static lwgsmr_t esp_lwgsm_event_cb(lwgsm_evt_t* evt)
{
char* report = NULL;
switch (lwgsm_evt_get_type(evt)) {
case LWGSM_EVT_INIT_FINISH:
ESP_LOGI(TAG, "Initialized.");
break;
case LWGSM_EVT_RESET:
ESP_LOGI(TAG, "Reset complete.");
break;
case LWGSM_EVT_DEVICE_IDENTIFIED:
ESP_LOGD(TAG, "Device identified.");
break;
case LWGSM_EVT_CMD_TIMEOUT:
ESP_LOGW(TAG, "Operation timeout.");
break;
case LWGSM_EVT_NETWORK_REG_CHANGED:
network_utils_process_reg_change(evt, &report);
if(esp_lwgsm_user_cb != NULL) { esp_lwgsm_user_cb(evt); }
break;
case LWGSM_EVT_NETWORK_OPERATOR_CURRENT:
network_utils_process_curr_operator(evt, &report);
break;
case LWGSM_EVT_SIGNAL_STRENGTH:
network_utils_process_rssi(evt, &report);
break;
case LWGSM_EVT_SIM_STATE_CHANGED:
process_sim_evt(evt, &report);
simState = evt->evt.cpin.state;
if(esp_lwgsm_user_cb != NULL) { esp_lwgsm_user_cb(evt); }
break;
case LWGSM_EVT_OPERATOR_SCAN:
operator_utils_print_scan(evt);
break;
case LWGSM_EVT_NETWORK_ATTACHED:
ESP_LOGI(TAG, "Operator network attached.");
if(esp_lwgsm_user_cb != NULL) { esp_lwgsm_user_cb(evt); }
break;
case LWGSM_EVT_NETWORK_DETACHED:
ESP_LOGW(TAG, "Operator network detached.");
if(esp_lwgsm_user_cb != NULL) { esp_lwgsm_user_cb(evt); }
break;
default:
ESP_LOGI(TAG, "CB called but not captured. Event: %d", lwgsm_evt_get_type(evt));
break;
}
if(report != NULL) {
ESP_LOGI(TAG, "%s", report);
lwgsm_mem_free(report);
report = NULL;
}
return lwgsmOK;
}
/**
* \brief Initialize the LWGSM stack, unlock SIM card and attach to operator network
* \param[in] reinit: Flag to indicate if the function should initialize or reinitialize
* \return \ref lwgsmOK on success, member of \ref lwgsmr_t otherwise
*/
static esp_err_t prv_esp_lwgsm_init(lwgsm_evt_fn evt_func, uint8_t reinit)
{
lwgsmr_t ret;
esp_err_t res;
int16_t rssi = 0;
char* apn_name;
char* pdp_address;
if(!initFlag){
ret = lwgsm_init(esp_lwgsm_event_cb, 1);
if(evt_func != NULL){
esp_lwgsm_user_cb = evt_func;
}else{
esp_lwgsm_user_cb = NULL;
}
if(ret != lwgsmOK){
initFlag = 0;
ESP_LOGE(TAG, "Cannot initialize.\r\n");
return ret;
}
else{
initFlag = 1;
}
}else{
ESP_LOGW(TAG, "ESP_LWGSM library already initialized...");
}
if(reinit){
ESP_LOGI(TAG, "Reset GSM module...");
lwgsm_reset_hw(1000, 1);
ret = esp_lwgsm_reset_sw();
if(ret != lwgsmOK){
ESP_LOGE(TAG, "Cannot reset.\r\n");
return ret;
}
}
/* Check SIM state and enter pin if needed */
if(simState == LWGSM_SIM_STATE_PIN){
if (configure_sim_card()) {
ESP_LOGD(TAG, "SIM card configured.");
lwgsm_delay(10000);
} else {
ESP_LOGE(TAG, "Cannot configure SIM card! Is it inserted, pin valid and not under PUK? Closing down...");
return lwgsmERR;
}
}
/* Check signal strength */
do{
ret = lwgsm_network_rssi(&rssi, NULL, NULL, pdTRUE);
if(rssi != 0){ break; }
lwgsm_delay(5000);
} while((rssi == 0) && (ret == lwgsmOK));
ret = aws_prov_get_gsm_settings(&apn_name, &pdp_address);
if(ret != ESP_OK){ return ret; }
ret = lwgsm_network_request_define_pdp_context(ESP_LWGSM_PDP_INDEX, LWGSM_PDP_TYPE_IP, apn_name, pdp_address, LWGSM_APN_D_COMP_OFF,
LWGSM_APN_H_COMP_OFF, false, true);
vPortFree(apn_name);
vPortFree(pdp_address);
if(ret != lwgsmOK){
ESP_LOGE(TAG, "Cannot set APN credentials.\r\n");
return ret;
}
do{
ret = lwgsm_network_request_attach();
if(ret != lwgsmOK){lwgsm_delay(5000);};
} while(ret != lwgsmOK);
res = ret == lwgsmOK ? ESP_OK : ESP_FAIL;
return res;
}