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