1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022-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_desc.h>
18094332d3Sopenharmony_ci#include <hdf_sbuf_ipc.h>
19094332d3Sopenharmony_ci#include "vibrator_uhdf_log.h"
20094332d3Sopenharmony_ci#include <osal_mem.h>
21094332d3Sopenharmony_ci#include "v1_3/vibrator_interface_stub.h"
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_ci#define HDF_LOG_TAG    uhdf_vibrator_service
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ciusing namespace OHOS::HDI::Vibrator::V1_3;
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cistruct HdfVibratorInterfaceHost {
28094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
29094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
30094332d3Sopenharmony_ci};
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_cistatic int32_t VibratorInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
33094332d3Sopenharmony_ci    struct HdfSBuf *reply)
34094332d3Sopenharmony_ci{
35094332d3Sopenharmony_ci    auto *hdfVibratorInterfaceHost = CONTAINER_OF(client->device->service, struct HdfVibratorInterfaceHost, ioService);
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ci    OHOS::MessageParcel *dataParcel = nullptr;
38094332d3Sopenharmony_ci    OHOS::MessageParcel *replyParcel = nullptr;
39094332d3Sopenharmony_ci    OHOS::MessageOption option;
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
42094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
43094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
44094332d3Sopenharmony_ci    }
45094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
46094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
47094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
48094332d3Sopenharmony_ci    }
49094332d3Sopenharmony_ci
50094332d3Sopenharmony_ci    return hdfVibratorInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
51094332d3Sopenharmony_ci}
52094332d3Sopenharmony_ci
53094332d3Sopenharmony_cistatic int HdfVibratorInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
54094332d3Sopenharmony_ci{
55094332d3Sopenharmony_ci    HDF_LOGI("HdfVibratorInterfaceDriverInit enter");
56094332d3Sopenharmony_ci    return HDF_SUCCESS;
57094332d3Sopenharmony_ci}
58094332d3Sopenharmony_ci
59094332d3Sopenharmony_cistatic int HdfVibratorInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
60094332d3Sopenharmony_ci{
61094332d3Sopenharmony_ci    auto *hdfVibratorInterfaceHost = new (std::nothrow) HdfVibratorInterfaceHost;
62094332d3Sopenharmony_ci    if (hdfVibratorInterfaceHost == nullptr) {
63094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to create HdfVibratorInterfaceHost object", __func__);
64094332d3Sopenharmony_ci        return HDF_FAILURE;
65094332d3Sopenharmony_ci    }
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    hdfVibratorInterfaceHost->ioService.Dispatch = VibratorInterfaceDriverDispatch;
68094332d3Sopenharmony_ci    hdfVibratorInterfaceHost->ioService.Open = nullptr;
69094332d3Sopenharmony_ci    hdfVibratorInterfaceHost->ioService.Release = nullptr;
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci    auto serviceImpl = OHOS::HDI::Vibrator::V1_3::IVibratorInterface::Get(true);
72094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
73094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
74094332d3Sopenharmony_ci        delete hdfVibratorInterfaceHost;
75094332d3Sopenharmony_ci        return HDF_FAILURE;
76094332d3Sopenharmony_ci    }
77094332d3Sopenharmony_ci
78094332d3Sopenharmony_ci    hdfVibratorInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
79094332d3Sopenharmony_ci        OHOS::HDI::Vibrator::V1_3::IVibratorInterface::GetDescriptor());
80094332d3Sopenharmony_ci    if (hdfVibratorInterfaceHost->stub == nullptr) {
81094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get stub object", __func__);
82094332d3Sopenharmony_ci        delete hdfVibratorInterfaceHost;
83094332d3Sopenharmony_ci        return HDF_FAILURE;
84094332d3Sopenharmony_ci    }
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    deviceObject->service = &hdfVibratorInterfaceHost->ioService;
87094332d3Sopenharmony_ci    HDF_LOGI("HdfVibratorInterfaceDriverBind Success");
88094332d3Sopenharmony_ci    return HDF_SUCCESS;
89094332d3Sopenharmony_ci}
90094332d3Sopenharmony_ci
91094332d3Sopenharmony_cistatic void HdfVibratorInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
92094332d3Sopenharmony_ci{
93094332d3Sopenharmony_ci    HDF_LOGI("HdfVibratorInterfaceDriverRelease enter");
94094332d3Sopenharmony_ci    if (deviceObject->service == nullptr) {
95094332d3Sopenharmony_ci        HDF_LOGE("HdfVibratorInterfaceDriverRelease not initted");
96094332d3Sopenharmony_ci        return;
97094332d3Sopenharmony_ci    }
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_ci    auto *hdfVibratorInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfVibratorInterfaceHost, ioService);
100094332d3Sopenharmony_ci    delete hdfVibratorInterfaceHost;
101094332d3Sopenharmony_ci}
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_cistatic struct HdfDriverEntry g_vibratorInterfaceDriverEntry = {
104094332d3Sopenharmony_ci    .moduleVersion = 1,
105094332d3Sopenharmony_ci    .moduleName = "vibrator_service",
106094332d3Sopenharmony_ci    .Bind = HdfVibratorInterfaceDriverBind,
107094332d3Sopenharmony_ci    .Init = HdfVibratorInterfaceDriverInit,
108094332d3Sopenharmony_ci    .Release = HdfVibratorInterfaceDriverRelease,
109094332d3Sopenharmony_ci};
110094332d3Sopenharmony_ci
111094332d3Sopenharmony_ci#ifdef __cplusplus
112094332d3Sopenharmony_ciextern "C" {
113094332d3Sopenharmony_ci#endif /* __cplusplus */
114094332d3Sopenharmony_ciHDF_INIT(g_vibratorInterfaceDriverEntry);
115094332d3Sopenharmony_ci#ifdef __cplusplus
116094332d3Sopenharmony_ci}
117094332d3Sopenharmony_ci#endif /* __cplusplus */
118