| 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 // TODO(jingxuy): move the enums from BluetoothLowEnergyWeavePacketGenerator to |
| 18 // a shared location |
| 19 |
| 20 // Receive the messages sent with uWeave protocol. |
| 21 // Example Usage: |
| 22 // State state = ReceivePacket(packet); |
| 23 // switch (state) { |
| 24 // case ReceiverState::DATA_READY: |
| 25 // OnBytesReceived(GetDataMessage()); |
| 26 // break; |
| 27 // case ReceiverState::CONNECTION_CLOSED: |
| 28 // Disconnect(); |
| 29 // break; |
| 30 // case ReceiverState::ERROR: |
| 31 // HandleError(); |
| 32 // break; |
| 33 // case ReceiverState::CONNECTING: |
| 34 // case ReceiverState::WAITING: |
| 35 // case ReceiverState::RECEIVING_DATA: |
| 36 // break; |
| 37 // default: |
| 38 // FoundABugInReceiver(); |
| 39 // break; |
| 40 // } |
| 41 class BluetoothLowEnergyWeavePacketReceiver { |
| 42 public: |
| 43 enum ReceiverType { CLIENT, SERVER }; |
| 44 |
| 45 class Factory { |
| 46 public: |
| 47 static std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> NewInstance( |
| 48 ReceiverType receiver_type); |
| 49 |
| 50 // Exposed for testing. |
| 51 static void SetInstanceForTesting(Factory* factory); |
| 52 |
| 53 protected: |
| 54 // Exposed for testing. |
| 55 BluetoothLowEnergyWeavePacketReceiver* BuildInstance( |
| 56 ReceiverType receiver_type); |
| 57 |
| 58 private: |
| 59 static Factory* factory_instance_; |
| 60 }; |
| 61 |
| 62 // CONNECTING: |
| 63 // The connection hasn't been estabalished. Accept a CONNECTION_REQUEST if |
| 64 // the receiver is a SERVER. Accept a CONNECTION_RESPONSE if the receiver is |
| 65 // a CLIENT. All other packets cause the receiver to move into ERROR state. |
| 66 // The state will transition to WAITING after a request/response if they |
| 67 // do not have extra data. The state will transition to DATA_READY if the |
| 68 // request/response have extra data since the extra data is treated as a |
| 69 // complete data message. This state is never reentered. |
| 70 // WAITING: |
| 71 // The reciever is ready but doesn't have any data. It's waiting for packet |
| 72 // to arrive. Will accept all but connection request/response packets. The |
| 73 // first data packet will move the receiver to the RECEIVING_DATA state. A |
| 74 // connection close packet will move the receiver to the CONNECTION_CLOSED |
| 75 // state. This state is also never reentered. |
| 76 // RECEIVING_DATA: |
| 77 // The receiver is in middle of receiving a data message consisted of |
| 78 // multiple packets. Will receive only data packets. The last data packet |
| 79 // will move the receiver into DATA_READY state. This state can be entered |
| 80 // once from WAITING and unlimited number of times from DATA_READY. |
| 81 // DATA_READY: |
| 82 // The data message is ready to be retrieved. If the data is not retrieved |
| 83 // before the next packet which will cause a transition, the data will be |
| 84 // lost. Move to RECEIVING_DATA on receiving first data packet. Move to |
| 85 // CONNECTION_CLOSED on receiving close. This state can be entered once from |
| 86 // CONNECTICNG and unlimited number of times from RECEIVING_DATA. |
| 87 // CONNECTION_CLOSED: |
| 88 // The connection is closed. Refuse any further messages. Allow the reason |
| 89 // for close to be retrieved. |
| 90 // ERROR: |
| 91 // Something bad happened along the way. Allow a reason to close be |
| 92 // retrieved. The reason to close tells the receiver's user what reason to |
| 93 // close the connection in case the user wants to send a CONNECTION_CLOSE. |
| 94 enum State { |
| 95 CONNECTING = 0x00, |
| 96 WAITING = 0x01, |
| 97 RECEIVING_DATA = 0x02, |
| 98 DATA_READY = 0x03, |
| 99 CONNECTION_CLOSED = 0x04, |
| 100 ERROR = 0x05 |
| 101 }; |
| 102 |
| 103 ~BluetoothLowEnergyWeavePacketReceiver(); |
| 104 |
| 105 typedef std::vector<uint8_t> Packet; |
| 106 |
| 107 // Get the receiver’s state. |
| 108 State GetState(); |
| 109 |
| 110 // Return the packet size that the receiver parsed out of request/response. |
| 111 uint16_t GetMaxPacketSize(); |
| 112 |
| 113 // Get the reason that receiver received in a connection close packet. |
| 114 // It's only defined in CONNECTION_CLOSED state. |
| 115 // Will crash unless receiver is in State::CONNECTION_CLOSED. |
| 116 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose GetReasonForClose(); |
| 117 |
| 118 // The reason that the receiver decided to enter the ERROR state. |
| 119 // This would be the reason that the receiver's want to send a connection |
| 120 // close to the other side of the connection. |
| 121 // Will crash unless receiver is in State::ERROR. |
| 122 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose GetReasonToClose(); |
| 123 |
| 124 // Get a complete data message that's yet received. |
| 125 // Will crash unless receiver is in State::DATA_READY. |
| 126 // NOTE: if this function is not called in DATA_READY state and the receiver |
| 127 // transitions out of that state, the data will be gone! |
| 128 std::string GetDataMessage(); |
| 129 |
| 130 // Add a packet that's just been received over Connection to the receiver. |
| 131 State ReceivePacket(const Packet& packet); |
| 132 |
| 133 protected: |
| 134 explicit BluetoothLowEnergyWeavePacketReceiver(ReceiverType receiver_type); |
| 135 |
| 136 private: |
| 137 void ReceiveFirstPacket(const Packet& packet); |
| 138 void ReceiveNonFirstPacket(const Packet& packet); |
| 139 |
| 140 void ReceiveConnectionRequest(const Packet& packet); |
| 141 void ReceiveConnectionResponse(const Packet& packet); |
| 142 void ReceiveConnectionClose(const Packet& packet); |
| 143 void AppendData(const Packet& packet, uint32_t byte_offset); |
| 144 |
| 145 uint16_t GetShortField(const Packet& packet, uint32_t byte_offset); |
| 146 uint8_t GetPacketType(const Packet& packet); |
| 147 uint8_t GetControlCommand(const Packet& packet); |
| 148 void VerifyPacketCounter(const Packet& packet); |
| 149 bool IsFirstDataPacket(const Packet& packet); |
| 150 bool IsLastDataPacket(const Packet& packet); |
| 151 bool AreLowerTwoBitsCleared(const Packet& packet); |
| 152 |
| 153 void MoveToErrorState( |
| 154 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close); |
| 155 |
| 156 void SetMaxPacketSize(uint16_t packet_size); |
| 157 uint16_t GetConceptualMaxPacketSize(); |
| 158 |
| 159 // Identify whether the receiver is for a client or a server. |
| 160 ReceiverType receiver_type_; |
| 161 |
| 162 // Max packet size of the connection. |
| 163 // Default is 0 which means the server will determine the size by observing |
| 164 // ATT_MTU of the client. |
| 165 uint16_t max_packet_size_; |
| 166 |
| 167 // Expected counter of the next packet received, starting at 0. |
| 168 uint32_t next_packet_number_; |
| 169 |
| 170 // Current state of the receiver. |
| 171 // Certain functions will only return valid value if the receiver is in the |
| 172 // appropriate state. |
| 173 State state_; |
| 174 |
| 175 // The reason why the connection was closed by the sender if any. |
| 176 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_for_close_; |
| 177 |
| 178 // The reason why the receiver is in an erronous state if any. |
| 179 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close_; |
| 180 |
| 181 // The data message if there is one. |
| 182 Packet data_message_; |
| 183 }; |
| 184 |
| 185 } // namespace proximity_auth |
| 186 |
| 187 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_RECEI
VER_H_ |
| OLD | NEW |