| 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 class BluetoothLowEnergyWeavePacketGenerator { |
| 18 public: |
| 19 class Factory { |
| 20 public: |
| 21 static std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> |
| 22 NewInstance(); |
| 23 |
| 24 // Exposed for testing. |
| 25 static void SetInstanceForTesting(Factory* factory); |
| 26 |
| 27 protected: |
| 28 // Exposed for testing. |
| 29 BluetoothLowEnergyWeavePacketGenerator* BuildInstance(); |
| 30 |
| 31 private: |
| 32 static Factory* factory_instance_; |
| 33 }; |
| 34 |
| 35 // Determine whether a packet is a control packet or a data packet. |
| 36 enum PacketType { DATA = 0x00, CONTROL = 0x01 }; |
| 37 |
| 38 // Identify the action intended by the control packet. |
| 39 enum ControlPacketCommand { |
| 40 CONNECTION_REQUEST = 0x00, |
| 41 CONNECTION_RESPONSE = 0x01, |
| 42 CONNECTION_CLOSE = 0x02 |
| 43 }; |
| 44 |
| 45 // Sent with the ConnectionClose control packet. |
| 46 // Identify why the client/server wished to close the connection. |
| 47 enum ReasonForClose { |
| 48 CLOSE_WITHOUT_ERROR = 0x00, |
| 49 UNKNOWN_ERROR = 0x01, |
| 50 NO_COMMON_VERSION_SUPPORTED = 0x02, |
| 51 RECEIVED_PACKET_OUT_OF_SEQUENCE = 0x03, |
| 52 APPLICATION_ERROR = 0x80 |
| 53 }; |
| 54 |
| 55 typedef std::vector<uint8_t> Packet; |
| 56 |
| 57 std::unique_ptr<Packet> CreateConnectionRequest(); |
| 58 std::unique_ptr<Packet> CreateConnectionResponse(); |
| 59 std::unique_ptr<Packet> CreateConnectionClose( |
| 60 ReasonForClose reason_for_close); |
| 61 |
| 62 void SetDataPacketSize(uint32_t size); |
| 63 |
| 64 std::unique_ptr<std::vector<Packet>> EncodeDataMessage(std::string message); |
| 65 |
| 66 protected: |
| 67 BluetoothLowEnergyWeavePacketGenerator(); |
| 68 |
| 69 private: |
| 70 Packet* CreateControlPacket(); |
| 71 |
| 72 // For error checking. |
| 73 bool IsBit(uint8_t val); |
| 74 |
| 75 void SetShortField(Packet* packet, uint32_t index, uint16_t val); |
| 76 void SetPacketTypeBit(Packet* packet, PacketType val); |
| 77 void SetControlCommand(Packet* packet, ControlPacketCommand val); |
| 78 void SetPacketCounter(Packet* packet); |
| 79 void SetDataFirstBit(Packet* packet); |
| 80 void SetDataLastBit(Packet* packet); |
| 81 |
| 82 // The default data packet length is 20 unless setDataPacketLength() is called |
| 83 // and specified otherwise. |
| 84 uint32_t packet_size_; |
| 85 |
| 86 // Counter for the number of packets sent, starting at 0. |
| 87 uint32_t packet_number_; |
| 88 }; |
| 89 |
| 90 } // namespace proximity_auth |
| 91 |
| 92 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENER
ATOR_H_ |
| OLD | NEW |