blob: 5811a91326670af69f806fe1aa6c7f1ff9ef64c9 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: MediaTekProprietary
2#include "AudioParamParserPriv.h"
3#include <time.h>
4
5typedef struct StrPair {
6 char *key; /* key */
7 char *value;
8 UT_hash_handle hh; /* hash handle */
9} StrPair;
10
11StrPair *strPairCreate(const char *key, const char *value) {
12 StrPair *pair = malloc(sizeof(StrPair));
13 if (pair) {
14 pair->key = strdup(key ? key : "");
15 if (!pair->key) {
16 ERR_LOG("malloc fail!\n");
17 }
18
19 pair->value = strdup(value ? value : "");
20 if (!pair->value) {
21 ERR_LOG("malloc fail!\n");
22 }
23 } else {
24 ERR_LOG("malloc fail!\n");
25 }
26
27 return pair;
28}
29
30void strPairRelease(StrPair *pair) {
31 if (pair) {
32 free(pair->key);
33 free(pair->value);
34 free(pair);
35 }
36}
37
38EXPORT void utilDumpAudioTypeLoadingList(const char *audioTypeLoadingList[]) {
39 int index = 0;
40 INFO_LOG("===AudioTypeLoadingList===\n");
41 if (audioTypeLoadingList != NULL) {
42 for (index = 0; audioTypeLoadingList[index] != NULL; index++) {
43 INFO_LOG("[%d] %s\n", index, audioTypeLoadingList[index]);
44 }
45 } else {
46 INFO_LOG("All audio type\n");
47 }
48}
49
50EXPORT char **appGetXmlDirFromProperty() {
51#if !defined(WIN32) && !defined(SYS_IMPL)&& !defined(MTK_YOCTO_AUDIO)
52 char **xmlDirList = NULL;
53 char *xmlDir = malloc(MAX_PROP_VALUE_LEN);
54
55 property_get(PROPERTY_KEY_XML_DEF_PATH, xmlDir, "");
56 if (strcmp(xmlDir, "")) {
57 MUST_LOG("%s = %s", PROPERTY_KEY_XML_DEF_PATH, xmlDir);
58 xmlDirList = malloc(sizeof(char*) * 2);
59 xmlDirList[0] = xmlDir;
60 xmlDirList[1] = NULL;
61 } else {
62 free(xmlDir);
63 }
64
65 return xmlDirList;
66#else
67 return NULL;
68#endif
69}
70
71EXPORT int appSetAudioTypeLoadingList(const char *audioTypeLoadingList[]) {
72 if (appAudioTypeLoadingList == NULL) {
73 appAudioTypeLoadingList = audioTypeLoadingList;
74 MUST_LOG("Set audioTypeLoadingList with %p\n", appAudioTypeLoadingList);
75 utilDumpAudioTypeLoadingList(appAudioTypeLoadingList);
76 return 1;
77 } else {
78 WARN_LOG("audioTypeLoadingList already been setup. (cur = %s, new = %s)\n", appAudioTypeLoadingList[0], audioTypeLoadingList[0]);
79 return 0;
80 }
81}
82
83EXPORT const char *appGetAudioTypeLoadingList(void) {
84 return (const char*)appAudioTypeLoadingList;
85}
86
87EXPORT void appSetDebugLevel(MSG_LEVEL level) {
88 INFO_LOG("appSetDebugLevel(), level = %d\n", level);
89#ifndef FORCE_DEBUG_LEVEL
90 appDebugLevel = level;
91#endif
92}
93
94EXPORT MSG_LEVEL appGetDebugLevel() {
95 INFO_LOG("appGetDebugLevel(), level = %d\n", appDebugLevel);
96 return appDebugLevel;
97}
98
99EXPORT xmlNode *findXmlNodeByElemName(xmlNode *node, const char *elemName) {
100 xmlNode *cur_node;
101
102 if (!node) {
103 DEBUG_LOG("xmlNode is NULL\n");
104 return NULL;
105 }
106
107 if (!elemName) {
108 DEBUG_LOG("elemName is NULL\n");
109 return NULL;
110 }
111
112 for (cur_node = node; cur_node; cur_node = cur_node->next) {
113 if (cur_node->type == XML_ELEMENT_NODE && !strncmp((const char *)cur_node->name, elemName, strlen(elemName) + 1)) {
114 return cur_node;
115 }
116 }
117 return NULL;
118}
119
120EXPORT int utilIsUIAudioType(const char *audioType) {
121 char* uiStr = strstr(audioType, "UI");
122 if (uiStr == NULL || strlen(uiStr) != 2) {
123 return 0;
124 }
125
126 return 1;
127}
128
129EXPORT int utilIsAudioTypeInLoadingList(const char *audioType) {
130
131 if (appAudioTypeLoadingList == NULL) {
132 /* If appAudioTypeLoadingList not specified, all audioType are allowed */
133 DEBUG_LOG("%s audio type is allow by default!\n", audioType);
134 return 1;
135 } else {
136 int index = 0;
137 for (index = 0; appAudioTypeLoadingList[index] != NULL; index++) {
138 if (!strcmp(audioType, appAudioTypeLoadingList[index])) {
139 DEBUG_LOG("%s audio type is in allow list!\n", audioType);
140 return 1;
141 }
142 }
143 DEBUG_LOG("%s audio type is not in allow list!\n", audioType);
144
145 return 0;
146 }
147}
148
149EXPORT xmlChar *xmlNodeGetProp(xmlNode *node, const char *prop) {
150 if (!node) {
151 ERR_LOG("xmlNode is NULL\n");
152 return NULL;
153 }
154
155 if (!prop) {
156 ERR_LOG("prop is NULL\n");
157 return NULL;
158 }
159
160 return xmlGetProp(node, (const xmlChar *)prop);
161}
162
163EXPORT xmlChar *xmlNodeGetWording(xmlNode *node) {
164 xmlChar *wording = xmlNodeGetProp(node, ATTRI_WORDING);
165 if (wording == NULL) {
166 wording = xmlNodeGetProp(node, ATTRI_NAME);
167 }
168 return wording;
169}
170
171void print_element_names(xmlNode *a_node) {
172 xmlNode *cur_node = NULL;
173
174 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
175 if (cur_node->type == XML_ELEMENT_NODE) {
176 printf("node type: Element, name: %s\n", cur_node->name);
177 }
178
179 print_element_names(cur_node->children);
180 }
181}
182
183void appDumpXmlDoc(xmlDoc *doc) {
184 /*Get the root element node */
185 xmlNode *root_element = xmlDocGetRootElement(doc);
186
187 print_element_names(root_element);
188}
189
190#ifndef WIN32
191EXPORT void signalHandler(int sig, siginfo_t *info, void *ucontext) {
192 INFO_LOG("Got thread notify signal. sig = %d, info = %p, ucontext = %p\n", sig, info, ucontext);
193}
194#endif
195
196EXPORT APP_STATUS utilConvDataStringToNative(DATA_TYPE dataType, const char *str, void **data, size_t *arraySize) {
197 errno = 0;
198 switch (dataType) {
199 case TYPE_STR:
200 *data = strdup(str);
201 *arraySize = 0;
202 break;
203 case TYPE_INT: {
204 int *val = malloc(sizeof(int));
205 if (!val) {
206 ERR_LOG("malloc fail!\n");
207 return APP_ERROR;
208 }
209
210 *val = strtol(str, NULL, 0);
211 if (errno == ERANGE) {
212 ERR_LOG("Cannot convert \"%s\" to int!\n", str);
213 free(val);
214 return APP_ERROR;
215 }
216
217 *data = val;
218 *arraySize = 0;
219 break;
220 }
221 case TYPE_UINT: {
222 unsigned int *val = malloc(sizeof(unsigned int));
223 if (!val) {
224 ERR_LOG("malloc fail!\n");
225 return APP_ERROR;
226 }
227
228 *val = strtoul(str, NULL, 0);
229 if (errno == ERANGE) {
230 ERR_LOG("Cannot convert \"%s\" to uint!\n", str);
231 free(val);
232 return APP_ERROR;
233 }
234
235 *data = val;
236 *arraySize = 0;
237 break;
238 }
239 case TYPE_FLOAT: {
240 float *val = malloc(sizeof(float));
241 *val = (float)strtod(str, NULL);
242 if (errno == ERANGE) {
243 ERR_LOG("Cannot convert \"%s\" to float!\n", str);
244 free(val);
245 return APP_ERROR;
246 }
247
248 *data = val;
249 *arraySize = 0;
250 break;
251 }
252 case TYPE_BYTE_ARRAY: {
253 char *elemData;
254 unsigned int convVal;
255 int index = 0;
256 APP_STATUS result = APP_NO_ERROR;
257
258 int size = paramGetArraySizeFromString(str);
259 char *val = malloc(sizeof(char) * size);
260 if (!val) {
261 ERR_LOG("malloc fail!\n");
262 return APP_ERROR;
263 }
264
265 if (size == 1) {
266 /* Convert str */
267 convVal = strtol(str, NULL, 0);
268 if (errno == ERANGE) {
269 ERR_LOG("Cannot convert \"%s\" to byte array!\n", str);
270 result = APP_ERROR;
271 } else {
272 val[0] = (char)convVal & 0xFF;
273 }
274 } else {
275 char *tmpStr = strdup(str);
276 char *restOfStr = NULL;
277 elemData = utilStrtok(tmpStr, ARRAY_SEPERATOR, &restOfStr);
278
279 /* Convert str */
280 if (elemData != NULL) {
281 convVal = strtol(elemData, NULL, 0);
282 } else {
283 errno = ERANGE;
284 }
285
286 if (errno == ERANGE) {
287 ERR_LOG("Cannot convert \"%s\" to byte array!\n", str);
288 result = APP_ERROR;
289 } else {
290 val[index++] = (char)convVal & 0xFF;
291
292 while ((elemData = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
293 convVal = strtol(elemData, NULL, 0);
294 if (errno == ERANGE) {
295 ERR_LOG("Cannot convert \"%s\" to byte array!\n", str);
296 result = APP_ERROR;
297 break;
298 }
299 val[index++] = (char)convVal & 0xFF;
300 }
301 }
302
303 free(tmpStr);
304 }
305
306 if (result == APP_NO_ERROR) {
307 *data = val;
308 *arraySize = size;
309 } else {
310 free(val);
311 *data = NULL;
312 *arraySize = 0;
313 }
314 return result;
315 }
316 case TYPE_UBYTE_ARRAY: {
317 char *elemData;
318 unsigned int convVal;
319 int index = 0;
320 APP_STATUS result = APP_NO_ERROR;
321
322 int size = paramGetArraySizeFromString(str);
323 unsigned char *val = malloc(sizeof(unsigned char) * size);
324 if (!val) {
325 ERR_LOG("malloc fail!\n");
326 return APP_ERROR;
327 }
328
329 if (size == 1) {
330 /* Convert str */
331 convVal = strtoul(str, NULL, 0);
332 if (errno == ERANGE) {
333 ERR_LOG("Cannot convert \"%s\" to ubyte array!\n", str);
334 result = APP_ERROR;
335 } else {
336 val[0] = (unsigned char)convVal & 0xFF;
337 }
338 } else {
339 char *tmpStr = strdup(str);
340 char *restOfStr = NULL;
341 elemData = utilStrtok(tmpStr, ARRAY_SEPERATOR, &restOfStr);
342
343 /* Convert str */
344 if (elemData != NULL) {
345 convVal = strtoul(elemData, NULL, 0);
346 } else {
347 errno = ERANGE;
348 }
349
350 if (errno == ERANGE) {
351 ERR_LOG("Cannot convert \"%s\" to ubyte array!\n", str);
352 result = APP_ERROR;
353 } else {
354 val[index++] = (unsigned char)convVal & 0xFF;
355
356 while ((elemData = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
357 convVal = strtoul(elemData, NULL, 0);
358 if (errno == ERANGE) {
359 ERR_LOG("Cannot convert \"%s\" to ubyte array!\n", str);
360 result = APP_ERROR;
361 break;
362 }
363 val[index++] = (unsigned char)convVal & 0xFF;
364 }
365 }
366
367 free(tmpStr);
368 }
369
370 if (result == APP_NO_ERROR) {
371 *data = val;
372 *arraySize = size;
373 } else {
374 free(val);
375 *data = NULL;
376 *arraySize = 0;
377 }
378 return result;
379 }
380 case TYPE_SHORT_ARRAY: {
381 char *elemData;
382 unsigned int convVal;
383 int index = 0;
384 APP_STATUS result = APP_NO_ERROR;
385
386 int size = paramGetArraySizeFromString(str);
387 short *val = malloc(sizeof(short) * size);
388 if (!val) {
389 ERR_LOG("malloc fail!\n");
390 return APP_ERROR;
391 }
392
393 if (size == 1) {
394 /* Convert str */
395 convVal = strtol(str, NULL, 0);
396 if (errno == ERANGE) {
397 ERR_LOG("Cannot convert \"%s\" to short array!\n", str);
398 result = APP_ERROR;
399 } else {
400 val[0] = (short)convVal & 0xFFFF;
401 }
402 } else {
403 char *tmpStr = strdup(str);
404 char *restOfStr = NULL;
405 elemData = utilStrtok(tmpStr, ARRAY_SEPERATOR, &restOfStr);
406
407 /* Convert str */
408 if (elemData != NULL) {
409 convVal = strtol(elemData, NULL, 0);
410 } else {
411 errno = ERANGE;
412 }
413
414 if (errno == ERANGE) {
415 ERR_LOG("Cannot convert \"%s\" to short array!\n", str);
416 result = APP_ERROR;
417 } else {
418 val[index++] = (short)convVal & 0xFFFF;
419
420 while ((elemData = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
421 convVal = strtol(elemData, NULL, 0);
422 if (errno == ERANGE) {
423 ERR_LOG("Cannot convert \"%s\" to short array!\n", str);
424 result = APP_ERROR;
425 break;
426 }
427 val[index++] = (short)convVal & 0xFFFF;
428 }
429 }
430
431 free(tmpStr);
432 }
433
434 if (result == APP_NO_ERROR) {
435 *data = val;
436 *arraySize = size;
437 } else {
438 free(val);
439 *data = NULL;
440 *arraySize = 0;
441 }
442 return result;
443 }
444 case TYPE_USHORT_ARRAY: {
445 char *elemData;
446 unsigned int convVal;
447 int index = 0;
448 APP_STATUS result = APP_NO_ERROR;
449
450 int size = paramGetArraySizeFromString(str);
451 unsigned short *val = malloc(sizeof(unsigned short) * size);
452 if (!val) {
453 ERR_LOG("malloc fail!\n");
454 return APP_ERROR;
455 }
456
457 if (size == 1) {
458 /* Convert str */
459 convVal = strtoul(str, NULL, 0);
460 if (errno == ERANGE) {
461 ERR_LOG("Cannot convert \"%s\" to ushort array!\n", str);
462 result = APP_ERROR;
463 } else {
464 val[0] = (unsigned short)convVal & 0xFFFF;
465 }
466 } else {
467 char *tmpStr = strdup(str);
468 char *restOfStr = NULL;
469 elemData = utilStrtok(tmpStr, ARRAY_SEPERATOR, &restOfStr);
470
471 /* Convert str */
472 if (elemData != NULL) {
473 convVal = strtoul(elemData, NULL, 0);
474 } else {
475 errno = ERANGE;
476 }
477
478 if (errno == ERANGE) {
479 ERR_LOG("Cannot convert \"%s\" to ushort array!\n", str);
480 result = APP_ERROR;
481 } else {
482 val[index++] = (unsigned short)convVal & 0xFFFF;
483
484 while ((elemData = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
485 convVal = strtoul(elemData, NULL, 0);
486 if (errno == ERANGE) {
487 ERR_LOG("Cannot convert \"%s\" to ushort array!\n", str);
488 result = APP_ERROR;
489 break;
490 }
491 val[index++] = (unsigned short)convVal & 0xFFFF;
492 }
493 }
494
495 free(tmpStr);
496 }
497
498 if (result == APP_NO_ERROR) {
499 *data = val;
500 *arraySize = size;
501 } else {
502 free(val);
503 *data = NULL;
504 *arraySize = 0;
505 }
506 return result;
507 }
508 case TYPE_INT_ARRAY: {
509 char *elemData;
510 unsigned int convVal;
511 int index = 0;
512 APP_STATUS result = APP_NO_ERROR;
513
514 int size = paramGetArraySizeFromString(str);
515 int *val = malloc(sizeof(int) * size);
516 if (!val) {
517 ERR_LOG("malloc fail!\n");
518 return APP_ERROR;
519 }
520
521 if (size == 1) {
522 /* Convert str */
523 convVal = strtol(str, NULL, 0);
524 if (errno == ERANGE) {
525 ERR_LOG("Cannot convert \"%s\" to int array!\n", str);
526 result = APP_ERROR;
527 } else {
528 val[0] = (int)convVal & 0xFFFFFFFF;
529 }
530 } else {
531 char *tmpStr = strdup(str);
532 char *restOfStr = NULL;
533 elemData = utilStrtok(tmpStr, ARRAY_SEPERATOR, &restOfStr);
534
535 /* Convert str */
536 if (elemData != NULL) {
537 convVal = strtol(elemData, NULL, 0);
538 } else {
539 errno = ERANGE;
540 }
541
542 if (errno == ERANGE) {
543 ERR_LOG("Cannot convert \"%s\" to int array!\n", str);
544 result = APP_ERROR;
545 } else {
546 val[index++] = (int)convVal & 0xFFFFFFFF;
547
548 while ((elemData = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
549 convVal = strtoul(elemData, NULL, 0);
550 if (errno == ERANGE) {
551 ERR_LOG("Cannot convert \"%s\" to int array!\n", str);
552 result = APP_ERROR;
553 break;
554 }
555 val[index++] = (int)convVal & 0xFFFFFFFF;
556 }
557 }
558
559 free(tmpStr);
560 }
561
562 if (result == APP_NO_ERROR) {
563 *data = val;
564 *arraySize = size;
565 } else {
566 free(val);
567 *data = NULL;
568 *arraySize = 0;
569 }
570 return result;
571 }
572 case TYPE_UINT_ARRAY: {
573 char *elemData;
574 unsigned int convVal;
575 int index = 0;
576 APP_STATUS result = APP_NO_ERROR;
577
578 int size = paramGetArraySizeFromString(str);
579 unsigned int *val = malloc(sizeof(unsigned int) * size);
580 if (!val) {
581 ERR_LOG("malloc fail!\n");
582 return APP_ERROR;
583 }
584
585 if (size == 1) {
586 /* Convert str */
587 convVal = strtoul(str, NULL, 0);
588 if (errno == ERANGE) {
589 ERR_LOG("Cannot convert \"%s\" to uint array!\n", str);
590 result = APP_ERROR;
591 } else {
592 val[0] = (unsigned int)convVal & 0xFFFFFFFF;
593 }
594 } else {
595 char *tmpStr = strdup(str);
596 char *restOfStr = NULL;
597 elemData = utilStrtok(tmpStr, ARRAY_SEPERATOR, &restOfStr);
598
599 /* Convert str */
600 if (elemData != NULL) {
601 convVal = strtoul(elemData, NULL, 0);
602 } else {
603 errno = ERANGE;
604 }
605
606 if (errno == ERANGE) {
607 ERR_LOG("Cannot convert \"%s\" to uint array!\n", str);
608 result = APP_ERROR;
609 } else {
610 val[index++] = (unsigned int)convVal & 0xFFFFFFFF;
611
612 while ((elemData = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
613 convVal = strtoul(elemData, NULL, 0);
614 if (errno == ERANGE) {
615 ERR_LOG("Cannot convert \"%s\" to uint array!\n", str);
616 result = APP_ERROR;
617 break;
618 }
619 val[index++] = (unsigned int)convVal & 0xFFFFFFFF;
620 }
621 }
622
623 free(tmpStr);
624 }
625
626 if (result == APP_NO_ERROR) {
627 *data = val;
628 *arraySize = size;
629 } else {
630 free(val);
631 *data = NULL;
632 *arraySize = 0;
633 }
634 return result;
635 }
636 case TYPE_DOUBLE_ARRAY: {
637 char *elemData, *p;
638 double convVal;
639 int index = 0;
640 APP_STATUS result = APP_NO_ERROR;
641
642 int size = paramGetArraySizeFromString(str);
643 double *val = malloc(sizeof(double) * size);
644 if (!val) {
645 ERR_LOG("malloc fail!\n");
646 return APP_ERROR;
647 }
648
649 if (size == 1) {
650 /* Convert str */
651 convVal = strtod(str, &p);
652 if (p != (str + strlen(str))) {
653 ERR_LOG("Cannot convert \"%s\" to double array!\n", str);
654 result = APP_ERROR;
655 } else {
656 val[0] = (double)convVal;
657 }
658 } else {
659 char *tmpStr = strdup(str);
660 char *restOfStr = NULL;
661 elemData = utilStrtok(tmpStr, ARRAY_SEPERATOR, &restOfStr);
662
663 if (elemData) {
664 /* Convert str */
665 convVal = strtod(elemData, &p);
666 if (p != (elemData + strlen(elemData))) {
667 ERR_LOG("Cannot convert \"%s\" to double array!\n", elemData);
668 result = APP_ERROR;
669 } else {
670 val[index++] = (double)convVal;
671
672 while ((elemData = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
673 convVal = strtod(elemData, &p);
674 if (p != (elemData + strlen(elemData))) {
675 ERR_LOG("Cannot convert \"%s\" to double array!\n", elemData);
676 result = APP_ERROR;
677 break;
678 }
679 val[index++] = (double)convVal;
680 }
681 }
682 } else {
683 ERR_LOG("elemData is NULL");
684 result = APP_ERROR;
685 }
686
687 free(tmpStr);
688 }
689
690 if (result == APP_NO_ERROR) {
691 *data = val;
692 *arraySize = size;
693 } else {
694 free(val);
695 *data = NULL;
696 *arraySize = 0;
697 }
698 return result;
699 }
700 case TYPE_UNKNOWN:
701 *data = strdup(str);
702 *arraySize = 0;
703 break;
704 default:
705 *data = NULL;
706 *arraySize = 0;
707 break;
708 }
709
710 return APP_NO_ERROR;
711}
712
713EXPORT char *utilConvDataToString(DATA_TYPE dataType, void *data, int arraySize, int uArrayHexMode) {
714 char *str = NULL;
715 UT_string *dataStr = NULL;
716
717 switch (dataType) {
718 case TYPE_STR: {
719 str = strdup((char *)data);
720 return str;
721 }
722 case TYPE_INT: {
723 int value = *(int *) data;
724 utstring_new(dataStr);
725 utstring_printf(dataStr, "%d", value);
726 str = strdup(utstring_body(dataStr));
727 utstring_free(dataStr);
728 return str;
729 }
730 case TYPE_UINT: {
731 unsigned int value = *(unsigned int *) data;
732 utstring_new(dataStr);
733 utstring_printf(dataStr, "%u", value);
734 str = strdup(utstring_body(dataStr));
735 utstring_free(dataStr);
736 return str;
737 }
738 case TYPE_FLOAT: {
739 float value = *(float *) data;
740 utstring_new(dataStr);
741 utstring_printf(dataStr, "%f", value);
742 str = strdup(utstring_body(dataStr));
743 utstring_free(dataStr);
744 return str;
745 }
746 case TYPE_BYTE_ARRAY: {
747 char *byteArray = (char *)data;
748 int i = 0;
749 utstring_new(dataStr);
750 for (i = 0; i < arraySize; i++) {
751 if (i == arraySize - 1) {
752 utstring_printf(dataStr, "%d", byteArray[i]);
753 } else {
754 utstring_printf(dataStr, "%d,", byteArray[i]);
755 }
756 }
757 str = strdup(utstring_body(dataStr));
758 utstring_free(dataStr);
759 return str;
760 }
761 case TYPE_UBYTE_ARRAY: {
762 unsigned char *ubyteArray = (unsigned char *)data;
763 int i = 0;
764 utstring_new(dataStr);
765 for (i = 0; i < arraySize; i++) {
766 if (i == arraySize - 1) {
767 utstring_printf(dataStr, uArrayHexMode ? "0x%X" : "%d", ubyteArray[i]);
768 } else {
769 utstring_printf(dataStr, uArrayHexMode ? "0x%X," : "%d,", ubyteArray[i]);
770 }
771 }
772 str = strdup(utstring_body(dataStr));
773 utstring_free(dataStr);
774 return str;
775 }
776 case TYPE_SHORT_ARRAY: {
777 short *shortArray = (short *)data;
778 int i = 0;
779 utstring_new(dataStr);
780 for (i = 0; i < arraySize; i++) {
781 if (i == arraySize - 1) {
782 utstring_printf(dataStr, "%d", shortArray[i]);
783 } else {
784 utstring_printf(dataStr, "%d,", shortArray[i]);
785 }
786 }
787 str = strdup(utstring_body(dataStr));
788 utstring_free(dataStr);
789 return str;
790 }
791 case TYPE_USHORT_ARRAY: {
792 unsigned short *ushortArray = (unsigned short *)data;
793 int i = 0;
794 utstring_new(dataStr);
795 for (i = 0; i < arraySize; i++) {
796 if (i == arraySize - 1) {
797 utstring_printf(dataStr, uArrayHexMode ? "0x%X" : "%d", ushortArray[i]);
798 } else {
799 utstring_printf(dataStr, uArrayHexMode ? "0x%X," : "%d,", ushortArray[i]);
800 }
801 }
802 str = strdup(utstring_body(dataStr));
803 utstring_free(dataStr);
804 return str;
805 }
806 case TYPE_INT_ARRAY: {
807 int *intArray = (int *)data;
808 int i = 0;
809 utstring_new(dataStr);
810 for (i = 0; i < arraySize; i++) {
811 if (i == arraySize - 1) {
812 utstring_printf(dataStr, "%d", intArray[i]);
813 } else {
814 utstring_printf(dataStr, "%d,", intArray[i]);
815 }
816 }
817 str = strdup(utstring_body(dataStr));
818 utstring_free(dataStr);
819 return str;
820 }
821 case TYPE_UINT_ARRAY: {
822 unsigned int *uintArray = (unsigned int *)data;
823 int i = 0;
824 utstring_new(dataStr);
825 for (i = 0; i < arraySize; i++) {
826 if (i == arraySize - 1) {
827 utstring_printf(dataStr, uArrayHexMode ? "0x%X" : "%d", uintArray[i]);
828 } else {
829 utstring_printf(dataStr, uArrayHexMode ? "0x%X," : "%d,", uintArray[i]);
830 }
831 }
832 str = strdup(utstring_body(dataStr));
833 utstring_free(dataStr);
834 return str;
835 }
836 case TYPE_DOUBLE_ARRAY: {
837 double *doubleArray = (double *)data;
838 int i = 0;
839 utstring_new(dataStr);
840 for (i = 0; i < arraySize; i++) {
841 if (i == arraySize - 1) {
842 utstring_printf(dataStr, "%f", doubleArray[i]);
843 } else {
844 utstring_printf(dataStr, "%f,", doubleArray[i]);
845 }
846 }
847 str = strdup(utstring_body(dataStr));
848 utstring_free(dataStr);
849 return str;
850 }
851 case TYPE_UNKNOWN:
852 case TYPE_FIELD:
853 break;
854 }
855
856 return str;
857}
858
859/* Convert category path to category group path. eg: HAC -> Handset */
860EXPORT UT_string *utilNormalizeCategoryGroupPathForAudioType(const char *categoryPath, AudioType *audioType) {
861 UT_string *searchPath = NULL;
862 char *categoryType, *category, *tmpCategoryPath;
863 char *restOfStr = NULL;
864 StrPair *strPairHash = NULL, *pair = NULL;
865 size_t numOfCategoryType, i;
866 utstring_new(searchPath);
867
868 /* Split string with token to parse category path info */
869 tmpCategoryPath = strdup(categoryPath);
870 if ((categoryType = utilStrtok(tmpCategoryPath, ARRAY_SEPERATOR, &restOfStr)) == NULL) {
871 free(tmpCategoryPath);
872 return searchPath;
873 }
874
875 if ((category = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr)) == NULL) {
876 free(tmpCategoryPath);
877 return searchPath;
878 }
879
880 pair = strPairCreate(categoryType, category);
881 HASH_ADD_KEYPTR(hh, strPairHash, pair->key, strlen(pair->key), pair);
882 while ((categoryType = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
883 if ((category = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
884 pair = strPairCreate(categoryType, category);
885 HASH_ADD_KEYPTR(hh, strPairHash, pair->key, strlen(pair->key), pair);
886 }
887 }
888 free(tmpCategoryPath);
889
890 /* Finding the audioType related Cateory*/
891 numOfCategoryType = audioTypeGetNumOfCategoryType(audioType);
892 for (i = 0; i < numOfCategoryType; i++) {
893 CategoryType *categoryType = audioTypeGetCategoryTypeByIndex(audioType, i);
894 HASH_FIND_STR(strPairHash, categoryType->name, pair);
895 if (pair) {
896 /* Checking if there is alias for the category name */
897 CategoryAlias *categoryAlias = categoryTypeGetCategoryByAlias(categoryType, pair->value);
898 if (categoryAlias) {
899 if (categoryAlias->category->parentType == PARENT_IS_CATEGORY_GROUP) {
900 utstring_printf(searchPath, "%s"ARRAY_SEPERATOR, ((CategoryGroup *)categoryAlias->category->parent.category)->name);
901 } else {
902 WARN_LOG("Cannot get the categroup name of %s category!\n", categoryAlias->category->name);
903 }
904 } else {
905 Category *category = categoryTypeGetCategoryByName(categoryType, pair->value);
906 if (category && category->parentType == PARENT_IS_CATEGORY_GROUP) {
907 utstring_printf(searchPath, "%s"ARRAY_SEPERATOR, ((CategoryGroup *)category->parent.category)->name);
908 } else {
909 /* If the category is not category group, checking the bypass list */
910 int arrayIndex = 0;
911 int bypassCategoryName = 0;
912
913 for (arrayIndex = 0; HARD_CATEGORY_GROUP[arrayIndex][0]; arrayIndex++) {
914 if (!strncmp(audioType->name, HARD_CATEGORY_GROUP[arrayIndex][0], strlen(audioType->name) + 1)
915 && !strncmp(pair->key, HARD_CATEGORY_GROUP[arrayIndex][1], strlen(pair->key) + 1)
916 && !strncmp(pair->value, HARD_CATEGORY_GROUP[arrayIndex][2], strlen(pair->value) + 1)) {
917 bypassCategoryName = 1;
918 break;
919 }
920 }
921
922 if (bypassCategoryName) {
923 utstring_printf(searchPath, "%s"ARRAY_SEPERATOR, HARD_CATEGORY_GROUP[arrayIndex][2]);
924 } else {
925 WARN_LOG("Cannot get the categroup name of %s category!\n", pair->value);
926 }
927 }
928 }
929 }
930 }
931
932 /* Remove the end of seperator char */
933 {
934 char *ch = strrchr(utstring_body(searchPath), ARRAY_SEPERATOR_CH);
935 if (ch) {
936 *ch = '\0';
937 }
938 }
939
940 /* Release strPair */
941 if (strPairHash) {
942 StrPair *tmp, *item;
943 HASH_ITER(hh, strPairHash, item, tmp) {
944 HASH_DEL(strPairHash, item);
945 strPairRelease(item);
946 }
947 }
948
949 return searchPath;
950}
951
952EXPORT UT_string *utilNormalizeCategoryPathForAudioType(const char *categoryPath, AudioType *audioType) {
953 UT_string *searchPath = NULL;
954 char *categoryType, *category, *tmpCategoryPath;
955 char *restOfStr = NULL;
956 StrPair *strPairHash = NULL, *pair = NULL;
957 size_t numOfCategoryType, i;
958 utstring_new(searchPath);
959
960 /* Split string with token to parse category path info */
961 tmpCategoryPath = strdup(categoryPath);
962 if ((categoryType = utilStrtok(tmpCategoryPath, ARRAY_SEPERATOR, &restOfStr)) == NULL) {
963 free(tmpCategoryPath);
964 return searchPath;
965 }
966
967 if ((category = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr)) == NULL) {
968 free(tmpCategoryPath);
969 return searchPath;
970 }
971
972 pair = strPairCreate(categoryType, category);
973 HASH_ADD_KEYPTR(hh, strPairHash, pair->key, strlen(pair->key), pair);
974 while ((categoryType = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
975 if ((category = utilStrtok(NULL, ARRAY_SEPERATOR, &restOfStr))) {
976 pair = strPairCreate(categoryType, category);
977 HASH_ADD_KEYPTR(hh, strPairHash, pair->key, strlen(pair->key), pair);
978 }
979 }
980 free(tmpCategoryPath);
981
982 /* Finding the audioType related Cateory*/
983 numOfCategoryType = audioTypeGetNumOfCategoryType(audioType);
984 for (i = 0; i < numOfCategoryType; i++) {
985 CategoryType *categoryType = audioTypeGetCategoryTypeByIndex(audioType, i);
986 HASH_FIND_STR(strPairHash, categoryType->name, pair);
987 if (pair) {
988 /* Checking if there is alias for the category name */
989 CategoryAlias *categoryAlias = categoryTypeGetCategoryByAlias(categoryType, pair->value);
990 if (categoryAlias) {
991 utstring_printf(searchPath, "%s"ARRAY_SEPERATOR, categoryAlias->category->name);
992 } else {
993 utstring_printf(searchPath, "%s"ARRAY_SEPERATOR, pair->value);
994 }
995 }
996 }
997
998 /* Remove the end of seperator char */
999 {
1000 char *ch = strrchr(utstring_body(searchPath), ARRAY_SEPERATOR_CH);
1001 if (ch) {
1002 *ch = '\0';
1003 }
1004 }
1005
1006 /* Release strPair */
1007 if (strPairHash) {
1008 StrPair *tmp, *item;
1009 HASH_ITER(hh, strPairHash, item, tmp) {
1010 HASH_DEL(strPairHash, item);
1011 strPairRelease(item);
1012 }
1013 }
1014
1015 return searchPath;
1016}
1017
1018EXPORT int utilFindUnusedParamId(AudioType *audioType) {
1019 ParamUnit *paramUnit;
1020 while (1) {
1021 HASH_FIND_INT(audioType->paramUnitHash, &audioType->unusedParamId, paramUnit);
1022 if (paramUnit) {
1023 audioType->unusedParamId++;
1024 } else {
1025 INFO_LOG("Unsed param ID found (id = %d)\n", audioType->unusedParamId);
1026 return audioType->unusedParamId++;
1027 }
1028 }
1029
1030 return 0;
1031}
1032
1033
1034EXPORT void utilUsleep(unsigned int usec) {
1035#ifndef WIN32
1036 usleep(usec);
1037#else
1038 Sleep(usec);
1039#endif
1040}
1041
1042EXPORT char *utilGetStdin(char *buf, int bufSize) {
1043 char *input;
1044 input = fgets(buf, bufSize, stdin);
1045 if ((input = strchr(input, '\n')) != NULL) {
1046 *input = '\0';
1047 }
1048 return buf;
1049}
1050
1051EXPORT void utilLog(char *format, ...) {
1052 time_t timep;
1053 struct tm *p;
1054 va_list arglist;
1055
1056 /* Get time struct */
1057 time(&timep);
1058 p = localtime(&timep);
1059 if (!p) {
1060 ERR_LOG("localtime fail!\n");
1061 return;
1062 }
1063
1064 if (appLogFp == NULL) {
1065 /* Create file handle */
1066 UT_string *fileName = NULL;
1067 utstring_new(fileName);
1068 utstring_printf(fileName, "%04d%02d%02d_%02d%02d%02d_AppLogFile.txt", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
1069 appLogFp = fopen(utstring_body(fileName), "a");
1070 utstring_free(fileName);
1071
1072 if (!appLogFp) {
1073 printf("Log file open failed!\n");
1074 exit(1);
1075 }
1076 }
1077
1078 /* Write to file */
1079 fprintf(appLogFp, "%02d-%02d %02d:%02d:%02d ", 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
1080 va_start(arglist, format);
1081 vfprintf(appLogFp, format, arglist);
1082 va_end(arglist);
1083
1084 /* Show log to console */
1085 if (outputLogToStdout) {
1086 va_start(arglist, format);
1087 vfprintf(stdout, format, arglist);
1088 va_end(arglist);
1089 }
1090}
1091
1092EXPORT void utilLogClose() {
1093 if (appLogFp) {
1094 fflush(appLogFp);
1095 fclose(appLogFp);
1096 appLogFp = NULL;
1097 }
1098}
1099
1100EXPORT void utilShowParamValue(Param *param) {
1101 if (!param) {
1102 ERR_LOG("param is NULL\n");
1103 return;
1104 }
1105
1106 switch (param->paramInfo->dataType) {
1107 case TYPE_STR: {
1108 char *value = (char *)param->data;
1109 printf("param name = %s, value = %s (type = %s, bytes = "APP_SIZE_T_FT")\n", param->name, value, paramDataTypeToStr(param->paramInfo->dataType), paramGetNumOfBytes(param));
1110 break;
1111 }
1112 case TYPE_INT: {
1113 int value = *(int *) param->data;
1114 printf("param name = %s, value = %d (type = %s, bytes = "APP_SIZE_T_FT")\n", param->name, value, paramDataTypeToStr(param->paramInfo->dataType), paramGetNumOfBytes(param));
1115 break;
1116 }
1117 case TYPE_UINT: {
1118 unsigned int value = *(unsigned int *) param->data;
1119 printf("param name = %s, value = %d (type = %s, bytes = "APP_SIZE_T_FT")\n", param->name, value, paramDataTypeToStr(param->paramInfo->dataType), paramGetNumOfBytes(param));
1120 break;
1121 }
1122 case TYPE_FLOAT: {
1123 float value = *(float *) param->data;
1124 /* JH's platform cannot show the float type, wei chiu could show the value by %f*/
1125 printf("param name = %s, value = %d (type = %s, bytes = "APP_SIZE_T_FT")\n", param->name, (int)value, paramDataTypeToStr(param->paramInfo->dataType), paramGetNumOfBytes(param));
1126 break;
1127 }
1128 case TYPE_BYTE_ARRAY: {
1129 char *byteArray = (char *)param->data;
1130 int arraySize = param->arraySize;
1131 int i = 0;
1132 printf("param name = %s (type = %s, size = %d, bytes = "APP_SIZE_T_FT")\n", param->name, paramDataTypeToStr(param->paramInfo->dataType), arraySize, paramGetNumOfBytes(param));
1133 for (i = 0; i < arraySize; i++) {
1134 printf("\tarray[%d] 0x%x\n", i, byteArray[i]);
1135 }
1136 break;
1137 }
1138 case TYPE_UBYTE_ARRAY: {
1139 unsigned char *ubyteArray = (unsigned char *)param->data;
1140 int arraySize = param->arraySize;
1141 int i = 0;
1142 printf("param name = %s (type = %s, size = %d, bytes = "APP_SIZE_T_FT")\n", param->name, paramDataTypeToStr(param->paramInfo->dataType), arraySize, paramGetNumOfBytes(param));
1143 for (i = 0; i < arraySize; i++) {
1144 printf("\tarray[%d] 0x%x\n", i, ubyteArray[i]);
1145 }
1146 break;
1147 }
1148 case TYPE_SHORT_ARRAY: {
1149 short *shortArray = (short *)param->data;
1150 int arraySize = param->arraySize;
1151 int i = 0;
1152 printf("param name = %s (type = %s, size = %d, bytes = "APP_SIZE_T_FT")\n", param->name, paramDataTypeToStr(param->paramInfo->dataType), arraySize, paramGetNumOfBytes(param));
1153 for (i = 0; i < arraySize; i++) {
1154 printf("\tarray[%d] %d\n", i, shortArray[i]);
1155 }
1156 break;
1157 }
1158 case TYPE_USHORT_ARRAY: {
1159 unsigned short *ushortArray = (unsigned short *)param->data;
1160 int arraySize = param->arraySize;
1161 int i = 0;
1162 printf("param name = %s (type = %s, size = %d, bytes = "APP_SIZE_T_FT")\n", param->name, paramDataTypeToStr(param->paramInfo->dataType), arraySize, paramGetNumOfBytes(param));
1163 for (i = 0; i < arraySize; i++) {
1164 printf("\tarray[%d] 0x%x\n", i, ushortArray[i]);
1165 }
1166 break;
1167 }
1168 case TYPE_INT_ARRAY: {
1169 int *intArray = (int *)param->data;
1170 int arraySize = param->arraySize;
1171 int i = 0;
1172 printf("param name = %s (type = %s, size = %d, bytes = "APP_SIZE_T_FT")\n", param->name, paramDataTypeToStr(param->paramInfo->dataType), arraySize, paramGetNumOfBytes(param));
1173 for (i = 0; i < arraySize; i++) {
1174 printf("\tarray[%d] %d\n", i, intArray[i]);
1175 }
1176 break;
1177 }
1178 case TYPE_UINT_ARRAY: {
1179 unsigned int *uintArray = (unsigned int *)param->data;
1180 int arraySize = param->arraySize;
1181 int i = 0;
1182 printf("param name = %s (type = %s, size = %d, bytes = "APP_SIZE_T_FT")\n", param->name, paramDataTypeToStr(param->paramInfo->dataType), arraySize, paramGetNumOfBytes(param));
1183 for (i = 0; i < arraySize; i++) {
1184 printf("\tarray[%d] 0x%x\n", i, uintArray[i]);
1185 }
1186 break;
1187 }
1188 case TYPE_DOUBLE_ARRAY: {
1189 double *doubleArray = (double *)param->data;
1190 int arraySize = param->arraySize;
1191 int i = 0;
1192 printf("param name = %s (type = %s, size = %d, bytes = "APP_SIZE_T_FT")\n", param->name, paramDataTypeToStr(param->paramInfo->dataType), arraySize, paramGetNumOfBytes(param));
1193 for (i = 0; i < arraySize; i++) {
1194 printf("\tarray[%d] %f\n", i, doubleArray[i]);
1195 }
1196 break;
1197 }
1198 default:
1199 printf("param name = %s, value = 0x%p (type = %s, bytes = "APP_SIZE_T_FT")\n", param->name, param->data, paramDataTypeToStr(param->paramInfo->dataType), paramGetNumOfBytes(param));
1200 }
1201}
1202
1203EXPORT FieldInfo *utilXmlNodeGetFieldInfo(AppHandle *appHandle, xmlNode *node, const char *audioTypeAttrName, const char *paramAttrName, const char *fieldAttrName) {
1204 AudioType *audioType = NULL;
1205 ParamInfo *paramInfo = NULL;
1206 FieldInfo *fieldInfo = NULL;
1207
1208 xmlChar *audioTypeName = NULL;
1209 xmlChar *paramName = NULL;
1210 xmlChar *fieldName = NULL;
1211
1212 audioTypeName = xmlNodeGetProp(node, audioTypeAttrName);
1213 if (audioTypeName) {
1214 audioType = appHandleGetAudioTypeByName(appHandle, (const char *)audioTypeName);
1215 } else {
1216 xmlFree(audioTypeName);
1217 return NULL;
1218 }
1219
1220 paramName = xmlNodeGetProp(node, paramAttrName);
1221 if (audioType && paramName) {
1222 paramInfo = audioTypeGetParamInfoByName(audioType, (const char *)paramName);
1223 } else {
1224 WARN_LOG("Cannot find FieldInfo (AudioType = %s, Param = %s, Field = %s)\n", audioTypeName, paramName, fieldName);
1225 xmlFree(audioTypeName);
1226 xmlFree(paramName);
1227 return NULL;
1228 }
1229
1230 fieldName = xmlNodeGetProp(node, fieldAttrName);
1231 if (paramInfo && fieldName) {
1232 fieldInfo = paramInfoGetFieldInfoByName(paramInfo, (const char *)fieldName);
1233 }
1234
1235 if (!fieldInfo) {
1236 WARN_LOG("Cannot find FieldInfo (AudioType = %s, Param = %s, Field = %s)\n", audioTypeName, paramName, fieldName);
1237 }
1238
1239 xmlFree(audioTypeName);
1240 xmlFree(paramName);
1241 xmlFree(fieldName);
1242 return fieldInfo;
1243}
1244
1245EXPORT char *utilGenCheckList(int bits) {
1246 UT_string *dataStr = NULL;
1247 double num = pow(2, bits);
1248 int i = 0;
1249 char *retStr = NULL;
1250
1251 utstring_new(dataStr);
1252 for (i = 0; i < num; i++) {
1253 if (i != num - 1) {
1254 utstring_printf(dataStr, "%d,%d,", i, i);
1255 } else {
1256 utstring_printf(dataStr, "%d,%d", i, i);
1257 }
1258 }
1259
1260 retStr = strdup(utstring_body(dataStr));
1261 utstring_free(dataStr);
1262 return retStr;
1263}
1264
1265EXPORT void showTreeRoot(xmlNode *treeRootNode, const char *categoryPath) {
1266}
1267
1268EXPORT void utilMkdir(const char *dir) {
1269#ifdef WIN32
1270 _mkdir(dir);
1271#else
1272 mkdir(dir, 0770);
1273#endif
1274}
1275
1276unsigned int utilNativeGetField(const char *audioTypeName, const char *categoryPath, const char *paramName, const char *fieldName) {
1277 INFO_LOG("audioTypeName = %s, categoryPath = %s, paramName = %s, fieldName = %s", audioTypeName, categoryPath, paramName, fieldName);
1278
1279 if (audioTypeName && categoryPath && paramName && fieldName) {
1280#if defined(SYS_IMPL)
1281 char *resultStr = NULL;
1282 unsigned int result = 0;
1283 UT_string *str = NULL;
1284
1285 utstring_new(str);
1286 utstring_printf(str, APP_GET_FIELD_KEY "#%s#%s#%s#%s", audioTypeName, categoryPath, paramName, fieldName);
1287 resultStr = audioSystemGetParameters(utstring_body(str));
1288 result = atoi(resultStr);
1289 free(resultStr);
1290 utstring_free(str);
1291
1292 return result;
1293#else
1294 AppHandle *appHandle = NULL;
1295 AudioType *audioType = NULL;
1296 ParamUnit *paramUnit = NULL;
1297 unsigned int fieldValue = 0;
1298
1299 appHandle = appHandleGetInstance();
1300 if (appHandle) {
1301 audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
1302 } else {
1303 ERR_LOG("appHandle is NULL\n");
1304 }
1305
1306 audioTypeReadLock(audioType, __FUNCTION__);
1307
1308 if (audioType) {
1309 paramUnit = audioTypeGetParamUnit(audioType, categoryPath);
1310 } else {
1311 ERR_LOG("categoryType is NULL\n");
1312 }
1313
1314 if (paramUnitGetFieldVal(paramUnit, paramName, fieldName, &fieldValue) == APP_ERROR) {
1315 ERR_LOG("Query field value fail!\n");
1316 } else {
1317 audioTypeUnlock(audioType);
1318 return fieldValue;
1319 }
1320 audioTypeUnlock(audioType);
1321#endif
1322 } else {
1323 ERR_LOG("Invalid parameter: audioType = %s, category path = %s, param = %s, field = %s\n", audioTypeName, categoryPath, paramName, fieldName);
1324 }
1325
1326 return 0;
1327}
1328
1329char *utilNativeGetParam(const char *audioTypeName, const char *categoryPath, const char *paramName) {
1330 char *paramDataStr = NULL;
1331 if (audioTypeName && categoryPath && paramName) {
1332#if defined(SYS_IMPL)
1333 INFO_LOG("audioTypeName = %s, categoryPath = %s, paramName = %s", audioTypeName, categoryPath, paramName);
1334 UT_string *str = NULL;
1335
1336 utstring_new(str);
1337 utstring_printf(str, APP_GET_PARAM_KEY "#%s#%s#%s", audioTypeName, categoryPath, paramName);
1338 paramDataStr = audioSystemGetParameters(utstring_body(str));
1339 utstring_free(str);
1340#else
1341 AppHandle *appHandle = NULL;
1342 AudioType *audioType = NULL;
1343 ParamUnit *paramUnit = NULL;
1344 Param *param = NULL;
1345
1346 appHandle = appHandleGetInstance();
1347 if (appHandle) {
1348 audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
1349 } else {
1350 ERR_LOG("appHandle is NULL\n");
1351 }
1352
1353 audioTypeReadLock(audioType, __FUNCTION__);
1354
1355 if (audioType) {
1356 paramUnit = audioTypeGetParamUnit(audioType, categoryPath);
1357 } else {
1358 ERR_LOG("categoryType is NULL\n");
1359 }
1360
1361 if (paramUnit) {
1362 param = paramUnitGetParamByName(paramUnit, paramName);
1363 } else {
1364 ERR_LOG("paramUnit is NULL\n");
1365 }
1366
1367 if (param) {
1368 paramDataStr = paramNewDataStr(param);
1369 }
1370
1371 audioTypeUnlock(audioType);
1372#endif
1373 } else {
1374 ERR_LOG("Invalid parameter: audioType = %s, category path = %s, param = %s\n", audioTypeName, categoryPath, paramName);
1375 }
1376
1377 return paramDataStr;
1378}
1379
1380EXPORT char *utilNativeGetCategory(const char *audioTypeName, const char *categoryTypeName) {
1381 INFO_LOG("audioTypeName = %s, categoryTypeName = %s", audioTypeName, categoryTypeName);
1382
1383 if (audioTypeName && categoryTypeName) {
1384#if defined(SYS_IMPL)
1385 char *result = NULL;
1386 UT_string *str = NULL;
1387
1388 utstring_new(str);
1389 utstring_printf(str, APP_GET_CATEGORY_KEY "#%s#%s", audioTypeName, categoryTypeName);
1390 result = audioSystemGetParameters(utstring_body(str));
1391 utstring_free(str);
1392
1393 return result;
1394#else
1395 UT_string *utString = NULL;
1396 char *result;
1397 int firstCategory = 1;
1398 int numOfCategory, numOfCategoryGroup = 0;
1399 int i, j, k;
1400 AppHandle *appHandle = NULL;
1401 AudioType *audioType = NULL;
1402 CategoryType *categoryType = NULL;
1403
1404 utstring_new(utString);
1405 appHandle = appHandleGetInstance();
1406 if (appHandle) {
1407 audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
1408 } else {
1409 ERR_LOG("appHandle is NULL\n");
1410 }
1411
1412 if (audioType) {
1413 categoryType = audioTypeGetCategoryTypeByName(audioType, categoryTypeName);
1414 } else {
1415 ERR_LOG("audioType is NULL\n");
1416 }
1417
1418 if (!categoryType) {
1419 ERR_LOG("categoryType is NULL\n");
1420 }
1421
1422 numOfCategoryGroup = categoryTypeGetNumOfCategoryGroup(categoryType);
1423 for (i = 0; i < numOfCategoryGroup; i++) {
1424 CategoryGroup *categoryGroup = categoryTypeGetCategoryGroupByIndex(categoryType, i);
1425 numOfCategory = categoryGroupGetNumOfCategory(categoryGroup);
1426 for (j = 0; j < numOfCategory; j++) {
1427 Category *category = categoryGroupGetCategoryByIndex(categoryGroup, j);
1428 if (firstCategory) {
1429 utstring_printf(utString, "%s,%s", category->name, category->wording);
1430 firstCategory = 0;
1431 } else {
1432 utstring_printf(utString, ",%s,%s", category->name, category->wording);
1433 }
1434 }
1435 }
1436
1437 numOfCategory = categoryTypeGetNumOfCategory(categoryType);
1438 for (k = 0; k < numOfCategory; k++) {
1439 Category *category = categoryTypeGetCategoryByIndex(categoryType, k);
1440 if (firstCategory) {
1441 utstring_printf(utString, "%s,%s", category->name, category->wording);
1442 firstCategory = 0;
1443 } else {
1444 utstring_printf(utString, ",%s,%s", category->name, category->wording);
1445 }
1446 }
1447
1448 result = strdup(utstring_body(utString));
1449 utstring_free(utString);
1450 return result;
1451#endif
1452 }
1453
1454 return NULL;
1455}
1456
1457EXPORT APP_STATUS utilNativeSetParam(const char *audioTypeName, const char *categoryPath, const char *paramName, const char *paramDataStr) {
1458 INFO_LOG("audioTypeName = %s, categoryPath = %s, paramName = %s", audioTypeName, categoryPath, paramName);
1459
1460 if (audioTypeName && categoryPath && paramName && paramDataStr) {
1461#if defined(SYS_IMPL)
1462 UT_string *str = NULL;
1463
1464 utstring_new(str);
1465 utstring_printf(str, APP_SET_PARAM_KEY "=%s#%s#%s#%s", audioTypeName, categoryPath, paramName, paramDataStr);
1466 audioSystemSetParameters(utstring_body(str));
1467 utstring_free(str);
1468
1469 return APP_NO_ERROR;
1470#else
1471 AppHandle *appHandle = NULL;
1472 AudioType *audioType = NULL;
1473 ParamInfo *paramInfo = NULL;
1474
1475 appHandle = appHandleGetInstance();
1476 if (appHandle) {
1477 audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
1478 } else {
1479 ERR_LOG("appHandle is NULL\n");
1480 }
1481
1482 if (audioType) {
1483 paramInfo = audioTypeGetParamInfoByName(audioType, paramName);
1484 } else {
1485 ERR_LOG("audioType is NULL\n");
1486 }
1487
1488 if (paramInfo) {
1489 void *paramData = NULL;
1490 size_t arraySize = 0;
1491
1492 if (utilConvDataStringToNative(paramInfo->dataType, paramDataStr, &paramData, &arraySize) == APP_NO_ERROR) {
1493 if (audioTypeSetParamData(audioType, categoryPath, paramInfo, paramData, arraySize) == APP_ERROR) {
1494 ERR_LOG("audioTypeSetParamData fail! audioType = %s, categoryPath = %s, paramInfo = %s\n", audioType->name, categoryPath, paramInfo->name);
1495 } else {
1496 if (paramData) {
1497 free(paramData);
1498 }
1499 return APP_NO_ERROR;
1500 }
1501 } else {
1502 ERR_LOG("Cannot convert param string to native type (param str = %s)\n", paramDataStr);
1503 }
1504
1505 if (paramData) {
1506 free(paramData);
1507 }
1508 } else {
1509 ERR_LOG("paramInfo is NULL\n");
1510 }
1511#endif
1512 } else {
1513 ERR_LOG("Invalid parameter\n");
1514 }
1515
1516 return APP_ERROR;
1517}
1518
1519EXPORT APP_STATUS utilNativeSetField(const char *audioTypeName, const char *categoryPath, const char *paramName, const char *fieldName, const char *fieldValueStr) {
1520 INFO_LOG("audioTypeName = %s, categoryPath = %s, paramName = %s, fieldName = %s", audioTypeName, categoryPath, paramName, fieldName);
1521
1522 if (audioTypeName && categoryPath && paramName && fieldName && fieldValueStr) {
1523#if defined(SYS_IMPL)
1524 UT_string *str = NULL;
1525
1526 utstring_new(str);
1527 utstring_printf(str, APP_SET_FIELD_KEY "=%s#%s#%s#%s#%s", audioTypeName, categoryPath, paramName, fieldName, fieldValueStr);
1528 audioSystemSetParameters(utstring_body(str));
1529 utstring_free(str);
1530
1531 return APP_NO_ERROR;
1532#else
1533 AppHandle *appHandle = NULL;
1534 AudioType *audioType = NULL;
1535 ParamInfo *paramInfo = NULL;
1536 FieldInfo *fieldInfo = NULL;
1537
1538 appHandle = appHandleGetInstance();
1539 if (appHandle) {
1540 audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
1541 } else {
1542 ERR_LOG("appHandle is NULL\n");
1543 }
1544
1545 if (audioType) {
1546 paramInfo = audioTypeGetParamInfoByName(audioType, paramName);
1547 } else {
1548 ERR_LOG("audioType is NULL\n");
1549 }
1550
1551 if (paramInfo) {
1552 fieldInfo = paramInfoGetFieldInfoByName(paramInfo, fieldName);
1553 } else {
1554 ERR_LOG("paramInfo is NULL\n");
1555 }
1556
1557 if (fieldInfo) {
1558 if (audioTypeSetFieldData(audioType, categoryPath, fieldInfo, strtoul(fieldValueStr, NULL, 0)) == APP_ERROR) {
1559 ERR_LOG("audioTypeSetFieldData fail!. audioType = %s, categoryPath = %s, paramInfo = %s, fieldInfo = %s, value = %s\n", audioType->name, categoryPath, paramInfo->name, fieldInfo->name, fieldValueStr);
1560 } else {
1561 return APP_NO_ERROR;
1562 }
1563 } else {
1564 ERR_LOG("fieldInfo is NULL\n");
1565 }
1566#endif
1567 } else {
1568 ERR_LOG("Invalid parameter\n");
1569 }
1570
1571 return APP_ERROR;
1572}
1573
1574EXPORT APP_STATUS utilNativeSaveXml(const char *audioTypeName) {
1575
1576#if defined(SYS_IMPL)
1577 INFO_LOG("audioTypeName = %s", audioTypeName);
1578 UT_string *str = NULL;
1579
1580 utstring_new(str);
1581 utstring_printf(str, APP_SAVE_XML_KEY "=%s", audioTypeName);
1582 audioSystemSetParameters(utstring_body(str));
1583 utstring_free(str);
1584#else
1585 AppHandle *appHandle = appHandleGetInstance();
1586 AudioType *audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
1587 if (audioType) {
1588 if (audioType->dirty) {
1589#ifdef WIN32
1590 return audioTypeSaveAudioParamXml(audioType, XML_CUS_FOLDER_ON_TUNING_TOOL, 1) ;
1591#else
1592 return audioTypeSaveAudioParamXml(audioType, XML_CUS_FOLDER_ON_DEVICE, 1);
1593#endif
1594 } else {
1595 INFO_LOG("%s AudioType's parameter not changed, don't save XML\n", audioType->name);
1596 }
1597 } else {
1598 ERR_LOG("audioType is NULL\n");
1599 return APP_ERROR;
1600 }
1601#endif
1602
1603 return APP_NO_ERROR;
1604}
1605
1606EXPORT const char *utilNativeGetChecklist(const char *audioTypeName, const char *paramName, const char *fieldName) {
1607#if defined(SYS_IMPL)
1608 INFO_LOG("audioTypeName = %s, paramName = %s, fieldName = %s", audioTypeName, paramName, fieldName);
1609
1610 char *result = NULL;
1611 UT_string *str = NULL;
1612
1613 utstring_new(str);
1614 utstring_printf(str, APP_GET_CHECKLIST_KEY "#%s#%s#%s", audioTypeName, paramName, fieldName);
1615 result = audioSystemGetParameters(utstring_body(str));
1616 utstring_free(str);
1617
1618 return result;
1619#else
1620 AppHandle *appHandle = appHandleGetInstance();
1621 AudioType *audioType = appHandleGetAudioTypeByName(appHandle, audioTypeName);
1622 ParamInfo *paramInfo = NULL;
1623 FieldInfo *fieldInfo = NULL;
1624
1625 if (!audioType) {
1626 ERR_LOG("Cannot find %s AudioType\n", audioTypeName);
1627 return NULL;
1628 }
1629
1630 paramInfo = audioTypeGetParamInfoByName(audioType, paramName);
1631 if (!paramInfo) {
1632 ERR_LOG("Cannot find %s paramInfo of %s AudioType\n", paramName, audioTypeName);
1633 return NULL;
1634 }
1635
1636 fieldInfo = paramInfoGetFieldInfoByName(paramInfo, fieldName);
1637 if (!fieldInfo) {
1638 ERR_LOG("Cannot find %s fieldInfo of %s paramInfo of %s AudioType\n", fieldName, paramName, audioTypeName);
1639 return NULL;
1640 }
1641
1642 return fieldInfo->checkListStr;
1643#endif
1644}
1645
1646EXPORT int utilCompNormalizeCategoryPath(AudioType *audioType, const char *srcCategoryPath, const char *dstCategoryPath) {
1647 UT_string *srcNormalizedPath;
1648 UT_string *dstNormalizedPath;
1649 int equal = 0;
1650
1651 if (!strncmp(srcCategoryPath, dstCategoryPath, strlen(dstCategoryPath) + 1)) {
1652 return 1;
1653 }
1654
1655 srcNormalizedPath = utilNormalizeCategoryPathForAudioType(srcCategoryPath, audioType);
1656 dstNormalizedPath = utilNormalizeCategoryPathForAudioType(dstCategoryPath, audioType);
1657
1658 if (!strncmp(utstring_body(srcNormalizedPath), utstring_body(dstNormalizedPath), strlen(utstring_body(dstNormalizedPath)) + 1)) {
1659 equal = 1;
1660 INFO_LOG("Src path %s and dst path %s are equal to %s\n", srcCategoryPath, dstCategoryPath, utstring_body(srcNormalizedPath));
1661 } else {
1662 equal = 0;
1663 }
1664
1665 utstring_free(srcNormalizedPath);
1666 utstring_free(dstNormalizedPath);
1667
1668 return equal;
1669}
1670
1671EXPORT char *utilStrtok(char *str, const char *delim, char **saveptr) {
1672#ifdef WIN32
1673 return strtok(str, delim);
1674#else
1675 return strtok_r(str, delim, saveptr);
1676#endif
1677}
1678
1679EXPORT void utilShellExecute(const char *prog, const char *params) {
1680#ifdef WIN32
1681 SHELLEXECUTEINFO ShExecInfo = {0};
1682 ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
1683 ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
1684 ShExecInfo.hwnd = NULL;
1685 ShExecInfo.lpVerb = NULL;
1686 ShExecInfo.lpFile = prog;
1687 ShExecInfo.lpParameters = params;
1688 ShExecInfo.lpDirectory = NULL;
1689 ShExecInfo.nShow = SW_HIDE;
1690 ShExecInfo.hInstApp = NULL;
1691 ShellExecuteEx(&ShExecInfo);
1692 WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
1693#endif
1694}