forked from lancaster-university/codal-microbit-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketBuffer.h
273 lines (246 loc) · 8.4 KB
/
PacketBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef MICROBIT_PACKET_BUFFER_H
#define MICROBIT_PACKET_BUFFER_H
#include "CodalConfig.h"
#include "codal-core/inc/types/Event.h"
#include "RefCounted.h"
namespace codal
{
struct PacketData : RefCounted
{
int rssi; // The radio signal strength this packet was received.
uint8_t length; // The length of the payload in bytes
uint8_t payload[0]; // User / higher layer protocol data
};
/**
* Class definition for a PacketBuffer.
* A PacketBuffer holds a series of bytes that can be sent or received from the MicroBitRadio channel.
*
* @note This is a mutable, managed type.
*/
class PacketBuffer
{
PacketData *ptr; // Pointer to payload data
public:
/**
* Provide a pointer to a memory location containing the packet data.
*
* @return The contents of this packet, as an array of bytes.
*/
uint8_t *getBytes();
/**
* Default Constructor.
* Creates an empty Packet Buffer.
*
* @code
* PacketBuffer p();
* @endcode
*/
PacketBuffer();
/**
* Constructor.
* Creates a new PacketBuffer of the given size.
*
* @param length The length of the buffer to create.
*
* @code
* PacketBuffer p(16); // Creates a PacketBuffer 16 bytes long.
* @endcode
*/
PacketBuffer(int length);
/**
* Constructor.
* Creates an empty Packet Buffer of the given size,
* and fills it with the data provided.
*
* @param data The data with which to fill the buffer.
*
* @param length The length of the buffer to create.
*
* @param rssi The radio signal strength at the time this packet was recieved. Defaults to 0.
*
* @code
* uint8_t buf = {13,5,2};
* PacketBuffer p(buf, 3); // Creates a PacketBuffer 3 bytes long.
* @endcode
*/
PacketBuffer(uint8_t *data, int length, int rssi = 0);
/**
* Copy Constructor.
* Add ourselves as a reference to an existing PacketBuffer.
*
* @param buffer The PacketBuffer to reference.
*
* @code
* PacketBuffer p();
* PacketBuffer p2(p); // Refers to the same packet as p.
* @endcode
*/
PacketBuffer(const PacketBuffer &buffer);
/**
* Internal constructor-initialiser.
*
* @param data The data with which to fill the buffer.
*
* @param length The length of the buffer to create.
*
* @param rssi The radio signal strength at the time this packet was recieved.
*/
void init(uint8_t *data, int length, int rssi);
/**
* Destructor.
*
* Removes buffer resources held by the instance.
*/
~PacketBuffer();
/**
* Copy assign operation.
*
* Called when one PacketBuffer is assigned the value of another using the '=' operator.
*
* Decrements our reference count and free up the buffer as necessary.
*
* Then, update our buffer to refer to that of the supplied PacketBuffer,
* and increase its reference count.
*
* @param p The PacketBuffer to reference.
*
* @code
* uint8_t buf = {13,5,2};
* PacketBuffer p1(16);
* PacketBuffer p2(buf, 3);
*
* p1 = p2;
* @endcode
*/
PacketBuffer& operator = (const PacketBuffer& p);
/**
* Array access operation (read).
*
* Called when a PacketBuffer is dereferenced with a [] operation.
*
* Transparently map this through to the underlying payload for elegance of programming.
*
* @code
* PacketBuffer p1(16);
* uint8_t data = p1[0];
* @endcode
*/
uint8_t operator [] (int i) const;
/**
* Array access operation (modify).
*
* Called when a PacketBuffer is dereferenced with a [] operation.
*
* Transparently map this through to the underlying payload for elegance of programming.
*
* @code
* PacketBuffer p1(16);
* p1[0] = 42;
* @endcode
*/
uint8_t& operator [] (int i);
/**
* Equality operation.
*
* Called when one PacketBuffer is tested to be equal to another using the '==' operator.
*
* @param p The PacketBuffer to test ourselves against.
*
* @return true if this PacketBuffer is identical to the one supplied, false otherwise.
*
* @code
* MicroBitDisplay display;
* uint8_t buf = {13,5,2};
* PacketBuffer p1();
* PacketBuffer p2();
*
* if(p1 == p2) // will be true
* display.scroll("same!");
* @endcode
*/
bool operator== (const PacketBuffer& p);
/**
* Sets the byte at the given index to value provided.
*
* @param position The index of the byte to change.
*
* @param value The new value of the byte (0-255).
*
* @return DEVICE_OK, or DEVICE_INVALID_PARAMETER.
*
* @code
* PacketBuffer p1(16);
* p1.setByte(0,255); // Sets the first byte in the buffer to the value 255.
* @endcode
*/
int setByte(int position, uint8_t value);
/**
* Determines the value of the given byte in the packet.
*
* @param position The index of the byte to read.
*
* @return The value of the byte at the given position, or DEVICE_INVALID_PARAMETER.
*
* @code
* PacketBuffer p1(16);
* p1.setByte(0,255); // Sets the first byte in the buffer to the value 255.
* p1.getByte(0); // Returns 255.
* @endcode
*/
int getByte(int position);
/**
* Gets number of bytes in this buffer
*
* @return The size of the buffer in bytes.
*
* @code
* PacketBuffer p1(16);
* p1.length(); // Returns 16.
* @endcode
*/
int length();
/**
* Retrieves the received signal strength of this packet.
*
* @return The signal strength of the radio when this packet was received, in -dbm.
* The higher the value, the stronger the signal. Typical values are in the range -42 to -128.
*
* @code
* PacketBuffer p1(16);
* p1.getRSSI(); // Returns the received signal strength.
* @endcode
*/
int getRSSI();
/**
* Sets the received signal strength of this packet.
*
* @code
* PacketBuffer p1(16);
* p1.setRSSI(37);
* @endcode
*/
void setRSSI(uint8_t rssi);
static PacketBuffer EmptyPacket;
};
}
#endif