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