forked from arduino/mkrwan1300-fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.c
799 lines (729 loc) · 21 KB
/
command.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
799
/*******************************************************************************
* @file command.c
* @author MCD Application Team
* @version V1.1.2
* @date 08-September-2017
* @brief main command driver dedicated to command AT
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V.
* All rights reserved.</center></h2>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <stdlib.h>
#include "at.h"
#include "hw.h"
#include "command.h"
/* comment the following to have help message */
/* #define NO_HELP */
/* #define NO_KEY_ADDR_EUI */
/* Private typedef -----------------------------------------------------------*/
/**
* @brief Structure defining an AT Command
*/
struct ATCommand_s {
const char *string; /*< command string, after the "AT" */
const int size_string; /*< size of the command string, not including the final \0 */
ATEerror_t (*get)(const char *param); /*< =? after the string to get the current value*/
ATEerror_t (*set)(const char *param); /*< = (but not =?\0) after the string to set a value */
ATEerror_t (*run)(const char *param); /*< \0 after the string - run the command */
#if !defined(NO_HELP)
const char *help_string; /*< to be printed when ? after the string */
#endif
};
/* Private define ------------------------------------------------------------*/
#define CMD_SIZE 128
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define NO_HELP
/**
* @brief Array corresponding to the description of each possible AT Error
*/
static const char *const ATError_description[] =
{
"+OK\r", /* AT_OK */
"+ERR\r", /* AT_ERROR */
"+ERR_PARAM\r", /* AT_PARAM_ERROR */
"+ERR_BUSY\r", /* AT_BUSY_ERROR */
"+ERR_PARAM_OVERFLOW\r", /* AT_TEST_PARAM_OVERFLOW */
"+ERR_NO_NETWORK\r", /* AT_NO_NET_JOINED */
"+ERR_RX\r", /* AT_RX_ERROR */
"+ERR_UNKNOWN\r", /* AT_MAX */
};
/**
* @brief Array of all supported AT Commands
*/
static const struct ATCommand_s ATCommand[] =
{
{
.string = AT_RESET,
.size_string = sizeof(AT_RESET) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RESET ": Trig a reset of the MCU\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_reset,
},
#ifndef NO_KEY_ADDR_EUI
{
.string = AT_DEUI,
.size_string = sizeof(AT_DEUI) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_DEUI ": Get the Device EUI\r\n",
#endif
.get = at_DevEUI_get,
.set = at_return_error,
.run = at_return_error,
},
#endif
#ifndef NO_KEY_ADDR_EUI
{
.string = AT_DADDR,
.size_string = sizeof(AT_DADDR) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_DADDR ": Get or Set the Device address\r\n",
#endif
.get = at_DevAddr_get,
.set = at_DevAddr_set,
.run = at_return_error,
},
#endif
#ifndef NO_BAND_RUNTIME_SWITCH
{
.string = AT_BAND,
.size_string = sizeof(AT_BAND) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_BAND ": Get or Set the Regional Band\r\n",
#endif
.get = at_Band_get,
.set = at_Band_set,
.run = at_return_error,
},
#endif
#ifndef NO_KEY_ADDR_EUI
{
.string = AT_APPKEY,
.size_string = sizeof(AT_APPKEY) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_APPKEY ": Get or Set the Application Key\r\n",
#endif
.get = at_AppKey_get,
.set = at_AppKey_set,
.run = at_return_error,
},
#endif
#ifndef NO_KEY_ADDR_EUI
{
.string = AT_NWKSKEY,
.size_string = sizeof(AT_NWKSKEY) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_NWKSKEY ": Get or Set the Network Session Key\r\n",
#endif
.get = at_NwkSKey_get,
.set = at_NwkSKey_set,
.run = at_return_error,
},
#endif
#ifndef NO_KEY_ADDR_EUI
{
.string = AT_APPSKEY,
.size_string = sizeof(AT_APPSKEY) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_APPSKEY ": Get or Set the Application Session Key\r\n",
#endif
.get = at_AppSKey_get,
.set = at_AppSKey_set,
.run = at_return_error,
},
#endif
#ifndef NO_KEY_ADDR_EUI
{
.string = AT_APPEUI,
.size_string = sizeof(AT_APPEUI) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_APPEUI ": Get or Set the Application EUI\r\n",
#endif
.get = at_AppEUI_get,
.set = at_AppEUI_set,
.run = at_return_error,
},
#endif
{
.string = AT_ADR,
.size_string = sizeof(AT_ADR) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_ADR ": Get or Set the Adaptive Data Rate setting. (0: off, 1: on)\r\n",
#endif
.get = at_ADR_get,
.set = at_ADR_set,
.run = at_return_error,
},
{
.string = AT_TXP,
.size_string = sizeof(AT_TXP) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_TXP ": Get or Set the Transmit Power (0-5)\r\n",
#endif
.get = at_TransmitPower_get,
.set = at_TransmitPower_set,
.run = at_return_error,
},
{
.string = AT_DR,
.size_string = sizeof(AT_DR) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_DR ": Get or Set the Data Rate. (0-7 corresponding to DR_X)\r\n",
#endif
.get = at_DataRate_get,
.set = at_DataRate_set,
.run = at_return_error,
},
{
.string = AT_DCS,
.size_string = sizeof(AT_DCS) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_DCS ": Get or Set the ETSI Duty Cycle setting - 0=disable, 1=enable - Only for testing\r\n",
#endif
.get = at_DutyCycle_get,
.set = at_DutyCycle_set,
.run = at_return_error,
},
{
.string = AT_PNM,
.size_string = sizeof(AT_PNM) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_PNM ": Get or Set the public network mode. (0: off, 1: on)\r\n",
#endif
.get = at_PublicNetwork_get,
.set = at_PublicNetwork_set,
.run = at_return_error,
},
{
.string = AT_RX2FQ,
.size_string = sizeof(AT_RX2FQ) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RX2FQ ": Get or Set the Rx2 window frequency\r\n",
#endif
.get = at_Rx2Frequency_get,
.set = at_Rx2Frequency_set,
.run = at_return_error,
},
{
.string = AT_RX2DR,
.size_string = sizeof(AT_RX2DR) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RX2DR ": Get or Set the Rx2 window data rate (0-7 corresponding to DR_X)\r\n",
#endif
.get = at_Rx2DataRate_get,
.set = at_Rx2DataRate_set,
.run = at_return_error,
},
{
.string = AT_RX1DL,
.size_string = sizeof(AT_RX1DL) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RX1DL ": Get or Set the delay between the end of the Tx and the Rx Window 1 in ms\r\n",
#endif
.get = at_Rx1Delay_get,
.set = at_Rx1Delay_set,
.run = at_return_error,
},
{
.string = AT_RX2DL,
.size_string = sizeof(AT_RX2DL) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RX2DL ": Get or Set the delay between the end of the Tx and the Rx Window 2 in ms\r\n",
#endif
.get = at_Rx2Delay_get,
.set = at_Rx2Delay_set,
.run = at_return_error,
},
{
.string = AT_JN1DL,
.size_string = sizeof(AT_JN1DL) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_JN1DL ": Get or Set the Join Accept Delay between the end of the Tx and the Join Rx Window 1 in ms\r\n",
#endif
.get = at_JoinAcceptDelay1_get,
.set = at_JoinAcceptDelay1_set,
.run = at_return_error,
},
{
.string = AT_JN2DL,
.size_string = sizeof(AT_JN2DL) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_JN2DL ": Get or Set the Join Accept Delay between the end of the Tx and the Join Rx Window 2 in ms\r\n",
#endif
.get = at_JoinAcceptDelay2_get,
.set = at_JoinAcceptDelay2_set,
.run = at_return_error,
},
{
.string = AT_NJM,
.size_string = sizeof(AT_NJM) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_NJM ": Get or Set the Network Join Mode. (0: ABP, 1: OTAA)\r\n",
#endif
.get = at_NetworkJoinMode_get,
.set = at_NetworkJoinMode_set,
.run = at_return_error,
},
{
.string = AT_NWKID,
.size_string = sizeof(AT_NWKID) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_NWKID ": Get or Set the Network ID\r\n",
#endif
.get = at_NetworkID_get,
.set = at_NetworkID_set,
.run = at_return_error,
},
{
.string = AT_FCU,
.size_string = sizeof(AT_FCU) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_FCU ": Get or Set the Frame Counter Uplink\r\n",
#endif
.get = at_UplinkCounter_get,
.set = at_UplinkCounter_set,
.run = at_return_error,
},
{
.string = AT_FCD,
.size_string = sizeof(AT_FCD) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_FCD ": Get or Set the Frame Counter Downlink\r\n",
#endif
.get = at_DownlinkCounter_get,
.set = at_DownlinkCounter_set,
.run = at_return_error,
},
{
.string = AT_CLASS,
.size_string = sizeof(AT_CLASS) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_CLASS ": Get or Set the Device Class\r\n",
#endif
.get = at_DeviceClass_get,
.set = at_DeviceClass_set,
.run = at_return_error,
},
{
.string = AT_JOIN,
.size_string = sizeof(AT_JOIN) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_JOIN ": Join network\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_Join,
},
{
.string = AT_NJS,
.size_string = sizeof(AT_NJS) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_NJS ": Get the join status\r\n",
#endif
.get = at_NetworkJoinStatus,
.set = at_return_error,
.run = at_return_error,
},
{
.string = AT_SENDB,
.size_string = sizeof(AT_SENDB) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_SENDB ": Send hexadecimal data along with the application port\r\n",
#endif
.get = at_return_error,
.set = at_SendBinary,
.run = at_return_error,
},
{
.string = AT_SEND,
.size_string = sizeof(AT_SEND) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_SEND ": Send text data along with the application port\r\n",
#endif
.get = at_return_error,
.set = at_Send,
.run = at_return_error,
},
{
.string = AT_RECVB,
.size_string = sizeof(AT_RECVB) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RECVB ": print last received data in binary format (with hexadecimal values)\r\n",
#endif
.get = at_ReceiveBinary,
.set = at_return_error,
.run = at_ReceiveBinary,
},
{
.string = AT_RECV,
.size_string = sizeof(AT_RECV) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RECV ": print last received data in raw format\r\n",
#endif
.get = at_Receive,
.set = at_return_error,
.run = at_Receive,
},
{
.string = AT_PORT,
.size_string = sizeof(AT_PORT) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_PORT ": set application port\r\n",
#endif
.get = at_Port_get,
.set = at_Port_set,
.run = at_return_error,
},
{
.string = AT_CTX,
.size_string = sizeof(AT_CTX) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_CTX ": send with confirmation\r\n",
#endif
.get = at_return_error,
.set = at_SendV2Confirmation,
.run = at_return_error,
},
{
.string = AT_UTX,
.size_string = sizeof(AT_UTX) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_UTX ": send without confirmation\r\n",
#endif
.get = at_return_error,
.set = at_SendV2,
.run = at_return_error,
},
{
.string = AT_FORMAT,
.size_string = sizeof(AT_FORMAT) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_FORMAT ": select hex or binary format\r\n",
#endif
.get = at_Format_get,
.set = at_Format_set,
.run = at_return_error,
},
{
.string = AT_VER,
.size_string = sizeof(AT_VER) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_VER ": Get the version of the AT_Slave FW\r\n",
#endif
.get = at_version_get,
.set = at_return_error,
.run = at_return_error,
},
{
.string = AT_DEV,
.size_string = sizeof(AT_DEV) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_DEV ": Get the version of the AT_Slave FW\r\n",
#endif
.get = at_device_get,
.set = at_return_error,
.run = at_return_error,
},
{
.string = AT_CFM,
.size_string = sizeof(AT_CFM) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_CFM ": Get or Set the confirmation mode (0-1)\r\n",
#endif
.get = at_ack_get,
.set = at_ack_set,
.run = at_return_error,
},
{
.string = AT_CFS,
.size_string = sizeof(AT_CFS) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_CFS ": Get confirmation status of the last AT+SEND (0-1)\r\n",
#endif
.get = at_isack_get,
.set = at_return_error,
.run = at_return_error,
},
{
.string = AT_SNR,
.size_string = sizeof(AT_SNR) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_SNR ": Get the SNR of the last received packet\r\n",
#endif
.get = at_snr_get,
.set = at_return_error,
.run = at_return_error,
},
{
.string = AT_RSSI,
.size_string = sizeof(AT_RSSI) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_RSSI ": Get the RSSI of the last received packet\r\n",
#endif
.get = at_rssi_get,
.set = at_return_error,
.run = at_return_error,
},
{
.string = AT_BAT,
.size_string = sizeof(AT_BAT) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_BAT ": Get the battery level\r\n",
#endif
.get = at_bat_get,
.set = at_return_error,
.run = at_return_error,
},
{
.string = AT_TRSSI,
.size_string = sizeof(AT_TRSSI) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_TRSSI ": Starts RF RSSI tone test\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_test_rxTone,
},
{
.string = AT_TTONE,
.size_string = sizeof(AT_TTONE) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_TTONE ": Starts RF Tone test\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_test_txTone,
},
{
.string = AT_TTLRA,
.size_string = sizeof(AT_TTLRA) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_TTLRA ": Starts RF Tx LORA test\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_test_txlora,
},
{
.string = AT_TRLRA,
.size_string = sizeof(AT_TRLRA) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_TRLRA ": Starts RF Rx LORA test\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_test_rxlora,
},
{
.string = AT_TCONF,
.size_string = sizeof(AT_TCONF) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_TCONF ": Config LORA RF test\r\n",
#endif
.get = at_test_get_lora_config,
.set = at_test_set_lora_config,
.run = at_return_error,
},
{
.string = AT_TOFF,
.size_string = sizeof(AT_TOFF) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_TOFF ": Stops on-going RF test\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_test_stop,
},
{
.string = AT_CERTIF,
.size_string = sizeof(AT_CERTIF) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_CERTIF ": Set the module in LoraWan Certification Mode\r\n",
#endif
.get = at_return_error,
.set = at_return_error,
.run = at_Certif,
},
{
.string = AT_CHANMASK,
.size_string = sizeof(AT_CHANMASK) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_CHANMASK ": Config the region's channel mask for the session\r\n",
#endif
.get = at_ChannelMask_get,
.set = at_ChannelMask_set,
.run = at_return_error,
},
{
.string = AT_CHANDEFMASK,
.size_string = sizeof(AT_CHANDEFMASK) - 1,
#ifndef NO_HELP
.help_string = "AT"AT_CHANDEFMASK ": Config the region's channel default mask for the session\r\n",
#endif
.get = at_ChannelDefaultMask_get,
.set = at_ChannelDefaultMask_set,
.run = at_return_error,
},
};
/* Private function prototypes -----------------------------------------------*/
/**
* @brief Print a string corresponding to an ATEerror_t
* @param The AT error code
* @retval None
*/
static void com_error(ATEerror_t error_type);
/**
* @brief Parse a command and process it
* @param The command
* @retval None
*/
static void parse_cmd(const char *cmd);
/* Exported functions ---------------------------------------------------------*/
void CMD_Init(void)
{
vcom_Init();
vcom_ReceiveInit();
}
void CMD_Process(void)
{
static char command[CMD_SIZE];
static unsigned i = 0;
/* Process all commands */
while (IsNewCharReceived() == SET)
{
command[i] = GetNewChar();
#if 0 /* echo On */
PRINTF("%c", command[i]);
#endif
if (command[i] == AT_ERROR_RX_CHAR)
{
i = 0;
com_error(AT_RX_ERROR);
break;
}
else
if ((command[i] == '\r'))
{
if (i != 0)
{
command[i] = '\0';
parse_cmd(command);
i = 0;
}
}
else
if (i == (CMD_SIZE - 1))
{
i = 0;
com_error(AT_TEST_PARAM_OVERFLOW);
}
else
{
i++;
}
}
}
/* Private functions ---------------------------------------------------------*/
static void com_error(ATEerror_t error_type)
{
if (error_type > AT_MAX)
{
error_type = AT_MAX;
}
AT_PRINTF(ATError_description[error_type]);
}
static void parse_cmd(const char *cmd)
{
ATEerror_t status = AT_OK;
const struct ATCommand_s *Current_ATCommand;
int i;
uint8_t confirm_set = 0;
if ((cmd[0] != 'A') || (cmd[1] != 'T'))
{
status = AT_ERROR;
}
else
if (cmd[2] == '\0')
{
/* status = AT_OK; */
com_error(status);
}
else
{
/* point to the start of the command, excluding AT */
status = AT_ERROR;
cmd += 2;
for (i = 0; i < (sizeof(ATCommand) / sizeof(struct ATCommand_s)); i++)
{
if (strncmp(cmd, ATCommand[i].string, ATCommand[i].size_string) == 0)
{
Current_ATCommand = &(ATCommand[i]);
/* point to the string after the command to parse it */
cmd += Current_ATCommand->size_string;
/* parse after the command */
switch (cmd[0])
{
case '\0': /* nothing after the command */
status = Current_ATCommand->run(cmd);
break;
case '?':
status = Current_ATCommand->get(cmd + 1);
break;
case '=':
case ' ': // special case for CTX and UTX
status = Current_ATCommand->set(cmd + 1);
confirm_set = 1;
break;
default:
/* not recognized */
break;
}
/* we end the loop as the command was found */
break;
}
}
}
if (status != AT_OK || (confirm_set == 1)) {
com_error(status);
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/