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