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