Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 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.0-impl" |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 18 | #include "bluetooth_hci.h" |
| 19 | |
Steven Moreland | 85d95ed | 2017-04-11 12:22:27 -0700 | [diff] [blame^] | 20 | #include <log/log.h> |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 21 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 22 | #include "vendor_interface.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace bluetooth { |
| 27 | namespace V1_0 { |
| 28 | namespace implementation { |
| 29 | |
| 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; |
| 33 | |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 34 | class 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 Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 42 | has_died_ = true; |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 43 | mHci->close(); |
| 44 | } |
| 45 | sp<IBluetoothHci> mHci; |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 46 | bool getHasDied() const { return has_died_; } |
| 47 | void setHasDied(bool has_died) { has_died_ = has_died; } |
| 48 | |
| 49 | private: |
| 50 | bool has_died_; |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Martijn Coenen | 678af7f | 2017-02-23 17:25:18 +0100 | [diff] [blame] | 53 | BluetoothHci::BluetoothHci() |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 54 | : death_recipient_(new BluetoothDeathRecipient(this)) {} |
Martijn Coenen | 678af7f | 2017-02-23 17:25:18 +0100 | [diff] [blame] | 55 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 56 | Return<void> BluetoothHci::initialize( |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 57 | const ::android::sp<IBluetoothHciCallbacks>& cb) { |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 58 | 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 Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 66 | |
| 67 | bool rc = VendorInterface::Initialize( |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 68 | [cb](bool status) { |
| 69 | auto hidl_status = cb->initializationComplete( |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 70 | status ? Status::SUCCESS : Status::INITIALIZATION_ERROR); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 71 | if (!hidl_status.isOk()) { |
| 72 | ALOGE("VendorInterface -> Unable to call initializationComplete()"); |
| 73 | } |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 74 | }, |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 75 | [cb](const hidl_vec<uint8_t>& packet) { |
| 76 | auto hidl_status = cb->hciEventReceived(packet); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 77 | if (!hidl_status.isOk()) { |
| 78 | ALOGE("VendorInterface -> Unable to call hciEventReceived()"); |
| 79 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 80 | }, |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 81 | [cb](const hidl_vec<uint8_t>& packet) { |
| 82 | auto hidl_status = cb->aclDataReceived(packet); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 83 | if (!hidl_status.isOk()) { |
| 84 | ALOGE("VendorInterface -> Unable to call aclDataReceived()"); |
| 85 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 86 | }, |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 87 | [cb](const hidl_vec<uint8_t>& packet) { |
| 88 | auto hidl_status = cb->scoDataReceived(packet); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 89 | if (!hidl_status.isOk()) { |
| 90 | ALOGE("VendorInterface -> Unable to call scoDataReceived()"); |
| 91 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 92 | }); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 93 | if (!rc) { |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 94 | auto hidl_status = cb->initializationComplete(Status::INITIALIZATION_ERROR); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame] | 95 | if (!hidl_status.isOk()) { |
| 96 | ALOGE("VendorInterface -> Unable to call initializationComplete(ERR)"); |
| 97 | } |
| 98 | } |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 99 | |
| 100 | unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) { |
| 101 | if (death_recipient->getHasDied()) |
| 102 | ALOGI("Skipping unlink call, service died."); |
| 103 | else |
| 104 | cb->unlinkToDeath(death_recipient); |
| 105 | }; |
| 106 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 107 | return Void(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | Return<void> BluetoothHci::close() { |
Myles Watson | 9cec0e3 | 2017-03-16 16:23:23 -0700 | [diff] [blame] | 111 | ALOGI("BluetoothHci::close()"); |
| 112 | unlink_cb_(death_recipient_); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 113 | VendorInterface::Shutdown(); |
| 114 | return Void(); |
| 115 | } |
| 116 | |
| 117 | Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& command) { |
| 118 | sendDataToController(HCI_DATA_TYPE_COMMAND, command); |
| 119 | return Void(); |
| 120 | } |
| 121 | |
| 122 | Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& data) { |
| 123 | sendDataToController(HCI_DATA_TYPE_ACL, data); |
| 124 | return Void(); |
| 125 | } |
| 126 | |
| 127 | Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) { |
| 128 | sendDataToController(HCI_DATA_TYPE_SCO, data); |
| 129 | return Void(); |
| 130 | } |
| 131 | |
| 132 | void BluetoothHci::sendDataToController(const uint8_t type, |
| 133 | const hidl_vec<uint8_t>& data) { |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 134 | VendorInterface::get()->Send(type, data.data(), data.size()); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* /* name */) { |
| 138 | return new BluetoothHci(); |
| 139 | } |
| 140 | |
| 141 | } // namespace implementation |
| 142 | } // namespace V1_0 |
| 143 | } // namespace bluetooth |
| 144 | } // namespace hardware |
| 145 | } // namespace android |