1/*
2 * Copyright (c) 2022 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_desc.h>
18#include <hdf_sbuf_ipc.h>
19#include "input_uhdf_log.h"
20#include "v1_0/input_interfaces_stub.h"
21
22using namespace OHOS::HDI::Input::V1_0;
23
24struct HdfInputInterfacesHost {
25    struct IDeviceIoService ioService;
26    OHOS::sptr<OHOS::IRemoteObject> stub;
27};
28
29static int32_t InputInterfacesDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
30    struct HdfSBuf *reply)
31{
32    auto *hdfInputInterfacesHost = CONTAINER_OF(client->device->service, struct HdfInputInterfacesHost, ioService);
33
34    OHOS::MessageParcel *dataParcel = nullptr;
35    OHOS::MessageParcel *replyParcel = nullptr;
36    OHOS::MessageOption option;
37
38    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
39        HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
40        return HDF_ERR_INVALID_PARAM;
41    }
42    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
43        HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
44        return HDF_ERR_INVALID_PARAM;
45    }
46
47    return hdfInputInterfacesHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
48}
49
50static int HdfInputInterfacesDriverInit(struct HdfDeviceObject *deviceObject)
51{
52    HDF_LOGI("HdfInputInterfacesDriverInit enter");
53    return HDF_SUCCESS;
54}
55
56static int HdfInputInterfacesDriverBind(struct HdfDeviceObject *deviceObject)
57{
58    HDF_LOGI("HdfInputInterfacesDriverBind enter");
59
60    auto *hdfInputInterfacesHost = new (std::nothrow) HdfInputInterfacesHost;
61    if (hdfInputInterfacesHost == nullptr) {
62        HDF_LOGE("%{public}s: failed to create create HdfInputInterfacesHost object", __func__);
63        return HDF_FAILURE;
64    }
65
66    hdfInputInterfacesHost->ioService.Dispatch = InputInterfacesDriverDispatch;
67    hdfInputInterfacesHost->ioService.Open = nullptr;
68    hdfInputInterfacesHost->ioService.Release = nullptr;
69
70    auto serviceImpl = IInputInterfaces::Get(true);
71    if (serviceImpl == nullptr) {
72        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
73        delete hdfInputInterfacesHost;
74        return HDF_FAILURE;
75    }
76
77    hdfInputInterfacesHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
78        IInputInterfaces::GetDescriptor());
79    if (hdfInputInterfacesHost->stub == nullptr) {
80        HDF_LOGE("%{public}s: failed to get stub object", __func__);
81        delete hdfInputInterfacesHost;
82        return HDF_FAILURE;
83    }
84
85    deviceObject->service = &hdfInputInterfacesHost->ioService;
86    return HDF_SUCCESS;
87}
88
89static void HdfInputInterfacesDriverRelease(struct HdfDeviceObject *deviceObject)
90{
91    HDF_LOGI("HdfInputInterfacesDriverRelease enter");
92    if (deviceObject->service == nullptr) {
93        HDF_LOGE("HdfInputInterfacesDriverRelease not initted");
94        return;
95    }
96
97    auto *hdfInputInterfacesHost = CONTAINER_OF(deviceObject->service, struct HdfInputInterfacesHost, ioService);
98    delete hdfInputInterfacesHost;
99}
100
101struct HdfDriverEntry g_inputinterfacesDriverEntry = {
102    .moduleVersion = 1,
103    .moduleName = "input_service",
104    .Bind = HdfInputInterfacesDriverBind,
105    .Init = HdfInputInterfacesDriverInit,
106    .Release = HdfInputInterfacesDriverRelease,
107};
108
109#ifdef __cplusplus
110extern "C" {
111#endif /* __cplusplus */
112HDF_INIT(g_inputinterfacesDriverEntry);
113#ifdef __cplusplus
114}
115#endif /* __cplusplus */
116