Commit 714da71c authored by jingxuy's avatar jingxuy Committed by Commit bot
Browse files

Move weave packet to common location b/t generator and receiver

BUG=617238

Review-Url: https://codereview.chromium.org/2096103003
Cr-Commit-Position: refs/heads/master@{#402355}
parent 578e7842
......@@ -14,6 +14,7 @@ source_set("ble") {
"bluetooth_low_energy_connection_finder.h",
"bluetooth_low_energy_device_whitelist.cc",
"bluetooth_low_energy_device_whitelist.h",
"bluetooth_low_energy_weave_defines.h",
"bluetooth_low_energy_weave_packet_generator.cc",
"bluetooth_low_energy_weave_packet_generator.h",
"fake_wire_message.cc",
......
// 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.
#ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_DEFINES_H_
#define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_DEFINES_H_
#include <stddef.h>
#include <stdint.h>
#include <vector>
namespace proximity_auth {
namespace weave {
enum PacketType { DATA = 0x00, CONTROL = 0x01 };
// Identify the action intended by the control packet.
enum ControlCommand {
CONNECTION_REQUEST = 0x00,
CONNECTION_RESPONSE = 0x01,
CONNECTION_CLOSE = 0x02
};
// Sent with the ConnectionClose control packet.
// Identify why the client/server wished to close the connection.
enum ReasonForClose {
CLOSE_WITHOUT_ERROR = 0x00,
UNKNOWN_ERROR = 0x01,
NO_COMMON_VERSION_SUPPORTED = 0x02,
RECEIVED_PACKET_OUT_OF_SEQUENCE = 0x03,
APPLICATION_ERROR = 0x80
};
typedef std::vector<uint8_t> Packet;
const uint16_t kMinConnectionRequestSize = 7;
const uint16_t kMinConnectionResponseSize = 5;
const uint16_t kMaxConnectionCloseSize = 3;
const uint16_t kDefaultMaxPacketSize = 20;
const uint16_t kWeaveVersion = 1;
// Defer selecting the max packet size to the server.
const uint16_t kSelectMaxPacketSize = 0;
const uint8_t kMaxPacketCounter = 8;
// Used only for tests.
const uint8_t kByteDefaultMaxPacketSize = 20;
const uint8_t kByteWeaveVersion = 1;
const uint8_t kByteSelectMaxPacketSize = 0;
const uint8_t kEmptyUpperByte = 0;
} // namespace weave
} // namespace proximity_auth
#endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_DEFINES_H_
......@@ -16,24 +16,8 @@
#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 kDefaultMaxPacketSize = 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 {
namespace weave {
// static.
BluetoothLowEnergyWeavePacketGenerator::Factory*
......@@ -72,9 +56,9 @@ Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() {
// resets the packet counter.
next_packet_counter_ = 1;
SetControlCommand(ControlCommand::CONNECTION_REQUEST, &packet);
SetShortField(1, kMinSupportedWeaveVersion, &packet);
SetShortField(3, kMaxSupportedWeaveVersion, &packet);
SetShortField(5, kMaxSupportedPacketSize, &packet);
SetShortField(1, kWeaveVersion, &packet);
SetShortField(3, kWeaveVersion, &packet);
SetShortField(5, kSelectMaxPacketSize, &packet);
return packet;
}
......@@ -87,7 +71,7 @@ Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() {
// resets the next packet counter.
next_packet_counter_ = 1;
SetControlCommand(ControlCommand::CONNECTION_RESPONSE, &packet);
SetShortField(1, kServerWeaveVersion, &packet);
SetShortField(1, kWeaveVersion, &packet);
SetShortField(3, max_packet_size_, &packet);
return packet;
......@@ -95,7 +79,7 @@ Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() {
Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose(
ReasonForClose reason_for_close) {
Packet packet(kMinConnectionCloseSize, 0);
Packet packet(kMaxConnectionCloseSize, 0);
SetPacketTypeBit(PacketType::CONTROL, &packet);
SetPacketCounter(&packet);
......@@ -105,7 +89,7 @@ Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose(
return packet;
}
void BluetoothLowEnergyWeavePacketGenerator::SetMaxPacketSize(uint32_t size) {
void BluetoothLowEnergyWeavePacketGenerator::SetMaxPacketSize(uint16_t size) {
DCHECK(size >= kDefaultMaxPacketSize);
max_packet_size_ = size;
}
......@@ -212,4 +196,6 @@ void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) {
packet->at(0) = packet->at(0) | (1 << 2);
}
} // namespace weave
} // namespace proximity_auth
......@@ -12,9 +12,12 @@
#include <string>
#include <vector>
#include "components/proximity_auth/ble/bluetooth_low_energy_weave_defines.h"
#include "build/build_config.h"
namespace proximity_auth {
namespace weave {
// Generates the messages sent using the uWeave protocol.
class BluetoothLowEnergyWeavePacketGenerator {
public:
......@@ -35,34 +38,12 @@ class BluetoothLowEnergyWeavePacketGenerator {
static Factory* factory_instance_;
};
// Determine whether a packet is a control packet or a data packet.
enum PacketType { DATA = 0x00, CONTROL = 0x01 };
// Identify the action intended by the control packet.
enum ControlCommand {
CONNECTION_REQUEST = 0x00,
CONNECTION_RESPONSE = 0x01,
CONNECTION_CLOSE = 0x02
};
// Sent with the ConnectionClose control packet.
// Identify why the client/server wished to close the connection.
enum ReasonForClose {
CLOSE_WITHOUT_ERROR = 0x00,
UNKNOWN_ERROR = 0x01,
NO_COMMON_VERSION_SUPPORTED = 0x02,
RECEIVED_PACKET_OUT_OF_SEQUENCE = 0x03,
APPLICATION_ERROR = 0x80
};
typedef std::vector<uint8_t> Packet;
Packet CreateConnectionRequest();
Packet CreateConnectionResponse();
Packet CreateConnectionClose(ReasonForClose reason_for_close);
// Packet size must be greater than or equal to 20.
void SetMaxPacketSize(uint32_t size);
void SetMaxPacketSize(uint16_t size);
// Will crash if message is empty.
std::vector<Packet> EncodeDataMessage(std::string message);
......@@ -80,12 +61,14 @@ class BluetoothLowEnergyWeavePacketGenerator {
// The default max packet length is 20 unless SetDataPacketLength() is called
// and specified otherwise.
uint32_t max_packet_size_;
uint16_t max_packet_size_;
// Counter for the number of packets sent, starting at 0.
uint32_t next_packet_counter_;
uint8_t next_packet_counter_;
};
} // namespace weave
} // namespace proximity_auth
#endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENERATOR_H_
......@@ -12,22 +12,8 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using proximity_auth::BluetoothLowEnergyWeavePacketGenerator;
namespace {
typedef BluetoothLowEnergyWeavePacketGenerator::ReasonForClose ReasonForClose;
typedef BluetoothLowEnergyWeavePacketGenerator::Packet Packet;
const uint8_t kDefaultPacketSize = 20;
const uint8_t kWeaveVersion = 1;
const uint8_t kMaxCounter = 8;
const uint8_t kEmptyUpperByte = 0;
const uint8_t kSelectDefaultPacketSize = 0;
const uint8_t kDataType = 0;
} // namespace
namespace proximity_auth {
namespace weave {
class ProximityAuthBluetoothLowEnergyWeavePacketGeneratorTest
: public testing::Test {
......@@ -76,9 +62,13 @@ TEST_F(ProximityAuthBluetoothLowEnergyWeavePacketGeneratorTest,
// -000 ---- : counter = 0
// ---- 0000 : command = 0 (request)
// 1000 0000 = 0x80
expected = {
0x80, kEmptyUpperByte, kWeaveVersion, kEmptyUpperByte,
kWeaveVersion, kEmptyUpperByte, kSelectDefaultPacketSize};
expected = {0x80,
kEmptyUpperByte,
kByteWeaveVersion,
kEmptyUpperByte,
kByteWeaveVersion,
kEmptyUpperByte,
kByteSelectMaxPacketSize};
EXPECT_EQ(expected, packet);
}
......@@ -97,8 +87,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyWeavePacketGeneratorTest,
// -000 ---- : counter = 0
// ---- 0001 : command = 1 (response)
// 1000 0001 = 0x81
expected_default = {0x81, kEmptyUpperByte, kWeaveVersion, kEmptyUpperByte,
kDefaultPacketSize};
expected_default = {0x81, kEmptyUpperByte, kByteWeaveVersion, kEmptyUpperByte,
kByteDefaultMaxPacketSize};
EXPECT_EQ(expected_default, packet);
}
......@@ -121,8 +111,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyWeavePacketGeneratorTest,
// -000 ---- : counter = 0
// ---- 0001 : command = 1 (response)
// 1000 0001 = 0x81
expected_selected = {0x81, kEmptyUpperByte, kWeaveVersion, kEmptyUpperByte,
kSelectedPacketSize};
expected_selected = {0x81, kEmptyUpperByte, kByteWeaveVersion,
kEmptyUpperByte, kSelectedPacketSize};
EXPECT_EQ(expected_selected, packet);
}
......@@ -254,17 +244,21 @@ TEST_F(ProximityAuthBluetoothLowEnergyWeavePacketGeneratorTest,
BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance();
const uint8_t kNumPackets = 100;
std::string data(kNumPackets * kDefaultPacketSize, 'a');
std::string data(kNumPackets * kByteDefaultMaxPacketSize, 'a');
std::vector<Packet> packets = generator->EncodeDataMessage(data);
std::vector<Packet> expected(kNumPackets);
const uint8_t kDataType = 0;
for (uint8_t i = 0; i < kNumPackets; ++i) {
uint8_t header = packets[i][0];
EXPECT_EQ(i % kMaxCounter, GetCounterFromHeader(header));
EXPECT_EQ(i % kMaxPacketCounter, GetCounterFromHeader(header));
EXPECT_EQ(kDataType, GetPacketType(header));
}
}
} // namespace weave
} // namespace proximity_auth
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment