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

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: addressed style comments 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"
sacomoto 2016/06/13 16:05:19 Is this header really necesary?
jingxuy 2016/06/17 00:56:45 it is used for DCHECK
12
13 namespace {
14
15 const uint16_t kMinSupportedWeaveVersion = 1;
16 const uint16_t kMaxSupportedWeaveVersion = 1;
17 const uint16_t kServerWeaveVersion = 1;
sacomoto 2016/06/13 16:05:19 This constant should not be necessary. See my comm
Kyle Horimoto 2016/06/13 17:55:53 Regarding your other comment: you are certainly co
jingxuy 2016/06/17 00:56:45 see my comment in the header file.
18 const uint16_t kControlPacketSize = 20;
19 const uint32_t kDefaultDataPacketSize = 20;
20 // Max packet size is 0, which means defer the decision to the server.
21 const uint16_t kMaxSupportedPacketSize = 0;
22 const uint8_t kMaxPacketCounter = 8;
23
24 } // namespace
25
26 namespace proximity_auth {
27
28 // static.
29 BluetoothLowEnergyWeavePacketGenerator::Factory*
30 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ =
31 nullptr;
32
33 // static.
34 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
35 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() {
36 if (factory_instance_ == nullptr) {
37 factory_instance_ = new Factory();
38 }
39 return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>(
40 factory_instance_->BuildInstance());
41 }
42
43 // static.
44 void BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting(
45 Factory* factory) {
46 factory_instance_ = factory;
47 }
48
49 BluetoothLowEnergyWeavePacketGenerator*
50 BluetoothLowEnergyWeavePacketGenerator::Factory::BuildInstance() {
51 return new BluetoothLowEnergyWeavePacketGenerator();
52 }
53
54 BluetoothLowEnergyWeavePacketGenerator::BluetoothLowEnergyWeavePacketGenerator()
55 : packet_size_(kDefaultDataPacketSize), packet_number_(0) {}
56
57 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator::Packet>
58 BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() {
59 Packet* packet = CreateControlPacket();
60
61 SetControlCommand(packet, ControlCommand::CONNECTION_REQUEST);
62 SetShortField(packet, 1, kMinSupportedWeaveVersion);
63 SetShortField(packet, 3, kMaxSupportedWeaveVersion);
64 SetShortField(packet, 5, kMaxSupportedPacketSize);
65
66 return std::unique_ptr<Packet>(packet);
67 }
68
69 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator::Packet>
70 BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() {
71 Packet* packet = CreateControlPacket();
72
73 SetControlCommand(packet, ControlCommand::CONNECTION_RESPONSE);
74 SetShortField(packet, 1, kServerWeaveVersion);
75 SetShortField(packet, 3, packet_size_);
76
77 return std::unique_ptr<Packet>(packet);
78 }
79
80 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator::Packet>
81 BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose(
82 ReasonForClose reason_for_close) {
83 Packet* packet = CreateControlPacket();
84
85 SetControlCommand(packet, ControlCommand::CONNECTION_CLOSE);
86 SetShortField(packet, 1, reason_for_close);
87
88 return std::unique_ptr<Packet>(packet);
89 }
90
91 BluetoothLowEnergyWeavePacketGenerator::Packet*
92 BluetoothLowEnergyWeavePacketGenerator::CreateControlPacket() {
93 Packet* packet = new Packet(kControlPacketSize, 0);
94
95 // Packet is a control packet.
96 SetPacketTypeBit(packet, PacketType::CONTROL);
97 SetPacketCounter(packet);
98
99 return packet;
100 }
101
102 void BluetoothLowEnergyWeavePacketGenerator::SetDataPacketSize(uint32_t size) {
103 packet_size_ = size;
104 }
105
106 std::unique_ptr<std::vector<BluetoothLowEnergyWeavePacketGenerator::Packet>>
107 BluetoothLowEnergyWeavePacketGenerator::EncodeDataMessage(std::string message) {
108 // The first byte of a packet is used by the uWeave protocol,
109 // hence the payload is 1 byte smaller than the packet size.
110 uint32_t packet_payload_size = packet_size_ - 1;
111
112 // The message's length is the length of the string plus '\0'.
sacomoto 2016/06/13 16:05:18 The uWeave protocol does not require the bytes sen
jingxuy 2016/06/17 00:56:45 I had a misconception thinking that we are sending
113 // (packet_payload_size - 1) is used to enforce rounding up.
114 uint32_t num_packets = ((message.length() + 1) + (packet_payload_size - 1)) /
115 packet_payload_size;
116
117 std::vector<Packet>* weave_message = new std::vector<Packet>(num_packets);
118
119 uint32_t message_num_bytes = message.length() + 1;
120 const char* byte_message = message.c_str();
sacomoto 2016/06/13 16:05:19 You should not append the '\0' at the end of the m
jingxuy 2016/06/17 00:56:45 Done.
121
122 for (uint32_t i = 0; i < num_packets; ++i) {
123 Packet* packet = &weave_message->at(i);
124 uint32_t begin = packet_payload_size * i;
125 uint32_t end = std::min(begin + packet_payload_size, message_num_bytes);
126
127 packet->push_back(0);
128
129 SetPacketTypeBit(packet, PacketType::DATA);
130 SetPacketCounter(packet);
131
132 for (uint32_t i = begin; i < end; ++i) {
133 packet->push_back(byte_message[i]);
134 }
135 }
136
137 // Since even empty string has \0, weave_message is guaranteed to be nonempty.
138 SetDataFirstBit(&weave_message->at(0));
139 SetDataLastBit(&weave_message->at(num_packets - 1));
140
141 return std::unique_ptr<std::vector<Packet>>(weave_message);
142 }
143
144 void BluetoothLowEnergyWeavePacketGenerator::SetShortField(Packet* packet,
145 uint32_t byte_offset,
146 uint16_t val) {
147 DCHECK(packet);
148 DCHECK_LT(byte_offset, packet->size());
149 DCHECK_LT(byte_offset + 1, packet->size());
150
151 uint8_t upper = (val >> 8) & 0xFF;
152 uint8_t lower = val & 0xFF;
153
154 packet->at(byte_offset) = lower;
155 packet->at(byte_offset + 1) = upper;
156 }
157
158 void BluetoothLowEnergyWeavePacketGenerator::SetPacketTypeBit(Packet* packet,
159 PacketType type) {
160 DCHECK(packet);
161 DCHECK(!packet->empty());
162
163 // Type bit is the highest bit of the first byte of the packet.
164 // So clear the highest bit and set it according to val.
165 packet->at(0) = (packet->at(0) & 0x7F) | (type << 7);
166 }
167
168 void BluetoothLowEnergyWeavePacketGenerator::SetControlCommand(
169 Packet* packet,
170 ControlCommand command) {
171 DCHECK(packet);
172 DCHECK(!packet->empty());
173
174 // Control Command is the lower 4 bits of the packet's first byte.
175 // So clear the lower 4 bites and set it according to val.
176 packet->at(0) = (packet->at(0) & 0xF0) | command;
177 }
178
179 void BluetoothLowEnergyWeavePacketGenerator::SetPacketCounter(Packet* packet) {
180 DCHECK(packet);
181 DCHECK(!packet->empty());
182 uint8_t counter = packet_number_ % kMaxPacketCounter;
183
184 // Packet counter is the bits 4, 5, and 6 of the packet's first byte.
185 // So clear those bits and set them according to current packet counter
186 // modular max packet counter.
187 packet->at(0) = (packet->at(0) & 0x8F) | (counter << 4);
188 packet_number_++;
189 }
190
191 void BluetoothLowEnergyWeavePacketGenerator::SetDataFirstBit(Packet* packet) {
192 DCHECK(packet);
193 DCHECK(!packet->empty());
194
195 // First bit is bit 3 of the packet's first byte and set it to 1.
196 packet->at(0) = packet->at(0) | (1 << 3);
197 }
198
199 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) {
200 DCHECK(packet);
201 DCHECK(!packet->empty());
202
203 // Last bit is the bit 2 of the packet's first byte and set it to 1.
204 packet->at(0) = packet->at(0) | (1 << 2);
205 }
206
207 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698