Newer
Older
/******************************************************************************
Copyright 2016 - 2019 Intel Corporation
Copyright 2015 - 2016 Lantiq Beteiligungs-GmbH & Co. KG
Copyright 2009 - 2014 Lantiq Deutschland GmbH
Copyright 2007 - 2008 Infineon Technologies AG
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
For licensing information, see the file 'LICENSE' in the root folder of
this software module.
******************************************************************************/
/** \file
DSL daemon command line interface
*/
/*#define DSL_INTERN*/
#include "dsl_cpe_control.h"
#include "dsl_cpe_os.h"
#include "dsl_cpe_cli.h"
#ifdef LOGGING_ID
#include "ulogging.h"
#ifndef LOG_LEVEL
uint16_t LOGLEVEL = SYS_LOG_DEBUG + 1;
#else
uint16_t LOGLEVEL = LOG_LEVEL + 1;
#endif
#ifndef LOG_TYPE
uint16_t LOGTYPE = SYS_LOG_TYPE_FILE;
#else
uint16_t LOGTYPE = LOG_TYPE;
#endif
#else /* LOGGING_ID */
#define LOGF_LOG_INFO(...)
#endif /* LOGGING_ID */
#ifdef INCLUDE_DSL_CPE_CLI_SUPPORT
#undef DSL_CCA_DBG_BLOCK
#define DSL_CCA_DBG_BLOCK DSL_CCA_DBG_CLI
static DSL_void_t DSL_CPE_CLI_Cleanup(void);
static DSL_Error_t DSL_CPE_CLI_PrintHelp
(
const DSL_char_t *psHelp,
DSL_char_t *psCmdLong,
DSL_char_t *psCmdShort,
DSL_uint32_t nCmdMask,
DSL_CPE_CLI_PrintHelpCallback_t pFct,
DSL_CPE_File_t *out
);
/** 'less then' defintion for binary tree, (a < b)*/
#define compLT(a,b) (strcmp(a,b) < 0)
/** 'equal' defintion for binary tree, (a == b)*/
#define compEQ(a,b) (strcmp(a,b) == 0)
#define DSL_CLI_HELP_NOT_AVAILABLE "nReturn=-1 (wrong number of parameters/help not available) \n"
#define DSL_CLI_HELP_MEMORY_ERROR "nReturn=-1 (help string memory allocation error) \n"
#define DSL_CLI_HELP_NOTE_FUNCTION_DEPRECATED "Note: This function is deprecated! \n"
/** implementation dependend declarations */
typedef enum
{
DSL_CPE_STATUS_OK,
DSL_CPE_STATUS_MEM_EXHAUSTED,
DSL_CPE_STATUS_DUPLICATE_KEY,
DSL_CPE_STATUS_KEY_NOT_FOUND,
DSL_CPE_STATUS_KEY_INVALID
} DSL_CPE_statusEnum;
/** type of key */
typedef char* DSL_CPE_keyType;
/** user data stored in tree */
typedef struct
{
DSL_char_t *sCmdShort;
DSL_char_t *sCmdLong;
const DSL_char_t *psHelp;
unsigned int mask;
DSL_int_t (*func)(DSL_int_t, DSL_char_t*, DSL_CPE_File_t*);
DSL_CPE_CLI_PrintHelpCallback_t printFunc;
} DSL_CPE_recType;
typedef struct DSL_CPE_nodeTag
{
/* left child */
struct DSL_CPE_nodeTag *left;
/** right child */
struct DSL_CPE_nodeTag *right;
/** parent */
struct DSL_CPE_nodeTag *parent;
/** key used for searching */
DSL_CPE_keyType key;
/** user data */
DSL_CPE_recType rec;
} DSL_CPE_nodeType;
static DSL_CPE_statusEnum DSL_CPE_keyInsert
(
DSL_CPE_keyType key,
DSL_CPE_recType *rec
);
#ifdef INCLUDE_DSL_RESOURCE_STATISTICS
static void DSL_CPE_treeResourceUsageGet
(
DSL_CPE_nodeType *node,
unsigned int *pResStatic,
unsigned int *pResDynamic
);
#endif /* INCLUDE_DSL_RESOURCE_STATISTICS*/
static DSL_CPE_statusEnum DSL_CPE_keyDelete
(
DSL_CPE_keyType key
);
DSL_CPE_statusEnum DSL_CPE_keyFind
(
DSL_CPE_keyType key,
DSL_CPE_recType *rec
);
static void DSL_CPE_treePrint
(
DSL_CPE_nodeType *node,
unsigned int mask,
DSL_CPE_File_t *
);
static void DSL_CPE_treeDelete
(
DSL_CPE_nodeType *node
);
static void DSL_CPE_nodeDelete
(
DSL_CPE_nodeType *node
);
/** root of binary tree */
DSL_CPE_nodeType *root_node = DSL_NULL;
struct DSL_CLI_Context
{
/**
pointer for list */
DSL_CLI_Context_t *next;
/**
Context for CLI Callbacks */
DSL_void_t *pCBContext;
/**
Callback for CLI Shutdown */
DSL_CPE_Exit_Callback_t pExitCallback;
/**
DSL_CPE_API Event Callback, may be DSL_NULL */
DSL_CLI_Event_Callback_t pEventCallback;
};
DSL_char_t CLI_EventText[16000];
static DSL_CLI_Context_t *CLI_List_head = DSL_NULL;
/** indicates if the CLI already initialized */
static DSL_boolean_t bCLI_Init = DSL_FALSE;
static const DSL_char_t g_sQuit[] =
#ifndef DSL_CPE_DEBUG_DISABLE
"Long Form: %s" DSL_CPE_CRLF
"Short Form: %s" DSL_CPE_CRLF
DSL_CPE_CRLF
"Quits DSL CPE API Control Application" DSL_CPE_CRLF
DSL_CPE_CRLF "";
#else
"";
#endif
static DSL_int_t DSL_CPE_CLI_Quit(
DSL_int_t fd,
DSL_char_t *pCommands,
DSL_CPE_File_t *out)
{
if (DSL_CPE_CLI_CheckParamNumber(pCommands, 0, DSL_CLI_EQUALS) == DSL_FALSE)
{
return -1;
}
DSL_CPE_FPrintf (out,
"DSL CPE API Control Application termination started..."DSL_CPE_CRLF);
return 0;
}
/******************************************************************************/
DSL_Error_t DSL_CPE_CLI_Init(DSL_void_t)
{
if(bCLI_Init == DSL_FALSE)
{
DSL_CPE_CLI_CMD_ADD_COMM("help", "Help", DSL_CPE_CLI_HelpPrint, DSL_NULL);
DSL_CPE_CLI_CMD_ADD_COMM("quit", "Quit", DSL_CPE_CLI_Quit, g_sQuit);
DSL_CPE_CLI_AccessCommandsRegister();
bCLI_Init = DSL_TRUE;
}
return DSL_SUCCESS;
}
DSL_Error_t DSL_CPE_CLI_Shutdown(DSL_void_t)
{
DSL_CPE_CLI_CommandClear();
DSL_CPE_FPrintf (DSL_CPE_STDOUT, DSL_CPE_PREFIX "Goodbye from DSL CPE API CLI "
"interface" DSL_CPE_CRLF );
bCLI_Init = DSL_FALSE;
return DSL_SUCCESS;
}
DSL_Error_t DSL_CPE_CLI_HandleEvent(DSL_char_t *pMsg)
{
DSL_CLI_Context_t *pCLIList = CLI_List_head;
/* Using logging framework for event data content */
LOGF_LOG_INFO("%s", pMsg);
while (pCLIList != DSL_NULL)
{
if (pCLIList->pEventCallback != DSL_NULL)
{
(void)pCLIList->pEventCallback(pCLIList->pCBContext, CLI_EventText);
}
pCLIList = pCLIList->next;
};
return DSL_SUCCESS;
}
DSL_Error_t DSL_CPE_CLI_Register(
DSL_CLI_Context_t **pNewCLIContext,
DSL_void_t *pCBContext,
DSL_CPE_Exit_Callback_t pExitCallback,
DSL_CLI_Event_Callback_t pEventCallback)
{
DSL_CLI_Context_t *pCLIContext;
if(*pNewCLIContext != DSL_NULL)
{
DSL_CPE_FPrintf (DSL_CPE_STDOUT, DSL_CPE_PREFIX "expecting zero context pointer "
"'*pNewCLIContext'" DSL_CPE_CRLF);
return DSL_ERROR;
}
*pNewCLIContext = malloc(sizeof(DSL_CLI_Context_t));
if(*pNewCLIContext == DSL_NULL)
{
DSL_CPE_FPrintf (DSL_CPE_STDOUT, DSL_CPE_PREFIX "no memory for '*pNewCLIContext'"
DSL_CPE_CRLF);
return DSL_ERROR;
}
pCLIContext = *pNewCLIContext;
memset(pCLIContext, 0x00, sizeof(*pCLIContext));
pCLIContext->next = DSL_NULL;
pCLIContext->pCBContext = pCBContext;
pCLIContext->pExitCallback = pExitCallback;
pCLIContext->pEventCallback = pEventCallback;
if (CLI_List_head == DSL_NULL)
CLI_List_head = pCLIContext;
else
{
DSL_CLI_Context_t *pCLI_List = CLI_List_head;
while (pCLI_List->next != DSL_NULL)
{
pCLI_List = pCLI_List->next;
}
pCLI_List->next = pCLIContext;
}
return DSL_SUCCESS;
}
#if defined(INCLUDE_DSL_API_CONSOLE_EXTRA) || defined(INCLUDE_DSL_CPE_DTI_SUPPORT)
DSL_Error_t DSL_CPE_CLI_Unregister(DSL_CLI_Context_t *pCLIContext)
{
DSL_CLI_Context_t *pCLIList=DSL_NULL;
if(pCLIContext == DSL_NULL)
{
return DSL_SUCCESS;
}
if (CLI_List_head == pCLIContext)
{
CLI_List_head = pCLIContext->next;
return DSL_SUCCESS;
}
else
{
pCLIList = CLI_List_head;
while (pCLIList != DSL_NULL)
{
if (pCLIList->next == pCLIContext)
{
/* entry found, remove from list */
pCLIList->next = pCLIContext->next;
return DSL_SUCCESS;
}
pCLIList = pCLIList->next;
}
}
return DSL_ERROR;
}
#endif /* #if defined(INCLUDE_DSL_API_CONSOLE_EXTRA) || defined(INCLUDE_DSL_CPE_DTI_SUPPORT)*/
/**
Cleanup the CLI (and stop the DSL CPE API)
*/
static DSL_void_t DSL_CPE_CLI_Cleanup(void)
{
DSL_CLI_Context_t *pCLIList = CLI_List_head;
DSL_CLI_Context_t *pCLI_del;
CLI_List_head = DSL_NULL;
while (pCLIList != DSL_NULL)
{
if (pCLIList->pExitCallback != DSL_NULL)
{
/*DSL_CPE_FPrintf(DSL_CPE_STDERR, "CLI: calling pExitCallback()" DSL_CPE_CRLF );*/
(void)pCLIList->pExitCallback(pCLIList->pCBContext);
}
pCLI_del = pCLIList;
pCLIList = pCLIList->next;
free(pCLI_del);
};
}
/**
Checks if the command string includes option to display help text.
\param pCommands
user command line arguments
\return
returns 0 in case of no help request or any other value if help is
requested from user.
*/
DSL_int_t DSL_CPE_CLI_CheckHelp (const DSL_char_t * pCommands)
{
if (pCommands && (strstr (pCommands, "-h") || strstr (pCommands, "--help") ||
strstr (pCommands, "/h") || strstr (pCommands, "-?")))
{
return -1;
}
return 0;
}
static DSL_Error_t DSL_CPE_CLI_PrintHelp(
const DSL_char_t *psHelp,
DSL_char_t *psCmdLong,
DSL_char_t *psCmdShort,
DSL_uint32_t nCmdMask,
DSL_CPE_CLI_PrintHelpCallback_t pFct,
DSL_CPE_File_t *out)
{
#ifndef DSL_CPE_DEBUG_DISABLE
DSL_char_t *sHelp = DSL_NULL;
if (psHelp == DSL_NULL)
{
return DSL_ERROR;
}
if (nCmdMask & DSL_CPE_MASK_DEPRECATED)
{
DSL_CPE_FPrintf (out, DSL_CLI_HELP_NOTE_FUNCTION_DEPRECATED);
}
if (pFct != NULL)
{
return pFct(psHelp, psCmdLong, psCmdShort, nCmdMask,out);
}
else
{
sHelp = (DSL_char_t*)DSL_CPE_Malloc(4096);
if (sHelp == DSL_NULL)
{
DSL_CPE_FPrintf(out, DSL_CLI_HELP_MEMORY_ERROR);
return DSL_ERROR;
}
snprintf(sHelp, 4096, psHelp, psCmdLong, psCmdShort);
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
DSL_CPE_FPrintf (out, "%s", sHelp);
DSL_CPE_Free(sHelp);
}
#else
DSL_CPE_FPrintf (out, DSL_CLI_HELP_NOT_AVAILABLE);
#endif /* DSL_CPE_DEBUG_DISABLE */
return DSL_SUCCESS;
}
/**
Checks a given command list (string) according to the included number of
parameter.
\param pCommands
specifies a pointer to a list of parameters
\param nParams
specifies the expected number of parameters that has to be included
within the command list
\return
Returns DSL_TRUE if the number of scanned parameters equals to the
given value of nParams otherwise returns DSL_FALSE
*/
DSL_boolean_t DSL_CPE_CLI_CheckParamNumber(
DSL_char_t *pCommands,
DSL_int_t nParams,
DSL_CLI_ParamCheckType_t nCheckType)
{
DSL_char_t string[256] = { 0 };
DSL_char_t seps[] = " ";
DSL_boolean_t bRet = DSL_FALSE;
DSL_char_t *token;
DSL_int_t i = 0;
cpe_control_strncpy_s(string, sizeof(string)-1, pCommands, strlen(pCommands));
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
string[sizeof(string)-1]=0;
/* Get first token */
token = strtok (string, seps);
if (token != DSL_NULL)
{
for (i = 1; ; i++)
{
/* Get next token */
token = strtok(DSL_NULL, seps);
/* Exit scanning if no further information is included */
if (token == DSL_NULL)
{
break;
}
}
}
bRet = DSL_FALSE;
switch (nCheckType)
{
case DSL_CLI_EQUALS:
if (i == nParams) bRet =DSL_TRUE;
break;
case DSL_CLI_MIN:
if (i >= nParams) bRet = DSL_TRUE;
break;
case DSL_CLI_MAX:
if (i <= nParams) bRet = DSL_TRUE;
break;
default:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX "DSL_CPE_CLI_CheckParamNumber: "
"unknown check type" DSL_CPE_CRLF);
break;
}
return bRet;
}
/**
Inform the user about the built in commands.
\param command not used
*/
DSL_int_t DSL_CPE_CLI_HelpPrint(
DSL_int_t fd,
DSL_char_t *command,
DSL_CPE_File_t *out)
{
unsigned int mask = 0;
DSL_CPE_File_t *target = out;
#ifdef WIN32
DSL_CPE_File_t *file = DSL_NULL;
#endif
if( (command != DSL_NULL) && ( strstr(command,"--help") ||
strstr(command,"-h")) )
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX "help [-h | --help"
#ifdef DSL_CPE_MASK_LONG
" | -l | --long |"
#endif
#ifdef WIN32
" | file |"
#endif
" all | device | g997 | pm | bnd | dsm"
#ifdef INCLUDE_DEPRECATED
" deprecated |"
#endif
" detailed]" DSL_CPE_CRLF );
return 0;
}
if(command)
{
#ifdef WIN32
if(strstr(command, "file") != 0)
{
file = DSL_CPE_FOpen("cli_help.txt","a+");
target = file;
}
#endif
#ifdef DSL_CPE_MASK_LONG
if((strstr(command, "-l") != 0) || (strstr(command, "--long") != 0) )
mask = DSL_CPE_MASK_LONG;
#endif
if(strstr(command, "detailed") != 0)
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX "detailed information" DSL_CPE_CRLF );
mask |= DSL_CPE_MASK_DETAILED;
}
#ifdef INCLUDE_DEPRECATED
if(strstr(command, "deprecated") != 0)
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX "deprecated functions" DSL_CPE_CRLF );
mask |= DSL_CPE_MASK_DEPRECATED;
}
#endif
if(strstr(command, "device") != 0)
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX "device related functions" DSL_CPE_CRLF );
DSL_CPE_treePrint(root_node, mask | DSL_CPE_MASK_DEVICE, target);
}
if(strstr(command, "g997") != 0)
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX "G997 related functions" DSL_CPE_CRLF );
DSL_CPE_treePrint(root_node, mask | DSL_CPE_MASK_G997, target);
}
if(strstr(command, "pm") != 0)
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX "performance related functions" DSL_CPE_CRLF );
DSL_CPE_treePrint(root_node, mask | DSL_CPE_MASK_PM, target);
}
if(strstr(command, "bnd") != 0)
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX "Bonding related functions" DSL_CPE_CRLF );
DSL_CPE_treePrint(root_node, mask | DSL_CPE_MASK_BND, target);
}
if(strstr(command, "dsm") != 0)
{
DSL_CPE_FPrintf(out, DSL_CPE_PREFIX
"(D)igital (S)pectrum (M)anagement (vectoring) related functions" DSL_CPE_CRLF );
DSL_CPE_treePrint(root_node, mask | DSL_CPE_MASK_DSM, target);
}
if(strlen(command)==0 || strstr(command, "all") != 0)
{
DSL_CPE_treePrint(root_node, mask | DSL_CPE_MASK_ALL, target);
}
}
else
{
DSL_CPE_treePrint(root_node, DSL_CPE_MASK_ALL | mask, target);
}
#ifdef WIN32
if(file != DSL_NULL)
DSL_CPE_FClose(file);
#endif
return 0;
}
/**
Execute command.
\param name Command name
\param command Command line (optional parameters)
*/
DSL_int_t DSL_CPE_CLI_CommandExecute(
DSL_int_t fd,
DSL_char_t *cmd,
DSL_char_t *arg,
DSL_CPE_File_t *out)
{
DSL_CPE_recType rec = {DSL_NULL, DSL_NULL, DSL_NULL, 0, DSL_NULL};
DSL_char_t dummy_arg[10] = "";
if(cmd == DSL_NULL)
{
DSL_CPE_CLI_HelpPrint (fd, "all", out);
return -1;
}
cmd = DSL_CPE_CLI_WhitespaceRemove(cmd);
if(arg != DSL_NULL)
{
arg = DSL_CPE_CLI_WhitespaceRemove(arg);
}
else
{
arg = dummy_arg;
}
switch(DSL_CPE_keyFind(cmd, &rec))
{
case DSL_CPE_STATUS_OK:
if(rec.func == DSL_NULL)
{
DSL_CPE_FPrintf(out, DSL_CPE_CRLF "Error: command \"%s\" without callback" DSL_CPE_CRLF ,cmd);
}
else
{
if ((rec.psHelp != DSL_NULL) && (DSL_CPE_CLI_CheckHelp(arg) != 0))
{
DSL_CPE_CLI_PrintHelp(rec.psHelp, rec.sCmdLong, rec.sCmdShort, rec.mask, rec.printFunc, out);
}
else
{
DSL_CPE_CLI_PrintHelp(rec.psHelp, rec.sCmdLong, rec.sCmdShort, rec.mask, rec.printFunc, out);
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
{
DSL_CPE_CLI_Cleanup();
/* main() will execute exit */
return 1;
}
}
}
break;
default:
#if 0 /* #ifdef LINUX */
/* FIXME: This leads into a crash on the RefBoard */
{
DSL_int_t k=0;
DSL_char_t *argv[DSL_MAX_ARGS];
DSL_char_t *ptr;
DSL_char_t file_name[64], *brkt;
DSL_int_t status;
/* try to call external utility */
argv[k++] = cmd;
ptr = strtok_r(arg, " ", &brkt);
while(ptr && (k<(DSL_MAX_ARGS-1)))
{
argv[k++] = ptr;
ptr = strtok_r(DSL_NULL, " ", &brkt);
}
argv[k] = DSL_NULL;
switch (fork())
{
case -1:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX "fork failed" DSL_CPE_CRLF );
break;
case 0:
snprintf(file_name, sizeof(file_name),"/bin/%s",cmd);
snprintf(file_name, sizeof(file_name),"/usr/bin/%s",cmd);
snprintf(file_name, sizeof(file_name),"/opt/ifx/%s",cmd);
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
execv(file_name,argv);
DSL_CPE_FPrintf(DSL_CPE_STDERR, "%s: command not found" DSL_CPE_CRLF,cmd);
exit(1);
break;
default:
wait(&status);
break;
}
}
return 0;
#else
DSL_CPE_FPrintf(out, "%s: command not found" DSL_CPE_CRLF, cmd);
DSL_CPE_CLI_HelpPrint (fd, "all", out);
return -1;
#endif /* LINUX */
}
return 0;
}
/**
Add a command to the static list.
\param name Command name
\param help Help string
\param func Command entry point
\return
DSL_ERROR no more space left in command table
DSL_SUCCESS command added to the command table
*/
DSL_Error_t DSL_CPE_CLI_CommandAdd(
DSL_char_t *name,
DSL_char_t *long_name,
DSL_int_t (*func)(DSL_int_t, DSL_char_t*, DSL_CPE_File_t*),
const DSL_char_t *psHelp,
DSL_uint32_t nCmdSortMask,
DSL_CPE_CLI_PrintHelpCallback_t printCallback)
{
DSL_CPE_recType rec;
DSL_char_t buf[64+1]="";
DSL_uint8_t i=0,k=0;
rec.mask = 0;
if(name == DSL_NULL)
{
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " name pointer is invalid" DSL_CPE_CRLF );
return DSL_ERROR;
}
if(long_name == DSL_NULL)
{
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " long_name pointer is invalid" DSL_CPE_CRLF );
return DSL_ERROR;
}
for(i = 0; (i < strlen(long_name)) && (k < sizeof(buf)/sizeof(buf[0]) - 1); i++)
{
if(long_name[i] >= 'A' && long_name[i] <= 'Z')
{
buf[k++] = 'a' + (long_name[i] - 'A');
}
else if(long_name[i] >= '0' && long_name[i] <= '9')
{
buf[k++] = long_name[i];
}
}
buf[k] = 0;
if(strcmp(buf, name) != 0)
{
if( ! ((strcmp(long_name,"Help") == 0) || (strcmp(long_name,"Quit") == 0)) )
{
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " CLI short name mismatch %s / %s / %s " DSL_CPE_CRLF, name, buf, long_name );
}
}
rec.func = func;
if(strstr(name, "g997") != 0)
{
rec.mask |= DSL_CPE_MASK_G997;
}
else if(long_name[0] == 'P' && long_name[1] == 'M')
{
rec.mask |= DSL_CPE_MASK_PM;
}
else if(strstr(name, "bnd") != 0)
{
rec.mask |= DSL_CPE_MASK_BND;
}
else if(strstr(name, "dsm") != 0)
{
rec.mask |= DSL_CPE_MASK_DSM;
}
else
{
rec.mask |= DSL_CPE_MASK_DEVICE;
}
rec.mask |= nCmdSortMask;
rec.sCmdShort = malloc(strlen(name)+1);
if(rec.sCmdShort)
{
cpe_control_strncpy_s(rec.sCmdShort, strlen(name)+1, name, strlen(name));
rec.sCmdShort[strlen(name)] = 0;
}
rec.sCmdLong = malloc(strlen(long_name)+1);
if(rec.sCmdLong)
{
cpe_control_strncpy_s(rec.sCmdLong, strlen(long_name)+1, long_name, strlen(long_name));
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
rec.sCmdLong[strlen(long_name)] = 0;
}
if(rec.sCmdShort == DSL_NULL || rec.sCmdLong == DSL_NULL)
{
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
}
rec.psHelp = psHelp;
rec.printFunc = printCallback;
switch(DSL_CPE_keyInsert(rec.sCmdShort, &rec))
{
case DSL_CPE_STATUS_KEY_INVALID:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " invalid key %s for %s" DSL_CPE_CRLF , rec.sCmdShort, rec.sCmdLong);
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
case DSL_CPE_STATUS_DUPLICATE_KEY:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " duplicate key %s for %s" DSL_CPE_CRLF , rec.sCmdShort, rec.sCmdLong);
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
case DSL_CPE_STATUS_MEM_EXHAUSTED:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " memory error" DSL_CPE_CRLF );
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
case DSL_CPE_STATUS_OK:
break;
default:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " insert key aborted" DSL_CPE_CRLF );
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
}
#ifdef DSL_CPE_MASK_LONG
rec.mask |= DSL_CPE_MASK_LONG;
switch(DSL_CPE_keyInsert(rec.sCmdLong, &rec))
{
case DSL_CPE_STATUS_KEY_INVALID:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " invalid key %s for %s" DSL_CPE_CRLF , rec.sCmdShort, rec.sCmdLong);
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
case DSL_CPE_STATUS_DUPLICATE_KEY:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " duplicate key %s for %s" DSL_CPE_CRLF , rec.sCmdShort, rec.sCmdLong);
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
case DSL_CPE_STATUS_MEM_EXHAUSTED:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " memory error" DSL_CPE_CRLF );
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
case DSL_CPE_STATUS_OK:
break;
default:
DSL_CPE_FPrintf(DSL_CPE_STDERR, DSL_CPE_PREFIX " insert key aborted" DSL_CPE_CRLF );
free(rec.sCmdShort);
free(rec.sCmdLong);
return DSL_ERROR;
}
#endif
return DSL_SUCCESS;
}
/**
Clean command list.
\return
DSL_SUCCESS successfull operation
*/
DSL_Error_t DSL_CPE_CLI_CommandClear(DSL_void_t)
{
DSL_CPE_nodeType *tmp_root = root_node;
/*root_node = DSL_NULL;*/
DSL_CPE_treeDelete(tmp_root);
return DSL_SUCCESS;
}
#ifdef INCLUDE_DSL_RESOURCE_STATISTICS
DSL_Error_t DSL_CPE_CLI_ResourceUsageGet(
DSL_CLI_ResourceUsageStatisticsData_t *pData)
{
DSL_Error_t ret = DSL_SUCCESS;
unsigned int ResStatic = 0, ResDynamic = 0;
if (pData == DSL_NULL)
{
return DSL_ERROR;
}
pData->staticMemUsage = 0;
pData->dynamicMemUsage = 0;
pData->staticMemUsage += sizeof(CLI_EventText) ;
DSL_CPE_treeResourceUsageGet(root_node, &ResStatic, &ResDynamic);
pData->staticMemUsage += ResStatic;
pData->dynamicMemUsage += ResDynamic;
return ret;
}
static void DSL_CPE_treeResourceUsageGet(
DSL_CPE_nodeType *node,
unsigned int *pResStatic,
unsigned int *pResDynamic)
{
if(node == DSL_NULL)
return;
DSL_CPE_treeResourceUsageGet(node->left, pResStatic, pResDynamic);
if (node->rec.psHelp)
{
*pResStatic += strlen(node->rec.psHelp);
}
*pResDynamic += sizeof(DSL_CPE_nodeType);
if (node->key)
{
*pResDynamic += strlen(node->key);
}
if (node->rec.sCmdShort)
{
*pResDynamic += strlen(node->rec.sCmdShort);
}
if (node->rec.sCmdLong)
{
*pResDynamic += strlen(node->rec.sCmdLong);
}
DSL_CPE_treeResourceUsageGet(node->right, pResStatic, pResDynamic);
return;
}
#endif /* INCLUDE_DSL_RESOURCE_STATISTICS*/
/**
allocate node for data and insert in tree
*/
static DSL_CPE_statusEnum DSL_CPE_keyInsert(DSL_CPE_keyType key, DSL_CPE_recType *rec)
{
DSL_CPE_nodeType *x, *current, *parent;
if(key == DSL_NULL)
return DSL_CPE_STATUS_KEY_INVALID;
/* find future parent */
current = root_node;
parent = 0;
while (current) {
if (compEQ(key, current->key))
return DSL_CPE_STATUS_DUPLICATE_KEY;
parent = current;
current = compLT(key, current->key) ?
current->left : current->right;
}
/* setup new node */
if ((x = malloc(sizeof(*x))) == 0) {
return DSL_CPE_STATUS_MEM_EXHAUSTED;
}
x->parent = parent;
x->left = DSL_NULL;
x->right = DSL_NULL;
x->key = key;
cpe_control_memcpy_s((void *)&x->rec, sizeof(DSL_CPE_recType), (void *)rec, sizeof(DSL_CPE_recType));