blob: 869c7231ca93a0e262d3921de28dccd673c2bbcc [file] [log] [blame]
Andre Eisenbach89ba5282016-10-13 15:45:02 -07001//
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.0-impl"
Myles Watson9cec0e32017-03-16 16:23:23 -070018#include "bluetooth_hci.h"
19
Steven Moreland96510c82017-04-11 12:22:27 -070020#include <log/log.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070021
Andre Eisenbach89ba5282016-10-13 15:45:02 -070022#include "vendor_interface.h"
23
24namespace android {
25namespace hardware {
26namespace bluetooth {
27namespace V1_0 {
28namespace implementation {
29
30static const uint8_t HCI_DATA_TYPE_COMMAND = 1;
31static const uint8_t HCI_DATA_TYPE_ACL = 2;
32static const uint8_t HCI_DATA_TYPE_SCO = 3;
33
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070034class BluetoothDeathRecipient : public hidl_death_recipient {
35 public:
xiaoshun.xu1adfae92022-09-14 20:39:16 +080036 BluetoothDeathRecipient(const sp<IBluetoothHci> hci)
37 : mHci(hci), has_died_(false) {}
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070038
39 virtual void serviceDied(
40 uint64_t /*cookie*/,
41 const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
42 ALOGE("BluetoothDeathRecipient::serviceDied - Bluetooth service died");
Myles Watson9cec0e32017-03-16 16:23:23 -070043 has_died_ = true;
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070044 mHci->close();
45 }
46 sp<IBluetoothHci> mHci;
Myles Watson9cec0e32017-03-16 16:23:23 -070047 bool getHasDied() const { return has_died_; }
48 void setHasDied(bool has_died) { has_died_ = has_died; }
49
50 private:
51 bool has_died_;
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070052};
53
Martijn Coenen678af7f2017-02-23 17:25:18 +010054BluetoothHci::BluetoothHci()
xiaoshun.xu1adfae92022-09-14 20:39:16 +080055 : death_recipient_(new BluetoothDeathRecipient(this)) {bt_enabled = 0;}
Martijn Coenen678af7f2017-02-23 17:25:18 +010056
Andre Eisenbach9041d972017-01-17 18:23:12 -080057Return<void> BluetoothHci::initialize(
Andre Eisenbach89ba5282016-10-13 15:45:02 -070058 const ::android::sp<IBluetoothHciCallbacks>& cb) {
Myles Watson9cec0e32017-03-16 16:23:23 -070059 ALOGI("BluetoothHci::initialize()");
60 if (cb == nullptr) {
61 ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
62 return Void();
63 }
64
xiaoshun.xu1adfae92022-09-14 20:39:16 +080065 if (bt_enabled == 1) {
66 ALOGE("initialize was called!");
67 return Void();
68 }
69 bt_enabled = 1;
Myles Watson9cec0e32017-03-16 16:23:23 -070070 death_recipient_->setHasDied(false);
71 cb->linkToDeath(death_recipient_, 0);
xiaoshun.xu1adfae92022-09-14 20:39:16 +080072 unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
73 if (death_recipient->getHasDied())
74 ALOGI("Skipping unlink call, service died.");
75 else
76 cb->unlinkToDeath(death_recipient);
77 };
Andre Eisenbach89ba5282016-10-13 15:45:02 -070078
79 bool rc = VendorInterface::Initialize(
Myles Watson9cec0e32017-03-16 16:23:23 -070080 [cb](bool status) {
81 auto hidl_status = cb->initializationComplete(
Andre Eisenbach9041d972017-01-17 18:23:12 -080082 status ? Status::SUCCESS : Status::INITIALIZATION_ERROR);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070083 if (!hidl_status.isOk()) {
84 ALOGE("VendorInterface -> Unable to call initializationComplete()");
85 }
Andre Eisenbach9041d972017-01-17 18:23:12 -080086 },
Myles Watson9cec0e32017-03-16 16:23:23 -070087 [cb](const hidl_vec<uint8_t>& packet) {
88 auto hidl_status = cb->hciEventReceived(packet);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070089 if (!hidl_status.isOk()) {
90 ALOGE("VendorInterface -> Unable to call hciEventReceived()");
91 }
Zach Johnson917efb12017-02-26 23:46:05 -080092 },
Myles Watson9cec0e32017-03-16 16:23:23 -070093 [cb](const hidl_vec<uint8_t>& packet) {
94 auto hidl_status = cb->aclDataReceived(packet);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070095 if (!hidl_status.isOk()) {
96 ALOGE("VendorInterface -> Unable to call aclDataReceived()");
97 }
Zach Johnson917efb12017-02-26 23:46:05 -080098 },
Myles Watson9cec0e32017-03-16 16:23:23 -070099 [cb](const hidl_vec<uint8_t>& packet) {
100 auto hidl_status = cb->scoDataReceived(packet);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -0700101 if (!hidl_status.isOk()) {
102 ALOGE("VendorInterface -> Unable to call scoDataReceived()");
103 }
Jakub Pawlowski13b4d312019-11-05 12:27:29 +0100104 },
105 [cb](const hidl_vec<uint8_t>&) {
106 ALOGE("VendorInterface -> No callback for ISO packets in HAL V1_0");
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700107 });
Andre Eisenbach9f8931c2017-03-16 22:19:19 -0700108 if (!rc) {
Myles Watson9cec0e32017-03-16 16:23:23 -0700109 auto hidl_status = cb->initializationComplete(Status::INITIALIZATION_ERROR);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -0700110 if (!hidl_status.isOk()) {
111 ALOGE("VendorInterface -> Unable to call initializationComplete(ERR)");
112 }
113 }
Myles Watson9cec0e32017-03-16 16:23:23 -0700114
115 unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
116 if (death_recipient->getHasDied())
117 ALOGI("Skipping unlink call, service died.");
118 else
119 cb->unlinkToDeath(death_recipient);
120 };
121
Andre Eisenbach9041d972017-01-17 18:23:12 -0800122 return Void();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700123}
124
125Return<void> BluetoothHci::close() {
Myles Watson9cec0e32017-03-16 16:23:23 -0700126 ALOGI("BluetoothHci::close()");
xiaoshun.xu1adfae92022-09-14 20:39:16 +0800127
128 if (bt_enabled != 1) {
129 ALOGE("should initialize first!");
130 return Void();
131 }
132 bt_enabled = 0;
Myles Watson9cec0e32017-03-16 16:23:23 -0700133 unlink_cb_(death_recipient_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700134 VendorInterface::Shutdown();
135 return Void();
136}
137
138Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& command) {
139 sendDataToController(HCI_DATA_TYPE_COMMAND, command);
140 return Void();
141}
142
143Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& data) {
144 sendDataToController(HCI_DATA_TYPE_ACL, data);
145 return Void();
146}
147
148Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {
149 sendDataToController(HCI_DATA_TYPE_SCO, data);
150 return Void();
151}
152
153void BluetoothHci::sendDataToController(const uint8_t type,
154 const hidl_vec<uint8_t>& data) {
xiaoshun.xu1adfae92022-09-14 20:39:16 +0800155 if (bt_enabled != 1) {
156 ALOGE("should initialize first!");
157 return;
158 }
159
Myles Watsondf765ea2017-01-30 09:07:37 -0800160 VendorInterface::get()->Send(type, data.data(), data.size());
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700161}
162
163IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* /* name */) {
164 return new BluetoothHci();
165}
166
167} // namespace implementation
168} // namespace V1_0
169} // namespace bluetooth
170} // namespace hardware
171} // namespace android