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" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include "bluetooth_hci.h" |
| 21 | #include "vendor_interface.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace bluetooth { |
| 26 | namespace V1_0 { |
| 27 | namespace implementation { |
| 28 | |
| 29 | static const uint8_t HCI_DATA_TYPE_COMMAND = 1; |
| 30 | static const uint8_t HCI_DATA_TYPE_ACL = 2; |
| 31 | static const uint8_t HCI_DATA_TYPE_SCO = 3; |
| 32 | |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame^] | 33 | class BluetoothDeathRecipient : public hidl_death_recipient { |
| 34 | public: |
| 35 | BluetoothDeathRecipient(const sp<IBluetoothHci> hci) : mHci(hci) {} |
| 36 | |
| 37 | virtual void serviceDied( |
| 38 | uint64_t /*cookie*/, |
| 39 | const wp<::android::hidl::base::V1_0::IBase>& /*who*/) { |
| 40 | ALOGE("BluetoothDeathRecipient::serviceDied - Bluetooth service died"); |
| 41 | mHci->close(); |
| 42 | } |
| 43 | sp<IBluetoothHci> mHci; |
| 44 | }; |
| 45 | |
Martijn Coenen | 678af7f | 2017-02-23 17:25:18 +0100 | [diff] [blame] | 46 | BluetoothHci::BluetoothHci() |
| 47 | : deathRecipient(new BluetoothDeathRecipient(this)) {} |
| 48 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 49 | Return<void> BluetoothHci::initialize( |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 50 | const ::android::sp<IBluetoothHciCallbacks>& cb) { |
| 51 | ALOGW("BluetoothHci::initialize()"); |
Martijn Coenen | 678af7f | 2017-02-23 17:25:18 +0100 | [diff] [blame] | 52 | cb->linkToDeath(deathRecipient, 0); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 53 | event_cb_ = cb; |
| 54 | |
| 55 | bool rc = VendorInterface::Initialize( |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 56 | [this](bool status) { |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame^] | 57 | auto hidl_status = event_cb_->initializationComplete( |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 58 | status ? Status::SUCCESS : Status::INITIALIZATION_ERROR); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame^] | 59 | if (!hidl_status.isOk()) { |
| 60 | ALOGE("VendorInterface -> Unable to call initializationComplete()"); |
| 61 | } |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 62 | }, |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 63 | [this](const hidl_vec<uint8_t>& packet) { |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame^] | 64 | auto hidl_status = event_cb_->hciEventReceived(packet); |
| 65 | if (!hidl_status.isOk()) { |
| 66 | ALOGE("VendorInterface -> Unable to call hciEventReceived()"); |
| 67 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 68 | }, |
| 69 | [this](const hidl_vec<uint8_t>& packet) { |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame^] | 70 | auto hidl_status = event_cb_->aclDataReceived(packet); |
| 71 | if (!hidl_status.isOk()) { |
| 72 | ALOGE("VendorInterface -> Unable to call aclDataReceived()"); |
| 73 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 74 | }, |
| 75 | [this](const hidl_vec<uint8_t>& packet) { |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame^] | 76 | auto hidl_status = event_cb_->scoDataReceived(packet); |
| 77 | if (!hidl_status.isOk()) { |
| 78 | ALOGE("VendorInterface -> Unable to call scoDataReceived()"); |
| 79 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 80 | }); |
Andre Eisenbach | 9f8931c | 2017-03-16 22:19:19 -0700 | [diff] [blame^] | 81 | if (!rc) { |
| 82 | auto hidl_status = |
| 83 | event_cb_->initializationComplete(Status::INITIALIZATION_ERROR); |
| 84 | if (!hidl_status.isOk()) { |
| 85 | ALOGE("VendorInterface -> Unable to call initializationComplete(ERR)"); |
| 86 | } |
| 87 | } |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 88 | return Void(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | Return<void> BluetoothHci::close() { |
| 92 | ALOGW("BluetoothHci::close()"); |
Martijn Coenen | 678af7f | 2017-02-23 17:25:18 +0100 | [diff] [blame] | 93 | event_cb_->unlinkToDeath(deathRecipient); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 94 | VendorInterface::Shutdown(); |
| 95 | return Void(); |
| 96 | } |
| 97 | |
| 98 | Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& command) { |
| 99 | sendDataToController(HCI_DATA_TYPE_COMMAND, command); |
| 100 | return Void(); |
| 101 | } |
| 102 | |
| 103 | Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& data) { |
| 104 | sendDataToController(HCI_DATA_TYPE_ACL, data); |
| 105 | return Void(); |
| 106 | } |
| 107 | |
| 108 | Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) { |
| 109 | sendDataToController(HCI_DATA_TYPE_SCO, data); |
| 110 | return Void(); |
| 111 | } |
| 112 | |
| 113 | void BluetoothHci::sendDataToController(const uint8_t type, |
| 114 | const hidl_vec<uint8_t>& data) { |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 115 | VendorInterface::get()->Send(type, data.data(), data.size()); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* /* name */) { |
| 119 | return new BluetoothHci(); |
| 120 | } |
| 121 | |
| 122 | } // namespace implementation |
| 123 | } // namespace V1_0 |
| 124 | } // namespace bluetooth |
| 125 | } // namespace hardware |
| 126 | } // namespace android |