Chromium Code Reviews| Index: components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.cc |
| diff --git a/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.cc b/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7d188b31d9ea06e5f204dfbcc6945d431d0ccd9d |
| --- /dev/null |
| +++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.cc |
| @@ -0,0 +1,406 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.h" |
| + |
| +#include "components/proximity_auth/logging.h" |
| + |
| +using proximity_auth::BluetoothLowEnergyWeavePacketGenerator; |
| + |
| +namespace { |
| +typedef BluetoothLowEnergyWeavePacketGenerator::PacketType PacketType; |
| +typedef BluetoothLowEnergyWeavePacketGenerator::ControlCommand ControlCommand; |
| +typedef BluetoothLowEnergyWeavePacketGenerator::ReasonForClose ReasonForClose; |
| + |
| +const uint8_t kMaxPacketCounter = 8; |
| +const uint16_t kMaxControlPacketSize = 20; |
| +const uint16_t kMinPacketSize = 20; |
| +const uint16_t kMinConnectionRequestSize = 7; |
| +const uint16_t kMinConnectionResponseSize = 5; |
| +const uint16_t kMinConnectionCloseSize = 3; |
| +const uint16_t kSupportedWeaveVersion = 1; |
| + |
| +} // namespace |
| + |
| +namespace proximity_auth { |
| + |
| +BluetoothLowEnergyWeavePacketReceiver::Factory* |
| + BluetoothLowEnergyWeavePacketReceiver::Factory::factory_instance_ = nullptr; |
| + |
| +std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> |
| +BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance( |
| + ReceiverType receiver_type) { |
| + if (factory_instance_ == nullptr) { |
| + factory_instance_ = new Factory(); |
| + } |
| + return std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver>( |
| + factory_instance_->BuildInstance(receiver_type)); |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( |
| + Factory* factory) { |
| + factory_instance_ = factory; |
| +} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver* |
| +BluetoothLowEnergyWeavePacketReceiver::Factory::BuildInstance( |
| + ReceiverType receiver_type) { |
| + return new BluetoothLowEnergyWeavePacketReceiver(receiver_type); |
| +} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver::BluetoothLowEnergyWeavePacketReceiver( |
| + ReceiverType receiver_type) |
| + : receiver_type_(receiver_type), |
| + packet_number_(0), |
| + state_(State::CONNECTING), |
| + reason_for_close_(ReasonForClose::CLOSE_WITHOUT_ERROR), |
| + reason_to_close_(ReasonForClose::CLOSE_WITHOUT_ERROR) { |
| + SetMaxPacketSize(kMinPacketSize); |
| +} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver:: |
| + ~BluetoothLowEnergyWeavePacketReceiver() {} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver::State |
| +BluetoothLowEnergyWeavePacketReceiver::GetState() { |
| + return state_; |
| +} |
| + |
| +uint16_t BluetoothLowEnergyWeavePacketReceiver::GetMaxPacketSize() { |
| + // max_packet_size_ is well defined in every state. |
| + return max_packet_size_; |
| +} |
| + |
| +ReasonForClose BluetoothLowEnergyWeavePacketReceiver::GetReasonForClose() { |
| + DCHECK(state_ == State::CONNECTION_CLOSED); |
| + return reason_for_close_; |
| +} |
| + |
| +ReasonForClose BluetoothLowEnergyWeavePacketReceiver::GetReasonToClose() { |
| + DCHECK(state_ == State::ERROR); |
| + return reason_to_close_; |
| +} |
| + |
| +std::string BluetoothLowEnergyWeavePacketReceiver::GetDataMessage() { |
| + DCHECK(state_ == State::DATA_READY); |
| + return std::string(data_message_.begin(), data_message_.end()); |
| +} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver::State |
| +BluetoothLowEnergyWeavePacketReceiver::ReceivePacket(const Packet& packet) { |
| + if (packet.empty()) { |
| + Error("Empty packet is not a valid uWeave packet.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + } else if (packet.size() > GetConceptualMaxPacketSize()) { |
| + Error("Can't receive packet greater than maximum packet size", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + } else { |
| + VerifyPacketCounter(packet); |
| + |
| + switch (state_) { |
| + case State::CONNECTING: |
| + ReceiveFirstPacket(packet); |
| + break; |
| + case State::WAITING: |
| + ReceiveNonFirstPacket(packet, true); |
| + break; |
| + case State::RECEIVING_DATA: |
| + ReceiveNonFirstPacket(packet, false); |
| + break; |
| + case State::DATA_READY: |
| + data_message_.clear(); |
| + ReceiveNonFirstPacket(packet, true); |
| + break; |
| + case State::CONNECTION_CLOSED: |
| + Error("Receiving message in ConnectionClosed state.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + break; |
| + case State::ERROR: |
| + PA_LOG(ERROR) << "Receiving message in ERROR state."; |
| + break; |
| + default: |
| + // Receiving an message in connection close or error state is not valid. |
| + Error("Receiving message in invalid state.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + break; |
| + } |
| + } |
| + return state_; |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveFirstPacket( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(state_ == State::CONNECTING); |
| + |
| + if (GetPacketType(packet) != PacketType::CONTROL) { |
| + Error("Can't receive data packets when not connected.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + return; |
| + } |
| + |
| + uint8_t command = GetControlCommand(packet); |
| + switch (command) { |
| + case ControlCommand::CONNECTION_REQUEST: |
| + if (receiver_type_ == ReceiverType::SERVER) { |
| + ReceiveConnectionRequest(packet); |
| + } else { |
| + Error("Server couldn't process connection response.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + } |
| + break; |
| + case ControlCommand::CONNECTION_RESPONSE: |
| + if (receiver_type_ == ReceiverType::CLIENT) { |
| + ReceiveConnectionResponse(packet); |
| + } else { |
| + Error("Client couldn't process connection request.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + } |
| + break; |
| + case ControlCommand::CONNECTION_CLOSE: |
| + Error("Shouldn't receive close without a connection.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + break; |
| + default: |
| + Error("Unrecognized control packet command.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + break; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveNonFirstPacket( |
| + const Packet& packet, |
| + bool expect_first_packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(((state_ == State::WAITING || state_ == State::DATA_READY) && |
| + expect_first_packet) || |
| + (state_ == State::RECEIVING_DATA && !expect_first_packet)); |
| + |
| + switch (GetPacketType(packet)) { |
| + case PacketType::CONTROL: |
| + if (GetControlCommand(packet) == ControlCommand::CONNECTION_CLOSE) { |
| + ReceiveConnectionClose(packet); |
| + } else { |
| + Error("Can only receive connection close during data transaction", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + } |
| + break; |
| + case PacketType::DATA: |
| + if (!IsLowerTwoBitsCleared(packet)) { |
| + Error("Lower two bits of data packet header are not clear.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + } else if (expect_first_packet ^ IsFirstDataPacket(packet)) { |
| + // This means that expectation of whether a packet would be a |
| + // first packet and what we actually got are different. |
| + Error("First bit of data packet is not set correctly.", |
| + ReasonForClose::RECEIVED_PACKET_OUT_OF_SEQUENCE); |
| + } else { |
| + AppendData(packet, 1); |
| + if (IsLastDataPacket(packet)) { |
| + state_ = State::DATA_READY; |
| + } else { |
| + state_ = State::RECEIVING_DATA; |
| + } |
| + } |
| + break; |
| + default: |
| + Error("Invalid packet type.", ReasonForClose::UNKNOWN_ERROR); |
| + break; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionRequest( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(state_ == State::CONNECTING); |
| + |
| + if (packet.size() < kMinConnectionRequestSize || |
| + packet.size() > kMaxControlPacketSize) { |
| + Error("Invalid connection request packet size.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + return; |
| + } |
| + |
| + uint16_t packet_size = GetShortField(packet, 5); |
| + // Packet size of 0 means the server can observe the ATT_MTU and selecte an |
| + // appropriate packet size; |
| + if (packet_size > 0 && packet_size < kMinPacketSize) { |
| + Error("Client must support at least 20 bytes per packet.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + return; |
| + } |
| + SetMaxPacketSize(packet_size); |
| + |
| + uint16_t min_version = GetShortField(packet, 1); |
| + uint16_t max_version = GetShortField(packet, 3); |
| + if (kSupportedWeaveVersion < min_version || |
| + kSupportedWeaveVersion > max_version) { |
| + Error("Server does not support client version range.", |
| + ReasonForClose::NO_COMMON_VERSION_SUPPORTED); |
| + return; |
| + } |
| + |
| + if (packet.size() > kMinConnectionRequestSize) { |
| + AppendData(packet, kMinConnectionRequestSize); |
| + state_ = State::DATA_READY; |
| + } else { |
| + state_ = State::WAITING; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionResponse( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(state_ == State::CONNECTING); |
| + |
| + if (packet.size() < kMinConnectionResponseSize || |
| + packet.size() > kMaxControlPacketSize) { |
| + Error("Invalid connection response packet size.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + return; |
| + } |
| + |
| + uint16_t selected_packet_size = GetShortField(packet, 3); |
| + if (selected_packet_size < kMinPacketSize) { |
| + Error("Server must support at least 20 bytes per packet.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + return; |
| + } |
| + SetMaxPacketSize(selected_packet_size); |
| + |
| + uint16_t selected_version = GetShortField(packet, 1); |
| + if (selected_version != kSupportedWeaveVersion) { |
| + Error("Client does not support server selected version.", |
| + ReasonForClose::NO_COMMON_VERSION_SUPPORTED); |
| + return; |
| + } |
| + |
| + if (packet.size() > kMinConnectionResponseSize) { |
| + AppendData(packet, kMinConnectionResponseSize); |
| + state_ = State::DATA_READY; |
| + } else { |
| + state_ = State::WAITING; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionClose( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + |
| + if (packet.size() < kMinConnectionCloseSize || |
|
sacomoto
2016/06/17 15:31:07
The current iOS and Android implementations do not
jingxuy
2016/06/17 18:59:51
Done.
|
| + packet.size() > kMaxControlPacketSize) { |
| + Error("Invalid connection close packet size.", |
| + ReasonForClose::UNKNOWN_ERROR); |
| + return; |
| + } |
| + |
| + uint16_t reason = GetShortField(packet, 1); |
| + |
| + switch (reason) { |
| + case ReasonForClose::CLOSE_WITHOUT_ERROR: |
| + case ReasonForClose::UNKNOWN_ERROR: |
| + case ReasonForClose::NO_COMMON_VERSION_SUPPORTED: |
| + case ReasonForClose::RECEIVED_PACKET_OUT_OF_SEQUENCE: |
| + case ReasonForClose::APPLICATION_ERROR: |
| + reason_for_close_ = static_cast<ReasonForClose>(reason); |
| + state_ = State::CONNECTION_CLOSED; |
| + break; |
| + default: |
| + Error("Invalid reason for close.", ReasonForClose::UNKNOWN_ERROR); |
| + break; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::AppendData(const Packet& packet, |
| + uint32_t byte_offset) { |
| + DCHECK(!packet.empty()); |
| + |
| + // Append to data_message_ bytes 1 through end of the packet. |
| + data_message_.insert(data_message_.end(), packet.begin() + byte_offset, |
| + packet.end()); |
| +} |
| + |
| +uint16_t BluetoothLowEnergyWeavePacketReceiver::GetShortField( |
| + const Packet& packet, |
| + uint32_t byte_offset) { |
| + DCHECK_LT(byte_offset, packet.size()); |
| + DCHECK_LT(byte_offset + 1, packet.size()); |
| + |
| + // packet[byte_offset + 1] is the upper byte and packet[byte_offset] is the |
| + // lower byte. |
| + return (packet[byte_offset + 1] << 8) | packet[byte_offset]; |
| +} |
| + |
| +uint8_t BluetoothLowEnergyWeavePacketReceiver::GetPacketType( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + // Packet type is stored in the highest bit of the first byte. |
| + return (packet[0] >> 7) & 1; |
| +} |
| + |
| +uint8_t BluetoothLowEnergyWeavePacketReceiver::GetControlCommand( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + // Control command is stored in the lower 4 bits of the first byte. |
| + return packet[0] & 0x0F; |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::VerifyPacketCounter( |
| + const Packet& packet) { |
| + if (state_ == State::ERROR) |
| + return; |
| + |
| + DCHECK(!packet.empty()); |
| + // Packet counter is bits 4, 5, and 6 of the first byte. |
| + uint8_t count = (packet[0] >> 4) & 7; |
| + |
| + if (count == (packet_number_ % kMaxPacketCounter)) { |
| + packet_number_++; |
| + } else { |
| + Error("Invalid packet counter.", |
| + ReasonForClose::RECEIVED_PACKET_OUT_OF_SEQUENCE); |
| + } |
| +} |
| + |
| +bool BluetoothLowEnergyWeavePacketReceiver::IsFirstDataPacket( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + // Bit 3 determines whether the packet is the first packet of the message. |
| + return (packet[0] >> 3) & 1; |
| +} |
| + |
| +bool BluetoothLowEnergyWeavePacketReceiver::IsLastDataPacket( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + // Bit 2 determines whether the packet is the last packet of the message. |
| + return (packet[0] >> 2) & 1; |
| +} |
| + |
| +bool BluetoothLowEnergyWeavePacketReceiver::IsLowerTwoBitsCleared( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + return (packet[0] & 3) == 0; |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::Error( |
| + std::string error_message, |
| + ReasonForClose reason_to_close) { |
| + PA_LOG(ERROR) << error_message; |
| + state_ = State::ERROR; |
| + reason_to_close_ = reason_to_close; |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::SetMaxPacketSize( |
| + uint16_t packet_size) { |
| + DCHECK(packet_size == 0 || packet_size >= kMinPacketSize); |
| + max_packet_size_ = packet_size; |
| +} |
| + |
| +uint16_t BluetoothLowEnergyWeavePacketReceiver::GetConceptualMaxPacketSize() { |
| + if (!max_packet_size_) |
| + return 20; |
| + return max_packet_size_; |
| +} |
| + |
| +} // namespace proximity_auth |