Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 17 | #include "link_layer_controller.h" |
| 18 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 19 | #include "hci.h" |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 20 | #include "include/le_advertisement.h" |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 21 | #include "os/log.h" |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 22 | #include "packet/raw_builder.h" |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 23 | |
| 24 | using std::vector; |
| 25 | using namespace std::chrono; |
| 26 | using namespace test_vendor_lib::packets; |
| 27 | |
| 28 | namespace test_vendor_lib { |
| 29 | |
Chienyuan | c27da09 | 2019-11-20 19:29:43 +0800 | [diff] [blame] | 30 | constexpr uint16_t kNumCommandPackets = 0x01; |
| 31 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 32 | // TODO: Model Rssi? |
| 33 | static uint8_t GetRssi() { |
| 34 | static uint8_t rssi = 0; |
| 35 | rssi += 5; |
| 36 | if (rssi > 128) { |
| 37 | rssi = rssi % 7; |
| 38 | } |
| 39 | return -(rssi); |
| 40 | } |
| 41 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 42 | void LinkLayerController::SendLeLinkLayerPacket( |
| 43 | std::unique_ptr<model::packets::LinkLayerPacketBuilder> packet) { |
| 44 | std::shared_ptr<model::packets::LinkLayerPacketBuilder> shared_packet = |
| 45 | std::move(packet); |
| 46 | ScheduleTask(milliseconds(50), [this, shared_packet]() { |
| 47 | send_to_remote_(std::move(shared_packet), Phy::Type::LOW_ENERGY); |
| 48 | }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 51 | void LinkLayerController::SendLinkLayerPacket( |
| 52 | std::unique_ptr<model::packets::LinkLayerPacketBuilder> packet) { |
| 53 | std::shared_ptr<model::packets::LinkLayerPacketBuilder> shared_packet = |
| 54 | std::move(packet); |
| 55 | ScheduleTask(milliseconds(50), [this, shared_packet]() { |
| 56 | send_to_remote_(std::move(shared_packet), Phy::Type::BR_EDR); |
| 57 | }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 60 | bluetooth::hci::ErrorCode LinkLayerController::SendCommandToRemoteByAddress( |
Chienyuan | 676cefe | 2019-11-22 16:46:24 +0800 | [diff] [blame^] | 61 | bluetooth::hci::OpCode opcode, bluetooth::packet::PacketView<true> args, |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 62 | const Address& remote) { |
| 63 | Address local_address = properties_.GetAddress(); |
| 64 | |
| 65 | switch (opcode) { |
| 66 | case (bluetooth::hci::OpCode::REMOTE_NAME_REQUEST): |
| 67 | // LMP features get requested with remote name requests. |
| 68 | SendLinkLayerPacket(model::packets::ReadRemoteLmpFeaturesBuilder::Create( |
| 69 | local_address, remote)); |
| 70 | SendLinkLayerPacket(model::packets::RemoteNameRequestBuilder::Create( |
| 71 | local_address, remote)); |
| 72 | break; |
| 73 | case (bluetooth::hci::OpCode::READ_REMOTE_SUPPORTED_FEATURES): |
| 74 | SendLinkLayerPacket( |
| 75 | model::packets::ReadRemoteSupportedFeaturesBuilder::Create( |
| 76 | local_address, remote)); |
| 77 | break; |
| 78 | case (bluetooth::hci::OpCode::READ_REMOTE_EXTENDED_FEATURES): { |
| 79 | uint8_t page_number = |
| 80 | (args.begin() + 2).extract<uint8_t>(); // skip the handle |
| 81 | SendLinkLayerPacket( |
| 82 | model::packets::ReadRemoteExtendedFeaturesBuilder::Create( |
| 83 | local_address, remote, page_number)); |
| 84 | } break; |
| 85 | case (bluetooth::hci::OpCode::READ_REMOTE_VERSION_INFORMATION): |
| 86 | SendLinkLayerPacket( |
| 87 | model::packets::ReadRemoteVersionInformationBuilder::Create( |
| 88 | local_address, remote)); |
| 89 | break; |
| 90 | case (bluetooth::hci::OpCode::READ_CLOCK_OFFSET): |
| 91 | SendLinkLayerPacket(model::packets::ReadClockOffsetBuilder::Create( |
| 92 | local_address, remote)); |
| 93 | break; |
| 94 | default: |
| 95 | LOG_INFO("Dropping unhandled command 0x%04x", |
| 96 | static_cast<uint16_t>(opcode)); |
| 97 | return bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 98 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 99 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 100 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 103 | bluetooth::hci::ErrorCode LinkLayerController::SendCommandToRemoteByHandle( |
Chienyuan | 676cefe | 2019-11-22 16:46:24 +0800 | [diff] [blame^] | 104 | bluetooth::hci::OpCode opcode, bluetooth::packet::PacketView<true> args, |
| 105 | uint16_t handle) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 106 | // TODO: Handle LE connections |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 107 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 108 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 109 | } |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 110 | return SendCommandToRemoteByAddress(opcode, args, |
| 111 | connections_.GetAddress(handle)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Chienyuan | 85db6ee | 2019-11-21 22:07:09 +0800 | [diff] [blame] | 114 | hci::Status LinkLayerController::SendAclToRemote( |
| 115 | bluetooth::hci::AclPacketView acl_packet) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 116 | uint16_t handle = acl_packet.GetHandle(); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 117 | if (!connections_.HasHandle(handle)) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 118 | return hci::Status::UNKNOWN_CONNECTION; |
| 119 | } |
| 120 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 121 | Address my_address = properties_.GetAddress(); |
| 122 | Address destination = connections_.GetAddress(handle); |
| 123 | if (connections_.GetOwnAddressType(handle) != 0) { // If it's not public, it must be LE |
| 124 | my_address = properties_.GetLeAddress(); |
| 125 | } |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 126 | |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 127 | LOG_INFO("%s(%s): handle 0x%x size %d", __func__, properties_.GetAddress().ToString().c_str(), handle, |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 128 | static_cast<int>(acl_packet.size())); |
| 129 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 130 | ScheduleTask(milliseconds(5), [this, handle]() { |
Chienyuan | c27da09 | 2019-11-20 19:29:43 +0800 | [diff] [blame] | 131 | std::vector<bluetooth::hci::CompletedPackets> completed_packets; |
| 132 | bluetooth::hci::CompletedPackets cp; |
| 133 | cp.connection_handle_ = handle; |
| 134 | cp.host_num_of_completed_packets_ = kNumCommandPackets; |
| 135 | completed_packets.push_back(cp); |
| 136 | auto packet = bluetooth::hci::NumberOfCompletedPacketsBuilder::Create( |
| 137 | completed_packets); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 138 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 139 | }); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 140 | |
| 141 | auto acl_payload = acl_packet.GetPayload(); |
| 142 | |
| 143 | std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr = |
| 144 | std::make_unique<bluetooth::packet::RawBuilder>(); |
| 145 | std::vector<uint8_t> payload_bytes(acl_payload.begin(), acl_payload.end()); |
| 146 | |
| 147 | uint16_t first_two_bytes = |
| 148 | static_cast<uint16_t>(acl_packet.GetHandle()) + |
Chienyuan | 85db6ee | 2019-11-21 22:07:09 +0800 | [diff] [blame] | 149 | (static_cast<uint16_t>(acl_packet.GetPacketBoundaryFlag()) << 12) + |
| 150 | (static_cast<uint16_t>(acl_packet.GetBroadcastFlag()) << 14); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 151 | raw_builder_ptr->AddOctets2(first_two_bytes); |
| 152 | raw_builder_ptr->AddOctets2(static_cast<uint16_t>(payload_bytes.size())); |
| 153 | raw_builder_ptr->AddOctets(payload_bytes); |
| 154 | |
| 155 | auto acl = model::packets::AclPacketBuilder::Create( |
| 156 | my_address, destination, std::move(raw_builder_ptr)); |
| 157 | |
| 158 | SendLinkLayerPacket(std::move(acl)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 159 | return hci::Status::SUCCESS; |
| 160 | } |
| 161 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 162 | void LinkLayerController::IncomingPacket( |
| 163 | model::packets::LinkLayerPacketView incoming) { |
| 164 | ASSERT(incoming.IsValid()); |
| 165 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 166 | // TODO: Resolvable private addresses? |
| 167 | if (incoming.GetDestinationAddress() != properties_.GetAddress() && |
| 168 | incoming.GetDestinationAddress() != properties_.GetLeAddress() && |
| 169 | incoming.GetDestinationAddress() != Address::kEmpty) { |
| 170 | // Drop packets not addressed to me |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | switch (incoming.GetType()) { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 175 | case model::packets::PacketType::ACL: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 176 | IncomingAclPacket(incoming); |
| 177 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 178 | case model::packets::PacketType::DISCONNECT: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 179 | IncomingDisconnectPacket(incoming); |
| 180 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 181 | case model::packets::PacketType::ENCRYPT_CONNECTION: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 182 | IncomingEncryptConnection(incoming); |
| 183 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 184 | case model::packets::PacketType::ENCRYPT_CONNECTION_RESPONSE: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 185 | IncomingEncryptConnectionResponse(incoming); |
| 186 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 187 | case model::packets::PacketType::INQUIRY: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 188 | if (inquiry_scans_enabled_) { |
| 189 | IncomingInquiryPacket(incoming); |
| 190 | } |
| 191 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 192 | case model::packets::PacketType::INQUIRY_RESPONSE: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 193 | IncomingInquiryResponsePacket(incoming); |
| 194 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 195 | case model::packets::PacketType::IO_CAPABILITY_REQUEST: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 196 | IncomingIoCapabilityRequestPacket(incoming); |
| 197 | break; |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 198 | case model::packets::PacketType::IO_CAPABILITY_RESPONSE: |
| 199 | IncomingIoCapabilityResponsePacket(incoming); |
| 200 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 201 | case model::packets::PacketType::IO_CAPABILITY_NEGATIVE_RESPONSE: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 202 | IncomingIoCapabilityNegativeResponsePacket(incoming); |
| 203 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 204 | case model::packets::PacketType::LE_ADVERTISEMENT: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 205 | if (le_scan_enable_ || le_connect_) { |
| 206 | IncomingLeAdvertisementPacket(incoming); |
| 207 | } |
| 208 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 209 | case model::packets::PacketType::LE_CONNECT: |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 210 | IncomingLeConnectPacket(incoming); |
| 211 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 212 | case model::packets::PacketType::LE_CONNECT_COMPLETE: |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 213 | IncomingLeConnectCompletePacket(incoming); |
| 214 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 215 | case model::packets::PacketType::LE_SCAN: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 216 | // TODO: Check Advertising flags and see if we are scannable. |
| 217 | IncomingLeScanPacket(incoming); |
| 218 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 219 | case model::packets::PacketType::LE_SCAN_RESPONSE: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 220 | if (le_scan_enable_ && le_scan_type_ == 1) { |
| 221 | IncomingLeScanResponsePacket(incoming); |
| 222 | } |
| 223 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 224 | case model::packets::PacketType::PAGE: |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 225 | if (page_scans_enabled_) { |
| 226 | IncomingPagePacket(incoming); |
| 227 | } |
| 228 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 229 | case model::packets::PacketType::PAGE_RESPONSE: |
Chienyuan Huang | 85471e4 | 2019-11-06 12:52:58 +0000 | [diff] [blame] | 230 | IncomingPageResponsePacket(incoming); |
| 231 | break; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 232 | case model::packets::PacketType::PAGE_REJECT: |
| 233 | IncomingPageRejectPacket(incoming); |
| 234 | break; |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 235 | case (model::packets::PacketType::REMOTE_NAME_REQUEST): |
| 236 | IncomingRemoteNameRequest(incoming); |
| 237 | break; |
| 238 | case (model::packets::PacketType::REMOTE_NAME_REQUEST_RESPONSE): |
| 239 | IncomingRemoteNameRequestResponse(incoming); |
| 240 | break; |
| 241 | case (model::packets::PacketType::READ_REMOTE_SUPPORTED_FEATURES): |
| 242 | IncomingReadRemoteSupportedFeatures(incoming); |
| 243 | break; |
| 244 | case (model::packets::PacketType::READ_REMOTE_SUPPORTED_FEATURES_RESPONSE): |
| 245 | IncomingReadRemoteSupportedFeaturesResponse(incoming); |
| 246 | break; |
| 247 | case (model::packets::PacketType::READ_REMOTE_LMP_FEATURES): |
| 248 | IncomingReadRemoteLmpFeatures(incoming); |
| 249 | break; |
| 250 | case (model::packets::PacketType::READ_REMOTE_LMP_FEATURES_RESPONSE): |
| 251 | IncomingReadRemoteLmpFeaturesResponse(incoming); |
| 252 | break; |
| 253 | case (model::packets::PacketType::READ_REMOTE_EXTENDED_FEATURES): |
| 254 | IncomingReadRemoteExtendedFeatures(incoming); |
| 255 | break; |
| 256 | case (model::packets::PacketType::READ_REMOTE_EXTENDED_FEATURES_RESPONSE): |
| 257 | IncomingReadRemoteExtendedFeaturesResponse(incoming); |
| 258 | break; |
| 259 | case (model::packets::PacketType::READ_REMOTE_VERSION_INFORMATION): |
| 260 | IncomingReadRemoteVersion(incoming); |
| 261 | break; |
| 262 | case (model::packets::PacketType::READ_REMOTE_VERSION_INFORMATION_RESPONSE): |
| 263 | IncomingReadRemoteVersionResponse(incoming); |
| 264 | break; |
| 265 | case (model::packets::PacketType::READ_CLOCK_OFFSET): |
| 266 | IncomingReadClockOffset(incoming); |
| 267 | break; |
| 268 | case (model::packets::PacketType::READ_CLOCK_OFFSET_RESPONSE): |
| 269 | IncomingReadClockOffsetResponse(incoming); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 270 | break; |
| 271 | default: |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 272 | LOG_WARN("Dropping unhandled packet of type %s", |
| 273 | model::packets::PacketTypeText(incoming.GetType()).c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 277 | void LinkLayerController::IncomingAclPacket( |
| 278 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 279 | LOG_INFO("Acl Packet %s -> %s", incoming.GetSourceAddress().ToString().c_str(), |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 280 | incoming.GetDestinationAddress().ToString().c_str()); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 281 | |
| 282 | auto acl = model::packets::AclPacketView::Create(incoming); |
| 283 | ASSERT(acl.IsValid()); |
| 284 | auto payload = acl.GetPayload(); |
| 285 | std::shared_ptr<std::vector<uint8_t>> payload_bytes = |
| 286 | std::make_shared<std::vector<uint8_t>>(payload.begin(), payload.end()); |
| 287 | |
Chienyuan | 85db6ee | 2019-11-21 22:07:09 +0800 | [diff] [blame] | 288 | bluetooth::hci::PacketView<bluetooth::hci::kLittleEndian> raw_packet( |
| 289 | payload_bytes); |
| 290 | auto acl_view = bluetooth::hci::AclPacketView::Create(raw_packet); |
| 291 | ASSERT(acl_view.IsValid()); |
| 292 | |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 293 | LOG_INFO("%s: remote handle 0x%x size %d", __func__, acl_view.GetHandle(), static_cast<int>(acl_view.size())); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 294 | uint16_t local_handle = connections_.GetHandle(incoming.GetSourceAddress()); |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 295 | LOG_INFO("%s: local handle 0x%x", __func__, local_handle); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 296 | |
Chienyuan | 85db6ee | 2019-11-21 22:07:09 +0800 | [diff] [blame] | 297 | std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr = |
| 298 | std::make_unique<bluetooth::packet::RawBuilder>(); |
| 299 | std::vector<uint8_t> payload_data(acl_view.GetPayload().begin(), |
| 300 | acl_view.GetPayload().end()); |
| 301 | raw_builder_ptr->AddOctets(payload_data); |
| 302 | |
| 303 | auto acl_packet = bluetooth::hci::AclPacketBuilder::Create( |
| 304 | local_handle, acl_view.GetPacketBoundaryFlag(), |
| 305 | acl_view.GetBroadcastFlag(), std::move(raw_builder_ptr)); |
| 306 | |
| 307 | send_acl_(std::move(acl_packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 310 | void LinkLayerController::IncomingRemoteNameRequest( |
| 311 | model::packets::LinkLayerPacketView packet) { |
| 312 | auto view = model::packets::RemoteNameRequestView::Create(packet); |
| 313 | ASSERT(view.IsValid()); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 314 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 315 | SendLinkLayerPacket(model::packets::RemoteNameRequestResponseBuilder::Create( |
| 316 | packet.GetDestinationAddress(), packet.GetSourceAddress(), |
| 317 | properties_.GetName())); |
| 318 | } |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 319 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 320 | void LinkLayerController::IncomingRemoteNameRequestResponse( |
| 321 | model::packets::LinkLayerPacketView packet) { |
| 322 | auto view = model::packets::RemoteNameRequestResponseView::Create(packet); |
| 323 | ASSERT(view.IsValid()); |
| 324 | |
| 325 | send_event_(bluetooth::hci::RemoteNameRequestCompleteBuilder::Create( |
| 326 | bluetooth::hci::ErrorCode::SUCCESS, packet.GetSourceAddress(), |
| 327 | view.GetName())); |
| 328 | } |
| 329 | |
| 330 | void LinkLayerController::IncomingReadRemoteLmpFeatures( |
| 331 | model::packets::LinkLayerPacketView packet) { |
| 332 | SendLinkLayerPacket( |
| 333 | model::packets::ReadRemoteLmpFeaturesResponseBuilder::Create( |
| 334 | packet.GetDestinationAddress(), packet.GetSourceAddress(), |
| 335 | properties_.GetExtendedFeatures(1))); |
| 336 | } |
| 337 | |
| 338 | void LinkLayerController::IncomingReadRemoteLmpFeaturesResponse( |
| 339 | model::packets::LinkLayerPacketView packet) { |
| 340 | auto view = model::packets::ReadRemoteLmpFeaturesResponseView::Create(packet); |
| 341 | ASSERT(view.IsValid()); |
| 342 | send_event_( |
| 343 | bluetooth::hci::RemoteHostSupportedFeaturesNotificationBuilder::Create( |
| 344 | packet.GetSourceAddress(), view.GetFeatures())); |
| 345 | } |
| 346 | |
| 347 | void LinkLayerController::IncomingReadRemoteSupportedFeatures( |
| 348 | model::packets::LinkLayerPacketView packet) { |
| 349 | SendLinkLayerPacket( |
| 350 | model::packets::ReadRemoteSupportedFeaturesResponseBuilder::Create( |
| 351 | packet.GetDestinationAddress(), packet.GetSourceAddress(), |
| 352 | properties_.GetSupportedFeatures())); |
| 353 | } |
| 354 | |
| 355 | void LinkLayerController::IncomingReadRemoteSupportedFeaturesResponse( |
| 356 | model::packets::LinkLayerPacketView packet) { |
| 357 | auto view = |
| 358 | model::packets::ReadRemoteSupportedFeaturesResponseView::Create(packet); |
| 359 | ASSERT(view.IsValid()); |
| 360 | Address source = packet.GetSourceAddress(); |
| 361 | if (connections_.IsDeviceConnected(source)) { |
| 362 | uint16_t handle = connections_.GetHandle(source); |
| 363 | send_event_( |
| 364 | bluetooth::hci::ReadRemoteSupportedFeaturesCompleteBuilder::Create( |
| 365 | bluetooth::hci::ErrorCode::SUCCESS, handle, view.GetFeatures())); |
| 366 | } else { |
| 367 | LOG_INFO("Discarding response from a disconnected device %s", |
| 368 | source.ToString().c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 369 | } |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 370 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 371 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 372 | void LinkLayerController::IncomingReadRemoteExtendedFeatures( |
| 373 | model::packets::LinkLayerPacketView packet) { |
| 374 | auto view = model::packets::ReadRemoteExtendedFeaturesView::Create(packet); |
| 375 | ASSERT(view.IsValid()); |
| 376 | uint8_t page_number = view.GetPageNumber(); |
| 377 | uint8_t error_code = static_cast<uint8_t>(bluetooth::hci::ErrorCode::SUCCESS); |
| 378 | if (page_number > properties_.GetExtendedFeaturesMaximumPageNumber()) { |
| 379 | error_code = static_cast<uint8_t>( |
| 380 | bluetooth::hci::ErrorCode::INVALID_LMP_OR_LL_PARAMETERS); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 381 | } |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 382 | SendLinkLayerPacket( |
| 383 | model::packets::ReadRemoteExtendedFeaturesResponseBuilder::Create( |
| 384 | packet.GetDestinationAddress(), packet.GetSourceAddress(), error_code, |
| 385 | page_number, properties_.GetExtendedFeaturesMaximumPageNumber(), |
| 386 | properties_.GetExtendedFeatures(view.GetPageNumber()))); |
| 387 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 388 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 389 | void LinkLayerController::IncomingReadRemoteExtendedFeaturesResponse( |
| 390 | model::packets::LinkLayerPacketView packet) { |
| 391 | auto view = |
| 392 | model::packets::ReadRemoteExtendedFeaturesResponseView::Create(packet); |
| 393 | ASSERT(view.IsValid()); |
| 394 | Address source = packet.GetSourceAddress(); |
| 395 | if (connections_.IsDeviceConnected(source)) { |
| 396 | uint16_t handle = connections_.GetHandle(packet.GetSourceAddress()); |
| 397 | send_event_( |
| 398 | bluetooth::hci::ReadRemoteExtendedFeaturesCompleteBuilder::Create( |
| 399 | static_cast<bluetooth::hci::ErrorCode>(view.GetStatus()), handle, |
| 400 | view.GetPageNumber(), view.GetMaxPageNumber(), view.GetFeatures())); |
| 401 | } else { |
| 402 | LOG_INFO("Discarding response from a disconnected device %s", |
| 403 | source.ToString().c_str()); |
| 404 | } |
| 405 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 406 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 407 | void LinkLayerController::IncomingReadRemoteVersion( |
| 408 | model::packets::LinkLayerPacketView packet) { |
| 409 | SendLinkLayerPacket( |
| 410 | model::packets::ReadRemoteSupportedFeaturesResponseBuilder::Create( |
| 411 | packet.GetDestinationAddress(), packet.GetSourceAddress(), |
| 412 | properties_.GetSupportedFeatures())); |
| 413 | } |
| 414 | |
| 415 | void LinkLayerController::IncomingReadRemoteVersionResponse( |
| 416 | model::packets::LinkLayerPacketView packet) { |
| 417 | auto view = |
| 418 | model::packets::ReadRemoteVersionInformationResponseView::Create(packet); |
| 419 | ASSERT(view.IsValid()); |
| 420 | Address source = packet.GetSourceAddress(); |
| 421 | if (connections_.IsDeviceConnected(source)) { |
| 422 | uint16_t handle = connections_.GetHandle(packet.GetSourceAddress()); |
| 423 | send_event_( |
| 424 | bluetooth::hci::ReadRemoteVersionInformationCompleteBuilder::Create( |
| 425 | bluetooth::hci::ErrorCode::SUCCESS, handle, view.GetLmpVersion(), |
| 426 | view.GetManufacturerName(), view.GetLmpSubversion())); |
| 427 | } else { |
| 428 | LOG_INFO("Discarding response from a disconnected device %s", |
| 429 | source.ToString().c_str()); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void LinkLayerController::IncomingReadClockOffset( |
| 434 | model::packets::LinkLayerPacketView packet) { |
| 435 | SendLinkLayerPacket(model::packets::ReadClockOffsetResponseBuilder::Create( |
| 436 | packet.GetDestinationAddress(), packet.GetSourceAddress(), |
| 437 | properties_.GetClockOffset())); |
| 438 | } |
| 439 | |
| 440 | void LinkLayerController::IncomingReadClockOffsetResponse( |
| 441 | model::packets::LinkLayerPacketView packet) { |
| 442 | auto view = model::packets::ReadClockOffsetResponseView::Create(packet); |
| 443 | ASSERT(view.IsValid()); |
| 444 | Address source = packet.GetSourceAddress(); |
| 445 | if (connections_.IsDeviceConnected(source)) { |
| 446 | uint16_t handle = connections_.GetHandle(packet.GetSourceAddress()); |
| 447 | send_event_(bluetooth::hci::ReadClockOffsetCompleteBuilder::Create( |
| 448 | bluetooth::hci::ErrorCode::SUCCESS, handle, view.GetOffset())); |
| 449 | } else { |
| 450 | LOG_INFO("Discarding response from a disconnected device %s", |
| 451 | source.ToString().c_str()); |
| 452 | } |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 455 | void LinkLayerController::IncomingDisconnectPacket( |
| 456 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 457 | LOG_INFO("Disconnect Packet"); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 458 | auto disconnect = model::packets::DisconnectView::Create(incoming); |
| 459 | ASSERT(disconnect.IsValid()); |
| 460 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 461 | Address peer = incoming.GetSourceAddress(); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 462 | uint16_t handle = connections_.GetHandle(peer); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 463 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 464 | LOG_INFO("%s: Unknown connection @%s", __func__, peer.ToString().c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 465 | return; |
| 466 | } |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 467 | ASSERT_LOG(connections_.Disconnect(handle), "GetHandle() returned invalid handle %hx", handle); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 468 | |
| 469 | uint8_t reason = disconnect.GetReason(); |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 470 | ScheduleTask(milliseconds(20), [this, handle, reason]() { DisconnectCleanup(handle, reason); }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 473 | void LinkLayerController::IncomingEncryptConnection( |
| 474 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 475 | LOG_INFO("%s", __func__); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 476 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 477 | // TODO: Check keys |
| 478 | Address peer = incoming.GetSourceAddress(); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 479 | uint16_t handle = connections_.GetHandle(peer); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 480 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 481 | LOG_INFO("%s: Unknown connection @%s", __func__, peer.ToString().c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 482 | return; |
| 483 | } |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 484 | send_event_(bluetooth::hci::EncryptionChangeBuilder::Create( |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 485 | bluetooth::hci::ErrorCode::SUCCESS, handle, |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 486 | bluetooth::hci::EncryptionEnabled::ON)); |
| 487 | |
| 488 | uint16_t count = security_manager_.ReadKey(peer); |
| 489 | if (count == 0) { |
| 490 | LOG_ERROR("NO KEY HERE for %s", peer.ToString().c_str()); |
| 491 | return; |
| 492 | } |
| 493 | auto array = security_manager_.GetKey(peer); |
| 494 | std::vector<uint8_t> key_vec{array.begin(), array.end()}; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 495 | auto response = model::packets::EncryptConnectionResponseBuilder::Create( |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 496 | properties_.GetAddress(), peer, key_vec); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 497 | SendLinkLayerPacket(std::move(response)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 500 | void LinkLayerController::IncomingEncryptConnectionResponse( |
| 501 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 502 | LOG_INFO("%s", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 503 | // TODO: Check keys |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 504 | uint16_t handle = connections_.GetHandle(incoming.GetSourceAddress()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 505 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 506 | LOG_INFO("%s: Unknown connection @%s", __func__, incoming.GetSourceAddress().ToString().c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 507 | return; |
| 508 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 509 | auto packet = bluetooth::hci::EncryptionChangeBuilder::Create( |
| 510 | bluetooth::hci::ErrorCode::SUCCESS, handle, |
| 511 | bluetooth::hci::EncryptionEnabled::ON); |
| 512 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 515 | void LinkLayerController::IncomingInquiryPacket( |
| 516 | model::packets::LinkLayerPacketView incoming) { |
| 517 | auto inquiry = model::packets::InquiryView::Create(incoming); |
| 518 | ASSERT(inquiry.IsValid()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 519 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 520 | Address peer = incoming.GetSourceAddress(); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 521 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 522 | switch (inquiry.GetInquiryType()) { |
| 523 | case (model::packets::InquiryType::STANDARD): { |
| 524 | auto inquiry_response = model::packets::InquiryResponseBuilder::Create( |
| 525 | properties_.GetAddress(), peer, |
| 526 | properties_.GetPageScanRepetitionMode(), |
| 527 | properties_.GetClassOfDevice(), properties_.GetClockOffset()); |
| 528 | SendLinkLayerPacket(std::move(inquiry_response)); |
| 529 | } break; |
| 530 | case (model::packets::InquiryType::RSSI): { |
| 531 | auto inquiry_response = |
| 532 | model::packets::InquiryResponseWithRssiBuilder::Create( |
| 533 | properties_.GetAddress(), peer, |
| 534 | properties_.GetPageScanRepetitionMode(), |
| 535 | properties_.GetClassOfDevice(), properties_.GetClockOffset(), |
| 536 | GetRssi()); |
| 537 | SendLinkLayerPacket(std::move(inquiry_response)); |
| 538 | } break; |
| 539 | case (model::packets::InquiryType::EXTENDED): { |
| 540 | auto inquiry_response = |
| 541 | model::packets::ExtendedInquiryResponseBuilder::Create( |
| 542 | properties_.GetAddress(), peer, |
| 543 | properties_.GetPageScanRepetitionMode(), |
| 544 | properties_.GetClassOfDevice(), properties_.GetClockOffset(), |
| 545 | GetRssi(), properties_.GetExtendedInquiryData()); |
| 546 | SendLinkLayerPacket(std::move(inquiry_response)); |
| 547 | |
| 548 | } break; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 549 | default: |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 550 | LOG_WARN("Unhandled Incoming Inquiry of type %d", static_cast<int>(inquiry.GetType())); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 551 | return; |
| 552 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 553 | // TODO: Send an Inquiry Response Notification Event 7.7.74 |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 554 | } |
| 555 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 556 | void LinkLayerController::IncomingInquiryResponsePacket( |
| 557 | model::packets::LinkLayerPacketView incoming) { |
| 558 | auto basic_inquiry_response = |
| 559 | model::packets::BasicInquiryResponseView::Create(incoming); |
| 560 | ASSERT(basic_inquiry_response.IsValid()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 561 | std::vector<uint8_t> eir; |
| 562 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 563 | switch (basic_inquiry_response.GetInquiryType()) { |
| 564 | case (model::packets::InquiryType::STANDARD): { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 565 | // TODO: Support multiple inquiries in the same packet. |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 566 | auto inquiry_response = |
| 567 | model::packets::InquiryResponseView::Create(basic_inquiry_response); |
| 568 | ASSERT(inquiry_response.IsValid()); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 569 | |
| 570 | auto page_scan_repetition_mode = |
| 571 | (bluetooth::hci::PageScanRepetitionMode) |
| 572 | inquiry_response.GetPageScanRepetitionMode(); |
| 573 | |
| 574 | auto packet = bluetooth::hci::InquiryResultBuilder::Create( |
| 575 | 0x01, inquiry_response.GetSourceAddress(), page_scan_repetition_mode, |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 576 | inquiry_response.GetClassOfDevice(), |
| 577 | inquiry_response.GetClockOffset()); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 578 | |
| 579 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 580 | } break; |
| 581 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 582 | case (model::packets::InquiryType::RSSI): { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 583 | auto inquiry_response = |
| 584 | model::packets::InquiryResponseWithRssiView::Create( |
| 585 | basic_inquiry_response); |
| 586 | ASSERT(inquiry_response.IsValid()); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 587 | |
| 588 | auto page_scan_repetition_mode = |
| 589 | (bluetooth::hci::PageScanRepetitionMode) |
| 590 | inquiry_response.GetPageScanRepetitionMode(); |
| 591 | |
| 592 | auto packet = bluetooth::hci::InquiryResultWithRssiBuilder::Create( |
| 593 | 0x01, inquiry_response.GetSourceAddress(), page_scan_repetition_mode, |
| 594 | inquiry_response.GetClassOfDevice(), |
| 595 | inquiry_response.GetClockOffset(), inquiry_response.GetRssi()); |
| 596 | send_event_(std::move(packet)); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 597 | } break; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 598 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 599 | case (model::packets::InquiryType::EXTENDED): { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 600 | auto inquiry_response = |
| 601 | model::packets::ExtendedInquiryResponseView::Create( |
| 602 | basic_inquiry_response); |
| 603 | ASSERT(inquiry_response.IsValid()); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 604 | |
| 605 | std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr = |
| 606 | std::make_unique<bluetooth::packet::RawBuilder>(); |
Chienyuan | c27da09 | 2019-11-20 19:29:43 +0800 | [diff] [blame] | 607 | raw_builder_ptr->AddOctets1(kNumCommandPackets); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 608 | raw_builder_ptr->AddAddress(inquiry_response.GetSourceAddress()); |
| 609 | raw_builder_ptr->AddOctets1(inquiry_response.GetPageScanRepetitionMode()); |
| 610 | raw_builder_ptr->AddOctets1(0x00); // _reserved_ |
| 611 | auto class_of_device = inquiry_response.GetClassOfDevice(); |
| 612 | for (unsigned int i = 0; i < class_of_device.kLength; i++) { |
| 613 | raw_builder_ptr->AddOctets1(class_of_device.cod[i]); |
| 614 | } |
| 615 | raw_builder_ptr->AddOctets2(inquiry_response.GetClockOffset()); |
| 616 | raw_builder_ptr->AddOctets1(inquiry_response.GetRssi()); |
| 617 | raw_builder_ptr->AddOctets(inquiry_response.GetExtendedData()); |
| 618 | |
| 619 | auto packet = bluetooth::hci::EventPacketBuilder::Create( |
| 620 | bluetooth::hci::EventCode::EXTENDED_INQUIRY_RESULT, |
| 621 | std::move(raw_builder_ptr)); |
| 622 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 623 | } break; |
| 624 | default: |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 625 | LOG_WARN("Unhandled Incoming Inquiry Response of type %d", |
| 626 | static_cast<int>(basic_inquiry_response.GetInquiryType())); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 630 | void LinkLayerController::IncomingIoCapabilityRequestPacket( |
| 631 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 632 | LOG_DEBUG("%s", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 633 | if (!simple_pairing_mode_enabled_) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 634 | LOG_WARN("%s: Only simple pairing mode is implemented", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 635 | return; |
| 636 | } |
Chienyuan Huang | 85471e4 | 2019-11-06 12:52:58 +0000 | [diff] [blame] | 637 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 638 | auto request = model::packets::IoCapabilityRequestView::Create(incoming); |
| 639 | ASSERT(request.IsValid()); |
| 640 | |
| 641 | Address peer = incoming.GetSourceAddress(); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 642 | uint8_t io_capability = request.GetIoCapability(); |
| 643 | uint8_t oob_data_present = request.GetOobDataPresent(); |
| 644 | uint8_t authentication_requirements = request.GetAuthenticationRequirements(); |
| 645 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 646 | uint16_t handle = connections_.GetHandle(peer); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 647 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 648 | LOG_INFO("%s: Device not connected %s", __func__, peer.ToString().c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 649 | return; |
| 650 | } |
| 651 | |
| 652 | security_manager_.AuthenticationRequest(peer, handle); |
| 653 | |
| 654 | security_manager_.SetPeerIoCapability(peer, io_capability, oob_data_present, authentication_requirements); |
| 655 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 656 | auto packet = bluetooth::hci::IoCapabilityResponseBuilder::Create( |
| 657 | peer, static_cast<bluetooth::hci::IoCapability>(io_capability), |
| 658 | static_cast<bluetooth::hci::OobDataPresent>(oob_data_present), |
| 659 | static_cast<bluetooth::hci::AuthenticationRequirements>( |
| 660 | authentication_requirements)); |
| 661 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 662 | |
| 663 | StartSimplePairing(peer); |
| 664 | } |
| 665 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 666 | void LinkLayerController::IncomingIoCapabilityResponsePacket( |
| 667 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 668 | LOG_DEBUG("%s", __func__); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 669 | |
| 670 | auto response = model::packets::IoCapabilityResponseView::Create(incoming); |
| 671 | ASSERT(response.IsValid()); |
| 672 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 673 | Address peer = incoming.GetSourceAddress(); |
| 674 | uint8_t io_capability = response.GetIoCapability(); |
| 675 | uint8_t oob_data_present = response.GetOobDataPresent(); |
| 676 | uint8_t authentication_requirements = response.GetAuthenticationRequirements(); |
| 677 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 678 | security_manager_.SetPeerIoCapability(peer, io_capability, oob_data_present, |
| 679 | authentication_requirements); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 680 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 681 | auto packet = bluetooth::hci::IoCapabilityResponseBuilder::Create( |
| 682 | peer, static_cast<bluetooth::hci::IoCapability>(io_capability), |
| 683 | static_cast<bluetooth::hci::OobDataPresent>(oob_data_present), |
| 684 | static_cast<bluetooth::hci::AuthenticationRequirements>( |
| 685 | authentication_requirements)); |
| 686 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 687 | |
| 688 | PairingType pairing_type = security_manager_.GetSimplePairingType(); |
| 689 | if (pairing_type != PairingType::INVALID) { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 690 | ScheduleTask(milliseconds(5), [this, peer, pairing_type]() { |
| 691 | AuthenticateRemoteStage1(peer, pairing_type); |
| 692 | }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 693 | } else { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 694 | LOG_INFO("%s: Security Manager returned INVALID", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 698 | void LinkLayerController::IncomingIoCapabilityNegativeResponsePacket( |
| 699 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 700 | LOG_DEBUG("%s", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 701 | Address peer = incoming.GetSourceAddress(); |
| 702 | |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 703 | ASSERT(security_manager_.GetAuthenticationAddress() == peer); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 704 | |
| 705 | security_manager_.InvalidateIoCapabilities(); |
| 706 | } |
| 707 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 708 | void LinkLayerController::IncomingLeAdvertisementPacket( |
| 709 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 710 | // TODO: Handle multiple advertisements per packet. |
| 711 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 712 | Address address = incoming.GetSourceAddress(); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 713 | auto advertisement = model::packets::LeAdvertisementView::Create(incoming); |
| 714 | ASSERT(advertisement.IsValid()); |
| 715 | auto adv_type = static_cast<LeAdvertisement::AdvertisementType>( |
| 716 | advertisement.GetAdvertisementType()); |
| 717 | auto address_type = |
| 718 | static_cast<LeAdvertisement::AddressType>(advertisement.GetAddressType()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 719 | |
| 720 | if (le_scan_enable_) { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 721 | vector<uint8_t> ad = advertisement.GetData(); |
| 722 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 723 | std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr = |
| 724 | std::make_unique<bluetooth::packet::RawBuilder>(); |
| 725 | raw_builder_ptr->AddOctets1( |
| 726 | static_cast<uint8_t>(bluetooth::hci::SubeventCode::ADVERTISING_REPORT)); |
| 727 | raw_builder_ptr->AddOctets1(0x01); // num reports |
| 728 | raw_builder_ptr->AddOctets1(static_cast<uint8_t>(adv_type)); |
| 729 | raw_builder_ptr->AddOctets1(static_cast<uint8_t>(address_type)); |
| 730 | raw_builder_ptr->AddAddress(address); |
| 731 | raw_builder_ptr->AddOctets1(ad.size()); |
| 732 | raw_builder_ptr->AddOctets(ad); |
| 733 | raw_builder_ptr->AddOctets1(GetRssi()); |
| 734 | auto packet = bluetooth::hci::EventPacketBuilder::Create( |
| 735 | bluetooth::hci::EventCode::LE_META_EVENT, std::move(raw_builder_ptr)); |
| 736 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 739 | // Active scanning |
| 740 | if (le_scan_enable_ && le_scan_type_ == 1) { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 741 | auto to_send = model::packets::LeScanBuilder::Create( |
| 742 | properties_.GetLeAddress(), address); |
| 743 | SendLeLinkLayerPacket(std::move(to_send)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 744 | } |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 745 | |
| 746 | // Connect |
| 747 | if ((le_connect_ && le_peer_address_ == address && le_peer_address_type_ == static_cast<uint8_t>(address_type) && |
| 748 | (adv_type == LeAdvertisement::AdvertisementType::ADV_IND || |
| 749 | adv_type == LeAdvertisement::AdvertisementType::ADV_DIRECT_IND)) || |
| 750 | (LeWhiteListContainsDevice(address, static_cast<uint8_t>(address_type)))) { |
| 751 | if (!connections_.CreatePendingLeConnection(incoming.GetSourceAddress(), static_cast<uint8_t>(address_type))) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 752 | LOG_WARN("%s: CreatePendingLeConnection failed for connection to %s (type %hhx)", __func__, |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 753 | incoming.GetSourceAddress().ToString().c_str(), address_type); |
| 754 | } |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 755 | LOG_INFO("%s: connecting to %s (type %hhx)", __func__, incoming.GetSourceAddress().ToString().c_str(), |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 756 | address_type); |
| 757 | le_connect_ = false; |
| 758 | le_scan_enable_ = false; |
| 759 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 760 | auto to_send = model::packets::LeConnectBuilder::Create( |
| 761 | properties_.GetLeAddress(), incoming.GetSourceAddress(), |
| 762 | le_connection_interval_min_, le_connection_interval_max_, |
| 763 | le_connection_latency_, le_connection_supervision_timeout_, |
| 764 | static_cast<uint8_t>(le_address_type_)); |
| 765 | |
| 766 | SendLeLinkLayerPacket(std::move(to_send)); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
| 770 | void LinkLayerController::HandleLeConnection(Address address, uint8_t address_type, uint8_t own_address_type, |
| 771 | uint8_t role, uint16_t connection_interval, uint16_t connection_latency, |
| 772 | uint16_t supervision_timeout) { |
| 773 | // TODO: Choose between LeConnectionComplete and LeEnhancedConnectionComplete |
| 774 | uint16_t handle = connections_.CreateLeConnection(address, address_type, own_address_type); |
| 775 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 776 | LOG_WARN("%s: No pending connection for connection from %s (type %hhx)", __func__, address.ToString().c_str(), |
| 777 | address_type); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 778 | return; |
| 779 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 780 | auto packet = bluetooth::hci::LeConnectionCompleteBuilder::Create( |
| 781 | bluetooth::hci::ErrorCode::SUCCESS, handle, |
| 782 | static_cast<bluetooth::hci::Role>(role), |
| 783 | static_cast<bluetooth::hci::AddressType>(address_type), address, |
| 784 | connection_interval, connection_latency, supervision_timeout, |
| 785 | static_cast<bluetooth::hci::MasterClockAccuracy>(0x00)); |
| 786 | send_event_(std::move(packet)); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 789 | void LinkLayerController::IncomingLeConnectPacket( |
| 790 | model::packets::LinkLayerPacketView incoming) { |
| 791 | auto connect = model::packets::LeConnectView::Create(incoming); |
| 792 | ASSERT(connect.IsValid()); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 793 | uint16_t connection_interval = (connect.GetLeConnectionIntervalMax() + connect.GetLeConnectionIntervalMin()) / 2; |
| 794 | if (!connections_.CreatePendingLeConnection(incoming.GetSourceAddress(), |
| 795 | static_cast<uint8_t>(connect.GetAddressType()))) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 796 | LOG_WARN("%s: CreatePendingLeConnection failed for connection from %s (type %hhx)", __func__, |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 797 | incoming.GetSourceAddress().ToString().c_str(), connect.GetAddressType()); |
| 798 | return; |
| 799 | } |
| 800 | HandleLeConnection(incoming.GetSourceAddress(), static_cast<uint8_t>(connect.GetAddressType()), |
| 801 | static_cast<uint8_t>(properties_.GetLeAdvertisingOwnAddressType()), |
| 802 | static_cast<uint8_t>(hci::Role::SLAVE), connection_interval, connect.GetLeConnectionLatency(), |
| 803 | connect.GetLeConnectionSupervisionTimeout()); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 804 | |
| 805 | auto to_send = model::packets::LeConnectCompleteBuilder::Create( |
| 806 | incoming.GetDestinationAddress(), incoming.GetSourceAddress(), |
| 807 | connection_interval, connect.GetLeConnectionLatency(), |
| 808 | connect.GetLeConnectionSupervisionTimeout(), |
| 809 | properties_.GetLeAdvertisingOwnAddressType()); |
| 810 | SendLeLinkLayerPacket(std::move(to_send)); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 813 | void LinkLayerController::IncomingLeConnectCompletePacket( |
| 814 | model::packets::LinkLayerPacketView incoming) { |
| 815 | auto complete = model::packets::LeConnectCompleteView::Create(incoming); |
| 816 | ASSERT(complete.IsValid()); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 817 | HandleLeConnection(incoming.GetSourceAddress(), static_cast<uint8_t>(complete.GetAddressType()), |
| 818 | static_cast<uint8_t>(le_address_type_), static_cast<uint8_t>(hci::Role::MASTER), |
| 819 | complete.GetLeConnectionInterval(), complete.GetLeConnectionLatency(), |
| 820 | complete.GetLeConnectionSupervisionTimeout()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 821 | } |
| 822 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 823 | void LinkLayerController::IncomingLeScanPacket( |
| 824 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 825 | LOG_INFO("LE Scan Packet"); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 826 | |
| 827 | auto to_send = model::packets::LeScanResponseBuilder::Create( |
| 828 | properties_.GetLeAddress(), incoming.GetSourceAddress(), |
| 829 | static_cast<model::packets::AddressType>(properties_.GetLeAddressType()), |
| 830 | static_cast<model::packets::AdvertisementType>( |
| 831 | properties_.GetLeAdvertisementType()), |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 832 | properties_.GetLeScanResponse()); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 833 | |
| 834 | SendLeLinkLayerPacket(std::move(to_send)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 837 | void LinkLayerController::IncomingLeScanResponsePacket( |
| 838 | model::packets::LinkLayerPacketView incoming) { |
| 839 | auto scan_response = model::packets::LeScanResponseView::Create(incoming); |
| 840 | ASSERT(scan_response.IsValid()); |
| 841 | vector<uint8_t> ad = scan_response.GetData(); |
| 842 | auto adv_type = static_cast<LeAdvertisement::AdvertisementType>( |
| 843 | scan_response.GetAdvertisementType()); |
| 844 | auto address_type = |
| 845 | static_cast<LeAdvertisement::AddressType>(scan_response.GetAddressType()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 846 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 847 | std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr = |
| 848 | std::make_unique<bluetooth::packet::RawBuilder>(); |
| 849 | raw_builder_ptr->AddOctets1( |
| 850 | static_cast<uint8_t>(bluetooth::hci::SubeventCode::ADVERTISING_REPORT)); |
| 851 | raw_builder_ptr->AddOctets1(0x01); // num reports |
| 852 | raw_builder_ptr->AddOctets1(static_cast<uint8_t>(adv_type)); |
| 853 | raw_builder_ptr->AddOctets1(static_cast<uint8_t>(address_type)); |
| 854 | raw_builder_ptr->AddAddress(incoming.GetSourceAddress()); |
| 855 | raw_builder_ptr->AddOctets1(ad.size()); |
| 856 | raw_builder_ptr->AddOctets(ad); |
| 857 | raw_builder_ptr->AddOctets1(GetRssi()); |
| 858 | auto packet = bluetooth::hci::EventPacketBuilder::Create( |
| 859 | bluetooth::hci::EventCode::LE_META_EVENT, std::move(raw_builder_ptr)); |
| 860 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 863 | void LinkLayerController::IncomingPagePacket( |
| 864 | model::packets::LinkLayerPacketView incoming) { |
| 865 | auto page = model::packets::PageView::Create(incoming); |
| 866 | ASSERT(page.IsValid()); |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 867 | LOG_INFO("%s from %s", __func__, incoming.GetSourceAddress().ToString().c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 868 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 869 | if (!connections_.CreatePendingConnection( |
| 870 | incoming.GetSourceAddress(), properties_.GetAuthenticationEnable())) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 871 | // Send a response to indicate that we're busy, or drop the packet? |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 872 | LOG_WARN("%s: Failed to create a pending connection for %s", __func__, |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 873 | incoming.GetSourceAddress().ToString().c_str()); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 876 | bluetooth::hci::Address source_address; |
| 877 | bluetooth::hci::Address::FromString(page.GetSourceAddress().ToString(), |
| 878 | source_address); |
| 879 | |
| 880 | auto packet = bluetooth::hci::ConnectionRequestBuilder::Create( |
| 881 | source_address, page.GetClassOfDevice(), |
| 882 | bluetooth::hci::ConnectionRequestLinkType::ACL); |
| 883 | |
| 884 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 887 | void LinkLayerController::IncomingPageRejectPacket( |
| 888 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 889 | LOG_INFO("%s: %s", __func__, incoming.GetSourceAddress().ToString().c_str()); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 890 | auto reject = model::packets::PageRejectView::Create(incoming); |
| 891 | ASSERT(reject.IsValid()); |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 892 | LOG_INFO("%s: Sending CreateConnectionComplete", __func__); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 893 | auto packet = bluetooth::hci::ConnectionCompleteBuilder::Create( |
| 894 | static_cast<bluetooth::hci::ErrorCode>(reject.GetReason()), 0x0eff, |
| 895 | incoming.GetSourceAddress(), bluetooth::hci::LinkType::ACL, |
| 896 | bluetooth::hci::Enable::DISABLED); |
| 897 | send_event_(std::move(packet)); |
Hansong Zhang | 4cb1cf5 | 2019-07-08 15:32:26 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 900 | void LinkLayerController::IncomingPageResponsePacket( |
| 901 | model::packets::LinkLayerPacketView incoming) { |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 902 | Address peer = incoming.GetSourceAddress(); |
| 903 | LOG_INFO("%s: %s", __func__, peer.ToString().c_str()); |
| 904 | bool awaiting_authentication = connections_.AuthenticatePendingConnection(); |
| 905 | uint16_t handle = connections_.CreateConnection(peer); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 906 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 907 | LOG_WARN("%s: No free handles", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 908 | return; |
| 909 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 910 | auto packet = bluetooth::hci::ConnectionCompleteBuilder::Create( |
| 911 | bluetooth::hci::ErrorCode::SUCCESS, handle, incoming.GetSourceAddress(), |
| 912 | bluetooth::hci::LinkType::ACL, bluetooth::hci::Enable::DISABLED); |
| 913 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 914 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 915 | if (awaiting_authentication) { |
| 916 | ScheduleTask(milliseconds(5), [this, peer, handle]() { |
| 917 | HandleAuthenticationRequest(peer, handle); |
| 918 | }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
| 922 | void LinkLayerController::TimerTick() { |
| 923 | if (inquiry_state_ == Inquiry::InquiryState::INQUIRY) Inquiry(); |
| 924 | if (inquiry_state_ == Inquiry::InquiryState::INQUIRY) PageScan(); |
Myles Watson | 99aea89 | 2019-09-02 15:54:08 -0700 | [diff] [blame] | 925 | LeAdvertising(); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 926 | Connections(); |
| 927 | } |
| 928 | |
Myles Watson | 99aea89 | 2019-09-02 15:54:08 -0700 | [diff] [blame] | 929 | void LinkLayerController::LeAdvertising() { |
| 930 | if (!le_advertising_enable_) { |
| 931 | return; |
| 932 | } |
| 933 | steady_clock::time_point now = steady_clock::now(); |
| 934 | if (duration_cast<milliseconds>(now - last_le_advertisement_) < milliseconds(200)) { |
| 935 | return; |
| 936 | } |
Chienyuan | 1290e96 | 2019-11-07 17:37:26 +0800 | [diff] [blame] | 937 | last_le_advertisement_ = now; |
Myles Watson | 99aea89 | 2019-09-02 15:54:08 -0700 | [diff] [blame] | 938 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 939 | auto own_address_type = static_cast<model::packets::AddressType>( |
| 940 | properties_.GetLeAdvertisingOwnAddressType()); |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 941 | Address advertising_address = Address::kEmpty; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 942 | if (own_address_type == model::packets::AddressType::PUBLIC) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 943 | advertising_address = properties_.GetAddress(); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 944 | } else if (own_address_type == model::packets::AddressType::RANDOM) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 945 | advertising_address = properties_.GetLeAddress(); |
Myles Watson | 99aea89 | 2019-09-02 15:54:08 -0700 | [diff] [blame] | 946 | } |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 947 | ASSERT(advertising_address != Address::kEmpty); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 948 | auto to_send = model::packets::LeAdvertisementBuilder::Create( |
| 949 | advertising_address, Address::kEmpty, own_address_type, |
| 950 | static_cast<model::packets::AdvertisementType>(own_address_type), |
| 951 | properties_.GetLeAdvertisement()); |
| 952 | SendLeLinkLayerPacket(std::move(to_send)); |
Myles Watson | 99aea89 | 2019-09-02 15:54:08 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 955 | void LinkLayerController::Connections() { |
| 956 | // TODO: Keep connections alive? |
| 957 | } |
| 958 | |
| 959 | void LinkLayerController::RegisterEventChannel( |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 960 | const std::function< |
| 961 | void(std::shared_ptr<bluetooth::hci::EventPacketBuilder>)>& callback) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 962 | send_event_ = callback; |
| 963 | } |
| 964 | |
| 965 | void LinkLayerController::RegisterAclChannel( |
Chienyuan | 85db6ee | 2019-11-21 22:07:09 +0800 | [diff] [blame] | 966 | const std::function< |
| 967 | void(std::shared_ptr<bluetooth::hci::AclPacketBuilder>)>& callback) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 968 | send_acl_ = callback; |
| 969 | } |
| 970 | |
| 971 | void LinkLayerController::RegisterScoChannel( |
| 972 | const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) { |
| 973 | send_sco_ = callback; |
| 974 | } |
| 975 | |
Jakub Pawlowski | de6c013 | 2019-11-12 16:14:32 +0100 | [diff] [blame] | 976 | void LinkLayerController::RegisterIsoChannel( |
| 977 | const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& |
| 978 | callback) { |
| 979 | send_iso_ = callback; |
| 980 | } |
| 981 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 982 | void LinkLayerController::RegisterRemoteChannel( |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 983 | const std::function<void( |
| 984 | std::shared_ptr<model::packets::LinkLayerPacketBuilder>, Phy::Type)>& |
| 985 | callback) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 986 | send_to_remote_ = callback; |
| 987 | } |
| 988 | |
| 989 | void LinkLayerController::RegisterTaskScheduler( |
| 990 | std::function<AsyncTaskId(milliseconds, const TaskCallback&)> event_scheduler) { |
| 991 | schedule_task_ = event_scheduler; |
| 992 | } |
| 993 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 994 | AsyncTaskId LinkLayerController::ScheduleTask(milliseconds delay_ms, const TaskCallback& callback) { |
| 995 | if (schedule_task_) { |
| 996 | return schedule_task_(delay_ms, callback); |
| 997 | } else { |
| 998 | callback(); |
| 999 | return 0; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1003 | void LinkLayerController::RegisterPeriodicTaskScheduler( |
| 1004 | std::function<AsyncTaskId(milliseconds, milliseconds, const TaskCallback&)> periodic_event_scheduler) { |
| 1005 | schedule_periodic_task_ = periodic_event_scheduler; |
| 1006 | } |
| 1007 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1008 | void LinkLayerController::CancelScheduledTask(AsyncTaskId task_id) { |
| 1009 | if (schedule_task_ && cancel_task_) { |
| 1010 | cancel_task_(task_id); |
| 1011 | } |
| 1012 | } |
| 1013 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1014 | void LinkLayerController::RegisterTaskCancel(std::function<void(AsyncTaskId)> task_cancel) { |
| 1015 | cancel_task_ = task_cancel; |
| 1016 | } |
| 1017 | |
| 1018 | void LinkLayerController::AddControllerEvent(milliseconds delay, const TaskCallback& task) { |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1019 | controller_events_.push_back(ScheduleTask(delay, task)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | void LinkLayerController::WriteSimplePairingMode(bool enabled) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1023 | ASSERT_LOG(enabled, "The spec says don't disable this!"); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1024 | simple_pairing_mode_enabled_ = enabled; |
| 1025 | } |
| 1026 | |
| 1027 | void LinkLayerController::StartSimplePairing(const Address& address) { |
| 1028 | // IO Capability Exchange (See the Diagram in the Spec) |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1029 | auto packet = bluetooth::hci::IoCapabilityRequestBuilder::Create(address); |
| 1030 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1031 | |
| 1032 | // Get a Key, then authenticate |
| 1033 | // PublicKeyExchange(address); |
| 1034 | // AuthenticateRemoteStage1(address); |
| 1035 | // AuthenticateRemoteStage2(address); |
| 1036 | } |
| 1037 | |
| 1038 | void LinkLayerController::AuthenticateRemoteStage1(const Address& peer, PairingType pairing_type) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1039 | ASSERT(security_manager_.GetAuthenticationAddress() == peer); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1040 | // TODO: Public key exchange first? |
| 1041 | switch (pairing_type) { |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1042 | case PairingType::AUTO_CONFIRMATION: |
| 1043 | send_event_( |
| 1044 | bluetooth::hci::UserConfirmationRequestBuilder::Create(peer, 123456)); |
| 1045 | break; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1046 | case PairingType::CONFIRM_Y_N: |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1047 | send_event_( |
| 1048 | bluetooth::hci::UserConfirmationRequestBuilder::Create(peer, 123456)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1049 | break; |
| 1050 | case PairingType::DISPLAY_PIN: |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1051 | send_event_( |
| 1052 | bluetooth::hci::UserConfirmationRequestBuilder::Create(peer, 123456)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1053 | break; |
| 1054 | case PairingType::DISPLAY_AND_CONFIRM: |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1055 | send_event_( |
| 1056 | bluetooth::hci::UserConfirmationRequestBuilder::Create(peer, 123456)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1057 | break; |
| 1058 | case PairingType::INPUT_PIN: |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1059 | send_event_(bluetooth::hci::UserPasskeyRequestBuilder::Create(peer)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1060 | break; |
| 1061 | default: |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1062 | LOG_ALWAYS_FATAL("Invalid PairingType %d", static_cast<int>(pairing_type)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void LinkLayerController::AuthenticateRemoteStage2(const Address& peer) { |
| 1067 | uint16_t handle = security_manager_.GetAuthenticationHandle(); |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1068 | ASSERT(security_manager_.GetAuthenticationAddress() == peer); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1069 | // Check key in security_manager_ ? |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1070 | auto packet = bluetooth::hci::AuthenticationCompleteBuilder::Create( |
| 1071 | bluetooth::hci::ErrorCode::SUCCESS, handle); |
| 1072 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1075 | bluetooth::hci::ErrorCode LinkLayerController::LinkKeyRequestReply( |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1076 | const Address& peer, const std::array<uint8_t, 16>& key) { |
| 1077 | security_manager_.WriteKey(peer, key); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1078 | security_manager_.AuthenticationRequestFinished(); |
| 1079 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1080 | ScheduleTask(milliseconds(5), [this, peer]() { AuthenticateRemoteStage2(peer); }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1081 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1082 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1085 | bluetooth::hci::ErrorCode LinkLayerController::LinkKeyRequestNegativeReply( |
| 1086 | const Address& address) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1087 | security_manager_.DeleteKey(address); |
| 1088 | // Simple pairing to get a key |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1089 | uint16_t handle = connections_.GetHandle(address); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1090 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1091 | LOG_INFO("%s: Device not connected %s", __func__, address.ToString().c_str()); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1092 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | security_manager_.AuthenticationRequest(address, handle); |
| 1096 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1097 | ScheduleTask(milliseconds(5), [this, address]() { StartSimplePairing(address); }); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1098 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1101 | bluetooth::hci::ErrorCode LinkLayerController::IoCapabilityRequestReply( |
| 1102 | const Address& peer, uint8_t io_capability, uint8_t oob_data_present_flag, |
| 1103 | uint8_t authentication_requirements) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1104 | security_manager_.SetLocalIoCapability(peer, io_capability, oob_data_present_flag, authentication_requirements); |
| 1105 | |
| 1106 | PairingType pairing_type = security_manager_.GetSimplePairingType(); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1107 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1108 | if (pairing_type != PairingType::INVALID) { |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1109 | ScheduleTask(milliseconds(5), [this, peer, pairing_type]() { |
| 1110 | AuthenticateRemoteStage1(peer, pairing_type); |
| 1111 | }); |
| 1112 | SendLinkLayerPacket(model::packets::IoCapabilityResponseBuilder::Create( |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1113 | properties_.GetAddress(), peer, io_capability, oob_data_present_flag, |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1114 | authentication_requirements)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1115 | } else { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1116 | LOG_INFO("%s: Requesting remote capability", __func__); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1117 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1118 | SendLinkLayerPacket(model::packets::IoCapabilityRequestBuilder::Create( |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1119 | properties_.GetAddress(), peer, io_capability, oob_data_present_flag, |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1120 | authentication_requirements)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1123 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1126 | bluetooth::hci::ErrorCode LinkLayerController::IoCapabilityRequestNegativeReply( |
| 1127 | const Address& peer, hci::Status reason) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1128 | if (security_manager_.GetAuthenticationAddress() != peer) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1129 | return bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | security_manager_.InvalidateIoCapabilities(); |
| 1133 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1134 | auto packet = model::packets::IoCapabilityNegativeResponseBuilder::Create( |
| 1135 | properties_.GetAddress(), peer, static_cast<uint8_t>(reason)); |
| 1136 | SendLinkLayerPacket(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1137 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1138 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1141 | bluetooth::hci::ErrorCode LinkLayerController::UserConfirmationRequestReply( |
| 1142 | const Address& peer) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1143 | if (security_manager_.GetAuthenticationAddress() != peer) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1144 | return bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1145 | } |
| 1146 | // TODO: Key could be calculated here. |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1147 | std::array<uint8_t, 16> key_vec{1, 2, 3, 4, 5, 6, 7, 8, |
| 1148 | 9, 10, 11, 12, 13, 14, 15, 16}; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1149 | security_manager_.WriteKey(peer, key_vec); |
| 1150 | |
| 1151 | security_manager_.AuthenticationRequestFinished(); |
| 1152 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1153 | ScheduleTask(milliseconds(5), [this, peer, key_vec]() { |
| 1154 | send_event_(bluetooth::hci::LinkKeyNotificationBuilder::Create( |
| 1155 | peer, key_vec, bluetooth::hci::KeyType::AUTHENTICATED_P256)); |
| 1156 | }); |
| 1157 | |
| 1158 | ScheduleTask(milliseconds(15), |
| 1159 | [this, peer]() { AuthenticateRemoteStage2(peer); }); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1160 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1163 | bluetooth::hci::ErrorCode |
| 1164 | LinkLayerController::UserConfirmationRequestNegativeReply(const Address& peer) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1165 | if (security_manager_.GetAuthenticationAddress() != peer) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1166 | return bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1167 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1168 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1171 | bluetooth::hci::ErrorCode LinkLayerController::UserPasskeyRequestReply( |
| 1172 | const Address& peer, uint32_t numeric_value) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1173 | if (security_manager_.GetAuthenticationAddress() != peer) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1174 | return bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1175 | } |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1176 | LOG_INFO("TODO:Do something with the passkey %06d", numeric_value); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1177 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1180 | bluetooth::hci::ErrorCode LinkLayerController::UserPasskeyRequestNegativeReply( |
| 1181 | const Address& peer) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1182 | if (security_manager_.GetAuthenticationAddress() != peer) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1183 | return bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1184 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1185 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1188 | bluetooth::hci::ErrorCode LinkLayerController::RemoteOobDataRequestReply( |
| 1189 | const Address& peer, const std::vector<uint8_t>& c, |
| 1190 | const std::vector<uint8_t>& r) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1191 | if (security_manager_.GetAuthenticationAddress() != peer) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1192 | return bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1193 | } |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1194 | LOG_INFO("TODO:Do something with the OOB data c=%d r=%d", c[0], r[0]); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1195 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1198 | bluetooth::hci::ErrorCode |
| 1199 | LinkLayerController::RemoteOobDataRequestNegativeReply(const Address& peer) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1200 | if (security_manager_.GetAuthenticationAddress() != peer) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1201 | return bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1202 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1203 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | void LinkLayerController::HandleAuthenticationRequest(const Address& address, uint16_t handle) { |
| 1207 | if (simple_pairing_mode_enabled_ == true) { |
| 1208 | security_manager_.AuthenticationRequest(address, handle); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1209 | auto packet = bluetooth::hci::LinkKeyRequestBuilder::Create(address); |
| 1210 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1211 | } else { // Should never happen for our phones |
| 1212 | // Check for a key, try to authenticate, ask for a PIN. |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1213 | auto packet = bluetooth::hci::AuthenticationCompleteBuilder::Create( |
| 1214 | bluetooth::hci::ErrorCode::AUTHENTICATION_FAILURE, handle); |
| 1215 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1219 | bluetooth::hci::ErrorCode LinkLayerController::AuthenticationRequested( |
| 1220 | uint16_t handle) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1221 | if (!connections_.HasHandle(handle)) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1222 | LOG_INFO("Authentication Requested for unknown handle %04x", handle); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1223 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1226 | Address remote = connections_.GetAddress(handle); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1227 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1228 | ScheduleTask(milliseconds(5), [this, remote, handle]() { HandleAuthenticationRequest(remote, handle); }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1229 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1230 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | void LinkLayerController::HandleSetConnectionEncryption(const Address& peer, uint16_t handle, |
| 1234 | uint8_t encryption_enable) { |
| 1235 | // TODO: Block ACL traffic or at least guard against it |
| 1236 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1237 | if (connections_.IsEncrypted(handle) && encryption_enable) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1238 | auto packet = bluetooth::hci::EncryptionChangeBuilder::Create( |
| 1239 | bluetooth::hci::ErrorCode::SUCCESS, handle, |
| 1240 | static_cast<bluetooth::hci::EncryptionEnabled>(encryption_enable)); |
| 1241 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1242 | return; |
| 1243 | } |
| 1244 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1245 | uint16_t count = security_manager_.ReadKey(peer); |
| 1246 | if (count == 0) { |
| 1247 | LOG_ERROR("NO KEY HERE for %s", peer.ToString().c_str()); |
| 1248 | return; |
| 1249 | } |
| 1250 | auto array = security_manager_.GetKey(peer); |
| 1251 | std::vector<uint8_t> key_vec{array.begin(), array.end()}; |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1252 | auto packet = model::packets::EncryptConnectionBuilder::Create( |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1253 | properties_.GetAddress(), peer, key_vec); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1254 | SendLinkLayerPacket(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1257 | bluetooth::hci::ErrorCode LinkLayerController::SetConnectionEncryption( |
| 1258 | uint16_t handle, uint8_t encryption_enable) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1259 | if (!connections_.HasHandle(handle)) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1260 | LOG_INFO("Set Connection Encryption for unknown handle %04x", handle); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1261 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1262 | } |
| 1263 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1264 | if (connections_.IsEncrypted(handle) && !encryption_enable) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1265 | return bluetooth::hci::ErrorCode::ENCRYPTION_MODE_NOT_ACCEPTABLE; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1266 | } |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1267 | Address remote = connections_.GetAddress(handle); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1268 | |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1269 | if (security_manager_.ReadKey(remote) == 0) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1270 | return bluetooth::hci::ErrorCode::PIN_OR_KEY_MISSING; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1271 | } |
| 1272 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1273 | ScheduleTask(milliseconds(5), [this, remote, handle, encryption_enable]() { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1274 | HandleSetConnectionEncryption(remote, handle, encryption_enable); |
| 1275 | }); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1276 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1279 | bluetooth::hci::ErrorCode LinkLayerController::AcceptConnectionRequest( |
| 1280 | const Address& addr, bool try_role_switch) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1281 | if (!connections_.HasPendingConnection(addr)) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1282 | LOG_INFO("%s: No pending connection for %s", __func__, addr.ToString().c_str()); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1283 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1286 | LOG_INFO("%s: Accept in 200ms", __func__); |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1287 | ScheduleTask(milliseconds(200), [this, addr, try_role_switch]() { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1288 | LOG_INFO("%s: Accepted", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1289 | MakeSlaveConnection(addr, try_role_switch); |
| 1290 | }); |
| 1291 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1292 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | void LinkLayerController::MakeSlaveConnection(const Address& addr, bool try_role_switch) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1296 | LOG_INFO("%s sending page response to %s", __func__, addr.ToString().c_str()); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1297 | auto to_send = model::packets::PageResponseBuilder::Create( |
| 1298 | properties_.GetAddress(), addr, try_role_switch); |
| 1299 | SendLinkLayerPacket(std::move(to_send)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1300 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1301 | uint16_t handle = connections_.CreateConnection(addr); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1302 | if (handle == acl::kReservedHandle) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1303 | LOG_INFO("%s CreateConnection failed", __func__); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1304 | return; |
| 1305 | } |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1306 | LOG_INFO("%s CreateConnection returned handle 0x%x", __func__, handle); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1307 | auto packet = bluetooth::hci::ConnectionCompleteBuilder::Create( |
| 1308 | bluetooth::hci::ErrorCode::SUCCESS, handle, addr, |
| 1309 | bluetooth::hci::LinkType::ACL, bluetooth::hci::Enable::DISABLED); |
| 1310 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1313 | bluetooth::hci::ErrorCode LinkLayerController::RejectConnectionRequest( |
| 1314 | const Address& addr, uint8_t reason) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1315 | if (!connections_.HasPendingConnection(addr)) { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1316 | LOG_INFO("%s: No pending connection for %s", __func__, addr.ToString().c_str()); |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1317 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1320 | ScheduleTask(milliseconds(200), |
| 1321 | [this, addr, reason]() { RejectSlaveConnection(addr, reason); }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1322 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1323 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | void LinkLayerController::RejectSlaveConnection(const Address& addr, uint8_t reason) { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1327 | auto to_send = model::packets::PageRejectBuilder::Create( |
| 1328 | properties_.GetAddress(), addr, reason); |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1329 | LOG_INFO("%s sending page reject to %s (reason 0x%02hhx)", __func__, |
| 1330 | addr.ToString().c_str(), reason); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1331 | SendLinkLayerPacket(std::move(to_send)); |
Hansong Zhang | 4cb1cf5 | 2019-07-08 15:32:26 -0700 | [diff] [blame] | 1332 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1333 | auto packet = bluetooth::hci::ConnectionCompleteBuilder::Create( |
| 1334 | static_cast<bluetooth::hci::ErrorCode>(reason), 0xeff, addr, |
| 1335 | bluetooth::hci::LinkType::ACL, bluetooth::hci::Enable::DISABLED); |
| 1336 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1339 | bluetooth::hci::ErrorCode LinkLayerController::CreateConnection( |
| 1340 | const Address& addr, uint16_t, uint8_t, uint16_t, |
| 1341 | uint8_t allow_role_switch) { |
Myles Watson | e066df4 | 2019-11-13 14:39:18 -0800 | [diff] [blame] | 1342 | if (!connections_.CreatePendingConnection( |
| 1343 | addr, properties_.GetAuthenticationEnable() == 1)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1344 | return bluetooth::hci::ErrorCode::CONTROLLER_BUSY; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1345 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1346 | auto page = model::packets::PageBuilder::Create( |
| 1347 | properties_.GetAddress(), addr, properties_.GetClassOfDevice(), |
| 1348 | allow_role_switch); |
| 1349 | SendLinkLayerPacket(std::move(page)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1350 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1351 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1354 | bluetooth::hci::ErrorCode LinkLayerController::CreateConnectionCancel( |
| 1355 | const Address& addr) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1356 | if (!connections_.CancelPendingConnection(addr)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1357 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1358 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1359 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1362 | bluetooth::hci::ErrorCode LinkLayerController::Disconnect(uint16_t handle, |
| 1363 | uint8_t reason) { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1364 | // TODO: Handle LE |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1365 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1366 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1369 | const Address& remote = connections_.GetAddress(handle); |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1370 | auto packet = model::packets::DisconnectBuilder::Create( |
| 1371 | properties_.GetAddress(), remote, reason); |
| 1372 | SendLinkLayerPacket(std::move(packet)); |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1373 | ASSERT_LOG(connections_.Disconnect(handle), "Disconnecting %hx", handle); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1374 | |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1375 | ScheduleTask(milliseconds(20), [this, handle]() { |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1376 | DisconnectCleanup(handle, static_cast<uint8_t>(hci::Status::CONNECTION_TERMINATED_BY_LOCAL_HOST)); |
| 1377 | }); |
| 1378 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1379 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | void LinkLayerController::DisconnectCleanup(uint16_t handle, uint8_t reason) { |
| 1383 | // TODO: Clean up other connection state. |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1384 | auto packet = bluetooth::hci::DisconnectionCompleteBuilder::Create( |
| 1385 | bluetooth::hci::ErrorCode::SUCCESS, handle, |
| 1386 | static_cast<bluetooth::hci::ErrorCode>(reason)); |
| 1387 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1390 | bluetooth::hci::ErrorCode LinkLayerController::ChangeConnectionPacketType( |
| 1391 | uint16_t handle, uint16_t types) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1392 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1393 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1394 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1395 | auto packet = bluetooth::hci::ConnectionPacketTypeChangedBuilder::Create( |
| 1396 | bluetooth::hci::ErrorCode::SUCCESS, handle, types); |
| 1397 | std::shared_ptr<bluetooth::hci::ConnectionPacketTypeChangedBuilder> |
| 1398 | shared_packet = std::move(packet); |
| 1399 | ScheduleTask(milliseconds(20), [this, shared_packet]() { |
| 1400 | send_event_(std::move(shared_packet)); |
| 1401 | }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1402 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1403 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1406 | bluetooth::hci::ErrorCode LinkLayerController::ChangeConnectionLinkKey( |
| 1407 | uint16_t handle) { |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1408 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1409 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1413 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1414 | } |
| 1415 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1416 | bluetooth::hci::ErrorCode LinkLayerController::MasterLinkKey( |
| 1417 | uint8_t /* key_flag */) { |
Chienyuan | 9145e7a | 2019-10-02 15:18:55 +0800 | [diff] [blame] | 1418 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1419 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | 9145e7a | 2019-10-02 15:18:55 +0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1422 | bluetooth::hci::ErrorCode LinkLayerController::HoldMode( |
| 1423 | uint16_t handle, uint16_t hold_mode_max_interval, |
| 1424 | uint16_t hold_mode_min_interval) { |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1425 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1426 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | if (hold_mode_max_interval < hold_mode_min_interval) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1430 | return bluetooth::hci::ErrorCode::INVALID_HCI_COMMAND_PARAMETERS; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1434 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1435 | } |
| 1436 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1437 | bluetooth::hci::ErrorCode LinkLayerController::SniffMode( |
| 1438 | uint16_t handle, uint16_t sniff_max_interval, uint16_t sniff_min_interval, |
| 1439 | uint16_t sniff_attempt, uint16_t sniff_timeout) { |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1440 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1441 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1444 | if (sniff_max_interval < sniff_min_interval || sniff_attempt < 0x0001 || sniff_attempt > 0x7FFF || |
| 1445 | sniff_timeout > 0x7FFF) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1446 | return bluetooth::hci::ErrorCode::INVALID_HCI_COMMAND_PARAMETERS; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1450 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1451 | } |
| 1452 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1453 | bluetooth::hci::ErrorCode LinkLayerController::ExitSniffMode(uint16_t handle) { |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1454 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1455 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1459 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1462 | bluetooth::hci::ErrorCode LinkLayerController::QosSetup( |
| 1463 | uint16_t handle, uint8_t service_type, uint32_t /* token_rate */, |
| 1464 | uint32_t /* peak_bandwidth */, uint32_t /* latency */, |
| 1465 | uint32_t /* delay_variation */) { |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1466 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1467 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | if (service_type > 0x02) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1471 | return bluetooth::hci::ErrorCode::INVALID_HCI_COMMAND_PARAMETERS; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1475 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1476 | } |
| 1477 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1478 | bluetooth::hci::ErrorCode LinkLayerController::SwitchRole(Address /* bd_addr */, |
| 1479 | uint8_t /* role */) { |
Chienyuan | 9145e7a | 2019-10-02 15:18:55 +0800 | [diff] [blame] | 1480 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1481 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | 9145e7a | 2019-10-02 15:18:55 +0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1484 | bluetooth::hci::ErrorCode LinkLayerController::WriteLinkPolicySettings( |
| 1485 | uint16_t handle, uint16_t) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1486 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1487 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1488 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1489 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1492 | bluetooth::hci::ErrorCode LinkLayerController::FlowSpecification( |
| 1493 | uint16_t handle, uint8_t flow_direction, uint8_t service_type, |
| 1494 | uint32_t /* token_rate */, uint32_t /* token_bucket_size */, |
| 1495 | uint32_t /* peak_bandwidth */, uint32_t /* access_latency */) { |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1496 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1497 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | if (flow_direction > 0x01 || service_type > 0x02) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1501 | return bluetooth::hci::ErrorCode::INVALID_HCI_COMMAND_PARAMETERS; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | // TODO: implement real logic |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1505 | return bluetooth::hci::ErrorCode::COMMAND_DISALLOWED; |
Chienyuan | ad340b0 | 2019-09-25 19:23:21 +0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1508 | bluetooth::hci::ErrorCode LinkLayerController::WriteLinkSupervisionTimeout( |
| 1509 | uint16_t handle, uint16_t) { |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1510 | if (!connections_.HasHandle(handle)) { |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1511 | return bluetooth::hci::ErrorCode::UNKNOWN_CONNECTION; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1512 | } |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1513 | return bluetooth::hci::ErrorCode::SUCCESS; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | void LinkLayerController::LeWhiteListClear() { |
| 1517 | le_white_list_.clear(); |
| 1518 | } |
| 1519 | |
Calvin Huang | ee1980c | 2019-11-01 16:35:55 -0700 | [diff] [blame] | 1520 | void LinkLayerController::LeResolvingListClear() { le_resolving_list_.clear(); } |
| 1521 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1522 | void LinkLayerController::LeWhiteListAddDevice(Address addr, uint8_t addr_type) { |
| 1523 | std::tuple<Address, uint8_t> new_tuple = std::make_tuple(addr, addr_type); |
| 1524 | for (auto dev : le_white_list_) { |
| 1525 | if (dev == new_tuple) { |
| 1526 | return; |
| 1527 | } |
| 1528 | } |
| 1529 | le_white_list_.emplace_back(new_tuple); |
| 1530 | } |
| 1531 | |
Calvin Huang | ee1980c | 2019-11-01 16:35:55 -0700 | [diff] [blame] | 1532 | void LinkLayerController::LeResolvingListAddDevice( |
| 1533 | Address addr, uint8_t addr_type, std::array<uint8_t, kIrk_size> peerIrk, |
| 1534 | std::array<uint8_t, kIrk_size> localIrk) { |
| 1535 | std::tuple<Address, uint8_t, std::array<uint8_t, kIrk_size>, |
| 1536 | std::array<uint8_t, kIrk_size>> |
| 1537 | new_tuple = std::make_tuple(addr, addr_type, peerIrk, localIrk); |
| 1538 | for (size_t i = 0; i < le_white_list_.size(); i++) { |
| 1539 | auto curr = le_white_list_[i]; |
| 1540 | if (std::get<0>(curr) == addr && std::get<1>(curr) == addr_type) { |
| 1541 | le_resolving_list_[i] = new_tuple; |
| 1542 | return; |
| 1543 | } |
| 1544 | } |
| 1545 | le_resolving_list_.emplace_back(new_tuple); |
| 1546 | } |
| 1547 | |
| 1548 | void LinkLayerController::LeSetPrivacyMode(uint8_t address_type, Address addr, |
| 1549 | uint8_t mode) { |
| 1550 | // set mode for addr |
| 1551 | LOG_INFO("address type = %d ", address_type); |
| 1552 | LOG_INFO("address = %s ", addr.ToString().c_str()); |
| 1553 | LOG_INFO("mode = %d ", mode); |
| 1554 | } |
| 1555 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1556 | void LinkLayerController::LeWhiteListRemoveDevice(Address addr, uint8_t addr_type) { |
| 1557 | // TODO: Add checks to see if advertising, scanning, or a connection request |
| 1558 | // with the white list is ongoing. |
| 1559 | std::tuple<Address, uint8_t> erase_tuple = std::make_tuple(addr, addr_type); |
| 1560 | for (size_t i = 0; i < le_white_list_.size(); i++) { |
| 1561 | if (le_white_list_[i] == erase_tuple) { |
| 1562 | le_white_list_.erase(le_white_list_.begin() + i); |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
Calvin Huang | ee1980c | 2019-11-01 16:35:55 -0700 | [diff] [blame] | 1567 | void LinkLayerController::LeResolvingListRemoveDevice(Address addr, |
| 1568 | uint8_t addr_type) { |
| 1569 | // TODO: Add checks to see if advertising, scanning, or a connection request |
| 1570 | // with the white list is ongoing. |
| 1571 | for (size_t i = 0; i < le_white_list_.size(); i++) { |
| 1572 | auto curr = le_white_list_[i]; |
| 1573 | if (std::get<0>(curr) == addr && std::get<1>(curr) == addr_type) { |
| 1574 | le_resolving_list_.erase(le_resolving_list_.begin() + i); |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1579 | bool LinkLayerController::LeWhiteListContainsDevice(Address addr, uint8_t addr_type) { |
| 1580 | std::tuple<Address, uint8_t> sought_tuple = std::make_tuple(addr, addr_type); |
| 1581 | for (size_t i = 0; i < le_white_list_.size(); i++) { |
| 1582 | if (le_white_list_[i] == sought_tuple) { |
| 1583 | return true; |
| 1584 | } |
| 1585 | } |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
Calvin Huang | ee1980c | 2019-11-01 16:35:55 -0700 | [diff] [blame] | 1589 | bool LinkLayerController::LeResolvingListContainsDevice(Address addr, |
| 1590 | uint8_t addr_type) { |
| 1591 | for (size_t i = 0; i < le_white_list_.size(); i++) { |
| 1592 | auto curr = le_white_list_[i]; |
| 1593 | if (std::get<0>(curr) == addr && std::get<1>(curr) == addr_type) { |
| 1594 | return true; |
| 1595 | } |
| 1596 | } |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1600 | bool LinkLayerController::LeWhiteListFull() { |
| 1601 | return le_white_list_.size() >= properties_.GetLeWhiteListSize(); |
| 1602 | } |
| 1603 | |
Calvin Huang | ee1980c | 2019-11-01 16:35:55 -0700 | [diff] [blame] | 1604 | bool LinkLayerController::LeResolvingListFull() { |
| 1605 | return le_resolving_list_.size() >= properties_.GetLeResolvingListSize(); |
| 1606 | } |
| 1607 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1608 | void LinkLayerController::Reset() { |
| 1609 | inquiry_state_ = Inquiry::InquiryState::STANDBY; |
| 1610 | last_inquiry_ = steady_clock::now(); |
| 1611 | le_scan_enable_ = 0; |
Myles Watson | 9672353 | 2019-09-02 14:52:46 -0700 | [diff] [blame] | 1612 | le_advertising_enable_ = 0; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1613 | le_connect_ = 0; |
| 1614 | } |
| 1615 | |
| 1616 | void LinkLayerController::PageScan() {} |
| 1617 | |
| 1618 | void LinkLayerController::StartInquiry(milliseconds timeout) { |
Myles Watson | 8fe710e | 2019-09-05 09:01:00 -0700 | [diff] [blame] | 1619 | ScheduleTask(milliseconds(timeout), [this]() { LinkLayerController::InquiryTimeout(); }); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1620 | inquiry_state_ = Inquiry::InquiryState::INQUIRY; |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | void LinkLayerController::InquiryCancel() { |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 1624 | ASSERT(inquiry_state_ == Inquiry::InquiryState::INQUIRY); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1625 | inquiry_state_ = Inquiry::InquiryState::STANDBY; |
| 1626 | } |
| 1627 | |
| 1628 | void LinkLayerController::InquiryTimeout() { |
| 1629 | if (inquiry_state_ == Inquiry::InquiryState::INQUIRY) { |
| 1630 | inquiry_state_ = Inquiry::InquiryState::STANDBY; |
Chienyuan | 3d8a803 | 2019-11-01 18:04:07 +0800 | [diff] [blame] | 1631 | auto packet = bluetooth::hci::InquiryCompleteBuilder::Create( |
| 1632 | bluetooth::hci::ErrorCode::SUCCESS); |
| 1633 | send_event_(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | void LinkLayerController::SetInquiryMode(uint8_t mode) { |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1638 | inquiry_mode_ = static_cast<model::packets::InquiryType>(mode); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | void LinkLayerController::SetInquiryLAP(uint64_t lap) { |
| 1642 | inquiry_lap_ = lap; |
| 1643 | } |
| 1644 | |
| 1645 | void LinkLayerController::SetInquiryMaxResponses(uint8_t max) { |
| 1646 | inquiry_max_responses_ = max; |
| 1647 | } |
| 1648 | |
| 1649 | void LinkLayerController::Inquiry() { |
| 1650 | steady_clock::time_point now = steady_clock::now(); |
| 1651 | if (duration_cast<milliseconds>(now - last_inquiry_) < milliseconds(2000)) { |
| 1652 | return; |
| 1653 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 1654 | |
| 1655 | auto packet = model::packets::InquiryBuilder::Create( |
| 1656 | properties_.GetAddress(), Address::kEmpty, inquiry_mode_); |
| 1657 | SendLinkLayerPacket(std::move(packet)); |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1658 | last_inquiry_ = now; |
| 1659 | } |
| 1660 | |
| 1661 | void LinkLayerController::SetInquiryScanEnable(bool enable) { |
| 1662 | inquiry_scans_enabled_ = enable; |
| 1663 | } |
| 1664 | |
| 1665 | void LinkLayerController::SetPageScanEnable(bool enable) { |
| 1666 | page_scans_enabled_ = enable; |
| 1667 | } |
| 1668 | |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 1669 | } // namespace test_vendor_lib |