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