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 VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
215
216 if (aTxParameters != nullptr)
217 {
218 VerifyOrExit(txParameters.IsValid(), error = kErrorInvalidArgs);
219 }
220
221 error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo),
222 txParameters, aHandler, aContext, aTransmitHook,
223 aReceiveHook);
224
225 exit:
226 return error;
227 }
228 #endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
229
otCoapSendRequestWithParameters(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,otCoapResponseHandler aHandler,void * aContext,const otCoapTxParameters * aTxParameters)230 otError otCoapSendRequestWithParameters(otInstance *aInstance,
231 otMessage *aMessage,
232 const otMessageInfo *aMessageInfo,
233 otCoapResponseHandler aHandler,
234 void *aContext,
235 const otCoapTxParameters *aTxParameters)
236 {
237 Error error;
238
239 const Coap::TxParameters &txParameters = Coap::TxParameters::From(aTxParameters);
240
241 VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
242
243 if (aTxParameters != nullptr)
244 {
245 VerifyOrExit(txParameters.IsValid(), error = kErrorInvalidArgs);
246 }
247
248 error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo),
249 txParameters, aHandler, aContext);
250
251 exit:
252 return error;
253 }
254
otCoapStart(otInstance * aInstance,uint16_t aPort)255 otError otCoapStart(otInstance *aInstance, uint16_t aPort)
256 {
257 return AsCoreType(aInstance).GetApplicationCoap().Start(aPort);
258 }
259
otCoapStop(otInstance * aInstance)260 otError otCoapStop(otInstance *aInstance) { return AsCoreType(aInstance).GetApplicationCoap().Stop(); }
261
262 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapAddBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)263 void otCoapAddBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
264 {
265 AsCoreType(aInstance).GetApplicationCoap().AddBlockWiseResource(AsCoreType(aResource));
266 }
267
otCoapRemoveBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)268 void otCoapRemoveBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
269 {
270 AsCoreType(aInstance).GetApplicationCoap().RemoveBlockWiseResource(AsCoreType(aResource));
271 }
272 #endif
273
otCoapAddResource(otInstance * aInstance,otCoapResource * aResource)274 void otCoapAddResource(otInstance *aInstance, otCoapResource *aResource)
275 {
276 AsCoreType(aInstance).GetApplicationCoap().AddResource(AsCoreType(aResource));
277 }
278
otCoapRemoveResource(otInstance * aInstance,otCoapResource * aResource)279 void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource)
280 {
281 AsCoreType(aInstance).GetApplicationCoap().RemoveResource(AsCoreType(aResource));
282 }
283
otCoapSetDefaultHandler(otInstance * aInstance,otCoapRequestHandler aHandler,void * aContext)284 void otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
285 {
286 AsCoreType(aInstance).GetApplicationCoap().SetDefaultHandler(aHandler, aContext);
287 }
288
289 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSendResponseBlockWiseWithParameters(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,const otCoapTxParameters * aTxParameters,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook)290 otError otCoapSendResponseBlockWiseWithParameters(otInstance *aInstance,
291 otMessage *aMessage,
292 const otMessageInfo *aMessageInfo,
293 const otCoapTxParameters *aTxParameters,
294 void *aContext,
295 otCoapBlockwiseTransmitHook aTransmitHook)
296 {
297 otError error;
298
299 VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
300
301 error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(AsCoapMessage(aMessage), AsCoreType(aMessageInfo),
302 Coap::TxParameters::From(aTxParameters), nullptr,
303 aContext, aTransmitHook, nullptr);
304 exit:
305 return error;
306 }
307 #endif
308
otCoapSendResponseWithParameters(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,const otCoapTxParameters * aTxParameters)309 otError otCoapSendResponseWithParameters(otInstance *aInstance,
310 otMessage *aMessage,
311 const otMessageInfo *aMessageInfo,
312 const otCoapTxParameters *aTxParameters)
313 {
314 otError error;
315
316 VerifyOrExit(!AsCoreType(aMessage).IsOriginThreadNetif(), error = kErrorInvalidArgs);
317
318 error = AsCoreType(aInstance).GetApplicationCoap().SendMessage(
319 AsCoapMessage(aMessage), AsCoreType(aMessageInfo), Coap::TxParameters::From(aTxParameters), nullptr, nullptr);
320
321 exit:
322 return error;
323 }
324
325 #endif // OPENTHREAD_CONFIG_COAP_API_ENABLE
326