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 "v2_0/geofence_interface_stub.h"
21 
22 using namespace OHOS::HDI::Location::Geofence::V2_0;
23 
24 struct HdfGeofenceInterfaceHost {
25     struct IDeviceIoService ioService;
26     OHOS::sptr<OHOS::IRemoteObject> stub;
27 
HdfGeofenceInterfaceHostHdfGeofenceInterfaceHost28     HdfGeofenceInterfaceHost()
29     {
30         ioService.object.objectId = 0;
31         ioService.Open = nullptr;
32         ioService.Release = nullptr;
33         ioService.Dispatch = nullptr;
34     }
35 };
36 
GeofenceInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)37 static int32_t GeofenceInterfaceDriverDispatch(struct HdfDeviceIoClient *client,
38     int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
39 {
40     auto *hdfGeofenceInterfaceHost = CONTAINER_OF(client->device->service, struct HdfGeofenceInterfaceHost, ioService);
41 
42     OHOS::MessageParcel *dataParcel = nullptr;
43     OHOS::MessageParcel *replyParcel = nullptr;
44     OHOS::MessageOption option;
45 
46     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
47         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
48         return HDF_ERR_INVALID_PARAM;
49     }
50     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
51         HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
52         return HDF_ERR_INVALID_PARAM;
53     }
54 
55     return hdfGeofenceInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
56 }
57 
HdfGeofenceInterfaceDriverInit(struct HdfDeviceObject * deviceObject)58 static int HdfGeofenceInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
59 {
60     HDF_LOGI("HdfGeofenceInterfaceDriverInit enter");
61     return HDF_SUCCESS;
62 }
63 
HdfGeofenceInterfaceDriverBind(struct HdfDeviceObject * deviceObject)64 static int HdfGeofenceInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
65 {
66     HDF_LOGI("HdfGeofenceInterfaceDriverBind enter");
67 
68     auto *hdfGeofenceInterfaceHost = new (std::nothrow) HdfGeofenceInterfaceHost;
69     if (hdfGeofenceInterfaceHost == nullptr) {
70         HDF_LOGE("%{public}s: failed to create create HdfGeofenceInterfaceHost object", __func__);
71         return HDF_FAILURE;
72     }
73 
74     hdfGeofenceInterfaceHost->ioService.Dispatch = GeofenceInterfaceDriverDispatch;
75 
76     auto serviceImpl = IGeofenceInterface::Get(true);
77     if (serviceImpl == nullptr) {
78         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
79         delete hdfGeofenceInterfaceHost;
80         return HDF_FAILURE;
81     }
82 
83     hdfGeofenceInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
84         IGeofenceInterface::GetDescriptor());
85     if (hdfGeofenceInterfaceHost->stub == nullptr) {
86         HDF_LOGE("%{public}s: failed to get stub object", __func__);
87         delete hdfGeofenceInterfaceHost;
88         return HDF_FAILURE;
89     }
90 
91     deviceObject->service = &hdfGeofenceInterfaceHost->ioService;
92     return HDF_SUCCESS;
93 }
94 
HdfGeofenceInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)95 static void HdfGeofenceInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
96 {
97     HDF_LOGI("HdfGeofenceInterfaceDriverRelease enter");
98     if (deviceObject->service == nullptr) {
99         HDF_LOGE("HdfGeofenceInterfaceDriverRelease not initted");
100         return;
101     }
102 
103     auto *hdfGeofenceInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfGeofenceInterfaceHost, ioService);
104     delete hdfGeofenceInterfaceHost;
105     deviceObject->service = nullptr;
106 }
107 
108 static struct HdfDriverEntry g_geofenceinterfaceDriverEntry = {
109     .moduleVersion = 1,
110     .moduleName = "location_geofence",
111     .Bind = HdfGeofenceInterfaceDriverBind,
112     .Init = HdfGeofenceInterfaceDriverInit,
113     .Release = HdfGeofenceInterfaceDriverRelease,
114 };
115 
116 #ifdef __cplusplus
117 extern "C" {
118 #endif /* __cplusplus */
119 HDF_INIT(g_geofenceinterfaceDriverEntry);
120 #ifdef __cplusplus
121 }
122 #endif /* __cplusplus */
123