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