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