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