1 /*
2  * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _CMD_UTIL_H_
31 #define _CMD_UTIL_H_
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include "sys/param.h"
36 #include "cmd_defs.h"
37 #include "cmd_debug.h"
38 //#include "console/console.h"
39 #include "os.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /* command format: <command-name> <arg>... */
46 struct cmd_data {
47     char *name;
48     enum cmd_status (*exec)(char *);
49 };
50 
51 /* command2 format: <command-name>[ <arg>...] */
52 struct cmd2_data {
53     char *name;
54     int name_len;
55     enum cmd_status (*exec)(char *);
56 };
57 
58 enum cmd_status cmd_exec(char *cmd, const struct cmd_data *cdata, int count);
59 enum cmd_status cmd2_exec(char *cmd, const struct cmd2_data *cdata, int count);
60 
61 int cmd_parse_argv(char *cmd, char *argv[], int size);
62 
63 const char *cmd_get_status_desc(enum cmd_status status);
64 //const char *cmd_get_event_desc(enum cmd_event event);
65 
66 int cmd_write(enum cmd_code_type type, int code, const char *fmt, ...);
67 
68 #define cmd_write_respond(status, fmt, arg...) \
69     cmd_write(CMD_CODE_TYEP_STATUS, (int)status, fmt, ##arg)
70 
71 #define cmd_write_event(event, fmt, arg...) \
72     cmd_write(CMD_CODE_TYEP_EVENT, (int)event, fmt, ##arg)
73 
74 int32_t cmd_raw_mode_read(uint8_t *buf, int32_t size, uint32_t msec);
75 int32_t cmd_raw_mode_write(uint8_t *buf, int32_t size);
76 
77 #define cmd_raw_mode_enable()   console_disable();
78 #define cmd_raw_mode_disable()  console_enable();
79 
80 
81 #define cmd_malloc(l)               malloc(l)
82 #define cmd_free(p)                 free(p)
83 
84 #define cmd_memcpy(d, s, n)         memcpy(d, s, n)
85 #define cmd_memset(s, c, n)         memset(s, c, n)
86 #define cmd_memcmp(s1, s2, n)       memcmp(s1, s2, n)
87 
88 #define cmd_strlen(s)               strlen(s)
89 #define cmd_strcmp(s1, s2)          strcmp(s1, s2)
90 #define cmd_strncmp(s1, s2, n)      strncmp(s1, s2, n)
91 #define cmd_strcasecmp(s1, s2)      strcasecmp(s1, s2)
92 #define cmd_strncasecmp(s1, s2, n)  strncasecmp(s1, s2, n)
93 #define cmd_strchr(s, c)            strchr(s, c)
94 #define cmd_strrchr(s, c)           strrchr(s, c)
95 #define cmd_strstr(s1, s2)          strstr(s1, s2)
96 #define cmd_strtol(s, p, b)         strtol(s, p, b)
97 #define cmd_strdup(s)               strdup(s)
98 #define cmd_strlcpy(d, s, n)        strlcpy(d, s, n)
99 
100 #define cmd_atoi(s)                 atoi(s)
101 #define cmd_atol(s)                 atol(s)
102 
103 #define cmd_sscanf(s, f, a...)      sscanf(s, f, ##a)
104 #define cmd_sprintf(s, f, a...)     sprintf(s, f, ##a)
105 #define cmd_snprintf(s, n, f, a...) snprintf(s, n, f, ##a)
106 
107 #define cmd_nitems(a)               nitems(a)
108 
109 #define cmd_msleep(msec)            OS_MSleep(msec)
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* _CMD_UTIL_H_ */
116