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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_genera tor.h"
6
7 #include <string.h>
8
9 #include <algorithm>
10
11 #include "base/logging.h"
12
13 namespace {
14
15 typedef proximity_auth::BluetoothLowEnergyWeavePacketGenerator::Packet Packet;
16
17 const uint16_t kMinSupportedWeaveVersion = 1;
18 const uint16_t kMaxSupportedWeaveVersion = 1;
19 const uint16_t kServerWeaveVersion = 1;
20 const uint16_t kMinConnectionRequestSize = 7;
21 const uint16_t kMinConnectionResponseSize = 5;
22 const uint16_t kMinConnectionCloseSize = 3;
23 const uint32_t kDefaultDataPacketSize = 20;
24 // Max packet size is 0, which means defer the decision to the server.
25 const uint16_t kMaxSupportedPacketSize = 0;
26 const uint8_t kMaxPacketCounter = 8;
27
28 } // namespace
29
30 namespace proximity_auth {
31
32 // static.
33 BluetoothLowEnergyWeavePacketGenerator::Factory*
34 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ =
35 nullptr;
36
37 // static.
38 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
39 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() {
40 if (factory_instance_ == nullptr) {
41 factory_instance_ = new Factory();
42 }
43 return factory_instance_->BuildInstance();
44 }
45
46 // static.
47 void BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting(
48 Factory* factory) {
49 factory_instance_ = factory;
50 }
51
52 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
53 BluetoothLowEnergyWeavePacketGenerator::Factory::BuildInstance() {
54 return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>(
55 new BluetoothLowEnergyWeavePacketGenerator());
56 }
57
58 BluetoothLowEnergyWeavePacketGenerator::BluetoothLowEnergyWeavePacketGenerator()
59 : packet_size_(kDefaultDataPacketSize), packet_number_(0) {}
60
61 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() {
62 Packet packet = CreateControlPacket(kMinConnectionRequestSize);
63
64 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.
65 SetShortField(&packet, 1, kMinSupportedWeaveVersion);
66 SetShortField(&packet, 3, kMaxSupportedWeaveVersion);
67 SetShortField(&packet, 5, kMaxSupportedPacketSize);
68
69 return packet;
70 }
71
72 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() {
73 Packet packet = CreateControlPacket(kMinConnectionResponseSize);
74
75 SetControlCommand(&packet, ControlCommand::CONNECTION_RESPONSE);
76 SetShortField(&packet, 1, kServerWeaveVersion);
77 SetShortField(&packet, 3, packet_size_);
78
79 return packet;
80 }
81
82 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose(
83 ReasonForClose reason_for_close) {
84 Packet packet = CreateControlPacket(kMinConnectionCloseSize);
85
86 SetControlCommand(&packet, ControlCommand::CONNECTION_CLOSE);
87 SetShortField(&packet, 1, reason_for_close);
88
89 return packet;
90 }
91
92 Packet BluetoothLowEnergyWeavePacketGenerator::CreateControlPacket(
93 uint16_t size) {
94 Packet packet(size, 0);
95
96 // Packet is a control packet.
97 SetPacketTypeBit(&packet, PacketType::CONTROL);
98 SetPacketCounter(&packet);
99
100 return packet;
101 }
102
103 void BluetoothLowEnergyWeavePacketGenerator::SetDataPacketSize(uint32_t size) {
104 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
105 packet_size_ = size;
106 }
107
108 std::vector<Packet> BluetoothLowEnergyWeavePacketGenerator::EncodeDataMessage(
109 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.
110 // The first byte of a packet is used by the uWeave protocol,
111 // hence the payload is 1 byte smaller than the packet size.
112 uint32_t packet_payload_size = packet_size_ - 1;
113
114 uint32_t message_length = message.length();
115 // (packet_payload_size - 1) is used to enforce rounding up.
116 uint32_t num_packets =
117 (message_length + (packet_payload_size - 1)) / packet_payload_size;
118
119 // Empty message resulted in an empty but existent packet.
120 num_packets = std::max(num_packets, static_cast<uint32_t>(1));
121
122 std::vector<Packet> weave_message(num_packets);
123
124 const char* byte_message = message.c_str();
125
126 for (uint32_t i = 0; i < num_packets; ++i) {
127 Packet* packet = &weave_message[i];
128 uint32_t begin = packet_payload_size * i;
129 uint32_t end = std::min(begin + packet_payload_size, message_length);
130
131 packet->push_back(0);
132
133 SetPacketTypeBit(packet, PacketType::DATA);
134 SetPacketCounter(packet);
135
136 for (uint32_t i = begin; i < end; ++i) {
137 packet->push_back(byte_message[i]);
138 }
139 }
140
141 // Guaranteed to have at least one packet.
142 SetDataFirstBit(&weave_message[0]);
143 SetDataLastBit(&weave_message[num_packets - 1]);
144
145 return weave_message;
146 }
147
148 void BluetoothLowEnergyWeavePacketGenerator::SetShortField(Packet* packet,
149 uint32_t byte_offset,
150 uint16_t val) {
151 DCHECK(packet);
152 DCHECK_LT(byte_offset, packet->size());
153 DCHECK_LT(byte_offset + 1, packet->size());
154
155 uint8_t upper = (val >> 8) & 0xFF;
156 uint8_t lower = val & 0xFF;
157
158 packet->at(byte_offset) = lower;
159 packet->at(byte_offset + 1) = upper;
160 }
161
162 void BluetoothLowEnergyWeavePacketGenerator::SetPacketTypeBit(Packet* packet,
163 PacketType type) {
164 DCHECK(packet);
165 DCHECK(!packet->empty());
166
167 // Type bit is the highest bit of the first byte of the packet.
168 // So clear the highest bit and set it according to val.
169 packet->at(0) = (packet->at(0) & 0x7F) | (type << 7);
170 }
171
172 void BluetoothLowEnergyWeavePacketGenerator::SetControlCommand(
173 Packet* packet,
174 ControlCommand command) {
175 DCHECK(packet);
176 DCHECK(!packet->empty());
177
178 // Control Command is the lower 4 bits of the packet's first byte.
179 // So clear the lower 4 bites and set it according to val.
180 packet->at(0) = (packet->at(0) & 0xF0) | command;
181 }
182
183 void BluetoothLowEnergyWeavePacketGenerator::SetPacketCounter(Packet* packet) {
184 DCHECK(packet);
185 DCHECK(!packet->empty());
186 uint8_t counter = packet_number_ % kMaxPacketCounter;
187
188 // Packet counter is the bits 4, 5, and 6 of the packet's first byte.
189 // So clear those bits and set them according to current packet counter
190 // modular max packet counter.
191 packet->at(0) = (packet->at(0) & 0x8F) | (counter << 4);
192 packet_number_++;
193 }
194
195 void BluetoothLowEnergyWeavePacketGenerator::SetDataFirstBit(Packet* packet) {
196 DCHECK(packet);
197 DCHECK(!packet->empty());
198
199 // First bit is bit 3 of the packet's first byte and set it to 1.
200 packet->at(0) = packet->at(0) | (1 << 3);
201 }
202
203 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) {
204 DCHECK(packet);
205 DCHECK(!packet->empty());
206
207 // Last bit is the bit 2 of the packet's first byte and set it to 1.
208 packet->at(0) = packet->at(0) | (1 << 2);
209 }
210
211 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698