1/*****************************************************************************
2 **
3 **  Name:           app_utils.c
4 **
5 **  Description:    Bluetooth utils functions
6 **
7 **  Copyright (c) 2009-2012, Broadcom Corp., All Rights Reserved.
8 **  Broadcom Bluetooth Core. Proprietary and confidential.
9 **
10 *****************************************************************************/
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16#include <stdarg.h>
17#include <time.h>
18#include <sys/time.h>
19#include <sys/stat.h>
20
21#include "nsa_api.h"
22
23//#include "app_xml_param.h"
24#include "app_nsa_utils.h"
25
26/*
27 * Definitions
28 */
29/* Terminal Attribute definitions */
30#define RESET       0
31#define BRIGHT      1
32#define DIM         2
33#define UNDERLINE   3
34#define BLINK       4
35#define REVERSE     7
36#define HIDDEN      8
37
38/* Terminal Color definitions */
39#define BLACK       0
40#define RED         1
41#define GREEN       2
42#define YELLOW      3
43#define BLUE        4
44#define MAGENTA     5
45#define CYAN        6
46#define WHITE       7
47
48/* Length of the string containing a TimeStamp */
49#define APP_TIMESTAMP_LEN 80
50/*
51 * Local functions
52 */
53
54#ifdef APP_TRACE_COLOR
55static void app_set_trace_color(int attr, int fg, int bg);
56#endif
57#ifdef APP_TRACE_TIMESTAMP
58static char *app_get_time_stamp(char *p_buffer, int buffer_size);
59#endif
60
61#if (NSA != TRUE)
62/*******************************************************************************
63 **
64 ** Function        app_get_cod_string
65 **
66 ** Description     This function is used to get readable string from Class of device
67 **
68 ** Parameters      class_of_device: The Class of device to decode
69 **
70 ** Returns         Pointer on string containing device type
71 **
72 *******************************************************************************/
73char *app_get_cod_string(const DEV_CLASS class_of_device)
74{
75    UINT8 major;
76
77    /* Extract Major Device Class value */
78    BTM_COD_MAJOR_CLASS(major, class_of_device);
79
80    switch (major)
81    {
82    case BTM_COD_MAJOR_MISCELLANEOUS:
83        return "Misc device";
84        break;
85    case BTM_COD_MAJOR_COMPUTER:
86        return "Computer";
87        break;
88    case BTM_COD_MAJOR_PHONE:
89        return "Phone";
90        break;
91    case BTM_COD_MAJOR_LAN_ACCESS_PT:
92        return "Access Point";
93        break;
94    case BTM_COD_MAJOR_AUDIO:
95        return "Audio/Video";
96        break;
97    case BTM_COD_MAJOR_PERIPHERAL:
98        return "Peripheral";
99        break;
100    case BTM_COD_MAJOR_IMAGING:
101        return "Imaging";
102        break;
103    case BTM_COD_MAJOR_WEARABLE:
104        return "Wearable";
105        break;
106    case BTM_COD_MAJOR_TOY:
107        return "Toy";
108        break;
109    case BTM_COD_MAJOR_HEALTH:
110        return "Health";
111        break;
112    default:
113        return "Unknown device type";
114        break;
115    }
116    return NULL;
117}
118#endif
119/*******************************************************************************
120 **
121 ** Function        app_get_choice
122 **
123 ** Description     Wait for a choice from user
124 **
125 ** Parameters      The string to print before waiting for input
126 **
127 ** Returns         The number typed by the user, or -1 if the value type was
128 **                 not parsable
129 **
130 *******************************************************************************/
131int app_get_choice(const char *querystring)
132{
133    int neg, value, c, base;
134
135    base = 10;
136    neg = 1;
137    printf("%s => ", querystring);
138    value = 0;
139    do
140    {
141        c = getchar();
142        if ((c >= '0') && (c <= '9'))
143        {
144            value = (value * base) + (c - '0');
145        }
146        else if ((c >= 'a') && (c <= 'f'))
147        {
148            value = (value * base) + (c - 'a' + 10);
149        }
150        else if ((c >= 'A') && (c <= 'F'))
151        {
152            value = (value * base) + (c - 'A' + 10);
153        }
154        else if (c == '-')
155        {
156            neg *= -1;
157        }
158        else if (c == 'x')
159        {
160            base = 16;
161        }
162
163    } while ((c != EOF) && (c != '\n'));
164
165    return value * neg;
166}
167
168/*******************************************************************************
169 **
170 ** Function        app_get_string
171 **
172 ** Description     Ask the user to enter a string value
173 **
174 ** Parameters      querystring: to print before waiting for input
175 **                 str: the char buffer to fill with user input
176 **                 len: the length of the char buffer
177 **
178 ** Returns         The length of the string entered not including last NULL char
179 **                 negative value in case of error
180 **
181 *******************************************************************************/
182int app_get_string(const char *querystring, char *str, int len)
183{
184    int c, index;
185
186    if (querystring)
187    {
188        printf("%s => ", querystring);
189    }
190
191    index = 0;
192    do
193    {
194        c = getchar();
195        if (c == EOF)
196        {
197            return -1;
198        }
199        if ((c != '\n') && (index < (len - 1)))
200        {
201            str[index] = (char)c;
202            index++;
203        }
204    } while (c != '\n');
205
206    str[index] = '\0';
207    return index;
208}
209
210#ifdef APP_TRACE_TIMESTAMP
211/*******************************************************************************
212 **
213 ** Function        app_get_time_stamp
214 **
215 ** Description     This function is used to get a timestamp
216 **
217 ** Parameters:     p_buffer: buffer to write the timestamp
218 **                 buffer_size: buffer size
219 **
220 ** Returns         pointer on p_buffer
221 **
222 *******************************************************************************/
223static char *app_get_time_stamp(char *p_buffer, int buffer_size)
224{
225    char time_string[80];
226
227    /* use GKI_get_time_stamp to have the same clock than the BSA client traces */
228    GKI_get_time_stamp((INT8 *)time_string);
229
230    snprintf(p_buffer, buffer_size, "%s", time_string);
231
232    return p_buffer;
233}
234#endif
235
236/*******************************************************************************
237 **
238 ** Function        app_print_info
239 **
240 ** Description     This function is used to print an application information message
241 **
242 ** Parameters:     format: Format string
243 **                 optional parameters
244 **
245 ** Returns         void
246 **
247 *******************************************************************************/
248void app_print_info(char *format, ...)
249{
250    va_list ap;
251#ifdef APP_TRACE_TIMESTAMP
252    char time_stamp[APP_TIMESTAMP_LEN];
253#endif
254
255#ifdef APP_TRACE_COLOR
256    app_set_trace_color(RESET, BLACK, WHITE);
257#endif
258
259#ifdef APP_TRACE_TIMESTAMP
260    app_get_time_stamp(time_stamp, sizeof(time_stamp));
261    printf("INFO@%s: ", time_stamp);
262#endif
263
264    va_start(ap, format);
265    vprintf(format,ap);
266    va_end(ap);
267
268#ifdef APP_TRACE_COLOR
269    app_set_trace_color(RESET, BLACK, WHITE);
270#endif
271}
272
273/*******************************************************************************
274 **
275 ** Function        app_print_debug
276 **
277 ** Description     This function is used to print an application debug message
278 **
279 ** Parameters:     format: Format string
280 **                 optional parameters
281 **
282 ** Returns         void
283 **
284 *******************************************************************************/
285void app_print_debug(char *format, ...)
286{
287    va_list ap;
288#ifdef APP_TRACE_TIMESTAMP
289    char time_stamp[APP_TIMESTAMP_LEN];
290#endif
291
292#ifdef APP_TRACE_COLOR
293    app_set_trace_color(RESET, GREEN, WHITE);
294#endif
295
296#ifdef APP_TRACE_TIMESTAMP
297    app_get_time_stamp(time_stamp, sizeof(time_stamp));
298    printf("DEBUG@%s: ", time_stamp);
299#else
300    printf("DEBUG: ");
301#endif
302
303    va_start(ap, format);
304    vprintf(format,ap);
305    va_end(ap);
306
307#ifdef APP_TRACE_COLOR
308    app_set_trace_color(RESET, BLACK, WHITE);
309#endif
310}
311
312/*******************************************************************************
313 **
314 ** Function        app_print_error
315 **
316 ** Description     This function is used to print an application error message
317 **
318 ** Parameters:     format: Format string
319 **                 optional parameters
320 **
321 ** Returns         void
322 **
323 *******************************************************************************/
324void app_print_error(char *format, ...)
325{
326    va_list ap;
327#ifdef APP_TRACE_TIMESTAMP
328    char time_stamp[APP_TIMESTAMP_LEN];
329#endif
330
331#ifdef APP_TRACE_COLOR
332    app_set_trace_color(RESET, RED, WHITE);
333#endif
334
335#ifdef APP_TRACE_TIMESTAMP
336    app_get_time_stamp(time_stamp, sizeof(time_stamp));
337    printf("ERROR@%s: ", time_stamp);
338#else
339    printf("ERROR: ");
340#endif
341
342    va_start(ap, format);
343    vprintf(format,ap);
344    va_end(ap);
345
346#ifdef APP_TRACE_COLOR
347    app_set_trace_color(RESET, BLACK, WHITE);
348#endif
349}
350
351#ifdef APP_TRACE_COLOR
352/*******************************************************************************
353 **
354 ** Function        app_set_trace_color
355 **
356 ** Description     This function changes the text color
357 **
358 ** Parameters:     attribute: Text attribute (reset/Blink/etc)
359 **                 foreground: foreground color
360 **                 background: background color
361 **
362 ** Returns         void
363 **
364 *******************************************************************************/
365static void app_set_trace_color(int attribute, int foreground, int background)
366{
367    char command[13];
368
369    /* Command is the control command to the terminal */
370    snprintf(command, sizeof(command), "%c[%d;%d;%dm", 0x1B, attribute, foreground + 30, background + 40);
371    printf("%s", command);
372}
373#endif
374
375/*******************************************************************************
376 **
377 ** Function        app_file_size
378 **
379 ** Description     Retrieve the size of a file identified by descriptor
380 **
381 ** Parameters:     fd: File descriptor
382 **
383 ** Returns         File size if successful or negative error number
384 **
385 *******************************************************************************/
386int app_file_size(int fd)
387{
388    struct stat file_stat;
389    int rc = -1;
390
391    rc = fstat(fd, &file_stat);
392
393    if (rc >= 0)
394    {
395        /* Retrieve the size of the file */
396        rc = file_stat.st_size;
397    }
398    else
399    {
400        APP_ERROR1("could not fstat(fd=%d)", fd);
401    }
402    return rc;
403}
404