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

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_weave_packet_generator.cc

Issue 2561203002: Migrate weave-related classes from proximity_auth/ble to cryptauth/ble. (Closed)
Patch Set: Rebase. Created 4 years 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 #ifdef OS_WIN
8 #include <winsock2.h>
9 #else
10 #include <netinet/in.h>
11 #endif
12
13 #include <string.h>
14
15 #include <algorithm>
16
17 #include "base/logging.h"
18
19 namespace proximity_auth {
20 namespace weave {
21
22 // static.
23 std::shared_ptr<BluetoothLowEnergyWeavePacketGenerator::Factory>
24 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ =
25 nullptr;
26
27 // static.
28 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
29 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() {
30 if (!factory_instance_) {
31 factory_instance_.reset(new Factory());
32 }
33 return factory_instance_->BuildInstance();
34 }
35
36 // static.
37 void BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting(
38 std::shared_ptr<Factory> factory) {
39 factory_instance_ = factory;
40 }
41
42 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>
43 BluetoothLowEnergyWeavePacketGenerator::Factory::BuildInstance() {
44 return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>(
45 new BluetoothLowEnergyWeavePacketGenerator());
46 }
47
48 BluetoothLowEnergyWeavePacketGenerator::BluetoothLowEnergyWeavePacketGenerator()
49 : max_packet_size_(kDefaultMaxPacketSize), next_packet_counter_(0) {}
50
51 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() {
52 Packet packet(kMinConnectionRequestSize, 0);
53
54 SetPacketTypeBit(PacketType::CONTROL, &packet);
55 // Since it only make sense for connection request to be the 0th packet,
56 // resets the packet counter.
57 next_packet_counter_ = 1;
58 SetControlCommand(ControlCommand::CONNECTION_REQUEST, &packet);
59 SetShortField(1, kWeaveVersion, &packet);
60 SetShortField(3, kWeaveVersion, &packet);
61 SetShortField(5, kSelectMaxPacketSize, &packet);
62
63 return packet;
64 }
65
66 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() {
67 Packet packet(kMinConnectionResponseSize, 0);
68
69 SetPacketTypeBit(PacketType::CONTROL, &packet);
70 // Since it only make sense for connection response to be the 0th packet,
71 // resets the next packet counter.
72 next_packet_counter_ = 1;
73 SetControlCommand(ControlCommand::CONNECTION_RESPONSE, &packet);
74 SetShortField(1, kWeaveVersion, &packet);
75 SetShortField(3, max_packet_size_, &packet);
76
77 return packet;
78 }
79
80 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose(
81 ReasonForClose reason_for_close) {
82 Packet packet(kMaxConnectionCloseSize, 0);
83
84 SetPacketTypeBit(PacketType::CONTROL, &packet);
85 SetPacketCounter(&packet);
86 SetControlCommand(ControlCommand::CONNECTION_CLOSE, &packet);
87 SetShortField(1, reason_for_close, &packet);
88
89 return packet;
90 }
91
92 void BluetoothLowEnergyWeavePacketGenerator::SetMaxPacketSize(uint16_t size) {
93 DCHECK(size >= kDefaultMaxPacketSize);
94 max_packet_size_ = size;
95 }
96
97 std::vector<Packet> BluetoothLowEnergyWeavePacketGenerator::EncodeDataMessage(
98 std::string message) {
99 DCHECK(!message.empty());
100
101 // The first byte of a packet is used by the uWeave protocol,
102 // hence the payload is 1 byte smaller than the packet size.
103 uint32_t packet_payload_size = max_packet_size_ - 1;
104
105 uint32_t message_length = message.length();
106 // (packet_payload_size - 1) is used to enforce rounding up.
107 uint32_t num_packets =
108 (message_length + (packet_payload_size - 1)) / packet_payload_size;
109
110 std::vector<Packet> weave_message(num_packets);
111
112 const char* byte_message = message.c_str();
113
114 for (uint32_t i = 0; i < num_packets; ++i) {
115 Packet* packet = &weave_message[i];
116 uint32_t begin = packet_payload_size * i;
117 uint32_t end = std::min(begin + packet_payload_size, message_length);
118
119 packet->push_back(0);
120
121 SetPacketTypeBit(PacketType::DATA, packet);
122 SetPacketCounter(packet);
123
124 for (uint32_t j = begin; j < end; ++j) {
125 packet->push_back(byte_message[j]);
126 }
127 }
128
129 // Guaranteed to have at least one packet since message is not empty.
130 SetDataFirstBit(&weave_message[0]);
131 SetDataLastBit(&weave_message[num_packets - 1]);
132
133 return weave_message;
134 }
135
136 void BluetoothLowEnergyWeavePacketGenerator::SetShortField(uint32_t byte_offset,
137 uint16_t val,
138 Packet* packet) {
139 DCHECK(packet);
140 DCHECK_LT(byte_offset, packet->size());
141 DCHECK_LT(byte_offset + 1, packet->size());
142
143 uint16_t network_val = htons(val);
144 uint8_t* network_val_ptr = reinterpret_cast<uint8_t*>(&network_val);
145
146 packet->at(byte_offset) = network_val_ptr[0];
147 packet->at(byte_offset + 1) = network_val_ptr[1];
148 }
149
150 void BluetoothLowEnergyWeavePacketGenerator::SetPacketTypeBit(PacketType type,
151 Packet* packet) {
152 DCHECK(packet);
153 DCHECK(!packet->empty());
154
155 // Type bit is the highest bit of the first byte of the packet.
156 // So clear the highest bit and set it according to val.
157 packet->at(0) = (packet->at(0) & 0x7F) | (type << 7);
158 }
159
160 void BluetoothLowEnergyWeavePacketGenerator::SetControlCommand(
161 ControlCommand command,
162 Packet* packet) {
163 DCHECK(packet);
164 DCHECK(!packet->empty());
165
166 // Control Command is the lower 4 bits of the packet's first byte.
167 // So clear the lower 4 bites and set it according to val.
168 packet->at(0) = (packet->at(0) & 0xF0) | command;
169 }
170
171 void BluetoothLowEnergyWeavePacketGenerator::SetPacketCounter(Packet* packet) {
172 DCHECK(packet);
173 DCHECK(!packet->empty());
174 uint8_t counter = next_packet_counter_ % kMaxPacketCounter;
175
176 // Packet counter is the bits 4, 5, and 6 of the packet's first byte.
177 // So clear those bits and set them according to current packet counter
178 // modular max packet counter.
179 packet->at(0) = (packet->at(0) & 0x8F) | (counter << 4);
180 next_packet_counter_++;
181 }
182
183 void BluetoothLowEnergyWeavePacketGenerator::SetDataFirstBit(Packet* packet) {
184 DCHECK(packet);
185 DCHECK(!packet->empty());
186
187 // First bit is bit 3 of the packet's first byte and set it to 1.
188 packet->at(0) = packet->at(0) | (1 << 3);
189 }
190
191 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) {
192 DCHECK(packet);
193 DCHECK(!packet->empty());
194
195 // Last bit is the bit 2 of the packet's first byte and set it to 1.
196 packet->at(0) = packet->at(0) | (1 << 2);
197 }
198
199 } // namespace weave
200
201 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698