2 // Copyright 2016 The Android Open Source Project
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #define LOG_TAG "android.hardware.bluetooth@1.0-impl"
18 #include "bluetooth_hci.h"
22 #include "vendor_interface.h"
28 namespace implementation
{
30 static const uint8_t HCI_DATA_TYPE_COMMAND
= 1;
31 static const uint8_t HCI_DATA_TYPE_ACL
= 2;
32 static const uint8_t HCI_DATA_TYPE_SCO
= 3;
34 class BluetoothDeathRecipient
: public hidl_death_recipient
{
36 BluetoothDeathRecipient(const sp
<IBluetoothHci
> hci
) : mHci(hci
) {}
38 virtual void serviceDied(
40 const wp
<::android::hidl::base::V1_0::IBase
>& /*who*/) {
41 ALOGE("BluetoothDeathRecipient::serviceDied - Bluetooth service died");
45 sp
<IBluetoothHci
> mHci
;
46 bool getHasDied() const { return has_died_
; }
47 void setHasDied(bool has_died
) { has_died_
= has_died
; }
53 BluetoothHci::BluetoothHci()
54 : death_recipient_(new BluetoothDeathRecipient(this)) {}
56 Return
<void> BluetoothHci::initialize(
57 const ::android::sp
<IBluetoothHciCallbacks
>& cb
) {
58 ALOGI("BluetoothHci::initialize()");
60 ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
64 death_recipient_
->setHasDied(false);
65 cb
->linkToDeath(death_recipient_
, 0);
67 bool rc
= VendorInterface::Initialize(
69 auto hidl_status
= cb
->initializationComplete(
70 status
? Status::SUCCESS
: Status::INITIALIZATION_ERROR
);
71 if (!hidl_status
.isOk()) {
72 ALOGE("VendorInterface -> Unable to call initializationComplete()");
75 [cb
](const hidl_vec
<uint8_t>& packet
) {
76 auto hidl_status
= cb
->hciEventReceived(packet
);
77 if (!hidl_status
.isOk()) {
78 ALOGE("VendorInterface -> Unable to call hciEventReceived()");
81 [cb
](const hidl_vec
<uint8_t>& packet
) {
82 auto hidl_status
= cb
->aclDataReceived(packet
);
83 if (!hidl_status
.isOk()) {
84 ALOGE("VendorInterface -> Unable to call aclDataReceived()");
87 [cb
](const hidl_vec
<uint8_t>& packet
) {
88 auto hidl_status
= cb
->scoDataReceived(packet
);
89 if (!hidl_status
.isOk()) {
90 ALOGE("VendorInterface -> Unable to call scoDataReceived()");
93 [cb
](const hidl_vec
<uint8_t>&) {
94 ALOGE("VendorInterface -> No callback for ISO packets in HAL V1_0");
97 auto hidl_status
= cb
->initializationComplete(Status::INITIALIZATION_ERROR
);
98 if (!hidl_status
.isOk()) {
99 ALOGE("VendorInterface -> Unable to call initializationComplete(ERR)");
103 unlink_cb_
= [cb
](sp
<BluetoothDeathRecipient
>& death_recipient
) {
104 if (death_recipient
->getHasDied())
105 ALOGI("Skipping unlink call, service died.");
107 cb
->unlinkToDeath(death_recipient
);
113 Return
<void> BluetoothHci::close() {
114 ALOGI("BluetoothHci::close()");
115 unlink_cb_(death_recipient_
);
116 VendorInterface::Shutdown();
120 Return
<void> BluetoothHci::sendHciCommand(const hidl_vec
<uint8_t>& command
) {
121 sendDataToController(HCI_DATA_TYPE_COMMAND
, command
);
125 Return
<void> BluetoothHci::sendAclData(const hidl_vec
<uint8_t>& data
) {
126 sendDataToController(HCI_DATA_TYPE_ACL
, data
);
130 Return
<void> BluetoothHci::sendScoData(const hidl_vec
<uint8_t>& data
) {
131 sendDataToController(HCI_DATA_TYPE_SCO
, data
);
135 void BluetoothHci::sendDataToController(const uint8_t type
,
136 const hidl_vec
<uint8_t>& data
) {
137 VendorInterface::get()->Send(type
, data
.data(), data
.size());
140 IBluetoothHci
* HIDL_FETCH_IBluetoothHci(const char* /* name */) {
141 return new BluetoothHci();
144 } // namespace implementation
146 } // namespace bluetooth
147 } // namespace hardware
148 } // namespace android