| 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_RECEIVER
_H_ |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_RECEIVER
_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_genera
tor.h" |
| 15 |
| 16 namespace proximity_auth { |
| 17 // Receive the messages sent with uWeave protocol. |
| 18 // TODO(jingxuy): move the enums from BluetoothLowEnergyWeavePacketGenerator to |
| 19 // a shared location |
| 20 // TODO(jingxuy): a complete state transition comment |
| 21 class BluetoothLowEnergyWeavePacketReceiver { |
| 22 public: |
| 23 enum ReceiverType { CLIENT, SERVER }; |
| 24 |
| 25 class Factory { |
| 26 public: |
| 27 static std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> NewInstance( |
| 28 ReceiverType receiver_type); |
| 29 |
| 30 // Exposed for testing. |
| 31 static void SetInstanceForTesting(Factory* factory); |
| 32 |
| 33 protected: |
| 34 // Exposed for testing. |
| 35 BluetoothLowEnergyWeavePacketReceiver* BuildInstance( |
| 36 ReceiverType receiver_type); |
| 37 |
| 38 private: |
| 39 static Factory* factory_instance_; |
| 40 }; |
| 41 |
| 42 enum State { |
| 43 CONNECTING = 0x00, |
| 44 WAITING = 0x01, |
| 45 RECEIVING_DATA = 0x02, |
| 46 DATA_READY = 0x03, |
| 47 CONNECTION_CLOSED = 0x04, |
| 48 ERROR = 0x05 |
| 49 }; |
| 50 |
| 51 ~BluetoothLowEnergyWeavePacketReceiver(); |
| 52 |
| 53 typedef std::vector<uint8_t> Packet; |
| 54 |
| 55 // Get the receiver’s state. |
| 56 State GetState(); |
| 57 |
| 58 // Return the packet size that the receiver parsed out of request/response. |
| 59 uint16_t GetMaxPacketSize(); |
| 60 |
| 61 // Will crash unless receiver is in State::CONNECTION_CLOSED. |
| 62 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose GetReasonForClose(); |
| 63 |
| 64 // Will crash unless receiver is in State::ERROR. |
| 65 // The reason that the receiver decided to enter ERROR state. |
| 66 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose GetReasonToClose(); |
| 67 |
| 68 // Will crash unless receiver is in State::DATA_READY. |
| 69 // NOTE: if this function is not called in DATA_READY state and the receiver |
| 70 // transitions out of that state, the data will be gone! |
| 71 std::string GetDataMessage(); |
| 72 |
| 73 // Add a packet that's just been received over Connection to the receiver. |
| 74 State ReceivePacket(const Packet& packet); |
| 75 |
| 76 protected: |
| 77 explicit BluetoothLowEnergyWeavePacketReceiver(ReceiverType receiver_type); |
| 78 |
| 79 private: |
| 80 void ReceiveFirstPacket(const Packet& packet); |
| 81 void ReceiveNonFirstPacket(const Packet& packet, bool expect_first_packet); |
| 82 |
| 83 void ReceiveConnectionRequest(const Packet& packet); |
| 84 void ReceiveConnectionResponse(const Packet& packet); |
| 85 void ReceiveConnectionClose(const Packet& packet); |
| 86 void AppendData(const Packet& packet, uint32_t byte_offset); |
| 87 |
| 88 uint16_t GetShortField(const Packet& packet, uint32_t byte_offset); |
| 89 uint8_t GetPacketType(const Packet& packet); |
| 90 uint8_t GetControlCommand(const Packet& packet); |
| 91 void VerifyPacketCounter(const Packet& packet); |
| 92 bool IsFirstDataPacket(const Packet& packet); |
| 93 bool IsLastDataPacket(const Packet& packet); |
| 94 bool IsLowerTwoBitsCleared(const Packet& packet); |
| 95 |
| 96 void Error( |
| 97 std::string error_message, |
| 98 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close); |
| 99 |
| 100 void SetMaxPacketSize(uint16_t packet_size); |
| 101 uint16_t GetConceptualMaxPacketSize(); |
| 102 |
| 103 // Identify whether the receiver is for a client or a server. |
| 104 ReceiverType receiver_type_; |
| 105 |
| 106 // Max packet size of the connection. |
| 107 // Default is 0 which means the server will determine the size by observing |
| 108 // ATT_MTU of the client. |
| 109 uint16_t max_packet_size_; |
| 110 |
| 111 // Counter for the number of packets sent, starting at 0. |
| 112 uint32_t packet_number_; |
| 113 |
| 114 // Current state of the receiver. |
| 115 // Certain functions will only return valid value if the receiver is in the |
| 116 // appropriate state. |
| 117 State state_; |
| 118 |
| 119 // The reason why the connection was closed by the sender if any. |
| 120 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_for_close_; |
| 121 |
| 122 // The reason why the receiver is in an erronous state if any. |
| 123 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close_; |
| 124 |
| 125 // The data message if there is one. |
| 126 Packet data_message_; |
| 127 }; |
| 128 |
| 129 } // namespace proximity_auth |
| 130 |
| 131 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_RECEI
VER_H_ |
| OLD | NEW |