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/gnss_interface_stub.h"
21 
22 using namespace OHOS::HDI::Location::Gnss::V2_0;
23 
24 struct HdfGnssInterfaceHost {
25     struct IDeviceIoService ioService;
26     OHOS::sptr<OHOS::IRemoteObject> stub;
27 
HdfGnssInterfaceHostHdfGnssInterfaceHost28     HdfGnssInterfaceHost()
29     {
30         ioService.object.objectId = 0;
31         ioService.Open = nullptr;
32         ioService.Release = nullptr;
33         ioService.Dispatch = nullptr;
34     }
35 };
36 
GnssInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)37 static int32_t GnssInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
38     struct HdfSBuf *reply)
39 {
40     auto *hdfGnssInterfaceHost = CONTAINER_OF(client->device->service, struct HdfGnssInterfaceHost, 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 hdfGnssInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
56 }
57 
HdfGnssInterfaceDriverInit(struct HdfDeviceObject * deviceObject)58 static int HdfGnssInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
59 {
60     HDF_LOGI("HdfGnssInterfaceDriverInit enter");
61     return HDF_SUCCESS;
62 }
63 
HdfGnssInterfaceDriverBind(struct HdfDeviceObject * deviceObject)64 static int HdfGnssInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
65 {
66     HDF_LOGI("HdfGnssInterfaceDriverBind enter");
67 
68     auto *hdfGnssInterfaceHost = new (std::nothrow) HdfGnssInterfaceHost;
69     if (hdfGnssInterfaceHost == nullptr) {
70         HDF_LOGE("%{public}s: failed to create create HdfGnssInterfaceHost object", __func__);
71         return HDF_FAILURE;
72     }
73 
74     hdfGnssInterfaceHost->ioService.Dispatch = GnssInterfaceDriverDispatch;
75 
76     auto serviceImpl = IGnssInterface::Get(true);
77     if (serviceImpl == nullptr) {
78         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
79         delete hdfGnssInterfaceHost;
80         return HDF_FAILURE;
81     }
82 
83     hdfGnssInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
84         IGnssInterface::GetDescriptor());
85     if (hdfGnssInterfaceHost->stub == nullptr) {
86         HDF_LOGE("%{public}s: failed to get stub object", __func__);
87         delete hdfGnssInterfaceHost;
88         return HDF_FAILURE;
89     }
90 
91     deviceObject->service = &hdfGnssInterfaceHost->ioService;
92     return HDF_SUCCESS;
93 }
94 
HdfGnssInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)95 static void HdfGnssInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
96 {
97     HDF_LOGI("HdfGnssInterfaceDriverRelease enter");
98     if (deviceObject->service == nullptr) {
99         HDF_LOGE("HdfGnssInterfaceDriverRelease not initted");
100         return;
101     }
102 
103     auto *hdfGnssInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfGnssInterfaceHost, ioService);
104     delete hdfGnssInterfaceHost;
105     deviceObject->service = nullptr;
106 }
107 
108 static struct HdfDriverEntry g_gnssinterfaceDriverEntry = {
109     .moduleVersion = 1,
110     .moduleName = "location_gnss",
111     .Bind = HdfGnssInterfaceDriverBind,
112     .Init = HdfGnssInterfaceDriverInit,
113     .Release = HdfGnssInterfaceDriverRelease,
114 };
115 
116 #ifdef __cplusplus
117 extern "C" {
118 #endif /* __cplusplus */
119 HDF_INIT(g_gnssinterfaceDriverEntry);
120 #ifdef __cplusplus
121 }
122 #endif /* __cplusplus */
123