1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "hdf_base.h"
17094332d3Sopenharmony_ci#include "hdf_device_object.h"
18094332d3Sopenharmony_ci#include "hdf_dlist.h"
19094332d3Sopenharmony_ci#include "osal_mem.h"
20094332d3Sopenharmony_ci#include "stub_collector.h"
21094332d3Sopenharmony_ci#include "v1_0/ieffect_model.h"
22094332d3Sopenharmony_ci#include "audio_uhdf_log.h"
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ci#define HDF_LOG_TAG HDF_AUDIO_EFFECT
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cistruct HdfEffectModelHost {
27094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
28094332d3Sopenharmony_ci    struct IEffectModel *service;
29094332d3Sopenharmony_ci    struct HdfRemoteService **stubObject;
30094332d3Sopenharmony_ci};
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_cistatic int32_t EffectModelDriverDispatch(
33094332d3Sopenharmony_ci    struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
34094332d3Sopenharmony_ci{
35094332d3Sopenharmony_ci    if (client == NULL || client->device == NULL || client->device->service == NULL) {
36094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:param is NULL!", __func__);
37094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
38094332d3Sopenharmony_ci    }
39094332d3Sopenharmony_ci
40094332d3Sopenharmony_ci    struct HdfEffectModelHost *effectModelHost =
41094332d3Sopenharmony_ci        CONTAINER_OF(client->device->service, struct HdfEffectModelHost, ioService);
42094332d3Sopenharmony_ci    if (effectModelHost->service == NULL || effectModelHost->stubObject == NULL) {
43094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: invalid service obj", __func__);
44094332d3Sopenharmony_ci        return HDF_ERR_INVALID_OBJECT;
45094332d3Sopenharmony_ci    }
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_ci    struct HdfRemoteService *stubObj = *effectModelHost->stubObject;
48094332d3Sopenharmony_ci    if (stubObj == NULL || stubObj->dispatcher == NULL || stubObj->dispatcher->Dispatch == NULL) {
49094332d3Sopenharmony_ci        return HDF_ERR_INVALID_OBJECT;
50094332d3Sopenharmony_ci    }
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci    return stubObj->dispatcher->Dispatch((struct HdfRemoteService *)stubObj->target, cmdId, data, reply);
53094332d3Sopenharmony_ci}
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_cistatic int32_t HdfEffectDriverInit(struct HdfDeviceObject *deviceObject)
56094332d3Sopenharmony_ci{
57094332d3Sopenharmony_ci    if (deviceObject == NULL) {
58094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:deviceObject is null!", __func__);
59094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
60094332d3Sopenharmony_ci    }
61094332d3Sopenharmony_ci    if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_AUDIO)) {
62094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:set primary DEVICE_CLASS_AUDIO fail!", __func__);
63094332d3Sopenharmony_ci    }
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    return HDF_SUCCESS;
66094332d3Sopenharmony_ci}
67094332d3Sopenharmony_ci
68094332d3Sopenharmony_cistatic int32_t HdfEffectModelDriverBind(struct HdfDeviceObject *deviceObject)
69094332d3Sopenharmony_ci{
70094332d3Sopenharmony_ci    HDF_LOGD("enter to %{public}s.", __func__);
71094332d3Sopenharmony_ci    if (deviceObject == NULL) {
72094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:param is NULL!", __func__);
73094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
74094332d3Sopenharmony_ci    }
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_ci    int32_t ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, IEFFECTMODEL_INTERFACE_DESC);
77094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
78094332d3Sopenharmony_ci        HDF_LOGE("failed to set interface descriptor object! ret = %{public}d", ret);
79094332d3Sopenharmony_ci        return HDF_FAILURE;
80094332d3Sopenharmony_ci    }
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    struct HdfEffectModelHost *effectModelHost =
83094332d3Sopenharmony_ci        (struct HdfEffectModelHost *)OsalMemCalloc(sizeof(struct HdfEffectModelHost));
84094332d3Sopenharmony_ci    if (effectModelHost == NULL) {
85094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:alloc HdfEffectModelHost failed!", __func__);
86094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    struct IEffectModel *serviceImpl = IEffectModelGet(true);
90094332d3Sopenharmony_ci    if (serviceImpl == NULL) {
91094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:create serviceImpl failed!", __func__);
92094332d3Sopenharmony_ci        OsalMemFree(effectModelHost);
93094332d3Sopenharmony_ci        return HDF_FAILURE;
94094332d3Sopenharmony_ci    }
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_ci    struct HdfRemoteService **stubObj = StubCollectorGetOrNewObject(IEFFECTMODEL_INTERFACE_DESC, serviceImpl);
97094332d3Sopenharmony_ci    if (stubObj == NULL) {
98094332d3Sopenharmony_ci        OsalMemFree(effectModelHost);
99094332d3Sopenharmony_ci        IEffectModelRelease(serviceImpl, true);
100094332d3Sopenharmony_ci        return HDF_FAILURE;
101094332d3Sopenharmony_ci    }
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_ci    effectModelHost->ioService.Dispatch = EffectModelDriverDispatch;
104094332d3Sopenharmony_ci    effectModelHost->ioService.Open = NULL;
105094332d3Sopenharmony_ci    effectModelHost->ioService.Release = NULL;
106094332d3Sopenharmony_ci    effectModelHost->service = serviceImpl;
107094332d3Sopenharmony_ci    effectModelHost->stubObject = stubObj;
108094332d3Sopenharmony_ci    deviceObject->service = &effectModelHost->ioService;
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci    return HDF_SUCCESS;
111094332d3Sopenharmony_ci}
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_cistatic void HdfEffectModelDriverRelease(struct HdfDeviceObject *deviceObject)
114094332d3Sopenharmony_ci{
115094332d3Sopenharmony_ci    HDF_LOGD("enter to %{public}s.", __func__);
116094332d3Sopenharmony_ci    if (deviceObject == NULL) {
117094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:param is NULL!", __func__);
118094332d3Sopenharmony_ci        return;
119094332d3Sopenharmony_ci    }
120094332d3Sopenharmony_ci
121094332d3Sopenharmony_ci    struct HdfEffectModelHost *effectModelHost =
122094332d3Sopenharmony_ci        CONTAINER_OF(deviceObject->service, struct HdfEffectModelHost, ioService);
123094332d3Sopenharmony_ci    if (effectModelHost == NULL) {
124094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:HdfEffectModelHost is NULL!", __func__);
125094332d3Sopenharmony_ci        return;
126094332d3Sopenharmony_ci    }
127094332d3Sopenharmony_ci
128094332d3Sopenharmony_ci    StubCollectorRemoveObject(IEFFECTMODEL_INTERFACE_DESC, effectModelHost->service);
129094332d3Sopenharmony_ci    IEffectModelRelease(effectModelHost->service, true);
130094332d3Sopenharmony_ci    OsalMemFree(effectModelHost);
131094332d3Sopenharmony_ci}
132094332d3Sopenharmony_ci
133094332d3Sopenharmony_cistatic struct HdfDriverEntry g_effectModelDriverEntry = {
134094332d3Sopenharmony_ci    .moduleVersion = 1,
135094332d3Sopenharmony_ci    .moduleName = "effect_model_service",
136094332d3Sopenharmony_ci    .Bind = HdfEffectModelDriverBind,
137094332d3Sopenharmony_ci    .Init = HdfEffectDriverInit,
138094332d3Sopenharmony_ci    .Release = HdfEffectModelDriverRelease,
139094332d3Sopenharmony_ci};
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_ciHDF_INIT(g_effectModelDriverEntry);