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_log.h>
19 #include <hdf_sbuf_ipc.h>
20 #include "v1_0/motion_interface_stub.h"
21
22 using namespace OHOS::HDI::Motion::V1_0;
23
24 struct HdfMotionInterfaceHost {
25 struct IDeviceIoService ioService;
26 OHOS::sptr<OHOS::IRemoteObject> stub;
27 };
28
MotionInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)29 static int32_t MotionInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
30 struct HdfSBuf *reply)
31 {
32 auto *hdfMotionInterfaceHost = CONTAINER_OF(client->device->service, struct HdfMotionInterfaceHost, 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 hdfMotionInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
48 }
49
HdfMotionInterfaceDriverInit(struct HdfDeviceObject * deviceObject)50 static int32_t HdfMotionInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
51 {
52 (void)deviceObject;
53 HDF_LOGI("HdfMotionInterfaceDriverInit enter");
54 return HDF_SUCCESS;
55 }
56
HdfMotionInterfaceDriverBind(struct HdfDeviceObject * deviceObject)57 static int32_t HdfMotionInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
58 {
59 HDF_LOGI("HdfMotionInterfaceDriverBind enter");
60
61 auto *hdfMotionInterfaceHost = new (std::nothrow) HdfMotionInterfaceHost;
62 if (hdfMotionInterfaceHost == nullptr) {
63 HDF_LOGE("%{public}s: failed to create create HdfMotionInterfaceHost object", __func__);
64 return HDF_FAILURE;
65 }
66
67 hdfMotionInterfaceHost->ioService.Dispatch = MotionInterfaceDriverDispatch;
68 hdfMotionInterfaceHost->ioService.Open = NULL;
69 hdfMotionInterfaceHost->ioService.Release = NULL;
70
71 auto serviceImpl = IMotionInterface::Get(true);
72 if (serviceImpl == nullptr) {
73 HDF_LOGE("%{public}s: failed to get of implement service", __func__);
74 return HDF_FAILURE;
75 }
76
77 hdfMotionInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
78 IMotionInterface::GetDescriptor());
79 if (hdfMotionInterfaceHost->stub == nullptr) {
80 HDF_LOGE("%{public}s: failed to get stub object", __func__);
81 return HDF_FAILURE;
82 }
83
84 deviceObject->service = &hdfMotionInterfaceHost->ioService;
85 return HDF_SUCCESS;
86 }
87
HdfMotionInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)88 static void HdfMotionInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
89 {
90 HDF_LOGI("HdfMotionInterfaceDriverRelease enter");
91 auto *hdfMotionInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfMotionInterfaceHost, ioService);
92 delete hdfMotionInterfaceHost;
93 hdfMotionInterfaceHost = nullptr;
94 }
95
96 struct HdfDriverEntry g_motioninterfaceDriverEntry = {
97 .moduleVersion = 1,
98 .moduleName = "motion_service",
99 .Bind = HdfMotionInterfaceDriverBind,
100 .Init = HdfMotionInterfaceDriverInit,
101 .Release = HdfMotionInterfaceDriverRelease,
102 };
103
104 #ifdef __cplusplus
105 extern "C" {
106 #endif /* __cplusplus */
107 HDF_INIT(g_motioninterfaceDriverEntry);
108 #ifdef __cplusplus
109 }
110 #endif /* __cplusplus */
111