1 /* 2 * Copyright 2015 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 "event_packet" 18 19 #include "event_packet.h" 20 21 #include "osi/include/log.h" 22 #include "stack/include/hcidefs.h" 23 24 using std::vector; 25 26 namespace test_vendor_lib { 27 28 EventPacket::EventPacket(uint8_t event_code) 29 : Packet(DATA_TYPE_EVENT, {event_code}) {} 30 31 uint8_t EventPacket::GetEventCode() const { return GetHeader()[0]; } 32 33 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.1 34 std::unique_ptr<EventPacket> EventPacket::CreateInquiryCompleteEvent( 35 uint8_t status) { 36 std::unique_ptr<EventPacket> evt_ptr = 37 std::unique_ptr<EventPacket>(new EventPacket(HCI_INQUIRY_COMP_EVT)); 38 CHECK(evt_ptr->AddPayloadOctets1(status)); 39 40 return evt_ptr; 41 } 42 43 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.14 44 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteEvent( 45 uint16_t command_opcode, const vector<uint8_t>& event_return_parameters) { 46 std::unique_ptr<EventPacket> evt_ptr = 47 std::unique_ptr<EventPacket>(new EventPacket(HCI_COMMAND_COMPLETE_EVT)); 48 49 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets 50 CHECK(evt_ptr->AddPayloadOctets2(command_opcode)); 51 CHECK(evt_ptr->AddPayloadOctets(event_return_parameters.size(), 52 event_return_parameters)); 53 54 return evt_ptr; 55 } 56 57 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteOnlyStatusEvent( 58 uint16_t command_opcode, uint8_t status) { 59 std::unique_ptr<EventPacket> evt_ptr = 60 std::unique_ptr<EventPacket>(new EventPacket(HCI_COMMAND_COMPLETE_EVT)); 61 62 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets 63 CHECK(evt_ptr->AddPayloadOctets2(command_opcode)); 64 CHECK(evt_ptr->AddPayloadOctets1(status)); 65 66 return evt_ptr; 67 } 68 69 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.15 70 std::unique_ptr<EventPacket> EventPacket::CreateCommandStatusEvent( 71 uint8_t status, uint16_t command_opcode) { 72 std::unique_ptr<EventPacket> evt_ptr = 73 std::unique_ptr<EventPacket>(new EventPacket(HCI_COMMAND_STATUS_EVT)); 74 75 CHECK(evt_ptr->AddPayloadOctets1(status)); 76 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets 77 CHECK(evt_ptr->AddPayloadOctets2(command_opcode)); 78 79 return evt_ptr; 80 } 81 82 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.19 83 std::unique_ptr<EventPacket> EventPacket::CreateNumberOfCompletedPacketsEvent( 84 uint16_t handle, uint16_t num_completed_packets) { 85 std::unique_ptr<EventPacket> evt_ptr = std::unique_ptr<EventPacket>( 86 new EventPacket(HCI_NUM_COMPL_DATA_PKTS_EVT)); 87 88 CHECK(evt_ptr->AddPayloadOctets1(0)); // Number of handles. 89 evt_ptr->AddCompletedPackets(handle, num_completed_packets); 90 91 return evt_ptr; 92 } 93 94 void EventPacket::AddCompletedPackets(uint16_t handle, 95 uint16_t num_completed_packets) { 96 CHECK(AddPayloadOctets2(handle)); 97 CHECK(AddPayloadOctets2(num_completed_packets)); 98 CHECK(IncrementPayloadCounter(1)); // Increment the number of handles. 99 } 100 101 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.3.10 102 std::unique_ptr<EventPacket> 103 EventPacket::CreateCommandCompleteDeleteStoredLinkKey( 104 uint8_t status, uint16_t num_keys_deleted) { 105 std::unique_ptr<EventPacket> evt_ptr = 106 EventPacket::CreateCommandCompleteOnlyStatusEvent( 107 HCI_DELETE_STORED_LINK_KEY, status); 108 109 CHECK(evt_ptr->AddPayloadOctets2(num_keys_deleted)); 110 111 return evt_ptr; 112 } 113 114 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.3.12 115 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteReadLocalName( 116 uint8_t status, const std::string& local_name) { 117 std::unique_ptr<EventPacket> evt_ptr = 118 EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_READ_LOCAL_NAME, 119 status); 120 121 for (size_t i = 0; i < local_name.length(); i++) 122 CHECK(evt_ptr->AddPayloadOctets1(local_name[i])); 123 CHECK(evt_ptr->AddPayloadOctets1(0)); // Null terminated 124 for (size_t i = 0; i < 248 - local_name.length() - 1; i++) 125 CHECK(evt_ptr->AddPayloadOctets1(0xFF)); 126 127 return evt_ptr; 128 } 129 130 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.1 131 std::unique_ptr<EventPacket> 132 EventPacket::CreateCommandCompleteReadLocalVersionInformation( 133 uint8_t status, uint8_t hci_version, uint16_t hci_revision, 134 uint8_t lmp_pal_version, uint16_t manufacturer_name, 135 uint16_t lmp_pal_subversion) { 136 std::unique_ptr<EventPacket> evt_ptr = 137 EventPacket::CreateCommandCompleteOnlyStatusEvent( 138 HCI_READ_LOCAL_VERSION_INFO, status); 139 140 CHECK(evt_ptr->AddPayloadOctets1(hci_version)); 141 CHECK(evt_ptr->AddPayloadOctets2(hci_revision)); 142 CHECK(evt_ptr->AddPayloadOctets1(lmp_pal_version)); 143 CHECK(evt_ptr->AddPayloadOctets2(manufacturer_name)); 144 CHECK(evt_ptr->AddPayloadOctets2(lmp_pal_subversion)); 145 146 return evt_ptr; 147 } 148 149 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.2 150 std::unique_ptr<EventPacket> 151 EventPacket::CreateCommandCompleteReadLocalSupportedCommands( 152 uint8_t status, const vector<uint8_t>& supported_commands) { 153 std::unique_ptr<EventPacket> evt_ptr = 154 EventPacket::CreateCommandCompleteOnlyStatusEvent( 155 HCI_READ_LOCAL_SUPPORTED_CMDS, status); 156 157 CHECK(evt_ptr->AddPayloadOctets(64, supported_commands)); 158 159 return evt_ptr; 160 } 161 162 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.4 163 std::unique_ptr<EventPacket> 164 EventPacket::CreateCommandCompleteReadLocalExtendedFeatures( 165 uint8_t status, uint8_t page_number, uint8_t maximum_page_number, 166 uint64_t extended_lmp_features) { 167 std::unique_ptr<EventPacket> evt_ptr = 168 EventPacket::CreateCommandCompleteOnlyStatusEvent( 169 HCI_READ_LOCAL_EXT_FEATURES, status); 170 171 CHECK(evt_ptr->AddPayloadOctets1(page_number)); 172 CHECK(evt_ptr->AddPayloadOctets1(maximum_page_number)); 173 CHECK(evt_ptr->AddPayloadOctets8(extended_lmp_features)); 174 175 return evt_ptr; 176 } 177 178 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.5 179 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteReadBufferSize( 180 uint8_t status, uint16_t hc_acl_data_packet_length, 181 uint8_t hc_synchronous_data_packet_length, 182 uint16_t hc_total_num_acl_data_packets, 183 uint16_t hc_total_synchronous_data_packets) { 184 std::unique_ptr<EventPacket> evt_ptr = 185 EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_READ_BUFFER_SIZE, 186 status); 187 188 CHECK(evt_ptr->AddPayloadOctets2(hc_acl_data_packet_length)); 189 CHECK(evt_ptr->AddPayloadOctets1(hc_synchronous_data_packet_length)); 190 CHECK(evt_ptr->AddPayloadOctets2(hc_total_num_acl_data_packets)); 191 CHECK(evt_ptr->AddPayloadOctets2(hc_total_synchronous_data_packets)); 192 193 return evt_ptr; 194 } 195 196 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.6 197 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteReadBdAddr( 198 uint8_t status, const BtAddress& address) { 199 std::unique_ptr<EventPacket> evt_ptr = 200 EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_READ_BD_ADDR, 201 status); 202 203 CHECK(evt_ptr->AddPayloadBtAddress(address)); 204 205 return evt_ptr; 206 } 207 208 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.8 209 std::unique_ptr<EventPacket> 210 EventPacket::CreateCommandCompleteReadLocalSupportedCodecs( 211 uint8_t status, const vector<uint8_t>& supported_codecs, 212 const vector<uint32_t>& vendor_specific_codecs) { 213 std::unique_ptr<EventPacket> evt_ptr = 214 EventPacket::CreateCommandCompleteOnlyStatusEvent( 215 HCI_READ_LOCAL_SUPPORTED_CODECS, status); 216 217 CHECK(evt_ptr->AddPayloadOctets(supported_codecs.size(), supported_codecs)); 218 for (size_t i = 0; i < vendor_specific_codecs.size(); i++) 219 CHECK(evt_ptr->AddPayloadOctets4(vendor_specific_codecs[i])); 220 221 return evt_ptr; 222 } 223 224 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.6.1 225 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteReadLoopbackMode( 226 uint8_t status, uint8_t mode) { 227 std::unique_ptr<EventPacket> evt_ptr = 228 EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_READ_LOOPBACK_MODE, 229 status); 230 CHECK(evt_ptr->AddPayloadOctets1(mode)); 231 232 return evt_ptr; 233 } 234 235 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.2 236 std::unique_ptr<EventPacket> EventPacket::CreateInquiryResultEvent() { 237 std::unique_ptr<EventPacket> evt_ptr = 238 std::unique_ptr<EventPacket>(new EventPacket(HCI_INQUIRY_RESULT_EVT)); 239 240 CHECK(evt_ptr->AddPayloadOctets1(0)); // Start with no responses 241 return evt_ptr; 242 } 243 244 bool EventPacket::AddInquiryResult(const BtAddress& address, 245 uint8_t page_scan_repetition_mode, 246 uint32_t class_of_device, 247 uint16_t clock_offset) { 248 CHECK(GetEventCode() == HCI_INQUIRY_RESULT_EVT); 249 250 if (!CanAddPayloadOctets(14)) return false; 251 252 CHECK(IncrementPayloadCounter(1)); // Increment the number of responses 253 254 CHECK(AddPayloadBtAddress(address)); 255 CHECK(AddPayloadOctets1(page_scan_repetition_mode)); 256 CHECK(AddPayloadOctets2(kReservedZero)); 257 CHECK(AddPayloadOctets3(class_of_device)); 258 CHECK(!(clock_offset & 0x8000)); 259 CHECK(AddPayloadOctets2(clock_offset)); 260 return true; 261 } 262 263 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.3 264 std::unique_ptr<EventPacket> EventPacket::CreateConnectionCompleteEvent( 265 uint8_t status, uint16_t handle, const BtAddress& address, 266 uint8_t link_type, bool encryption_enabled) { 267 std::unique_ptr<EventPacket> evt_ptr = 268 std::unique_ptr<EventPacket>(new EventPacket(HCI_CONNECTION_COMP_EVT)); 269 270 CHECK(evt_ptr->AddPayloadOctets1(status)); 271 CHECK((handle & 0xf000) == 0); // Handles are 12-bit values. 272 CHECK(evt_ptr->AddPayloadOctets2(handle)); 273 CHECK(evt_ptr->AddPayloadBtAddress(address)); 274 CHECK(evt_ptr->AddPayloadOctets1(link_type)); 275 CHECK(evt_ptr->AddPayloadOctets1(encryption_enabled ? 1 : 0)); 276 277 return evt_ptr; 278 } 279 280 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.25 281 std::unique_ptr<EventPacket> EventPacket::CreateLoopbackCommandEvent( 282 uint16_t opcode, const vector<uint8_t>& payload) { 283 std::unique_ptr<EventPacket> evt_ptr = 284 std::unique_ptr<EventPacket>(new EventPacket(HCI_LOOPBACK_COMMAND_EVT)); 285 CHECK(evt_ptr->AddPayloadOctets2(opcode)); 286 for (const auto& payload_byte : payload) // Fill the packet. 287 evt_ptr->AddPayloadOctets1(payload_byte); 288 return evt_ptr; 289 } 290 291 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.38 292 std::unique_ptr<EventPacket> EventPacket::CreateExtendedInquiryResultEvent( 293 const BtAddress& address, uint8_t page_scan_repetition_mode, 294 uint32_t class_of_device, uint16_t clock_offset, uint8_t rssi, 295 const vector<uint8_t>& extended_inquiry_response) { 296 std::unique_ptr<EventPacket> evt_ptr = std::unique_ptr<EventPacket>( 297 new EventPacket(HCI_EXTENDED_INQUIRY_RESULT_EVT)); 298 299 CHECK(evt_ptr->AddPayloadOctets1(1)); // Always contains a single response 300 301 CHECK(evt_ptr->AddPayloadBtAddress(address)); 302 CHECK(evt_ptr->AddPayloadOctets1(page_scan_repetition_mode)); 303 CHECK(evt_ptr->AddPayloadOctets1(kReservedZero)); 304 CHECK(evt_ptr->AddPayloadOctets3(class_of_device)); 305 CHECK(!(clock_offset & 0x8000)); 306 CHECK(evt_ptr->AddPayloadOctets2(clock_offset)); 307 CHECK(evt_ptr->AddPayloadOctets1(rssi)); 308 CHECK(evt_ptr->AddPayloadOctets(extended_inquiry_response.size(), 309 extended_inquiry_response)); 310 while (evt_ptr->AddPayloadOctets1(0xff)) 311 ; // Fill packet 312 return evt_ptr; 313 } 314 315 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.65.1 316 std::unique_ptr<EventPacket> EventPacket::CreateLeConnectionCompleteEvent( 317 uint8_t status, uint16_t handle, uint8_t role, uint8_t peer_address_type, 318 const BtAddress& peer, uint16_t interval, uint16_t latency, 319 uint16_t supervision_timeout) { 320 std::unique_ptr<EventPacket> evt_ptr = 321 std::unique_ptr<EventPacket>(new EventPacket(HCI_BLE_EVENT)); 322 323 CHECK(evt_ptr->AddPayloadOctets1(HCI_BLE_CONN_COMPLETE_EVT)); 324 CHECK(evt_ptr->AddPayloadOctets1(status)); 325 CHECK(evt_ptr->AddPayloadOctets2(handle)); 326 CHECK(evt_ptr->AddPayloadOctets1(role)); 327 CHECK(evt_ptr->AddPayloadOctets1(peer_address_type)); 328 CHECK(evt_ptr->AddPayloadBtAddress(peer)); 329 CHECK(evt_ptr->AddPayloadOctets2(interval)); 330 CHECK(evt_ptr->AddPayloadOctets2(latency)); 331 CHECK(evt_ptr->AddPayloadOctets2(supervision_timeout)); 332 CHECK(evt_ptr->AddPayloadOctets1( 333 0x00)); // Master Clock Accuracy (unused for master) 334 335 return evt_ptr; 336 } 337 338 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.65.2 339 std::unique_ptr<EventPacket> EventPacket::CreateLeAdvertisingReportEvent() { 340 std::unique_ptr<EventPacket> evt_ptr = 341 std::unique_ptr<EventPacket>(new EventPacket(HCI_BLE_EVENT)); 342 343 CHECK(evt_ptr->AddPayloadOctets1(HCI_BLE_ADV_PKT_RPT_EVT)); 344 345 CHECK(evt_ptr->AddPayloadOctets1(0)); // Start with an empty report 346 347 return evt_ptr; 348 } 349 350 bool EventPacket::AddLeAdvertisingReport(uint8_t event_type, uint8_t addr_type, 351 const BtAddress& addr, 352 const vector<uint8_t>& data, 353 uint8_t rssi) { 354 if (!CanAddPayloadOctets(10 + data.size())) return false; 355 356 CHECK(GetEventCode() == HCI_BLE_EVENT); 357 358 CHECK(IncrementPayloadCounter(2)); // Increment the number of responses 359 360 CHECK(AddPayloadOctets1(event_type)); 361 CHECK(AddPayloadOctets1(addr_type)); 362 CHECK(AddPayloadBtAddress(addr)); 363 CHECK(AddPayloadOctets1(data.size())); 364 CHECK(AddPayloadOctets(data.size(), data)); 365 CHECK(AddPayloadOctets1(rssi)); 366 return true; 367 } 368 369 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.65.4 370 std::unique_ptr<EventPacket> EventPacket::CreateLeRemoteUsedFeaturesEvent( 371 uint8_t status, uint16_t handle, uint64_t features) { 372 std::unique_ptr<EventPacket> evt_ptr = 373 std::unique_ptr<EventPacket>(new EventPacket(HCI_BLE_EVENT)); 374 375 CHECK(evt_ptr->AddPayloadOctets1(HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT)); 376 377 CHECK(evt_ptr->AddPayloadOctets1(status)); 378 CHECK(evt_ptr->AddPayloadOctets2(handle)); 379 CHECK(evt_ptr->AddPayloadOctets8(features)); 380 381 return evt_ptr; 382 } 383 384 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.2 385 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteLeReadBufferSize( 386 uint8_t status, uint16_t hc_le_data_packet_length, 387 uint8_t hc_total_num_le_data_packets) { 388 std::unique_ptr<EventPacket> evt_ptr = 389 EventPacket::CreateCommandCompleteOnlyStatusEvent( 390 HCI_BLE_READ_BUFFER_SIZE, status); 391 392 CHECK(evt_ptr->AddPayloadOctets2(hc_le_data_packet_length)); 393 CHECK(evt_ptr->AddPayloadOctets1(hc_total_num_le_data_packets)); 394 395 return evt_ptr; 396 } 397 398 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.3 399 std::unique_ptr<EventPacket> 400 EventPacket::CreateCommandCompleteLeReadLocalSupportedFeatures( 401 uint8_t status, uint64_t le_features) { 402 std::unique_ptr<EventPacket> evt_ptr = 403 EventPacket::CreateCommandCompleteOnlyStatusEvent( 404 HCI_BLE_READ_LOCAL_SPT_FEAT, status); 405 406 CHECK(evt_ptr->AddPayloadOctets8(le_features)); 407 408 return evt_ptr; 409 } 410 411 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.14 412 std::unique_ptr<EventPacket> 413 EventPacket::CreateCommandCompleteLeReadWhiteListSize(uint8_t status, 414 uint8_t white_list_size) { 415 std::unique_ptr<EventPacket> evt_ptr = 416 EventPacket::CreateCommandCompleteOnlyStatusEvent( 417 HCI_BLE_READ_WHITE_LIST_SIZE, status); 418 419 CHECK(evt_ptr->AddPayloadOctets8(white_list_size)); 420 421 return evt_ptr; 422 } 423 424 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.23 425 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteLeRand( 426 uint8_t status, uint64_t random_val) { 427 std::unique_ptr<EventPacket> evt_ptr = 428 EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_BLE_RAND, status); 429 430 CHECK(evt_ptr->AddPayloadOctets8(random_val)); 431 432 return evt_ptr; 433 } 434 435 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.27 436 std::unique_ptr<EventPacket> 437 EventPacket::CreateCommandCompleteLeReadSupportedStates(uint8_t status, 438 uint64_t le_states) { 439 std::unique_ptr<EventPacket> evt_ptr = 440 EventPacket::CreateCommandCompleteOnlyStatusEvent( 441 HCI_BLE_READ_SUPPORTED_STATES, status); 442 443 CHECK(evt_ptr->AddPayloadOctets8(le_states)); 444 445 return evt_ptr; 446 } 447 448 // Vendor-specific commands (see hcidefs.h) 449 450 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteLeVendorCap( 451 uint8_t status, const vector<uint8_t>& vendor_cap) { 452 std::unique_ptr<EventPacket> evt_ptr = 453 EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_BLE_VENDOR_CAP_OCF, 454 status); 455 456 CHECK(evt_ptr->AddPayloadOctets(vendor_cap.size(), vendor_cap)); 457 458 return evt_ptr; 459 } 460 461 } // namespace test_vendor_lib 462