blob: fec5b8155c90cd79c07141fd97586e317947fe61 [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"
18#include <utils/Log.h>
19
20#include "bluetooth_hci.h"
21#include "vendor_interface.h"
22
23namespace android {
24namespace hardware {
25namespace bluetooth {
26namespace V1_0 {
27namespace implementation {
28
29static const uint8_t HCI_DATA_TYPE_COMMAND = 1;
30static const uint8_t HCI_DATA_TYPE_ACL = 2;
31static const uint8_t HCI_DATA_TYPE_SCO = 3;
32
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070033class 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 Coenen678af7f2017-02-23 17:25:18 +010046BluetoothHci::BluetoothHci()
47 : deathRecipient(new BluetoothDeathRecipient(this)) {}
48
Andre Eisenbach9041d972017-01-17 18:23:12 -080049Return<void> BluetoothHci::initialize(
Andre Eisenbach89ba5282016-10-13 15:45:02 -070050 const ::android::sp<IBluetoothHciCallbacks>& cb) {
51 ALOGW("BluetoothHci::initialize()");
Martijn Coenen678af7f2017-02-23 17:25:18 +010052 cb->linkToDeath(deathRecipient, 0);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070053 event_cb_ = cb;
54
55 bool rc = VendorInterface::Initialize(
Andre Eisenbach9041d972017-01-17 18:23:12 -080056 [this](bool status) {
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070057 auto hidl_status = event_cb_->initializationComplete(
Andre Eisenbach9041d972017-01-17 18:23:12 -080058 status ? Status::SUCCESS : Status::INITIALIZATION_ERROR);
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070059 if (!hidl_status.isOk()) {
60 ALOGE("VendorInterface -> Unable to call initializationComplete()");
61 }
Andre Eisenbach9041d972017-01-17 18:23:12 -080062 },
Zach Johnson917efb12017-02-26 23:46:05 -080063 [this](const hidl_vec<uint8_t>& packet) {
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070064 auto hidl_status = event_cb_->hciEventReceived(packet);
65 if (!hidl_status.isOk()) {
66 ALOGE("VendorInterface -> Unable to call hciEventReceived()");
67 }
Zach Johnson917efb12017-02-26 23:46:05 -080068 },
69 [this](const hidl_vec<uint8_t>& packet) {
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070070 auto hidl_status = event_cb_->aclDataReceived(packet);
71 if (!hidl_status.isOk()) {
72 ALOGE("VendorInterface -> Unable to call aclDataReceived()");
73 }
Zach Johnson917efb12017-02-26 23:46:05 -080074 },
75 [this](const hidl_vec<uint8_t>& packet) {
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070076 auto hidl_status = event_cb_->scoDataReceived(packet);
77 if (!hidl_status.isOk()) {
78 ALOGE("VendorInterface -> Unable to call scoDataReceived()");
79 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -070080 });
Andre Eisenbach9f8931c2017-03-16 22:19:19 -070081 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 Eisenbach9041d972017-01-17 18:23:12 -080088 return Void();
Andre Eisenbach89ba5282016-10-13 15:45:02 -070089}
90
91Return<void> BluetoothHci::close() {
92 ALOGW("BluetoothHci::close()");
Martijn Coenen678af7f2017-02-23 17:25:18 +010093 event_cb_->unlinkToDeath(deathRecipient);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070094 VendorInterface::Shutdown();
95 return Void();
96}
97
98Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& command) {
99 sendDataToController(HCI_DATA_TYPE_COMMAND, command);
100 return Void();
101}
102
103Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& data) {
104 sendDataToController(HCI_DATA_TYPE_ACL, data);
105 return Void();
106}
107
108Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {
109 sendDataToController(HCI_DATA_TYPE_SCO, data);
110 return Void();
111}
112
113void BluetoothHci::sendDataToController(const uint8_t type,
114 const hidl_vec<uint8_t>& data) {
Myles Watsondf765ea2017-01-30 09:07:37 -0800115 VendorInterface::get()->Send(type, data.data(), data.size());
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700116}
117
118IBluetoothHci* 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