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 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENERATO R_H_ | |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENERATO R_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 namespace proximity_auth { | |
| 16 // Generates the messages sent using the uWeave protocol. | |
| 17 // Internally tracks the packet number. | |
| 18 class BluetoothLowEnergyWeavePacketGenerator { | |
| 19 public: | |
| 20 class Factory { | |
| 21 public: | |
| 22 static std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> | |
| 23 NewInstance(); | |
| 24 | |
| 25 // Exposed for testing. | |
| 26 static void SetInstanceForTesting(Factory* factory); | |
| 27 | |
| 28 protected: | |
| 29 // Exposed for testing. | |
| 30 BluetoothLowEnergyWeavePacketGenerator* BuildInstance(); | |
| 31 | |
| 32 private: | |
| 33 static Factory* factory_instance_; | |
| 34 }; | |
| 35 | |
| 36 enum PacketType { DATA = 0x00, CONTROL = 0x01 }; | |
| 37 | |
| 38 enum ControlPacketCommand { REQUEST = 0x00, RESPONSE = 0x01, CLOSE = 0x02 }; | |
|
Kyle Horimoto
2016/06/08 00:06:30
Prefix these with CONNECTION_.
| |
| 39 | |
| 40 enum ReasonForClose { | |
| 41 CLOSE_WITHOUT_ERROR = 0x00, | |
| 42 UNKNOWN_ERROR = 0x01, | |
| 43 NO_COMMON_VERSION_SUPPORTED = 0x02, | |
| 44 RECEIVED_PACKET_OUT_OF_SEQUENCE = 0x03, | |
| 45 APPLICATION_ERROR = 0x80 | |
| 46 }; | |
| 47 | |
| 48 typedef std::vector<uint8_t> Packet; | |
| 49 | |
| 50 std::unique_ptr<Packet> CreateConnectionRequest(); | |
| 51 std::unique_ptr<Packet> CreateConnectionResponse(); | |
| 52 std::unique_ptr<Packet> CreateConnectionClose( | |
| 53 ReasonForClose reason_for_close); | |
| 54 | |
| 55 void SetDataPacketSize(uint32_t size); | |
| 56 | |
| 57 std::unique_ptr<std::vector<Packet>> EncodeDataMessage(std::string message); | |
| 58 | |
| 59 protected: | |
| 60 BluetoothLowEnergyWeavePacketGenerator(); | |
| 61 | |
| 62 private: | |
| 63 static const uint16_t kMinSupportedWeaveVersion = 1; | |
| 64 static const uint16_t kMaxSupportedWeaveVersion = 1; | |
| 65 static const uint16_t kServerWeaveVersion = 1; | |
| 66 static const uint16_t kControlPacketSize = 20; | |
| 67 static const uint32_t kDefaultDataPacketSize = 20; | |
| 68 | |
| 69 // Max packet size is 0, which means defer the decision to the server. | |
| 70 static const uint16_t kMaxSupportedPacketSize = 0; | |
| 71 static const uint8_t kMaxPacketCounter = 8; | |
| 72 | |
| 73 Packet* CreateControlPacket(); | |
| 74 | |
| 75 // For error checking. | |
| 76 bool IsBit(uint8_t val); | |
| 77 | |
| 78 void SetIntField(Packet* packet, uint32_t index, uint16_t val); | |
| 79 void SetPacketTypeBit(Packet* packet, bool val); | |
| 80 void SetControlCmd(Packet* packet, uint8_t val); | |
| 81 void SetPacketCounter(Packet* packet); | |
| 82 void SetDataFirstBit(Packet* packet); | |
| 83 void SetDataLastBit(Packet* packet); | |
| 84 | |
| 85 // The default data packet length is 20 unless setDataPacketLength() is called | |
| 86 // and specified otherwise. | |
| 87 uint32_t packet_size_; | |
| 88 | |
| 89 // Counter for the number of packets sent, starting at 0. | |
| 90 uint32_t packet_number_; | |
| 91 }; | |
| 92 | |
| 93 } // namespace proximity_auth | |
| 94 | |
| 95 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENER ATOR_H_ | |
| OLD | NEW |