blob: a2211f4e836ae5ea68df616d04fc451a3750b76d [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:
36 BluetoothDeathRecipient(const sp<IBluetoothHci> hci) : mHci(hci) {}
37
38 virtual void serviceDied(
39 uint64_t /*cookie*/,
40 const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
41 ALOGE("BluetoothDeathRecipient::serviceDied - Bluetooth service died");
Myles Watson9cec0e32017-03-16 16:23:23 -070042 has_died_ = true;
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070043 mHci->close();
44 }
45 sp<IBluetoothHci> mHci;
Myles Watson9cec0e32017-03-16 16:23:23 -070046 bool getHasDied() const { return has_died_; }
47 void setHasDied(bool has_died) { has_died_ = has_died; }
48
49 private:
50 bool has_died_;
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070051};
52
Martijn Coenen678af7f2017-02-23 17:25:18 +010053BluetoothHci::BluetoothHci()
Myles Watson9cec0e32017-03-16 16:23:23 -070054 : death_recipient_(new BluetoothDeathRecipient(this)) {}
Martijn Coenen678af7f2017-02-23 17:25:18 +010055
Andre Eisenbach9041d972017-01-17 18:23:12 -080056Return<void> BluetoothHci::initialize(
Andre Eisenbach89ba5282016-10-13 15:45:02 -070057 const ::android::sp<IBluetoothHciCallbacks>& cb) {
Myles Watson9cec0e32017-03-16 16:23:23 -070058 ALOGI("BluetoothHci::initialize()");
59 if (cb == nullptr) {
60 ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
61 return Void();
62 }
63
64 death_recipient_->setHasDied(false);
65 cb->linkToDeath(death_recipient_, 0);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070066
67 bool rc = VendorInterface::Initialize(
Myles Watson9cec0e32017-03-16 16:23:23 -070068 [cb](bool status) {
69 auto hidl_status = cb->initializationComplete(
Andre Eisenbach9041d972017-01-17 18:23:12 -080070 status ? Status::SUCCESS : Status::INITIALIZATION_ERROR);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070071 if (!hidl_status.isOk()) {
72 ALOGE("VendorInterface -> Unable to call initializationComplete()");
73 }
Andre Eisenbach9041d972017-01-17 18:23:12 -080074 },
Myles Watson9cec0e32017-03-16 16:23:23 -070075 [cb](const hidl_vec<uint8_t>& packet) {
76 auto hidl_status = cb->hciEventReceived(packet);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070077 if (!hidl_status.isOk()) {
78 ALOGE("VendorInterface -> Unable to call hciEventReceived()");
79 }
Zach Johnson917efb12017-02-26 23:46:05 -080080 },
Myles Watson9cec0e32017-03-16 16:23:23 -070081 [cb](const hidl_vec<uint8_t>& packet) {
82 auto hidl_status = cb->aclDataReceived(packet);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070083 if (!hidl_status.isOk()) {
84 ALOGE("VendorInterface -> Unable to call aclDataReceived()");
85 }
Zach Johnson917efb12017-02-26 23:46:05 -080086 },
Myles Watson9cec0e32017-03-16 16:23:23 -070087 [cb](const hidl_vec<uint8_t>& packet) {
88 auto hidl_status = cb->scoDataReceived(packet);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070089 if (!hidl_status.isOk()) {
90 ALOGE("VendorInterface -> Unable to call scoDataReceived()");
91 }
Jakub Pawlowski13b4d312019-11-05 12:27:29 +010092 },
93 [cb](const hidl_vec<uint8_t>&) {
94 ALOGE("VendorInterface -> No callback for ISO packets in HAL V1_0");
Andre Eisenbach89ba5282016-10-13 15:45:02 -070095 });
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070096 if (!rc) {
Myles Watson9cec0e32017-03-16 16:23:23 -070097 auto hidl_status = cb->initializationComplete(Status::INITIALIZATION_ERROR);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070098 if (!hidl_status.isOk()) {
99 ALOGE("VendorInterface -> Unable to call initializationComplete(ERR)");
100 }
101 }
Myles Watson9cec0e32017-03-16 16:23:23 -0700102
103 unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
104 if (death_recipient->getHasDied())
105 ALOGI("Skipping unlink call, service died.");
106 else
107 cb->unlinkToDeath(death_recipient);
108 };
109
Andre Eisenbach9041d972017-01-17 18:23:12 -0800110 return Void();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700111}
112
113Return<void> BluetoothHci::close() {
Myles Watson9cec0e32017-03-16 16:23:23 -0700114 ALOGI("BluetoothHci::close()");
115 unlink_cb_(death_recipient_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700116 VendorInterface::Shutdown();
117 return Void();
118}
119
120Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& command) {
121 sendDataToController(HCI_DATA_TYPE_COMMAND, command);
122 return Void();
123}
124
125Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& data) {
126 sendDataToController(HCI_DATA_TYPE_ACL, data);
127 return Void();
128}
129
130Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {
131 sendDataToController(HCI_DATA_TYPE_SCO, data);
132 return Void();
133}
134
135void BluetoothHci::sendDataToController(const uint8_t type,
136 const hidl_vec<uint8_t>& data) {
Myles Watsondf765ea2017-01-30 09:07:37 -0800137 VendorInterface::get()->Send(type, data.data(), data.size());
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700138}
139
140IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* /* name */) {
141 return new BluetoothHci();
142}
143
144} // namespace implementation
145} // namespace V1_0
146} // namespace bluetooth
147} // namespace hardware
148} // namespace android