| 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 "build/build_config.h" | |
| 15 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_defines.h" | |
| 16 | |
| 17 namespace proximity_auth { | |
| 18 namespace weave { | |
| 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(GetReasonForClose()); | |
| 29 // break; | |
| 30 // case ReceiverState::ERROR: | |
| 31 // HandleError(GetReasonToClose()); | |
| 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 // CONNECTING: | |
| 46 // The connection hasn't been estabalished. Accept a CONNECTION_REQUEST if | |
| 47 // the receiver is a SERVER. Accept a CONNECTION_RESPONSE if the receiver is | |
| 48 // a CLIENT. All other packets cause the receiver to move into ERROR state. | |
| 49 // The state will transition to WAITING after a request/response if they | |
| 50 // do not have extra data. The state will transition to DATA_READY if the | |
| 51 // request/response have extra data since the extra data is treated as a | |
| 52 // complete data message. This state is never reentered. | |
| 53 // WAITING: | |
| 54 // The reciever is ready but doesn't have any data. It's waiting for packet | |
| 55 // to arrive. Will accept all but connection request/response packets. The | |
| 56 // first data packet will move the receiver to the RECEIVING_DATA state. A | |
| 57 // connection close packet will move the receiver to the CONNECTION_CLOSED | |
| 58 // state. This state is also never reentered. | |
| 59 // RECEIVING_DATA: | |
| 60 // The receiver is in middle of receiving a data message consisted of | |
| 61 // multiple packets. Will receive only data packets. The last data packet | |
| 62 // will move the receiver into DATA_READY state. This state can be entered | |
| 63 // once from WAITING and unlimited number of times from DATA_READY. | |
| 64 // DATA_READY: | |
| 65 // The data message is ready to be retrieved. If the data is not retrieved | |
| 66 // before the next packet which will cause a transition, the data will be | |
| 67 // lost. Move to RECEIVING_DATA on receiving first data packet. Move to | |
| 68 // CONNECTION_CLOSED on receiving close. This state can be entered once from | |
| 69 // CONNECTING and unlimited number of times from RECEIVING_DATA. | |
| 70 // CONNECTION_CLOSED: | |
| 71 // The connection is closed. Refuse any further messages. Allow the reason | |
| 72 // for close to be retrieved. | |
| 73 // ERROR: | |
| 74 // Something bad happened along the way. Allow a reason to close be | |
| 75 // retrieved. The reason to close tells the receiver's user what reason to | |
| 76 // close the connection in case the user wants to send a CONNECTION_CLOSE. | |
| 77 enum State { | |
| 78 CONNECTING = 0x00, | |
| 79 WAITING = 0x01, | |
| 80 RECEIVING_DATA = 0x02, | |
| 81 DATA_READY = 0x03, | |
| 82 CONNECTION_CLOSED = 0x04, | |
| 83 ERROR_DETECTED = 0x05 | |
| 84 }; | |
| 85 | |
| 86 // The specific error that caused the receiver to move to ERROR state. | |
| 87 enum ReceiverError { | |
| 88 NO_ERROR_DETECTED, | |
| 89 EMPTY_PACKET, | |
| 90 RECEIVED_PACKET_IN_CONNECTION_CLOSED, | |
| 91 RECEIVED_DATA_IN_CONNECTING, | |
| 92 SERVER_RECEIVED_CONNECTION_RESPONSE, | |
| 93 CLIENT_RECEIVED_CONNECTION_REQUEST, | |
| 94 RECEIVED_CONNECTION_CLOSE_IN_CONNECTING, | |
| 95 UNRECOGNIZED_CONTROL_COMMAND, | |
| 96 INVALID_CONTROL_COMMAND_IN_DATA_TRANSACTION, | |
| 97 INVALID_DATA_PACKET_SIZE, | |
| 98 DATA_HEADER_LOW_BITS_NOT_CLEARED, | |
| 99 INCORRECT_DATA_FIRST_BIT, | |
| 100 INVALID_CONNECTION_REQUEST_SIZE, | |
| 101 INVALID_REQUESTED_MAX_PACKET_SIZE, | |
| 102 NOT_SUPPORTED_REQUESTED_VERSION, | |
| 103 INVALID_CONNECTION_RESPONSE_SIZE, | |
| 104 INVALID_SELECTED_MAX_PACKET_SIZE, | |
| 105 NOT_SUPPORTED_SELECTED_VERSION, | |
| 106 INVALID_CONNECTION_CLOSE_SIZE, | |
| 107 UNRECOGNIZED_REASON_FOR_CLOSE, | |
| 108 PACKET_OUT_OF_SEQUENCE | |
| 109 }; | |
| 110 | |
| 111 class Factory { | |
| 112 public: | |
| 113 static std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> NewInstance( | |
| 114 ReceiverType receiver_type); | |
| 115 | |
| 116 // Exposed for testing. | |
| 117 static void SetInstanceForTesting(std::shared_ptr<Factory> factory); | |
| 118 | |
| 119 protected: | |
| 120 // Exposed for testing. | |
| 121 virtual std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> | |
| 122 BuildInstance(ReceiverType receiver_type); | |
| 123 | |
| 124 private: | |
| 125 static std::shared_ptr<Factory> factory_instance_; | |
| 126 }; | |
| 127 | |
| 128 ~BluetoothLowEnergyWeavePacketReceiver(); | |
| 129 | |
| 130 typedef std::vector<uint8_t> Packet; | |
| 131 | |
| 132 // Get the receiver’s state. | |
| 133 virtual State GetState(); | |
| 134 | |
| 135 // Return the packet size that the receiver parsed out of request/response. | |
| 136 virtual uint16_t GetMaxPacketSize(); | |
| 137 | |
| 138 // Get the reason that receiver received in a connection close packet. | |
| 139 // It's only defined in CONNECTION_CLOSED state. | |
| 140 // Will crash unless receiver is in State::CONNECTION_CLOSED. | |
| 141 virtual ReasonForClose GetReasonForClose(); | |
| 142 | |
| 143 // The reason that the receiver decided to enter the ERROR state. | |
| 144 // This would be the reason that the receiver's want to send a connection | |
| 145 // close to the other side of the connection. | |
| 146 // Will crash unless receiver is in State::ERROR. | |
| 147 virtual ReasonForClose GetReasonToClose(); | |
| 148 | |
| 149 // Get a complete data message that's yet received. | |
| 150 // Will crash unless receiver is in State::DATA_READY. | |
| 151 // NOTE: if this function is not called in DATA_READY state and the receiver | |
| 152 // transitions out of that state, the data will be gone! | |
| 153 virtual std::string GetDataMessage(); | |
| 154 | |
| 155 // Get the specific error that caused the receiver to jump into ERROR state. | |
| 156 // Can be called from any state. Will return NO_ERROR if no error occurred. | |
| 157 virtual ReceiverError GetReceiverError(); | |
| 158 | |
| 159 // Add a packet that's just been received over Connection to the receiver. | |
| 160 virtual State ReceivePacket(const Packet& packet); | |
| 161 | |
| 162 protected: | |
| 163 explicit BluetoothLowEnergyWeavePacketReceiver(ReceiverType receiver_type); | |
| 164 | |
| 165 private: | |
| 166 void ReceiveFirstPacket(const Packet& packet); | |
| 167 void ReceiveNonFirstPacket(const Packet& packet); | |
| 168 | |
| 169 void ReceiveConnectionRequest(const Packet& packet); | |
| 170 void ReceiveConnectionResponse(const Packet& packet); | |
| 171 void ReceiveConnectionClose(const Packet& packet); | |
| 172 void AppendData(const Packet& packet, uint32_t byte_offset); | |
| 173 | |
| 174 uint16_t GetShortField(const Packet& packet, uint32_t byte_offset); | |
| 175 uint8_t GetPacketType(const Packet& packet); | |
| 176 uint8_t GetControlCommand(const Packet& packet); | |
| 177 void VerifyPacketCounter(const Packet& packet); | |
| 178 bool IsFirstDataPacket(const Packet& packet); | |
| 179 bool IsLastDataPacket(const Packet& packet); | |
| 180 bool AreLowerTwoBitsCleared(const Packet& packet); | |
| 181 | |
| 182 void MoveToErrorState(ReasonForClose reason_to_close, | |
| 183 ReceiverError receiver_error); | |
| 184 | |
| 185 void SetMaxPacketSize(uint16_t packet_size); | |
| 186 uint16_t GetConceptualMaxPacketSize(); | |
| 187 | |
| 188 // Identify whether the receiver is for a client or a server. | |
| 189 ReceiverType receiver_type_; | |
| 190 | |
| 191 // Max packet size of the connection. | |
| 192 // Default is 0 which means the server will determine the size by observing | |
| 193 // ATT_MTU of the client. | |
| 194 uint16_t max_packet_size_; | |
| 195 | |
| 196 // Expected counter of the next packet received, starting at 0. | |
| 197 uint8_t next_packet_counter_; | |
| 198 | |
| 199 // Current state of the receiver. | |
| 200 // Certain functions will only return valid value if the receiver is in the | |
| 201 // appropriate state. | |
| 202 State state_; | |
| 203 | |
| 204 // The reason why the connection was closed by the sender if any. | |
| 205 ReasonForClose reason_for_close_; | |
| 206 | |
| 207 // The reason why the receiver is in an erronous state if any. | |
| 208 ReasonForClose reason_to_close_; | |
| 209 | |
| 210 // The data message if there is one. | |
| 211 Packet data_message_; | |
| 212 | |
| 213 // The error the receiver encountered while processing packets. | |
| 214 // Used for debugging purproses. | |
| 215 ReceiverError receiver_error_; | |
| 216 }; | |
| 217 | |
| 218 } // namespace weave | |
| 219 | |
| 220 } // namespace proximity_auth | |
| 221 | |
| 222 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_RECEI
VER_H_ | |
| OLD | NEW |