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 CoAP API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #if OPENTHREAD_CONFIG_COAP_API_ENABLE
37 
38 #include <openthread/coap.h>
39 
40 #include "coap/coap_message.hpp"
41 #include "common/as_core_type.hpp"
42 #include "common/locator_getters.hpp"
43 
44 using namespace ot;
45 
otCoapNewMessage(otInstance * aInstance,const otMessageSettings * aSettings)46 otMessage *otCoapNewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
47 {
48     return AsCoreType(aInstance).GetApplicationCoap().NewMessage(Message::Settings::From(aSettings));
49 }
50 
otCoapMessageInit(otMessage * aMessage,otCoapType aType,otCoapCode aCode)51 void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode)
52 {
53     AsCoapMessage(aMessage).Init(MapEnum(aType), MapEnum(aCode));
54 }
55 
otCoapMessageInitResponse(otMessage * aResponse,const otMessage * aRequest,otCoapType aType,otCoapCode aCode)56 otError otCoapMessageInitResponse(otMessage *aResponse, const otMessage *aRequest, otCoapType aType, otCoapCode aCode)
57 {
58     Coap::Message       &response = AsCoapMessage(aResponse);
59     const Coap::Message &request  = AsCoapMessage(aRequest);
60 
61     response.Init(MapEnum(aType), MapEnum(aCode));
62     response.SetMessageId(request.GetMessageId());
63 
64     return response.SetTokenFromMessage(request);
65 }
66 
otCoapMessageSetToken(otMessage * aMessage,const uint8_t * aToken,uint8_t aTokenLength)67 otError otCoapMessageSetToken(otMessage *aMessage, const uint8_t *aToken, uint8_t aTokenLength)
68 {
69     return AsCoapMessage(aMessage).SetToken(aToken, aTokenLength);
70 }
71 
otCoapMessageGenerateToken(otMessage * aMessage,uint8_t aTokenLength)72 void otCoapMessageGenerateToken(otMessage *aMessage, uint8_t aTokenLength)
73 {
74     IgnoreError(AsCoapMessage(aMessage).GenerateRandomToken(aTokenLength));
75 }
76 
otCoapMessageAppendContentFormatOption(otMessage * aMessage,otCoapOptionContentFormat aContentFormat)77 otError otCoapMessageAppendContentFormatOption(otMessage *aMessage, otCoapOptionContentFormat aContentFormat)
78 {
79     return AsCoapMessage(aMessage).AppendContentFormatOption(aContentFormat);
80 }
81 
otCoapMessageAppendOption(otMessage * aMessage,uint16_t aNumber,uint16_t aLength,const void * aValue)82 otError otCoapMessageAppendOption(otMessage *aMessage, uint16_t aNumber, uint16_t aLength, const void *aValue)
83 {
84     return AsCoapMessage(aMessage).AppendOption(aNumber, aLength, aValue);
85 }
86 
otCoapMessageAppendUintOption(otMessage * aMessage,uint16_t aNumber,uint32_t aValue)87 otError otCoapMessageAppendUintOption(otMessage *aMessage, uint16_t aNumber, uint32_t aValue)
88 {
89     return AsCoapMessage(aMessage).AppendUintOption(aNumber, aValue);
90 }
91 
otCoapMessageAppendObserveOption(otMessage * aMessage,uint32_t aObserve)92 otError otCoapMessageAppendObserveOption(otMessage *aMessage, uint32_t aObserve)
93 {
94     return AsCoapMessage(aMessage).AppendObserveOption(aObserve);
95 }
96 
otCoapMessageAppendUriPathOptions(otMessage * aMessage,const char * aUriPath)97 otError otCoapMessageAppendUriPathOptions(otMessage *aMessage, const char *aUriPath)
98 {
99     return AsCoapMessage(aMessage).AppendUriPathOptions(aUriPath);
100 }
101 
otCoapBlockSizeFromExponent(otCoapBlockSzx aSize)102 uint16_t otCoapBlockSizeFromExponent(otCoapBlockSzx aSize)
103 {
104     return static_cast<uint16_t>(1 << (static_cast<uint8_t>(aSize) + Coap::Message::kBlockSzxBase));
105 }
106 
otCoapMessageAppendBlock2Option(otMessage * aMessage,uint32_t aNum,bool aMore,otCoapBlockSzx aSize)107 otError otCoapMessageAppendBlock2Option(otMessage *aMessage, uint32_t aNum, bool aMore, otCoapBlockSzx aSize)
108 {
109     return AsCoapMessage(aMessage).AppendBlockOption(Coap::Message::kBlockType2, aNum, aMore, aSize);
110 }
111 
otCoapMessageAppendBlock1Option(otMessage * aMessage,uint32_t aNum,bool aMore,otCoapBlockSzx aSize)112 otError otCoapMessageAppendBlock1Option(otMessage *aMessage, uint32_t aNum, bool aMore, otCoapBlockSzx aSize)
113 {
114     return AsCoapMessage(aMessage).AppendBlockOption(Coap::Message::kBlockType1, aNum, aMore, aSize);
115 }
116 
otCoapMessageAppendProxyUriOption(otMessage * aMessage,const char * aUriPath)117 otError otCoapMessageAppendProxyUriOption(otMessage *aMessage, const char *aUriPath)
118 {
119     return AsCoapMessage(aMessage).AppendProxyUriOption(aUriPath);
120 }
121 
otCoapMessageAppendMaxAgeOption(otMessage * aMessage,uint32_t aMaxAge)122 otError otCoapMessageAppendMaxAgeOption(otMessage *aMessage, uint32_t aMaxAge)
123 {
124     return AsCoapMessage(aMessage).AppendMaxAgeOption(aMaxAge);
125 }
126 
otCoapMessageAppendUriQueryOption(otMessage * aMessage,const char * aUriQuery)127 otError otCoapMessageAppendUriQueryOption(otMessage *aMessage, const char *aUriQuery)
128 {
129     return AsCoapMessage(aMessage).AppendUriQueryOption(aUriQuery);
130 }
131 
otCoapMessageSetPayloadMarker(otMessage * aMessage)132 otError otCoapMessageSetPayloadMarker(otMessage *aMessage) { return AsCoapMessage(aMessage).SetPayloadMarker(); }
133 
otCoapMessageGetType(const otMessage * aMessage)134 otCoapType otCoapMessageGetType(const otMessage *aMessage)
135 {
136     return static_cast<otCoapType>(AsCoapMessage(aMessage).GetType());
137 }
138 
otCoapMessageGetCode(const otMessage * aMessage)139 otCoapCode otCoapMessageGetCode(const otMessage *aMessage)
140 {
141     return static_cast<otCoapCode>(AsCoapMessage(aMessage).GetCode());
142 }
143 
otCoapMessageSetCode(otMessage * aMessage,otCoapCode aCode)144 void otCoapMessageSetCode(otMessage *aMessage, otCoapCode aCode) { AsCoapMessage(aMessage).SetCode(MapEnum(aCode)); }
145 
otCoapMessageCodeToString(const otMessage * aMessage)146 const char *otCoapMessageCodeToString(const otMessage *aMessage) { return AsCoapMessage(aMessage).CodeToString(); }
147 
otCoapMessageGetMessageId(const otMessage * aMessage)148 uint16_t otCoapMessageGetMessageId(const otMessage *aMessage) { return AsCoapMessage(aMessage).GetMessageId(); }
149 
otCoapMessageGetTokenLength(const otMessage * aMessage)150 uint8_t otCoapMessageGetTokenLength(const otMessage *aMessage) { return AsCoapMessage(aMessage).GetTokenLength(); }
151 
otCoapMessageGetToken(const otMessage * aMessage)152 const uint8_t *otCoapMessageGetToken(const otMessage *aMessage) { return AsCoapMessage(aMessage).GetToken(); }
153 
otCoapOptionIteratorInit(otCoapOptionIterator * aIterator,const otMessage * aMessage)154 otError otCoapOptionIteratorInit(otCoapOptionIterator *aIterator, const otMessage *aMessage)
155 {
156     return AsCoreType(aIterator).Init(AsCoapMessage(aMessage));
157 }
158 
otCoapOptionIteratorGetFirstOptionMatching(otCoapOptionIterator * aIterator,uint16_t aOption)159 const otCoapOption *otCoapOptionIteratorGetFirstOptionMatching(otCoapOptionIterator *aIterator, uint16_t aOption)
160 {
161     Coap::Option::Iterator &iterator = AsCoreType(aIterator);
162 
163     IgnoreError(iterator.Init(iterator.GetMessage(), aOption));
164     return iterator.GetOption();
165 }
166 
otCoapOptionIteratorGetFirstOption(otCoapOptionIterator * aIterator)167 const otCoapOption *otCoapOptionIteratorGetFirstOption(otCoapOptionIterator *aIterator)
168 {
169     Coap::Option::Iterator &iterator = AsCoreType(aIterator);
170 
171     IgnoreError(iterator.Init(iterator.GetMessage()));
172     return iterator.GetOption();
173 }
174 
otCoapOptionIteratorGetNextOptionMatching(otCoapOptionIterator * aIterator,uint16_t aOption)175 const otCoapOption *otCoapOptionIteratorGetNextOptionMatching(otCoapOptionIterator *aIterator, uint16_t aOption)
176 {
177     Coap::Option::Iterator &iterator = AsCoreType(aIterator);
178 
179     IgnoreError(iterator.Advance(aOption));
180     return iterator.GetOption();
181 }
182 
otCoapOptionIteratorGetNextOption(otCoapOptionIterator * aIterator)183 const otCoapOption *otCoapOptionIteratorGetNextOption(otCoapOptionIterator *aIterator)
184 {
185     Coap::Option::Iterator &iterator = AsCoreType(aIterator);
186 
187     IgnoreError(iterator.Advance());
188     return iterator.GetOption();
189 }
190 
otCoapOptionIteratorGetOptionUintValue(otCoapOptionIterator * aIterator,uint64_t * aValue)191 otError otCoapOptionIteratorGetOptionUintValue(otCoapOptionIterator *aIterator, uint64_t *aValue)
192 {
193     return AsCoreType(aIterator).ReadOptionValue(*aValue);
194 }
195 
otCoapOptionIteratorGetOptionValue(otCoapOptionIterator * aIterator,void * aValue)196 otError otCoapOptionIteratorGetOptionValue(otCoapOptionIterator *aIterator, void *aValue)
197 {
198     return AsCoreType(aIterator).ReadOptionValue(aValue);
199 }
200 
201 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSendRequestBlockWiseWithParameters(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,otCoapResponseHandler aHandler,void * aContext,const otCoapTxParameters * aTxParameters,otCoapBlockwiseTransmitHook aTransmitHook,otCoapBlockwiseReceiveHook aReceiveHook)202 otError otCoapSendRequestBlockWiseWithParameters(otInstance                 *aInstance,
203                                                  otMessage                  *aMessage,
204                                                  const otMessageInfo        *aMessageInfo,
205                                                  otCoapResponseHandler       aHandler,
206                                                  void                       *aContext,
207                                                  const otCoapTxParameters   *aTxParameters,
208                                                  otCoapBlockwiseTransmitHook aTransmitHook,
209                                                  otCoapBlockwiseReceiveHook  aReceiveHook)
210 {
211     Error                     error;
212     const Coap::TxParameters &txParameters = Coap::TxParameters::From(aTxParameters);
213 
214     if (aTxParameters != nullptr)
215     {
216         VerifyOrExit(txParameters.IsValid(), error = kErrorInvalidArgs);
217     }
218 
219     error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo),
220                                                                    txParameters, aHandler, aContext, aTransmitHook,
221                                                                    aReceiveHook);
222 
223 exit:
224     return error;
225 }
226 #endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
227 
otCoapSendRequestWithParameters(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,otCoapResponseHandler aHandler,void * aContext,const otCoapTxParameters * aTxParameters)228 otError otCoapSendRequestWithParameters(otInstance               *aInstance,
229                                         otMessage                *aMessage,
230                                         const otMessageInfo      *aMessageInfo,
231                                         otCoapResponseHandler     aHandler,
232                                         void                     *aContext,
233                                         const otCoapTxParameters *aTxParameters)
234 {
235     Error error;
236 
237     const Coap::TxParameters &txParameters = Coap::TxParameters::From(aTxParameters);
238 
239     if (aTxParameters != nullptr)
240     {
241         VerifyOrExit(txParameters.IsValid(), error = kErrorInvalidArgs);
242     }
243 
244     error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo),
245                                                                    txParameters, aHandler, aContext);
246 
247 exit:
248     return error;
249 }
250 
otCoapStart(otInstance * aInstance,uint16_t aPort)251 otError otCoapStart(otInstance *aInstance, uint16_t aPort)
252 {
253     return AsCoreType(aInstance).GetApplicationCoap().Start(aPort);
254 }
255 
otCoapStop(otInstance * aInstance)256 otError otCoapStop(otInstance *aInstance) { return AsCoreType(aInstance).GetApplicationCoap().Stop(); }
257 
258 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapAddBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)259 void otCoapAddBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
260 {
261     AsCoreType(aInstance).GetApplicationCoap().AddBlockWiseResource(AsCoreType(aResource));
262 }
263 
otCoapRemoveBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)264 void otCoapRemoveBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
265 {
266     AsCoreType(aInstance).GetApplicationCoap().RemoveBlockWiseResource(AsCoreType(aResource));
267 }
268 #endif
269 
otCoapAddResource(otInstance * aInstance,otCoapResource * aResource)270 void otCoapAddResource(otInstance *aInstance, otCoapResource *aResource)
271 {
272     AsCoreType(aInstance).GetApplicationCoap().AddResource(AsCoreType(aResource));
273 }
274 
otCoapRemoveResource(otInstance * aInstance,otCoapResource * aResource)275 void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource)
276 {
277     AsCoreType(aInstance).GetApplicationCoap().RemoveResource(AsCoreType(aResource));
278 }
279 
otCoapSetDefaultHandler(otInstance * aInstance,otCoapRequestHandler aHandler,void * aContext)280 void otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
281 {
282     AsCoreType(aInstance).GetApplicationCoap().SetDefaultHandler(aHandler, aContext);
283 }
284 
285 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSendResponseBlockWiseWithParameters(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,const otCoapTxParameters * aTxParameters,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook)286 otError otCoapSendResponseBlockWiseWithParameters(otInstance                 *aInstance,
287                                                   otMessage                  *aMessage,
288                                                   const otMessageInfo        *aMessageInfo,
289                                                   const otCoapTxParameters   *aTxParameters,
290                                                   void                       *aContext,
291                                                   otCoapBlockwiseTransmitHook aTransmitHook)
292 {
293     return AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo),
294                                                                   Coap::TxParameters::From(aTxParameters), nullptr,
295                                                                   aContext, aTransmitHook, nullptr);
296 }
297 #endif
298 
otCoapSendResponseWithParameters(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,const otCoapTxParameters * aTxParameters)299 otError otCoapSendResponseWithParameters(otInstance               *aInstance,
300                                          otMessage                *aMessage,
301                                          const otMessageInfo      *aMessageInfo,
302                                          const otCoapTxParameters *aTxParameters)
303 {
304     return AsCoreType(aInstance).GetApplicationCoap().SendMessage(
305         AsCoapMessage(aMessage), AsCoreType(aMessageInfo), Coap::TxParameters::From(aTxParameters), nullptr, nullptr);
306 }
307 
308 #endif // OPENTHREAD_CONFIG_COAP_API_ENABLE
309