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