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