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

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

Issue 2031953003: Weave Packet Generator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: enforce the minimum packet size to be 2 so can contain header and 1 byte of data 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_generator.cc
diff --git a/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_generator.cc b/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_generator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..402923c0e4b6d99757c026551bb04be563e1faf3
--- /dev/null
+++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_packet_generator.cc
@@ -0,0 +1,211 @@
+// 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_generator.h"
+
+#include <string.h>
+
+#include <algorithm>
+
+#include "base/logging.h"
+
+namespace {
+
+typedef proximity_auth::BluetoothLowEnergyWeavePacketGenerator::Packet Packet;
+
+const uint16_t kMinSupportedWeaveVersion = 1;
+const uint16_t kMaxSupportedWeaveVersion = 1;
+const uint16_t kServerWeaveVersion = 1;
+const uint16_t kMinConnectionRequestSize = 7;
+const uint16_t kMinConnectionResponseSize = 5;
+const uint16_t kMinConnectionCloseSize = 3;
+const uint32_t kDefaultDataPacketSize = 20;
+// Max packet size is 0, which means defer the decision to the server.
+const uint16_t kMaxSupportedPacketSize = 0;
+const uint8_t kMaxPacketCounter = 8;
+
+} // namespace
+
+namespace proximity_auth {
+
+// static.
+BluetoothLowEnergyWeavePacketGenerator::Factory*
+ BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ =
+ nullptr;
+
+// static.
+std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
+BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() {
+ if (factory_instance_ == nullptr) {
+ factory_instance_ = new Factory();
+ }
+ return factory_instance_->BuildInstance();
+}
+
+// static.
+void BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting(
+ Factory* factory) {
+ factory_instance_ = factory;
+}
+
+std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
+BluetoothLowEnergyWeavePacketGenerator::Factory::BuildInstance() {
+ return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>(
+ new BluetoothLowEnergyWeavePacketGenerator());
+}
+
+BluetoothLowEnergyWeavePacketGenerator::BluetoothLowEnergyWeavePacketGenerator()
+ : packet_size_(kDefaultDataPacketSize), packet_number_(0) {}
+
+Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() {
+ Packet packet = CreateControlPacket(kMinConnectionRequestSize);
+
+ SetControlCommand(&packet, ControlCommand::CONNECTION_REQUEST);
Kyle Horimoto 2016/06/17 04:04:12 Instead of passing |&packet| everywhere, just make
Kyle Horimoto 2016/06/20 18:01:23 Ping.
jingxuy 2016/06/20 21:29:32 Is it the convention that if the caller expects a
Kyle Horimoto 2016/06/20 21:52:17 Ah, you're right. Nope, please leave it as-is. How
Kyle Horimoto 2016/06/21 02:15:29 Ping.
jingxuy 2016/06/21 06:03:10 Done.
+ SetShortField(&packet, 1, kMinSupportedWeaveVersion);
+ SetShortField(&packet, 3, kMaxSupportedWeaveVersion);
+ SetShortField(&packet, 5, kMaxSupportedPacketSize);
+
+ return packet;
+}
+
+Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() {
+ Packet packet = CreateControlPacket(kMinConnectionResponseSize);
+
+ SetControlCommand(&packet, ControlCommand::CONNECTION_RESPONSE);
+ SetShortField(&packet, 1, kServerWeaveVersion);
+ SetShortField(&packet, 3, packet_size_);
+
+ return packet;
+}
+
+Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose(
+ ReasonForClose reason_for_close) {
+ Packet packet = CreateControlPacket(kMinConnectionCloseSize);
+
+ SetControlCommand(&packet, ControlCommand::CONNECTION_CLOSE);
+ SetShortField(&packet, 1, reason_for_close);
+
+ return packet;
+}
+
+Packet BluetoothLowEnergyWeavePacketGenerator::CreateControlPacket(
+ uint16_t size) {
+ Packet packet(size, 0);
+
+ // Packet is a control packet.
+ SetPacketTypeBit(&packet, PacketType::CONTROL);
+ SetPacketCounter(&packet);
+
+ return packet;
+}
+
+void BluetoothLowEnergyWeavePacketGenerator::SetDataPacketSize(uint32_t size) {
+ DCHECK(size >= 2);
sacomoto 2016/06/17 15:38:04 s/2/20. The BLE specs guarantee at least 20 for t
jingxuy 2016/06/17 18:59:00 I think 20 is the guaranteed max size, the 2 is ju
Kyle Horimoto 2016/06/20 18:01:23 Nope - 20 should be the minimum, not the maximum.
jingxuy 2016/06/20 21:29:32 I think 20 is the minimum maximum. As the receiver
Kyle Horimoto 2016/06/20 21:52:17 I think there's a misunderstanding. We aren't sayi
Kyle Horimoto 2016/06/21 02:15:29 Ping.
jingxuy 2016/06/21 06:03:10 I knew what you guys meant. I just think we should
+ packet_size_ = size;
+}
+
+std::vector<Packet> BluetoothLowEnergyWeavePacketGenerator::EncodeDataMessage(
+ std::string message) {
Kyle Horimoto 2016/06/17 04:04:12 Should we throw an error here if message is empty?
sacomoto 2016/06/17 15:38:04 +1 I suggested a DCHECK in an earlier comment, bu
jingxuy 2016/06/17 18:59:00 Done.
+ // The first byte of a packet is used by the uWeave protocol,
+ // hence the payload is 1 byte smaller than the packet size.
+ uint32_t packet_payload_size = packet_size_ - 1;
+
+ uint32_t message_length = message.length();
+ // (packet_payload_size - 1) is used to enforce rounding up.
+ uint32_t num_packets =
+ (message_length + (packet_payload_size - 1)) / packet_payload_size;
+
+ // Empty message resulted in an empty but existent packet.
+ num_packets = std::max(num_packets, static_cast<uint32_t>(1));
+
+ std::vector<Packet> weave_message(num_packets);
+
+ const char* byte_message = message.c_str();
+
+ for (uint32_t i = 0; i < num_packets; ++i) {
+ Packet* packet = &weave_message[i];
+ uint32_t begin = packet_payload_size * i;
+ uint32_t end = std::min(begin + packet_payload_size, message_length);
+
+ packet->push_back(0);
+
+ SetPacketTypeBit(packet, PacketType::DATA);
+ SetPacketCounter(packet);
+
+ for (uint32_t i = begin; i < end; ++i) {
+ packet->push_back(byte_message[i]);
+ }
+ }
+
+ // Guaranteed to have at least one packet.
+ SetDataFirstBit(&weave_message[0]);
+ SetDataLastBit(&weave_message[num_packets - 1]);
+
+ return weave_message;
+}
+
+void BluetoothLowEnergyWeavePacketGenerator::SetShortField(Packet* packet,
+ uint32_t byte_offset,
+ uint16_t val) {
+ DCHECK(packet);
+ DCHECK_LT(byte_offset, packet->size());
+ DCHECK_LT(byte_offset + 1, packet->size());
+
+ uint8_t upper = (val >> 8) & 0xFF;
+ uint8_t lower = val & 0xFF;
+
+ packet->at(byte_offset) = lower;
+ packet->at(byte_offset + 1) = upper;
+}
+
+void BluetoothLowEnergyWeavePacketGenerator::SetPacketTypeBit(Packet* packet,
+ PacketType type) {
+ DCHECK(packet);
+ DCHECK(!packet->empty());
+
+ // Type bit is the highest bit of the first byte of the packet.
+ // So clear the highest bit and set it according to val.
+ packet->at(0) = (packet->at(0) & 0x7F) | (type << 7);
+}
+
+void BluetoothLowEnergyWeavePacketGenerator::SetControlCommand(
+ Packet* packet,
+ ControlCommand command) {
+ DCHECK(packet);
+ DCHECK(!packet->empty());
+
+ // Control Command is the lower 4 bits of the packet's first byte.
+ // So clear the lower 4 bites and set it according to val.
+ packet->at(0) = (packet->at(0) & 0xF0) | command;
+}
+
+void BluetoothLowEnergyWeavePacketGenerator::SetPacketCounter(Packet* packet) {
+ DCHECK(packet);
+ DCHECK(!packet->empty());
+ uint8_t counter = packet_number_ % kMaxPacketCounter;
+
+ // Packet counter is the bits 4, 5, and 6 of the packet's first byte.
+ // So clear those bits and set them according to current packet counter
+ // modular max packet counter.
+ packet->at(0) = (packet->at(0) & 0x8F) | (counter << 4);
+ packet_number_++;
+}
+
+void BluetoothLowEnergyWeavePacketGenerator::SetDataFirstBit(Packet* packet) {
+ DCHECK(packet);
+ DCHECK(!packet->empty());
+
+ // First bit is bit 3 of the packet's first byte and set it to 1.
+ packet->at(0) = packet->at(0) | (1 << 3);
+}
+
+void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) {
+ DCHECK(packet);
+ DCHECK(!packet->empty());
+
+ // Last bit is the bit 2 of the packet's first byte and set it to 1.
+ packet->at(0) = packet->at(0) | (1 << 2);
+}
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698