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