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