blob: 8744dff410170e4a04e017765bbca40326fbec37 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: MediaTekProprietary
2/* MediaTek Inc. (C) 2016. All rights reserved.
3 *
4 * Copyright Statement:
5 * This software/firmware and related documentation ("MediaTek Software") are
6 * protected under relevant copyright laws. The information contained herein is
7 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
8 * the prior written permission of MediaTek inc. and/or its licensors, any
9 * reproduction, modification, use or disclosure of MediaTek Software, and
10 * information contained herein, in whole or in part, shall be strictly
11 * prohibited.
12 */
13
14/*
15 * Description:
16 * Implement ParamUnit related APIs
17 */
18
19#include "AudioParamParserPriv.h"
20
21EXPORT ParamUnit *paramUnitCreate(AudioType *audioType, int id, Param *param) {
22 size_t numOfParam = 0, i = 0;
23 ParamUnit *paramUnit = (ParamUnit *)malloc(sizeof(ParamUnit));
24 if (!paramUnit) {
25 ERR_LOG("malloc fail!\n");
26 return NULL;
27 }
28
29 paramUnit->paramId = id;
30 paramUnit->refCount = 0;
31 paramUnit->audioType = audioType;
32 paramUnit->paramHash = param;
33
34 /* Update param's param unit info */
35 numOfParam = paramUnitGetNumOfParam(paramUnit);
36 for (i = 0; i < numOfParam; i++) {
37 Param *param = paramUnitGetParamByIndex(paramUnit, i);
38 if (param) {
39 param->paramUnit = paramUnit;
40 } else {
41 WARN_LOG("paramUnitGetParamByIndex fail (idx = "APP_SIZE_T_FT")", i);
42 }
43 }
44
45 return paramUnit;
46}
47
48EXPORT ParamUnit *paramUnitClone(ParamUnit *oldParamUnit) {
49 Param *item = NULL;
50 ParamUnit *paramUnit = NULL;
51
52 if (!oldParamUnit) {
53 ERR_LOG("Original ParamUnit is NULL\n");
54 return NULL;
55 }
56
57 paramUnit = (ParamUnit *)malloc(sizeof(ParamUnit));
58 if (!paramUnit) {
59 ERR_LOG("malloc fail!\n");
60 return NULL;
61 }
62
63 paramUnit->paramId = oldParamUnit->paramId;
64 paramUnit->refCount = oldParamUnit->refCount;
65 paramUnit->audioType = oldParamUnit->audioType;
66 paramUnit->paramHash = paramHashClone(oldParamUnit->paramHash);
67
68 /* Update param's param unit info */
69 if (paramUnit->paramHash) {
70 for (item = paramUnit->paramHash; item != NULL; item = item->hh.next) {
71 item->paramUnit = paramUnit;
72 }
73 }
74
75 return paramUnit;
76}
77
78EXPORT void paramUnitRelease(ParamUnit *paramUnit) {
79 if (paramUnit) {
80 /* Free ParamUnit's param hash */
81 if (paramUnit->paramHash) {
82 Param *tmp, *item;
83 HASH_ITER(hh, paramUnit->paramHash, item, tmp) {
84 if (paramUnit->paramHash) {
85 HASH_DEL(paramUnit->paramHash, item);
86 paramRelease(item);
87 }
88 }
89 free(paramUnit->paramHash);
90 }
91 free(paramUnit);
92 }
93}
94
95EXPORT size_t paramUnitGetNumOfParam(ParamUnit *paramUnit) {
96 if (!paramUnit) {
97 ERR_LOG("paramUnit is NULL!\n");
98 return 0;
99 }
100
101 return HASH_COUNT(paramUnit->paramHash);
102}
103
104EXPORT Param *paramUnitGetParamByIndex(ParamUnit *paramUnit, size_t index) {
105 Param *param = NULL;
106 size_t i = 0;
107
108 if (!paramUnit) {
109 ERR_LOG("paramUnit is NULL!\n");
110 return NULL;
111 }
112
113 for (param = paramUnit->paramHash; param ; param = param->hh.next) {
114 if (index == i++) {
115 return param;
116 }
117 }
118
119 return NULL;
120}
121
122EXPORT Param *paramUnitGetParamByName(ParamUnit *paramUnit, const char *name) {
123 Param *param = NULL;
124
125 if (!paramUnit) {
126 ERR_LOG("paramUnit is NULL!\n");
127 return NULL;
128 }
129
130 INFO_LOG("AudioType = %s, name = %s\n", paramUnit->audioType ? paramUnit->audioType->name : "NULL", name);
131
132 HASH_FIND_STR(paramUnit->paramHash, name, param);
133
134 if (param && appDebugLevel <= DEBUG_LEVEL) {
135 utilShowParamValue(param);
136 }
137
138 DEBUG_LOG("name = %s, param data = 0x%p, size = "APP_SIZE_T_FT"\n", name, param ? param->data : NULL, param ? param->arraySize : 0);
139 return param;
140}
141
142
143EXPORT APP_STATUS paramUnitGetFieldVal(ParamUnit *paramUnit, const char *paramName, const char *fieldName, unsigned int *val) {
144 ParamInfo *paramInfo = NULL;
145 FieldInfo *fieldInfo = NULL;
146 Param *param = NULL;
147
148 if (!paramUnit) {
149 ERR_LOG("paramUnit is NULL!\n");
150 return APP_ERROR;
151 }
152
153 /* Query field Info */
154 paramInfo = audioTypeGetParamInfoByName(paramUnit->audioType, paramName);
155 if (!paramInfo) {
156 WARN_LOG("Cannot find paramInfo. (param = %s\n)", paramName);
157 return APP_ERROR;
158 }
159
160 fieldInfo = paramInfoGetFieldInfoByName(paramInfo, fieldName);
161 if (!fieldInfo) {
162 WARN_LOG("Cannot find fieldInfo. (fieldName = %s\n)", fieldName);
163 return APP_ERROR;
164 }
165
166 /* Query param */
167 param = paramUnitGetParamByName(paramUnit, paramName);
168 if (!param) {
169 WARN_LOG("Cannot get param. (name = %s)\n", paramName);
170 return APP_ERROR;
171 }
172
173 /* Query field val */
174 return paramGetFieldVal(param, fieldInfo, val);
175}
176
177EXPORT ParamInfo *paramUnitGetParamInfo(ParamUnit *paramUnit, const char *paramInfoName) {
178 if (!paramUnit) {
179 ERR_LOG("paramUnit is NULL!\n");
180 return NULL;
181 }
182
183 return audioTypeGetParamInfoByName(paramUnit->audioType, paramInfoName);
184}
185
186EXPORT FieldInfo *paramUnitGetFieldInfo(ParamUnit *paramUnit, const char *paramName, const char *fieldName) {
187 ParamInfo *paramInfo = NULL;
188 if (!paramUnit || !paramName || !fieldName) {
189 WARN_LOG("Cannot get field info. (paramUnit id=%d, paramInfoName=%s, fieldInfoName=%s\n)", paramUnit ? paramUnit->paramId : -1, paramName, fieldName);
190 return NULL;
191 }
192
193 paramInfo = audioTypeGetParamInfoByName(paramUnit->audioType, paramName);
194 if (paramInfo) {
195 return paramInfoGetFieldInfoByName(paramInfo, fieldName);
196 } else {
197 return NULL;
198 }
199}