1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <hdf_base.h>
17094332d3Sopenharmony_ci#include <hdf_device_desc.h>
18094332d3Sopenharmony_ci#include <hdf_log.h>
19094332d3Sopenharmony_ci#include <hdf_sbuf_ipc.h>
20094332d3Sopenharmony_ci#include "v2_0/geofence_interface_stub.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ciusing namespace OHOS::HDI::Location::Geofence::V2_0;
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_cistruct HdfGeofenceInterfaceHost {
25094332d3Sopenharmony_ci    struct IDeviceIoService ioService;
26094332d3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> stub;
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_ci    HdfGeofenceInterfaceHost()
29094332d3Sopenharmony_ci    {
30094332d3Sopenharmony_ci        ioService.object.objectId = 0;
31094332d3Sopenharmony_ci        ioService.Open = nullptr;
32094332d3Sopenharmony_ci        ioService.Release = nullptr;
33094332d3Sopenharmony_ci        ioService.Dispatch = nullptr;
34094332d3Sopenharmony_ci    }
35094332d3Sopenharmony_ci};
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_cistatic int32_t GeofenceInterfaceDriverDispatch(struct HdfDeviceIoClient *client,
38094332d3Sopenharmony_ci    int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
39094332d3Sopenharmony_ci{
40094332d3Sopenharmony_ci    auto *hdfGeofenceInterfaceHost = CONTAINER_OF(client->device->service, struct HdfGeofenceInterfaceHost, ioService);
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ci    OHOS::MessageParcel *dataParcel = nullptr;
43094332d3Sopenharmony_ci    OHOS::MessageParcel *replyParcel = nullptr;
44094332d3Sopenharmony_ci    OHOS::MessageOption option;
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_ci    if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
47094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
48094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
49094332d3Sopenharmony_ci    }
50094332d3Sopenharmony_ci    if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
51094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
52094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
53094332d3Sopenharmony_ci    }
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_ci    return hdfGeofenceInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
56094332d3Sopenharmony_ci}
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_cistatic int HdfGeofenceInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
59094332d3Sopenharmony_ci{
60094332d3Sopenharmony_ci    HDF_LOGI("HdfGeofenceInterfaceDriverInit enter");
61094332d3Sopenharmony_ci    return HDF_SUCCESS;
62094332d3Sopenharmony_ci}
63094332d3Sopenharmony_ci
64094332d3Sopenharmony_cistatic int HdfGeofenceInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
65094332d3Sopenharmony_ci{
66094332d3Sopenharmony_ci    HDF_LOGI("HdfGeofenceInterfaceDriverBind enter");
67094332d3Sopenharmony_ci
68094332d3Sopenharmony_ci    auto *hdfGeofenceInterfaceHost = new (std::nothrow) HdfGeofenceInterfaceHost;
69094332d3Sopenharmony_ci    if (hdfGeofenceInterfaceHost == nullptr) {
70094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to create create HdfGeofenceInterfaceHost object", __func__);
71094332d3Sopenharmony_ci        return HDF_FAILURE;
72094332d3Sopenharmony_ci    }
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ci    hdfGeofenceInterfaceHost->ioService.Dispatch = GeofenceInterfaceDriverDispatch;
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_ci    auto serviceImpl = IGeofenceInterface::Get(true);
77094332d3Sopenharmony_ci    if (serviceImpl == nullptr) {
78094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get of implement service", __func__);
79094332d3Sopenharmony_ci        delete hdfGeofenceInterfaceHost;
80094332d3Sopenharmony_ci        return HDF_FAILURE;
81094332d3Sopenharmony_ci    }
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    hdfGeofenceInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
84094332d3Sopenharmony_ci        IGeofenceInterface::GetDescriptor());
85094332d3Sopenharmony_ci    if (hdfGeofenceInterfaceHost->stub == nullptr) {
86094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: failed to get stub object", __func__);
87094332d3Sopenharmony_ci        delete hdfGeofenceInterfaceHost;
88094332d3Sopenharmony_ci        return HDF_FAILURE;
89094332d3Sopenharmony_ci    }
90094332d3Sopenharmony_ci
91094332d3Sopenharmony_ci    deviceObject->service = &hdfGeofenceInterfaceHost->ioService;
92094332d3Sopenharmony_ci    return HDF_SUCCESS;
93094332d3Sopenharmony_ci}
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_cistatic void HdfGeofenceInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
96094332d3Sopenharmony_ci{
97094332d3Sopenharmony_ci    HDF_LOGI("HdfGeofenceInterfaceDriverRelease enter");
98094332d3Sopenharmony_ci    if (deviceObject->service == nullptr) {
99094332d3Sopenharmony_ci        HDF_LOGE("HdfGeofenceInterfaceDriverRelease not initted");
100094332d3Sopenharmony_ci        return;
101094332d3Sopenharmony_ci    }
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_ci    auto *hdfGeofenceInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfGeofenceInterfaceHost, ioService);
104094332d3Sopenharmony_ci    delete hdfGeofenceInterfaceHost;
105094332d3Sopenharmony_ci    deviceObject->service = nullptr;
106094332d3Sopenharmony_ci}
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_cistatic struct HdfDriverEntry g_geofenceinterfaceDriverEntry = {
109094332d3Sopenharmony_ci    .moduleVersion = 1,
110094332d3Sopenharmony_ci    .moduleName = "location_geofence",
111094332d3Sopenharmony_ci    .Bind = HdfGeofenceInterfaceDriverBind,
112094332d3Sopenharmony_ci    .Init = HdfGeofenceInterfaceDriverInit,
113094332d3Sopenharmony_ci    .Release = HdfGeofenceInterfaceDriverRelease,
114094332d3Sopenharmony_ci};
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_ci#ifdef __cplusplus
117094332d3Sopenharmony_ciextern "C" {
118094332d3Sopenharmony_ci#endif /* __cplusplus */
119094332d3Sopenharmony_ciHDF_INIT(g_geofenceinterfaceDriverEntry);
120094332d3Sopenharmony_ci#ifdef __cplusplus
121094332d3Sopenharmony_ci}
122094332d3Sopenharmony_ci#endif /* __cplusplus */
123