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