| 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 { | |
| 20 | |
| 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; | |
| 27 const uint16_t kMinConnectionResponseSize = 5; | |
| 28 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 | |
| 34 } // namespace | |
| 35 | |
| 36 namespace proximity_auth { | 19 namespace proximity_auth { |
| 20 namespace weave { |
| 37 | 21 |
| 38 // static. | 22 // static. |
| 39 BluetoothLowEnergyWeavePacketGenerator::Factory* | 23 BluetoothLowEnergyWeavePacketGenerator::Factory* |
| 40 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ = | 24 BluetoothLowEnergyWeavePacketGenerator::Factory::factory_instance_ = |
| 41 nullptr; | 25 nullptr; |
| 42 | 26 |
| 43 // static. | 27 // static. |
| 44 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> | 28 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> |
| 45 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() { | 29 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance() { |
| 46 if (factory_instance_ == nullptr) { | 30 if (factory_instance_ == nullptr) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 65 : max_packet_size_(kDefaultMaxPacketSize), next_packet_counter_(0) {} | 49 : max_packet_size_(kDefaultMaxPacketSize), next_packet_counter_(0) {} |
| 66 | 50 |
| 67 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() { | 51 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionRequest() { |
| 68 Packet packet(kMinConnectionRequestSize, 0); | 52 Packet packet(kMinConnectionRequestSize, 0); |
| 69 | 53 |
| 70 SetPacketTypeBit(PacketType::CONTROL, &packet); | 54 SetPacketTypeBit(PacketType::CONTROL, &packet); |
| 71 // Since it only make sense for connection request to be the 0th packet, | 55 // Since it only make sense for connection request to be the 0th packet, |
| 72 // resets the packet counter. | 56 // resets the packet counter. |
| 73 next_packet_counter_ = 1; | 57 next_packet_counter_ = 1; |
| 74 SetControlCommand(ControlCommand::CONNECTION_REQUEST, &packet); | 58 SetControlCommand(ControlCommand::CONNECTION_REQUEST, &packet); |
| 75 SetShortField(1, kMinSupportedWeaveVersion, &packet); | 59 SetShortField(1, kWeaveVersion, &packet); |
| 76 SetShortField(3, kMaxSupportedWeaveVersion, &packet); | 60 SetShortField(3, kWeaveVersion, &packet); |
| 77 SetShortField(5, kMaxSupportedPacketSize, &packet); | 61 SetShortField(5, kSelectMaxPacketSize, &packet); |
| 78 | 62 |
| 79 return packet; | 63 return packet; |
| 80 } | 64 } |
| 81 | 65 |
| 82 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() { | 66 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionResponse() { |
| 83 Packet packet(kMinConnectionResponseSize, 0); | 67 Packet packet(kMinConnectionResponseSize, 0); |
| 84 | 68 |
| 85 SetPacketTypeBit(PacketType::CONTROL, &packet); | 69 SetPacketTypeBit(PacketType::CONTROL, &packet); |
| 86 // Since it only make sense for connection response to be the 0th packet, | 70 // Since it only make sense for connection response to be the 0th packet, |
| 87 // resets the next packet counter. | 71 // resets the next packet counter. |
| 88 next_packet_counter_ = 1; | 72 next_packet_counter_ = 1; |
| 89 SetControlCommand(ControlCommand::CONNECTION_RESPONSE, &packet); | 73 SetControlCommand(ControlCommand::CONNECTION_RESPONSE, &packet); |
| 90 SetShortField(1, kServerWeaveVersion, &packet); | 74 SetShortField(1, kWeaveVersion, &packet); |
| 91 SetShortField(3, max_packet_size_, &packet); | 75 SetShortField(3, max_packet_size_, &packet); |
| 92 | 76 |
| 93 return packet; | 77 return packet; |
| 94 } | 78 } |
| 95 | 79 |
| 96 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose( | 80 Packet BluetoothLowEnergyWeavePacketGenerator::CreateConnectionClose( |
| 97 ReasonForClose reason_for_close) { | 81 ReasonForClose reason_for_close) { |
| 98 Packet packet(kMinConnectionCloseSize, 0); | 82 Packet packet(kMaxConnectionCloseSize, 0); |
| 99 | 83 |
| 100 SetPacketTypeBit(PacketType::CONTROL, &packet); | 84 SetPacketTypeBit(PacketType::CONTROL, &packet); |
| 101 SetPacketCounter(&packet); | 85 SetPacketCounter(&packet); |
| 102 SetControlCommand(ControlCommand::CONNECTION_CLOSE, &packet); | 86 SetControlCommand(ControlCommand::CONNECTION_CLOSE, &packet); |
| 103 SetShortField(1, reason_for_close, &packet); | 87 SetShortField(1, reason_for_close, &packet); |
| 104 | 88 |
| 105 return packet; | 89 return packet; |
| 106 } | 90 } |
| 107 | 91 |
| 108 void BluetoothLowEnergyWeavePacketGenerator::SetMaxPacketSize(uint32_t size) { | 92 void BluetoothLowEnergyWeavePacketGenerator::SetMaxPacketSize(uint16_t size) { |
| 109 DCHECK(size >= kDefaultMaxPacketSize); | 93 DCHECK(size >= kDefaultMaxPacketSize); |
| 110 max_packet_size_ = size; | 94 max_packet_size_ = size; |
| 111 } | 95 } |
| 112 | 96 |
| 113 std::vector<Packet> BluetoothLowEnergyWeavePacketGenerator::EncodeDataMessage( | 97 std::vector<Packet> BluetoothLowEnergyWeavePacketGenerator::EncodeDataMessage( |
| 114 std::string message) { | 98 std::string message) { |
| 115 DCHECK(!message.empty()); | 99 DCHECK(!message.empty()); |
| 116 | 100 |
| 117 // The first byte of a packet is used by the uWeave protocol, | 101 // The first byte of a packet is used by the uWeave protocol, |
| 118 // hence the payload is 1 byte smaller than the packet size. | 102 // hence the payload is 1 byte smaller than the packet size. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 189 } |
| 206 | 190 |
| 207 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) { | 191 void BluetoothLowEnergyWeavePacketGenerator::SetDataLastBit(Packet* packet) { |
| 208 DCHECK(packet); | 192 DCHECK(packet); |
| 209 DCHECK(!packet->empty()); | 193 DCHECK(!packet->empty()); |
| 210 | 194 |
| 211 // Last bit is the bit 2 of the packet's first byte and set it to 1. | 195 // 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); | 196 packet->at(0) = packet->at(0) | (1 << 2); |
| 213 } | 197 } |
| 214 | 198 |
| 199 } // namespace weave |
| 200 |
| 215 } // namespace proximity_auth | 201 } // namespace proximity_auth |
| OLD | NEW |