1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
//
// Copyright 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#define LOG_TAG "android.hardware.bluetooth@1.0.sim"
#include "bluetooth_hci.h"
#include <base/logging.h>
#include <string.h>
#include <utils/Log.h>
#include "acl_packet.h"
#include "event_packet.h"
#include "hci_internals.h"
#include "sco_packet.h"
namespace android {
namespace hardware {
namespace bluetooth {
namespace V1_0 {
namespace sim {
using android::hardware::hidl_vec;
using test_vendor_lib::AclPacket;
using test_vendor_lib::AsyncManager;
using test_vendor_lib::AsyncTaskId;
using test_vendor_lib::CommandPacket;
using test_vendor_lib::DualModeController;
using test_vendor_lib::EventPacket;
using test_vendor_lib::ScoPacket;
using test_vendor_lib::TaskCallback;
using test_vendor_lib::TestChannelTransport;
class BluetoothDeathRecipient : public hidl_death_recipient {
public:
BluetoothDeathRecipient(const sp<IBluetoothHci> hci) : mHci(hci) {}
virtual void serviceDied(
uint64_t /* cookie */,
const wp<::android::hidl::base::V1_0::IBase>& /* who */) {
ALOGE("BluetoothDeathRecipient::serviceDied - Bluetooth service died");
has_died_ = true;
mHci->close();
}
sp<IBluetoothHci> mHci;
bool getHasDied() const { return has_died_; }
void setHasDied(bool has_died) { has_died_ = has_died; }
private:
bool has_died_;
};
BluetoothHci::BluetoothHci()
: death_recipient_(new BluetoothDeathRecipient(this)) {}
Return<void> BluetoothHci::initialize(const sp<IBluetoothHciCallbacks>& cb) {
ALOGI("%s", __func__);
if (cb == nullptr) {
ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
return Void();
}
death_recipient_->setHasDied(false);
cb->linkToDeath(death_recipient_, 0);
test_channel_transport_.RegisterCommandHandler(
[this](const std::string& name, const std::vector<std::string>& args) {
async_manager_.ExecAsync(
std::chrono::milliseconds(0), [this, name, args]() {
controller_.HandleTestChannelCommand(name, args);
});
});
controller_.RegisterEventChannel([cb](std::unique_ptr<EventPacket> event) {
size_t header_bytes = event->GetHeaderSize();
size_t payload_bytes = event->GetPayloadSize();
hidl_vec<uint8_t> hci_event;
hci_event.resize(header_bytes + payload_bytes);
memcpy(hci_event.data(), event->GetHeader().data(), header_bytes);
memcpy(hci_event.data() + header_bytes, event->GetPayload().data(),
payload_bytes);
cb->hciEventReceived(hci_event);
});
controller_.RegisterAclChannel([cb](std::unique_ptr<AclPacket> packet) {
std::vector<uint8_t> acl_vector = packet->GetPacket();
hidl_vec<uint8_t> acl_packet = acl_vector;
cb->aclDataReceived(acl_packet);
});
controller_.RegisterScoChannel([cb](std::unique_ptr<ScoPacket> packet) {
size_t header_bytes = packet->GetHeaderSize();
size_t payload_bytes = packet->GetPayloadSize();
hidl_vec<uint8_t> sco_packet;
sco_packet.resize(header_bytes + payload_bytes);
memcpy(sco_packet.data(), packet->GetHeader().data(), header_bytes);
memcpy(sco_packet.data() + header_bytes, packet->GetPayload().data(),
payload_bytes);
cb->scoDataReceived(sco_packet);
});
controller_.RegisterTaskScheduler(
[this](std::chrono::milliseconds delay, const TaskCallback& task) {
return async_manager_.ExecAsync(delay, task);
});
controller_.RegisterPeriodicTaskScheduler(
[this](std::chrono::milliseconds delay, std::chrono::milliseconds period,
const TaskCallback& task) {
return async_manager_.ExecAsyncPeriodically(delay, period, task);
});
controller_.RegisterTaskCancel(
[this](AsyncTaskId task) { async_manager_.CancelAsyncTask(task); });
SetUpTestChannel(6111);
unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
if (death_recipient->getHasDied())
ALOGI("Skipping unlink call, service died.");
else
cb->unlinkToDeath(death_recipient);
};
cb->initializationComplete(Status::SUCCESS);
return Void();
}
Return<void> BluetoothHci::close() {
ALOGI("%s", __func__);
return Void();
}
Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& packet) {
async_manager_.ExecAsync(std::chrono::milliseconds(0), [this, packet]() {
uint16_t opcode = packet[0] | (packet[1] << 8);
std::unique_ptr<CommandPacket> command =
std::unique_ptr<CommandPacket>(new CommandPacket(opcode));
for (size_t i = 3; i < packet.size(); i++)
command->AddPayloadOctets1(packet[i]);
controller_.HandleCommand(std::move(command));
});
return Void();
}
Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& packet) {
async_manager_.ExecAsync(std::chrono::milliseconds(0), [this, packet]() {
uint16_t channel = (packet[0] | (packet[1] << 8)) & 0xfff;
AclPacket::PacketBoundaryFlags boundary_flags =
static_cast<AclPacket::PacketBoundaryFlags>((packet[1] & 0x30) >> 4);
AclPacket::BroadcastFlags broadcast_flags =
static_cast<AclPacket::BroadcastFlags>((packet[1] & 0xC0) >> 6);
std::unique_ptr<AclPacket> acl = std::unique_ptr<AclPacket>(
new AclPacket(channel, boundary_flags, broadcast_flags));
for (size_t i = 4; i < packet.size(); i++)
acl->AddPayloadOctets1(packet[i]);
controller_.HandleAcl(std::move(acl));
});
return Void();
}
Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& packet) {
async_manager_.ExecAsync(std::chrono::milliseconds(0), [this, packet]() {
uint16_t channel = (packet[0] | (packet[1] << 8)) & 0xfff;
ScoPacket::PacketStatusFlags packet_status =
static_cast<ScoPacket::PacketStatusFlags>((packet[1] & 0x30) >> 4);
std::unique_ptr<ScoPacket> sco =
std::unique_ptr<ScoPacket>(new ScoPacket(channel, packet_status));
for (size_t i = 3; i < packet.size(); i++)
sco->AddPayloadOctets1(packet[i]);
controller_.HandleSco(std::move(sco));
});
return Void();
}
void BluetoothHci::SetUpTestChannel(int port) {
int socket_fd = test_channel_transport_.SetUp(port);
if (socket_fd == -1) {
ALOGE("Test channel SetUp(%d) failed.", port);
return;
}
ALOGI("Test channel SetUp() successful");
async_manager_.WatchFdForNonBlockingReads(socket_fd, [this](int socket_fd) {
int conn_fd = test_channel_transport_.Accept(socket_fd);
if (conn_fd < 0) {
ALOGE("Error watching test channel fd.");
return;
}
ALOGI("Test channel connection accepted.");
async_manager_.WatchFdForNonBlockingReads(conn_fd, [this](int conn_fd) {
test_channel_transport_.OnCommandReady(conn_fd, [this, conn_fd]() {
async_manager_.StopWatchingFileDescriptor(conn_fd);
});
});
});
}
/* Fallback to shared library if there is no service. */
IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* /* name */) {
return new BluetoothHci();
}
} // namespace gce
} // namespace V1_0
} // namespace bluetooth
} // namespace hardware
} // namespace android
|