blob: 1d6e600ba0a5948af785b3e463aa7617217c4950 [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
Martijn Coenen678af7f2017-02-23 17:25:18 +010033BluetoothHci::BluetoothHci()
34 : deathRecipient(new BluetoothDeathRecipient(this)) {}
35
Andre Eisenbach9041d972017-01-17 18:23:12 -080036Return<void> BluetoothHci::initialize(
Andre Eisenbach89ba5282016-10-13 15:45:02 -070037 const ::android::sp<IBluetoothHciCallbacks>& cb) {
38 ALOGW("BluetoothHci::initialize()");
Martijn Coenen678af7f2017-02-23 17:25:18 +010039 cb->linkToDeath(deathRecipient, 0);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070040 event_cb_ = cb;
41
42 bool rc = VendorInterface::Initialize(
Andre Eisenbach9041d972017-01-17 18:23:12 -080043 [this](bool status) {
44 event_cb_->initializationComplete(
45 status ? Status::SUCCESS : Status::INITIALIZATION_ERROR);
46 },
Andre Eisenbach89ba5282016-10-13 15:45:02 -070047 [this](HciPacketType type, const hidl_vec<uint8_t>& packet) {
48 switch (type) {
49 case HCI_PACKET_TYPE_EVENT:
50 event_cb_->hciEventReceived(packet);
51 break;
52 case HCI_PACKET_TYPE_ACL_DATA:
53 event_cb_->aclDataReceived(packet);
54 break;
55 case HCI_PACKET_TYPE_SCO_DATA:
56 event_cb_->scoDataReceived(packet);
57 break;
58 default:
59 ALOGE("%s Unexpected event type %d", __func__, type);
60 break;
61 }
62 });
Andre Eisenbach9041d972017-01-17 18:23:12 -080063 if (!rc) event_cb_->initializationComplete(Status::INITIALIZATION_ERROR);
64 return Void();
Andre Eisenbach89ba5282016-10-13 15:45:02 -070065}
66
67Return<void> BluetoothHci::close() {
68 ALOGW("BluetoothHci::close()");
Martijn Coenen678af7f2017-02-23 17:25:18 +010069 event_cb_->unlinkToDeath(deathRecipient);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070070 VendorInterface::Shutdown();
71 return Void();
72}
73
74Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& command) {
75 sendDataToController(HCI_DATA_TYPE_COMMAND, command);
76 return Void();
77}
78
79Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& data) {
80 sendDataToController(HCI_DATA_TYPE_ACL, data);
81 return Void();
82}
83
84Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {
85 sendDataToController(HCI_DATA_TYPE_SCO, data);
86 return Void();
87}
88
89void BluetoothHci::sendDataToController(const uint8_t type,
90 const hidl_vec<uint8_t>& data) {
Myles Watsondf765ea2017-01-30 09:07:37 -080091 VendorInterface::get()->Send(type, data.data(), data.size());
Andre Eisenbach89ba5282016-10-13 15:45:02 -070092}
93
94IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* /* name */) {
95 return new BluetoothHci();
96}
97
98} // namespace implementation
99} // namespace V1_0
100} // namespace bluetooth
101} // namespace hardware
102} // namespace android