forked from zephyrproject-rtos/openthread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.h
449 lines (409 loc) · 13 KB
/
message.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* @brief
* This file defines the top-level OpenThread APIs related to message buffer and queues.
*/
#ifndef OPENTHREAD_MESSAGE_H_
#define OPENTHREAD_MESSAGE_H_
#include <openthread/instance.h>
#include <openthread/platform/toolchain.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @addtogroup api-message
*
* @brief
* This module includes functions that manipulate OpenThread message buffers.
*
* @{
*
*/
/**
* An opaque representation of an OpenThread message buffer.
*
*/
typedef struct otMessage otMessage;
/**
* Defines the OpenThread message priority levels.
*
*/
typedef enum otMessagePriority
{
OT_MESSAGE_PRIORITY_LOW = 0, ///< Low priority level.
OT_MESSAGE_PRIORITY_NORMAL = 1, ///< Normal priority level.
OT_MESSAGE_PRIORITY_HIGH = 2, ///< High priority level.
} otMessagePriority;
/**
* Defines the OpenThread message origins.
*
*/
typedef enum otMessageOrigin
{
OT_MESSAGE_ORIGIN_THREAD_NETIF = 0, ///< Message from Thread Netif.
OT_MESSAGE_ORIGIN_HOST_TRUSTED = 1, ///< Message from a trusted source on host.
OT_MESSAGE_ORIGIN_HOST_UNTRUSTED = 2, ///< Message from an untrusted source on host.
} otMessageOrigin;
/**
* Represents a message settings.
*
*/
typedef struct otMessageSettings
{
bool mLinkSecurityEnabled; ///< TRUE if the message should be secured at Layer 2.
uint8_t mPriority; ///< Priority level (MUST be a `OT_MESSAGE_PRIORITY_*` from `otMessagePriority`).
} otMessageSettings;
/**
* Free an allocated message buffer.
*
* @param[in] aMessage A pointer to a message buffer.
*
* @sa otMessageAppend
* @sa otMessageGetLength
* @sa otMessageSetLength
* @sa otMessageGetOffset
* @sa otMessageSetOffset
* @sa otMessageRead
* @sa otMessageWrite
*
*/
void otMessageFree(otMessage *aMessage);
/**
* Get the message length in bytes.
*
* @param[in] aMessage A pointer to a message buffer.
*
* @returns The message length in bytes.
*
* @sa otMessageFree
* @sa otMessageAppend
* @sa otMessageSetLength
* @sa otMessageGetOffset
* @sa otMessageSetOffset
* @sa otMessageRead
* @sa otMessageWrite
* @sa otMessageSetLength
*
*/
uint16_t otMessageGetLength(const otMessage *aMessage);
/**
* Set the message length in bytes.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aLength A length in bytes.
*
* @retval OT_ERROR_NONE Successfully set the message length.
* @retval OT_ERROR_NO_BUFS No available buffers to grow the message.
*
* @sa otMessageFree
* @sa otMessageAppend
* @sa otMessageGetLength
* @sa otMessageGetOffset
* @sa otMessageSetOffset
* @sa otMessageRead
* @sa otMessageWrite
*
*/
otError otMessageSetLength(otMessage *aMessage, uint16_t aLength);
/**
* Get the message offset in bytes.
*
* @param[in] aMessage A pointer to a message buffer.
*
* @returns The message offset value.
*
* @sa otMessageFree
* @sa otMessageAppend
* @sa otMessageGetLength
* @sa otMessageSetLength
* @sa otMessageSetOffset
* @sa otMessageRead
* @sa otMessageWrite
*
*/
uint16_t otMessageGetOffset(const otMessage *aMessage);
/**
* Set the message offset in bytes.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aOffset An offset in bytes.
*
* @sa otMessageFree
* @sa otMessageAppend
* @sa otMessageGetLength
* @sa otMessageSetLength
* @sa otMessageGetOffset
* @sa otMessageRead
* @sa otMessageWrite
*
*/
void otMessageSetOffset(otMessage *aMessage, uint16_t aOffset);
/**
* Indicates whether or not link security is enabled for the message.
*
* @param[in] aMessage A pointer to a message buffer.
*
* @retval TRUE If link security is enabled.
* @retval FALSE If link security is not enabled.
*
*/
bool otMessageIsLinkSecurityEnabled(const otMessage *aMessage);
/**
* Indicates whether or not the message is allowed to be looped back to host.
*
* @param[in] aMessage A pointer to a message buffer.
*
* @retval TRUE If the message is allowed to be looped back to host.
* @retval FALSE If the message is not allowed to be looped back to host.
*
*/
bool otMessageIsLoopbackToHostAllowed(const otMessage *aMessage);
/**
* Sets whether or not the message is allowed to be looped back to host.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aAllowLoopbackToHost Whether to allow the message to be looped back to host.
*
*/
void otMessageSetLoopbackToHostAllowed(otMessage *aMessage, bool aAllowLoopbackToHost);
/**
* Gets the message origin.
*
* @param[in] aMessage A pointer to a message buffer.
*
* @returns The message origin.
*
*/
otMessageOrigin otMessageGetOrigin(const otMessage *aMessage);
/**
* Sets the message origin.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aOrigin The message origin.
*
*/
void otMessageSetOrigin(otMessage *aMessage, otMessageOrigin aOrigin);
/**
* Sets/forces the message to be forwarded using direct transmission.
* Default setting for a new message is `false`.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aEnabled If `true`, the message is forced to use direct transmission. If `false`, the message follows
* the normal procedure.
*
*/
void otMessageSetDirectTransmission(otMessage *aMessage, bool aEnabled);
/**
* Returns the average RSS (received signal strength) associated with the message.
*
* @returns The average RSS value (in dBm) or OT_RADIO_RSSI_INVALID if no average RSS is available.
*
*/
int8_t otMessageGetRss(const otMessage *aMessage);
/**
* Append bytes to a message.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aBuf A pointer to the data to append.
* @param[in] aLength Number of bytes to append.
*
* @retval OT_ERROR_NONE Successfully appended to the message
* @retval OT_ERROR_NO_BUFS No available buffers to grow the message.
*
* @sa otMessageFree
* @sa otMessageGetLength
* @sa otMessageSetLength
* @sa otMessageGetOffset
* @sa otMessageSetOffset
* @sa otMessageRead
* @sa otMessageWrite
*
*/
otError otMessageAppend(otMessage *aMessage, const void *aBuf, uint16_t aLength);
/**
* Read bytes from a message.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aOffset An offset in bytes.
* @param[in] aBuf A pointer to a buffer that message bytes are read to.
* @param[in] aLength Number of bytes to read.
*
* @returns The number of bytes read.
*
* @sa otMessageFree
* @sa otMessageAppend
* @sa otMessageGetLength
* @sa otMessageSetLength
* @sa otMessageGetOffset
* @sa otMessageSetOffset
* @sa otMessageWrite
*
*/
uint16_t otMessageRead(const otMessage *aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength);
/**
* Write bytes to a message.
*
* @param[in] aMessage A pointer to a message buffer.
* @param[in] aOffset An offset in bytes.
* @param[in] aBuf A pointer to a buffer that message bytes are written from.
* @param[in] aLength Number of bytes to write.
*
* @returns The number of bytes written.
*
* @sa otMessageFree
* @sa otMessageAppend
* @sa otMessageGetLength
* @sa otMessageSetLength
* @sa otMessageGetOffset
* @sa otMessageSetOffset
* @sa otMessageRead
*
*/
int otMessageWrite(otMessage *aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength);
/**
* Represents an OpenThread message queue.
*/
typedef struct
{
void *mData; ///< Opaque data used by the implementation.
} otMessageQueue;
/**
* Represents information about a message queue.
*
*/
typedef struct otMessageQueueInfo
{
uint16_t mNumMessages; ///< Number of messages in the queue.
uint16_t mNumBuffers; ///< Number of data buffers used by messages in the queue.
uint32_t mTotalBytes; ///< Total number of bytes used by all messages in the queue.
} otMessageQueueInfo;
/**
* Represents the message buffer information for different queues used by OpenThread stack.
*
*/
typedef struct otBufferInfo
{
uint16_t mTotalBuffers; ///< The total number of buffers in the messages pool (0xffff if unknown).
uint16_t mFreeBuffers; ///< The number of free buffers (0xffff if unknown).
/**
* The maximum number of used buffers at the same time since OT stack initialization or last call to
* `otMessageResetBufferInfo()`.
*
*/
uint16_t mMaxUsedBuffers;
otMessageQueueInfo m6loSendQueue; ///< Info about 6LoWPAN send queue.
otMessageQueueInfo m6loReassemblyQueue; ///< Info about 6LoWPAN reassembly queue.
otMessageQueueInfo mIp6Queue; ///< Info about IPv6 send queue.
otMessageQueueInfo mMplQueue; ///< Info about MPL send queue.
otMessageQueueInfo mMleQueue; ///< Info about MLE delayed message queue.
otMessageQueueInfo mCoapQueue; ///< Info about CoAP/TMF send queue.
otMessageQueueInfo mCoapSecureQueue; ///< Info about CoAP secure send queue.
otMessageQueueInfo mApplicationCoapQueue; ///< Info about application CoAP send queue.
} otBufferInfo;
/**
* Initialize the message queue.
*
* MUST be called once and only once for a `otMessageQueue` instance before any other `otMessageQueue`
* functions. The behavior is undefined if other queue APIs are used with an `otMessageQueue` before it being
* initialized or if it is initialized more than once.
*
* @param[in] aQueue A pointer to a message queue.
*
*/
void otMessageQueueInit(otMessageQueue *aQueue);
/**
* Adds a message to the end of the given message queue.
*
* @param[in] aQueue A pointer to the message queue.
* @param[in] aMessage The message to add.
*
*/
void otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage *aMessage);
/**
* Adds a message at the head/front of the given message queue.
*
* @param[in] aQueue A pointer to the message queue.
* @param[in] aMessage The message to add.
*
*/
void otMessageQueueEnqueueAtHead(otMessageQueue *aQueue, otMessage *aMessage);
/**
* Removes a message from the given message queue.
*
* @param[in] aQueue A pointer to the message queue.
* @param[in] aMessage The message to remove.
*
*/
void otMessageQueueDequeue(otMessageQueue *aQueue, otMessage *aMessage);
/**
* Returns a pointer to the message at the head of the queue.
*
* @param[in] aQueue A pointer to a message queue.
*
* @returns A pointer to the message at the head of queue or NULL if queue is empty.
*
*/
otMessage *otMessageQueueGetHead(otMessageQueue *aQueue);
/**
* Returns a pointer to the next message in the queue by iterating forward (from head to tail).
*
* @param[in] aQueue A pointer to a message queue.
* @param[in] aMessage A pointer to current message buffer.
*
* @returns A pointer to the next message in the queue after `aMessage` or NULL if `aMessage is the tail of queue.
* NULL is returned if `aMessage` is not in the queue `aQueue`.
*
*/
otMessage *otMessageQueueGetNext(otMessageQueue *aQueue, const otMessage *aMessage);
/**
* Get the Message Buffer information.
*
* @param[in] aInstance A pointer to the OpenThread instance.
* @param[out] aBufferInfo A pointer where the message buffer information is written.
*
*/
void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo);
/**
* Reset the Message Buffer information counter tracking the maximum number buffers in use at the same time.
*
* This resets `mMaxUsedBuffers` in `otBufferInfo`.
*
* @param[in] aInstance A pointer to the OpenThread instance.
*
*/
void otMessageResetBufferInfo(otInstance *aInstance);
/**
* @}
*
*/
#ifdef __cplusplus
} // extern "C"
#endif
#endif // OPENTHREAD_MESSAGE_H_