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

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: commented test and h file. Fixed memory leak in test. style modification in cc file 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();
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 estabalish. Accept a CONNECTION_REQUEST if the
Kyle Horimoto 2016/06/21 02:10:27 s/estabalish/established/
Kyle Horimoto 2016/06/21 18:13:38 Ping.
jingxuy 2016/06/22 01:42:23 Done.
64 // receiver is a SERVER. Accept a CONNECTION_RESPONSE if the receiver is a
65 // CLIENT. All other packets cause the receiver to move into ERROR state.
66 // The state will transition to WAITING after a request/respone if they
Kyle Horimoto 2016/06/21 02:10:27 s/respone/response/
Kyle Horimoto 2016/06/21 18:13:38 Ping.
jingxuy 2016/06/22 01:42:23 Done.
67 // do not have extra data. The state will transition to DATA_READY if the
68 // request/resposne have extra data since the extra data is treated as a
Kyle Horimoto 2016/06/21 02:10:27 s/resposne/response/
Kyle Horimoto 2016/06/21 18:13:38 Ping.
jingxuy 2016/06/22 01:42:23 Done.
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 request/response packets. The first data
Kyle Horimoto 2016/06/21 02:10:27 connection request/response
Kyle Horimoto 2016/06/21 18:13:38 Ping.
jingxuy 2016/06/22 01:42:23 Done.
73 // packet will move the receiver to the RECEIVING_DATA state. A close packet
74 // will move the receiver to the CONNECTION_CLOSED state. This state is also
75 // 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, bool expect_first_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 ThrowError(
154 std::string error_message,
155 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close);
156
157 void SetMaxPacketSize(uint16_t packet_size);
158 uint16_t GetConceptualMaxPacketSize();
159
160 // Identify whether the receiver is for a client or a server.
161 ReceiverType receiver_type_;
162
163 // Max packet size of the connection.
164 // Default is 0 which means the server will determine the size by observing
165 // ATT_MTU of the client.
166 uint16_t max_packet_size_;
167
168 // Expected counter of the next packet received, starting at 0.
169 uint32_t next_packet_number_;
170
171 // Current state of the receiver.
172 // Certain functions will only return valid value if the receiver is in the
173 // appropriate state.
174 State state_;
175
176 // The reason why the connection was closed by the sender if any.
177 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_for_close_;
178
179 // The reason why the receiver is in an erronous state if any.
180 BluetoothLowEnergyWeavePacketGenerator::ReasonForClose reason_to_close_;
181
182 // The data message if there is one.
183 Packet data_message_;
184 };
185
186 } // namespace proximity_auth
187
188 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_PACKET_RECEI VER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698