1 /*
2 * Copyright (c) 2022-2024 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 <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_log.h>
19 #include <hdf_sbuf_ipc.h>
20 #include <v1_0/audio_manager_stub.h>
21
22 #include "audio_manager_interface_impl.h"
23 #include <shared_mutex>
24
25 using namespace OHOS::HDI::DistributedAudio::Audio::V1_0;
26
27 namespace {
28 std::shared_mutex mutex_;
29 }
30
31 struct HdfAudioManagerHost {
32 struct IDeviceIoService ioService;
33 OHOS::sptr<OHOS::IRemoteObject> stub;
34 };
35
AudioManagerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)36 static int32_t AudioManagerDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
37 struct HdfSBuf *reply)
38 {
39 OHOS::MessageParcel *dataParcel = nullptr;
40 OHOS::MessageParcel *replyParcel = nullptr;
41 OHOS::MessageOption option;
42
43 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
44 HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
45 return HDF_ERR_INVALID_PARAM;
46 }
47 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
48 HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
49 return HDF_ERR_INVALID_PARAM;
50 }
51
52 std::shared_lock lock(mutex_);
53 if (client == nullptr || client->device == nullptr || client->device->service == nullptr) {
54 HDF_LOGE("%{public}s: client or client.device or service is nullptr", __func__);
55 return HDF_FAILURE;
56 }
57 auto *hdfAudioManagerHost = CONTAINER_OF(client->device->service, struct HdfAudioManagerHost, ioService);
58 if (hdfAudioManagerHost == NULL || hdfAudioManagerHost->stub == NULL) {
59 HDF_LOGE("%{public}s:invalid hdfAudioManagerHost", __func__);
60 return HDF_ERR_INVALID_PARAM;
61 }
62 return hdfAudioManagerHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
63 }
64
HdfAudioManagerDriverInit(struct HdfDeviceObject * deviceObject)65 int HdfAudioManagerDriverInit(struct HdfDeviceObject *deviceObject)
66 {
67 HDF_LOGI("Hdf audio manager driver init.");
68 AudioManagerInterfaceImpl::GetAudioManager()->SetDeviceObject(deviceObject);
69 HdfDeviceSetClass(deviceObject, DEVICE_CLASS_AUDIO);
70 return HDF_SUCCESS;
71 }
72
HdfAudioManagerDriverBind(struct HdfDeviceObject * deviceObject)73 int HdfAudioManagerDriverBind(struct HdfDeviceObject *deviceObject)
74 {
75 HDF_LOGI("Hdf audio manager driver bind.");
76
77 if (deviceObject == nullptr) {
78 HDF_LOGE("%{public}s: deviceObject is nullptr", __func__);
79 return HDF_FAILURE;
80 }
81
82 auto *hdfAudioManagerHost = new (std::nothrow) HdfAudioManagerHost;
83 if (hdfAudioManagerHost == nullptr) {
84 HDF_LOGE("%{public}s: failed to create create HdfAudioManagerHost object", __func__);
85 return HDF_FAILURE;
86 }
87
88 hdfAudioManagerHost->ioService.Dispatch = AudioManagerDriverDispatch;
89 hdfAudioManagerHost->ioService.Open = NULL;
90 hdfAudioManagerHost->ioService.Release = NULL;
91
92 auto serviceImpl = IAudioManager::Get("daudio_primary_service", true);
93 if (serviceImpl == nullptr) {
94 HDF_LOGE("%{public}s: failed to get of implement service", __func__);
95 delete hdfAudioManagerHost;
96 return HDF_FAILURE;
97 }
98
99 hdfAudioManagerHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
100 IAudioManager::GetDescriptor());
101 if (hdfAudioManagerHost->stub == nullptr) {
102 HDF_LOGE("%{public}s: failed to get stub object", __func__);
103 delete hdfAudioManagerHost;
104 return HDF_FAILURE;
105 }
106
107 deviceObject->service = &hdfAudioManagerHost->ioService;
108 return HDF_SUCCESS;
109 }
110
HdfAudioManagerDriverRelease(struct HdfDeviceObject * deviceObject)111 void HdfAudioManagerDriverRelease(struct HdfDeviceObject *deviceObject)
112 {
113 HDF_LOGI("Hdf audio manager driver release.");
114 if (deviceObject == nullptr || deviceObject->service == nullptr) {
115 HDF_LOGE("%{public}s: deviceObject or service is nullptr", __func__);
116 return;
117 }
118 std::unique_lock lock(mutex_);
119 auto *hdfAudioManagerHost = CONTAINER_OF(deviceObject->service, struct HdfAudioManagerHost, ioService);
120 if (hdfAudioManagerHost != nullptr) {
121 hdfAudioManagerHost->stub = nullptr;
122 }
123 delete hdfAudioManagerHost;
124 hdfAudioManagerHost = nullptr;
125 if (deviceObject != nullptr) {
126 deviceObject->service = nullptr;
127 }
128 }
129
130 struct HdfDriverEntry g_audiomanagerDriverEntry = {
131 .moduleVersion = 1,
132 .moduleName = "daudio",
133 .Bind = HdfAudioManagerDriverBind,
134 .Init = HdfAudioManagerDriverInit,
135 .Release = HdfAudioManagerDriverRelease,
136 };
137
138 #ifndef __cplusplus
139 extern "C" {
140 #endif
141 HDF_INIT(g_audiomanagerDriverEntry);
142 #ifndef __cplusplus
143 }
144 #endif
145