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