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 
otMessageGetThreadLinkInfo(const otMessage * aMessage,otThreadLinkInfo * aLinkInfo)93 otError otMessageGetThreadLinkInfo(const otMessage *aMessage, otThreadLinkInfo *aLinkInfo)
94 {
95     return AsCoreType(aMessage).GetLinkInfo(AsCoreType(aLinkInfo));
96 }
97 
otMessageAppend(otMessage * aMessage,const void * aBuf,uint16_t aLength)98 otError otMessageAppend(otMessage *aMessage, const void *aBuf, uint16_t aLength)
99 {
100     AssertPointerIsNotNull(aBuf);
101 
102     return AsCoreType(aMessage).AppendBytes(aBuf, aLength);
103 }
104 
otMessageRead(const otMessage * aMessage,uint16_t aOffset,void * aBuf,uint16_t aLength)105 uint16_t otMessageRead(const otMessage *aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength)
106 {
107     AssertPointerIsNotNull(aBuf);
108 
109     return AsCoreType(aMessage).ReadBytes(aOffset, aBuf, aLength);
110 }
111 
otMessageWrite(otMessage * aMessage,uint16_t aOffset,const void * aBuf,uint16_t aLength)112 int otMessageWrite(otMessage *aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength)
113 {
114     AssertPointerIsNotNull(aBuf);
115 
116     AsCoreType(aMessage).WriteBytes(aOffset, aBuf, aLength);
117 
118     return aLength;
119 }
120 
otMessageQueueInit(otMessageQueue * aQueue)121 void otMessageQueueInit(otMessageQueue *aQueue)
122 {
123     AssertPointerIsNotNull(aQueue);
124 
125     aQueue->mData = nullptr;
126 }
127 
otMessageQueueEnqueue(otMessageQueue * aQueue,otMessage * aMessage)128 void otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage *aMessage)
129 {
130     AsCoreType(aQueue).Enqueue(AsCoreType(aMessage));
131 }
132 
otMessageQueueEnqueueAtHead(otMessageQueue * aQueue,otMessage * aMessage)133 void otMessageQueueEnqueueAtHead(otMessageQueue *aQueue, otMessage *aMessage)
134 {
135     AsCoreType(aQueue).Enqueue(AsCoreType(aMessage), MessageQueue::kQueuePositionHead);
136 }
137 
otMessageQueueDequeue(otMessageQueue * aQueue,otMessage * aMessage)138 void otMessageQueueDequeue(otMessageQueue *aQueue, otMessage *aMessage)
139 {
140     AsCoreType(aQueue).Dequeue(AsCoreType(aMessage));
141 }
142 
otMessageQueueGetHead(otMessageQueue * aQueue)143 otMessage *otMessageQueueGetHead(otMessageQueue *aQueue) { return AsCoreType(aQueue).GetHead(); }
144 
otMessageQueueGetNext(otMessageQueue * aQueue,const otMessage * aMessage)145 otMessage *otMessageQueueGetNext(otMessageQueue *aQueue, const otMessage *aMessage)
146 {
147     Message *next;
148 
149     VerifyOrExit(aMessage != nullptr, next = nullptr);
150 
151     VerifyOrExit(AsCoreType(aMessage).GetMessageQueue() == aQueue, next = nullptr);
152     next = AsCoreType(aMessage).GetNext();
153 
154 exit:
155     return next;
156 }
157 
158 #if OPENTHREAD_MTD || OPENTHREAD_FTD
otMessageGetBufferInfo(otInstance * aInstance,otBufferInfo * aBufferInfo)159 void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo)
160 {
161     AsCoreType(aInstance).GetBufferInfo(AsCoreType(aBufferInfo));
162 }
163 
otMessageResetBufferInfo(otInstance * aInstance)164 void otMessageResetBufferInfo(otInstance *aInstance) { AsCoreType(aInstance).ResetBufferInfo(); }
165 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
166