• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
43         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
44         return HDF_ERR_INVALID_PARAM;
45     }
46     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
47         HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
48         return HDF_ERR_INVALID_PARAM;
49     }
50 
51     std::shared_lock lock(mutex_);
52     if (client == nullptr || client->device == nullptr || client->device->service == nullptr) {
53         HDF_LOGE("%{public}s: client or client.device or service is nullptr", __func__);
54         return HDF_FAILURE;
55     }
56     auto *hdfAudioManagerHost = CONTAINER_OF(client->device->service, struct HdfAudioManagerHost, ioService);
57     if (hdfAudioManagerHost == NULL || hdfAudioManagerHost->stub == NULL) {
58         HDF_LOGE("%{public}s:invalid hdfAudioManagerHost", __func__);
59         return HDF_ERR_INVALID_PARAM;
60     }
61     return hdfAudioManagerHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
62 }
63 
HdfAudioManagerDriverInit(struct HdfDeviceObject * deviceObject)64 int HdfAudioManagerDriverInit(struct HdfDeviceObject *deviceObject)
65 {
66     HDF_LOGI("Hdf audio manager driver init.");
67     AudioManagerInterfaceImpl::GetAudioManager()->SetDeviceObject(deviceObject);
68     HdfDeviceSetClass(deviceObject, DEVICE_CLASS_AUDIO);
69     return HDF_SUCCESS;
70 }
71 
HdfAudioManagerDriverBind(struct HdfDeviceObject * deviceObject)72 int HdfAudioManagerDriverBind(struct HdfDeviceObject *deviceObject)
73 {
74     HDF_LOGI("Hdf audio manager driver bind.");
75 
76     if (deviceObject == nullptr) {
77         HDF_LOGE("%{public}s: deviceObject is nullptr", __func__);
78         return HDF_FAILURE;
79     }
80 
81     auto *hdfAudioManagerHost = new (std::nothrow) HdfAudioManagerHost;
82     if (hdfAudioManagerHost == nullptr) {
83         HDF_LOGE("%{public}s: failed to create create HdfAudioManagerHost object", __func__);
84         return HDF_FAILURE;
85     }
86 
87     hdfAudioManagerHost->ioService.Dispatch = AudioManagerDriverDispatch;
88     hdfAudioManagerHost->ioService.Open = NULL;
89     hdfAudioManagerHost->ioService.Release = NULL;
90 
91     auto serviceImpl = IAudioManager::Get("daudio_primary_service", true);
92     if (serviceImpl == nullptr) {
93         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
94         delete hdfAudioManagerHost;
95         return HDF_FAILURE;
96     }
97 
98     hdfAudioManagerHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
99         IAudioManager::GetDescriptor());
100     if (hdfAudioManagerHost->stub == nullptr) {
101         HDF_LOGE("%{public}s: failed to get stub object", __func__);
102         delete hdfAudioManagerHost;
103         return HDF_FAILURE;
104     }
105 
106     deviceObject->service = &hdfAudioManagerHost->ioService;
107     return HDF_SUCCESS;
108 }
109 
HdfAudioManagerDriverRelease(struct HdfDeviceObject * deviceObject)110 void HdfAudioManagerDriverRelease(struct HdfDeviceObject *deviceObject)
111 {
112     HDF_LOGI("Hdf audio manager driver release.");
113     if (deviceObject == nullptr || deviceObject->service == nullptr) {
114         HDF_LOGE("%{public}s: deviceObject or service is nullptr", __func__);
115         return;
116     }
117     std::unique_lock lock(mutex_);
118     auto *hdfAudioManagerHost = CONTAINER_OF(deviceObject->service, struct HdfAudioManagerHost, ioService);
119     if (hdfAudioManagerHost != nullptr) {
120         hdfAudioManagerHost->stub = nullptr;
121     }
122     delete hdfAudioManagerHost;
123     hdfAudioManagerHost = nullptr;
124     if (deviceObject != nullptr) {
125         deviceObject->service = nullptr;
126     }
127 }
128 
129 struct HdfDriverEntry g_audiomanagerDriverEntry = {
130     .moduleVersion = 1,
131     .moduleName = "daudio",
132     .Bind = HdfAudioManagerDriverBind,
133     .Init = HdfAudioManagerDriverInit,
134     .Release = HdfAudioManagerDriverRelease,
135 };
136 
137 #ifndef __cplusplus
138 extern "C" {
139 #endif
140 HDF_INIT(g_audiomanagerDriverEntry);
141 #ifndef __cplusplus
142 }
143 #endif
144