OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_genera
tor.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_genera
tor.h" |
6 | 6 |
7 #ifdef OS_WIN | 7 #ifdef OS_WIN |
8 #include <winsock2.h> | 8 #include <winsock2.h> |
9 #else | 9 #else |
10 #include <netinet/in.h> | 10 #include <netinet/in.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <string.h> | 13 #include <string.h> |
14 | 14 |
15 #include <algorithm> | 15 #include <algorithm> |
16 | 16 |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 | 18 |
| 19 namespace proximity_auth { |
| 20 namespace weave { |
19 namespace { | 21 namespace { |
20 | 22 |
21 typedef proximity_auth::BluetoothLowEnergyWeavePacketGenerator::Packet Packet; | |
22 | |
23 const uint16_t kMinSupportedWeaveVersion = 1; | |
24 const uint16_t kMaxSupportedWeaveVersion = 1; | |
25 const uint16_t kServerWeaveVersion = 1; | |
26 const uint16_t kMinConnectionRequestSize = 7; | 23 const uint16_t kMinConnectionRequestSize = 7; |
27 const uint16_t kMinConnectionResponseSize = 5; | 24 const uint16_t kMinConnectionResponseSize = 5; |
28 const uint16_t kMinConnectionCloseSize = 3; | 25 const uint16_t kMinConnectionCloseSize = 3; |
29 const uint32_t kDefaultMaxPacketSize = 20; | |
30 // Max packet size is 0, which means defer the decision to the server. | |
31 const uint16_t kMaxSupportedPacketSize = 0; | |
32 const uint8_t kMaxPacketCounter = 8; | |
33 | 26 |
34 } // namespace | 27 } // namespace |
35 | 28 |
36 namespace proximity_auth { | |
37 | |
38 // static. | 29 // static. |
39 BluetoothLowEnergyWeavePacketGenerator::Factory* | 30 BluetoothLowEnergyWeavePacketGenerator::Factory* |
40 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ = | 31 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ = |
41 nullptr; | 32 nullptr; |
42 | 33 |
43 // static. | 34 // static. |
44 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> | 35 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> |
45 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() { | 36 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() { |
46 if (factory_instance_ == nullptr) { | 37 if (factory_instance_ == nullptr) { |
47 factory_instance_ = new Factory(); | 38 factory_instance_ = new Factory(); |
(...skipping 17 matching lines...) Expand all Loading... |
65 : max_packet_size_(kDefaultMaxPacketSize), next_packet_counter_(0) {} | 56 : max_packet_size_(kDefaultMaxPacketSize), next_packet_counter_(0) {} |
66 | 57 |
67 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() { | 58 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() { |
68 Packet packet(kMinConnectionRequestSize, 0); | 59 Packet packet(kMinConnectionRequestSize, 0); |
69 | 60 |
70 SetPacketTypeBit(PacketType::CONTROL, &packet); | 61 SetPacketTypeBit(PacketType::CONTROL, &packet); |
71 // Since it only make sense for connection request to be the 0th packet, | 62 // Since it only make sense for connection request to be the 0th packet, |
72 // resets the packet counter. | 63 // resets the packet counter. |
73 next_packet_counter_ = 1; | 64 next_packet_counter_ = 1; |
74 SetControlCommand(ControlCommand::CONNECTION_REQUEST, &packet); | 65 SetControlCommand(ControlCommand::CONNECTION_REQUEST, &packet); |
75 SetShortField(1, kMinSupportedWeaveVersion, &packet); | 66 SetShortField(1, kWeaveVersion, &packet); |
76 SetShortField(3, kMaxSupportedWeaveVersion, &packet); | 67 SetShortField(3, kWeaveVersion, &packet); |
77 SetShortField(5, kMaxSupportedPacketSize, &packet); | 68 SetShortField(5, kSelectMaxPacketSize, &packet); |
78 | 69 |
79 return packet; | 70 return packet; |
80 } | 71 } |
81 | 72 |
82 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() { | 73 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() { |
83 Packet packet(kMinConnectionResponseSize, 0); | 74 Packet packet(kMinConnectionResponseSize, 0); |
84 | 75 |
85 SetPacketTypeBit(PacketType::CONTROL, &packet); | 76 SetPacketTypeBit(PacketType::CONTROL, &packet); |
86 // Since it only make sense for connection response to be the 0th packet, | 77 // Since it only make sense for connection response to be the 0th packet, |
87 // resets the next packet counter. | 78 // resets the next packet counter. |
88 next_packet_counter_ = 1; | 79 next_packet_counter_ = 1; |
89 SetControlCommand(ControlCommand::CONNECTION_RESPONSE, &packet); | 80 SetControlCommand(ControlCommand::CONNECTION_RESPONSE, &packet); |
90 SetShortField(1, kServerWeaveVersion, &packet); | 81 SetShortField(1, kWeaveVersion, &packet); |
91 SetShortField(3, max_packet_size_, &packet); | 82 SetShortField(3, max_packet_size_, &packet); |
92 | 83 |
93 return packet; | 84 return packet; |
94 } | 85 } |
95 | 86 |
96 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose( | 87 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose( |
97 ReasonForClose reason_for_close) { | 88 ReasonForClose reason_for_close) { |
98 Packet packet(kMinConnectionCloseSize, 0); | 89 Packet packet(kMinConnectionCloseSize, 0); |
99 | 90 |
100 SetPacketTypeBit(PacketType::CONTROL, &packet); | 91 SetPacketTypeBit(PacketType::CONTROL, &packet); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 196 } |
206 | 197 |
207 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) { | 198 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) { |
208 DCHECK(packet); | 199 DCHECK(packet); |
209 DCHECK(!packet->empty()); | 200 DCHECK(!packet->empty()); |
210 | 201 |
211 // Last bit is the bit 2 of the packet's first byte and set it to 1. | 202 // Last bit is the bit 2 of the packet's first byte and set it to 1. |
212 packet->at(0) = packet->at(0) | (1 << 2); | 203 packet->at(0) = packet->at(0) | (1 << 2); |
213 } | 204 } |
214 | 205 |
| 206 } // namespace weave |
215 } // namespace proximity_auth | 207 } // namespace proximity_auth |
OLD | NEW |