• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <hdf_log.h>
16 #include <hdf_remote_service.h>
17 #include "audio_proxy_common.h"
18 #include "audio_proxy_internal.h"
19 #include "audio_adapter_info_common.h"
20 
21 namespace OHOS::HDI::Audio_Bluetooth {
AudioProxyCommonInitAttrs(struct HdfSBuf * data,const struct AudioSampleAttributes * attrs)22 int32_t AudioProxyCommonInitAttrs(struct HdfSBuf *data, const struct AudioSampleAttributes *attrs)
23 {
24     if (data == NULL || attrs == NULL) {
25         HDF_LOGE("data == NULL || attrs == NULL");
26         return HDF_FAILURE;
27     }
28     uint32_t tempAtrr = (uint32_t)attrs->interleaved;
29     if (!HdfSbufWriteUint32(data, tempAtrr)) {
30         return HDF_FAILURE;
31     }
32     tempAtrr = (uint32_t)attrs->type;
33     if (!HdfSbufWriteUint32(data, tempAtrr)) {
34         return HDF_FAILURE;
35     }
36     if (!HdfSbufWriteUint32(data, attrs->period)) {
37         return HDF_FAILURE;
38     }
39     if (!HdfSbufWriteUint32(data, attrs->frameSize)) {
40         return HDF_FAILURE;
41     }
42     if (!HdfSbufWriteUint32(data, attrs->startThreshold)) {
43         HDF_LOGE("startThreshold Write Fail");
44         return HDF_FAILURE;
45     }
46     if (!HdfSbufWriteUint32(data, attrs->stopThreshold)) {
47         HDF_LOGE("stopThreshold Write Fail");
48         return HDF_FAILURE;
49     }
50     if (!HdfSbufWriteUint32(data, attrs->silenceThreshold)) {
51         return HDF_FAILURE;
52     }
53     tempAtrr = (uint32_t)attrs->isBigEndian;
54     if (!HdfSbufWriteUint32(data, tempAtrr)) {
55         return HDF_FAILURE;
56     }
57     tempAtrr = (uint32_t)attrs->isSignedData;
58     if (!HdfSbufWriteUint32(data, tempAtrr)) {
59         return HDF_FAILURE;
60     }
61     return HDF_SUCCESS;
62 }
63 
AudioProxyCommonInitCreateData(struct HdfSBuf * data,const struct AudioHwAdapter * adapter,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs)64 int32_t AudioProxyCommonInitCreateData(struct HdfSBuf *data, const struct AudioHwAdapter *adapter,
65     const struct AudioDeviceDescriptor *desc, const struct AudioSampleAttributes *attrs)
66 {
67     HDF_LOGI("%{public}s, ", __func__);
68     if (data == NULL || adapter == NULL || desc == NULL || attrs == NULL) {
69         HDF_LOGE("data == NULL || adapter == NULL || desc == NULL || attrs == NULL");
70         return HDF_FAILURE;
71     }
72     int32_t pid = getpid();
73     const char *adapterName = adapter->adapterDescriptor.adapterName;
74     if (adapterName == NULL) {
75         return HDF_FAILURE;
76     }
77     if (!HdfSbufWriteString(data, adapterName)) {
78         return HDF_FAILURE;
79     }
80     if (!HdfSbufWriteInt32(data, pid)) {
81         return HDF_FAILURE;
82     }
83     uint32_t tempAtrr = (uint32_t)attrs->format;
84     if (!HdfSbufWriteUint32(data, tempAtrr)) {
85         return HDF_FAILURE;
86     }
87     if (!HdfSbufWriteUint32(data, attrs->channelCount)) {
88         return HDF_FAILURE;
89     }
90     if (!HdfSbufWriteUint32(data, attrs->sampleRate)) {
91         return HDF_FAILURE;
92     }
93     if (AudioProxyCommonInitAttrs(data, attrs) < 0) {
94         return HDF_FAILURE;
95     }
96     if (!HdfSbufWriteUint32(data, desc->portId)) {
97         return HDF_FAILURE;
98     }
99     uint32_t tempDesc = (uint32_t)desc->pins;
100     if (!HdfSbufWriteUint32(data, tempDesc)) {
101         return HDF_FAILURE;
102     }
103     return HDF_SUCCESS;
104 }
105 
GetAudioProxyRenderFunc(struct AudioHwRender * hwRender)106 int32_t GetAudioProxyRenderFunc(struct AudioHwRender *hwRender)
107 {
108     if (hwRender == NULL) {
109         return HDF_FAILURE;
110     }
111     hwRender->common.control.Start = AudioProxyRenderStart;
112     hwRender->common.control.Stop = AudioProxyRenderStop;
113     hwRender->common.control.Pause = AudioProxyRenderPause;
114     hwRender->common.control.Resume = AudioProxyRenderResume;
115     hwRender->common.control.Flush = AudioProxyRenderFlush;
116     hwRender->common.control.TurnStandbyMode = AudioProxyRenderTurnStandbyMode;
117     hwRender->common.control.AudioDevDump = AudioProxyRenderAudioDevDump;
118     hwRender->common.attr.GetFrameSize = AudioProxyRenderGetFrameSize;
119     hwRender->common.attr.GetFrameCount = AudioProxyRenderGetFrameCount;
120     hwRender->common.attr.SetSampleAttributes = AudioProxyRenderSetSampleAttributes;
121     hwRender->common.attr.GetSampleAttributes = AudioProxyRenderGetSampleAttributes;
122     hwRender->common.attr.GetCurrentChannelId = AudioProxyRenderGetCurrentChannelId;
123     hwRender->common.attr.SetExtraParams = AudioProxyRenderSetExtraParams;
124     hwRender->common.attr.GetExtraParams = AudioProxyRenderGetExtraParams;
125     hwRender->common.attr.ReqMmapBuffer = AudioProxyRenderReqMmapBuffer;
126     hwRender->common.attr.GetMmapPosition = AudioProxyRenderGetMmapPosition;
127     hwRender->common.scene.CheckSceneCapability = AudioProxyRenderCheckSceneCapability;
128     hwRender->common.scene.SelectScene = AudioProxyRenderSelectScene;
129     hwRender->common.volume.SetMute = AudioProxyRenderSetMute;
130     hwRender->common.volume.GetMute = AudioProxyRenderGetMute;
131     hwRender->common.volume.SetVolume = AudioProxyRenderSetVolume;
132     hwRender->common.volume.GetVolume = AudioProxyRenderGetVolume;
133     hwRender->common.volume.GetGainThreshold = AudioProxyRenderGetGainThreshold;
134     hwRender->common.volume.GetGain = AudioProxyRenderGetGain;
135     hwRender->common.volume.SetGain = AudioProxyRenderSetGain;
136     hwRender->common.GetLatency = AudioProxyRenderGetLatency;
137     hwRender->common.RenderFrame = AudioProxyRenderRenderFrame;
138     hwRender->common.GetRenderPosition = AudioProxyRenderGetRenderPosition;
139     hwRender->common.SetRenderSpeed = AudioProxyRenderSetRenderSpeed;
140     hwRender->common.GetRenderSpeed = AudioProxyRenderGetRenderSpeed;
141     hwRender->common.SetChannelMode = AudioProxyRenderSetChannelMode;
142     hwRender->common.GetChannelMode = AudioProxyRenderGetChannelMode;
143     hwRender->common.RegCallback = AudioProxyRenderRegCallback;
144     hwRender->common.DrainBuffer = AudioProxyRenderDrainBuffer;
145     return HDF_SUCCESS;
146 }
147 
InitHwRenderParam(struct AudioHwRender * hwRender,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs)148 int32_t InitHwRenderParam(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc,
149                           const struct AudioSampleAttributes *attrs)
150 {
151     if (hwRender == NULL || desc == NULL || attrs == NULL) {
152         HDF_LOGE("InitHwRenderParam param Is NULL");
153         return HDF_FAILURE;
154     }
155     hwRender->renderParam.renderMode.hwInfo.deviceDescript = *desc;
156     hwRender->renderParam.frameRenderMode.attrs = *attrs;
157     return HDF_SUCCESS;
158 }
159 
160 AudioFormat g_formatIdZero = AUDIO_FORMAT_TYPE_PCM_16_BIT;
InitForGetPortCapability(struct AudioPort portIndex,struct AudioPortCapability * capabilityIndex)161 int32_t InitForGetPortCapability(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex)
162 {
163     if (capabilityIndex == NULL) {
164         HDF_LOGE("capabilityIndex Is NULL");
165         return HDF_FAILURE;
166     }
167     /* get capabilityIndex from driver or default */
168     if (portIndex.dir != PORT_OUT) {
169         capabilityIndex->hardwareMode = true;
170         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
171         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
172         return HDF_SUCCESS;
173     }
174     if (portIndex.portId == 0) {
175         capabilityIndex->hardwareMode = true;
176         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
177         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
178         capabilityIndex->deviceType = portIndex.dir;
179         capabilityIndex->deviceId = PIN_OUT_SPEAKER;
180         capabilityIndex->formatNum = 1;
181         capabilityIndex->formats = &g_formatIdZero;
182         capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000;
183         capabilityIndex->subPortsNum = 1;
184         capabilityIndex->subPorts =
185             reinterpret_cast<struct AudioSubPortCapability *>(calloc(capabilityIndex->subPortsNum,
186             sizeof(struct AudioSubPortCapability)));
187         if (capabilityIndex->subPorts == NULL) {
188             HDF_LOGE("pointer is null!");
189             return HDF_FAILURE;
190         }
191         capabilityIndex->subPorts->portId = portIndex.portId;
192         capabilityIndex->subPorts->desc = portIndex.portName;
193         capabilityIndex->subPorts->mask = PORT_PASSTHROUGH_LPCM;
194         return HDF_SUCCESS;
195     }
196     if (portIndex.portId == 1) {
197         capabilityIndex->hardwareMode = true;
198         capabilityIndex->channelMasks = AUDIO_CHANNEL_STEREO;
199         capabilityIndex->channelCount = CONFIG_CHANNEL_COUNT;
200         capabilityIndex->deviceType = portIndex.dir;
201         capabilityIndex->deviceId = PIN_OUT_HEADSET;
202         capabilityIndex->formatNum = 1;
203         capabilityIndex->formats = &g_formatIdZero;
204         capabilityIndex->sampleRateMasks = AUDIO_SAMPLE_RATE_MASK_16000 | AUDIO_SAMPLE_RATE_MASK_8000;
205         return HDF_SUCCESS;
206     }
207     if (portIndex.portId == HDMI_PORT_ID) {
208         return HdmiPortInit(portIndex, capabilityIndex);
209     }
210     return HDF_FAILURE;
211 }
212 
AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability * portCapabilitys,const int32_t num)213 void AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability *portCapabilitys, const int32_t num)
214 {
215     int32_t i = 0;
216     if (portCapabilitys == NULL) {
217         return;
218     }
219     while (i < num) {
220         if (&portCapabilitys[i] == NULL) {
221             break;
222         }
223         AudioMemFree((void **)(&portCapabilitys[i].capability.subPorts));
224         i++;
225     }
226     return;
227 }
228 
AudioProxyAdapterInitAllPorts(struct AudioAdapter * adapter)229 int32_t AudioProxyAdapterInitAllPorts(struct AudioAdapter *adapter)
230 {
231     struct HdfSBuf *data = NULL;
232     struct HdfSBuf *reply = NULL;
233     const char *adapterName = NULL;
234     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
235     if (hwAdapter == NULL || hwAdapter->adapterDescriptor.adapterName == NULL ||
236         hwAdapter->proxyRemoteHandle == NULL) {
237         HDF_LOGE("hwAdapter Is NULL");
238         return AUDIO_HAL_ERR_INVALID_PARAM;
239     }
240     /* Fake data */
241     uint32_t portNum = hwAdapter->adapterDescriptor.portNum;
242     struct AudioPort *ports = hwAdapter->adapterDescriptor.ports;
243     if (ports == NULL || portNum == 0) {
244         HDF_LOGE("ports is NULL!");
245         return AUDIO_HAL_ERR_INTERNAL;
246     }
247     struct AudioPortAndCapability *portCapability = reinterpret_cast<struct AudioPortAndCapability *>(calloc(portNum,
248         sizeof(struct AudioPortAndCapability)));
249     if (portCapability == NULL) {
250         HDF_LOGE("portCapability is NULL!");
251         return AUDIO_HAL_ERR_MALLOC_FAIL;
252     }
253     for (uint32_t i = 0; i < portNum; i++) {
254         portCapability[i].port = ports[i];
255         if (InitForGetPortCapability(ports[i], &portCapability[i].capability)) {
256             HDF_LOGE("ports Init Invalid!");
257             AudioAdapterReleaseCapSubPorts(portCapability, portNum);
258             AudioMemFree((void **)&portCapability);
259             return AUDIO_HAL_ERR_INTERNAL;
260         }
261     }
262     hwAdapter->portCapabilitys = portCapability;
263     hwAdapter->portCapabilitys->mode = PORT_PASSTHROUGH_LPCM;
264     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
265         return AUDIO_HAL_ERR_INTERNAL;
266     }
267     adapterName = hwAdapter->adapterDescriptor.adapterName;
268     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
269         AudioProxyBufReplyRecycle(data, reply);
270         return AUDIO_HAL_ERR_INTERNAL;
271     }
272     if (!HdfSbufWriteString(data, adapterName)) {
273         AudioProxyBufReplyRecycle(data, reply);
274         return AUDIO_HAL_ERR_INTERNAL;
275     }
276     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_INIT_PORTS, data, reply);
277     if (ret < 0) {
278         HDF_LOGE("Get Failed AudioAdapter!");
279         AudioProxyBufReplyRecycle(data, reply);
280         return ret;
281     }
282     AudioProxyBufReplyRecycle(data, reply);
283     return AUDIO_HAL_SUCCESS;
284 }
285 
AudioProxyAdapterCreateRenderSplit(const struct AudioHwAdapter * hwAdapter,struct AudioHwRender * hwRender)286 int32_t AudioProxyAdapterCreateRenderSplit(const struct AudioHwAdapter *hwAdapter, struct AudioHwRender *hwRender)
287 {
288     if (hwAdapter == NULL || hwRender == NULL) {
289         return HDF_FAILURE;
290     }
291     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
292         return HDF_FAILURE;
293     }
294     uint32_t adapterNameLen = strlen(hwAdapter->adapterDescriptor.adapterName);
295     /* Get Adapter name */
296     int32_t ret = strncpy_s(hwRender->renderParam.renderMode.hwInfo.adapterName, NAME_LEN - 1,
297         hwAdapter->adapterDescriptor.adapterName, adapterNameLen);
298     if (ret != EOK) {
299         return HDF_FAILURE;
300     }
301     return HDF_SUCCESS;
302 }
303 
AudioProxyAdapterCreateRender(struct AudioAdapter * adapter,const struct AudioDeviceDescriptor * desc,const struct AudioSampleAttributes * attrs,struct AudioRender ** render)304 int32_t AudioProxyAdapterCreateRender(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc,
305                                       const struct AudioSampleAttributes *attrs, struct AudioRender **render)
306 {
307     HDF_LOGI("%{public}s, ", __func__);
308     struct HdfSBuf *data = NULL;
309     struct HdfSBuf *reply = NULL;
310     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
311     if (hwAdapter == NULL || hwAdapter->proxyRemoteHandle == NULL || desc == NULL || attrs == NULL || render == NULL) {
312         return AUDIO_HAL_ERR_INVALID_PARAM;
313     }
314     struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(calloc(1, sizeof(*hwRender)));
315     if (hwRender == NULL) {
316         HDF_LOGE("hwRender is NULL!");
317         return AUDIO_HAL_ERR_MALLOC_FAIL;
318     }
319     hwRender->proxyRemoteHandle = hwAdapter->proxyRemoteHandle;
320     if (GetAudioProxyRenderFunc(hwRender) < 0) {
321         AudioMemFree(reinterpret_cast<void **>(&hwRender));
322         return AUDIO_HAL_ERR_INTERNAL;
323     }
324     /* Fill hwRender para */
325     if (InitHwRenderParam(hwRender, desc, attrs) < 0) {
326         AudioMemFree(reinterpret_cast<void **>(&hwRender));
327         return AUDIO_HAL_ERR_INTERNAL;
328     }
329     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
330         AudioMemFree(reinterpret_cast<void **>(&hwRender));
331         return AUDIO_HAL_ERR_INTERNAL;
332     }
333     if (!HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
334         AudioProxyBufReplyRecycle(data, reply);
335         AudioMemFree(reinterpret_cast<void **>(&hwRender));
336         return AUDIO_HAL_ERR_INTERNAL;
337     }
338     if (AudioProxyCommonInitCreateData(data, hwAdapter, desc, attrs) < 0) {
339         HDF_LOGE("Failed to obtain reply");
340         AudioProxyBufReplyRecycle(data, reply);
341         AudioMemFree(reinterpret_cast<void **>(&hwRender));
342         return AUDIO_HAL_ERR_INTERNAL;
343     }
344     int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, AUDIO_HDI_RENDER_CREATE_RENDER, data, reply);
345     if (ret < 0) {
346         HDF_LOGE("Send Server fail!");
347         AudioProxyBufReplyRecycle(data, reply);
348         AudioMemFree(reinterpret_cast<void **>(&hwRender));
349         return ret;
350     }
351     AudioProxyBufReplyRecycle(data, reply);
352     if (AudioProxyAdapterCreateRenderSplit(hwAdapter, hwRender) < 0) {
353         AudioMemFree(reinterpret_cast<void **>(&hwRender));
354         return AUDIO_HAL_ERR_INTERNAL;
355     }
356     *render = &hwRender->common;
357     return AUDIO_HAL_SUCCESS;
358 }
359 
AudioProxyAdapterDestroyRender(struct AudioAdapter * adapter,struct AudioRender * render)360 int32_t AudioProxyAdapterDestroyRender(struct AudioAdapter *adapter, struct AudioRender *render)
361 {
362     struct HdfSBuf *data = NULL;
363     struct HdfSBuf *reply = NULL;
364     if (adapter == NULL || render == NULL) {
365         return AUDIO_HAL_ERR_INVALID_PARAM;
366     }
367     struct AudioHwRender *hwRender = reinterpret_cast<struct AudioHwRender *>(render);
368     if (hwRender == NULL || hwRender->proxyRemoteHandle == NULL) {
369         return AUDIO_HAL_ERR_INVALID_PARAM;
370     }
371     if (AudioProxyPreprocessRender(reinterpret_cast<AudioHwRender *>(render), &data, &reply) < 0) {
372         return AUDIO_HAL_ERR_INTERNAL;
373     }
374     int32_t ret = AudioProxyDispatchCall(hwRender->proxyRemoteHandle, AUDIO_HDI_RENDER_DESTROY, data, reply);
375     if (ret < 0) {
376         if (ret != HDF_ERR_INVALID_OBJECT) {
377             HDF_LOGE("AudioRenderRenderFrame FAIL");
378         }
379         AudioProxyBufReplyRecycle(data, reply);
380         return ret;
381     }
382     AudioMemFree(reinterpret_cast<void **>(&hwRender->renderParam.frameRenderMode.buffer));
383     AudioMemFree(reinterpret_cast<void **>(&render));
384     AudioProxyBufReplyRecycle(data, reply);
385     return AUDIO_HAL_SUCCESS;
386 }
387 
AudioProxyAdapterWritePortCapability(const struct AudioHwAdapter * hwAdapter,const struct AudioPort * port,struct HdfSBuf * data)388 int32_t AudioProxyAdapterWritePortCapability(const struct AudioHwAdapter *hwAdapter,
389     const struct AudioPort *port, struct HdfSBuf *data)
390 {
391     if (hwAdapter == NULL || port == NULL || data == NULL) {
392         return HDF_FAILURE;
393     }
394     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
395         return HDF_FAILURE;
396     }
397     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
398     if (!HdfSbufWriteString(data, adapterName)) {
399         return HDF_FAILURE;
400     }
401     uint32_t tempDir = (uint32_t)port->dir;
402     if (!HdfSbufWriteUint32(data, tempDir)) {
403         return HDF_FAILURE;
404     }
405     if (!HdfSbufWriteUint32(data, port->portId)) {
406         return HDF_FAILURE;
407     }
408     if (port->portName == NULL) {
409         return HDF_FAILURE;
410     }
411     if (!HdfSbufWriteString(data, port->portName)) {
412         return HDF_FAILURE;
413     }
414     return HDF_SUCCESS;
415 }
416 
AudioProxyAdapterGetPortCapability(struct AudioAdapter * adapter,const struct AudioPort * port,struct AudioPortCapability * capability)417 int32_t AudioProxyAdapterGetPortCapability(struct AudioAdapter *adapter,
418     const struct AudioPort *port, struct AudioPortCapability *capability)
419 {
420     HDF_LOGI("%{public}s, ", __func__);
421     if (adapter == NULL || port == NULL || port->portName == NULL || capability == NULL) {
422         return AUDIO_HAL_ERR_INVALID_PARAM;
423     }
424     if (port->portId < 0) {
425         return AUDIO_HAL_ERR_INTERNAL;
426     }
427     struct HdfSBuf *data = NULL;
428     struct HdfSBuf *reply = NULL;
429     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
430         return AUDIO_HAL_ERR_INTERNAL;
431     }
432     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
433     if (hwAdapter == NULL || !HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
434         AudioProxyBufReplyRecycle(data, reply);
435         return AUDIO_HAL_ERR_INTERNAL;
436     }
437     if (AudioProxyAdapterWritePortCapability(hwAdapter, port, data)) {
438         AudioProxyBufReplyRecycle(data, reply);
439         return AUDIO_HAL_ERR_INTERNAL;
440     }
441     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_GET_PORT_CAPABILITY, data, reply);
442     if (ret < 0) {
443         AudioProxyBufReplyRecycle(data, reply);
444         return ret;
445     }
446     AudioProxyBufReplyRecycle(data, reply);
447     /* proxy must init local capability ,this capability the same of Server's */
448     struct AudioPortAndCapability *hwAdapterPortCapabilitys = hwAdapter->portCapabilitys;
449     if (hwAdapterPortCapabilitys == NULL) {
450         HDF_LOGE("hwAdapter portCapabilitys is NULL!");
451         return AUDIO_HAL_ERR_INTERNAL;
452     }
453     int32_t portNum = hwAdapter->adapterDescriptor.portNum;
454     while (hwAdapterPortCapabilitys != NULL && (portNum > 0)) {
455         if (hwAdapterPortCapabilitys->port.portId == port->portId) {
456             *capability = hwAdapterPortCapabilitys->capability;
457             return AUDIO_HAL_SUCCESS;
458         }
459         hwAdapterPortCapabilitys++;
460         portNum--;
461     }
462     return AUDIO_HAL_ERR_INTERNAL;
463 }
464 
AudioProxyAdapterSetAndGetPassthroughModeSBuf(struct HdfSBuf * data,const struct HdfSBuf * reply,const struct AudioPort * port)465 int32_t AudioProxyAdapterSetAndGetPassthroughModeSBuf(struct HdfSBuf *data,
466     const struct HdfSBuf *reply, const struct AudioPort *port)
467 {
468     if (data == NULL || port == NULL || port->portName == NULL) {
469         return HDF_FAILURE;
470     }
471     uint32_t tempDir = port->dir;
472     if (!HdfSbufWriteUint32(data, tempDir)) {
473         return HDF_FAILURE;
474     }
475     if (!HdfSbufWriteUint32(data, port->portId)) {
476         return HDF_FAILURE;
477     }
478     if (!HdfSbufWriteString(data, port->portName)) {
479         HDF_LOGE("HdfSbufWriteString error");
480         return HDF_FAILURE;
481     }
482     return HDF_SUCCESS;
483 }
484 
AudioProxyAdapterSetPassthroughMode(struct AudioAdapter * adapter,const struct AudioPort * port,AudioPortPassthroughMode mode)485 int32_t AudioProxyAdapterSetPassthroughMode(struct AudioAdapter *adapter,
486     const struct AudioPort *port, AudioPortPassthroughMode mode)
487 {
488     HDF_LOGI("%{public}s, ", __func__);
489     struct HdfSBuf *data = NULL;
490     struct HdfSBuf *reply = NULL;
491     if (adapter == NULL || port == NULL || port->portName == NULL) {
492         return AUDIO_HAL_ERR_INVALID_PARAM;
493     }
494     if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
495         return AUDIO_HAL_ERR_INTERNAL;
496     }
497     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
498         HDF_LOGE("AudioProxyPreprocessSBuf Fail");
499         return AUDIO_HAL_ERR_INTERNAL;
500     }
501     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
502     if (hwAdapter == NULL || !HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data) ||
503         hwAdapter->adapterDescriptor.adapterName == NULL) {
504         AudioProxyBufReplyRecycle(data, reply);
505         return AUDIO_HAL_ERR_INTERNAL;
506     }
507     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
508     if (!HdfSbufWriteString(data, adapterName)) {
509         HDF_LOGE("adapterName Write Fail");
510         AudioProxyBufReplyRecycle(data, reply);
511         return AUDIO_HAL_ERR_INTERNAL;
512     }
513     if (AudioProxyAdapterSetAndGetPassthroughModeSBuf(data, reply, port) < 0) {
514         HDF_LOGE("Failed to obtain data");
515         AudioProxyBufReplyRecycle(data, reply);
516         return AUDIO_HAL_ERR_INTERNAL;
517     }
518     uint32_t tempMode = (uint32_t)mode;
519     if (!HdfSbufWriteUint32(data, tempMode)) {
520         HDF_LOGE("Mode Write Fail");
521         AudioProxyBufReplyRecycle(data, reply);
522         return AUDIO_HAL_ERR_INTERNAL;
523     }
524     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_SET_PASS_MODE, data, reply);
525     if (ret < 0) {
526         AudioProxyBufReplyRecycle(data, reply);
527         HDF_LOGE("Failed to send server");
528         return ret;
529     }
530     AudioProxyBufReplyRecycle(data, reply);
531     return AUDIO_HAL_SUCCESS;
532 }
533 
AudioProxyAdapterGetPassthroughMode(struct AudioAdapter * adapter,const struct AudioPort * port,AudioPortPassthroughMode * mode)534 int32_t AudioProxyAdapterGetPassthroughMode(struct AudioAdapter *adapter,
535     const struct AudioPort *port, AudioPortPassthroughMode *mode)
536 {
537     struct HdfSBuf *data = NULL;
538     struct HdfSBuf *reply = NULL;
539     if (adapter == NULL || port == NULL || port->portName == NULL || mode == NULL) {
540         return AUDIO_HAL_ERR_INVALID_PARAM;
541     }
542     if (port->dir != PORT_OUT || port->portId < 0 || strcmp(port->portName, "AOP") != 0) {
543         return AUDIO_HAL_ERR_INTERNAL;
544     }
545     if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
546         return AUDIO_HAL_ERR_INTERNAL;
547     }
548     struct AudioHwAdapter *hwAdapter = reinterpret_cast<struct AudioHwAdapter *>(adapter);
549     if (hwAdapter == NULL || !HdfRemoteServiceWriteInterfaceToken(hwAdapter->proxyRemoteHandle, data)) {
550         AudioProxyBufReplyRecycle(data, reply);
551         return AUDIO_HAL_ERR_INTERNAL;
552     }
553     if (hwAdapter->adapterDescriptor.adapterName == NULL) {
554         AudioProxyBufReplyRecycle(data, reply);
555         return AUDIO_HAL_ERR_INTERNAL;
556     }
557     const char *adapterName = hwAdapter->adapterDescriptor.adapterName;
558     if (!HdfSbufWriteString(data, adapterName)) {
559         AudioProxyBufReplyRecycle(data, reply);
560         return AUDIO_HAL_ERR_INTERNAL;
561     }
562     if (AudioProxyAdapterSetAndGetPassthroughModeSBuf(data, reply, port) < 0) {
563         HDF_LOGE("Failed to obtain data");
564         AudioProxyBufReplyRecycle(data, reply);
565         return AUDIO_HAL_ERR_INTERNAL;
566     }
567     int32_t ret = AudioProxyDispatchCall(hwAdapter->proxyRemoteHandle, AUDIO_HDI_ADT_GET_PASS_MODE, data, reply);
568     if (ret < 0) {
569         AudioProxyBufReplyRecycle(data, reply);
570         return ret;
571     }
572     uint32_t tempMode = 0;
573     if (!HdfSbufReadUint32(reply, &tempMode)) {
574         AudioProxyBufReplyRecycle(data, reply);
575         return AUDIO_HAL_ERR_INTERNAL;
576     }
577     *mode = (AudioPortPassthroughMode)tempMode;
578     AudioProxyBufReplyRecycle(data, reply);
579     return AUDIO_HAL_SUCCESS;
580 }
581 }