FreeCalypso > hg > fc-magnetite
comparison src/g23m-fad/app/app_cmds.c @ 174:90eb61ecd093
src/g23m-fad: initial import from TCS3.2/LoCosto
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 12 Oct 2016 05:40:46 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
173:bf64d785238a | 174:90eb61ecd093 |
---|---|
1 /* | |
2 +------------------------------------------------------------------------------ | |
3 | File: app_cmds.c | |
4 +------------------------------------------------------------------------------ | |
5 | Copyright 2004 Texas Instruments Berlin, AG | |
6 | All rights reserved. | |
7 | | |
8 | This file is confidential and a trade secret of Texas | |
9 | Instruments Berlin, AG | |
10 | The receipt of or possession of this file does not convey | |
11 | any rights to reproduce or disclose its contents or to | |
12 | manufacture, use, or sell anything it may describe, in | |
13 | whole, or in part, without the specific written consent of | |
14 | Texas Instruments Berlin, AG. | |
15 +----------------------------------------------------------------------------- | |
16 | Purpose : Example application for TCP/IP and Socket API -- command parser. | |
17 +----------------------------------------------------------------------------- | |
18 */ | |
19 | |
20 | |
21 #define APP_CMDS_C | |
22 | |
23 #ifdef FF_GPF_TCPIP | |
24 | |
25 #define ENTITY_APP | |
26 | |
27 /*==== INCLUDES =============================================================*/ | |
28 | |
29 #include <string.h> /* String functions, e. g. strncpy(). */ | |
30 #include <ctype.h> | |
31 #include <stdlib.h> | |
32 #ifndef _SIMULATION_ | |
33 #include "typedefs.h" /* Condat data types. */ | |
34 #endif /* _SIMULATION_ */ | |
35 #include "vsi.h" /* A lot of macros. */ | |
36 #ifndef _SIMULATION_ | |
37 #include "custom.h" | |
38 #include "gsm.h" /* A lot of macros. */ | |
39 //#include "prim.h" /* Definitions of used SAP and directions. */ | |
40 #include "pei.h" /* PEI interface. */ | |
41 #include "tools.h" /* Common tools. */ | |
42 #endif /* _SIMULATION_ */ | |
43 #include "prim.h" /* Definitions of used SAP and directions. */ | |
44 #include "socket_api.h" /* Socket API. */ | |
45 #include "app.h" /* Global entity definitions. */ | |
46 | |
47 | |
48 | |
49 char custom_apn[SOCK_MAX_APN_LEN+1]; | |
50 char custom_user_id[SOCK_MAX_USERID_LEN+1]; | |
51 char custom_password[SOCK_MAX_PASSWORD_LEN+1]; | |
52 | |
53 BOOL custom_apn_valid = FALSE; | |
54 /*==== Local defines =========================================================*/ | |
55 | |
56 #define MAX_TOKENS 4 /* Maximum number of tokens to search for; | |
57 * first token is the command name, last token | |
58 * the rest of the line. */ | |
59 | |
60 | |
61 | |
62 | |
63 /*==== Local data ============================================================*/ | |
64 | |
65 /* Type of command table entry. */ | |
66 typedef struct APP_CMD_ENTRY app_cmd_entry_t ; | |
67 | |
68 /* Type of some application core functions. */ | |
69 typedef void (*core_func_t)(int prov, int n, int reps) ; | |
70 | |
71 /* Type of command handler functions. */ | |
72 typedef char *(*cmd_handler_t)(app_cmd_entry_t *cmd_entry_ptr, | |
73 char *param1, | |
74 char *param2, | |
75 char *param3, | |
76 core_func_t core_func) ; | |
77 | |
78 /* Struct for command table entry. */ | |
79 struct APP_CMD_ENTRY { | |
80 char *cmd_name ; /* Command string. */ | |
81 cmd_handler_t cmd_func ; /* Command handler function. */ | |
82 core_func_t core_func ; /* Related core function (if applicable). */ | |
83 char *cmd_usage ; /* Command usage string (informative only). */ | |
84 } ; | |
85 | |
86 static char *app_cmd_data(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
87 static char *app_cmd_echo(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
88 static char *app_cmd_tcpsrv(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
89 static char *app_cmd_dnsquery(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
90 static char *app_cmd_server(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
91 static char *app_cmd_shutdown(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
92 static char *app_cmd_pstat(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
93 static char *app_cmd_help(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
94 static char *app_cmd_buffer(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
95 static char *app_cmd_port(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
96 static char *app_cmd_bearer(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
97 static char *app_cmd_xon(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
98 static char *app_cmd_xoff(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
99 static char *app_cmd_open_bearer(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
100 static char *app_cmd_close_bearer(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
101 static char *app_cmd_set_provider(app_cmd_entry_t *, char *, char *, char *, core_func_t) ; | |
102 | |
103 | |
104 /* Command handler table. */ | |
105 static app_cmd_entry_t app_cmd_table[] = { | |
106 { "tcpdl", app_cmd_data, app_start_tcpdl, "[Provider][size] [reps]" }, | |
107 { "tcpul", app_cmd_data, app_start_tcpul, "[Provider][size] [reps]" }, | |
108 { "udpdl", app_cmd_data, app_start_udpdl, "[Provider][size] [reps]" }, | |
109 { "udpul", app_cmd_data, app_start_udpul, "[Provider][size] [reps]" }, | |
110 { "tcpecho", app_cmd_echo, app_start_tcpecho, "[Provider][items] [reps]" }, | |
111 { "udpecho", app_cmd_echo, app_start_udpecho, "[Provider] [items] [reps]" }, | |
112 { "tcpsrv", app_cmd_tcpsrv, app_start_tcpsrv, "[Provider][port] [repeat]" }, | |
113 { "dnsquery", app_cmd_dnsquery, 0, "[Provider][times] [fqdn]" }, | |
114 { "server", app_cmd_server, 0, "[server-address]" }, | |
115 { "port", app_cmd_port, 0, "[portnumber]" }, | |
116 { "buffer", app_cmd_buffer, 0, "[bufsize]" }, | |
117 { "bearer", app_cmd_bearer, 0, "[gsm|gprs|best|none]" }, | |
118 { "shutdown", app_cmd_shutdown, 0, "" }, | |
119 { "stat", app_cmd_pstat, 0, "" }, | |
120 { "help", app_cmd_help, 0, "" }, | |
121 { "xon", app_cmd_xon, 0, "" }, | |
122 { "xoff", app_cmd_xoff, 0, "" }, | |
123 { "open_bearer", app_cmd_open_bearer,app_open_bearer,"[Provider]" }, | |
124 { "close_bearer", app_cmd_close_bearer,0, "[Provider]" }, | |
125 { "set_provider", app_cmd_set_provider, 0,"[apn] [user_id] [password]" }, | |
126 { 0, 0, 0, 0}, | |
127 } ; | |
128 | |
129 /*============ Support functions =============================================*/ | |
130 static U32 get_provider(char *param) | |
131 { | |
132 U32 prov = APP_PROV_DEFAULT; | |
133 if(param) | |
134 { | |
135 prov = atoi(param) ; | |
136 if( (prov EQ 0) OR (!(prov < APP_PROV_INVALID)) ) | |
137 { | |
138 TRACE_EVENT("ERROR: unkown provider - setting to default"); | |
139 prov = APP_PROV_DEFAULT; | |
140 } | |
141 } | |
142 return prov; | |
143 } | |
144 | |
145 static U32 get_item(char *param, U32 default_item, BOOL can_null) | |
146 { | |
147 U32 item = default_item; | |
148 if (param) | |
149 { | |
150 item = atoi(param); | |
151 if (!can_null AND item EQ 0) | |
152 { | |
153 TRACE_EVENT("ERROR: item is 0, setting to default"); | |
154 item = default_item; | |
155 } | |
156 } | |
157 return item; | |
158 } | |
159 | |
160 | |
161 /*==== Command handler functions =============================================*/ | |
162 | |
163 static char *app_cmd_data(app_cmd_entry_t *cmd_entry_ptr, | |
164 char *param1, char *param2, char *param3, core_func_t core_func) | |
165 { | |
166 U32 prov = get_provider(param1); | |
167 U32 size = get_item(param2, APP_DEF_DOWNLOAD_SIZE,TRUE); | |
168 U32 reps = get_item(param3, APP_DEF_DOWNLOAD_REPS,FALSE); | |
169 | |
170 TRACE_FUNCTION("app_cmd_data()") ; | |
171 core_func(prov,size, reps) ; | |
172 return 0 ; | |
173 } | |
174 | |
175 | |
176 static char *app_cmd_echo(app_cmd_entry_t *cmd_entry_ptr, | |
177 char *param1, char *param2, char *param3, core_func_t core_func) | |
178 { | |
179 U32 prov = get_provider(param1); | |
180 U32 size = get_item(param2, APP_DEF_ECHO_ITEMS,TRUE); | |
181 U32 reps = get_item(param3, APP_DEF_ECHO_REPS,FALSE); | |
182 | |
183 TRACE_FUNCTION("app_cmd_echo()") ; | |
184 | |
185 core_func(prov,size, reps) ; | |
186 return 0 ; | |
187 } | |
188 | |
189 | |
190 static char *app_cmd_tcpsrv(app_cmd_entry_t *cmd_entry_ptr, | |
191 char *param1, char *param2, char *param3, core_func_t core_func) | |
192 { | |
193 U32 prov = get_provider(param1); | |
194 U32 port = get_item(param2, APP_DEF_TCPSRV_PORT,TRUE); | |
195 U32 rep = get_item(param3, APP_DEF_TCPSRV_REPEAT,TRUE); | |
196 | |
197 TRACE_FUNCTION("app_cmd_tcpsrv()") ; | |
198 | |
199 app_start_tcpsrv(prov,port, rep) ; | |
200 return 0 ; | |
201 } | |
202 | |
203 | |
204 static char *app_cmd_dnsquery(app_cmd_entry_t *cmd_entry_ptr, | |
205 char *param1, char *param2, char *param3, core_func_t core_func) | |
206 { | |
207 U32 prov = get_provider(param1); | |
208 int times = get_item(param2, APP_DEF_DNSQUERY_TIMES,FALSE); | |
209 | |
210 app_start_dnsquery(prov,times, param3) ; | |
211 return 0 ; | |
212 } | |
213 | |
214 | |
215 static char *app_cmd_server(app_cmd_entry_t *cmd_entry_ptr, | |
216 char *param1, char *param2, char *param3, core_func_t core_func) | |
217 { | |
218 app_server(param1) ; | |
219 return 0 ; | |
220 } | |
221 | |
222 | |
223 static char *app_cmd_buffer(app_cmd_entry_t *cmd_entry_ptr, | |
224 char *param1, char *param2, char *param3, core_func_t core_func) | |
225 { | |
226 app_buffer(param1) ; | |
227 return 0 ; | |
228 } | |
229 | |
230 | |
231 static char *app_cmd_port(app_cmd_entry_t *cmd_entry_ptr, | |
232 char *param1, char *param2, char *param3, core_func_t core_func) | |
233 { | |
234 app_port(param1) ; | |
235 return 0 ; | |
236 } | |
237 | |
238 | |
239 static char *app_cmd_shutdown(app_cmd_entry_t *cmd_entry_ptr, | |
240 char *param1, char *param2, char *param3, core_func_t core_func) | |
241 { | |
242 app_shutdown() ; | |
243 return 0 ; | |
244 } | |
245 | |
246 | |
247 static char *app_cmd_bearer(app_cmd_entry_t *cmd_entry_ptr, | |
248 char *param1, char *param2, char *param3, core_func_t core_func) | |
249 { | |
250 app_bearer(param1) ; | |
251 return 0 ; | |
252 } | |
253 | |
254 | |
255 static char *app_cmd_pstat(app_cmd_entry_t *cmd_entry_ptr, | |
256 char *param1, char *param2, char *param3, core_func_t core_func) | |
257 { | |
258 app_pstat() ; | |
259 return 0 ; | |
260 } | |
261 | |
262 | |
263 static char *app_cmd_xon(app_cmd_entry_t *cmd_entry_ptr, | |
264 char *param1, char *param2, char *param3, core_func_t core_func) | |
265 { | |
266 app_switch_flow(1) ; | |
267 return 0 ; | |
268 } | |
269 | |
270 | |
271 static char *app_cmd_xoff(app_cmd_entry_t *cmd_entry_ptr, | |
272 char *param1, char *param2, char *param3, core_func_t core_func) | |
273 { | |
274 app_switch_flow(0) ; | |
275 return 0 ; | |
276 } | |
277 | |
278 | |
279 static char *app_cmd_help(app_cmd_entry_t *cmd_entry_ptr, | |
280 char *param1, char *param2, char *param3, core_func_t core_func) | |
281 { | |
282 TRACE_EVENT("Available commands:") ; | |
283 for (cmd_entry_ptr = &app_cmd_table[0]; | |
284 cmd_entry_ptr->cmd_func; | |
285 cmd_entry_ptr++) | |
286 { | |
287 TRACE_EVENT_P2(" %s %s", | |
288 cmd_entry_ptr->cmd_name, cmd_entry_ptr->cmd_usage) ; | |
289 } | |
290 return 0 ; | |
291 } | |
292 | |
293 | |
294 static char *app_cmd_open_bearer(app_cmd_entry_t *cmd_entry_ptr, char *param1, | |
295 char *param2, char *param3, core_func_t core_func) | |
296 { | |
297 U32 prov = get_provider(param1); | |
298 | |
299 TRACE_FUNCTION("app_cmd_open_bearer()") ; | |
300 core_func(prov, 0, 0); | |
301 return 0; | |
302 } | |
303 | |
304 static char *app_cmd_close_bearer(app_cmd_entry_t *cmd_entry_ptr, char *param1, | |
305 char *param2, char *param3, core_func_t core_func) | |
306 { | |
307 TRACE_FUNCTION("app_cmd_close_bearer()") ; | |
308 app_close_bearer(); | |
309 return 0; | |
310 } | |
311 | |
312 static char *app_cmd_set_provider(app_cmd_entry_t *cmd_entry_ptr, char *param1, | |
313 char *param2, char *param3, core_func_t core_func) | |
314 { | |
315 /* To indicate presence of valid Custom APN */ | |
316 custom_apn_valid = TRUE; | |
317 | |
318 /* Set the APN string sent by the user */ | |
319 strcpy(custom_apn, param1); | |
320 | |
321 /* Set used ID sent by the user */ | |
322 strcpy(custom_user_id, param2); | |
323 | |
324 /* Set used ID sent by the user */ | |
325 strcpy(custom_password, param3); | |
326 | |
327 return 0; | |
328 | |
329 } | |
330 | |
331 /*==== Local functions =======================================================*/ | |
332 | |
333 static char **app_tokenize_cmd(char *command) | |
334 { | |
335 /* Three tokens will be enough: command name, parameter 1, parameter 2. Empty | |
336 * tokens will be NULL. */ | |
337 static char *cmd_token[MAX_TOKENS] ; | |
338 char *cur ; /* Pointer to current character. */ | |
339 | |
340 int cur_tok ; /* Current token number. */ | |
341 | |
342 | |
343 cur = command ; | |
344 cur_tok = 0 ; | |
345 do | |
346 { | |
347 while (isspace(*cur)) /* FALSE also for NUl character. */ | |
348 { | |
349 cur++ ; /* Skip whitespace. */ | |
350 } | |
351 if (!*cur) /* String terminated. */ | |
352 { | |
353 cmd_token[cur_tok] = 0 ; /* No token here and stop. */ | |
354 break ; | |
355 } | |
356 cmd_token[cur_tok++] = cur ; | |
357 cmd_token[cur_tok] = 0 ; | |
358 while (*cur && !isspace(*cur)) | |
359 { | |
360 cur++ ; /* Skip non-whitespace. */ | |
361 } | |
362 if (*cur) *cur++ = 0 ; /* Zero-terminate token if not end of string. */ | |
363 } | |
364 while (cur_tok < MAX_TOKENS) ; | |
365 | |
366 return cmd_token ; | |
367 } | |
368 | |
369 #if !defined (FF_GPF_TCPIP) && !defined (CO_TCPIP_TESTAPP) | |
370 char *string_to_lower(char *s) | |
371 { | |
372 char *tmp ; | |
373 | |
374 for (tmp = s; *tmp; tmp++) | |
375 { | |
376 *tmp = tolower(*tmp) ; | |
377 } | |
378 return s ; | |
379 } | |
380 #endif /* #if !defined (FF_GPF_TCPIP) && !defined (CO_TCPIP_TESTAPP) */ | |
381 | |
382 | |
383 /*==== Exported functions ====================================================*/ | |
384 | |
385 /** Parse a command and execute it if it is valid. Return appropriate error | |
386 * message if the command is invalid or fails. | |
387 * | |
388 * @param command command line to execute | |
389 * @return an error message or NULL on success | |
390 */ | |
391 char *app_handle_command_tcpip(char *command) | |
392 { | |
393 char **tokened_cmd ; /* Tokenized command line. */ | |
394 int cmd_index ; | |
395 | |
396 TRACE_FUNCTION("app_handle_command_tcpip()") ; | |
397 | |
398 tokened_cmd = app_tokenize_cmd(command) ; | |
399 if (!tokened_cmd[0]) | |
400 { | |
401 return "ERROR: empty command line" ; | |
402 } | |
403 | |
404 string_to_lower(tokened_cmd[0]) ; /* convert to lower char */ | |
405 cmd_index = 0 ; | |
406 while (app_cmd_table[cmd_index].cmd_name) | |
407 { | |
408 if (!strcmp(tokened_cmd[0], app_cmd_table[cmd_index].cmd_name)) | |
409 { | |
410 TRACE_EVENT_P4("Call %s(%s, %s, %s)", app_cmd_table[cmd_index].cmd_name, | |
411 tokened_cmd[1] ? tokened_cmd[1] : "(null)", | |
412 tokened_cmd[2] ? tokened_cmd[2] : "(null)", | |
413 tokened_cmd[3] ? tokened_cmd[3] : "(null)") ; | |
414 return app_cmd_table[cmd_index].cmd_func(&app_cmd_table[cmd_index], | |
415 tokened_cmd[1], | |
416 tokened_cmd[2], | |
417 tokened_cmd[3], | |
418 app_cmd_table[cmd_index].core_func) ; | |
419 } | |
420 cmd_index++ ; | |
421 } | |
422 return "ERROR: command not recognized" ; | |
423 } | |
424 | |
425 /* This is onoy for the case that the macro UI_TRACE() is undefined, in which | |
426 * case the linker bemoans it as missing. */ | |
427 | |
428 | |
429 #endif /* FF_GPF_TCPIP */ | |
430 | |
431 /* EOF */ |