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 "audio_proxy_manager.h"
16 #include <hdf_log.h>
17 #include <hdf_remote_service.h>
18 #include <servmgr_hdi.h>
19 #include <osal_mem.h>
20 #include "audio_proxy_common.h"
21 #include "audio_proxy_internal.h"
22 #include "audio_adapter_info_common.h"
23
24 namespace OHOS::HDI::Audio_Bluetooth {
25 constexpr const char *HDI_SERVER_NAME = "audio_bluetooth_hdi_service";
26
AudioProxyManagerGetAllAdapters(struct AudioProxyManager * manager,struct AudioAdapterDescriptor ** descs,int * size)27 int32_t AudioProxyManagerGetAllAdapters(struct AudioProxyManager *manager,
28 struct AudioAdapterDescriptor **descs,
29 int *size)
30 {
31 HDF_LOGI("%{public}s, ", __func__);
32 struct HdfSBuf *data = NULL;
33 struct HdfSBuf *reply = NULL;
34 int32_t ret;
35 if (manager == NULL || manager->remote == NULL || descs == NULL || size == NULL) {
36 return AUDIO_HAL_ERR_INVALID_PARAM;
37 }
38 if (AudioProxyPreprocessSBuf(&data, &reply) < 0) {
39 return AUDIO_HAL_ERR_INTERNAL;
40 }
41 HDF_LOGI("%{public}s, call HdfRemoteServiceWriteInterfaceToken ", __func__);
42 if (!HdfRemoteServiceWriteInterfaceToken(manager->remote, data)) {
43 HDF_LOGE("%{public}s, call HdfRemoteServiceWriteInterfaceToken error ", __func__);
44 AudioProxyBufReplyRecycle(data, reply);
45 return AUDIO_HAL_ERR_INTERNAL;
46 }
47 ret = AudioAdaptersForUser(descs, size);
48 if (ret < 0) {
49 AudioProxyBufReplyRecycle(data, reply);
50 HDF_LOGE("AudioAdaptersForUser FAIL!");
51 return AUDIO_HAL_ERR_NOTREADY; // Failed to read sound card configuration file
52 }
53 HDF_LOGI("%{public}s, call AudioProxyDispatchCall ", __func__);
54 ret = AudioProxyDispatchCall(manager->remote, AUDIO_HDI_MGR_GET_ALL_ADAPTER, data, reply);
55 if (ret != AUDIO_HAL_SUCCESS) {
56 AudioProxyBufReplyRecycle(data, reply);
57 HDF_LOGE("Failed to send service call!");
58 return ret;
59 }
60 AudioProxyBufReplyRecycle(data, reply);
61 return AUDIO_HAL_SUCCESS;
62 }
63
AudioProxyManagerLoadAdapter(struct AudioProxyManager * manager,const struct AudioAdapterDescriptor * desc,struct AudioAdapter ** adapter)64 int32_t AudioProxyManagerLoadAdapter(struct AudioProxyManager *manager, const struct AudioAdapterDescriptor *desc,
65 struct AudioAdapter **adapter)
66 {
67 HDF_LOGI("%{public}s, ", __func__);
68 struct HdfSBuf *data = NULL;
69 struct HdfSBuf *reply = NULL;
70
71 if (manager == NULL || manager->remote == NULL || desc == NULL ||
72 desc->adapterName == NULL || desc->ports == NULL || adapter == NULL) {
73 return AUDIO_HAL_ERR_INVALID_PARAM;
74 }
75 if (AudioAdapterExist(desc->adapterName) || (AudioProxyPreprocessSBuf(&data, &reply) < 0)) {
76 return AUDIO_HAL_ERR_INTERNAL;
77 }
78 if (!HdfRemoteServiceWriteInterfaceToken(manager->remote, data)) {
79 AudioProxyBufReplyRecycle(data, reply);
80 return AUDIO_HAL_ERR_INTERNAL;
81 }
82 if (!HdfSbufWriteString(data, desc->adapterName)) {
83 AudioProxyBufReplyRecycle(data, reply);
84 return AUDIO_HAL_ERR_INTERNAL;
85 }
86 uint32_t tempDir = (uint32_t)desc->ports->dir;
87 if (!HdfSbufWriteUint32(data, tempDir)) {
88 AudioProxyBufReplyRecycle(data, reply);
89 return AUDIO_HAL_ERR_INTERNAL;
90 }
91 struct AudioHwAdapter *hwAdapter =
92 reinterpret_cast<struct AudioHwAdapter *>(calloc(1, sizeof(struct AudioHwAdapter)));
93 if (hwAdapter == NULL) {
94 AudioProxyBufReplyRecycle(data, reply);
95 return AUDIO_HAL_ERR_MALLOC_FAIL;
96 }
97 int32_t ret = AudioProxyDispatchCall(manager->remote, AUDIO_HDI_MGR_LOAD_ADAPTER, data, reply);
98 if (ret < 0) {
99 AudioProxyBufReplyRecycle(data, reply);
100 AudioMemFree(reinterpret_cast<void **>(&hwAdapter));
101 return ret;
102 }
103 hwAdapter->common.InitAllPorts = AudioProxyAdapterInitAllPorts;
104 hwAdapter->common.CreateRender = AudioProxyAdapterCreateRender;
105 hwAdapter->common.DestroyRender = AudioProxyAdapterDestroyRender;
106 hwAdapter->common.GetPortCapability = AudioProxyAdapterGetPortCapability;
107 hwAdapter->common.SetPassthroughMode = AudioProxyAdapterSetPassthroughMode;
108 hwAdapter->common.GetPassthroughMode = AudioProxyAdapterGetPassthroughMode;
109 hwAdapter->adapterDescriptor = *desc;
110 hwAdapter->proxyRemoteHandle = manager->remote; // get dispatch Server
111 *adapter = &hwAdapter->common;
112 AudioProxyBufReplyRecycle(data, reply);
113 return AUDIO_HAL_SUCCESS;
114 }
115
AudioProxyManagerUnloadAdapter(const struct AudioProxyManager * manager,const struct AudioAdapter * adapter)116 void AudioProxyManagerUnloadAdapter(const struct AudioProxyManager *manager, const struct AudioAdapter *adapter)
117 {
118 struct HdfSBuf *data = NULL;
119 struct HdfSBuf *reply = NULL;
120 const char *adapterName = NULL;
121 int32_t i = 0;
122 int32_t portNum;
123 const struct AudioHwAdapter *hwAdapter = reinterpret_cast<const struct AudioHwAdapter *>(adapter);
124 if (manager == NULL || manager->remote == NULL || adapter == NULL) {
125 return;
126 }
127 if (hwAdapter->portCapabilitys != NULL) {
128 portNum = hwAdapter->adapterDescriptor.portNum;
129 while (i < portNum) {
130 if (&hwAdapter->portCapabilitys[i] != NULL) {
131 AudioMemFree((void **)&hwAdapter->portCapabilitys[i].capability.subPorts);
132 }
133 i++;
134 }
135 AudioMemFree(reinterpret_cast<void **>(const_cast<AudioPortAndCapability **>(&hwAdapter->portCapabilitys)));
136 }
137 if (AudioProxyPreprocessSBuf(&data, &reply) == AUDIO_HAL_SUCCESS) {
138 adapterName = hwAdapter->adapterDescriptor.adapterName;
139 if (!HdfRemoteServiceWriteInterfaceToken(manager->remote, data)) {
140 AudioProxyBufReplyRecycle(data, reply);
141 return;
142 }
143 if (HdfSbufWriteString(data, adapterName)) {
144 int32_t ret = AudioProxyDispatchCall(manager->remote, AUDIO_HDI_MGR_UNLOAD_ADAPTER, data, reply);
145 if (ret < 0) {
146 HDF_LOGE("Send Server fail!");
147 }
148 }
149 AudioProxyBufReplyRecycle(data, reply);
150 }
151 AudioMemFree(reinterpret_cast<void **>(const_cast<AudioAdapter **>(&adapter)));
152 return;
153 }
154
ProxyMgrConstruct(struct AudioProxyManager * proxyMgr)155 static void ProxyMgrConstruct(struct AudioProxyManager *proxyMgr)
156 {
157 proxyMgr->GetAllAdapters = AudioProxyManagerGetAllAdapters;
158 proxyMgr->LoadAdapter = AudioProxyManagerLoadAdapter;
159 proxyMgr->UnloadAdapter = AudioProxyManagerUnloadAdapter;
160 }
161
GetAudioProxyManagerFuncs(void)162 struct AudioProxyManager *GetAudioProxyManagerFuncs(void)
163 {
164 HDF_LOGI("%{public}s, ", __func__);
165 struct HDIServiceManager *serviceMgr = HDIServiceManagerGet();
166 if (serviceMgr == NULL) {
167 HDF_LOGE("HDIServiceManagerGet failed!");
168 return NULL;
169 }
170 struct HdfRemoteService *remote = serviceMgr->GetService(serviceMgr, HDI_SERVER_NAME);
171 if (remote == NULL) {
172 HDF_LOGE("Remote GetService failed!");
173 HDIServiceManagerRelease(serviceMgr);
174 return NULL;
175 }
176 HDIServiceManagerRelease(serviceMgr);
177 HDF_LOGE("HdfRemoteServiceSetInterfaceDesc");
178 if (!HdfRemoteServiceSetInterfaceDesc(remote, "ohos.hdi.audio_bluetooth_service")) {
179 HDF_LOGE("failed to init interface desc");
180 HdfRemoteServiceRecycle(remote);
181 return NULL;
182 }
183 struct AudioProxyManager *proxyDevMgr =
184 reinterpret_cast<AudioProxyManager *>(OsalMemAlloc(sizeof(struct AudioProxyManager)));
185 if (proxyDevMgr == NULL) {
186 HDF_LOGE("malloc failed!");
187 HdfRemoteServiceRecycle(remote);
188 return NULL;
189 }
190 ProxyMgrConstruct(proxyDevMgr);
191 proxyDevMgr->remote = remote;
192 return proxyDevMgr;
193 }
194 }