1 /*
2 * osapi.c
3 *
4 * Copyright 2001-2009 Texas Instruments, Inc. - http://www.ti.com/
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /****************************************************************************
20 *
21 * MODULE: osapi.c
22 *
23 * PURPOSE:
24 *
25 * DESCRIPTION:
26 * ============
27 * OS Memory API for user mode application (CUDK)
28 *
29 ****************************************************************************/
30
31 /* includes */
32 /************/
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include "cu_os.h"
42 #include "cu_osapi.h"
43
44 /* defines */
45 /***********/
46 #define MAX_HOST_MESSAGE_SIZE 512
47
48 S32 ipc_pipe[2];
49
50 extern S32 user_main (S32 argc, PPS8 argv);
51
52 /**
53 * \fn main
54 * \brief Main entry point to a user-mode program
55 *
56 * This is the main() function for a user mode program, or the entry point
57 * called by the OS, This calls an OS-abstracted main function
58 *
59 * \param argc - command line argument count
60 * \param argv - command line arguments
61 * \return 0 on success, any other value indicates error
62 * \sa user_main
63 */
main(int argc,char ** argv)64 int main (int argc, char** argv)
65 {
66 return user_main (argc, (PPS8)argv);
67 }
68
69 /****************************************************************************************
70 * os_error_printf()
71 ****************************************************************************************
72 DESCRIPTION: This function prints a debug message
73
74 ARGUMENTS: OsContext - our adapter context.
75 arg_list - string to output with arguments
76
77 RETURN: None
78 *****************************************************************************************/
os_error_printf(S32 debug_level,const PS8 arg_list,...)79 VOID os_error_printf(S32 debug_level, const PS8 arg_list ,...)
80 {
81 static int g_debug_level = CU_MSG_ERROR; /* TODO ronen: create debug logic for CLI */
82 S8 msg[MAX_HOST_MESSAGE_SIZE];
83 va_list ap;
84 #ifdef OS_CLI_LOG_TO_FILE
85 char file_name[30]="/cli.log";
86 FILE *ftmp;
87 #endif
88
89 if (debug_level < g_debug_level)
90 return;
91
92 /* Format the message */
93 va_start(ap, arg_list);
94 vsprintf((char *)msg, (char *)arg_list, ap);
95 va_end(ap);
96
97 /* print the message */
98 fprintf(stderr, (char *)msg);
99
100 #ifdef OS_CLI_LOG_TO_FILE
101 ftmp = fopen(file_name, "a");
102 if (ftmp != NULL) {
103 fprintf(ftmp,(char *)msg);
104 fclose(ftmp);
105 }
106 #endif
107 }
108
109 /****************************************************************************************
110 * os_strcpy()
111 ****************************************************************************************
112 DESCRIPTION: wrapper to the OS strcpy fucntion
113
114 ARGUMENTS:
115
116 RETURN:
117 *****************************************************************************************/
os_strcpy(PS8 dest,const PS8 src)118 inline PS8 os_strcpy(PS8 dest, const PS8 src)
119 {
120 return (PS8)strcpy((char*)dest, (char*)src);
121 }
122
123 /****************************************************************************************
124 * os_strncpy()
125 ****************************************************************************************
126 DESCRIPTION: wrapper to the OS strncpy fucntion
127
128 ARGUMENTS:
129
130 RETURN:
131 *****************************************************************************************/
os_strncpy(PS8 dest,const PS8 src,S32 n)132 PS8 os_strncpy(PS8 dest, const PS8 src, S32 n)
133 {
134 return (PS8)strncpy((char*)dest, (char*)src, n);
135 }
136
137 /****************************************************************************************
138 * os_sprintf()
139 ****************************************************************************************
140 DESCRIPTION: wrapper to the OS sprintf fucntion
141
142 ARGUMENTS:
143
144 RETURN:
145 *****************************************************************************************/
os_sprintf(PS8 str,const PS8 arg_list,...)146 S32 os_sprintf(PS8 str, const PS8 arg_list, ...)
147 {
148 va_list ap;
149 S8 msg[MAX_HOST_MESSAGE_SIZE];
150
151 va_start(ap, arg_list);
152 vsprintf((char*)msg, (char*)arg_list, ap);
153 va_end(ap);
154
155 return sprintf((char*)str, (char*)msg);
156 }
157
158 /****************************************************************************************
159 * os_Printf()
160 ****************************************************************************************
161 DESCRIPTION: wrapper to the OS printf fucntion
162
163 ARGUMENTS:
164
165 RETURN:
166 *****************************************************************************************/
os_Printf(const PS8 buffer)167 S32 os_Printf(const PS8 buffer)
168 {
169 return printf((char*)buffer);
170 }
171
172
173 /****************************************************************************************
174 * os_strcat()
175 ****************************************************************************************
176 DESCRIPTION: wrapper to the OS strcat fucntion
177
178 ARGUMENTS:
179
180 RETURN:
181 *****************************************************************************************/
os_strcat(PS8 dest,const PS8 src)182 PS8 os_strcat(PS8 dest, const PS8 src)
183 {
184 return (PS8)strcat((char*)dest, (char*)src);
185 }
186
187 /****************************************************************************************
188 * os_strlen()
189 ****************************************************************************************
190 DESCRIPTION: wrapper to the OS strlen fucntion
191
192 ARGUMENTS:
193
194 RETURN:
195 *****************************************************************************************/
os_strlen(const PS8 s)196 U32 os_strlen(const PS8 s)
197 {
198 return strlen((char*)s);
199 }
200
201 /****************************************************************************************
202 * os_memoryCAlloc()
203 ****************************************************************************************
204 DESCRIPTION: Allocates an array in memory with elements initialized to 0.
205
206 ARGUMENTS: OsContext - our adapter context.
207 Number - Number of elements
208 Size - Length in bytes of each element
209
210 RETURN: Pointer to the allocated memory.
211 *****************************************************************************************/
os_MemoryCAlloc(U32 Number,U32 Size)212 PVOID os_MemoryCAlloc(U32 Number, U32 Size)
213 {
214 return calloc(Number, Size);
215 }
216
217 /****************************************************************************************
218 * os_memoryAlloc()
219 ****************************************************************************************
220 DESCRIPTION: Allocates resident (nonpaged) system-space memory.
221
222 ARGUMENTS: OsContext - our adapter context.
223 Size - Specifies the size, in bytes, to be allocated.
224
225 RETURN: Pointer to the allocated memory.
226 *****************************************************************************************/
os_MemoryAlloc(U32 Size)227 PVOID os_MemoryAlloc(U32 Size)
228 {
229 return malloc(Size);
230 }
231
232 /****************************************************************************************
233 * os_memoryFree()
234 ****************************************************************************************
235 DESCRIPTION: This function releases a block of memory previously allocated with the
236 os_memoryAlloc function.
237
238
239 ARGUMENTS: OsContext - our adapter context.
240 pMemPtr - Pointer to the base virtual address of the allocated memory.
241 This address was returned by the os_memoryAlloc function.
242 Size - Redundant, needed only for kernel mode.
243
244 RETURN: None
245 *****************************************************************************************/
os_MemoryFree(PVOID pMemPtr)246 VOID os_MemoryFree(PVOID pMemPtr)
247 {
248 free(pMemPtr);
249 }
250
251 /****************************************************************************************
252 * os_memset()
253 ****************************************************************************************
254 DESCRIPTION: wrapper to the OS memset fucntion
255
256 ARGUMENTS:
257
258 RETURN:
259 *****************************************************************************************/
os_memset(PVOID s,U8 c,U32 n)260 PVOID os_memset(PVOID s, U8 c, U32 n)
261 {
262 return memset(s, c, n);
263 }
264
265 /****************************************************************************************
266 * os_memcpy()
267 ****************************************************************************************
268 DESCRIPTION: wrapper to the OS memcpy fucntion
269
270 ARGUMENTS:
271
272 RETURN:
273 *****************************************************************************************/
os_memcpy(PVOID dest,const PVOID src,U32 n)274 PVOID os_memcpy(PVOID dest, const PVOID src, U32 n)
275 {
276 return memcpy(dest, src, n);
277 }
278
279 /****************************************************************************************
280 * os_memcmp()
281 ****************************************************************************************
282 DESCRIPTION: wrapper to the OS memcmp fucntion
283
284 ARGUMENTS:
285
286 RETURN:
287 *****************************************************************************************/
os_memcmp(const PVOID s1,const PVOID s2,S32 n)288 S32 os_memcmp(const PVOID s1, const PVOID s2, S32 n)
289 {
290 return memcmp(s1, s2, n);
291 }
292
293 /************************************************************************
294 * os_strcmp *
295 ************************************************************************
296 DESCRIPTION: wrapper to the OS strcmp fucntion
297
298 CONTEXT:
299 ************************************************************************/
os_strcmp(const PS8 s1,const PS8 s2)300 S32 os_strcmp(const PS8 s1, const PS8 s2)
301 {
302 return strcmp((char*)s1,(char*)s2);
303 }
304
305
306 /************************************************************************
307 * os_strncmp *
308 ************************************************************************
309 DESCRIPTION: wrapper to the OS strcmp fucntion
310
311 CONTEXT:
312 ************************************************************************/
os_strncmp(const PS8 s1,const PS8 s2,U32 n)313 S32 os_strncmp(const PS8 s1, const PS8 s2, U32 n)
314 {
315 return strncmp((char*)s1,(char*)s2,n);
316 }
317
318 /************************************************************************
319 * os_sscanf *
320 ************************************************************************
321 DESCRIPTION: wrapper to the OS sscanf fucntion
322
323 CONTEXT:
324 ************************************************************************/
os_sscanf(const PS8 str,const PS8 arg_list,...)325 S32 os_sscanf(const PS8 str, const PS8 arg_list, ...)
326 {
327 va_list ap;
328 S8 msg[MAX_HOST_MESSAGE_SIZE];
329
330 va_start(ap, arg_list);
331 vsprintf((char*)msg, (char*)arg_list, ap);
332 va_end(ap);
333
334 return sscanf((char*)str, (char*)msg);
335 }
336
337 /************************************************************************
338 * os_strchr *
339 ************************************************************************
340 DESCRIPTION: wrapper to the OS strchr fucntion
341
342 CONTEXT:
343 ************************************************************************/
os_strchr(const PS8 s,S32 c)344 PS8 os_strchr(const PS8 s, S32 c)
345 {
346 return (PS8)strchr((char*)s,c);
347 }
348
349 /************************************************************************
350 * os_strtol *
351 ************************************************************************
352 DESCRIPTION: wrapper to the OS strtol fucntion
353
354 CONTEXT:
355 ************************************************************************/
os_strtol(const PS8 nptr,PPS8 endptr,S32 base)356 S32 os_strtol(const PS8 nptr, PPS8 endptr, S32 base)
357 {
358 return strtol((char*)nptr, (char**)endptr, base);
359 }
360
361 /************************************************************************
362 * os_strtoul *
363 ************************************************************************
364 DESCRIPTION: wrapper to the OS strtoul fucntion
365
366 CONTEXT:
367 ************************************************************************/
os_strtoul(const PS8 nptr,PPS8 endptr,S32 base)368 U32 os_strtoul(const PS8 nptr, PPS8 endptr, S32 base)
369 {
370 return strtoul((char*)nptr, (char**)endptr, base);
371 }
372
373 /************************************************************************
374 * os_tolower *
375 ************************************************************************
376 DESCRIPTION: wrapper to the OS tolower fucntion
377
378 CONTEXT:
379 ************************************************************************/
os_tolower(S32 c)380 S32 os_tolower(S32 c)
381 {
382 return tolower(c);
383 }
384
385 /************************************************************************
386 * os_tolower *
387 ************************************************************************
388 DESCRIPTION: wrapper to the OS tolower fucntion
389
390 CONTEXT:
391 ************************************************************************/
os_isupper(S32 c)392 S32 os_isupper(S32 c)
393 {
394 return isupper(c);
395 }
396
397 /************************************************************************
398 * os_tolower *
399 ************************************************************************
400 DESCRIPTION: wrapper to the OS tolower fucntion
401
402 CONTEXT:
403 ************************************************************************/
os_toupper(S32 c)404 S32 os_toupper(S32 c)
405 {
406 return toupper(c);
407
408 }
409
410 /************************************************************************
411 * os_atoi *
412 ************************************************************************
413 DESCRIPTION: wrapper to the OS atoi fucntion
414
415 CONTEXT:
416 ************************************************************************/
os_atoi(const PS8 str)417 S32 os_atoi(const PS8 str)
418 {
419 return (S32)atoi(str);
420 }
421
422 /************************************************************************
423 * os_fopen *
424 ************************************************************************
425 DESCRIPTION: wrapper to the OS fopen fucntion
426
427 CONTEXT:
428 ************************************************************************/
os_fopen(const PS8 path,os_fopen_mode_e mode)429 PVOID os_fopen(const PS8 path, os_fopen_mode_e mode)
430 {
431 switch(mode)
432 {
433 case OS_FOPEN_READ:
434 return fopen((char*)path, "r");
435 case OS_FOPEN_READ_BINARY:
436 return fopen((char*)path, "rb");
437 case OS_FOPEN_WRITE:
438 return fopen(path, "w");
439 case OS_FOPEN_WRITE_BINARY:
440 return fopen(path, "wb");
441
442
443 default:
444 return NULL;
445 }
446 }
447
448 /************************************************************************
449 * os_getFileSize *
450 ************************************************************************
451 DESCRIPTION: wrapper to the OS fopen fucntion
452
453 CONTEXT:
454 ************************************************************************/
os_getFileSize(PVOID file)455 S32 os_getFileSize (PVOID file)
456 {
457 S32 size;
458
459 if (fseek(file, 0, SEEK_END))
460 {
461 os_error_printf (CU_MSG_ERROR, (PS8)"Cannot seek file to end\n");
462 return -1;
463 }
464 size = ftell(file);
465 rewind(file);
466 return size;
467 }
468
469 /************************************************************************
470 * os_fgets *
471 ************************************************************************
472 DESCRIPTION: wrapper to the OS fgets fucntion
473
474 CONTEXT:
475 ************************************************************************/
os_fgets(PS8 s,S32 size,PVOID stream)476 inline PS8 os_fgets(PS8 s, S32 size, PVOID stream)
477 {
478 return (PS8)fgets((char*)s, size, stream);
479 }
480
481 /************************************************************************
482 * os_fread *
483 ************************************************************************
484 DESCRIPTION: wrapper to the OS fread fucntion
485
486 CONTEXT:
487 ************************************************************************/
os_fread(PVOID ptr,S32 size,S32 nmemb,PVOID stream)488 inline S32 os_fread (PVOID ptr, S32 size, S32 nmemb, PVOID stream)
489 {
490 return fread (ptr, size, nmemb, stream);
491 }
492
493 /************************************************************************
494 * os_fwrite *
495 ************************************************************************
496 DESCRIPTION: wrapper to the OS fwrite fucntion
497
498 CONTEXT:
499 ************************************************************************/
os_fwrite(PVOID ptr,S32 size,S32 nmemb,PVOID stream)500 S32 os_fwrite (PVOID ptr, S32 size, S32 nmemb, PVOID stream)
501 {
502 return fwrite (ptr, size, nmemb, stream);
503 }
504
505 /************************************************************************
506 * os_fclose *
507 ************************************************************************
508 DESCRIPTION: wrapper to the OS fclose fucntion
509
510 CONTEXT:
511 ************************************************************************/
os_fclose(PVOID stream)512 inline S32 os_fclose(PVOID stream)
513 {
514 return fclose(stream);
515 }
516
517 /************************************************************************
518 * os_getInputString *
519 ************************************************************************
520 DESCRIPTION: get the input string for the console from the appropiate inputs
521
522 CONTEXT:
523 ************************************************************************/
os_getInputString(PS8 inbuf,S32 len)524 S32 os_getInputString(PS8 inbuf, S32 len)
525 {
526 fd_set read_set;
527 S32 max_fd_index;
528 S32 result;
529 S32 pid;
530
531 /*
532 * Wait for one of two external events:
533 * -----------------------------------
534 *
535 * 1. Data received from STDIN
536 * 2. Data received from the event process
537 */
538
539 /* Prepare the read set fields */
540 FD_ZERO(&read_set);
541 FD_SET(0, &read_set);
542 FD_SET(ipc_pipe[0], &read_set);
543
544 /* Determine the maximum index of the file descriptor */
545 max_fd_index = max(0, ipc_pipe[0]) + 1;
546
547 /* Wait for event - blocking */
548 result = select(max_fd_index, &read_set, NULL, NULL, NULL);
549
550 if (result > 0)
551 {
552 if (FD_ISSET(0, &read_set))
553 {
554 /* Data received from STDIN */
555 if ( fgets( (char*)inbuf, len, stdin ) == NULL )
556 return FALSE;
557 else
558 return TRUE;
559 }
560
561 if (FD_ISSET(ipc_pipe[0], &read_set))
562 {
563 /**********************************/
564 /* Data received from TCP client */
565 /********************************/
566 result = read(ipc_pipe[0], inbuf, len);
567
568 /* Get the pid of the calling process */
569 pid = *(inbuf + 0) | (*(inbuf + 1) << 8);
570
571 /*
572 Signal the calling process (tell him that we have
573 received the command, and he can send us another one
574 */
575 if (pid != 0xFFFF)
576 {
577 kill(pid, SIGUSR1);
578 }
579
580 if ( result <= 0 )
581 return FALSE;
582 else
583 return TRUE;
584 }
585 }
586
587 /* Error */
588 os_error_printf(CU_MSG_ERROR, (PS8)"Input selection mismatch (0x%x)...\n", read_set);
589 return FALSE;
590 }
591
592 /************************************************************************
593 * os_Catch_CtrlC_Signal *
594 ************************************************************************
595 DESCRIPTION: register to catch the Ctrl+C signal
596
597 CONTEXT:
598 ************************************************************************/
os_Catch_CtrlC_Signal(PVOID SignalCB)599 VOID os_Catch_CtrlC_Signal(PVOID SignalCB)
600 {
601 if(signal(SIGINT, SignalCB) == SIG_ERR)
602 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - os_Catch_CtrlC_Signal - cant catch Ctrl+C signal\n");
603 }
604
605
os_OsSpecificCmdParams(S32 argc,PS8 * argv)606 VOID os_OsSpecificCmdParams(S32 argc, PS8* argv)
607 {
608 }
609
os_InitOsSpecificModules(VOID)610 VOID os_InitOsSpecificModules(VOID)
611 {
612 }
613
os_DeInitOsSpecificModules(VOID)614 VOID os_DeInitOsSpecificModules(VOID)
615 {
616 }
617
os_get_last_error()618 TI_SIZE_T os_get_last_error()
619 {
620 return errno;
621 }
622