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) { AsCoreType(aMessage).Free(); }
44 
otMessageGetLength(const otMessage * aMessage)45 uint16_t otMessageGetLength(const otMessage *aMessage) { return AsCoreType(aMessage).GetLength(); }
46 
otMessageSetLength(otMessage * aMessage,uint16_t aLength)47 otError otMessageSetLength(otMessage *aMessage, uint16_t aLength) { return AsCoreType(aMessage).SetLength(aLength); }
48 
otMessageGetOffset(const otMessage * aMessage)49 uint16_t otMessageGetOffset(const otMessage *aMessage) { return AsCoreType(aMessage).GetOffset(); }
50 
otMessageSetOffset(otMessage * aMessage,uint16_t aOffset)51 void otMessageSetOffset(otMessage *aMessage, uint16_t aOffset) { AsCoreType(aMessage).SetOffset(aOffset); }
52 
otMessageIsLinkSecurityEnabled(const otMessage * aMessage)53 bool otMessageIsLinkSecurityEnabled(const otMessage *aMessage) { return AsCoreType(aMessage).IsLinkSecurityEnabled(); }
54 
otMessageIsLoopbackToHostAllowed(const otMessage * aMessage)55 bool otMessageIsLoopbackToHostAllowed(const otMessage *aMessage)
56 {
57     return AsCoreType(aMessage).IsLoopbackToHostAllowed();
58 }
59 
otMessageSetLoopbackToHostAllowed(otMessage * aMessage,bool aAllowLoopbackToHost)60 void otMessageSetLoopbackToHostAllowed(otMessage *aMessage, bool aAllowLoopbackToHost)
61 {
62     return AsCoreType(aMessage).SetLoopbackToHostAllowed(aAllowLoopbackToHost);
63 }
64 
otMessageIsMulticastLoopEnabled(otMessage * aMessage)65 bool otMessageIsMulticastLoopEnabled(otMessage *aMessage) { return AsCoreType(aMessage).GetMulticastLoop(); }
66 
otMessageSetMulticastLoopEnabled(otMessage * aMessage,bool aEnabled)67 void otMessageSetMulticastLoopEnabled(otMessage *aMessage, bool aEnabled)
68 {
69     AsCoreType(aMessage).SetMulticastLoop(aEnabled);
70 }
71 
otMessageGetOrigin(const otMessage * aMessage)72 otMessageOrigin otMessageGetOrigin(const otMessage *aMessage) { return MapEnum(AsCoreType(aMessage).GetOrigin()); }
73 
otMessageSetOrigin(otMessage * aMessage,otMessageOrigin aOrigin)74 void otMessageSetOrigin(otMessage *aMessage, otMessageOrigin aOrigin)
75 {
76     AsCoreType(aMessage).SetOrigin(MapEnum(aOrigin));
77 }
78 
otMessageSetDirectTransmission(otMessage * aMessage,bool aEnabled)79 void otMessageSetDirectTransmission(otMessage *aMessage, bool aEnabled)
80 {
81     if (aEnabled)
82     {
83         AsCoreType(aMessage).SetDirectTransmission();
84     }
85     else
86     {
87         AsCoreType(aMessage).ClearDirectTransmission();
88     }
89 }
90 
otMessageGetRss(const otMessage * aMessage)91 int8_t otMessageGetRss(const otMessage *aMessage) { return AsCoreType(aMessage).GetAverageRss(); }
92 
otMessageAppend(otMessage * aMessage,const void * aBuf,uint16_t aLength)93 otError otMessageAppend(otMessage *aMessage, const void *aBuf, uint16_t aLength)
94 {
95     AssertPointerIsNotNull(aBuf);
96 
97     return AsCoreType(aMessage).AppendBytes(aBuf, aLength);
98 }
99 
otMessageRead(const otMessage * aMessage,uint16_t aOffset,void * aBuf,uint16_t aLength)100 uint16_t otMessageRead(const otMessage *aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength)
101 {
102     AssertPointerIsNotNull(aBuf);
103 
104     return AsCoreType(aMessage).ReadBytes(aOffset, aBuf, aLength);
105 }
106 
otMessageWrite(otMessage * aMessage,uint16_t aOffset,const void * aBuf,uint16_t aLength)107 int otMessageWrite(otMessage *aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength)
108 {
109     AssertPointerIsNotNull(aBuf);
110 
111     AsCoreType(aMessage).WriteBytes(aOffset, aBuf, aLength);
112 
113     return aLength;
114 }
115 
otMessageQueueInit(otMessageQueue * aQueue)116 void otMessageQueueInit(otMessageQueue *aQueue)
117 {
118     AssertPointerIsNotNull(aQueue);
119 
120     aQueue->mData = nullptr;
121 }
122 
otMessageQueueEnqueue(otMessageQueue * aQueue,otMessage * aMessage)123 void otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage *aMessage)
124 {
125     AsCoreType(aQueue).Enqueue(AsCoreType(aMessage));
126 }
127 
otMessageQueueEnqueueAtHead(otMessageQueue * aQueue,otMessage * aMessage)128 void otMessageQueueEnqueueAtHead(otMessageQueue *aQueue, otMessage *aMessage)
129 {
130     AsCoreType(aQueue).Enqueue(AsCoreType(aMessage), MessageQueue::kQueuePositionHead);
131 }
132 
otMessageQueueDequeue(otMessageQueue * aQueue,otMessage * aMessage)133 void otMessageQueueDequeue(otMessageQueue *aQueue, otMessage *aMessage)
134 {
135     AsCoreType(aQueue).Dequeue(AsCoreType(aMessage));
136 }
137 
otMessageQueueGetHead(otMessageQueue * aQueue)138 otMessage *otMessageQueueGetHead(otMessageQueue *aQueue) { return AsCoreType(aQueue).GetHead(); }
139 
otMessageQueueGetNext(otMessageQueue * aQueue,const otMessage * aMessage)140 otMessage *otMessageQueueGetNext(otMessageQueue *aQueue, const otMessage *aMessage)
141 {
142     Message *next;
143 
144     VerifyOrExit(aMessage != nullptr, next = nullptr);
145 
146     VerifyOrExit(AsCoreType(aMessage).GetMessageQueue() == aQueue, next = nullptr);
147     next = AsCoreType(aMessage).GetNext();
148 
149 exit:
150     return next;
151 }
152 
153 #if OPENTHREAD_MTD || OPENTHREAD_FTD
otMessageGetBufferInfo(otInstance * aInstance,otBufferInfo * aBufferInfo)154 void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo)
155 {
156     AsCoreType(aInstance).GetBufferInfo(AsCoreType(aBufferInfo));
157 }
158 
otMessageResetBufferInfo(otInstance * aInstance)159 void otMessageResetBufferInfo(otInstance *aInstance) { AsCoreType(aInstance).ResetBufferInfo(); }
160 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
161