Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(591)

Unified Diff: components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.cc

Issue 2053013002: Weave Packet Receiver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proximity_auth_weave_migration
Patch Set: fixed typo in comment Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..c07364db40ca063fa2fd03596dfb86339da01f11
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.cc
@@ -0,0 +1,252 @@
+// 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 proximity_auth {
+namespace {
Kyle Horimoto 2016/06/09 23:59:46 The anonymous namespace should be above the proxim
jingxuy 2016/06/10 23:11:12 Done.
+typedef BluetoothLowEnergyWeavePacketGenerator::PacketType PacketType;
+typedef BluetoothLowEnergyWeavePacketGenerator::ControlCommand ControlCommand;
+typedef BluetoothLowEnergyWeavePacketGenerator::ReasonForClose ReasonForClose;
+
+const uint8_t kMaxPacketCounter = 8;
+} // namespace
+
+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::EXCHANGING_CONTROLS),
+ reason_for_close_(ReasonForClose::CLOSE_WITHOUT_ERROR) {}
+
+BluetoothLowEnergyWeavePacketReceiver::
+ ~BluetoothLowEnergyWeavePacketReceiver() {}
+
+void BluetoothLowEnergyWeavePacketReceiver::ResetReceiver() {
+ packet_size_ = 0;
+ packet_number_ = 0;
+ state_ = State::EXCHANGING_CONTROLS;
+ reason_for_close_ = ReasonForClose::CLOSE_WITHOUT_ERROR;
+}
+
+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) {
Kyle Horimoto 2016/06/09 23:59:46 General comment: You don't preserve messages if th
jingxuy 2016/06/10 23:11:12 Acknowledged.
+ VerifyPacketCounter(packet);
+
+ switch (state_) {
+ case State::EXCHANGING_CONTROLS:
+ ReceiveWithEmptyData(packet);
+ break;
+ case State::RECEIVING_DATA:
+ ReceiveWithPartialData(packet);
+ break;
+ case State::DATA_READY:
+ data_message_.clear();
+ ReceiveWithEmptyData(packet);
+ break;
+ default:
+ // Receiving an message in connection close or error state is not valid
+ state_ = State::ERROR;
+ break;
+ }
+ return state_;
+}
+
+void BluetoothLowEnergyWeavePacketReceiver::ReceiveWithEmptyData(
+ const Packet& packet) {
+ // TODO(jingxuy): add dcheck stuff
+ PacketType type = GetPacketType(packet);
+ switch (type) {
+ case PacketType::CONTROL:
+ switch (GetControlCommand(packet)) {
+ case ControlCommand::CONNECTION_REQUEST:
+ ReceiveConnectionRequest(packet);
+ break;
+ case ControlCommand::CONNECTION_RESPONSE:
+ ReceiveConnectionResponse(packet);
+ break;
+ case ControlCommand::CONNECTION_CLOSE:
+ ReceiveConnectionClose(packet);
+ break;
+ default:
+ state_ = State::ERROR;
+ break;
+ }
+ break;
+ case PacketType::DATA:
+ if (!IsFirstDataPacket(packet)) {
+ state_ = State::ERROR;
+ } else {
+ AppendData(packet);
Kyle Horimoto 2016/06/09 23:59:46 You also need to check that it has data at all (pa
jingxuy 2016/06/10 23:11:12 Acknowledged.
+ }
+ break;
+ default:
+ state_ = State::ERROR;
+ break;
+ }
+}
+
+void BluetoothLowEnergyWeavePacketReceiver::ReceiveWithPartialData(
+ const Packet& packet) {
+ // TODO(jingxuy): add dcheck stuff
+ PacketType type = GetPacketType(packet);
+ switch (type) {
+ case PacketType::CONTROL:
Kyle Horimoto 2016/06/09 23:59:46 If we're receiving partial data, we shouldn't even
jingxuy 2016/06/10 23:11:12 Pending response from Gustavo
+ if (GetControlCommand(packet) == ControlCommand::CONNECTION_CLOSE) {
+ ReceiveConnectionClose(packet);
+ } else {
+ // Shouldn't be receiving request/response in the middle of a data
Kyle Horimoto 2016/06/09 23:59:46 DLOG something here and everywhere else where ther
+ // transaction.
+ state_ = State::ERROR;
+ }
+ break;
+ case PacketType::DATA:
+ if (IsFirstDataPacket(packet)) {
+ state_ = State::ERROR;
+ } else {
+ AppendData(packet);
+ if (IsLastDataPacket(packet)) {
+ state_ = State::DATA_READY;
+ }
+ }
+ break;
+ default:
+ state_ = State::ERROR;
+ break;
+ }
+}
+
+void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionRequest(
+ const Packet& packet) {
+ // TODO(jingxuy): add dcheck stuff
+ uint16_t min_version = GetShortField(packet, 1);
+ uint16_t max_version = GetShortField(packet, 3);
+ if (!(min_version == 1 && max_version == 1)) {
+ state_ = State::ERROR;
+ } else {
+ packet_size_ = GetShortField(packet, 5);
+ // TODO(jingxuy): what do I do with the 13 bytes of data?
Kyle Horimoto 2016/06/09 23:59:45 You should AppendData() and set state to DATA_READ
jingxuy 2016/06/10 23:11:12 Pending response from Gustavo
+ }
+}
+
+void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionResponse(
+ const Packet& packet) {
+ // TODO(jingxuy): add dcheck stuff
+ uint16_t selected_version = GetShortField(packet, 1);
+ if (selected_version != 1) {
+ state_ = State::ERROR;
+ } else {
+ packet_size_ = GetShortField(packet, 3);
+ // TODO(jingxuy): what do I do with the 15 bytes of data?
+ }
+}
+
+void BluetoothLowEnergyWeavePacketReceiver::ReceiveConnectionClose(
+ const Packet& packet) {
+ // TODO(jingxuy): add dcheck stuff
+ reason_for_close_ = static_cast<ReasonForClose>(GetShortField(packet, 1));
Kyle Horimoto 2016/06/09 23:59:46 What happens if the value passed isn't a ReasonFor
jingxuy 2016/06/10 23:11:12 Done.
+ state_ = State::CONNECTION_CLOSED;
+}
+
+void BluetoothLowEnergyWeavePacketReceiver::AppendData(const Packet& packet) {
+ // TODO(jingxuy): add dcheck stuff
+ // 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];
+}
+
+PacketType BluetoothLowEnergyWeavePacketReceiver::GetPacketType(
+ const Packet& packet) {
+ // Packet type is stored in the highest bit of the first byte.
+ return static_cast<PacketType>((packet[0] >> 7) & 1);
+}
+
+ControlCommand BluetoothLowEnergyWeavePacketReceiver::GetControlCommand(
+ const Packet& packet) {
+ // Control command is stored in the lower 4 bits of the first byte.
+ return static_cast<ControlCommand>((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 {
+ 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

Powered by Google App Engine
This is Rietveld 408576698