0.01.00
coap.h
Go to the documentation of this file.
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 
35 #ifndef OPENTHREAD_COAP_H_
36 #define OPENTHREAD_COAP_H_
37 
38 #include <stdint.h>
39 
40 #include <openthread/message.h>
41 #include <openthread/types.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
60 #define OT_DEFAULT_COAP_PORT 5683
61 
62 
66 typedef enum otCoapType
67 {
72 } otCoapType;
73 
78 #define OT_COAP_CODE(c, d) ((((c) & 0x7) << 5) | ((d) & 0x1f))
79 
84 typedef enum otCoapCode
85 {
91 
98 
109 
116 } otCoapCode;
117 
121 typedef enum otCoapOptionType
122 {
140 
145 typedef struct otCoapOption
146 {
147  uint16_t mNumber;
148  uint16_t mLength;
149  const uint8_t *mValue;
150 } otCoapOption;
151 
157 {
165 
166 #define OT_COAP_HEADER_MAX_LENGTH 128
167 
168 
172 typedef struct otCoapHeader
173 {
174  union
175  {
176  struct
177  {
179  uint8_t mCode;
180  uint16_t mMessageId;
181  } mFields;
182  uint8_t mBytes[OT_COAP_HEADER_MAX_LENGTH];
183  } mHeader;
184  uint8_t mHeaderLength;
185  uint16_t mOptionLast;
187  uint16_t mNextOptionOffset;
189 } otCoapHeader;
190 
205 typedef void (*otCoapResponseHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
206  const otMessageInfo *aMessageInfo, otError aResult);
207 
217 typedef void (*otCoapRequestHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage,
218  const otMessageInfo *aMessageInfo);
219 
224 typedef struct otCoapResource
225 {
226  const char *mUriPath;
228  void *mContext;
231 
240 void otCoapHeaderInit(otCoapHeader *aHeader, otCoapType aType, otCoapCode aCode);
241 
250 void otCoapHeaderSetToken(otCoapHeader *aHeader, const uint8_t *aToken, uint8_t aTokenLength);
251 
259 void otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength);
260 
280 
293 
307 otError otCoapHeaderAppendUintOption(otCoapHeader *aHeader, uint16_t aNumber, uint32_t aValue);
308 
319 otError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve);
320 
332 otError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath);
333 
344 otError otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge);
345 
356 otError otCoapHeaderAppendUriQueryOption(otCoapHeader *aHeader, const char *aUriQuery);
357 
368 
376 void otCoapHeaderSetMessageId(otCoapHeader *aHeader, uint16_t aMessageId);
377 
387 
397 
406 uint16_t otCoapHeaderGetMessageId(const otCoapHeader *aHeader);
407 
416 uint8_t otCoapHeaderGetTokenLength(const otCoapHeader *aHeader);
417 
426 const uint8_t *otCoapHeaderGetToken(const otCoapHeader *aHeader);
427 
437 
447 
457 otMessage *otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader);
458 
475 otError otCoapSendRequest(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo,
476  otCoapResponseHandler aHandler, void *aContext);
477 
487 otError otCoapStart(otInstance *aInstance, uint16_t aPort);
488 
497 otError otCoapStop(otInstance *aInstance);
498 
509 otError otCoapAddResource(otInstance *aInstance, otCoapResource *aResource);
510 
518 void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource);
519 
527 void otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext);
528 
540 otError otCoapSendResponse(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo);
541 
547 #ifdef __cplusplus
548 } // extern "C"
549 #endif
550 
551 #endif /* OPENTHREAD_COAP_H_ */
uint16_t mLength
Option Length.
Definition: coap.h:148
void otCoapHeaderSetToken(otCoapHeader *aHeader, const uint8_t *aToken, uint8_t aTokenLength)
This function sets the Token value and length in a header.
Uri-Query.
Definition: coap.h:133
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
This file defines the types and structures used in the OpenThread library API.
otCoapCode
CoAP Code values.
Definition: coap.h:84
const uint8_t * mValue
A pointer to the Option Value.
Definition: coap.h:149
otCoapOptionType
CoAP Option Numbers.
Definition: coap.h:121
otError otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge)
This function appends a Max-Age option.
uint8_t mHeaderLength
The CoAP header length (bytes)
Definition: coap.h:184
Delete.
Definition: coap.h:90
otCoapType otCoapHeaderGetType(const otCoapHeader *aHeader)
This function returns the Type value.
Content.
Definition: coap.h:97
const char * mUriPath
The URI Path string.
Definition: coap.h:226
struct otCoapResource otCoapResource
This structure represents a CoAP resource.
Location-Path.
Definition: coap.h:129
Acknowledgment.
Definition: coap.h:70
Changed.
Definition: coap.h:96
Unauthorized.
Definition: coap.h:100
Internal Server Error.
Definition: coap.h:110
void otCoapHeaderSetPayloadMarker(otCoapHeader *aHeader)
This function adds Payload Marker indicating beginning of the payload to the CoAP header...
otCoapCode otCoapHeaderGetCode(const otCoapHeader *aHeader)
This function returns the Code value.
This structure represents a CoAP option.
Definition: coap.h:145
void otCoapHeaderInit(otCoapHeader *aHeader, otCoapType aType, otCoapCode aCode)
This function initializes the CoAP header.
uint16_t mOptionLast
The last CoAP Option Number value.
Definition: coap.h:185
otError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath)
This function appends an Uri-Path option.
const otCoapOption * otCoapHeaderGetNextOption(otCoapHeader *aHeader)
This function returns a pointer to the next option.
struct otCoapHeader otCoapHeader
This structure represents a CoAP header.
otError otCoapSendRequest(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo, otCoapResponseHandler aHandler, void *aContext)
This function sends a CoAP request.
This file defines the top-level OpenThread APIs related to message buffer and queues.
Reset.
Definition: coap.h:71
struct otCoapResource * mNext
The next CoAP resource in the list.
Definition: coap.h:229
Valid.
Definition: coap.h:95
Max-Age.
Definition: coap.h:132
Put.
Definition: coap.h:89
otError otCoapStop(otInstance *aInstance)
This function stops the CoAP server.
2.00
Definition: coap.h:92
This type points to an OpenThread message buffer.
Definition: types.h:479
application/exi
Definition: coap.h:162
uint16_t mNextOptionOffset
The byte offset for the next CoAP Option.
Definition: coap.h:187
Size1.
Definition: coap.h:138
application/link-format
Definition: coap.h:159
void otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
This function sets the default handler for unhandled CoAP requests.
Proxy-Scheme.
Definition: coap.h:137
Empty message code.
Definition: coap.h:86
otCoapType
CoAP Type values.
Definition: coap.h:66
This structure represents a CoAP resource.
Definition: coap.h:224
Uri-Path.
Definition: coap.h:130
uint8_t mCode
The CoAP Code.
Definition: coap.h:179
struct otCoapOption otCoapOption
This structure represents a CoAP option.
Bad Request.
Definition: coap.h:99
Not Found.
Definition: coap.h:103
#define OT_COAP_HEADER_MAX_LENGTH
Max CoAP header length (bytes)
Definition: coap.h:166
otError otCoapHeaderAppendUriQueryOption(otCoapHeader *aHeader, const char *aUriQuery)
This function appends a single Uri-Query option.
otError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve)
This function appends an Observe option.
If-Match.
Definition: coap.h:123
void otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength)
This function sets the Token length and randomizes its value.
otError otCoapHeaderAppendContentFormatOption(otCoapHeader *aHeader, otCoapOptionContentFormat aContentFormat)
This function appends the Content Format CoAP option as specified in https://tools.ietf.org/html/rfc7252#page-92.
void otCoapHeaderSetMessageId(otCoapHeader *aHeader, uint16_t aMessageId)
This function sets the Message ID value.
void otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource)
This function removes a resource from the CoAP server.
Non-confirmable.
Definition: coap.h:69
ETag.
Definition: coap.h:125
const otCoapOption * otCoapHeaderGetFirstOption(otCoapHeader *aHeader)
This function returns a pointer to the first option.
Bad Option.
Definition: coap.h:101
otError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *aOption)
This function appends a CoAP option in a header.
Forbidden.
Definition: coap.h:102
Get.
Definition: coap.h:87
Precondition Failed.
Definition: coap.h:106
uint8_t mVersionTypeToken
The CoAP Version, Type, and Token Length.
Definition: coap.h:178
otError otCoapSendResponse(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo)
This function sends a CoAP response from the server.
Unsupported Content-Format.
Definition: coap.h:108
If-None-Match.
Definition: coap.h:126
otCoapRequestHandler mHandler
The callback for handling a received request.
Definition: coap.h:227
Uri-Host.
Definition: coap.h:124
Service Unavailable.
Definition: coap.h:113
void(* otCoapResponseHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult)
This function pointer is called when a CoAP response is received or on the request timeout...
Definition: coap.h:205
const uint8_t * otCoapHeaderGetToken(const otCoapHeader *aHeader)
This function returns a pointer to the Token value.
Uri-Port.
Definition: coap.h:128
This structure represents a CoAP header.
Definition: coap.h:172
This structure represents the local and peer IPv6 socket addresses.
Definition: types.h:436
Not Acceptable.
Definition: coap.h:105
Created.
Definition: coap.h:93
Observe.
Definition: coap.h:127
uint16_t mFirstOptionOffset
The byte offset for the first CoAP Option.
Definition: coap.h:186
text/plain
Definition: coap.h:158
Deleted.
Definition: coap.h:94
otCoapOption mOption
A structure representing the current CoAP Option.
Definition: coap.h:188
Location-Query.
Definition: coap.h:135
void * mContext
Application-specific context.
Definition: coap.h:228
application/json
Definition: coap.h:163
Request Entity Too Large.
Definition: coap.h:107
#define OT_COAP_CODE(c, d)
Helper macro to define CoAP Code values.
Definition: coap.h:78
Method Not Allowed.
Definition: coap.h:104
otMessage * otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader)
This function creates a new message with a CoAP header.
Content-Format.
Definition: coap.h:131
application/xml
Definition: coap.h:160
uint16_t otCoapHeaderGetMessageId(const otCoapHeader *aHeader)
This function returns the Message ID value.
otError otCoapStart(otInstance *aInstance, uint16_t aPort)
This function starts the CoAP server.
uint8_t otCoapHeaderGetTokenLength(const otCoapHeader *aHeader)
This function returns the Token length.
Proxying Not Supported.
Definition: coap.h:115
uint16_t mMessageId
The CoAP Message ID.
Definition: coap.h:180
Bad Gateway.
Definition: coap.h:112
void(* otCoapRequestHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo)
This function pointer is called when a CoAP request with a given Uri-Path is received.
Definition: coap.h:217
otError otCoapAddResource(otInstance *aInstance, otCoapResource *aResource)
This function adds a resource to the CoAP server.
Accept.
Definition: coap.h:134
otCoapOptionContentFormat
CoAP Content Format codes.
Definition: coap.h:156
application/octet-stream
Definition: coap.h:161
Confirmable.
Definition: coap.h:68
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
uint16_t mNumber
Option Number.
Definition: coap.h:147
Not Implemented.
Definition: coap.h:111
otError otCoapHeaderAppendUintOption(otCoapHeader *aHeader, uint16_t aNumber, uint32_t aValue)
This function appends an unsigned integer CoAP option as specified in https://tools.ietf.org/html/rfc7252#section-3.2.
Proxy-Uri.
Definition: coap.h:136
Gateway Timeout.
Definition: coap.h:114
Post.
Definition: coap.h:88