1 /*
2 * Copyright 2016 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #define LOG_TAG "android.hardware.bluetooth@1.1-impl"
18 #include "bluetooth_hci.h"
19
20 #include <log/log.h>
21
22 #include "vendor_interface.h"
23
24 using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
25
26 namespace android {
27 namespace hardware {
28 namespace bluetooth {
29 namespace V1_1 {
30 namespace implementation {
31
32 static const uint8_t HCI_DATA_TYPE_COMMAND = 1;
33 static const uint8_t HCI_DATA_TYPE_ACL = 2;
34 static const uint8_t HCI_DATA_TYPE_SCO = 3;
35 static const uint8_t HCI_DATA_TYPE_ISO = 5;
36
37 class BluetoothDeathRecipient : public hidl_death_recipient {
38 public:
BluetoothDeathRecipient(const sp<IBluetoothHci> hci)39 BluetoothDeathRecipient(const sp<IBluetoothHci> hci) : mHci(hci) {}
40
serviceDied(uint64_t,const wp<::android::hidl::base::V1_0::IBase> &)41 virtual void serviceDied(
42 uint64_t /*cookie*/,
43 const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
44 ALOGE("BluetoothDeathRecipient::serviceDied - Bluetooth service died");
45 has_died_ = true;
46 mHci->close();
47 }
48 sp<IBluetoothHci> mHci;
getHasDied() const49 bool getHasDied() const { return has_died_; }
setHasDied(bool has_died)50 void setHasDied(bool has_died) { has_died_ = has_died; }
51
52 private:
53 bool has_died_;
54 };
55
BluetoothHci()56 BluetoothHci::BluetoothHci()
57 : death_recipient_(new BluetoothDeathRecipient(this)) {}
58
initialize_1_1(const::android::sp<V1_1::IBluetoothHciCallbacks> & cb)59 Return<void> BluetoothHci::initialize_1_1(
60 const ::android::sp<V1_1::IBluetoothHciCallbacks>& cb) {
61 ALOGI("BluetoothHci::initialize_1_1()");
62 if (cb == nullptr) {
63 ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
64 return Void();
65 }
66
67 death_recipient_->setHasDied(false);
68 cb->linkToDeath(death_recipient_, 0);
69
70 bool rc = VendorInterface::Initialize(
71 [cb](bool status) {
72 auto hidl_status = cb->initializationComplete(
73 status ? V1_0::Status::SUCCESS
74 : V1_0::Status::INITIALIZATION_ERROR);
75 if (!hidl_status.isOk()) {
76 ALOGE("VendorInterface -> Unable to call initializationComplete()");
77 }
78 },
79 [cb](const hidl_vec<uint8_t>& packet) {
80 auto hidl_status = cb->hciEventReceived(packet);
81 if (!hidl_status.isOk()) {
82 ALOGE("VendorInterface -> Unable to call hciEventReceived()");
83 }
84 },
85 [cb](const hidl_vec<uint8_t>& packet) {
86 auto hidl_status = cb->aclDataReceived(packet);
87 if (!hidl_status.isOk()) {
88 ALOGE("VendorInterface -> Unable to call aclDataReceived()");
89 }
90 },
91 [cb](const hidl_vec<uint8_t>& packet) {
92 auto hidl_status = cb->scoDataReceived(packet);
93 if (!hidl_status.isOk()) {
94 ALOGE("VendorInterface -> Unable to call scoDataReceived()");
95 }
96 },
97 [cb](const hidl_vec<uint8_t>& packet) {
98 auto hidl_status = cb->isoDataReceived(packet);
99 if (!hidl_status.isOk()) {
100 ALOGE("VendorInterface -> Unable to call isoDataReceived()");
101 }
102 });
103 if (!rc) {
104 auto hidl_status =
105 cb->initializationComplete(V1_0::Status::INITIALIZATION_ERROR);
106 if (!hidl_status.isOk()) {
107 ALOGE("VendorInterface -> Unable to call initializationComplete(ERR)");
108 }
109 }
110
111 unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
112 if (death_recipient->getHasDied())
113 ALOGI("Skipping unlink call, service died.");
114 else
115 cb->unlinkToDeath(death_recipient);
116 };
117
118 return Void();
119 }
120
121 class OldCbWrapper : public V1_1::IBluetoothHciCallbacks {
122 public:
123 const ::android::sp<V1_0::IBluetoothHciCallbacks> old_cb_;
OldCbWrapper(const::android::sp<V1_0::IBluetoothHciCallbacks> & old_cb)124 OldCbWrapper(const ::android::sp<V1_0::IBluetoothHciCallbacks>& old_cb)
125 : old_cb_(old_cb) {}
126
127 virtual ~OldCbWrapper() = default;
128
initializationComplete(V1_0::Status status)129 Return<void> initializationComplete(V1_0::Status status) override {
130 return old_cb_->initializationComplete(status);
131 };
132
hciEventReceived(const::android::hardware::hidl_vec<uint8_t> & event)133 Return<void> hciEventReceived(
134 const ::android::hardware::hidl_vec<uint8_t>& event) override {
135 return old_cb_->hciEventReceived(event);
136 };
137
aclDataReceived(const::android::hardware::hidl_vec<uint8_t> & data)138 Return<void> aclDataReceived(
139 const ::android::hardware::hidl_vec<uint8_t>& data) override {
140 return old_cb_->aclDataReceived(data);
141 };
142
scoDataReceived(const::android::hardware::hidl_vec<uint8_t> & data)143 Return<void> scoDataReceived(
144 const ::android::hardware::hidl_vec<uint8_t>& data) override {
145 return old_cb_->scoDataReceived(data);
146 };
147
isoDataReceived(const::android::hardware::hidl_vec<uint8_t> &)148 Return<void> isoDataReceived(
149 const ::android::hardware::hidl_vec<uint8_t>&) override {
150 ALOGE("Please use HAL V1_1 for ISO.");
151 return Void();
152 };
153 };
154
initialize(const::android::sp<V1_0::IBluetoothHciCallbacks> & cb)155 Return<void> BluetoothHci::initialize(
156 const ::android::sp<V1_0::IBluetoothHciCallbacks>& cb) {
157 ALOGE("Using initialize from HAL V1_0 instead of initialize_1_1.");
158 return initialize_1_1(new OldCbWrapper(cb));
159 }
160
close()161 Return<void> BluetoothHci::close() {
162 ALOGI("BluetoothHci::close()");
163 unlink_cb_(death_recipient_);
164 VendorInterface::Shutdown();
165 return Void();
166 }
167
sendHciCommand(const hidl_vec<uint8_t> & command)168 Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& command) {
169 sendDataToController(HCI_DATA_TYPE_COMMAND, command);
170 return Void();
171 }
172
sendAclData(const hidl_vec<uint8_t> & data)173 Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& data) {
174 sendDataToController(HCI_DATA_TYPE_ACL, data);
175 return Void();
176 }
177
sendScoData(const hidl_vec<uint8_t> & data)178 Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {
179 sendDataToController(HCI_DATA_TYPE_SCO, data);
180 return Void();
181 }
182
sendIsoData(const hidl_vec<uint8_t> & data)183 Return<void> BluetoothHci::sendIsoData(const hidl_vec<uint8_t>& data) {
184 sendDataToController(HCI_DATA_TYPE_ISO, data);
185 return Void();
186 }
187
sendDataToController(const uint8_t type,const hidl_vec<uint8_t> & data)188 void BluetoothHci::sendDataToController(const uint8_t type,
189 const hidl_vec<uint8_t>& data) {
190 VendorInterface::get()->Send(type, data.data(), data.size());
191 }
192
193 } // namespace implementation
194 } // namespace V1_1
195 } // namespace bluetooth
196 } // namespace hardware
197 } // namespace android
198