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