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..e8913eedead85618819e469f09d56c0414631f69 |
| --- /dev/null |
| +++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.cc |
| @@ -0,0 +1,275 @@ |
| +// 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 "base/logging.h" |
| + |
| +using proximity_auth::BluetoothLowEnergyWeavePacketGenerator; |
| + |
| +namespace { |
| +typedef BluetoothLowEnergyWeavePacketGenerator::PacketType PacketType; |
| +typedef BluetoothLowEnergyWeavePacketGenerator::ControlCommand ControlCommand; |
| +typedef BluetoothLowEnergyWeavePacketGenerator::ReasonForClose ReasonForClose; |
| + |
| +const uint8_t kMaxPacketCounter = 8; |
| +} // namespace |
| + |
| +namespace proximity_auth { |
| + |
| +BluetoothLowEnergyWeavePacketReceiver::Factory* |
| + BluetoothLowEnergyWeavePacketReceiver::Factory::factory_instance_ = nullptr; |
| + |
| +std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> |
| +BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance() { |
| + if (factory_instance_ == nullptr) { |
| + factory_instance_ = new Factory(); |
| + } |
| + return std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver>( |
| + factory_instance_->BuildInstance()); |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( |
| + Factory* factory) { |
| + factory_instance_ = factory; |
| +} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver* |
| +BluetoothLowEnergyWeavePacketReceiver::Factory::BuildInstance() { |
| + return new BluetoothLowEnergyWeavePacketReceiver(); |
| +} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver::BluetoothLowEnergyWeavePacketReceiver() |
| + : packet_size_(0), |
| + packet_number_(0), |
| + state_(State::CONNECTING), |
| + reason_for_close_(ReasonForClose::CLOSE_WITHOUT_ERROR) {} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver:: |
| + ~BluetoothLowEnergyWeavePacketReceiver() {} |
| + |
| +BluetoothLowEnergyWeavePacketReceiver::State |
| +BluetoothLowEnergyWeavePacketReceiver::GetState() { |
| + return state_; |
| +} |
| + |
| +uint32_t BluetoothLowEnergyWeavePacketReceiver::GetPacketSize() { |
| + // packet_size_ is well defined in every state. |
| + return packet_size_; |
| +} |
| + |
| +ReasonForClose BluetoothLowEnergyWeavePacketReceiver::GetReasonForClose() { |
| + DCHECK(state_ == State::CONNECTION_CLOSED); |
| + return reason_for_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()) { |
| + DLOG(ERROR) << "Empty packet is not a valid uWeave packet."; |
| + state_ = State::ERROR; |
| + } else { |
| + VerifyPacketCounter(packet); |
| + |
| + switch (state_) { |
| + case State::CONNECTING: |
| + ReceiveControlPacket(packet); |
| + break; |
| + case State::RECEIVING_DATA: |
| + ReceiveDataPacket(packet); |
| + break; |
| + case State::DATA_READY: |
| + data_message_.clear(); |
| + ReceiveDataPacket(packet); |
| + break; |
| + default: |
| + // Receiving an message in connection close or error state is not valid. |
| + DLOG(ERROR) << "Receiving message in " << state_ |
| + << " state is not valid."; |
| + state_ = State::ERROR; |
| + break; |
| + } |
| + } |
| + return state_; |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveControlPacket( |
|
Tim Song
2016/06/11 00:39:46
This should probably be called ReceiveConnectionPa
jingxuy
2016/06/16 08:42:28
Done.
|
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(state_ == State::CONNECTING); |
| + |
| + if (GetPacketType(packet) == PacketType::CONTROL) { |
| + uint8_t command = GetControlCommand(packet); |
| + switch (command) { |
| + case ControlCommand::CONNECTION_REQUEST: |
| + ReceiveConnectionRequest(packet); |
| + break; |
| + case ControlCommand::CONNECTION_RESPONSE: |
| + ReceiveConnectionResponse(packet); |
| + break; |
| + case ControlCommand::CONNECTION_CLOSE: |
| + ReceiveConnectionClose(packet); |
| + break; |
| + default: |
| + DLOG(ERROR) << "Unrecognized control packet command " << command; |
| + state_ = State::ERROR; |
| + break; |
| + } |
| + } else { |
| + DLOG(ERROR) << "Can't receive data packets when not connected."; |
| + state_ = State::ERROR; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveDataPacket( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(state_ == State::RECEIVING_DATA); |
|
Tim Song
2016/06/11 00:39:46
Also check if state_ == State::DATA_READY
jingxuy
2016/06/16 08:42:28
Done.
|
| + |
| + switch (GetPacketType(packet)) { |
| + case PacketType::CONTROL: |
| + if (GetControlCommand(packet) == ControlCommand::CONNECTION_CLOSE) { |
| + ReceiveConnectionClose(packet); |
| + } else { |
| + DLOG(ERROR) << "Can't receive request/response during data " |
| + << "transaction."; |
| + state_ = State::ERROR; |
| + } |
| + break; |
| + case PacketType::DATA: |
| + // Either it's a first packet and data_message_ is empty, |
| + // or it's not a first packet and data_message_ is not empty. |
| + if (IsFirstDataPacket(packet) ^ !data_message_.empty()) { |
| + AppendData(packet); |
| + if (IsLastDataPacket(packet)) { |
| + state_ = State::DATA_READY; |
| + } |
| + } else { |
| + DLOG(ERROR) << "First bit of data packet is not set correctly."; |
| + state_ = State::ERROR; |
| + } |
| + break; |
| + default: |
| + DLOG(ERROR) << "Invalid packet type."; |
| + state_ = State::ERROR; |
| + break; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionRequest( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(state_ == State::CONNECTING); |
| + |
| + uint16_t min_version = GetShortField(packet, 1); |
| + uint16_t max_version = GetShortField(packet, 3); |
|
Tim Song
2016/06/11 00:39:46
You should validate that the packet is sufficientl
sacomoto
2016/06/13 17:16:40
+1.
And you should move the error state if that'
jingxuy
2016/06/16 08:42:28
Done.
|
| + if (!(min_version == 1 && max_version == 1)) { |
|
sacomoto
2016/06/13 17:16:40
This is not correct.
As long as, min_version <=
jingxuy
2016/06/16 08:42:28
Done.
|
| + state_ = State::ERROR; |
| + } else { |
| + packet_size_ = GetShortField(packet, 5); |
| + state_ = State::RECEIVING_DATA; |
| + // TODO(jingxuy): what do I do with the 13 bytes of data? |
|
sacomoto
2016/06/13 17:16:40
If there is any data in the connection request pay
jingxuy
2016/06/16 08:42:28
Done.
|
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionResponse( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + DCHECK(state_ == State::CONNECTING); |
| + |
| + uint16_t selected_version = GetShortField(packet, 1); |
| + if (selected_version != 1) { |
| + state_ = State::ERROR; |
| + } else { |
| + packet_size_ = GetShortField(packet, 3); |
|
Tim Song
2016/06/11 00:39:46
You should validate that the packet is sufficientl
sacomoto
2016/06/13 17:16:40
+1.
And you should move the error state if that'
jingxuy
2016/06/16 08:42:28
Done.
|
| + state_ = State::RECEIVING_DATA; |
| + // TODO(jingxuy): what do I do with the 15 bytes of data? |
|
sacomoto
2016/06/13 17:16:40
If there is any data in the connection response pa
jingxuy
2016/06/16 08:42:28
Done.
|
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionClose( |
| + const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + |
| + uint16_t reason = GetShortField(packet, 1); |
|
sacomoto
2016/06/13 17:16:40
Check if the packet is large enough.
jingxuy
2016/06/16 08:42:28
Done.
|
| + |
| + 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>(GetShortField(packet, 1)); |
|
sacomoto
2016/06/13 17:16:40
Check if the packet is large enough.
But you sho
jingxuy
2016/06/16 08:42:28
I don't really understand the comment. Should I no
sacomoto
2016/06/17 15:31:07
I was referring to the current iOS and Android imp
jingxuy
2016/06/17 18:59:51
Done.
|
| + state_ = State::CONNECTION_CLOSED; |
| + break; |
| + default: |
| + DLOG(ERROR) << "Invalid reason for close " << reason << "."; |
| + state_ = State::ERROR; |
| + break; |
| + } |
| +} |
| + |
| +void BluetoothLowEnergyWeavePacketReceiver::AppendData(const Packet& packet) { |
| + DCHECK(!packet.empty()); |
| + |
| + // Append to data_message_ bytes 1 through end of the packet. |
| + data_message_.insert(data_message_.end(), packet.begin() + 1, packet.end()); |
| +} |
| + |
| +uint16_t BluetoothLowEnergyWeavePacketReceiver::GetShortField( |
| + const Packet& packet, |
| + uint32_t index) { |
| + DCHECK_LT(index, packet.size()); |
| + DCHECK_LT(index + 1, packet.size()); |
| + |
| + // packet[index + 1] is the upper byte and packet[index] is the lower byte. |
| + return (packet[index + 1] << 8) | packet[index]; |
| +} |
| + |
| +uint8_t BluetoothLowEnergyWeavePacketReceiver::GetPacketType( |
| + const Packet& packet) { |
| + // Packet type is stored in the highest bit of the first byte. |
| + return (packet[0] >> 7) & 1; |
| +} |
| + |
| +uint8_t BluetoothLowEnergyWeavePacketReceiver::GetControlCommand( |
| + const Packet& packet) { |
| + // 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; |
| + |
| + // 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 { |
| + DLOG(ERROR) << "Invalid packet counter " << count << "."; |
| + state_ = State::ERROR; |
| + } |
| +} |
| + |
| +bool BluetoothLowEnergyWeavePacketReceiver::IsFirstDataPacket( |
| + const Packet& packet) { |
| + // Bit 3 determines whether the packet is the first packet of the message. |
| + return (packet[0] >> 3) & 1; |
| +} |
| + |
| +bool BluetoothLowEnergyWeavePacketReceiver::IsLastDataPacket( |
| + const Packet& packet) { |
| + // Bit 2 determines whether the packet is the last packet of the message. |
| + return (packet[0] >> 2) & 1; |
| +} |
| + |
| +} // namespace proximity_auth |