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 <hdf_log.h>
20 #include <osal_mem.h>
21 #include "vibrator_interface_impl.h"
22
23 #define HDF_LOG_TAG hdf_vibrator_if_driver
24
25 using namespace OHOS::HDI::Vibrator::V1_0;
26
27 struct HdfVibratorInterfaceHost {
28 struct IDeviceIoService ioservice;
29 VibratorInterfaceImpl *service;
30 };
31
VibratorInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int32_t VibratorInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
33 struct HdfSBuf *reply)
34 {
35 struct HdfVibratorInterfaceHost *hdfVibratorInterfaceHost = CONTAINER_OF(
36 client->device->service, struct HdfVibratorInterfaceHost, ioservice);
37
38 OHOS::MessageParcel *dataParcel = nullptr;
39 OHOS::MessageParcel *replyParcel = nullptr;
40 OHOS::MessageOption option;
41
42 (void)SbufToParcel(reply, &replyParcel);
43 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
44 HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
45 return HDF_ERR_INVALID_PARAM;
46 }
47
48 return hdfVibratorInterfaceHost->service->OnRemoteRequest(cmdId, *dataParcel, *replyParcel, option);
49 }
50
HdfVibratorInterfaceDriverInit(struct HdfDeviceObject * deviceObject)51 static int HdfVibratorInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
52 {
53 HDF_LOGI("HdfVibratorInterfaceDriverInit enter");
54 return HDF_SUCCESS;
55 }
56
HdfVibratorInterfaceDriverBind(struct HdfDeviceObject * deviceObject)57 static int HdfVibratorInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
58 {
59 struct HdfVibratorInterfaceHost *hdfVibratorInterfaceHost = (struct HdfVibratorInterfaceHost *)OsalMemAlloc(
60 sizeof(struct HdfVibratorInterfaceHost));
61 if (hdfVibratorInterfaceHost == nullptr) {
62 HDF_LOGE("HdfVibratorInterfaceDriverBind OsalMemAlloc HdfVibratorInterfaceHost failed!");
63 return HDF_FAILURE;
64 }
65
66 hdfVibratorInterfaceHost->ioservice.Dispatch = VibratorInterfaceDriverDispatch;
67 hdfVibratorInterfaceHost->ioservice.Open = NULL;
68 hdfVibratorInterfaceHost->ioservice.Release = NULL;
69 hdfVibratorInterfaceHost->service = new VibratorInterfaceImpl();
70
71 deviceObject->service = &hdfVibratorInterfaceHost->ioservice;
72 HDF_LOGI("HdfVibratorInterfaceDriverBind Success");
73 return HDF_SUCCESS;
74 }
75
HdfVibratorInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)76 static void HdfVibratorInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
77 {
78 HDF_LOGI("HdfVibratorInterfaceDriverRelease enter");
79
80 struct HdfVibratorInterfaceHost *hdfVibratorInterfaceHost = CONTAINER_OF(
81 deviceObject->service, struct HdfVibratorInterfaceHost, ioservice);
82 delete hdfVibratorInterfaceHost->service;
83 OsalMemFree(hdfVibratorInterfaceHost);
84 }
85
86 struct HdfDriverEntry g_vibratorInterfaceDriverEntry = {
87 .moduleVersion = 1,
88 .moduleName = "vibrator_service",
89 .Bind = HdfVibratorInterfaceDriverBind,
90 .Init = HdfVibratorInterfaceDriverInit,
91 .Release = HdfVibratorInterfaceDriverRelease,
92 };
93
94 #ifndef __cplusplus
95 extern "C" {
96 #endif
97 HDF_INIT(g_vibratorInterfaceDriverEntry);
98 #ifndef __cplusplus
99 }
100 #endif
101