Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.h

Issue 2053013002: Weave Packet Receiver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proximity_auth_weave_migration
Patch Set: added receiver error code and some test along the waywq Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(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 // CONNECTICNG and unlimited number of times from RECEIVING_DATA.
sacomoto 2016/06/23 16:09:19 nit: s/CONNECTICNG/CONNECTING/.
jingxuy 2016/06/23 21:16:14 Done.
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 = 0x05
84 };
85
86 // The specific error that caused the receiver to move to ERROR state.
87 enum ReceiverError {
88 NO_ERROR,
89 EMPTY_PACKET,
90 RECEIVED_PACKET_IN_ERROR,
91 RECEIVED_PACKET_IN_CONNECTION_CLOSED,
92 RECEIVED_DATA_IN_CONNECTING,
93 SERVER_RECEIVED_CONNECTION_RESPONSE,
94 CLIENT_RECEIVED_CONNECTION_REQUEST,
95 RECEIVED_CONNECTION_CLOSE_IN_CONNECTING,
96 UNRECOGNIZED_CONTROL_COMMAND,
97 INVALID_CONTROL_COMMAND_IN_DATA_TRANSACTION,
98 INVALID_DATA_PACKET_SIZE,
99 DATA_HEADER_LOW_BITS_NOT_CLEARED,
100 INCORRECT_DATA_FIRST_BIT,
101 INVALID_CONNECTION_REQUEST_SIZE,
102 INVALID_REQUESTED_MAX_PACKET_SIZE,
103 NOT_SUPPORTED_REQUESTED_VERSION,
104 INVALID_CONNECTION_RESPONSE_SIZE,
105 INVALID_SELECTED_MAX_PACKET_SIZE,
106 NOT_SUPPORTED_SELECTED_VERSION,
107 INVALID_CONNECTION_CLOSE_SIZE,
108 UNRECOGNIZED_REASON_FOR_CLOSE,
109 PACKET_OUT_OF_SEQUENCE
110 };
111
112 class Factory {
113 public:
114 static std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> NewInstance(
115 ReceiverType receiver_type);
116
117 // Exposed for testing.
118 static void SetInstanceForTesting(Factory* factory);
119
120 protected:
121 // Exposed for testing.
122 BluetoothLowEnergyWeavePacketReceiver* BuildInstance(
123 ReceiverType receiver_type);
124
125 private:
126 static Factory* factory_instance_;
127 };
128
129 ~BluetoothLowEnergyWeavePacketReceiver();
130
131 typedef std::vector<uint8_t> Packet;
132
133 // Get the receiver’s state.
134 State GetState();
135
136 // Return the packet size that the receiver parsed out of request/response.
137 uint16_t GetMaxPacketSize();
138
139 // Get the reason that receiver received in a connection close packet.
140 // It's only defined in CONNECTION_CLOSED state.
141 // Will crash unless receiver is in State::CONNECTION_CLOSED.
142 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose GetReasonForClose();
143
144 // The reason that the receiver decided to enter the ERROR state.
145 // This would be the reason that the receiver's want to send a connection
146 // close to the other side of the connection.
147 // Will crash unless receiver is in State::ERROR.
148 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose GetReasonToClose();
Kyle Horimoto 2016/06/22 22:44:06 Is this function necessary anymore?
jingxuy 2016/06/22 22:55:53 yah, it's what Gustavo wanted. Like a reason to se
sacomoto 2016/06/23 16:09:19 Jing is right. You'll need a reason to send in the
jingxuy 2016/06/23 21:16:14 Done.
149
150 // Get a complete data message that's yet received.
151 // Will crash unless receiver is in State::DATA_READY.
152 // NOTE: if this function is not called in DATA_READY state and the receiver
153 // transitions out of that state, the data will be gone!
154 std::string GetDataMessage();
155
156 // Get the specific error that caused the receiver to jump into ERROR state.
157 ReceiverError GetReceiverError();
158
159 // Add a packet that's just been received over Connection to the receiver.
160 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(
183 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close,
184 ReceiverError receiver_error);
185
186 void SetMaxPacketSize(uint16_t packet_size);
187 uint16_t GetConceptualMaxPacketSize();
188
189 // Identify whether the receiver is for a client or a server.
190 ReceiverType receiver_type_;
191
192 // Max packet size of the connection.
193 // Default is 0 which means the server will determine the size by observing
194 // ATT_MTU of the client.
195 uint16_t max_packet_size_;
196
197 // Expected counter of the next packet received, starting at 0.
198 uint32_t next_packet_number_;
199
200 // Current state of the receiver.
201 // Certain functions will only return valid value if the receiver is in the
202 // appropriate state.
203 State state_;
204
205 // The reason why the connection was closed by the sender if any.
206 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_for_close_;
207
208 // The reason why the receiver is in an erronous state if any.
209 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close_;
210
211 // The data message if there is one.
212 Packet data_message_;
213
214 // The error the receiver encountered while processing packets.
215 // Used for debugging purproses.
216 ReceiverError receiver_error_;
217 };
218
219 } // namespace proximity_auth
220
221 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_RECEI VER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698