1 /** @file cli_utils.c
2 *
3 * @brief This file provides Convenience functions for the CLI
4 *
5 * Copyright 2008-2024 NXP
6 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 *
9 */
10
11 /* cli_utils.c: Convenience functions for the CLI
12 *
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <wm_utils.h>
17 #include <cli_utils.h>
18 #include <ctype.h>
19 #include <wm_net.h> /* for errno */
20
string_equal(const char * s1,const char * s2)21 bool string_equal(const char *s1, const char *s2)
22 {
23 size_t len = strlen(s1);
24
25 if (len == strlen(s2) && (strncmp(s1, s2, len) == 0))
26 {
27 return true;
28 }
29 return false;
30 }
31
32 /**
33 *@brief convert char to hex integer
34 *
35 *@param chr char
36 *@return hex integer
37 **/
hexc2bin(char chr)38 unsigned char hexc2bin(char chr)
39 {
40 if (chr >= '0' && chr <= '9')
41 {
42 chr -= '0';
43 }
44 else if (chr >= 'A' && chr <= 'F')
45 {
46 chr -= ('A' - 10);
47 }
48 else if (chr >= 'a' && chr <= 'f')
49 {
50 chr -= ('a' - 10);
51 }
52 else
53 { /* Do Nothing */
54 }
55 return (uint8_t)chr;
56 }
57
58 /**
59 *@brief convert string to hex integer
60 *
61 *@param s A pointer string buffer
62 *@return hex integer
63 **/
a2hex(const char * s)64 unsigned int a2hex(const char *s)
65 {
66 uint32_t val = 0;
67
68 if (strncmp("0x", s, 2) == 0 || strncmp("0X", s, 2) == 0)
69 {
70 s += 2;
71 }
72
73 while ((*s != '\0') && (isxdigit((unsigned char)(*s)) != 0))
74 {
75 val = (val << 4) + hexc2bin(*s++);
76 }
77 return val;
78 }
79
80 /*
81 * @brief convert String to integer
82 *
83 *@param value A pointer to string
84 *@return integer
85 **/
a2hex_or_atoi(char * value)86 unsigned int a2hex_or_atoi(char *value)
87 {
88 unsigned int ret = 0;
89
90 if (value[0] == '0' && (value[1] == 'X' || value[1] == 'x'))
91 {
92 ret = a2hex(value + 2);
93 }
94 else if (isdigit((unsigned char)*value) != 0)
95 {
96 errno = 0;
97 ret = (uint32_t)strtol(value, NULL, 10);
98 if (errno != 0)
99 {
100 (void)PRINTF("Error during strtoul errno:%d", errno);
101 }
102 }
103 else
104 {
105 ret = (uint32_t)(*value);
106 }
107 return ret;
108 }
109
get_uint(const char * arg,unsigned int * dest,unsigned int len)110 bool get_uint(const char *arg, unsigned int *dest, unsigned int len)
111 {
112 unsigned int i;
113 unsigned int val = 0;
114
115 for (i = 0; i < len; i++)
116 {
117 if (arg[i] < '0' || arg[i] > '9')
118 {
119 return true;
120 }
121 val *= 10U;
122 val += (unsigned int)arg[i] - (unsigned int)'0';
123 }
124
125 *dest = val;
126 return false;
127 }
128
129 /* Parse string 'arg' formatted "AA:BB:CC:DD:EE:FF" (assuming 'sep' is ':')
130 * into a 6-byte array 'dest' such that dest = {0xAA,0xBB,0xCC,0xDD,0xEE,0xFF}
131 * set 'sep' accordingly. */
get_mac(const char * arg,char * dest,char sep)132 bool get_mac(const char *arg, char *dest, char sep)
133 {
134 unsigned char n;
135 int i, j, k = 0;
136 int l = 0;
137
138 if (strlen(arg) < 17U)
139 {
140 return true;
141 }
142
143 (void)memset(dest, 0, 6);
144
145 for (i = 0; i < 17; i += 3)
146 {
147 for (j = 0; j < 2; j++)
148 {
149 if (arg[i + j] >= '0' && arg[i + j] <= '9')
150 {
151 n = (unsigned char)(arg[i + j]) - (unsigned char)'0';
152 }
153 else if (arg[i + j] >= 'A' && arg[i + j] <= 'F')
154 {
155 n = (unsigned char)(arg[i + j]) - (unsigned char)'A' + (unsigned char)10;
156 }
157 else if (arg[i + j] >= 'a' && arg[i + j] <= 'f')
158 {
159 n = (unsigned char)(arg[i + j]) - (unsigned char)'a' + (unsigned char)10;
160 }
161 else
162 {
163 return true;
164 }
165
166 l = 1 - j;
167
168 n <<= (unsigned int)4 * (unsigned int)l;
169 dest[k] += n;
170 }
171 if (i < 15 && arg[i + 2] != sep)
172 {
173 return true;
174 }
175 k++;
176 }
177
178 return false;
179 }
180
181 #if defined(RW610) && (CONFIG_ANT_DETECT)
get_channel_list(const char * arg,uint8_t * num_channels,uint8_t * chan_number,char sep)182 bool get_channel_list(const char *arg, uint8_t *num_channels, uint8_t *chan_number, char sep)
183 {
184 unsigned int len = 0;
185 unsigned int i;
186 uint8_t count = 0;
187 uint8_t val = 0;
188
189 len = strlen(arg);
190
191 if (len == 0U)
192 {
193 (void)PRINTF("Error: len == 0\r\n");
194 return true;
195 }
196
197 for (i = 0; i < len; i++)
198 {
199 if (arg[i] == sep)
200 {
201 chan_number[count] = val;
202 count++;
203 val = 0;
204 continue;
205 }
206
207 if (arg[i] < '0' || arg[i] > '9')
208 {
209 return true;
210 }
211 val *= 10U;
212 val += (uint8_t)arg[i] - (uint8_t)'0';
213
214 if (i == len - 1)
215 {
216 chan_number[count] = val;
217 }
218 }
219
220 *num_channels = count + 1;
221 return false;
222 }
223 #endif
224
225 /* Non-reentrant getopt implementation */
226 int cli_optind = 0;
227 char *cli_optarg = NULL;
cli_getopt(int argc,char ** argv,const char * fmt)228 int cli_getopt(int argc, char **argv, const char *fmt)
229 {
230 char *opt, *c;
231
232 if (cli_optind == argc)
233 {
234 return -1;
235 }
236 cli_optarg = NULL;
237 opt = argv[cli_optind];
238 if (opt[0] != '-')
239 {
240 return -1;
241 }
242 if (opt[0] == '\0' || opt[1] == '\0')
243 {
244 return (int)'?';
245 }
246 cli_optind++;
247 c = strchr(fmt, (int)opt[1]);
248 if (c == NULL)
249 {
250 return (int)opt[1];
251 }
252 if (c[1] == ':')
253 {
254 if (cli_optind < argc)
255 {
256 cli_optarg = argv[cli_optind++];
257 }
258 }
259 return (int)c[0];
260 }
261
262 /* allocate a copy of a string */
string_dup(const char * s)263 char *string_dup(const char *s)
264 {
265 char *snew = (char *)OSA_MemoryAllocate(strlen(s) + 1);
266 if (snew)
267 (void)strcpy(snew, s);
268 return snew;
269 }
270