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
otMessageSetDirectTransmission(otMessage * aMessage,bool aEnabled)55 void otMessageSetDirectTransmission(otMessage *aMessage, bool aEnabled)
56 {
57 if (aEnabled)
58 {
59 AsCoreType(aMessage).SetDirectTransmission();
60 }
61 else
62 {
63 AsCoreType(aMessage).ClearDirectTransmission();
64 }
65 }
66
otMessageGetRss(const otMessage * aMessage)67 int8_t otMessageGetRss(const otMessage *aMessage) { return AsCoreType(aMessage).GetAverageRss(); }
68
otMessageAppend(otMessage * aMessage,const void * aBuf,uint16_t aLength)69 otError otMessageAppend(otMessage *aMessage, const void *aBuf, uint16_t aLength)
70 {
71 AssertPointerIsNotNull(aBuf);
72
73 return AsCoreType(aMessage).AppendBytes(aBuf, aLength);
74 }
75
otMessageRead(const otMessage * aMessage,uint16_t aOffset,void * aBuf,uint16_t aLength)76 uint16_t otMessageRead(const otMessage *aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength)
77 {
78 AssertPointerIsNotNull(aBuf);
79
80 return AsCoreType(aMessage).ReadBytes(aOffset, aBuf, aLength);
81 }
82
otMessageWrite(otMessage * aMessage,uint16_t aOffset,const void * aBuf,uint16_t aLength)83 int otMessageWrite(otMessage *aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength)
84 {
85 AssertPointerIsNotNull(aBuf);
86
87 AsCoreType(aMessage).WriteBytes(aOffset, aBuf, aLength);
88
89 return aLength;
90 }
91
otMessageQueueInit(otMessageQueue * aQueue)92 void otMessageQueueInit(otMessageQueue *aQueue)
93 {
94 AssertPointerIsNotNull(aQueue);
95
96 aQueue->mData = nullptr;
97 }
98
otMessageQueueEnqueue(otMessageQueue * aQueue,otMessage * aMessage)99 void otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage *aMessage)
100 {
101 AsCoreType(aQueue).Enqueue(AsCoreType(aMessage));
102 }
103
otMessageQueueEnqueueAtHead(otMessageQueue * aQueue,otMessage * aMessage)104 void otMessageQueueEnqueueAtHead(otMessageQueue *aQueue, otMessage *aMessage)
105 {
106 AsCoreType(aQueue).Enqueue(AsCoreType(aMessage), MessageQueue::kQueuePositionHead);
107 }
108
otMessageQueueDequeue(otMessageQueue * aQueue,otMessage * aMessage)109 void otMessageQueueDequeue(otMessageQueue *aQueue, otMessage *aMessage)
110 {
111 AsCoreType(aQueue).Dequeue(AsCoreType(aMessage));
112 }
113
otMessageQueueGetHead(otMessageQueue * aQueue)114 otMessage *otMessageQueueGetHead(otMessageQueue *aQueue) { return AsCoreType(aQueue).GetHead(); }
115
otMessageQueueGetNext(otMessageQueue * aQueue,const otMessage * aMessage)116 otMessage *otMessageQueueGetNext(otMessageQueue *aQueue, const otMessage *aMessage)
117 {
118 Message *next;
119
120 VerifyOrExit(aMessage != nullptr, next = nullptr);
121
122 VerifyOrExit(AsCoreType(aMessage).GetMessageQueue() == aQueue, next = nullptr);
123 next = AsCoreType(aMessage).GetNext();
124
125 exit:
126 return next;
127 }
128
129 #if OPENTHREAD_MTD || OPENTHREAD_FTD
otMessageGetBufferInfo(otInstance * aInstance,otBufferInfo * aBufferInfo)130 void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo)
131 {
132 AsCoreType(aInstance).GetBufferInfo(AsCoreType(aBufferInfo));
133 }
134
otMessageResetBufferInfo(otInstance * aInstance)135 void otMessageResetBufferInfo(otInstance *aInstance) { AsCoreType(aInstance).ResetBufferInfo(); }
136 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
137