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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* Copyright 2021 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.
*/
#include "btaa/hci_processor.h"
#include "os/log.h"
namespace bluetooth {
namespace activity_attribution {
void DeviceParser::match_handle_with_address(uint16_t connection_handle, hci::Address& address) {
if (connection_handle && !address.IsEmpty()) {
connection_lookup_table_[connection_handle] = address;
} else if (connection_handle) {
if (connection_lookup_table_.find(connection_handle) != connection_lookup_table_.end()) {
address = connection_lookup_table_[connection_handle];
}
}
}
void HciProcessor::process_le_event(
std::vector<BtaaHciPacket>& btaa_hci_packets, int16_t byte_count, hci::EventView& event) {
uint16_t connection_handle_value = 0;
hci::Address address_value;
auto le_packet_view = hci::LeMetaEventView::Create(event);
if (!le_packet_view.IsValid()) {
return;
}
auto subevent_code = le_packet_view.GetSubeventCode();
auto le_event_info = lookup_le_event(subevent_code);
if (le_event_info.activity != Activity::UNKNOWN) {
// lookup_le_event returns all simple classic event which does not require additional processing.
if (le_event_info.connection_handle_pos) {
auto connection_handle_it = event.begin() + le_event_info.connection_handle_pos;
connection_handle_value = connection_handle_it.extract<uint16_t>();
}
if (le_event_info.address_pos) {
auto address_value_it = event.begin() + le_event_info.address_pos;
address_value = address_value_it.extract<hci::Address>();
}
device_parser_.match_handle_with_address(connection_handle_value, address_value);
btaa_hci_packets.push_back(BtaaHciPacket(le_event_info.activity, address_value, byte_count));
}
}
void HciProcessor::process_special_event(
std::vector<BtaaHciPacket>& btaa_hci_packets,
hci::EventCode event_code,
uint16_t byte_count,
hci::EventView& event) {
uint16_t avg_byte_count;
hci::Address address_value;
switch (event_code) {
case hci::EventCode::INQUIRY_RESULT:
case hci::EventCode::INQUIRY_RESULT_WITH_RSSI: {
auto packet_view = hci::InquiryResultView::Create(event);
if (!packet_view.IsValid()) {
return;
}
auto inquiry_results = packet_view.GetResponses();
avg_byte_count = byte_count / inquiry_results.size();
for (auto& inquiry_result : inquiry_results) {
btaa_hci_packets.push_back(BtaaHciPacket(Activity::SCAN, inquiry_result.bd_addr_, avg_byte_count));
}
} break;
case hci::EventCode::NUMBER_OF_COMPLETED_PACKETS: {
auto packet_view = hci::NumberOfCompletedPacketsView::Create(event);
if (!packet_view.IsValid()) {
return;
}
auto completed_packets = packet_view.GetCompletedPackets();
avg_byte_count = byte_count / completed_packets.size();
for (auto& completed_packet : completed_packets) {
device_parser_.match_handle_with_address(completed_packet.connection_handle_, address_value);
btaa_hci_packets.push_back(BtaaHciPacket(Activity::CONNECT, address_value, avg_byte_count));
}
} break;
case hci::EventCode::RETURN_LINK_KEYS: {
auto packet_view = hci::ReturnLinkKeysView::Create(event);
if (!packet_view.IsValid()) {
return;
}
auto keys_and_addresses = packet_view.GetKeys();
avg_byte_count = byte_count / keys_and_addresses.size();
for (auto& key_and_address : keys_and_addresses) {
btaa_hci_packets.push_back(BtaaHciPacket(Activity::CONNECT, key_and_address.address_, avg_byte_count));
}
} break;
default: {
btaa_hci_packets.push_back(BtaaHciPacket(Activity::UNKNOWN, address_value, byte_count));
} break;
}
}
void HciProcessor::process_command(
std::vector<BtaaHciPacket>& btaa_hci_packets,
packet::PacketView<packet::kLittleEndian>& packet_view,
uint16_t byte_count) {
hci::CommandView command = hci::CommandView::Create(packet_view);
if (!command.IsValid()) {
return;
}
uint16_t connection_handle_value = 0;
hci::Address address_value;
auto opcode = command.GetOpCode();
auto cmd_info = lookup_cmd(opcode);
if (cmd_info.connection_handle_pos) {
auto connection_handle_it = command.begin() + cmd_info.connection_handle_pos;
connection_handle_value = connection_handle_it.extract<uint16_t>();
}
if (cmd_info.address_pos) {
auto address_value_it = command.begin() + cmd_info.address_pos;
address_value = address_value_it.extract<hci::Address>();
}
device_parser_.match_handle_with_address(connection_handle_value, address_value);
pending_command_.btaa_hci_packet = BtaaHciPacket(cmd_info.activity, address_value, byte_count);
pending_command_.opcode = opcode;
}
void HciProcessor::process_event(
std::vector<BtaaHciPacket>& btaa_hci_packets,
packet::PacketView<packet::kLittleEndian>& packet_view,
uint16_t byte_count) {
hci::EventView event = hci::EventView::Create(packet_view);
if (!event.IsValid()) {
return;
}
uint16_t connection_handle_value = 0;
hci::Address address_value;
auto event_code = event.GetEventCode();
auto event_info = lookup_event(event_code);
if (event_info.activity != Activity::UNKNOWN) {
// lookup_event returns all simple classic event which does not require additional processing.
if (event_info.connection_handle_pos) {
auto connection_handle_it = event.begin() + event_info.connection_handle_pos;
connection_handle_value = connection_handle_it.extract<uint16_t>();
}
if (event_info.address_pos) {
auto address_value_it = event.begin() + event_info.address_pos;
address_value = address_value_it.extract<hci::Address>();
}
device_parser_.match_handle_with_address(connection_handle_value, address_value);
btaa_hci_packets.push_back(BtaaHciPacket(event_info.activity, address_value, byte_count));
} else {
// The event requires additional processing.
switch (event_code) {
case hci::EventCode::COMMAND_COMPLETE: {
auto packet_view = hci::CommandCompleteView::Create(event);
if (packet_view.IsValid() && packet_view.GetCommandOpCode() == pending_command_.opcode) {
pending_command_.btaa_hci_packet.byte_count += byte_count;
btaa_hci_packets.push_back(std::move(pending_command_.btaa_hci_packet));
} else {
btaa_hci_packets.push_back(BtaaHciPacket(Activity::UNKNOWN, address_value, byte_count));
}
} break;
case hci::EventCode::COMMAND_STATUS: {
auto packet_view = hci::CommandStatusView::Create(event);
if (packet_view.IsValid() && packet_view.GetCommandOpCode() == pending_command_.opcode) {
pending_command_.btaa_hci_packet.byte_count += byte_count;
btaa_hci_packets.push_back(std::move(pending_command_.btaa_hci_packet));
} else {
btaa_hci_packets.push_back(BtaaHciPacket(Activity::UNKNOWN, address_value, byte_count));
}
break;
}
case hci::EventCode::LE_META_EVENT:
process_le_event(btaa_hci_packets, byte_count, event);
break;
case hci::EventCode::VENDOR_SPECIFIC:
btaa_hci_packets.push_back(BtaaHciPacket(Activity::VENDOR, address_value, byte_count));
break;
default:
process_special_event(btaa_hci_packets, event_code, byte_count, event);
break;
}
}
}
void HciProcessor::process_acl(
std::vector<BtaaHciPacket>& btaa_hci_packets,
packet::PacketView<packet::kLittleEndian>& packet_view,
uint16_t byte_count) {
hci::AclView acl = hci::AclView::Create(packet_view);
auto connection_handle = acl.begin();
// Connection handle is extracted from the 12 least significant bit.
uint16_t connection_handle_value = connection_handle.extract<uint16_t>() & 0xfff;
hci::Address address_value;
device_parser_.match_handle_with_address(connection_handle_value, address_value);
btaa_hci_packets.push_back(BtaaHciPacket(Activity::ACL, address_value, byte_count));
}
void HciProcessor::process_sco(
std::vector<BtaaHciPacket>& btaa_hci_packets,
packet::PacketView<packet::kLittleEndian>& packet_view,
uint16_t byte_count) {
hci::ScoView sco = hci::ScoView::Create(packet_view);
auto connection_handle = sco.begin();
// Connection handle is extracted from the 12 least significant bit.
uint16_t connection_handle_value = connection_handle.extract<uint16_t>() & 0xfff;
hci::Address address_value;
device_parser_.match_handle_with_address(connection_handle_value, address_value);
btaa_hci_packets.push_back(BtaaHciPacket(Activity::HFP, address_value, byte_count));
}
void HciProcessor::process_iso(
std::vector<BtaaHciPacket>& btaa_hci_packets,
packet::PacketView<packet::kLittleEndian>& packet_view,
uint16_t byte_count) {
hci::IsoView iso = hci::IsoView::Create(packet_view);
auto connection_handle = iso.begin();
// Connection handle is extracted from the 12 least significant bit.
uint16_t connection_handle_value = connection_handle.extract<uint16_t>() & 0xfff;
hci::Address address_value;
device_parser_.match_handle_with_address(connection_handle_value, address_value);
btaa_hci_packets.push_back(BtaaHciPacket(Activity::ISO, address_value, byte_count));
}
std::vector<BtaaHciPacket> HciProcessor::OnHciPacket(
hal::HciPacket packet, hal::SnoopLogger::PacketType type, uint16_t length) {
std::vector<BtaaHciPacket> btaa_hci_packets;
auto packet_view = packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(packet));
switch (type) {
case hal::SnoopLogger::PacketType::CMD:
process_command(btaa_hci_packets, packet_view, length);
break;
case hal::SnoopLogger::PacketType::EVT:
process_event(btaa_hci_packets, packet_view, length);
break;
case hal::SnoopLogger::PacketType::ACL:
process_acl(btaa_hci_packets, packet_view, length);
break;
case hal::SnoopLogger::PacketType::SCO:
process_sco(btaa_hci_packets, packet_view, length);
break;
case hal::SnoopLogger::PacketType::ISO:
process_iso(btaa_hci_packets, packet_view, length);
break;
}
return btaa_hci_packets;
}
} // namespace activity_attribution
} // namespace bluetooth
|