-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirmware.c
More file actions
1561 lines (1241 loc) · 49.8 KB
/
firmware.c
File metadata and controls
1561 lines (1241 loc) · 49.8 KB
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pico/runtime.h"
#include "pico/stdlib.h"
#include "pico/time.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
// To see detailed debug messages, uncomment the following line.
#define DETAILED_PRINT
#define PICO_MALLOC_PANIC 1
#define PICO_DEBUG_MALLOC 1
// Registers
#define AIRCR_Register (*((volatile uint32_t*)(PPB_BASE + 0x0ED0C)))
/******** UART0 TELIT MODEM SETTINGS ********/
#define TELIT_UART uart0
#define TELIT_UART_BAUDRATE 115200
#define TELIT_UART_DATABITS 8
#define TELIT_UART_STOPBITS 1
#define TELIT_UART_PARITY UART_PARITY_NONE
#define TELIT_UART_TX_PIN 0
#define TELIT_UART_RX_PIN 1
#define TELIT_UART_IRQ (TELIT_UART == uart0 ? UART0_IRQ : UART1_IRQ)
#define TELIT_BUFFER_SIZE 128
#define TELIT_MSG_WAIT_MS 5000
char uart0_buffer[TELIT_BUFFER_SIZE]; // It holds the data coming from RX.
uint16_t uart0_buffer_index = 0; // It holds the index of the buffer of TELIT.
char recieved_char = '\0'; // It holds the last char received.
volatile bool is_message_finished = false; // It is true when the message is finished with OK or ERROR.
const char start_message[] = "AT"; // It is the start message of the TELIT.
const char end_message[] = "\r\n"; // It is the end message of the TELIT.
// The ones on the Heap.
char* index_start;
char* index_end;
uint16_t* ip_address;
/*************************************************/
/******** BOARD BUTTON SETTINGS ********/
#define BOARD_BUTTON_PIN 2
#define BOUNCING_DELAY 150000
#define USB_BUFFER_SIZE 256
volatile uint32_t btn_rise_started_time = 0; // It holds the time when the button is pressed.
volatile bool is_board_button_clicked = false; // It is true when the button is clicked.
char usb_buffer[USB_BUFFER_SIZE]; // It holds the data to be sent to the USB.
uint16_t usb_buffer_index = 0; // It holds the index of the buffer of USB.
char concat_buffer[USB_BUFFER_SIZE + 4]; // It holds the data to be sent to the USB with CRLF.
/*************************************************/
/********** Function Declarations ***********/
void reboot_pico();
/*void free_heap_usage(uint8_t);*/
/*void prepare_for_next(uint8_t);*/
void set_gpios();
// TELIT
void send_message_to_telit(char[]);
void set_telit_uart_ready();
char* create_message(char*);
bool check_signal_quality();
void process_signal_quailty();
uint8_t check_carrier_registration();
void process_carrier_registration();
uint8_t check_gprs_registration();
void process_gprs_registration();
bool check_gprs_attach();
void process_gprs_attach();
bool define_apn();
bool activate_pdp();
void telit_init_3g();
/*bool check_telit_ready();*/
// MQTT
bool mqtt_enable_and_configure(bool, char[], char[]);
uint8_t mqtt_login(char[], char[], char[]);
bool mqtt_logout();
bool mqtt_subscribe_topic(char[]);
bool mqtt_publish(char[], char[]);
char* mqtt_read(uint8_t);
char* mqtt_read_in_queue();
uint8_t mqtt_new_message_count();
bool process_mqtt_login(char[], char[], char[]);
bool process_mqtt_enable(bool, char[], char[]);
//-- Timers
bool repeating_timer_callback(struct repeating_timer *t);
char* _timer_msg = NULL;
bool _check_read_timer = false;
//-- Interrupts
void on_uart0_rx();
void gpio_interrupt_handler(uint, uint32_t);
/*************************************************/
int main(){
/*
* Initialize the stdio from the USB/UART0.
* Note that, CMakeLists file has to have
* "pico_enable_stdio_uart(main 0)" to
* not use UART channel for communication
* between PC and Pico.
*/
stdio_init_all();
// Wait for the USB to be ready.
sleep_ms(5000);
// Inform the USB that the Pico is ready.
printf("$> Welcome to the Pico!\n");
// Set the UART0 ready for TELIT modem.
set_telit_uart_ready();
// Set the GPIOs.
set_gpios();
// Inform the GPIO and TELIT_UART is ready.
printf("$> GPIO and UART setup completed.\n");
// Wait to get a signal for a moment.
sleep_ms(10000);
// Initilization of the TELIT mode.
telit_init_3g();
// Enable and set the MQTT.
if (!process_mqtt_enable(false, "mqtt3.thingspeak.com", "1883")) {
// Login to the MQTT broker.
process_mqtt_login("NzsNHSIHKy40Jx8AKSIfHg8", "NzsNHSIHKy40Jx8AKSIfHg8", "WIr4lWLSISAqwDL8fOuRp0Nk");
// Subscribe to the topic.
bool status = mqtt_subscribe_topic("channels/1708249/subscribe/fields/field1");
if (!status)
printf("$> Subscribed to the topic.\n");
else {
printf("$> Failed to subscribe to the topic.\n");
printf("$> Rebooting the Pico in 3 seconds.\n");
sleep_ms(3000);
reboot_pico();
}
// Publish to the topic.
mqtt_publish("channels/1708249/publish", "field1=500&status=MQTTPUBLISH");
printf("$> Checking message queue in MQTT Broker...\n");
uint8_t new_msg = mqtt_new_message_count();
if (new_msg == 60) {
printf("$> Something got wrong with the MQTT Broker.\n");
printf("$> Rebooting the Pico in 3 seconds.");
sleep_ms(3000);
reboot_pico();
} else {
printf("$> There are %d messages in queue.\n", new_msg);
}
printf("$> Reading first message from MQTT Broker...\n");
mqtt_read_in_queue();
printf("$> Publishing to the topic... Value: 11\n");
mqtt_publish("channels/1708249/publish", "field1=0&status=MQTTPUBLISH");
printf("$> Reading second message from MQTT Broker...\n");
mqtt_read_in_queue();
printf("$> Publishing to the topic... Value: 250\n");
mqtt_publish("channels/1708249/publish", "field1=26&status=MQTTPUBLISH");
printf("$> Checking message queue in MQTT Broker...\n");
new_msg = mqtt_new_message_count();
printf("$> There are %d messages in queue.\n", new_msg);
/*printf("$> Reading second message from MQTT Broker...\n");
msg = mqtt_read(2);
printf("$> The second message is: %s\n", msg);
prepare_for_next(1);
free(msg);*/
printf("$> Reading third message from MQTT Broker...\n");
mqtt_read_in_queue();
// Logout from the MQTT broker.
mqtt_logout();
}
// Create the timer for getting input every 10 seconds.
/*
struct repeating_timer timer;
add_repeating_timer_ms(10000, repeating_timer_callback, NULL, &timer);
*/
/* INFINITE LOOP */
while (true) {
// If timer runt.
if (_check_read_timer) {
printf("$> Timer: Getting last message if there is.");
if (_timer_msg != NULL) free(_timer_msg);
uint8_t message_count = mqtt_new_message_count();
printf("$> ~ MSG CNT: %d", message_count);
if (message_count != 60) {
_timer_msg = mqtt_read_in_queue();
printf("$> ~ MSG: %s", _timer_msg);
free(_timer_msg);
}
_check_read_timer = false;
}
if (is_board_button_clicked) {
printf("> TELIT cmd: ");
// Get the command from the PC.
char last_char = getchar_timeout_us(5000);
while (last_char != 0x0A && last_char != 0x0D) {
// If it is not timeout, then add it to the buffer.
if (last_char != 0xFF) {
usb_buffer[usb_buffer_index] = last_char;
usb_buffer_index++;
}
// Get new one.
last_char = getchar_timeout_us(5000);
}
usb_buffer[usb_buffer_index] = '\0';
// Send the message to TELIT.
printf("\n>$ Send the usb buffer to TELIT. Message: %s", usb_buffer);
send_message_to_telit(usb_buffer);
// Clear the buffer.
memset(usb_buffer, '\0', sizeof(usb_buffer));
usb_buffer_index = 0;
// Assign the default value of variable, to let it get new commands.
is_board_button_clicked = false;
}
tight_loop_contents();
}
}
uint8_t mqtt_new_message_count() {
#ifdef DETAILED_PRINT
printf("\n==== mqtt_new_message_count() ====\n");
#endif
char command[] = "#MQREAD?";
char response[] = "MQREAD: 1,";
send_message_to_telit(command);
#ifdef DETAILED_PRINT
printf("-- message count request sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, response);
if (index_start != NULL) {
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
// Save it as a variable to use it later.
uint8_t message_count = index_start[strlen(response)] - '0';
#ifdef DETAILED_PRINT
printf("-- RESULT: message count %d\n", message_count);
printf("==== mqtt_new_message_count() ====\n\n");
#endif
if (index_end == NULL) return 60;
else return message_count;
}
#ifdef DETAILED_PRINT
printf("==== mqtt_new_message_count() ====\n\n");
#endif
return 60; // 60 is the error code.
}
char* mqtt_read_in_queue() {
#ifdef DETAILED_PRINT
printf("\n====== mqtt_read_in_queue() ======\n");
#endif
char command[] = "#MQREAD=1,1";
char response[] = "#MQREAD: 1,";
// Send command to the server.
send_message_to_telit(command);
#ifdef DETAILED_PRINT
printf("-- first message request sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
/*printf("-- buff %s", uart0_buffer);*/
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, response);
/*printf("-- index start %s", index_start);*/
if (index_start != NULL) {
index_start = index_start + strlen(response);
// Find the data size of response.
char* delimeter = strstr(index_start, ",");
char* data_size_crlr = strstr(delimeter, "\r\n");
// Get the data size from the response.
char* data_size = (char *) malloc(sizeof(char) * (data_size_crlr - delimeter));
memset(data_size, '\0', sizeof(char) * (data_size_crlr - delimeter));
strncpy(data_size, delimeter + 1, data_size_crlr - delimeter - 1);
// Conversion to int.
uint32_t data_size_int = atoi(data_size);
free(data_size);
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
if (index_end == NULL) return "ERROR";
// Get the index of the message start.
char* index_of_message = strstr(index_start, "<") + (sizeof(char) * (data_size_int + 3));
/*printf("index_of_message %s\n", index_of_message);*/
/*printf("message_to_send size: %d\n", sizeof(char) * (data_size_int + 1));*/
char* message = (char *) malloc(sizeof(char) * (data_size_int + 1));
memset(message, '\0', sizeof(char) * (data_size_int + 1));
strncpy(message, index_of_message, data_size_int);
#ifdef DETAILED_PRINT
printf("-- RESULT: message databits: %d\n", data_size_int);
printf("-- RESULT: message came: %s\n", message);
printf("==== mqtt_read_in_queue() ====\n\n");
#endif
free(message);
return "";
}
#ifdef DETAILED_PRINT
printf("==== mqtt_read_in_queue() ====\n\n");
#endif
}
char* mqtt_read(uint8_t order) {
#ifdef DETAILED_PRINT
printf("\n====== mqtt_read() ======\n");
#endif
uint8_t msg_count = 3/*mqtt_new_message_count()*/;
/*prepare_for_next(1);*/
// printf("-- message count: %d\n", msg_count);
if (msg_count >= order) {
char command[] = "#MQREAD=1,2";
char response[] = "MQREAD: 1,";
// Convert the order to string.
//char order_num[2];
//sprintf(order_num, "%d", order);
//printf("-- order: %d\n-- order_num: %s\n", order, order_num);
// Create the message space in the heap.
//char* message = malloc(sizeof(char) * (strlen(command) + strlen(order_num) + 1));
//memset(message, '\0', sizeof(char) * (strlen(command) + strlen(order_num) + 1));
// Concat the number of the order, and the prefix of command.
//strcat(message, command);
//strcat(message, order_num);
// Send command to the server.
// send_message_to_telit(message);
is_message_finished = false;
if (uart_is_writable(TELIT_UART)) {
int index = 0;
while (index != strlen(command)) {
uart_putc_raw(TELIT_UART, command[index]);
index++;
}
}
#ifdef DETAILED_PRINT
//printf("-- %s. message request sent to modem.\n", order_num);
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(5*TELIT_MSG_WAIT_MS);
printf("-- buffer: %s", uart0_buffer);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, response);
if (index_start != NULL) {
index_start = index_start + strlen(response);
// Find the data size of response.
char* delimeter = strstr(index_start, ",");
char* data_size_crlr = strstr(delimeter, "\r\n");
char* data_size = malloc(sizeof(char) * (data_size_crlr - delimeter));
memset(data_size, '\0', sizeof(char) * (data_size_crlr - delimeter));
strncpy(data_size, delimeter + 1, data_size_crlr - delimeter - 1);
uint32_t data_size_int = atoi(data_size);
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
// Get the index of the message start.
char* index_of_message = strstr(index_start, "<") + (sizeof(char) * (data_size_int + 3));
char* message_to_send = (char*) malloc(sizeof(char) * (data_size_int + 1));
memset(message_to_send, '\0', sizeof(char) * (data_size_int + 1));
strncpy(message_to_send, index_of_message, data_size_int);
#ifdef DETAILED_PRINT
printf("-- RESULT: message databits: %d\n", data_size_int);
printf("-- RESULT: message came: %s\n", message_to_send);
printf("==== mqtt_read() ====\n\n");
#endif
free(data_size);
//free(message);
if (index_end == NULL) return "ERROR";
else return message_to_send;
}
#ifdef DETAILED_PRINT
printf("==== mqtt_read() ====\n\n");
#endif
} else {
// No message to read.
printf("$> Not enough new message.\n");
#ifdef DETAILED_PRINT
printf("==== mqtt_new_message_count() ====\n\n");
#endif
return NULL;
}
}
bool mqtt_logout() {
#ifdef DETAILED_PRINT
// Inform the function entrance.
printf("\n======= mqtt_logout() =======\n");
#endif
// Create command to send it.
char command_message[] = "#MQDISC=1";
send_message_to_telit(command_message);
#ifdef DETAILED_PRINT
printf("-- logout message sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, command_message);
if (index_start != NULL) {
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
// If there is no OK, then something got wrong.
if (index_end == NULL) return true;
#ifdef DETAILED_PRINT
printf("-- RESULT: mqtt logged out is %s\n", (index_end != NULL) ? "performed" : "failed");
printf("======= mqtt_logout() =======\n\n");
#endif
return false;
}
#ifdef DETAILED_PRINT
printf("======= mqtt_logout() =======\n\n");
#endif
return true;
}
bool process_mqtt_enable(bool will, char server_address[], char server_port[]) {
printf("$> MQTT is enabling, and setting up...\n");
// Try 3 times to enable, and set the MQTT.
for (int try = 0; try < 3; try++) {
if (!mqtt_enable_and_configure(will, server_address, server_port)) {
printf("$> MQTT is enabled and setted.\n");
return false;
} else {
printf("$> MQTT is failed to enable.\n");
// If it couldn't achieve on the tryings, reboot Pico.
if (try == 2) {
printf("$> MQTT enabling couldn't completed.\n");
printf("$> Pico will be reboot in 3 seconds.\n");
// Let the Pico to sleep for 3 seconds to show the information to user.
sleep_ms(3000);
// Reboot the Pico.
reboot_pico();
}
sleep_ms(TELIT_MSG_WAIT_MS);
}
}
return true;
}
bool process_mqtt_login(char client_id[], char user_name[], char password[]){
uint8_t status_code = 0;
// Try to connect for 3 times.
for (uint8_t try = 0; try < 3; try++) {
// Inform the user.
printf("$> Logging into the MQTT broker... (%d)\n", try + 1);
// Get the status code.
status_code = mqtt_login(client_id, user_name, password);
// If it is not 1, it is not connected.
if (status_code == 1) {
printf("$> Logged in to the MQTT broker.\n");
break;
} else {
printf("$> Failed to login to the MQTT broker, trying again.\n");
mqtt_logout();
if (try == 2) {
printf("$> MQTT login couldn't completed.\n");
printf("$> Pico will be reboot in 3 seconds.\n");
// Let the Pico to sleep for 3 seconds to show the information to user.
sleep_ms(3000);
// Reboot the Pico.
reboot_pico();
}
}
}
}
bool mqtt_enable_and_configure(bool last_will, char server_address[], char server_port[]) {
/************************** ENABLING MQTT ****************************/
#ifdef DETAILED_PRINT
// Inform the function entrance.
printf("\n==== mqtt_enable_and_configure() ====\n");
#endif
// Create command to send it.
char command_message_enable[] = "#MQEN=1,1";
send_message_to_telit(command_message_enable);
#ifdef DETAILED_PRINT
printf("-- enable message sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, command_message_enable);
if (index_start != NULL) {
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
#ifdef DETAILED_PRINT
printf("-- RESULT: mqtt has %s\n", (index_end != NULL) ? "enabled" : "error");
#endif
// If there is no OK, then something got wrong.
if (index_end == NULL) return true;
}
/************************** LAST-WILL SET ****************************/
// Create the message, and send it.
char command_message_lastwill[] = "#MQWCFG=1,0";
command_message_lastwill[10] = (last_will) ? '1' : '0';
send_message_to_telit(command_message_lastwill);
#ifdef DETAILED_PRINT
printf("-- last will setting message sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, command_message_lastwill);
if (index_start != NULL) {
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
#ifdef DETAILED_PRINT
printf("-- RESULT: last will is %s\n", (index_end != NULL) ? "setted" : "error");
#endif
// If there is no OK, then something got wrong.
if (index_end == NULL) return true;
}
/************************** SERVER SET ****************************/
const char prefix[] = "#MQCFG=1,";
const char midfix[] = ",";
const char postfix[] = ",1";
// Create a heap memory for the message concating.
char* concat_message = malloc(sizeof(char) * (strlen(prefix) + strlen(server_address) + strlen(midfix) + strlen(server_port) + strlen(postfix) + 1));
memset(concat_message, '\0', sizeof(char) * (strlen(prefix) + strlen(server_address) + strlen(midfix) + strlen(server_port) + strlen(postfix) + 1));
// Concat the message.
strcat(concat_message, prefix);
strcat(concat_message, server_address);
strcat(concat_message, midfix);
strcat(concat_message, server_port);
strcat(concat_message, postfix);
send_message_to_telit(concat_message);
#ifdef DETAILED_PRINT
printf("-- server setting message sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, concat_message);
if (index_start != NULL) {
// Free the memory.
free(concat_message);
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
#ifdef DETAILED_PRINT
printf("-- RESULT: server settings is %s\n", (index_end != NULL) ? "setted" : "error");
#endif
// If there is no OK, then something got wrong.
if (index_end == NULL) {
return true;
}
#ifdef DETAILED_PRINT
printf("==== mqtt_enable_and_configure() ====\n\n");
#endif
return false;
}
// Free the concat message, since it won't be used anymore.
free(concat_message);
#ifdef DETAILED_PRINT
printf("\n==== mqtt_enable_and_configure() ====\n\n");
#endif
return true;
}
uint8_t mqtt_login(char client_id[], char user_name[], char password[]) {
#ifdef DETAILED_PRINT
printf("\n======= mqtt_login() =======\n");
#endif
/********************* SENDING USER INFO **********************/
const char prefix[] = "#MQCONN=1,";
const char midfix[] = ",";
// Create a heap memory for the message concating.
char* concat_message = (char *) malloc(sizeof(char) * (strlen(prefix) + strlen(client_id) + strlen(midfix) + strlen(user_name) + strlen(midfix) + strlen(password) + 1));
memset(concat_message, '\0', sizeof(char) * (strlen(prefix) + strlen(client_id) + strlen(midfix) + strlen(user_name) + strlen(midfix) + strlen(password) + 1));
// Concate the message.
strcat(concat_message, prefix);
strcat(concat_message, client_id);
strcat(concat_message, midfix);
strcat(concat_message, user_name);
strcat(concat_message, midfix);
strcat(concat_message, password);
// Send it to TELIT.
send_message_to_telit(concat_message);
#ifdef DETAILED_PRINT
printf("-- login details sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 10 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS * 2);
free(concat_message);
/********************* SENDING CONFIRM **********************/
char confirm_message[] = "#MQCONN?";
const char confirm_prefix[] = "#MQCONN: 1,";
send_message_to_telit(confirm_message);
#ifdef DETAILED_PRINT
printf("-- confirmation request sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, confirm_prefix);
if (index_start != NULL) {
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
if (index_end == NULL) return 60;
// Save it as a variable to use it later.
uint8_t status_code = index_start[strlen(confirm_prefix)] - '0';
#ifdef DETAILED_PRINT
printf("-- RESULT: status code %d\n", status_code);
printf("======= mqtt_login() =======\n\n");
#endif
return status_code;
}
#ifdef DETAILED_PRINT
printf("======= mqtt_login() =======\n\n");
#endif
return 60; // 60 is the error code.
}
bool mqtt_subscribe_topic(char topic_subscribe_address[]) {
#ifdef DETAILED_PRINT
printf("\n==== mqtt_subscribe_topic() ====\n");
#endif
const char prefix[] = "#MQSUB=1,";
const char confirm[] = "#MQSUB";
// Create a heap memory for the message concating.
char* concat_message = (char *) malloc(sizeof(char) * (strlen(prefix) + strlen(topic_subscribe_address) + 1));
memset(concat_message, '\0', sizeof(char) * (strlen(prefix) + strlen(topic_subscribe_address) + 1));
// Concate the message.
strcat(concat_message, prefix);
strcat(concat_message, topic_subscribe_address);
// Send it to TELIT.
send_message_to_telit(concat_message);
free(concat_message);
#ifdef DETAILED_PRINT
printf("-- subscription request sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, confirm);
if (index_start != NULL) {
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
#ifdef DETAILED_PRINT
printf("-- RESULT: %s\n", (index_end != NULL) ? "subscribed" : "error");
printf("==== mqtt_subscribe_topic() ====\n\n");
#endif
if (index_end == NULL) return true;
else return false;
}
printf("==== mqtt_subscribe_topic() ====\n\n");
return true;
}
bool mqtt_publish(char topic_publish_address[], char string_to_publish[]) {
#ifdef DETAILED_PRINT
printf("\n======= mqtt_publish() =======\n");
#endif
const char prefix[] = "#MQPUBS=1,";
const char midfix[] = ",0,0,";
// Create a heap memory for the message concating.
char* concat_message = (char *) malloc(sizeof(char) * (strlen(prefix) + strlen(topic_publish_address) + strlen(midfix) + strlen(string_to_publish) + 1));
memset(concat_message, '\0', sizeof(char) * (strlen(prefix) + strlen(topic_publish_address) + strlen(midfix) + strlen(string_to_publish) + 1));
// Concate the message.
strcat(concat_message, prefix);
strcat(concat_message, topic_publish_address);
strcat(concat_message, midfix);
strcat(concat_message, string_to_publish);
// Send it to TELIT.
send_message_to_telit(concat_message);
free(concat_message);
#ifdef DETAILED_PRINT
printf("-- publish request sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 5 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, prefix);
if (index_start != NULL) {
// Check if there is a OK signal.
index_end = strstr(uart0_buffer, "\r\nOK\r\n");
#ifdef DETAILED_PRINT
printf("-- RESULT: The message has %s\n", (index_end != NULL) ? "sent." : "not sent.");
printf("======= mqtt_publish() =======\n\n");
#endif
if (index_end == NULL) return true;
else return false;
}
printf("======= mqtt_publish() =======\n\n");
return true;
}
/**
* @brief The function checks and sets everything, and connect the TELIT into 3G network.
*
*/
void telit_init_3g() {
// Signal Quailty Check.
process_signal_quailty();
// Carrier Registration Check.
process_carrier_registration();
// GPRS Registration Check.
process_gprs_registration();
// GPRS Attach Check.
process_gprs_attach();
// Define APN.
printf("$> Defining APN...\n");
bool is_apn_ready = !define_apn();
if (is_apn_ready)
printf("$> APN definition success.\n");
else
printf("$> APN definition failed.\n");
// Activate PDP Context.
printf("$> Activating PDP context...\n");
bool is_pdp_ready = !activate_pdp();
if (is_pdp_ready)
printf("$> PDP context activated.\n");
else
printf("$> PDP context couldn't activated.\n");
}
/**
* @brief This function activates Packet Domain Protocol. Returns "false" if it is activated.
*
* @return true: PDP is not activated.
* @return false PDP is activated.
*/
bool activate_pdp() {
#ifdef DETAILED_PRINT
// Inform the carrier registration is being checked.
printf("\n==== activate_pdp() ====\n");
#endif
// Create command and send it.
char command_message[] = "#SGACT=1,1";
char return_message[] = "#SGACT: ";
send_message_to_telit(command_message);
#ifdef DETAILED_PRINT
printf("-- message sent to modem.\n");
// Wait a little bit to recieve message.
printf("-- waiting 15 seconds.\n");
#endif
sleep_ms(TELIT_MSG_WAIT_MS * 3);
// Check if the returned message is belongs to our command.
index_start = strstr(uart0_buffer, command_message);
if (index_start != NULL) {
// Select only the IP address of the returned message.
index_start = strstr(index_start + strlen(command_message), return_message);
// Check if it is OK.
index_end = strstr(index_start, "\r\n\r\nOK\r\n");
if (index_end == NULL) return true;
char* substr = (char*) malloc(sizeof(char) * (index_end - index_start + 1));
strncpy(substr, index_start + strlen(return_message), index_end - index_start + 1);
// Save the positions of the delimeters.
uint8_t indices_of_delimeters[4];
uint8_t index_of_delimeter = 0;
// Travel inside the sub string substr.
for (uint16_t str_index = 0; str_index < strlen(substr); str_index++) {
// Check if the current character is a delimeter.
if (substr[str_index] == '.' || (substr[str_index] == '\r' && substr[str_index+1] == '\n')) {
// If it is, save the index of the delimeter.
indices_of_delimeters[index_of_delimeter] = str_index;
// Increase the index of the delimeter.
index_of_delimeter++;
}
// End the loop, if indices are finished.
if (index_of_delimeter == 4) break;
}
// Save each IP address number into a char array.
char sub_data[4][4] = {"", "", "", ""};
strncpy(sub_data[0], substr, indices_of_delimeters[0]);
strncpy(sub_data[1], substr + indices_of_delimeters[0] + 1, indices_of_delimeters[1] - indices_of_delimeters[0] - 1);
strncpy(sub_data[2], substr + indices_of_delimeters[1] + 1, indices_of_delimeters[2] - indices_of_delimeters[1] - 1);
strncpy(sub_data[3], substr + indices_of_delimeters[2] + 1, indices_of_delimeters[3] - indices_of_delimeters[2] - 1);
// Create ip_address in heap, and give the numbers into.
ip_address = (uint16_t*) malloc(sizeof(uint16_t) * 4);
for (int i = 0; i < 4; i++)
ip_address[i] = atoi(sub_data[i]);
#ifdef DETAILED_PRINT
printf("-- RESULT: ip addr= %d %d %d %d", ip_address[0], ip_address[1], ip_address[2], ip_address[3]);
printf("\n==== define_apn() ====\n\n");
#endif
// Free the memory.
free(substr);
free(ip_address);
return false;
}
// Any other case, return true -- means PDP is not defined.
return true;
}
/**
* @brief It defines APN details. If everything works correctly, it returns false.
*
* @return true APN is not setted.
* @return false APN is setted.
*/
bool define_apn() {
#ifdef DETAILED_PRINT
// Inform the carrier registration is being checked.
printf("\n==== define_apn() ====\n");
#endif
/* TODO: Let user to change "super" in future. */
// Create command to send it.