| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENERATO
R_H_ | 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_ | 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENERATO
R_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 | 17 |
| 17 namespace proximity_auth { | 18 namespace proximity_auth { |
| 19 |
| 18 // Generates the messages sent using the uWeave protocol. | 20 // Generates the messages sent using the uWeave protocol. |
| 19 class BluetoothLowEnergyWeavePacketGenerator { | 21 class BluetoothLowEnergyWeavePacketGenerator { |
| 20 public: | 22 public: |
| 21 class Factory { | 23 class Factory { |
| 22 public: | 24 public: |
| 23 static std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> | 25 static std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> |
| 24 NewInstance(); | 26 NewInstance(); |
| 25 | 27 |
| 26 // Exposed for testing. | 28 // Exposed for testing. |
| 27 static void SetInstanceForTesting(Factory* factory); | 29 static void SetInstanceForTesting(Factory* factory); |
| 28 | 30 |
| 29 protected: | 31 protected: |
| 30 // Exposed for testing. | 32 // Exposed for testing. |
| 31 virtual std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> | 33 virtual std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> |
| 32 BuildInstance(); | 34 BuildInstance(); |
| 33 | 35 |
| 34 private: | 36 private: |
| 35 static Factory* factory_instance_; | 37 static Factory* factory_instance_; |
| 36 }; | 38 }; |
| 37 | 39 |
| 38 // Determine whether a packet is a control packet or a data packet. | 40 BluetoothLowEnergyWeavePacket::Packet CreateConnectionRequest(); |
| 39 enum PacketType { DATA = 0x00, CONTROL = 0x01 }; | 41 BluetoothLowEnergyWeavePacket::Packet CreateConnectionResponse(); |
| 40 | 42 BluetoothLowEnergyWeavePacket::Packet CreateConnectionClose( |
| 41 // Identify the action intended by the control packet. | 43 BluetoothLowEnergyWeavePacket::ReasonForClose reason_for_close); |
| 42 enum ControlCommand { | |
| 43 CONNECTION_REQUEST = 0x00, | |
| 44 CONNECTION_RESPONSE = 0x01, | |
| 45 CONNECTION_CLOSE = 0x02 | |
| 46 }; | |
| 47 | |
| 48 // Sent with the ConnectionClose control packet. | |
| 49 // Identify why the client/server wished to close the connection. | |
| 50 enum ReasonForClose { | |
| 51 CLOSE_WITHOUT_ERROR = 0x00, | |
| 52 UNKNOWN_ERROR = 0x01, | |
| 53 NO_COMMON_VERSION_SUPPORTED = 0x02, | |
| 54 RECEIVED_PACKET_OUT_OF_SEQUENCE = 0x03, | |
| 55 APPLICATION_ERROR = 0x80 | |
| 56 }; | |
| 57 | |
| 58 typedef std::vector<uint8_t> Packet; | |
| 59 | |
| 60 Packet CreateConnectionRequest(); | |
| 61 Packet CreateConnectionResponse(); | |
| 62 Packet CreateConnectionClose(ReasonForClose reason_for_close); | |
| 63 | 44 |
| 64 // Packet size must be greater than or equal to 20. | 45 // Packet size must be greater than or equal to 20. |
| 65 void SetMaxPacketSize(uint32_t size); | 46 void SetMaxPacketSize(uint32_t size); |
| 66 | 47 |
| 67 // Will crash if message is empty. | 48 // Will crash if message is empty. |
| 68 std::vector<Packet> EncodeDataMessage(std::string message); | 49 std::vector<BluetoothLowEnergyWeavePacket::Packet> EncodeDataMessage( |
| 50 std::string message); |
| 69 | 51 |
| 70 protected: | 52 protected: |
| 71 BluetoothLowEnergyWeavePacketGenerator(); | 53 BluetoothLowEnergyWeavePacketGenerator(); |
| 72 | 54 |
| 73 private: | 55 private: |
| 74 void SetShortField(uint32_t byte_offset, uint16_t val, Packet* packet); | 56 void SetShortField(uint32_t byte_offset, |
| 75 void SetPacketTypeBit(PacketType val, Packet* packet); | 57 uint16_t val, |
| 76 void SetControlCommand(ControlCommand val, Packet* packet); | 58 BluetoothLowEnergyWeavePacket::Packet* packet); |
| 77 void SetPacketCounter(Packet* packet); | 59 void SetPacketTypeBit(BluetoothLowEnergyWeavePacket::PacketType val, |
| 78 void SetDataFirstBit(Packet* packet); | 60 BluetoothLowEnergyWeavePacket::Packet* packet); |
| 79 void SetDataLastBit(Packet* packet); | 61 void SetControlCommand(BluetoothLowEnergyWeavePacket::ControlCommand val, |
| 62 BluetoothLowEnergyWeavePacket::Packet* packet); |
| 63 void SetPacketCounter(BluetoothLowEnergyWeavePacket::Packet* packet); |
| 64 void SetDataFirstBit(BluetoothLowEnergyWeavePacket::Packet* packet); |
| 65 void SetDataLastBit(BluetoothLowEnergyWeavePacket::Packet* packet); |
| 80 | 66 |
| 81 // The default max packet length is 20 unless SetDataPacketLength() is called | 67 // The default max packet length is 20 unless SetDataPacketLength() is called |
| 82 // and specified otherwise. | 68 // and specified otherwise. |
| 83 uint32_t max_packet_size_; | 69 uint32_t max_packet_size_; |
| 84 | 70 |
| 85 // Counter for the number of packets sent, starting at 0. | 71 // Counter for the number of packets sent, starting at 0. |
| 86 uint32_t next_packet_counter_; | 72 uint32_t next_packet_counter_; |
| 87 }; | 73 }; |
| 88 | 74 |
| 89 } // namespace proximity_auth | 75 } // namespace proximity_auth |
| 90 | 76 |
| 91 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENER
ATOR_H_ | 77 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_GENER
ATOR_H_ |
| OLD | NEW |