libschc
Classes | Macros | Typedefs | Enumerations | Functions | Variables
picocoap.h File Reference
#include <stdint.h>
#include <stddef.h>

Go to the source code of this file.

Classes

struct  pcoap_option_names
 
struct  pcoap_pdu
 Packet Data Unit. More...
 
struct  pcoap_option
 CoAP Option. More...
 
struct  pcoap_payload
 CoAP Payload. More...
 

Macros

#define COAP_ACK_TIMEOUT   2
 CoAP Defined Parameters. More...
 
#define COAP_ACK_RANDOM_FACTOR   1.5
 
#define COAP_MAX_RETRANSMIT   4
 
#define COAP_NSTART   1
 
#define COAP_DEFAULT_LEISURE   5
 
#define COAP_PROBING_RATE   1
 
#define COAP_MAX_TRANSMIT_SPAN   45
 
#define COAP_MAX_TRANSMIT_WAIT   93
 
#define COAP_MAX_LATENCY   100
 
#define COAP_PROCESSING_DELAY   2
 
#define COAP_MAX_RTT   202
 
#define COAP_EXCHANGE_LIFETIME   247
 
#define COAP_NON_LIFETIME   145
 
#define COAP_OPTIONS_LENGTH   16
 

Typedefs

typedef enum coap_error pcoap_error
 Status Codes. More...
 
typedef enum coap_version pcoap_version
 Protocol Versions. More...
 
typedef enum coap_type pcoap_type
 Message Types. More...
 
typedef enum pcoap_code pcoap_code
 Message Codes. More...
 
typedef enum pcoap_option_number pcoap_option_number
 Option Numbers. More...
 
typedef struct pcoap_pdu pcoap_pdu
 Packet Data Unit. More...
 
typedef struct pcoap_option pcoap_option
 CoAP Option. More...
 
typedef struct pcoap_payload pcoap_payload
 CoAP Payload. More...
 

Enumerations

enum  coap_error {
  CE_NONE = 0, CE_INVALID_PACKET, CE_BAD_VERSION, CE_TOKEN_LENGTH_OUT_OF_RANGE,
  CE_UNKNOWN_CODE, CE_TOO_MANY_OPTIONS, CE_OUT_OF_ORDER_OPTIONS_LIST, CE_INSUFFICIENT_BUFFER,
  CE_FOUND_PAYLOAD_MARKER, CE_END_OF_PACKET
}
 Status Codes. More...
 
enum  coap_version { COAP_V1 = 1 }
 Protocol Versions. More...
 
enum  coap_type { CT_CON = 0, CT_NON = 1, CT_ACK = 2, CT_RST = 3 }
 Message Types. More...
 
enum  pcoap_code {
  CC_EMPTY = 0, CC_GET = 1, CC_POST = 2, CC_PUT = 3,
  CC_DELETE = 4, CC_CREATED = 65, CC_DELETED = 66, CC_VALID = 67,
  CC_CHANGED = 68, CC_CONTENT = 69, CC_CONTINUE = 95, CC_BAD_REQUEST = 128,
  CC_UNAUTHORIZED = 129, CC_BAD_OPTION = 130, CC_FORBIDDEN = 131, CC_NOT_FOUND = 132,
  CC_METHOD_NOT_ALLOWED = 133, CC_NOT_ACCEPTABLE = 134, CC_REQUEST_ENTITY_INCOMPLETE = 136, CC_PRECONDITION_FAILED = 140,
  CC_REQUEST_ENTITY_TOO_LARGE = 141, CC_UNSUPPORTED_CONTENT = 143, CC_INTERNAL_SERVER_ERROR = 160, CC_NOT_IMPLEMENTED = 161,
  CC_BAD_GATEWAY = 162, CC_SERVICE_UNAVAILABLE = 163, CC_GATEWAY_TIMEOUT = 164, CC_PROXYING_NOT_SUPPORTED = 165
}
 Message Codes. More...
 
enum  pcoap_option_number {
  CON_IF_MATCH = 1, CON_URI_HOST = 3, CON_ETAG = 4, CON_IF_NONE_MATCH = 5,
  CON_OBSERVE = 6, CON_URI_PORT = 7, CON_LOCATION_PATH = 8, CON_URI_PATH = 11,
  CON_CONTENT_FORMAT = 12, CON_MAX_AGE = 14, CON_URI_QUERY = 15, CON_ACCEPT = 17,
  CON_LOCATION_QUERY = 20, CON_PROXY_URI = 35, CON_PROXY_SCHEME = 39, CON_SIZE1 = 60,
  CON_NO_RESPONSE = 258
}
 Option Numbers. More...
 

Functions

uint8_t pcoap_get_coap_offset (pcoap_pdu *pdu)
 Finds the length of the CoAP header. More...
 
pcoap_error pcoap_validate_pkt (pcoap_pdu *pdu)
 Validate Packet. More...
 
static pcoap_version pcoap_get_version (pcoap_pdu *pdu)
 Get Version. More...
 
static pcoap_type pcoap_get_type (pcoap_pdu *pdu)
 Get Message Type. More...
 
static uint8_t pcoap_get_tkl (pcoap_pdu *pdu)
 Get Token Length. More...
 
static pcoap_code pcoap_get_code (pcoap_pdu *pdu)
 Get Message Code. More...
 
static uint8_t pcoap_get_code_class (pcoap_pdu *pdu)
 Get Message Code Class. More...
 
static uint8_t pcoap_get_code_detail (pcoap_pdu *pdu)
 Get Message Code Detail. More...
 
static uint16_t pcoap_get_mid (pcoap_pdu *pdu)
 Get Message ID. More...
 
uint8_t pcoap_get_token (pcoap_pdu *pdu, uint8_t *ptr)
 Get Message Token. More...
 
pcoap_option pcoap_get_option (pcoap_pdu *pdu, pcoap_option *last)
 Get Option. More...
 
pcoap_option pcoap_get_option_by_num (pcoap_pdu *pdu, pcoap_option_number num, uint8_t occ)
 Get Option by Option Number. More...
 
pcoap_payload pcoap_get_payload (pcoap_pdu *pdu)
 Get Option. More...
 
pcoap_error pcoap_decode_option (uint8_t *pkt_ptr, size_t pkt_len, uint16_t *option_number, size_t *option_length, uint8_t **value)
 Internal Method. More...
 
pcoap_error pcoap_init_pdu (pcoap_pdu *pdu)
 Initialize Packet. More...
 
pcoap_error pcoap_set_version (pcoap_pdu *pdu, pcoap_version ver)
 Set Version. More...
 
pcoap_error pcoap_set_type (pcoap_pdu *pdu, pcoap_type mtype)
 Set Message Type. More...
 
pcoap_error pcoap_set_code (pcoap_pdu *pdu, pcoap_code code)
 Set Message Code. More...
 
pcoap_error pcoap_set_mid (pcoap_pdu *pdu, uint16_t mid)
 Set Message ID. More...
 
pcoap_error pcoap_set_token (pcoap_pdu *pdu, uint8_t *token, uint8_t tkl)
 Set Message Token. More...
 
pcoap_error pcoap_add_option (pcoap_pdu *pdu, int32_t opt_num, uint8_t *value, uint16_t opt_len)
 Add Message Option. More...
 
pcoap_error pcoap_set_payload (pcoap_pdu *pdu, uint8_t *payload, size_t payload_len)
 Add Message Option. More...
 
static uint8_t pcoap_build_code (uint8_t _class, uint8_t detail)
 Build Message Code from Class and Detail. More...
 
pcoap_error pcoap_adjust_option_deltas (uint8_t *opts, size_t *opts_len, size_t max_len, int32_t offset)
 Internal Method. More...
 
int8_t pcoap_build_option_header (uint8_t *buf, size_t max_len, int32_t opt_delta, int32_t opt_len)
 Internal Method. More...
 
int8_t pcoap_compute_option_header_len (int32_t opt_delta, int32_t opt_len)
 Internal Method. More...
 

Variables

static struct pcoap_option_names coap_options [COAP_OPTIONS_LENGTH]
 

Macro Definition Documentation

◆ COAP_ACK_RANDOM_FACTOR

#define COAP_ACK_RANDOM_FACTOR   1.5

◆ COAP_ACK_TIMEOUT

#define COAP_ACK_TIMEOUT   2

CoAP Defined Parameters.

◆ COAP_DEFAULT_LEISURE

#define COAP_DEFAULT_LEISURE   5

◆ COAP_EXCHANGE_LIFETIME

#define COAP_EXCHANGE_LIFETIME   247

◆ COAP_MAX_LATENCY

#define COAP_MAX_LATENCY   100

◆ COAP_MAX_RETRANSMIT

#define COAP_MAX_RETRANSMIT   4

◆ COAP_MAX_RTT

#define COAP_MAX_RTT   202

◆ COAP_MAX_TRANSMIT_SPAN

#define COAP_MAX_TRANSMIT_SPAN   45

◆ COAP_MAX_TRANSMIT_WAIT

#define COAP_MAX_TRANSMIT_WAIT   93

◆ COAP_NON_LIFETIME

#define COAP_NON_LIFETIME   145

◆ COAP_NSTART

#define COAP_NSTART   1

◆ COAP_OPTIONS_LENGTH

#define COAP_OPTIONS_LENGTH   16

◆ COAP_PROBING_RATE

#define COAP_PROBING_RATE   1

◆ COAP_PROCESSING_DELAY

#define COAP_PROCESSING_DELAY   2

Typedef Documentation

◆ pcoap_code

typedef enum pcoap_code pcoap_code

Message Codes.

All known message request/response codes.

◆ pcoap_error

typedef enum coap_error pcoap_error

Status Codes.

These codes represent the possible errors that functions in this library can return.

◆ pcoap_option

typedef struct pcoap_option pcoap_option

CoAP Option.

One option in a CoAP message.

◆ pcoap_option_number

Option Numbers.

All known option numbers.

◆ pcoap_payload

typedef struct pcoap_payload pcoap_payload

CoAP Payload.

Payload container.

◆ pcoap_pdu

typedef struct pcoap_pdu pcoap_pdu

Packet Data Unit.

This contains all information about the message buffer.

◆ pcoap_type

typedef enum coap_type pcoap_type

Message Types.

The four types of messages possible.

◆ pcoap_version

Protocol Versions.

All known version of the protocol.

Enumeration Type Documentation

◆ coap_error

enum coap_error

Status Codes.

These codes represent the possible errors that functions in this library can return.

Enumerator
CE_NONE 
CE_INVALID_PACKET 
CE_BAD_VERSION 
CE_TOKEN_LENGTH_OUT_OF_RANGE 
CE_UNKNOWN_CODE 
CE_TOO_MANY_OPTIONS 
CE_OUT_OF_ORDER_OPTIONS_LIST 
CE_INSUFFICIENT_BUFFER 
CE_FOUND_PAYLOAD_MARKER 
CE_END_OF_PACKET 

◆ coap_type

enum coap_type

Message Types.

The four types of messages possible.

Enumerator
CT_CON 
CT_NON 
CT_ACK 
CT_RST 

◆ coap_version

Protocol Versions.

All known version of the protocol.

Enumerator
COAP_V1 

◆ pcoap_code

enum pcoap_code

Message Codes.

All known message request/response codes.

Enumerator
CC_EMPTY 
CC_GET 
CC_POST 
CC_PUT 
CC_DELETE 
CC_CREATED 
CC_DELETED 
CC_VALID 
CC_CHANGED 
CC_CONTENT 
CC_CONTINUE 
CC_BAD_REQUEST 
CC_UNAUTHORIZED 
CC_BAD_OPTION 
CC_FORBIDDEN 
CC_NOT_FOUND 
CC_METHOD_NOT_ALLOWED 
CC_NOT_ACCEPTABLE 
CC_REQUEST_ENTITY_INCOMPLETE 
CC_PRECONDITION_FAILED 
CC_REQUEST_ENTITY_TOO_LARGE 
CC_UNSUPPORTED_CONTENT 
CC_INTERNAL_SERVER_ERROR 
CC_NOT_IMPLEMENTED 
CC_BAD_GATEWAY 
CC_SERVICE_UNAVAILABLE 
CC_GATEWAY_TIMEOUT 
CC_PROXYING_NOT_SUPPORTED 

◆ pcoap_option_number

Option Numbers.

All known option numbers.

Enumerator
CON_IF_MATCH 
CON_URI_HOST 
CON_ETAG 
CON_IF_NONE_MATCH 
CON_OBSERVE 
CON_URI_PORT 
CON_LOCATION_PATH 
CON_URI_PATH 
CON_CONTENT_FORMAT 
CON_MAX_AGE 
CON_URI_QUERY 
CON_ACCEPT 
CON_LOCATION_QUERY 
CON_PROXY_URI 
CON_PROXY_SCHEME 
CON_SIZE1 
CON_NO_RESPONSE 

Function Documentation

◆ pcoap_add_option()

pcoap_error pcoap_add_option ( pcoap_pdu pdu,
int32_t  opt_num,
uint8_t *  value,
uint16_t  opt_len 
)

Add Message Option.

Adds an option to the existing message. Options SHOULD be added in order of option number. In the case of multiple options of the same type, they are sorted in the order that they are added.

Parameters
[in,out]pdupointer to the coap message struct.
[in]optoption container.
Returns
coap_error (0 == no error)

◆ pcoap_adjust_option_deltas()

pcoap_error pcoap_adjust_option_deltas ( uint8_t *  opts,
size_t *  opts_len,
size_t  max_len,
int32_t  offset 
)

Internal Method.

◆ pcoap_build_code()

static uint8_t pcoap_build_code ( uint8_t  _class,
uint8_t  detail 
)
inlinestatic

Build Message Code from Class and Detail.

Gets the class portion of the message code.

Parameters
[in]classthe code class.
[in]detailthe code detail.
See also
coap_get_code

◆ pcoap_build_option_header()

int8_t pcoap_build_option_header ( uint8_t *  buf,
size_t  max_len,
int32_t  opt_delta,
int32_t  opt_len 
)

Internal Method.

◆ pcoap_compute_option_header_len()

int8_t pcoap_compute_option_header_len ( int32_t  opt_delta,
int32_t  opt_len 
)

Internal Method.

◆ pcoap_decode_option()

pcoap_error pcoap_decode_option ( uint8_t *  pkt_ptr,
size_t  pkt_len,
uint16_t *  option_number,
size_t *  option_length,
uint8_t **  value 
)

Internal Method.

◆ pcoap_get_coap_offset()

uint8_t pcoap_get_coap_offset ( pcoap_pdu pdu)

Finds the length of the CoAP header.

Parameters
pduthe CoAP pdu, containing the header and payload
Returns
the length of the CoAP header

◆ pcoap_get_code()

static pcoap_code pcoap_get_code ( pcoap_pdu pdu)
inlinestatic

Get Message Code.

Extracts the message code from the given message.

Parameters
[in]pdupointer to the coap message struct.
Returns
code.
See also
coap_code

◆ pcoap_get_code_class()

static uint8_t pcoap_get_code_class ( pcoap_pdu pdu)
inlinestatic

Get Message Code Class.

Gets the class portion of the message code.

Parameters
[in]pdupointer to the coap message struct.
See also
coap_get_code

◆ pcoap_get_code_detail()

static uint8_t pcoap_get_code_detail ( pcoap_pdu pdu)
inlinestatic

Get Message Code Detail.

Gets the detail portion of the message code.

Parameters
[in]pdupointer to the coap message struct.
See also
coap_get_code

◆ pcoap_get_mid()

static uint16_t pcoap_get_mid ( pcoap_pdu pdu)
inlinestatic

Get Message ID.

Extracts the message ID from the given message.

Parameters
[in]pdupointer to the coap message struct.
Returns
mid.

◆ pcoap_get_option()

pcoap_option pcoap_get_option ( pcoap_pdu pdu,
pcoap_option last 
)

Get Option.

Iterates over the options in the given message.

Parameters
[in]pdupointer to the coap message struct.
[in,out]pointerto the last/next option, pass 0 for the first option.
Returns
coap_option

◆ pcoap_get_option_by_num()

pcoap_option pcoap_get_option_by_num ( pcoap_pdu pdu,
pcoap_option_number  num,
uint8_t  occ 
)

Get Option by Option Number.

Gets a single specified by the option number and index of which occurrence of that option number you'd like.

Parameters
[in]pdupointer to the coap message struct.
[in]numoption number to get.
[in]occoccurrence of to get (0th, 1st, 2nd, etc) 0 for the first option.
Returns
coap_option

◆ pcoap_get_payload()

pcoap_payload pcoap_get_payload ( pcoap_pdu pdu)

Get Option.

Extracts the option with the given index in the given message.

Parameters
[in]pdupointer to the coap message struct.
Returns
coap_payload

◆ pcoap_get_tkl()

static uint8_t pcoap_get_tkl ( pcoap_pdu pdu)
inlinestatic

Get Token Length.

Extracts the token length from the given message.

Parameters
[in]pdupointer to the coap message struct.
Returns
length.
See also
coap_type

◆ pcoap_get_token()

uint8_t pcoap_get_token ( pcoap_pdu pdu,
uint8_t *  ptr 
)

Get Message Token.

Extracts the token from the given message.

Parameters
[in]pdupointer to the coap message struct.
Returns
token.

◆ pcoap_get_type()

static pcoap_type pcoap_get_type ( pcoap_pdu pdu)
inlinestatic

Get Message Type.

Extracts the message type from the given message.

Parameters
[in]pdupointer to the coap message struct.
Returns
type.
See also
coap_type

◆ pcoap_get_version()

static pcoap_version pcoap_get_version ( pcoap_pdu pdu)
inlinestatic

Get Version.

Extracts the CoAP version from the given message.

Parameters
[in]pdupointer to the coap message struct.
Returns
version.
See also
coap_version

◆ pcoap_init_pdu()

pcoap_error pcoap_init_pdu ( pcoap_pdu pdu)

Initialize Packet.

Initializes on an empty buffer for creating new CoAP packets. This function (or coap_validate for parsing packets) must be called and must return CE_NONE before you can use any of the getters or setter. The packet is initialized to a CoAP Ping.

Parameters
[in,out]pdupointer to the coap message struct.
Returns
coap_error (0 == no error)

◆ pcoap_set_code()

pcoap_error pcoap_set_code ( pcoap_pdu pdu,
pcoap_code  code 
)

Set Message Code.

Sets the message type header field.

Parameters
[in,out]pdupointer to the coap message struct.
[in]codecode to set.
Returns
coap_error (0 == no error)
See also
coap_code

◆ pcoap_set_mid()

pcoap_error pcoap_set_mid ( pcoap_pdu pdu,
uint16_t  mid 
)

Set Message ID.

Sets the message ID header field.

Parameters
[in,out]pdupointer to the coap message struct.
[in]midmessage ID to set.
Returns
coap_error (0 == no error)

◆ pcoap_set_payload()

pcoap_error pcoap_set_payload ( pcoap_pdu pdu,
uint8_t *  payload,
size_t  payload_len 
)

Add Message Option.

Sets the payload of the given message to the value in payload.

Parameters
[in,out]pdupointer to the coap message struct.
[in]plpayload container.
Returns
coap_error (0 == no error)

◆ pcoap_set_token()

pcoap_error pcoap_set_token ( pcoap_pdu pdu,
uint8_t *  token,
uint8_t  tkl 
)

Set Message Token.

Sets the message token header field.

Parameters
[in,out]pdupointer to the coap message struct.
[in]tokenpointer to the token array to set.
Returns
coap_error (0 == no error)

◆ pcoap_set_type()

pcoap_error pcoap_set_type ( pcoap_pdu pdu,
pcoap_type  mtype 
)

Set Message Type.

Sets the message type header field.

Parameters
[in,out]pdupointer to the coap message struct.
[in]mtypetype to set.
Returns
coap_error (0 == no error)
See also
coap_type

◆ pcoap_set_version()

pcoap_error pcoap_set_version ( pcoap_pdu pdu,
pcoap_version  ver 
)

Set Version.

Sets the version number header field.

Parameters
[in,out]pdupointer to the coap message struct.
[in]verversion to set. Must be COAP_V1.
Returns
coap_error (0 == no error)
See also
coap_version

◆ pcoap_validate_pkt()

pcoap_error pcoap_validate_pkt ( pcoap_pdu pdu)

Validate Packet.

Parses the given packet to check if it is a valid CoAP message. This function (or coap_init_pdu for creating new packets) must be called and must return CE_NONE before you can use any of the getters or setter.

Parameters
[in]pdupointer to the coap message struct.
Returns
error code (CE_NONE == 0 == no error).
See also
coap_error
coap_init_pdu

Variable Documentation

◆ coap_options

struct pcoap_option_names coap_options[COAP_OPTIONS_LENGTH]
static
Initial value:
= {
{ 1, "if-match" },
{ 3, "uri-host" },
{ 4, "etag" },
{ 5, "if-none-match" },
{ 7, "uri-port" },
{ 8, "location-path" },
{ 11, "uri-path"} ,
{ 12, "content-format" },
{ 14, "max-age" },
{ 15, "uri-query" },
{ 17, "accept" },
{ 20, "location-query" },
{ 35, "proxy-uri" },
{ 39, "proxy-scheme" },
{ 60, "size1" },
{ 258, "no-response"}
}