1 /*
2 * Copyright (c) 2016, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements the OpenThread Message API.
32 */
33
34 #include "openthread-core-config.h"
35
36 #include <openthread/message.h>
37
38 #include "common/as_core_type.hpp"
39 #include "common/locator_getters.hpp"
40
41 using namespace ot;
42
otMessageFree(otMessage * aMessage)43 void otMessageFree(otMessage *aMessage)
44 {
45 AsCoreType(aMessage).Free();
46 }
47
otMessageGetLength(const otMessage * aMessage)48 uint16_t otMessageGetLength(const otMessage *aMessage)
49 {
50 return AsCoreType(aMessage).GetLength();
51 }
52
otMessageSetLength(otMessage * aMessage,uint16_t aLength)53 otError otMessageSetLength(otMessage *aMessage, uint16_t aLength)
54 {
55 return AsCoreType(aMessage).SetLength(aLength);
56 }
57
otMessageGetOffset(const otMessage * aMessage)58 uint16_t otMessageGetOffset(const otMessage *aMessage)
59 {
60 return AsCoreType(aMessage).GetOffset();
61 }
62
otMessageSetOffset(otMessage * aMessage,uint16_t aOffset)63 void otMessageSetOffset(otMessage *aMessage, uint16_t aOffset)
64 {
65 AsCoreType(aMessage).SetOffset(aOffset);
66 }
67
otMessageIsLinkSecurityEnabled(const otMessage * aMessage)68 bool otMessageIsLinkSecurityEnabled(const otMessage *aMessage)
69 {
70 return AsCoreType(aMessage).IsLinkSecurityEnabled();
71 }
72
otMessageSetDirectTransmission(otMessage * aMessage,bool aEnabled)73 void otMessageSetDirectTransmission(otMessage *aMessage, bool aEnabled)
74 {
75 if (aEnabled)
76 {
77 AsCoreType(aMessage).SetDirectTransmission();
78 }
79 else
80 {
81 AsCoreType(aMessage).ClearDirectTransmission();
82 }
83 }
84
otMessageGetRss(const otMessage * aMessage)85 int8_t otMessageGetRss(const otMessage *aMessage)
86 {
87 return AsCoreType(aMessage).GetAverageRss();
88 }
89
otMessageAppend(otMessage * aMessage,const void * aBuf,uint16_t aLength)90 otError otMessageAppend(otMessage *aMessage, const void *aBuf, uint16_t aLength)
91 {
92 return AsCoreType(aMessage).AppendBytes(aBuf, aLength);
93 }
94
otMessageRead(const otMessage * aMessage,uint16_t aOffset,void * aBuf,uint16_t aLength)95 uint16_t otMessageRead(const otMessage *aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength)
96 {
97 return AsCoreType(aMessage).ReadBytes(aOffset, aBuf, aLength);
98 }
99
otMessageWrite(otMessage * aMessage,uint16_t aOffset,const void * aBuf,uint16_t aLength)100 int otMessageWrite(otMessage *aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength)
101 {
102 AsCoreType(aMessage).WriteBytes(aOffset, aBuf, aLength);
103
104 return aLength;
105 }
106
otMessageQueueInit(otMessageQueue * aQueue)107 void otMessageQueueInit(otMessageQueue *aQueue)
108 {
109 aQueue->mData = nullptr;
110 }
111
otMessageQueueEnqueue(otMessageQueue * aQueue,otMessage * aMessage)112 void otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage *aMessage)
113 {
114 AsCoreType(aQueue).Enqueue(AsCoreType(aMessage));
115 }
116
otMessageQueueEnqueueAtHead(otMessageQueue * aQueue,otMessage * aMessage)117 void otMessageQueueEnqueueAtHead(otMessageQueue *aQueue, otMessage *aMessage)
118 {
119 AsCoreType(aQueue).Enqueue(AsCoreType(aMessage), MessageQueue::kQueuePositionHead);
120 }
121
otMessageQueueDequeue(otMessageQueue * aQueue,otMessage * aMessage)122 void otMessageQueueDequeue(otMessageQueue *aQueue, otMessage *aMessage)
123 {
124 AsCoreType(aQueue).Dequeue(AsCoreType(aMessage));
125 }
126
otMessageQueueGetHead(otMessageQueue * aQueue)127 otMessage *otMessageQueueGetHead(otMessageQueue *aQueue)
128 {
129 return AsCoreType(aQueue).GetHead();
130 }
131
otMessageQueueGetNext(otMessageQueue * aQueue,const otMessage * aMessage)132 otMessage *otMessageQueueGetNext(otMessageQueue *aQueue, const otMessage *aMessage)
133 {
134 Message *next;
135
136 VerifyOrExit(aMessage != nullptr, next = nullptr);
137
138 VerifyOrExit(AsCoreType(aMessage).GetMessageQueue() == aQueue, next = nullptr);
139 next = AsCoreType(aMessage).GetNext();
140
141 exit:
142 return next;
143 }
144
145 #if OPENTHREAD_MTD || OPENTHREAD_FTD
otMessageGetBufferInfo(otInstance * aInstance,otBufferInfo * aBufferInfo)146 void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo)
147 {
148 AsCoreType(aInstance).GetBufferInfo(AsCoreType(aBufferInfo));
149 }
150 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
151