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

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: uWeave Packet Generator 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 proximity_auth {
14
15 BluetoothLowEnergyWeavePacketGenerator::Factory*
16 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_;
17
18 BluetoothLowEnergyWeavePacketGenerator::Factory::Factory() {
19 factory_instance_ = this;
Kyle Horimoto 2016/06/06 19:30:54 Remove this. See comment below.
jingxuy 2016/06/07 01:19:06 Done.
20 }
21
22 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
23 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() {
Kyle Horimoto 2016/06/06 19:30:54 This should be structured a bit differently. Do so
jingxuy 2016/06/07 01:19:07 Done.
24 return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>(
25 factory_instance_->BuildInstance());
26 }
27
28 void BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting(
29 Factory* factory) {
30 factory_instance_ = factory;
31 }
32
33 BluetoothLowEnergyWeavePacketGenerator*
34 BluetoothLowEnergyWeavePacketGenerator::Factory::BuildInstance() {
35 return new BluetoothLowEnergyWeavePacketGenerator();
36 }
37
38 BluetoothLowEnergyWeavePacketGenerator::
39 BluetoothLowEnergyWeavePacketGenerator() {
40 packet_size_ = 20;
Kyle Horimoto 2016/06/06 19:30:54 Please use a constant instead of "20" directly.
Kyle Horimoto 2016/06/06 19:30:55 Please initialize these via an initializer list.
jingxuy 2016/06/07 01:19:07 Done.
41 packet_number_ = 0;
42 }
43
44 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator::Packet>
45 BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() {
46 Packet* packet = CreateControlPacket();
47
48 // Command is a connection request.
49 SetControlCmd(packet, 0, 0);
sacomoto 2016/06/06 15:25:20 Please create an enum for the command values: enu
jingxuy 2016/06/07 01:19:06 Done.
50 // Min supported version is 1.
sacomoto 2016/06/06 15:25:20 Please use a constant for the min supported versio
Kyle Horimoto 2016/06/06 19:30:55 Just use the kWeaveVersion constant you already ha
jingxuy 2016/06/07 01:19:07 Done.
51 SetIntField(packet, 1, 1);
52 // Max supported version is 1.
sacomoto 2016/06/06 15:25:20 The same for the max supported version.
jingxuy 2016/06/07 01:19:06 Done.
53 SetIntField(packet, 3, 1);
54 // Max packet size is 0 which means defer the decision to server.
sacomoto 2016/06/06 15:25:20 The same for max packet size.
jingxuy 2016/06/07 01:19:06 Done.
55 SetIntField(packet, 5, 0);
56
57 return std::unique_ptr<Packet>(packet);
58 }
59
60 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator::Packet>
61 BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() {
62 Packet* packet = CreateControlPacket();
sacomoto 2016/06/06 15:25:21 See the comments above.
jingxuy 2016/06/07 01:19:07 Done.
63
64 // Command is a connection response.
65 SetControlCmd(packet, 0, 1);
66 // Selected protocol version is 1.
67 SetIntField(packet, 1, 1);
68 // Selected packet size is packet_size_.
69 SetIntField(packet, 3, packet_size_);
Kyle Horimoto 2016/06/06 19:30:54 I'm confused by your use of packet_size_. It's use
Kyle Horimoto 2016/06/08 00:06:29 Ping.
jingxuy 2016/06/08 21:30:35 Do you want me to put a comment above packet_size_
Kyle Horimoto 2016/06/09 22:55:42 How about this: just don't use packet_size_ for th
jingxuy 2016/06/11 00:49:32 We can talk more about this when you get back. I d
Kyle Horimoto 2016/06/13 17:55:52 Let me know when you are ready to discuss this.
70
71 return std::unique_ptr<Packet>(packet);
72 }
73
74 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator::Packet>
75 BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose(
76 ReasonForClose reason_for_close) {
sacomoto 2016/06/06 15:25:20 See the comments above.
jingxuy 2016/06/07 01:19:07 Done.
77 Packet* packet = CreateControlPacket();
78
79 // Command is a connection close.
80 SetControlCmd(packet, 0, 2);
81 // Setting reason for close.
82 SetIntField(packet, 1, reason_for_close);
83
84 return std::unique_ptr<Packet>(packet);
85 }
86
87 BluetoothLowEnergyWeavePacketGenerator::Packet*
88 BluetoothLowEnergyWeavePacketGenerator::CreateControlPacket() {
89 Packet* packet = new Packet(kControlPacketSize, 0);
90
91 // Packet is a control packet.
92 SetPacketTypeBit(packet, 0, 1);
Kyle Horimoto 2016/06/06 19:30:54 Use a constant for the value to pass (e.g., kContr
jingxuy 2016/06/07 01:19:06 Done.
93 SetPacketCounter(packet, 0);
94
95 return packet;
96 }
97
98 bool BluetoothLowEnergyWeavePacketGenerator::IsBit(uint8_t val) {
Kyle Horimoto 2016/06/06 19:30:54 Why is this method used? I don't really see the va
jingxuy 2016/06/07 01:19:07 Done.
99 return val == 0 || val == 1;
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 uint32_t payload_size = packet_size_ - 1;
Kyle Horimoto 2016/06/06 19:30:54 Please comment about why this is the case.
Kyle Horimoto 2016/06/08 00:06:29 Ping.
jingxuy 2016/06/08 21:30:35 Done.
109 // (message.length() + 1) + (payload_size - 1) / (payload_size).
Kyle Horimoto 2016/06/06 19:30:54 Can you please explain this calculation?
Kyle Horimoto 2016/06/08 00:06:29 Ping.
jingxuy 2016/06/08 21:30:36 Done.
110 uint32_t num_packets = (message.length() + payload_size) / payload_size;
111 std::vector<Packet>* weave_message = new std::vector<Packet>(num_packets);
112
113 uint32_t last_byte = message.length() + 1;
114 const char* byte_message = message.c_str();
115
116 for (uint32_t i = 0; i < num_packets; ++i) {
117 Packet* packet = &weave_message->at(i);
Kyle Horimoto 2016/06/06 19:30:54 Instead of at(), just use the bracket notation sho
jingxuy 2016/06/07 01:19:05 [] does not work with unique_ptr, hence the at()
118 uint32_t begin = payload_size * i;
119 uint32_t end = std::min(begin + payload_size, last_byte);
120
121 packet->push_back(0);
122 // This is a data packet.
123 SetPacketTypeBit(packet, 0, 0);
124 SetPacketCounter(packet, 0);
125
126 for (uint32_t i = begin; i < end; ++i) {
127 packet->push_back(byte_message[i]);
128 }
129 }
130
131 // Since even empty string has \0, weave_message is guaranteed to be nonempty.
Kyle Horimoto 2016/06/06 19:30:55 weave_message isn't necessarily guaranteed to be n
jingxuy 2016/06/07 01:19:05 the string that is passed in at least has to be an
Kyle Horimoto 2016/06/08 00:06:29 That's not true. An empty string has a length of 0
jingxuy 2016/06/08 21:30:35 length used in this case is the number of bytes in
132 SetDataFirstBit(&weave_message->at(0), 0, 1);
133 SetDataLastBit(&weave_message->at(num_packets - 1), 0, 1);
134
135 return std::unique_ptr<std::vector<Packet>>(weave_message);
136 }
137
138 void BluetoothLowEnergyWeavePacketGenerator::SetIntField(Packet* packet,
139 uint32_t index,
140 uint16_t val) {
141 DCHECK(packet);
142 DCHECK_LT(index, packet->size());
143 DCHECK_LT(index + 1, packet->size());
144
sacomoto 2016/06/06 15:25:20 In the uWeave protocol the integers sent in the co
jingxuy 2016/06/07 01:19:05 Done.
Kyle Horimoto 2016/06/08 00:06:29 You can't assume the endianness of this machine. P
jingxuy 2016/06/08 21:30:36 what do you mean by endianness of this machine? I
sacomoto 2016/06/13 16:05:18 You are both right. The byte ordering is machine d
jingxuy 2016/06/17 00:56:44 Acknowledged.
145 uint8_t upper = val >> 8 & 0xFF;
146 uint8_t lower = val & 0xFF;
147
148 packet->at(index) = upper;
149 packet->at(index + 1) = lower;
150 }
151
152 void BluetoothLowEnergyWeavePacketGenerator::SetPacketTypeBit(Packet* packet,
Kyle Horimoto 2016/06/06 19:30:54 For all these Send*() methods, please give a descr
Kyle Horimoto 2016/06/08 00:06:29 Ping.
jingxuy 2016/06/08 21:30:35 Done.
153 uint32_t index,
sacomoto 2016/06/06 15:25:21 Please remove the |index| parameter, as it's alway
jingxuy 2016/06/07 01:19:07 Done.
154 uint8_t val) {
sacomoto 2016/06/06 15:25:21 Please use |bool| instead of |uint8_t|.
Kyle Horimoto 2016/06/06 19:30:54 +1
jingxuy 2016/06/07 01:19:07 Done.
155 DCHECK(packet);
156 DCHECK_LT(index, packet->size());
157 DCHECK(IsBit(val));
sacomoto 2016/06/06 15:25:20 Please remove the last two DCHECK's, and the |IsBi
jingxuy 2016/06/07 01:19:07 Done.
158 packet->at(index) = (packet->at(index) & 0x7F) | (val << 7);
159 }
160
161 void BluetoothLowEnergyWeavePacketGenerator::SetControlCmd(Packet* packet,
162 uint32_t index,
sacomoto 2016/06/06 15:25:21 Please remove the |index| parameter, as it's alway
jingxuy 2016/06/07 01:19:07 Done.
163 uint8_t val) {
sacomoto 2016/06/06 15:25:20 Please pass the ControlCommand enum (see a few com
jingxuy 2016/06/07 01:19:07 Done.
164 DCHECK(packet);
165 DCHECK_LT(index, packet->size());
166 // Currently only commands supported are 0, 1, and 2
167 DCHECK_LT(val, 3);
168 packet->at(index) = (packet->at(index) & 0xF0) | val;
169 }
170
171 void BluetoothLowEnergyWeavePacketGenerator::SetPacketCounter(Packet* packet,
172 uint32_t index) {
sacomoto 2016/06/06 15:25:21 Please remove the |index| parameter, as it's alway
jingxuy 2016/06/07 01:19:07 Done.
173 DCHECK(packet);
174 DCHECK_LT(index, packet->size());
175 uint8_t counter = packet_number_ % 8;
sacomoto 2016/06/06 15:25:20 Please add a kMaxPacketCounter constant, and use i
jingxuy 2016/06/07 01:19:07 Done.
176 packet->at(index) = (packet->at(index) & 0x8F) | (counter << 4);
177 packet_number_++;
178 }
179
180 void BluetoothLowEnergyWeavePacketGenerator::SetDataFirstBit(Packet* packet,
181 uint32_t index,
sacomoto 2016/06/06 15:25:20 Please remove the |index| parameter, as it's alway
jingxuy 2016/06/07 01:19:05 Done.
182 uint8_t val) {
sacomoto 2016/06/06 15:25:20 Please remove the |val| parameter, as this is alwa
jingxuy 2016/06/07 01:19:07 Done.
183 DCHECK(packet);
184 DCHECK_LT(index, packet->size());
185 DCHECK(IsBit(val));
186 packet->at(index) = (packet->at(index) & 0xF7) | (val << 3);
187 }
188
189 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet,
190 uint32_t index,
191 uint8_t val) {
sacomoto 2016/06/06 15:25:20 See the comments above.
jingxuy 2016/06/07 01:19:07 Done.
192 DCHECK(packet);
193 DCHECK_LT(index, packet->size());
194 DCHECK(IsBit(val));
195 packet->at(index) = (packet->at(index) & 0xFB) | (val << 2);
196 }
197
198 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698