1/*
2 * Copyright (c) 2023 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_object.h"
18#include "hdf_dlist.h"
19#include "osal_mem.h"
20#include "stub_collector.h"
21#include "v1_0/ieffect_model.h"
22#include "audio_uhdf_log.h"
23
24#define HDF_LOG_TAG HDF_AUDIO_EFFECT
25
26struct HdfEffectModelHost {
27    struct IDeviceIoService ioService;
28    struct IEffectModel *service;
29    struct HdfRemoteService **stubObject;
30};
31
32static int32_t EffectModelDriverDispatch(
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        HDF_LOGE("%{public}s:param is NULL!", __func__);
37        return HDF_ERR_INVALID_PARAM;
38    }
39
40    struct HdfEffectModelHost *effectModelHost =
41        CONTAINER_OF(client->device->service, struct HdfEffectModelHost, ioService);
42    if (effectModelHost->service == NULL || effectModelHost->stubObject == NULL) {
43        HDF_LOGE("%{public}s: invalid service obj", __func__);
44        return HDF_ERR_INVALID_OBJECT;
45    }
46
47    struct HdfRemoteService *stubObj = *effectModelHost->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
55static int32_t HdfEffectDriverInit(struct HdfDeviceObject *deviceObject)
56{
57    if (deviceObject == NULL) {
58        HDF_LOGE("%{public}s:deviceObject is null!", __func__);
59        return HDF_ERR_INVALID_PARAM;
60    }
61    if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_AUDIO)) {
62        HDF_LOGE("%{public}s:set primary DEVICE_CLASS_AUDIO fail!", __func__);
63    }
64
65    return HDF_SUCCESS;
66}
67
68static int32_t HdfEffectModelDriverBind(struct HdfDeviceObject *deviceObject)
69{
70    HDF_LOGD("enter to %{public}s.", __func__);
71    if (deviceObject == NULL) {
72        HDF_LOGE("%{public}s:param is NULL!", __func__);
73        return HDF_ERR_INVALID_PARAM;
74    }
75
76    int32_t ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, IEFFECTMODEL_INTERFACE_DESC);
77    if (ret != HDF_SUCCESS) {
78        HDF_LOGE("failed to set interface descriptor object! ret = %{public}d", ret);
79        return HDF_FAILURE;
80    }
81
82    struct HdfEffectModelHost *effectModelHost =
83        (struct HdfEffectModelHost *)OsalMemCalloc(sizeof(struct HdfEffectModelHost));
84    if (effectModelHost == NULL) {
85        HDF_LOGE("%{public}s:alloc HdfEffectModelHost failed!", __func__);
86        return HDF_ERR_MALLOC_FAIL;
87    }
88
89    struct IEffectModel *serviceImpl = IEffectModelGet(true);
90    if (serviceImpl == NULL) {
91        HDF_LOGE("%{public}s:create serviceImpl failed!", __func__);
92        OsalMemFree(effectModelHost);
93        return HDF_FAILURE;
94    }
95
96    struct HdfRemoteService **stubObj = StubCollectorGetOrNewObject(IEFFECTMODEL_INTERFACE_DESC, serviceImpl);
97    if (stubObj == NULL) {
98        OsalMemFree(effectModelHost);
99        IEffectModelRelease(serviceImpl, true);
100        return HDF_FAILURE;
101    }
102
103    effectModelHost->ioService.Dispatch = EffectModelDriverDispatch;
104    effectModelHost->ioService.Open = NULL;
105    effectModelHost->ioService.Release = NULL;
106    effectModelHost->service = serviceImpl;
107    effectModelHost->stubObject = stubObj;
108    deviceObject->service = &effectModelHost->ioService;
109
110    return HDF_SUCCESS;
111}
112
113static void HdfEffectModelDriverRelease(struct HdfDeviceObject *deviceObject)
114{
115    HDF_LOGD("enter to %{public}s.", __func__);
116    if (deviceObject == NULL) {
117        HDF_LOGE("%{public}s:param is NULL!", __func__);
118        return;
119    }
120
121    struct HdfEffectModelHost *effectModelHost =
122        CONTAINER_OF(deviceObject->service, struct HdfEffectModelHost, ioService);
123    if (effectModelHost == NULL) {
124        HDF_LOGE("%{public}s:HdfEffectModelHost is NULL!", __func__);
125        return;
126    }
127
128    StubCollectorRemoveObject(IEFFECTMODEL_INTERFACE_DESC, effectModelHost->service);
129    IEffectModelRelease(effectModelHost->service, true);
130    OsalMemFree(effectModelHost);
131}
132
133static struct HdfDriverEntry g_effectModelDriverEntry = {
134    .moduleVersion = 1,
135    .moduleName = "effect_model_service",
136    .Bind = HdfEffectModelDriverBind,
137    .Init = HdfEffectDriverInit,
138    .Release = HdfEffectModelDriverRelease,
139};
140
141HDF_INIT(g_effectModelDriverEntry);