Repository navigation
Expand file tree
/
Copy pathcli.c
More file actions
1328 lines (1180 loc) · 39.9 KB
/
Copy pathcli.c
File metadata and controls
1328 lines (1180 loc) · 39.9 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
// Copyright (c) 2026 Ziga Miklosic
// All Rights Reserved
// This software is under MIT licence (https://opensource.org/licenses/MIT)
////////////////////////////////////////////////////////////////////////////////
/**
*@file cli.c
*@brief Command Line Interface
*@author Ziga Miklosic
*@email ziga.miklosic@gmail.com
*@date 29.01.2026
*@version V3.1.1
*/
////////////////////////////////////////////////////////////////////////////////
/*!
* @addtogroup CLI
* @{ <!-- BEGIN GROUP -->
*/
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "cli.h"
#include "cli_util.h"
#include "cli_nvm.h"
#include "cli_par.h"
#include "cli_osci.h"
#include "../../cli_cfg.h"
#include "../../cli_if.h"
////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function prototypes
////////////////////////////////////////////////////////////////////////////////
static cli_status_t cli_parser_hndl (void);
static void cli_execute_cmd (const char * const p_cmd);
static bool cli_table_check_and_exe (const char * p_cmd, const uint32_t cmd_size, const char * attr);
static uint32_t cli_calc_cmd_size (const char * p_cmd, const char * attr);
// Basic CLI functions
static void cli_help (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_reset (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_sw_version (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_hw_version (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_boot_version (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_proj_info (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_uptime (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_ch_info (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_ch_en (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_sys_info (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_send_intro (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_show_intro (void);
#if ( 1 == CLI_CFG_ARBITRARY_RAM_ACCESS_EN )
static void cli_ram_write (const cli_cmd_t * p_cmd, const char * p_attr);
static void cli_ram_read (const cli_cmd_t * p_cmd, const char * p_attr);
#endif
static bool cli_validate_user_table (const cli_cmd_t * const p_cmd_table, const uint32_t num_of_cmd);
static const char * cli_find_char (const char * const str, const char target_char, const uint32_t size);
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
/**
* Initialization guard
*/
static bool gb_is_init = false;
/**
* Basic CLI commands
*/
CLI_DEFINE_CMD_TABLE( g_cli_basic_table,
// --------------------------------------------------------------------------------------------------------------------------
// name function help string context
// --------------------------------------------------------------------------------------------------------------------------
{ "help", cli_help, "Print help message", NULL },
{ "intro", cli_send_intro, "Print intro message", NULL },
{ "reset", cli_reset, "Reset device", NULL },
{ "sw_ver", cli_sw_version, "Print device software version", NULL },
{ "hw_ver", cli_hw_version, "Print device hardware version", NULL },
{ "boot_ver", cli_boot_version, "Print device bootloader (sw) version", NULL },
{ "proj_info", cli_proj_info, "Print project informations", NULL },
{ "uptime", cli_uptime, "Get device uptime [ms]", NULL },
{ "ch_info", cli_ch_info, "Print COM channel informations", NULL },
{ "ch_en", cli_ch_en, "Enable/disable COM channel. Args: [chEnum][en]", NULL },
{ "sys_info", cli_sys_info, "Print system informations", NULL },
#if ( 1 == CLI_CFG_ARBITRARY_RAM_ACCESS_EN )
{ "ram_write", cli_ram_write, "Write data to RAM. Args: [address<hex>][size][value<hex>]", NULL },
{ "ram_read", cli_ram_read, "Read data from RAM. Args: [address<hex>][size]", NULL },
#endif
);
/**
* Pointer to first registered CLI command table
*/
static cli_cmd_table_t * gp_cli_cmd_tables = NULL;
////////////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Parse input string
*
* @return status - Status of operation
*/
////////////////////////////////////////////////////////////////////////////////
static cli_status_t cli_parser_hndl(void)
{
cli_status_t status = eCLI_OK;
static uint32_t buf_idx = 0;
uint32_t escape_cnt = 0;
static char rx_buffer[CLI_CFG_RX_BUF_SIZE] = {0};
static uint32_t first_byte_time = 0U;
// Take all data from reception buffer
while ( ( eCLI_OK == cli_if_receive( &rx_buffer[buf_idx] ))
&& ( escape_cnt < 10000UL ))
{
if( 0 == buf_idx )
{
first_byte_time = CLI_GET_SYSTICK();
}
// Find termination character
char * p_term_str_start = strstr((char*) &rx_buffer, (char*) CLI_CFG_TERMINATION_STRING );
// Termination string found
if ( NULL != p_term_str_start )
{
// Replace all termination character with NULL
memset((char*) p_term_str_start, 0, strlen( CLI_CFG_TERMINATION_STRING ));
// Execute command
cli_execute_cmd( (const char *)rx_buffer );
// Reset buffer
memset( &rx_buffer, 0U, sizeof( rx_buffer ));
buf_idx = 0;
break;
}
// Still size in buffer?
else if ( buf_idx < ( CLI_CFG_RX_BUF_SIZE - 2 ))
{
buf_idx++;
}
// No more size in buffer --> OVERRUN ERROR
else
{
CLI_DBG_PRINT( "CLI: Overrun Error!" );
CLI_ASSERT( 0 );
// Reset buffer
memset( &rx_buffer, 0U, sizeof( rx_buffer ));
buf_idx = 0;
status = eCLI_ERROR;
break;
}
// Increment escape count in order to prevent infinite loop
escape_cnt++;
}
// Expected that complete command will be received within 100ms
if ( ((uint32_t)( CLI_GET_SYSTICK() - first_byte_time ) >= 100U )
&& ( buf_idx > 0 )) // Reception ongoing
{
CLI_DBG_PRINT( "CLI: Timeout!" );
memset( &rx_buffer, 0U, sizeof( rx_buffer ));
buf_idx = 0;
status = eCLI_ERROR;
}
return status;
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Find & execute Command Line Interface command
*
* @note Entering that function means CR or LF has been received. Input
* string is terminated with '\0'.
*
* @note Attribute to all CLI functions are after space character.
*
* Format: >>>cmd_name "attr"
*
*
* E.g. with cmd_name="par_get", attr="12"
*
* >>>par_get 12
* ||--> start of attributes
* |
* empty space
*
* will raise function:
*
* void cli_par_set(const uint8_t * attr)
* {
* // attr = "12"
* }
*
*
* @param[in] p_cmd - NULL terminated input string
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_execute_cmd(const char * const p_cmd)
{
// Get command options
const char * attr = cli_find_char(p_cmd, ' ', CLI_CFG_RX_BUF_SIZE );
// Calculate size of command string
const uint32_t cmd_size = cli_calc_cmd_size( p_cmd, attr );
// Check and execute for basic commands
const bool cmd_found = cli_table_check_and_exe( p_cmd, cmd_size, attr );
// No command found in any of the tables
if ( false == cmd_found )
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Check and execute table commands
*
* @note Commands are divided into simple and combined commands
*
* SIMPLE COMMAND: Do not pass additional attributes
*
* E.g.: >>>help
*
* COMBINED COMMAND: Has additional attributes separated by
* empty spaces (' ').
*
* E.g.: >>>par_get 0
*
*
* @param[in] p_cmd - NULL terminated input string
* @param[in] attr - Additional command attributes
* @return cmd_found - Command found flag
*/
////////////////////////////////////////////////////////////////////////////////
static bool cli_table_check_and_exe(const char * p_cmd, const uint32_t cmd_size, const char * attr)
{
// Iterate thru table linked list
for ( const cli_cmd_table_t * table = gp_cli_cmd_tables; NULL != table; table = (*table->p_next))
{
// Iterate thru all commands in table
for (size_t cmd = 0; cmd < table->num_of; cmd++)
{
// String size to compare
const size_t size_to_compare = MAX( cmd_size, strlen( table->p_cmd[cmd].name ));
// Valid command?
if ( 0 == ( strncmp( p_cmd, table->p_cmd[cmd].name, size_to_compare )))
{
// Execute command
table->p_cmd[cmd].func( &table->p_cmd[cmd], attr );
// Command founded
return true;
}
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Calculate inputed command size
*
* @param[in] p_cmd - Command string
* @param[in] attr - Rest of the command string
* @return size - Size of the command
*/
////////////////////////////////////////////////////////////////////////////////
static uint32_t cli_calc_cmd_size(const char * p_cmd, const char * attr)
{
uint32_t size = 0;
// Simple command
if ( NULL == attr )
{
size = strlen((const char*) p_cmd );
}
// Combined command - must have a empty space
else
{
const char * p_cmd_end = cli_find_char( p_cmd, ' ', CLI_CFG_RX_BUF_SIZE );
if (NULL != p_cmd_end)
{
size = (uint32_t)(p_cmd_end - p_cmd - 1); // -1 because find character function returns position +1
}
}
return size;
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show help
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_help(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
// No additional attributes
if ( NULL == p_attr )
{
cli_printf( " " );
cli_printf( " List of device commands" );
// Iterate thru table linked list
for ( const cli_cmd_table_t * table = gp_cli_cmd_tables; NULL != table; table = (*table->p_next))
{
// Are there any commands
if ( table->num_of > 0U )
{
// Print separator between user commands
cli_printf( "--------------------------------------------------------" );
// Show help for that table
for ( uint32_t cmd_idx = 0; cmd_idx < table->num_of; cmd_idx++ )
{
// Get name and help string
const char * name_str = table->p_cmd[cmd_idx].name;
const char * help_str = table->p_cmd[cmd_idx].help;
// Left adjust for 25 chars
cli_printf( "%-25s%s", name_str, help_str );
}
}
}
// Print separator at the end
cli_printf( "--------------------------------------------------------" );
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Reset device
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_reset(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
if ( NULL == p_attr )
{
cli_printf("OK, Reseting device...");
cli_if_device_reset();
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show SW version
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_sw_version(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
if ( NULL == p_attr )
{
cli_printf( "OK, %s", CLI_CFG_INTRO_SW_VER );
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show HW version
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_hw_version(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
if ( NULL == p_attr )
{
cli_printf( "OK, %s", CLI_CFG_INTRO_HW_VER );
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show bootloader (SW) version
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_boot_version(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
if ( NULL == p_attr )
{
cli_printf( "OK, %s", CLI_CFG_INTRO_BOOT_VER );
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show detailed project informations
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_proj_info(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
if ( NULL == p_attr )
{
cli_printf( "OK, %s", CLI_CFG_INTRO_PROJ_INFO );
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Get device uptime [ms]
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_uptime(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
if ( NULL == p_attr )
{
const uint64_t uptime = cli_if_get_uptime();
cli_printf("OK, %lu%09lums", (uint32_t)(uptime / 1000000000ULL), (uint32_t)(uptime % 1000000000ULL));
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show communication channel info
*
* @note Command format: >>>cli_ch_info
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_ch_info(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
if ( NULL == p_attr )
{
cli_printf( "--------------------------------------------------------" );
cli_printf( " Communication Channels Info" );
cli_printf( "--------------------------------------------------------" );
cli_printf( " %-8s%-20s%s", "chEnum", "Name", "State" );
cli_printf( " ------------------------------------" );
for (uint8_t ch = 0; ch < eCLI_CH_NUM_OF; ch++)
{
cli_printf(" %02d %-20s%s", ch, cli_cfg_get_ch_name(ch), (cli_cfg_get_ch_en(ch) ? ( "Enable" ) : ( "Disable" )));
}
cli_printf( "--------------------------------------------------------" );
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Enable/Disable communication channel
*
*
* @note Command format: >>>cli_ch_en [chEnum,en]
*
* E.g.: >>>cli_ch_en 0, 1 // Disable "WARNING" channel
* E.g.: >>>cli_ch_en 1, 0 // Enable "ERROR" channel
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_ch_en(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
uint32_t ch;
uint32_t en;
if ( NULL != p_attr )
{
// Check input command
if ( 2U == sscanf((const char*) p_attr, "%u,%u", (unsigned int*)&ch, (unsigned int*)&en ))
{
if ( ch < eCLI_CH_NUM_OF )
{
cli_cfg_set_ch_en( ch, en );
cli_printf( "OK, %s channel %s", (( true == en ) ? ("Enabling") : ("Disabling")), cli_cfg_get_ch_name( ch ));
}
else
{
cli_printf( "ERR, Invalid chEnum!" );
}
}
else
{
cli_util_unknown_cmd_rsp();
}
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show system informations about errors & warnings
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_sys_info(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
UNUSED(p_attr);
cli_printf( "--------------------------------------------------------" );
cli_printf( " System Errors (%d total)", (int)eSYS_ERR_NUM_OF );
cli_printf( "--------------------------------------------------------" );
cli_printf( " %-6s %-6s %-6s %-8s %s", "Enum", "Src", "Code", "Status", "Description" );
cli_printf( "--------------------------------------------------------" );
for ( sys_err_t err = 0; err < eSYS_ERR_NUM_OF; err++ )
{
const bool active = sys_diag_is_err_active( err );
const sys_diag_src_t src = sys_diag_get_err_source( err );
const uint8_t code = sys_diag_get_err_code( err );
const char * desc = sys_diag_get_err_desc( err );
cli_printf( " %-6u %-6u %-6u %-8s %s",
(unsigned)err,
(unsigned)src,
(unsigned)code,
active ? "ACTIVE" : "---",
( desc != NULL ) ? desc : "-" );
}
cli_printf( " " );
cli_printf( "--------------------------------------------------------" );
cli_printf( " System Warnings (%d total)", (int)eSYS_WAR_NUM_OF );
cli_printf( "--------------------------------------------------------" );
cli_printf( " %-6s %-6s %-6s %-8s %s", "Enum", "Src", "Code", "Status", "Description" );
cli_printf( "--------------------------------------------------------" );
for ( sys_war_t war = 0; war < eSYS_WAR_NUM_OF; war++ )
{
const bool active = sys_diag_is_war_active( war );
const sys_diag_src_t src = sys_diag_get_war_source( war );
const uint8_t code = sys_diag_get_war_code( war );
const char * desc = sys_diag_get_war_desc( war );
cli_printf( " %-6u %-6u %-6u %-8s %s",
(unsigned)war,
(unsigned)src,
(unsigned)code,
active ? "ACTIVE" : "---",
( desc != NULL ) ? desc : "-" );
}
cli_printf( "--------------------------------------------------------" );
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Show intro
*
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_show_intro(void)
{
cli_printf( " " );
cli_printf( "********************************************************" );
cli_printf( " %s", CLI_CFG_INTRO_PROJECT_NAME );
cli_printf( "********************************************************" );
cli_printf( " %s", CLI_CFG_INTRO_SW_VER );
cli_printf( " %s", CLI_CFG_INTRO_HW_VER );
cli_printf( " ");
cli_printf( " Enter 'help' to display supported commands" );
cli_printf( "********************************************************" );
cli_printf( "Ready to take orders..." );
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Send intro string
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_send_intro(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
UNUSED(p_attr);
cli_show_intro();
}
#if ( 1 == CLI_CFG_ARBITRARY_RAM_ACCESS_EN )
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Write data to RAM
*
*
* @note Command format: >>>cli_ram_write [address,size,value]
* Address and value arguments must be inputed in hexadecimal format
* with '0x' prefix and followed by lowercase characters.
* Size must be in decimal format, with only valid values being:
* 1, 2 and 4.
*
* E.g.: >>>cli_ram_write 0xabcdef01,2,0x1234
* E.g.: >>>cli_ram_write 0x1234,4,0xab112233
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_ram_write(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
uint32_t addr;
uint32_t size;
uint32_t val;
// Make sure we can cast uint32_t to unsigned int below to supress compiler warning when types do not match exactly
// for example unsigned long to unsigned int
STATIC_ASSERT_TYPES(uint32_t, unsigned int);
if ( NULL != p_attr )
{
if ( 3U == sscanf((const char*) p_attr, "0x%x,%u,0x%x", (unsigned int *)&addr, (unsigned int *)&size, (unsigned int *)&val ))
{
if ((1 == size) || (2 == size) || (4 == size))
{
if (cli_if_check_ram_addr_range(addr, size) == eCLI_OK)
{
switch (size)
{
case 1:
*(uint8_t *)addr = (uint8_t)val;
break;
case 2:
*(uint16_t *)addr = (uint16_t)val;
break;
case 4:
*(uint32_t *)addr = (uint32_t)val;
break;
default:
// Internal inconsistency. Should not reach here since we check
// size above.
CLI_ASSERT(0);
break;
}
cli_printf( "OK, [0x%08x,0x%08x] = 0x%x", addr, addr + size - 1, val);
}
else
{
cli_printf( "ERR, Invalid address!" );
}
}
else
{
cli_printf( "ERR, Invalid size!" );
}
}
else
{
cli_util_unknown_cmd_rsp();
}
}
else
{
cli_util_unknown_cmd_rsp();
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Read data from RAM
*
*
* @note Command format: >>>cli_ram_read [address,size]
* Address argument must be inputed in hexadecimal format with '0x'
* prefix and followed by lowercase characters.
* Size must be in decimal format, with only valid values being:
* 1, 2 and 4.
*
* E.g.: >>>cli_ram_read 0xabcdef01,1
* E.g.: >>>cli_ram_read 0x1234,4
*
* @param[in] p_cmd - Pointer to command
* @param[in] p_attr - Inputed command attributes
* @return void
*/
////////////////////////////////////////////////////////////////////////////////
static void cli_ram_read(const cli_cmd_t * p_cmd, const char * p_attr)
{
UNUSED(p_cmd);
uint32_t addr;
uint32_t size;
// Make sure we can cast uint32_t to unsigned int below to supress compiler warning when types do not match exactly
// for example unsigned long to unsigned int
STATIC_ASSERT_TYPES(uint32_t, unsigned int);
if ( NULL != p_attr )
{
if ( 2U == sscanf((const char*) p_attr, "0x%x,%u", (unsigned int *)&addr, (unsigned int *)&size ))
{
if ((1 == size) || (2 == size) || (4 == size))
{
if (cli_if_check_ram_addr_range(addr, size) == eCLI_OK)
{
uint32_t val = 0;
switch (size)
{
case 1:
val = *(uint8_t *)addr;
break;
case 2:
val = *(uint16_t *)addr;
break;
case 4:
val = *(uint32_t *)addr;
break;
default:
// Internal inconsistency. Should not reach here since we check
// size above.
CLI_ASSERT(0);
break;
}
cli_printf( "0x%x", val);
}
else
{
cli_printf( "ERR, Invalid address!" );
}
}
else
{
cli_printf( "ERR, Invalid size!" );
}
}
else
{
cli_util_unknown_cmd_rsp();
}
}
else
{
cli_util_unknown_cmd_rsp();
}
}
#endif // CLI_CFG_ARBITRARY_RAM_ACCESS_EN
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Validate user defined table
*
* @param[in] p_cmd_table - User defined cli command table
* @param[in] num_of_cmd - Number of commands
* @return valid - Validation flag
*/
////////////////////////////////////////////////////////////////////////////////
static bool cli_validate_user_table(const cli_cmd_t * const p_cmd_table, const uint32_t num_of_cmd)
{
bool valid = true;
// Roll thru all commands
for (uint32_t cmd = 0; cmd < num_of_cmd; cmd++)
{
// Missing definitions?
if ( ( NULL == p_cmd_table[cmd].name )
|| ( NULL == p_cmd_table[cmd].help )
|| ( NULL == p_cmd_table[cmd].func ))
{
valid = false;
break;
}
}
return valid;
}
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Find character inside string
*
* @note Returned sub-string is string from target_char on and not with
* it!
*
* E.g: str = "Hello World"
* target_char = ' '
* sub_str = "World"
*
* @note If target string is last char in string it will return NULL!
*
* @param[in] str - Search string
* @param[in] target_char - Character to find
* @param[in] size - Total size to search for
* @return sub_str - Sub-string from target char on
*/
////////////////////////////////////////////////////////////////////////////////
static const char * cli_find_char(const char * const str, const char target_char, const uint32_t size)
{
char * sub_str = NULL;
uint32_t ch = 0;
for ( ch = 0; ch < size; ch++)
{
// End string reached
if ( '\0' == str[ch] )
{
break;
}
// Target char found
else if ( target_char == str[ch] )
{
// Return sub-string without target char
sub_str = (char*)( str + ch + 1 );
break;
}
else
{
// No actions...
}
}
return sub_str;
}
////////////////////////////////////////////////////////////////////////////////
/**
* @} <!-- END GROUP -->
*/
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
*@addtogroup CLI_API
* @{ <!-- BEGIN GROUP -->
*
* Following function are part of CLI API.
*/
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Initialize command line interface
*
* @return status - Status of initialization
*/
////////////////////////////////////////////////////////////////////////////////
cli_status_t cli_init(void)
{
cli_status_t status = eCLI_OK;
if ( false == gb_is_init )
{
// Initialize interface
status = cli_if_init();
// Register basic table
cli_register_cmd_table((cli_cmd_table_t*) &g_cli_basic_table );
// Initialize cli sub-components
#if ( 1 == CLI_CFG_PAR_USE_EN )
status |= cli_par_init();
#if ( 1 == CLI_CFG_PAR_OSCI_EN )
status |= cli_osci_init();
#endif
#endif
// Low level driver init error!
CLI_ASSERT( eCLI_OK == status );
if ( eCLI_OK == status )
{
// Init success
gb_is_init = true;
#if ( 1 == CLI_CFG_INTRO_STRING_EN )
cli_show_intro();
#endif
}
}
else
{
status = eCLI_ERROR_INIT;
}
return status;
}