1 /*
2  * Copyright (c) 2022-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_sbuf_ipc.h>
19 #include "vibrator_uhdf_log.h"
20 #include <osal_mem.h>
21 #include "v1_3/vibrator_interface_stub.h"
22 
23 #define HDF_LOG_TAG    uhdf_vibrator_service
24 
25 using namespace OHOS::HDI::Vibrator::V1_3;
26 
27 struct HdfVibratorInterfaceHost {
28     struct IDeviceIoService ioService;
29     OHOS::sptr<OHOS::IRemoteObject> stub;
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     auto *hdfVibratorInterfaceHost = CONTAINER_OF(client->device->service, struct HdfVibratorInterfaceHost, 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 hdfVibratorInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
51 }
52 
HdfVibratorInterfaceDriverInit(struct HdfDeviceObject * deviceObject)53 static int HdfVibratorInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
54 {
55     HDF_LOGI("HdfVibratorInterfaceDriverInit enter");
56     return HDF_SUCCESS;
57 }
58 
HdfVibratorInterfaceDriverBind(struct HdfDeviceObject * deviceObject)59 static int HdfVibratorInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
60 {
61     auto *hdfVibratorInterfaceHost = new (std::nothrow) HdfVibratorInterfaceHost;
62     if (hdfVibratorInterfaceHost == nullptr) {
63         HDF_LOGE("%{public}s: failed to create HdfVibratorInterfaceHost object", __func__);
64         return HDF_FAILURE;
65     }
66 
67     hdfVibratorInterfaceHost->ioService.Dispatch = VibratorInterfaceDriverDispatch;
68     hdfVibratorInterfaceHost->ioService.Open = nullptr;
69     hdfVibratorInterfaceHost->ioService.Release = nullptr;
70 
71     auto serviceImpl = OHOS::HDI::Vibrator::V1_3::IVibratorInterface::Get(true);
72     if (serviceImpl == nullptr) {
73         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
74         delete hdfVibratorInterfaceHost;
75         return HDF_FAILURE;
76     }
77 
78     hdfVibratorInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
79         OHOS::HDI::Vibrator::V1_3::IVibratorInterface::GetDescriptor());
80     if (hdfVibratorInterfaceHost->stub == nullptr) {
81         HDF_LOGE("%{public}s: failed to get stub object", __func__);
82         delete hdfVibratorInterfaceHost;
83         return HDF_FAILURE;
84     }
85 
86     deviceObject->service = &hdfVibratorInterfaceHost->ioService;
87     HDF_LOGI("HdfVibratorInterfaceDriverBind Success");
88     return HDF_SUCCESS;
89 }
90 
HdfVibratorInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)91 static void HdfVibratorInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
92 {
93     HDF_LOGI("HdfVibratorInterfaceDriverRelease enter");
94     if (deviceObject->service == nullptr) {
95         HDF_LOGE("HdfVibratorInterfaceDriverRelease not initted");
96         return;
97     }
98 
99     auto *hdfVibratorInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfVibratorInterfaceHost, ioService);
100     delete hdfVibratorInterfaceHost;
101 }
102 
103 static struct HdfDriverEntry g_vibratorInterfaceDriverEntry = {
104     .moduleVersion = 1,
105     .moduleName = "vibrator_service",
106     .Bind = HdfVibratorInterfaceDriverBind,
107     .Init = HdfVibratorInterfaceDriverInit,
108     .Release = HdfVibratorInterfaceDriverRelease,
109 };
110 
111 #ifdef __cplusplus
112 extern "C" {
113 #endif /* __cplusplus */
114 HDF_INIT(g_vibratorInterfaceDriverEntry);
115 #ifdef __cplusplus
116 }
117 #endif /* __cplusplus */
118