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 "audio_uhdf_log.h"
17 #include "hdf_base.h"
18 #include "hdf_device_object.h"
19 #include "hdf_dlist.h"
20 #include "osal_mem.h"
21 #include "stub_collector.h"
22 #include "v1_0/iaudio_manager.h"
23
24 #define HDF_LOG_TAG AUDIO_HDI_SVC
25
26 struct HdfAudioManagerHost {
27 struct IDeviceIoService ioService;
28 struct IAudioManager *service;
29 struct HdfRemoteService **stubObject;
30 };
31
AudioManagerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int32_t AudioManagerDriverDispatch(
33 struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
34 {
35 if (client == NULL || client->device == NULL || client->device->service == NULL) {
36 AUDIO_FUNC_LOGE("Param is NULL!");
37 return HDF_ERR_INVALID_PARAM;
38 }
39
40 struct HdfAudioManagerHost *audiomanagerHost =
41 CONTAINER_OF(client->device->service, struct HdfAudioManagerHost, ioService);
42 if (audiomanagerHost->service == NULL || audiomanagerHost->stubObject == NULL) {
43 HDF_LOGE("%{public}s: invalid service obj", __func__);
44 return HDF_ERR_INVALID_OBJECT;
45 }
46
47 struct HdfRemoteService *stubObj = *audiomanagerHost->stubObject;
48 if (stubObj == NULL || stubObj->dispatcher == NULL || stubObj->dispatcher->Dispatch == NULL) {
49 return HDF_ERR_INVALID_OBJECT;
50 }
51
52 return stubObj->dispatcher->Dispatch((struct HdfRemoteService *)stubObj->target, cmdId, data, reply);
53 }
54
HdfAudioManagerDriverInit(struct HdfDeviceObject * deviceObject)55 static int32_t HdfAudioManagerDriverInit(struct HdfDeviceObject *deviceObject)
56 {
57 if (deviceObject == NULL) {
58 AUDIO_FUNC_LOGE("deviceObject is null!");
59 return HDF_ERR_INVALID_PARAM;
60 }
61 if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_AUDIO)) {
62 AUDIO_FUNC_LOGE("Set Primary DEVICE_CLASS_AUDIO fail!");
63 }
64
65 return HDF_SUCCESS;
66 }
67
HdfAudioManagerDriverBind(struct HdfDeviceObject * deviceObject)68 static int32_t HdfAudioManagerDriverBind(struct HdfDeviceObject *deviceObject)
69 {
70 AUDIO_FUNC_LOGI("enter.");
71 if (deviceObject == NULL) {
72 AUDIO_FUNC_LOGE("Param is NULL!");
73 return HDF_ERR_INVALID_PARAM;
74 }
75
76 int32_t ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, IAUDIOMANAGER_INTERFACE_DESC);
77 if (ret != HDF_SUCCESS) {
78 AUDIO_FUNC_LOGI("failed to set interface descriptor object! ret = %{public}d", ret);
79 return HDF_FAILURE;
80 }
81
82 struct HdfAudioManagerHost *audiomanagerHost =
83 (struct HdfAudioManagerHost *)OsalMemCalloc(sizeof(struct HdfAudioManagerHost));
84 if (audiomanagerHost == NULL) {
85 AUDIO_FUNC_LOGE("alloc HdfAudioManagerHost failed!");
86 return HDF_ERR_MALLOC_FAIL;
87 }
88
89 struct IAudioManager *serviceImpl = IAudioManagerGet(true);
90 if (serviceImpl == NULL) {
91 AUDIO_FUNC_LOGE("create serviceImpl failed!");
92 OsalMemFree(audiomanagerHost);
93 return HDF_FAILURE;
94 }
95
96 struct HdfRemoteService **stubObj = StubCollectorGetOrNewObject(IAUDIOMANAGER_INTERFACE_DESC, serviceImpl);
97 if (stubObj == NULL) {
98 OsalMemFree(audiomanagerHost);
99 IAudioManagerRelease(serviceImpl, true);
100 return HDF_FAILURE;
101 }
102
103 audiomanagerHost->ioService.Dispatch = AudioManagerDriverDispatch;
104 audiomanagerHost->ioService.Open = NULL;
105 audiomanagerHost->ioService.Release = NULL;
106 audiomanagerHost->service = serviceImpl;
107 audiomanagerHost->stubObject = stubObj;
108 deviceObject->service = &audiomanagerHost->ioService;
109
110 return HDF_SUCCESS;
111 }
112
HdfAudioManagerDriverRelease(struct HdfDeviceObject * deviceObject)113 static void HdfAudioManagerDriverRelease(struct HdfDeviceObject *deviceObject)
114 {
115 AUDIO_FUNC_LOGI("enter.");
116 if (deviceObject == NULL) {
117 AUDIO_FUNC_LOGE("Param is NULL!");
118 return;
119 }
120
121 struct HdfAudioManagerHost *audiomanagerHost =
122 CONTAINER_OF(deviceObject->service, struct HdfAudioManagerHost, ioService);
123 if (audiomanagerHost == NULL) {
124 AUDIO_FUNC_LOGE("HdfAudioManagerHost is NULL!");
125 return;
126 }
127
128 StubCollectorRemoveObject(IAUDIOMANAGER_INTERFACE_DESC, audiomanagerHost->service);
129 IAudioManagerRelease(audiomanagerHost->service, true);
130 OsalMemFree(audiomanagerHost);
131 }
132
133 static struct HdfDriverEntry g_audiomanagerDriverEntry = {
134 .moduleVersion = 1,
135 .moduleName = "audio_manager_service",
136 .Bind = HdfAudioManagerDriverBind,
137 .Init = HdfAudioManagerDriverInit,
138 .Release = HdfAudioManagerDriverRelease,
139 };
140
141 HDF_INIT(g_audiomanagerDriverEntry);
142