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_receiv er.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
|
Tim Song
2016/06/09 22:08:34
Please use "components/proximity_auth/logging.h"
jingxuy
2016/06/09 23:29:03
base/logging is where DCHECK is defined, should I
Tim Song
2016/06/10 21:25:21
Okay, it seems like you don't do any logging, so t
jingxuy
2016/06/11 00:10:46
oh actually I need to do some logging and should I
Tim Song
2016/06/11 00:39:45
The proximity_auth specific logger PA_LOG is a wra
jingxuy
2016/06/17 18:59:51
Done.
| |
| 8 | |
| 9 using proximity_auth::BluetoothLowEnergyWeavePacketGenerator; | |
| 10 | |
| 11 namespace proximity_auth { | |
| 12 namespace { | |
| 13 typedef BluetoothLowEnergyWeavePacketGenerator::PacketType PacketType; | |
| 14 typedef BluetoothLowEnergyWeavePacketGenerator::ControlPacketCommand | |
| 15 ControlPacketCommand; | |
| 16 typedef BluetoothLowEnergyWeavePacketGenerator::ReasonForClose ReasonForClose; | |
| 17 | |
| 18 const uint8_t kMaxPacketCounter = 8; | |
| 19 } // namespace | |
| 20 | |
| 21 BluetoothLowEnergyWeavePacketReceiver::Factory* | |
| 22 BluetoothLowEnergyWeavePacketReceiver::Factory::factory_instance_ = nullptr; | |
| 23 | |
| 24 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> | |
| 25 BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance() { | |
| 26 if (factory_instance_ == nullptr) { | |
| 27 factory_instance_ = new Factory(); | |
| 28 } | |
| 29 return std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver>( | |
| 30 factory_instance_->BuildInstance()); | |
| 31 } | |
| 32 | |
| 33 void BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( | |
| 34 Factory* factory) { | |
| 35 factory_instance_ = factory; | |
| 36 } | |
| 37 | |
| 38 BluetoothLowEnergyWeavePacketReceiver* | |
| 39 BluetoothLowEnergyWeavePacketReceiver::Factory::BuildInstance() { | |
| 40 return new BluetoothLowEnergyWeavePacketReceiver(); | |
| 41 } | |
| 42 | |
| 43 BluetoothLowEnergyWeavePacketReceiver::BluetoothLowEnergyWeavePacketReceiver() | |
| 44 : packet_size_(0), | |
| 45 packet_number_(0), | |
| 46 state_(State::EXCHANGING_CONTROLS), | |
| 47 reason_for_close_(ReasonForClose::CLOSE_WITHOUT_ERROR) {} | |
| 48 | |
| 49 BluetoothLowEnergyWeavePacketReceiver:: | |
| 50 ~BluetoothLowEnergyWeavePacketReceiver() {} | |
| 51 | |
| 52 BluetoothLowEnergyWeavePacketReceiver::State | |
| 53 BluetoothLowEnergyWeavePacketReceiver::GetState() { | |
| 54 return state_; | |
| 55 } | |
| 56 | |
| 57 uint32_t BluetoothLowEnergyWeavePacketReceiver::GetPacketSize() { | |
| 58 return packet_size_; | |
| 59 } | |
| 60 | |
| 61 ReasonForClose BluetoothLowEnergyWeavePacketReceiver::GetReasonForClose() { | |
| 62 return reason_for_close_; | |
| 63 } | |
| 64 | |
| 65 std::string BluetoothLowEnergyWeavePacketReceiver::GetDataMessage() { | |
| 66 return std::string(data_message_.begin(), data_message_.end()); | |
|
Tim Song
2016/06/09 22:08:35
Check if state_ == DATA_READY?
jingxuy
2016/06/09 23:29:03
This entire section has been rewritten.
| |
| 67 } | |
| 68 | |
| 69 void BluetoothLowEnergyWeavePacketReceiver::ReceivePacket( | |
| 70 const Packet& packet) { | |
| 71 if (state_ == State::ERROR) { | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 VerifyPacketCounter(packet); | |
| 76 | |
| 77 if (state_ == State::ERROR) { | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 PacketType type = GetPacketType(packet); | |
| 82 | |
| 83 switch (type) { | |
| 84 case PacketType::CONTROL: | |
| 85 switch (GetControlCommand(packet)) { | |
|
Tim Song
2016/06/09 22:08:34
Please refactor this switch to its own function.
jingxuy
2016/06/09 23:29:03
Done.
| |
| 86 case ControlPacketCommand::CONNECTION_REQUEST: | |
| 87 if (state_ == State::RECEIVING_DATA) { | |
|
Tim Song
2016/06/09 22:08:35
Shouldn't this be checking that state_ != EXCHANGI
jingxuy
2016/06/09 23:29:03
Done.
| |
| 88 state_ = State::ERROR; | |
| 89 return; | |
| 90 } | |
| 91 // Currently just ignoring the version stuff because it's always 1. | |
| 92 packet_size_ = GetShortField(packet, 5); | |
| 93 // TODO(jingxuy): what do I do with the 13 bytes of data? | |
|
Tim Song
2016/06/09 22:08:34
I don't think you'll get any extra data. The packe
jingxuy
2016/06/09 23:29:03
This is some uWeave stuff that all control packets
Tim Song
2016/06/10 21:25:21
The Android and Mac implementations do not append
jingxuy
2016/06/11 00:10:46
yah, I think worrying about it actually creates mo
| |
| 94 break; | |
| 95 case ControlPacketCommand::CONNECTION_RESPONSE: | |
| 96 if (state_ == State::RECEIVING_DATA) { | |
|
Tim Song
2016/06/09 22:08:35
shouldn't this be checking that state_ != EXCHANGI
jingxuy
2016/06/09 23:29:03
Done.
| |
| 97 state_ = State::ERROR; | |
| 98 return; | |
| 99 } | |
| 100 // Currently just ignoring the version stuff because it's always 1. | |
| 101 packet_size_ = GetShortField(packet, 3); | |
| 102 // TODO(jingxuy): what do I do with the 15 bytes of data? | |
|
Tim Song
2016/06/09 22:08:34
same here.
jingxuy
2016/06/09 23:29:03
Done.
| |
| 103 break; | |
| 104 case ControlPacketCommand::CONNECTION_CLOSE: | |
|
Tim Song
2016/06/09 22:08:35
check that the current state is valid.
jingxuy
2016/06/09 23:29:03
Done.
| |
| 105 reason_for_close_ = | |
| 106 static_cast<ReasonForClose>(GetShortField(packet, 1)); | |
| 107 state_ = State::CONNECTION_CLOSED; | |
| 108 break; | |
| 109 default: | |
| 110 state_ = State::ERROR; | |
| 111 return; | |
| 112 } | |
| 113 break; | |
| 114 case PacketType::DATA: | |
|
Tim Song
2016/06/09 22:08:34
Also refactor this block into its own function.
jingxuy
2016/06/09 23:29:04
Done.
| |
| 115 if (IsFirstDataPacket(packet)) { | |
| 116 if (state_ != State::EXCHANGING_CONTROLS) { | |
|
Tim Song
2016/06/09 22:08:34
Shouldn't this state you check here be DATA_READY?
jingxuy
2016/06/09 23:29:03
Done.
| |
| 117 state_ = State::ERROR; | |
| 118 return; | |
| 119 } | |
| 120 data_message_.clear(); | |
| 121 state_ = State::RECEIVING_DATA; | |
| 122 } | |
| 123 | |
| 124 if (state_ != State::RECEIVING_DATA) { | |
| 125 state_ = State::ERROR; | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 if (IsLastDataPacket(packet)) { | |
| 130 state_ = State::DATA_READY; | |
| 131 } | |
| 132 | |
| 133 // Append to data_message_ bytes 1 through end of the packet. | |
| 134 data_message_.insert(data_message_.end(), packet.begin() + 1, | |
| 135 packet.end()); | |
| 136 break; | |
| 137 default: | |
| 138 state_ = State::ERROR; | |
| 139 return; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 uint16_t BluetoothLowEnergyWeavePacketReceiver::GetShortField( | |
| 144 const Packet& packet, | |
| 145 uint32_t index) { | |
| 146 DCHECK_LT(index, packet.size()); | |
|
Tim Song
2016/06/09 22:08:34
I don't think you should DCHECK this condition, as
jingxuy
2016/06/09 23:29:03
This is an internal function. I'm just dchecking t
| |
| 147 DCHECK_LT(index + 1, packet.size()); | |
| 148 | |
| 149 // packet[index + 1] is the upper byte and packet[index] is the lower byte. | |
| 150 return (packet[index + 1] << 8) | packet[index]; | |
| 151 } | |
| 152 | |
| 153 PacketType BluetoothLowEnergyWeavePacketReceiver::GetPacketType( | |
| 154 const Packet& packet) { | |
| 155 // Packet type is stored in the highest bit of the first byte. | |
| 156 return static_cast<PacketType>((packet[0] >> 7) & 1); | |
| 157 } | |
| 158 | |
| 159 ControlPacketCommand BluetoothLowEnergyWeavePacketReceiver::GetControlCommand( | |
| 160 const Packet& packet) { | |
| 161 // Control command is stored in the lower 4 bits of the first byte. | |
| 162 return static_cast<ControlPacketCommand>((packet[0] & 0x0F)); | |
| 163 } | |
| 164 | |
| 165 void BluetoothLowEnergyWeavePacketReceiver::VerifyPacketCounter( | |
| 166 const Packet& packet) { | |
| 167 // Packet counter is bits 4, 5, and 6 of the first byte. | |
| 168 uint8_t count = (packet[0] >> 4) & 7; | |
| 169 | |
| 170 if (count != (packet_number_ % kMaxPacketCounter)) { | |
| 171 state_ = State::ERROR; | |
| 172 return; | |
| 173 } | |
| 174 packet_number_++; | |
| 175 } | |
| 176 | |
| 177 bool BluetoothLowEnergyWeavePacketReceiver::IsFirstDataPacket( | |
| 178 const Packet& packet) { | |
| 179 // Bit determining whether the packet is first pacet is bit 3. | |
|
Tim Song
2016/06/09 22:08:35
nit: s/pacet/packet/
jingxuy
2016/06/09 23:29:04
Done.
| |
| 180 return (packet[0] >> 3) & 1; | |
| 181 } | |
| 182 | |
| 183 bool BluetoothLowEnergyWeavePacketReceiver::IsLastDataPacket( | |
| 184 const Packet& packet) { | |
| 185 // Bit determining whether the packet is first pacet is bit 2. | |
| 186 return (packet[0] >> 2) & 1; | |
| 187 } | |
| 188 | |
| 189 } // namespace proximity_auth | |
| OLD | NEW |