• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "framework_common.h"
17 #include <string.h>
18 #include "securec.h"
19 #include "hdf_base.h"
20 
21 #define MOVE_LEFT_NUM 8
22 #define UHDF_PASSTHROUGH_LIB "libhdi_audio"
23 #define UHDF_CLIENT_LIB      "libhdi_audio_client"
24 
SystemInputFail(void)25 void SystemInputFail(void)
26 {
27     printf("please ENTER to go on...\n");
28     while (getchar() != '\n') {
29         continue;
30     }
31 }
32 
StringToInt(const char * flag)33 uint32_t StringToInt(const char *flag)
34 {
35     if (flag == NULL) {
36         return 0;
37     }
38     uint32_t temp = flag[0];
39     for (int32_t i = (int32_t)strlen(flag) - 1; i >= 0; i--) {
40         temp <<= MOVE_LEFT_NUM;
41         temp += flag[i];
42     }
43     return temp;
44 }
45 
CheckPcmFormat(int32_t val,uint32_t * audioPcmFormat)46 int32_t CheckPcmFormat(int32_t val, uint32_t *audioPcmFormat)
47 {
48     if (audioPcmFormat == NULL) {
49         AUDIO_FUNC_LOGE("fomat is null!");
50         return HDF_FAILURE;
51     }
52     switch (val) {
53         case AUDIO_FORMAT_PCM_8_BIT:
54             *audioPcmFormat = AUDIO_FORMAT_PCM_8_BIT;
55             break;
56         case AUDIO_FORMAT_PCM_16_BIT:
57             *audioPcmFormat = AUDIO_FORMAT_PCM_16_BIT;
58             break;
59         case AUDIO_FORMAT_PCM_24_BIT:
60             *audioPcmFormat = AUDIO_FORMAT_PCM_24_BIT;
61             break;
62         case AUDIO_FORMAT_PCM_32_BIT:
63             *audioPcmFormat = AUDIO_FORMAT_PCM_32_BIT;
64             break;
65         default:
66             *audioPcmFormat = AUDIO_FORMAT_PCM_16_BIT;
67             break;
68     }
69 
70     return HDF_SUCCESS;
71 }
72 
PcmFormatToBits(enum AudioFormat formatBit)73 uint32_t PcmFormatToBits(enum AudioFormat formatBit)
74 {
75     switch (formatBit) {
76         case AUDIO_FORMAT_PCM_16_BIT:
77             return PCM_16_BIT;
78         case AUDIO_FORMAT_PCM_8_BIT:
79             return PCM_8_BIT;
80         default:
81             return PCM_16_BIT;
82     }
83 }
84 
CleanStdin(void)85 void CleanStdin(void)
86 {
87     int c;
88     do {
89         c = getchar();
90     } while (c != '\n' && c != EOF);
91 }
92 
FileClose(FILE ** file)93 void FileClose(FILE **file)
94 {
95     if ((file != NULL) && ((*file) != NULL)) {
96         (void)fclose(*file);
97         *file = NULL;
98     }
99     return;
100 }
101 
FormatLoadLibPath(char * resolvedPath,int32_t pathLen,int choice)102 int32_t FormatLoadLibPath(char *resolvedPath, int32_t pathLen, int choice)
103 {
104     if (resolvedPath == NULL) {
105         AUDIO_FUNC_LOGE("The Parameter is NULL.");
106         return HDF_FAILURE;
107     }
108     char *uhdfLibPath = NULL;
109     switch (choice) {
110         case DIRECT: // Direct Loading
111             uhdfLibPath = HDF_LIBRARY_FULL_PATH(UHDF_PASSTHROUGH_LIB);
112             if (snprintf_s(resolvedPath, pathLen, pathLen - 1, "%s", uhdfLibPath) < 0) {
113                 AUDIO_FUNC_LOGE("snprintf_s failed!");
114                 return HDF_FAILURE;
115             }
116             break;
117         case SERVICE: // IPC Loading
118             uhdfLibPath = HDF_LIBRARY_FULL_PATH(UHDF_CLIENT_LIB);
119             if (snprintf_s(resolvedPath, pathLen, pathLen - 1, "%s", uhdfLibPath) < 0) {
120                 AUDIO_FUNC_LOGE("snprintf_s failed!");
121                 return HDF_FAILURE;
122             }
123             break;
124         default:
125             uhdfLibPath = HDF_LIBRARY_FULL_PATH(UHDF_PASSTHROUGH_LIB);
126             printf("Input error,Switched to direct loading in for you,");
127             SystemInputFail();
128             if (snprintf_s(resolvedPath, pathLen, pathLen - 1, "%s", uhdfLibPath) < 0) {
129                 return HDF_FAILURE;
130             }
131             break;
132     }
133     return HDF_SUCCESS;
134 }
135