-
Notifications
You must be signed in to change notification settings - Fork 0
/
iridium.c
798 lines (700 loc) · 25.6 KB
/
iridium.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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include "iridium.h"
static const char *TAG_IRIDIUM = "esp32_iridium";
/*
Helper Iridium Functions
*/
bool startsWith(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0;
}
char** str_split(char* a_str, char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
/**
* @brief Process data returned to device from UART bus.
* @param satcom the iridium_t struct pointer.
* @param command the AT command being processed.
* @param data the data returned to be parsed into iridium_t struct.
* @return a iridium_status_t with SAT_OK or SAT_ERROR value.
*/
iridium_status_t iridium_satcom_process_result(iridium_t *satcom, char *command, char *data) {
if (strcmp ("AT", command) == 0) { return SAT_OK; }
if (strcmp ("AT&K0", command) == 0) { return SAT_OK; }
if (strcmp ("AT&w0", command) == 0) { return SAT_OK; }
if (strcmp ("AT&K0", command) == 0) { return SAT_OK; }
if (startsWith("AT+SBDMTA", command)) { return SAT_OK; }
if (strcmp ("AT+CGMI", command) == 0) {
strcpy(satcom->manufacturer_identification, data);
satcom->callback(satcom, AT_CGMI, SAT_OK);
return SAT_OK;
}
if (strcmp ("AT+CGMM", command) == 0) {
strcpy(satcom->model_identification, data);
satcom->callback(satcom, AT_CGMM, SAT_OK);
return SAT_OK;
}
if (strcmp ("AT+CSQ", command) == 0) {
char** tokens = str_split(data, ':');
satcom->signal_strength = atoi(*(tokens + 1));
satcom->callback(satcom, AT_CSQ, SAT_OK);
free(tokens);
return SAT_OK;
}
if (strcmp ("AT+SBDSX", command) == 0) {
char** tokens = str_split(data, ':');
char** results = str_split(*(tokens + 1), ',');
satcom->status_outbound = atoi(*(results));
satcom->sequence_outbound = atoi(*(results + 1));
satcom->status_inbound = atoi(*(results + 2));
satcom->sequence_inbound = atoi(*(results + 3));
satcom->bytes_received = atoi(*(results + 4));
satcom->messages_waiting = atoi(*(results + 5));
satcom->callback(satcom, AT_SBDSX, SAT_OK);
free(tokens);
free(results);
return SAT_OK;
}
if (strcmp ("AT+SBDIX", command) == 0 || strcmp ("AT+SBDIXA", command) == 0) {
char** tokens = str_split(data, ':');
char** results = str_split(*(tokens + 1), ',');
satcom->status_outbound = atoi(*(results));
satcom->sequence_outbound = atoi(*(results + 1));
satcom->status_inbound = atoi(*(results + 2));
satcom->sequence_inbound = atoi(*(results + 3));
satcom->bytes_received = atoi(*(results + 4));
satcom->messages_waiting = atoi(*(results + 5));
satcom->callback(satcom, AT_SBDSX, SAT_OK);
free(tokens);
free(results);
return SAT_OK;
}
if (strcmp ("AT+SBDRT", command) == 0) {
char** tokens = str_split(data, '+');
iridium_message_t msg;
strcpy(msg.data, *(tokens));
msg.size = strlen(*(tokens));
xQueueSend(satcom->message_queue, (void *)&msg, 10);
free(tokens);
return SAT_OK;
}
if (strcmp ("AT+CRIS", command) == 0) {
char** tokens = str_split(data, ':');
char** results = str_split(*(tokens + 1), ',');
free(tokens);
free(results);
return SAT_OK;
}
return SAT_ERROR;
}
/**
* @brief Update the iridium queue status.
* @param satcom the iridium_t struct pointer.
* @param status the current IQS status.
* @return a iridium_status_t with SAT_OK or SAT_ERROR value.
*/
iridium_status_t iridium_update_iqs(iridium_t* satcom, iridium_queue_status_t status) {
pthread_mutex_lock(&satcom->p_status_mutex);
satcom->status = status;
pthread_mutex_unlock(&satcom->p_status_mutex);
return SAT_OK;
}
/**
* @brief Update the processing message nonce.
* @param satcom the iridium_t struct pointer.
* @param nonce the message nonce int.
* @return a iridium_status_t with SAT_OK or SAT_ERROR value.
*/
iridium_status_t iridium_update_p_nonce(iridium_t* satcom, int nonce) {
pthread_mutex_lock(&satcom->p_nonce_mutex);
satcom->p_nonce = nonce;
pthread_mutex_unlock(&satcom->p_nonce_mutex);
return SAT_OK;
}
/**
* @brief Retrieves the current queue status while processing a message nonce.
* @param satcom the iridium_t struct pointer.
* @return a iridium_queue_status_t with IQS_NONE, IQS_OPEN or IQS_WAITING value.
*/
iridium_queue_status_t iridium_get_iqs(iridium_t* satcom) {
iridium_queue_status_t t_status = IQS_NONE;
pthread_mutex_lock(&satcom->p_status_mutex);
t_status = satcom->status;
pthread_mutex_unlock(&satcom->p_status_mutex);
return t_status;
}
/**
* @brief Send data payload across the UART bus.
* @param satcom the iridium_t struct pointer.
* @param data the data to be sent.
* @param nonce the nonce used to track responses.
* @return a iridium_status_t with SAT_OK or SAT_ERROR value.
*/
iridium_status_t iridium_send_raw(iridium_t* satcom, char *data, int nonce) {
if (satcom->status == IQS_WAITING) {
// send iridium_message_t to buffer queue
ESP_LOGI(TAG_IRIDIUM, "IN_BUFFER_QUEUE[%d] = %s", nonce, data);
iridium_message_t msg;
strcpy(msg.data, data);
msg.size = strlen(data);
msg.nonce = nonce;
xQueueSend(satcom->buffer_queue, (void *)&msg, 10);
return SAT_OK;
}
ESP_LOGI(TAG_IRIDIUM, "SENT_TO_UART_1[%d] = %s", nonce, data);
/* transmit data via UART */
uart_write_bytes(satcom->uart_number, data, strlen(data));
/* update IQS */
iridium_update_iqs(satcom, IQS_WAITING);
iridium_update_p_nonce(satcom, nonce);
return SAT_OK;
}
/**
* @brief Enabled or disable the ring notification on the modem.
* @param satcom the iridium_t struct pointer.
* @param enabled the ring notification.
* @return a iridium_result_t with metadata.
*/
iridium_result_t iridium_config_ring(iridium_t *satcom, bool enabled) {
iridium_result_t result;
/* enable/disable satcom ring */
if (enabled) {
result = iridium_send(satcom, AT_SBDMTA, "1", true, 500);
if (result.status != SAT_OK) {
return result;
}
} else {
result = iridium_send(satcom, AT_SBDMTA, "0", true, 500);
if (result.status != SAT_OK) {
return result;
}
}
vTaskDelay(pdMS_TO_TICKS(IRI_BUFF_DELAY));
/* save config */
result = iridium_send(satcom, AT_W0, "", true, 500);
if (result.status != SAT_OK) {
return result;
}
vTaskDelay(pdMS_TO_TICKS(IRI_BUFF_DELAY));
/* turn off flow control */
result = iridium_send(satcom, AT_K0, "", true, 500);
if (result.status != SAT_OK) {
return result;
}
vTaskDelay(pdMS_TO_TICKS(IRI_BUFF_DELAY));
/* check ring status */
result = iridium_send(satcom, AT_SBDMTAQ, "", true, 500);
return result;
}
/**
* @brief Transmit a message to the iridium network.
* @param satcom the iridium_t struct pointer.
* @param message to be sent.
* @return a iridium_result_t with metadata.
*/
iridium_result_t iridium_tx_message(iridium_t *satcom, char *message) {
iridium_result_t result;
iridium_result_t r1 = iridium_send(satcom, AT_SBDWT, message, true, 500);
/* failed to set outbound message buffer */
if (r1.status != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
int delays[6] = {2000,4000,20000,30000,300000,300000};
/* short burst - send message - with adaptive retry */
for (int i = 0; i < 5; i++){
iridium_result_t r2 = iridium_send(satcom, AT_SBDIX, NULL, true, 500);
if (r2.status != SAT_OK) {
result.status = SAT_ERROR;
break;
}
/* check short burst message returns */
if (satcom->status_outbound == MO_TRANSFERRED_SUCCESSFULLY ||
satcom->status_outbound == MO_TRANSFERRED_SUCCESSFULLY_TOO_BIG ||
satcom->status_outbound == MO_TRANSFERRED_SUCCESSFULLY_LOC_NOT_ACCEPTED) {
break;
} else {
result.status = SAT_ERROR;
}
vTaskDelay(pdMS_TO_TICKS(delays[i]));
}
return result;
}
/*
AT+SBDIX = +SBDIX:<MO status>,<MOMSN>,<MT status>,<MTMSN>,<MT length>,<MT queued>
*/
/**
* @brief Send AT command with data.
* @param satcom the iridium_t struct pointer.
* @param command the iridium modem AT command.
* @param rdata the raw data.
* @param wait_response wait for a responce from the modem.
* @param wait_interval the amount of time in ms for wait interval check.
* @return a iridium_result_t with metadata.
*/
iridium_result_t iridium_send(iridium_t* satcom, iridium_command_t command, char *rdata, bool wait_response, int wait_interval) {
iridium_result_t result;
/* increment c_nonce */
satcom->c_nonce++;
int t_nonce = satcom->c_nonce;
switch (command) {
case AT:
if (iridium_send_raw(satcom, "AT\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_CSQ:
if (iridium_send_raw(satcom, "AT+CSQ\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_CGMI:
if (iridium_send_raw(satcom, "AT+CGMI\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_CGMM:
if (iridium_send_raw(satcom, "AT+CGMM\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_SBDIX:
if (iridium_send_raw(satcom, "AT+SBDIX\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_SBDSX:
if (iridium_send_raw(satcom, "AT+SBDSX\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_MSSTM:
if (iridium_send_raw(satcom, "AT-MSSTM\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_SBDRT:
if (iridium_send_raw(satcom, "AT+SBDRT\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_CRIS:
if (iridium_send_raw(satcom, "AT+CRIS\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_SBDIXA:
if (iridium_send_raw(satcom, "AT+SBDIXA\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_SBDMTAQ:
if (iridium_send_raw(satcom, "AT+SBDMTA?\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_SBDWT: {
char *message = (char*)malloc(50 * sizeof(char));
sprintf(message, "AT+SBDWT=%s\r", rdata);
result.status = iridium_send_raw(satcom, message, t_nonce);
free(message);
return result;
}
case AT_SBDMTA: {
char *message = (char*)malloc(20 * sizeof(char));
sprintf(message, "AT+SBDMTA=%s\r", rdata);
result.status = iridium_send_raw(satcom, message, t_nonce);
free(message);
return result;
}
case AT_W0:
if (iridium_send_raw(satcom, "AT&w0\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
case AT_K0:
if (iridium_send_raw(satcom, "AT&K0\r", t_nonce) != SAT_OK) {
result.status = SAT_ERROR;
return result;
}
break;
default:
break;
}
if (wait_response) {
iridium_queue_status_t t_status = IQS_NONE;
while (t_status != IQS_OPEN && satcom->p_nonce == t_nonce) {
vTaskDelay(pdMS_TO_TICKS(wait_interval));
t_status = iridium_get_iqs(satcom);
}
strcpy(result.result, satcom->buffer_data);
ESP_LOGI(TAG_IRIDIUM, "WAIT_DONE_NONCE = [%d]", t_nonce);
}
result.status = SAT_OK;
return result;
}
void ring_satcom_task(void *pvParameters) {
iridium_t* satcom = (iridium_t *)pvParameters;
vTaskDelay(pdMS_TO_TICKS(1000));
iridium_result_t rcris = iridium_send(satcom, AT_CRIS, NULL, true, 500);
if (rcris.status == SAT_OK) { }
for(;;) {
iridium_result_t r1 = iridium_send(satcom, AT_SBDIXA, "", true, 500);
if (r1.status == SAT_OK) {
ESP_LOGI(TAG_IRIDIUM, "RST_R1[%d] = %s", r1.status, r1.result);
}
if (satcom->status_outbound == MO_TRANSFERRED_SUCCESSFULLY ||
satcom->status_outbound == MO_TRANSFERRED_SUCCESSFULLY_TOO_BIG ||
satcom->status_outbound == MO_TRANSFERRED_SUCCESSFULLY_LOC_NOT_ACCEPTED) {
if (satcom->messages_waiting == 0) {
break;
}
vTaskDelay(pdMS_TO_TICKS(5000));
iridium_result_t r2 = iridium_send(satcom, AT_SBDRT, NULL, true, 500);
if (r2.status == SAT_OK) {
ESP_LOGI(TAG_IRIDIUM, "RST_R2[%d] = %s", r2.status, r2.result);
}
}
vTaskDelay(pdMS_TO_TICKS(10000));
}
iridium_result_t r2 = iridium_send(satcom, AT_SBDRT, NULL, true, 500);
if (r2.status == SAT_OK) {
ESP_LOGI(TAG_IRIDIUM, "RST_R3[%d] = %s", r2.status, r2.result);
}
satcom->ring_task_running = 0;
vTaskDelete(NULL);
}
void uart_satcom_task(void *pvParameters) {
iridium_t* satcom = (iridium_t *)pvParameters;
uint8_t* dtmp = (uint8_t*) malloc(IRI_RD_BUF_SIZE);
uart_event_t event;
struct stack_t *s = newStack();
for(;;) {
if(xQueueReceive(satcom->uart_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
bzero(dtmp, IRI_RD_BUF_SIZE);
switch(event.type) {
case UART_DATA:
uart_read_bytes(satcom->uart_number, dtmp, event.size, portMAX_DELAY);
ESP_LOGI(TAG_IRIDIUM, "R:%s-", dtmp);
char* pch = NULL;
pch = strtok((char*)dtmp, "\r\n");
while (pch != NULL) {
/* AT Command Check */
if (startsWith("AT", pch)) {
push(s, pch);
} else {
if (strcmp("SBDRING", pch) == 0) {
if (satcom->ring_task_running == 0) {
satcom->ring_task_running = 1;
xTaskCreate(&ring_satcom_task,
"ring_satcom_task",
4096,
satcom,
12, NULL);
}
break;
}
if (strcmp ("ERROR", pch) == 0) {
continue;
}
if (strcmp ("OK", pch) == 0) {
// Process
char data[100];
char command[20];
data[0] = '\0';
while (top(s) != NULL) {
// Grab top value / pop
char* tmp = top(s);
ESP_LOGI(TAG_IRIDIUM, "TMP:[%s]", tmp);
if (startsWith("AT", tmp)) {
strcpy(command, tmp);
} else {
strcat(data, tmp);
}
pop(s);
}
ESP_LOGI(TAG_IRIDIUM, "P: %s = %s", command, data);
strcpy(satcom->buffer_data, data);
if (iridium_satcom_process_result(satcom, command, data) == SAT_OK) {
ESP_LOGI(TAG_IRIDIUM, "OK_R[%d]: %s = %s ", satcom->p_nonce, command, pch);
} else {
ESP_LOGI(TAG_IRIDIUM, "ERROR_R[%d]: %s = %s ", satcom->p_nonce, command, pch);
}
/* Clean up after AT processing */
clear_stack(s);
iridium_update_iqs(satcom, IQS_OPEN);
} else {
push(s, pch);
}
}
pch = strtok(NULL, "\r\n");
}
break;
case UART_FIFO_OVF:
uart_flush_input(satcom->uart_number);
xQueueReset(satcom->uart_queue);
break;
case UART_BUFFER_FULL:
uart_flush_input(satcom->uart_number);
xQueueReset(satcom->uart_queue);
break;
case UART_BREAK:
break;
case UART_PARITY_ERR:
break;
case UART_FRAME_ERR:
break;
case UART_PATTERN_DET:
break;
default:
break;
}
}
}
free(dtmp);
dtmp = NULL;
destroy_stack(&s);
vTaskDelete(NULL);
}
void buffer_satcom_task(void *pvParameters) {
iridium_t* satcom = (iridium_t *)pvParameters;
iridium_queue_status_t t_status = IQS_NONE;
int delay_ms = satcom->buffer_delay_ms;
for(;;) {
/* waiting for buffer message event */
t_status = iridium_get_iqs(satcom);
if (t_status == IQS_OPEN) {
iridium_message_t rcv_msg;
if (xQueueReceive(satcom->buffer_queue, (void *)&rcv_msg, 0) == pdTRUE) {
ESP_LOGI(TAG_IRIDIUM, "SENT_TO_UART_FROM_BUFFER[%d] = %s", rcv_msg.nonce, rcv_msg.data);
iridium_send_raw(satcom, rcv_msg.data, rcv_msg.nonce);
}
}
vTaskDelay(pdMS_TO_TICKS(delay_ms));
}
vTaskDelete(NULL);
}
void message_satcom_task(void *pvParameters) {
iridium_t* satcom = (iridium_t *)pvParameters;
int delay_ms = satcom->buffer_delay_ms;
for(;;) {
iridium_message_t rcv_msg;
if (xQueueReceive(satcom->message_queue, (void *)&rcv_msg, 0) == pdTRUE) {
satcom->message_callback(satcom, rcv_msg.data);
}
vTaskDelay(pdMS_TO_TICKS(delay_ms));
}
vTaskDelete(NULL);
}
/**
* @brief Create a default iridium configuration.
* @return a valid iridium_t struct configuration.
*/
iridium_t* iridium_default_configuration() {
iridium_t *satcom = malloc(sizeof(iridium_t));
satcom->buffer_size = 10; // item size
satcom->buffer_delay_ms = 1000; // ms
satcom->task_message_stack_depth = 4096;
satcom->task_buffer_stack_depth = 2024;
satcom->task_uart_stack_depth = 4096;
satcom->gpio_sleep_pin_number = -1;
satcom->gpio_net_pin_number = -1;
return satcom;
}
int iridium_is_available(iridium_t *satcom) {
if (satcom->gpio_net_pin_number != -1) {
return gpio_get_level(satcom->gpio_net_pin_number);
}
return -1;
}
/**
* @brief Modem specs.
* @return a iridium_status_t with SAT_OK or SAT_ERROR value.
*/
iridium_status_t iridium_system_spec(iridium_t *satcom) {
/* AT system details */
iridium_result_t r;
r = iridium_send(satcom, AT_CGMI, NULL, true, 500);
if (r.status != SAT_OK) {
return r.status;
}
r = iridium_send(satcom, AT_CGMM, NULL, true, 500);
if (r.status != SAT_OK) {
return r.status;
}
return r.status;
}
/**
* @brief Toggle modem to sleep.
* @return a iridium_status_t with SAT_OK or SAT_ERROR value.
*/
iridium_status_t iridium_modem_sleep(iridium_t *satcom) {
/* turn off modem */
esp_err_t err = gpio_set_level(satcom->gpio_sleep_pin_number, IRI_GPIO_SLP_OFF);
if (err != ESP_OK) {
return SAT_ERROR;
}
return SAT_OK;
}
/**
* @brief Configure iridium modem via UART connection.
* @param satcom the iridium_t struct pointer.
* @return a iridium_status_t with SAT_OK or SAT_ERROR value.
*/
iridium_status_t iridium_config(iridium_t *satcom) {
/*
Check SLP pin is configured
*/
if (satcom->gpio_sleep_pin_number != -1) {
gpio_config_t slp_conf;
slp_conf.intr_type = GPIO_INTR_DISABLE;
slp_conf.mode = GPIO_MODE_OUTPUT;
slp_conf.pin_bit_mask = ((1ULL<<satcom->gpio_sleep_pin_number) | (1ULL<<satcom->gpio_sleep_pin_number));
slp_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
slp_conf.pull_up_en = GPIO_PULLDOWN_ENABLE;
esp_err_t err = gpio_config(&slp_conf);
if (err != ESP_OK) {
return SAT_ERROR;
}
/* turn on modem */
vTaskDelay(pdMS_TO_TICKS(IRI_GPIO_CONF_BUFF));
gpio_set_level(satcom->gpio_sleep_pin_number, IRI_GPIO_SLP_ON);
}
if (satcom->gpio_net_pin_number != -1) {
gpio_config_t net_conf;
net_conf.intr_type = GPIO_INTR_DISABLE;
net_conf.mode = GPIO_MODE_INPUT;
net_conf.pin_bit_mask = ((1ULL<<satcom->gpio_net_pin_number) | (1ULL<<satcom->gpio_net_pin_number));
esp_err_t err = gpio_config(&net_conf);
if (err != ESP_OK) {
return SAT_ERROR;
}
/* settle delay */
vTaskDelay(pdMS_TO_TICKS(IRI_GPIO_CONF_BUFF));
}
/*
Baud Rate = 19200 Data Bits = 8 Parity = N Stop Bits = 1
*/
const int DEFAULT_BAUD_RATE = 19200;
// TODO: Fix mod here
uart_config_t uart_config = {
.baud_rate = DEFAULT_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT
};
esp_log_level_set(TAG_IRIDIUM, ESP_LOG_INFO);
/* init pthread_mutex handles */
pthread_mutex_init(&(satcom->p_status_mutex), NULL);
pthread_mutex_init(&(satcom->p_nonce_mutex), NULL);
if (satcom->buffer_delay_ms == 0) {
satcom->buffer_delay_ms = 1000; // ms
}
satcom->c_nonce = 0;
satcom->p_nonce = 0;
satcom->ring_task_running = 0;
satcom->status = IQS_OPEN;
satcom->buffer_queue = xQueueCreate(satcom->buffer_size, sizeof(iridium_message_t));
satcom->message_queue = xQueueCreate(20, sizeof(iridium_message_t));
/* install uart drivers */
if (uart_driver_install(satcom->uart_number,
IRI_BUF_SIZE * 2,
IRI_BUF_SIZE * 2, 20,
&satcom->uart_queue, 0) != ESP_OK) {
return SAT_ERROR;
}
/* set uart pin outs */
if (uart_set_pin(satcom->uart_number,
satcom->uart_txn_number,
satcom->uart_rxd_number,
satcom->uart_rts_number,
satcom->uart_cts_number) != ESP_OK) {
return SAT_ERROR;
}
ESP_ERROR_CHECK(uart_param_config(satcom->uart_number, &uart_config));
/*if (uart_param_config(satcom->uart_number, &uart_config) != ESP_OK) { return SAT_ERROR; }*/
/* start message processing tasks */
xTaskCreate(&message_satcom_task,
"message_satcom_task",
satcom->task_message_stack_depth,
satcom,
12, NULL);
/* start uart processing tasks */
xTaskCreate(&uart_satcom_task,
"uart_satcom_task",
satcom->task_uart_stack_depth,
satcom,
12, NULL);
/* start buffer processing tasks */
xTaskCreate(&buffer_satcom_task,
"buffer_satcom_task",
satcom->task_buffer_stack_depth,
satcom,
12, NULL);
/* 1000ms delay */
vTaskDelay(pdMS_TO_TICKS(1000));
/* AT check */
iridium_result_t r;
r = iridium_send(satcom, AT, NULL, true, 500);
if (r.status != SAT_OK) {
return r.status;
}
return r.status;
}