1 /*
2 * Copyright (c) 2021 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 <cctype>
17 #include <climits>
18 #include <hdf_log.h>
19 #include "audio_internal.h"
20 #include "cJSON.h"
21 #include "audio_adapter_info_common.h"
22 namespace OHOS::HDI::Audio_Bluetooth {
23 constexpr const char *AUDIO_ADAPTER_CONFIG = "/vendor/etc/hdfconfig/a2dp_adapter_config.json";
24 constexpr int ADAPTER_NAME_LEN = 32;
25 #define PORT_NAME_LEN ADAPTER_NAME_LEN
26 constexpr int SUPPORT_ADAPTER_NUM_MAX = 8;
27 constexpr int SUPPORT_PORT_NUM_MAX = 3;
28 constexpr int SUPPORT_PORT_ID_MAX = 18;
29 constexpr int CONFIG_FILE_SIZE_MAX = (SUPPORT_ADAPTER_NUM_MAX * 1024); // 8KB
30 constexpr int CONFIG_CHANNEL_COUNT = 2; // two channels
31 constexpr int DECIMAL_SYSTEM = 10;
32
33 int32_t g_adapterNum = 0;
34 struct AudioAdapterDescriptor *g_audioAdapterOut = NULL;
35 struct AudioAdapterDescriptor *g_audioAdapterDescs = NULL;
36 static const char *g_adaptersName[SUPPORT_ADAPTER_NUM_MAX] = {NULL};
37 static const char *g_portsName[SUPPORT_ADAPTER_NUM_MAX][SUPPORT_PORT_NUM_MAX] = {{NULL}};
38
ClearAdaptersAllName(void)39 static void ClearAdaptersAllName(void)
40 {
41 int i, j;
42
43 for (i = 0; i < SUPPORT_ADAPTER_NUM_MAX; i++) {
44 g_adaptersName[i] = NULL;
45 for (j = 0; j < SUPPORT_PORT_NUM_MAX; j++) {
46 g_portsName[i][j] = NULL;
47 }
48 }
49 }
50
AudioAdapterGetConfigOut(void)51 struct AudioAdapterDescriptor *AudioAdapterGetConfigOut(void)
52 {
53 return g_audioAdapterOut;
54 }
55
AudioAdapterGetConfigDescs(void)56 struct AudioAdapterDescriptor *AudioAdapterGetConfigDescs(void)
57 {
58 return g_audioAdapterDescs;
59 }
60
AudioAdapterGetAdapterNum(void)61 int32_t AudioAdapterGetAdapterNum(void)
62 {
63 return g_adapterNum;
64 }
65
AudioAdapterCheckPortFlow(const char * name)66 static int32_t AudioAdapterCheckPortFlow(const char *name)
67 {
68 uint32_t len;
69
70 if (name == NULL) {
71 HDF_LOGE("Invalid parameter!\n");
72
73 return HDF_ERR_INVALID_PARAM;
74 }
75
76 len = strlen(name);
77 if (len == 0) {
78 HDF_LOGE("port name is null!\n");
79
80 return HDF_FAILURE;
81 } else if (len >= PORT_NAME_LEN) {
82 HDF_LOGE("port name is too long!\n");
83
84 return HDF_FAILURE;
85 } else {
86 /* Nothing to do */
87 }
88
89 if (strcmp(name, "AIP") && strcmp(name, "AOP") && strcmp(name, "AIOP")) {
90 HDF_LOGE("Incorrect port name: [ %s ]!\n", name);
91
92 return HDF_FAILURE;
93 }
94
95 return HDF_SUCCESS;
96 }
97
AudioAdapterCheckName(const char * name)98 static int32_t AudioAdapterCheckName(const char *name)
99 {
100 uint32_t len;
101
102 if (name == NULL) {
103 HDF_LOGE("Invalid parameter!\n");
104
105 return HDF_ERR_INVALID_PARAM;
106 }
107
108 len = strlen(name);
109 if (len == 0) {
110 HDF_LOGE("adapter name is null!\n");
111
112 return HDF_FAILURE;
113 } else if (len >= ADAPTER_NAME_LEN) {
114 HDF_LOGE("adapter name is too long!\n");
115
116 return HDF_FAILURE;
117 } else {
118 /* Nothing to do */
119 }
120
121 if (!isalpha(*name++)) { // Names must begin with a letter
122 HDF_LOGE("The adapter name of the illegal!\n");
123
124 return HDF_FAILURE;
125 }
126
127 while (*name != '\0') {
128 if (*name == '_') {
129 name++;
130 continue;
131 }
132
133 if (!isalnum(*name++)) {
134 HDF_LOGE("The adapter name of the illegal!\n");
135
136 return HDF_FAILURE;
137 }
138 }
139
140 return HDF_SUCCESS;
141 }
142
AudioAdapterExist(const char * adapterName)143 int32_t AudioAdapterExist(const char *adapterName)
144 {
145 if (adapterName == NULL) {
146 HDF_LOGE("Invalid parameter!\n");
147
148 return HDF_ERR_INVALID_PARAM;
149 }
150 if (g_audioAdapterDescs == NULL || g_adapterNum <= 0 || g_adapterNum > SUPPORT_ADAPTER_NUM_MAX) {
151 HDF_LOGE("no adapter info");
152
153 return HDF_FAILURE;
154 }
155 for (int i = 0; i < g_adapterNum; i++) {
156 if (strcmp(adapterName, g_audioAdapterDescs[i].adapterName) == 0) {
157 return HDF_SUCCESS;
158 }
159 }
160
161 return HDF_FAILURE;
162 }
163
AudioAdapterJudegReleaseDescs(const struct AudioAdapterDescriptor * desc)164 static void AudioAdapterJudegReleaseDescs(const struct AudioAdapterDescriptor *desc)
165 {
166 uint32_t portIdx;
167
168 if (desc == NULL) {
169 return;
170 }
171
172 if (desc->adapterName != NULL) {
173 AudioMemFree(reinterpret_cast<void **>(const_cast<char **>(&desc->adapterName)));
174 }
175
176 if (desc->ports != NULL) {
177 portIdx = 0;
178 if (desc->portNum <= 0 || desc->portNum > SUPPORT_PORT_NUM_MAX) {
179 HDF_LOGE("desc->portNum error!\n");
180 AudioMemFree(reinterpret_cast<void **>(const_cast<AudioPort **>(&desc->ports)));
181
182 return;
183 }
184
185 while (portIdx < desc->portNum) {
186 if (desc->ports[portIdx].portName != NULL) {
187 AudioMemFree((void **)&desc->ports[portIdx].portName);
188 }
189 portIdx++;
190 }
191 AudioMemFree(reinterpret_cast<void **>(const_cast<AudioPort **>(&desc->ports)));
192 }
193 }
194
AudioAdapterReleaseDescs(const struct AudioAdapterDescriptor * descs,int32_t adapterNum)195 static void AudioAdapterReleaseDescs(const struct AudioAdapterDescriptor *descs, int32_t adapterNum)
196 {
197 int32_t adapterIdx = 0;
198
199 if (descs == NULL || adapterNum <= 0 || adapterNum > SUPPORT_ADAPTER_NUM_MAX) {
200 return;
201 }
202
203 if (adapterNum > g_adapterNum) {
204 adapterNum = g_adapterNum;
205 }
206
207 while (adapterIdx < adapterNum) {
208 AudioAdapterJudegReleaseDescs(&descs[adapterIdx]);
209 adapterIdx++;
210 }
211
212 AudioMemFree(reinterpret_cast<void **>(const_cast<AudioAdapterDescriptor **>(&descs)));
213 }
214
AudioAdapterGetDir(const char * dir)215 static int32_t AudioAdapterGetDir(const char *dir)
216 {
217 if (dir == NULL) {
218 return HDF_FAILURE;
219 }
220 if (strcmp(dir, "PORT_OUT") == 0) {
221 return PORT_OUT;
222 } else if (strcmp(dir, "PORT_IN") == 0) {
223 return PORT_IN;
224 } else if (strcmp(dir, "PORT_OUT_IN") == 0) {
225 return PORT_OUT_IN;
226 } else {
227 return HDF_FAILURE;
228 }
229 }
230
AudioAdaptersGetArraySize(const cJSON * cJsonObj,int * size)231 static int32_t AudioAdaptersGetArraySize(const cJSON *cJsonObj, int *size)
232 {
233 int adapterArraySize;
234
235 if (cJsonObj == NULL || size == NULL) {
236 HDF_LOGE("Invalid parameter!\n");
237
238 return HDF_ERR_INVALID_PARAM;
239 }
240
241 /* Follow the new adapterNum by the number of actual parses */
242 adapterArraySize = cJSON_GetArraySize(cJsonObj);
243 if (adapterArraySize <= 0) {
244 HDF_LOGE("Failed to get JSON array size!\n");
245
246 return HDF_FAILURE;
247 }
248 *size = adapterArraySize;
249
250 return HDF_SUCCESS;
251 }
252
AudioAdapterParsePort(struct AudioPort * info,const cJSON * port)253 static int32_t AudioAdapterParsePort(struct AudioPort *info, const cJSON *port)
254 {
255 int32_t ret;
256 uint32_t tmpId;
257 cJSON *portDir = NULL;
258 cJSON *portID = NULL;
259 cJSON *portName = NULL;
260
261 if (info == NULL || port == NULL) {
262 HDF_LOGE("Invalid parameter!\n");
263
264 return HDF_ERR_INVALID_PARAM;
265 }
266
267 portDir = cJSON_GetObjectItem(port, "dir");
268 if (portDir == NULL || portDir->valuestring == NULL) {
269 return HDF_FAILURE;
270 }
271 ret = AudioAdapterGetDir(portDir->valuestring);
272 if (ret == HDF_FAILURE) {
273 HDF_LOGE("port dir error!\n");
274
275 return ret;
276 }
277 info->dir = (AudioPortDirection)ret;
278
279 portID = cJSON_GetObjectItem(port, "id");
280 if (portID == NULL) {
281 return HDF_FAILURE;
282 }
283 tmpId = portID->valueint;
284 if (tmpId < 0 || tmpId > SUPPORT_PORT_ID_MAX) {
285 HDF_LOGE("portID error!\n");
286
287 return HDF_FAILURE;
288 }
289 info->portId = (uint32_t)tmpId;
290
291 portName = cJSON_GetObjectItem(port, "name");
292 if (portName == NULL || portName->valuestring == NULL) {
293 return HDF_FAILURE;
294 }
295 ret = AudioAdapterCheckPortFlow(portName->valuestring);
296 if (ret != HDF_SUCCESS) {
297 HDF_LOGE("Port name error!\n");
298
299 return ret;
300 }
301 info->portName = static_cast<char *>(calloc(1, PORT_NAME_LEN));
302 if (info->portName == NULL) {
303 HDF_LOGE("Out of memory\n");
304
305 return HDF_ERR_MALLOC_FAIL;
306 }
307 ret = memcpy_s(static_cast<void *>(const_cast<char *>(info->portName)), PORT_NAME_LEN,
308 portName->valuestring, strlen(portName->valuestring));
309 if (ret != EOK) {
310 HDF_LOGE("memcpy_s port name fail");
311
312 return HDF_FAILURE;
313 }
314
315 return HDF_SUCCESS;
316 }
317
AudioAdapterParsePorts(struct AudioAdapterDescriptor * desc,const cJSON * adapter)318 static int32_t AudioAdapterParsePorts(struct AudioAdapterDescriptor *desc, const cJSON *adapter)
319 {
320 uint32_t i;
321 int32_t ret, tmpNum;
322 cJSON *adapterPort = NULL;
323 int32_t realSize = 0;
324 if (desc == NULL || adapter == NULL) {
325 HDF_LOGE("Invalid parameter!\n");
326
327 return HDF_ERR_INVALID_PARAM;
328 }
329 cJSON *adapterPortNum = cJSON_GetObjectItem(adapter, "portnum");
330 if (adapterPortNum == NULL) {
331 return HDF_FAILURE;
332 }
333 tmpNum = cJSON_GetNumberValue(adapterPortNum);
334 if (tmpNum <= 0 || tmpNum > SUPPORT_PORT_NUM_MAX) {
335 HDF_LOGE("portnum error!\n");
336
337 return HDF_FAILURE;
338 }
339 desc->portNum = (uint32_t)tmpNum;
340
341 cJSON *adapterPorts = cJSON_GetObjectItem(adapter, "port");
342 if (adapterPorts == NULL) {
343 return HDF_FAILURE;
344 }
345 ret = AudioAdaptersGetArraySize(adapterPorts, &realSize);
346 if (ret != HDF_SUCCESS || realSize != (int)(desc->portNum)) {
347 HDF_LOGE("realSize = %d, portNum = %d.\n", realSize, desc->portNum);
348 HDF_LOGE("The defined portnum does not match the actual portnum!\n");
349
350 return HDF_FAILURE;
351 }
352
353 desc->ports = reinterpret_cast<struct AudioPort *>(calloc(1, desc->portNum * sizeof(struct AudioPort)));
354 if (desc->ports == NULL) {
355 HDF_LOGE("Out of memory!\n");
356
357 return HDF_ERR_MALLOC_FAIL;
358 }
359 for (i = 0; i < desc->portNum; i++) {
360 adapterPort = cJSON_GetArrayItem(adapterPorts, i);
361 if (adapterPort) {
362 ret = AudioAdapterParsePort(&desc->ports[i], adapterPort);
363 if (ret != HDF_SUCCESS) {
364 return ret;
365 }
366 }
367 }
368 return HDF_SUCCESS;
369 }
370
AudioAdapterParseAdapter(struct AudioAdapterDescriptor * desc,const cJSON * adapter)371 static int32_t AudioAdapterParseAdapter(struct AudioAdapterDescriptor *desc,
372 const cJSON *adapter)
373 {
374 int32_t ret;
375
376 if (desc == NULL || adapter == NULL) {
377 HDF_LOGE("Invalid parameter!\n");
378
379 return HDF_ERR_INVALID_PARAM;
380 }
381
382 cJSON *adapterName = cJSON_GetObjectItem(adapter, "name");
383 if (adapterName == NULL || adapterName->valuestring == NULL) {
384 return HDF_FAILURE;
385 }
386 ret = AudioAdapterCheckName(adapterName->valuestring);
387 if (ret != HDF_SUCCESS) {
388 HDF_LOGE("The Adapter name is incorrect!\n");
389
390 return ret;
391 }
392
393 desc->adapterName = static_cast<char *>(calloc(1, ADAPTER_NAME_LEN));
394 if (desc->adapterName == NULL) {
395 HDF_LOGE("Out of memory!\n");
396
397 return HDF_ERR_MALLOC_FAIL;
398 }
399 ret = memcpy_s(static_cast<void *>(const_cast<char *>(desc->adapterName)), ADAPTER_NAME_LEN,
400 adapterName->valuestring, strlen(adapterName->valuestring));
401 if (ret != EOK) {
402 HDF_LOGE("memcpy_s adapter name fail!\n");
403
404 return HDF_FAILURE;
405 }
406
407 ret = AudioAdapterParsePorts(desc, adapter);
408 if (ret != HDF_SUCCESS) {
409 return ret;
410 }
411
412 return HDF_SUCCESS;
413 }
414
AudioAdaptersGetConfig(const char * fpath)415 static char *AudioAdaptersGetConfig(const char *fpath)
416 {
417 char *pJsonStr = NULL;
418
419 if (fpath == NULL) {
420 /* The file path is bad or unreadable */
421 return NULL;
422 }
423 if (access(fpath, F_OK | R_OK)) {
424 return NULL;
425 }
426 FILE *fp = fopen(fpath, "r");
427 if (fp == NULL) {
428 HDF_LOGE("Can not open config file [ %s ].\n", fpath);
429 return NULL;
430 }
431 if (fseek(fp, 0, SEEK_END) != 0) {
432 fclose(fp);
433 return NULL;
434 }
435 int32_t jsonStrSize = ftell(fp);
436 if (jsonStrSize <= 0) {
437 fclose(fp);
438 return NULL;
439 }
440 rewind(fp);
441 if (jsonStrSize > CONFIG_FILE_SIZE_MAX) {
442 HDF_LOGE("The configuration file is too large to load!\n");
443 fclose(fp);
444 return NULL;
445 }
446 pJsonStr = static_cast<char *>(calloc(1, (uint32_t)jsonStrSize));
447 if (pJsonStr == NULL) {
448 fclose(fp);
449 return NULL;
450 }
451 if (fread(pJsonStr, jsonStrSize, 1, fp) != 1) {
452 HDF_LOGE("read to file fail!");
453 fclose(fp);
454 AudioMemFree(reinterpret_cast<void **>(&pJsonStr));
455 return NULL;
456 }
457 if (fclose(fp) != 0) {
458 HDF_LOGE("close fp fail!");
459 }
460 return pJsonStr;
461 }
462
AudioAdaptersGetConfigToJsonObj(const char * fpath)463 cJSON *AudioAdaptersGetConfigToJsonObj(const char *fpath)
464 {
465 char *pJsonStr = AudioAdaptersGetConfig(fpath);
466 if (pJsonStr == NULL) {
467 return NULL;
468 }
469 cJSON *cJsonObj = cJSON_Parse(pJsonStr);
470 if (cJsonObj == NULL) {
471 AudioMemFree(reinterpret_cast<void **>(&pJsonStr));
472 return NULL;
473 }
474 AudioMemFree(reinterpret_cast<void **>(&pJsonStr));
475 cJSON *adapterNum = cJSON_GetObjectItem(cJsonObj, "adapterNum");
476 if (adapterNum == NULL) {
477 cJSON_Delete(cJsonObj);
478 return NULL;
479 }
480 g_adapterNum = adapterNum->valueint;
481 if (g_adapterNum <= 0 || g_adapterNum > SUPPORT_ADAPTER_NUM_MAX) {
482 HDF_LOGE("Adapter number error!\n");
483 cJSON_Delete(cJsonObj);
484 return NULL;
485 }
486 return cJsonObj;
487 }
488
AudioAdaptersSetAdapter(struct AudioAdapterDescriptor ** descs,int32_t adapterNum,const cJSON * adaptersObj)489 static int32_t AudioAdaptersSetAdapter(struct AudioAdapterDescriptor **descs,
490 int32_t adapterNum, const cJSON *adaptersObj)
491 {
492 int32_t i, ret;
493 cJSON *adapterObj = NULL;
494
495 if (descs == NULL || adaptersObj == NULL ||
496 adapterNum <= 0 || adapterNum > SUPPORT_ADAPTER_NUM_MAX) {
497 HDF_LOGE("Invalid parameter!\n");
498
499 return HDF_ERR_INVALID_PARAM;
500 }
501 if (*descs != NULL) {
502 /* Existing content is no longer assigned twice */
503 return HDF_SUCCESS;
504 }
505
506 *descs = reinterpret_cast<struct AudioAdapterDescriptor *>(calloc(1,
507 adapterNum * sizeof(struct AudioAdapterDescriptor)));
508 if (*descs == NULL) {
509 HDF_LOGE("calloc g_audioAdapterDescs failed");
510
511 return HDF_ERR_MALLOC_FAIL;
512 }
513
514 for (i = 0; i < adapterNum; i++) {
515 adapterObj = cJSON_GetArrayItem(adaptersObj, i);
516 if (adapterObj) {
517 ret = AudioAdapterParseAdapter(&(*descs)[i], adapterObj);
518 if (ret != HDF_SUCCESS) {
519 AudioAdapterReleaseDescs(*descs, adapterNum);
520 *descs = NULL;
521
522 return HDF_FAILURE;
523 }
524 }
525 }
526
527 return HDF_SUCCESS;
528 }
529
AudioAdaptersNamesRepair(void)530 static void AudioAdaptersNamesRepair(void)
531 {
532 int i, realNum;
533
534 if (g_audioAdapterOut == NULL ||
535 g_audioAdapterDescs == NULL || g_adapterNum <= 0) {
536 return;
537 }
538
539 realNum = (g_adapterNum < SUPPORT_ADAPTER_NUM_MAX) ? g_adapterNum : SUPPORT_ADAPTER_NUM_MAX;
540 for (i = 0; i < realNum; i++) {
541 if (g_adaptersName[i] == NULL) {
542 return;
543 }
544
545 if (strcmp(g_audioAdapterOut[i].adapterName, g_audioAdapterDescs[i].adapterName)) {
546 /* Retrieve the location of the port name */
547 g_audioAdapterOut[i].adapterName = g_adaptersName[i];
548 }
549 }
550 }
551
AudioPortsNamesRepair(void)552 static void AudioPortsNamesRepair(void)
553 {
554 int i, j, adapterNum, portNum;
555
556 if (g_audioAdapterOut == NULL ||
557 g_audioAdapterDescs == NULL || g_adapterNum <= 0) {
558 return;
559 }
560
561 adapterNum = (g_adapterNum < SUPPORT_ADAPTER_NUM_MAX) ? g_adapterNum : SUPPORT_ADAPTER_NUM_MAX;
562 for (i = 0; i < adapterNum; i++) {
563 portNum = (g_audioAdapterOut[i].portNum < SUPPORT_PORT_NUM_MAX) ?
564 g_audioAdapterOut[i].portNum : SUPPORT_PORT_NUM_MAX;
565 for (j = 0; j < portNum; j++) {
566 if (g_portsName[i][j] == NULL) {
567 return;
568 }
569 if (strcmp(g_audioAdapterOut[i].ports[j].portName, g_audioAdapterDescs[i].ports[j].portName)) {
570 /* Retrieve the location of the sound card name */
571 g_audioAdapterOut[i].ports[j].portName = g_portsName[i][j];
572 }
573 }
574 }
575 }
576
AudioAdaptersNamesRecord(void)577 static void AudioAdaptersNamesRecord(void)
578 {
579 int i, currentNum;
580
581 if (g_audioAdapterOut == NULL ||
582 g_audioAdapterDescs == NULL || g_adapterNum <= 0) {
583 return;
584 }
585
586 currentNum = (g_adapterNum < SUPPORT_ADAPTER_NUM_MAX) ? g_adapterNum : SUPPORT_ADAPTER_NUM_MAX;
587 for (i = 0; i < currentNum; i++) {
588 /* Record the location of the sound card name */
589 g_adaptersName[i] = g_audioAdapterOut[i].adapterName;
590 }
591 }
592
AudioPortsNamesRecord(void)593 static void AudioPortsNamesRecord(void)
594 {
595 int i, j, adapterCurNum, portCurNum;
596
597 if (g_audioAdapterOut == NULL || g_audioAdapterDescs == NULL || g_adapterNum <= 0) {
598 return;
599 }
600
601 adapterCurNum = (g_adapterNum < SUPPORT_ADAPTER_NUM_MAX) ? g_adapterNum : SUPPORT_ADAPTER_NUM_MAX;
602 for (i = 0; i < adapterCurNum; i++) {
603 portCurNum = (g_audioAdapterOut[i].portNum < SUPPORT_PORT_NUM_MAX) ?
604 g_audioAdapterOut[i].portNum : SUPPORT_PORT_NUM_MAX;
605 for (j = 0; j < portCurNum; j++) {
606 /* Record the location of the port name */
607 g_portsName[i][j] = g_audioAdapterOut[i].ports[j].portName;
608 }
609 }
610 }
611
AudioAdaptersForUser(struct AudioAdapterDescriptor ** descs,int * size)612 int32_t AudioAdaptersForUser(struct AudioAdapterDescriptor **descs, int *size)
613 {
614 int32_t realSize = -1;
615
616 if (descs == NULL || size == NULL) {
617 return HDF_ERR_INVALID_PARAM;
618 }
619 if (g_audioAdapterDescs != NULL && g_audioAdapterOut != NULL &&
620 g_adapterNum > 0 && g_adapterNum <= SUPPORT_ADAPTER_NUM_MAX) {
621 AudioAdaptersNamesRepair();
622 AudioPortsNamesRepair();
623 /* Existing content is no longer assigned twice */
624 *descs = g_audioAdapterOut;
625 *size = g_adapterNum;
626
627 return HDF_SUCCESS;
628 }
629 cJSON *cJsonObj = AudioAdaptersGetConfigToJsonObj(AUDIO_ADAPTER_CONFIG);
630 if (cJsonObj == NULL) {
631 return HDF_FAILURE;
632 }
633 cJSON *adaptersObj = cJSON_GetObjectItem(cJsonObj, "adapters");
634 if (adaptersObj == NULL) {
635 cJSON_Delete(cJsonObj);
636
637 return HDF_FAILURE;
638 }
639 if (AudioAdaptersGetArraySize(adaptersObj, &realSize) != HDF_SUCCESS || realSize != g_adapterNum) {
640 HDF_LOGE("realSize = %d, adaptersNum = %d.\n", realSize, g_adapterNum);
641 HDF_LOGE("The defined adaptersnum does not match the actual adapters!\n");
642 g_adapterNum = 0;
643 cJSON_Delete(cJsonObj);
644
645 return HDF_FAILURE;
646 }
647 if (AudioAdaptersSetAdapter(&g_audioAdapterDescs, g_adapterNum, adaptersObj) != HDF_SUCCESS) {
648 g_adapterNum = 0;
649 cJSON_Delete(cJsonObj);
650
651 return HDF_FAILURE;
652 }
653 if (AudioAdaptersSetAdapter(&g_audioAdapterOut, g_adapterNum, adaptersObj) != HDF_SUCCESS) {
654 /* g_audioAdapterOut failure also releases g_audioAdapterDescs */
655 AudioAdapterReleaseDescs(g_audioAdapterDescs, g_adapterNum);
656 ClearAdaptersAllName();
657 g_audioAdapterDescs = NULL;
658 g_adapterNum = 0;
659 cJSON_Delete(cJsonObj);
660
661 return HDF_FAILURE;
662 }
663 AudioAdaptersNamesRecord();
664 AudioPortsNamesRecord();
665 *descs = g_audioAdapterOut;
666 *size = g_adapterNum;
667 cJSON_Delete(cJsonObj);
668
669 return HDF_SUCCESS;
670 }
671
672 static AudioFormat g_formatIdZero = AUDIO_FORMAT_TYPE_PCM_16_BIT;
HdmiPortInit(struct AudioPort portIndex,struct AudioPortCapability * capabilityIndex)673 int32_t HdmiPortInit(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex)
674 {
675 if (capabilityIndex == NULL) {
676 HDF_LOGE("capabilityIndex Is NULL");
677 return HDF_FAILURE;
678 }
679 capabilityIndex->hardwareMode = true;
680 capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
681 capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
682 capabilityIndex->deviceType = portIndex.dir;
683 capabilityIndex->deviceId = PIN_OUT_SPEAKER;
684 capabilityIndex->formatNum = 1;
685 capabilityIndex->formats = &g_formatIdZero;
686 capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000 | AUDIO_SAMPLE_RATE_MASK_24000;
687 capabilityIndex->subPortsNum = 1;
688 capabilityIndex->subPorts = reinterpret_cast<struct AudioSubPortCapability *>(calloc(capabilityIndex->subPortsNum,
689 sizeof(struct AudioSubPortCapability)));
690 if (capabilityIndex->subPorts == NULL) {
691 HDF_LOGE("The pointer is null!");
692 return HDF_FAILURE;
693 }
694 capabilityIndex->subPorts->portId = portIndex.portId;
695 capabilityIndex->subPorts->desc = portIndex.portName;
696 capabilityIndex->subPorts->mask = PORT_PASSTHROUGH_LPCM;
697 return HDF_SUCCESS;
698 }
699
FormatToBits(AudioFormat format,uint32_t * formatBits)700 int32_t FormatToBits(AudioFormat format, uint32_t *formatBits)
701 {
702 if (formatBits == NULL) {
703 return HDF_FAILURE;
704 }
705 switch (format) {
706 case AUDIO_FORMAT_TYPE_PCM_32_BIT:
707 *formatBits = BIT_NUM_32;
708 return HDF_SUCCESS;
709 case AUDIO_FORMAT_TYPE_PCM_24_BIT:
710 *formatBits = BIT_NUM_24;
711 return HDF_SUCCESS;
712 case AUDIO_FORMAT_TYPE_PCM_16_BIT:
713 *formatBits = BIT_NUM_16;
714 return HDF_SUCCESS;
715 case AUDIO_FORMAT_TYPE_PCM_8_BIT:
716 *formatBits = BIT_NUM_8;
717 return HDF_SUCCESS;
718 default:
719 return HDF_ERR_NOT_SUPPORT;
720 }
721 }
722
BitsToFormat(AudioFormat * format,long formatBits)723 int32_t BitsToFormat(AudioFormat *format, long formatBits)
724 {
725 if (format == NULL) {
726 return HDF_FAILURE;
727 }
728 switch (formatBits) {
729 case BIT_NUM_32:
730 *format = AUDIO_FORMAT_TYPE_PCM_32_BIT;
731 return HDF_SUCCESS;
732 case BIT_NUM_24:
733 *format = AUDIO_FORMAT_TYPE_PCM_24_BIT;
734 return HDF_SUCCESS;
735 case BIT_NUM_16:
736 *format = AUDIO_FORMAT_TYPE_PCM_16_BIT;
737 return HDF_SUCCESS;
738 case BIT_NUM_8:
739 *format = AUDIO_FORMAT_TYPE_PCM_8_BIT;
740 return HDF_SUCCESS;
741 default:
742 return HDF_ERR_NOT_SUPPORT;
743 }
744 }
745
CheckAttrRoute(long param)746 int32_t CheckAttrRoute(long param)
747 {
748 if (param < DEEP_BUFF || param > LOW_LATRNCY) {
749 return HDF_FAILURE;
750 }
751 return HDF_SUCCESS;
752 }
753
CheckAttrChannel(unsigned long param)754 int32_t CheckAttrChannel(unsigned long param)
755 {
756 if (param != 1 && param != 2) { // channel 1 and 2
757 return HDF_FAILURE;
758 }
759 return HDF_SUCCESS;
760 }
761
TransferRoute(const char * value,int32_t * route)762 int32_t TransferRoute(const char *value, int32_t *route)
763 {
764 if (value == NULL || route == NULL) {
765 return HDF_FAILURE;
766 }
767 char *endptr = NULL;
768 errno = 0;
769 long tempRoute = strtol(value, &endptr, DECIMAL_SYSTEM);
770 if ((errno == ERANGE && (tempRoute == LONG_MAX || tempRoute == LONG_MIN)) || (errno != 0 && tempRoute == 0)) {
771 return HDF_FAILURE;
772 }
773 int32_t ret = CheckAttrRoute(tempRoute);
774 if (ret == 0) {
775 *route = tempRoute;
776 }
777 return ret;
778 }
779
TransferFormat(const char * value,int32_t * format)780 int32_t TransferFormat(const char *value, int32_t *format)
781 {
782 if (value == NULL || format == NULL) {
783 return HDF_FAILURE;
784 }
785 char *endptr = NULL;
786 errno = 0;
787 long tempFormat = strtol(value, &endptr, DECIMAL_SYSTEM);
788 if ((errno == ERANGE && (tempFormat == LONG_MAX || tempFormat == LONG_MIN)) || (errno != 0 && tempFormat == 0)) {
789 return HDF_FAILURE;
790 }
791 AudioFormat audioFormat;
792 int32_t ret = BitsToFormat(&audioFormat, tempFormat);
793 if (ret == HDF_SUCCESS) {
794 ret = CheckAttrFormat(audioFormat);
795 if (ret == 0) {
796 *format = audioFormat;
797 }
798 }
799 return ret;
800 }
801
TransferChannels(const char * value,uint32_t * channels)802 int32_t TransferChannels(const char *value, uint32_t *channels)
803 {
804 if (value == NULL || channels == NULL) {
805 return HDF_FAILURE;
806 }
807 char *endptr = NULL;
808 errno = 0;
809 unsigned long tempChannels = strtoul(value, &endptr, DECIMAL_SYSTEM);
810 if ((errno == ERANGE && (tempChannels == ULONG_MAX)) || (errno != 0 && tempChannels == 0)) {
811 return HDF_FAILURE;
812 }
813 int32_t ret = CheckAttrChannel(tempChannels);
814 if (ret == 0) {
815 *channels = tempChannels;
816 }
817 return ret;
818 }
819
TransferFrames(const char * value,uint64_t * frames)820 int32_t TransferFrames(const char *value, uint64_t *frames)
821 {
822 if (value == NULL || frames == NULL) {
823 return HDF_FAILURE;
824 }
825 char *endptr = NULL;
826 errno = 0;
827 uint64_t tempFrames = strtoull(value, &endptr, 10);
828 if ((errno == ERANGE && (tempFrames == ULLONG_MAX)) || (errno != 0 && tempFrames == 0)) {
829 return HDF_FAILURE;
830 } else {
831 *frames = tempFrames;
832 return HDF_SUCCESS;
833 }
834 }
835
TransferSampleRate(const char * value,uint32_t * sampleRate)836 int32_t TransferSampleRate(const char *value, uint32_t *sampleRate)
837 {
838 if (value == NULL || sampleRate == NULL) {
839 return HDF_FAILURE;
840 }
841 char *endptr = NULL;
842 errno = 0;
843 unsigned long tempSampleRate = strtoul(value, &endptr, DECIMAL_SYSTEM);
844 if ((errno == ERANGE && (tempSampleRate == ULONG_MAX)) || (errno != 0 && tempSampleRate == 0)) {
845 return HDF_FAILURE;
846 }
847 int32_t ret = CheckAttrSamplingRate(tempSampleRate);
848 if (ret == 0) {
849 *sampleRate = tempSampleRate;
850 }
851 return ret;
852 }
853
854 #ifdef A2DP_HDI_SERVICE
TransferA2dpSuspended(const char * value,uint32_t * result)855 int32_t TransferA2dpSuspended(const char *value, uint32_t *result)
856 {
857 if (value == NULL || result == NULL) {
858 return HDF_FAILURE;
859 }
860 char *endptr = NULL;
861 errno = 0;
862 unsigned long toSuspend = strtoul(value, &endptr, DECIMAL_SYSTEM);
863 if (errno == ERANGE) {
864 return HDF_FAILURE;
865 }
866 if (toSuspend != 0 && toSuspend != 1) {
867 HDF_LOGE("TransferA2dpSuspended, wrong value");
868 return HDF_FAILURE;
869 }
870 *result = toSuspend;
871 return HDF_SUCCESS;
872 }
873 #endif
874
KeyValueListToMap(const char * keyValueList,struct ParamValMap mParamValMap[],int32_t * count)875 int32_t KeyValueListToMap(const char *keyValueList, struct ParamValMap mParamValMap[], int32_t *count)
876 {
877 if (keyValueList == NULL || mParamValMap == NULL || count == NULL) {
878 return HDF_FAILURE;
879 }
880 int i = 0;
881 char *mParaMap[MAP_MAX];
882 char buffer[ERROR_REASON_DESC_LEN] = {0};
883 errno_t ret = strcpy_s(buffer, ERROR_REASON_DESC_LEN, keyValueList);
884 if (ret != EOK) {
885 HDF_LOGE("strcpy_s failed!");
886 return HDF_FAILURE;
887 }
888 char *tempBuf = buffer;
889 char *outPtr = NULL;
890 char *inPtr = NULL;
891 while (i < MAP_MAX && ((mParaMap[i] = strtok_r(tempBuf, ";", &outPtr)) != NULL)) {
892 tempBuf = mParaMap[i];
893 if ((mParaMap[i] = strtok_r(tempBuf, "=", &inPtr)) != NULL) {
894 ret = strncpy_s(mParamValMap[i].key, EXTPARAM_LEN - 1, mParaMap[i], strlen(mParaMap[i]) + 1);
895 if (ret != 0) {
896 return HDF_FAILURE;
897 }
898 tempBuf = NULL;
899 }
900 if ((mParaMap[i] = strtok_r(tempBuf, "=", &inPtr)) != NULL) {
901 ret = strncpy_s(mParamValMap[i].value, EXTPARAM_LEN - 1, mParaMap[i], strlen(mParaMap[i]) + 1);
902 if (ret != 0) {
903 return HDF_FAILURE;
904 }
905 tempBuf = NULL;
906 }
907 tempBuf = NULL;
908 i++;
909 }
910 *count = i;
911 return HDF_SUCCESS;
912 }
913
AddElementToList(char * keyValueList,int32_t listLenth,const char * key,void * value)914 int32_t AddElementToList(char *keyValueList, int32_t listLenth, const char *key, void *value)
915 {
916 if (keyValueList == NULL || key == NULL || value == NULL) {
917 return HDF_FAILURE;
918 }
919 int32_t ret = HDF_FAILURE;
920 char strValue[MAP_MAX] = { 0 };
921 if (strcmp(key, AUDIO_ATTR_PARAM_ROUTE) == 0) {
922 ret = sprintf_s(strValue, sizeof(strValue), "%s=%d;", key, *(reinterpret_cast<int32_t *>(value)));
923 } else if (strcmp(key, AUDIO_ATTR_PARAM_FORMAT) == 0) {
924 uint32_t formatBits = 0;
925 ret = FormatToBits((AudioFormat)(*(reinterpret_cast<int32_t *>(value))), &formatBits);
926 if (ret == 0) {
927 ret = sprintf_s(strValue, sizeof(strValue), "%s=%u;", key, formatBits);
928 }
929 } else if (strcmp(key, AUDIO_ATTR_PARAM_CHANNELS) == 0) {
930 ret = sprintf_s(strValue, sizeof(strValue), "%s=%u;", key, *(reinterpret_cast<uint32_t *>(value)));
931 } else if (strcmp(key, AUDIO_ATTR_PARAM_FRAME_COUNT) == 0) {
932 ret = sprintf_s(strValue, sizeof(strValue), "%s=%llu;", key, *(reinterpret_cast<uint64_t *>(value)));
933 } else if (strcmp(key, AUDIO_ATTR_PARAM_SAMPLING_RATE) == 0) {
934 ret = sprintf_s(strValue, sizeof(strValue), "%s=%u", key, *(reinterpret_cast<uint32_t *>(value)));
935 } else {
936 HDF_LOGE("NO this key correspond value!");
937 return HDF_FAILURE;
938 }
939 if (ret < 0) {
940 HDF_LOGE("sprintf_s failed!");
941 return HDF_FAILURE;
942 }
943 ret = strncat_s(keyValueList, listLenth, strValue, strlen(strValue));
944 if (ret < 0) {
945 HDF_LOGE("strcat_s failed!");
946 return HDF_FAILURE;
947 }
948 return HDF_SUCCESS;
949 }
950
SetExtParam(const char * key,const char * value,struct ExtraParams * mExtraParams)951 int32_t SetExtParam(const char *key, const char *value, struct ExtraParams *mExtraParams)
952 {
953 if (key == NULL || value == NULL || mExtraParams == NULL) {
954 return HDF_FAILURE;
955 }
956 HDF_LOGI("SetExtParam, key is:%{public}s", key);
957 int ret = HDF_FAILURE;
958 #ifdef A2DP_HDI_SERVICE
959 if (strcmp(key, A2DP_SUSPEND) == 0) {
960 uint32_t result = 0;
961 ret = TransferA2dpSuspended(value, &result);
962 if (ret < 0) {
963 return HDF_FAILURE;
964 }
965 mExtraParams->audioStreamCtl = result;
966 }
967 #endif
968 if (strcmp(key, AUDIO_ATTR_PARAM_ROUTE) == 0) {
969 int32_t route = 0;
970 ret = TransferRoute(value, &route);
971 if (ret < 0) {
972 return HDF_FAILURE;
973 }
974 mExtraParams->route = route;
975 } else if (strcmp(key, AUDIO_ATTR_PARAM_FORMAT) == 0) {
976 int32_t format = 0;
977 ret = TransferFormat(value, &format);
978 if (ret < 0) {
979 return HDF_FAILURE;
980 }
981 mExtraParams->format = format;
982 } else if (strcmp(key, AUDIO_ATTR_PARAM_CHANNELS) == 0) {
983 uint32_t channels = 0;
984 ret = TransferChannels(value, &channels);
985 if (ret < 0) {
986 return HDF_FAILURE;
987 }
988 mExtraParams->channels = channels;
989 } else if (strcmp(key, AUDIO_ATTR_PARAM_FRAME_COUNT) == 0) {
990 uint64_t frames = 0;
991 ret = TransferFrames(value, &frames);
992 if (ret < 0) {
993 return HDF_FAILURE;
994 }
995 mExtraParams->frames = frames;
996 mExtraParams->flag = true;
997 } else if (strcmp(key, AUDIO_ATTR_PARAM_SAMPLING_RATE) == 0) {
998 uint32_t sampleRate = 0;
999 ret = TransferSampleRate(value, &sampleRate);
1000 if (ret < 0) {
1001 return HDF_FAILURE;
1002 }
1003 mExtraParams->sampleRate = sampleRate;
1004 } else {
1005 HDF_LOGE("NO this key correspond value or value is invalid!");
1006 return HDF_FAILURE;
1007 }
1008 return ret;
1009 }
1010
GetErrorReason(int reason,char * reasonDesc)1011 int32_t GetErrorReason(int reason, char *reasonDesc)
1012 {
1013 int32_t ret;
1014 if (reasonDesc == NULL) {
1015 return HDF_FAILURE;
1016 }
1017 switch (reason) {
1018 case HDF_FAILURE:
1019 ret = snprintf_s(reasonDesc, ERROR_REASON_DESC_LEN - 1, strlen("NOT SUPPORT") + 1, "%s", "NOT SUPPORT");
1020 break;
1021 case HDF_ERR_NOT_SUPPORT:
1022 ret = snprintf_s(reasonDesc, ERROR_REASON_DESC_LEN - 1, strlen("BUFFER FULL") + 1, "%s", "BUFFER FULL");
1023 break;
1024 default:
1025 ret = snprintf_s(reasonDesc, ERROR_REASON_DESC_LEN - 1, strlen("UNKNOW") + 1, "%s", "UNKNOW");
1026 break;
1027 }
1028 if (ret < 0) {
1029 HDF_LOGE("sprintf_s failed!");
1030 return HDF_FAILURE;
1031 }
1032 return HDF_SUCCESS;
1033 }
1034
AudioSetExtraParams(const char * keyValueList,int32_t * count,struct ExtraParams * mExtraParams,int32_t * sumOk)1035 int32_t AudioSetExtraParams(const char *keyValueList, int32_t *count,
1036 struct ExtraParams *mExtraParams, int32_t *sumOk)
1037 {
1038 if (keyValueList == NULL || count == NULL || mExtraParams == NULL || sumOk == NULL) {
1039 return AUDIO_HAL_ERR_INTERNAL;
1040 }
1041 struct ParamValMap mParamValMap[MAP_MAX];
1042 int32_t ret = KeyValueListToMap(keyValueList, mParamValMap, count);
1043 if (ret < 0) {
1044 HDF_LOGE("Convert to map FAIL!");
1045 return AUDIO_HAL_ERR_INTERNAL;
1046 }
1047 int index = 0;
1048 mExtraParams->route = -1;
1049 mExtraParams->format = -1;
1050 mExtraParams->channels = 0;
1051 mExtraParams->frames = 0;
1052 mExtraParams->sampleRate = 0;
1053 mExtraParams->flag = false;
1054 while (index < *count) {
1055 ret = SetExtParam(mParamValMap[index].key, mParamValMap[index].value, mExtraParams);
1056 if (ret < 0) {
1057 return AUDIO_HAL_ERR_INTERNAL;
1058 } else {
1059 (*sumOk)++;
1060 }
1061 index++;
1062 }
1063 return AUDIO_HAL_SUCCESS;
1064 }
1065 }
1066