libcoap  4.3.0rc1
pdu.c File Reference
#include "coap2/coap_internal.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+ Include dependency graph for pdu.c:

Go to the source code of this file.

Data Structures

struct  error_desc_t
 

Macros

#define min(a, b)   ((a) < (b) ? (a) : (b))
 
#define max(a, b)   ((a) > (b) ? (a) : (b))
 

Functions

void coap_pdu_clear (coap_pdu_t *pdu, size_t size)
 Clears any contents from pdu and resets used_size, and data pointers. More...
 
coap_pdu_tcoap_pdu_init (uint8_t type, uint8_t code, uint16_t mid, size_t size)
 Creates a new CoAP PDU with at least enough storage space for the given size maximum message size. More...
 
coap_pdu_tcoap_new_pdu (const struct coap_session_t *session)
 Creates a new CoAP PDU. More...
 
void coap_delete_pdu (coap_pdu_t *pdu)
 Dispose of an CoAP PDU and frees associated storage. More...
 
coap_pdu_tcoap_pdu_duplicate (const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, uint8_t *token, coap_opt_filter_t *drop_options)
 Duplicate an existing PDU. More...
 
int coap_pdu_resize (coap_pdu_t *pdu, size_t new_size)
 Dynamically grows the size of pdu to new_size. More...
 
int coap_pdu_check_resize (coap_pdu_t *pdu, size_t size)
 Dynamically grows the size of pdu to new_size if needed. More...
 
int coap_add_token (coap_pdu_t *pdu, size_t len, const uint8_t *data)
 Adds token of length len to pdu. More...
 
int coap_update_token (coap_pdu_t *pdu, size_t len, const uint8_t *data)
 Updates token in pdu with length len and data. More...
 
int coap_remove_option (coap_pdu_t *pdu, uint16_t type)
 Removes option of given type from the pdu. More...
 
size_t coap_insert_option (coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
 Inserts option of given type in the pdu with the appropriate data. More...
 
size_t coap_update_option (coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
 Updates existing first option of given type in the pdu with the new data. More...
 
size_t coap_add_option (coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
 Adds option of given type to pdu that is passed as first parameter. More...
 
int coap_add_data (coap_pdu_t *pdu, size_t len, const uint8_t *data)
 Adds given data to the pdu that is passed as first parameter. More...
 
uint8_t * coap_add_data_after (coap_pdu_t *pdu, size_t len)
 Adds given data to the pdu that is passed as first parameter but does not copy it. More...
 
int coap_get_data (const coap_pdu_t *pdu, size_t *len, uint8_t **data)
 Retrieves the length and data pointer of specified PDU. More...
 
int coap_get_data_large (const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
 Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs. More...
 
const char * coap_response_phrase (unsigned char code)
 Returns a human-readable response phrase for the specified CoAP response code. More...
 
static size_t next_option_safe (coap_opt_t **optp, size_t *length, uint16_t *max_opt)
 Advances *optp to next option if still in PDU. More...
 
size_t coap_pdu_parse_header_size (coap_proto_t proto, const uint8_t *data)
 Interprets data to determine the number of bytes in the header. More...
 
size_t coap_pdu_parse_size (coap_proto_t proto, const uint8_t *data, size_t length)
 Parses data to extract the message size. More...
 
int coap_pdu_parse_header (coap_pdu_t *pdu, coap_proto_t proto)
 Decode the protocol specific header for the specified PDU. More...
 
static int coap_pdu_parse_opt_csm (coap_pdu_t *pdu, uint16_t len)
 
static int coap_pdu_parse_opt_base (coap_pdu_t *pdu, uint16_t len)
 
static int write_prefix (char **obp, size_t *len, const char *prf, size_t prflen)
 
static int write_char (char **obp, size_t *len, char c, int printable)
 
int coap_pdu_parse_opt (coap_pdu_t *pdu)
 Verify consistency in the given CoAP PDU structure and locate the data. More...
 
int coap_pdu_parse (coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
 Parses data into the CoAP PDU structure given in result. More...
 
size_t coap_pdu_encode_header (coap_pdu_t *pdu, coap_proto_t proto)
 Compose the protocol specific header for the specified PDU. More...
 

Variables

error_desc_t coap_error []
 

Macro Definition Documentation

◆ max

#define max (   a,
 
)    ((a) > (b) ? (a) : (b))

Definition at line 31 of file pdu.c.

◆ min

#define min (   a,
 
)    ((a) < (b) ? (a) : (b))

Definition at line 27 of file pdu.c.

Function Documentation

◆ coap_add_data()

int coap_add_data ( coap_pdu_t pdu,
size_t  len,
const uint8_t *  data 
)

Adds given data to the pdu that is passed as first parameter.

Note that the PDU's data is destroyed by coap_add_option(). coap_add_data() must be called only once per PDU, otherwise the result is undefined.

Definition at line 614 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_add_data_after()

uint8_t* coap_add_data_after ( coap_pdu_t pdu,
size_t  len 
)

Adds given data to the pdu that is passed as first parameter but does not copy it.

Note that the PDU's data is destroyed by coap_add_option(). coap_add_data() must be have been called once for this PDU, otherwise the result is undefined. The actual data must be copied at the returned location.

Definition at line 626 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_add_option()

size_t coap_add_option ( coap_pdu_t pdu,
uint16_t  type,
size_t  len,
const uint8_t *  data 
)

Adds option of given type to pdu that is passed as first parameter.

coap_add_option() destroys the PDU's data, so coap_add_data() must be called after all options have been added. As coap_add_token() destroys the options following the token, the token must be added before coap_add_option() is called. This function returns the number of bytes written or 0 on error.

Definition at line 535 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_add_token()

int coap_add_token ( coap_pdu_t pdu,
size_t  len,
const uint8_t *  data 
)

Adds token of length len to pdu.

Adding the token destroys any following contents of the pdu. Hence options and data must be added after coap_add_token() has been called. In pdu, length is set to len + 4, and max_delta is set to 0. This function returns 0 on error or a value greater than zero on success.

Parameters
pduThe PDU where the token is to be added.
lenThe length of the new token.
dataThe token to add.
Returns
A value greater than zero on success, or 0 on error.

Definition at line 249 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_delete_pdu()

void coap_delete_pdu ( coap_pdu_t pdu)

Dispose of an CoAP PDU and frees associated storage.

Not that in general you should not call this function directly. When a PDU is sent with coap_send() or coap_send_large(), coap_delete_pdu() will be called automatically for you.

Parameters
pduThe PDU for free off.

Definition at line 133 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_get_data()

int coap_get_data ( const coap_pdu_t pdu,
size_t *  len,
uint8_t **  data 
)

Retrieves the length and data pointer of specified PDU.

Returns 0 on error or 1 if *len and *data have correct values. Note that these values are destroyed with the pdu.

Definition at line 644 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_get_data_large()

int coap_get_data_large ( const coap_pdu_t pdu,
size_t *  len,
const uint8_t **  data,
size_t *  offset,
size_t *  total 
)

Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.

Note: The data pointed to on return is destroyed when the PDU is destroyed.

Parameters
pduThe specified PDU.
lenReturns the length of the current data
dataReturns the ptr to the current data
offsetReturns the offset of the current data from the start of the body comprising of many blocks (RFC7959)
totalReturns the total size of the body. If offset + length < total, then there is more data to follow.
Returns
1 if len, data, offset and total are correctly filled in, else 0 if there is no data.

Definition at line 658 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_insert_option()

size_t coap_insert_option ( coap_pdu_t pdu,
uint16_t  type,
size_t  len,
const uint8_t *  data 
)

Inserts option of given type in the pdu with the appropriate data.

The option will be inserted in the appropriate place in the options in the pdu.

Internal use only.

Definition at line 405 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_new_pdu()

coap_pdu_t* coap_new_pdu ( const struct coap_session_t session)

Creates a new CoAP PDU.

Parameters
sessionThe session that will be using this PDU
Returns
The skeletal PDU or NULL if failure.

Definition at line 125 of file pdu.c.

+ Here is the call graph for this function:

◆ coap_pdu_check_resize()

int coap_pdu_check_resize ( coap_pdu_t pdu,
size_t  new_size 
)

Dynamically grows the size of pdu to new_size if needed.

The new size must not exceed the PDU's configured maximum size. On success, this function returns 1, otherwise 0.

Internal use only.

Parameters
pduThe PDU to resize.
new_sizeThe new size in bytes.
Returns
1 if the operation succeeded, 0 otherwise.

Definition at line 232 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_pdu_clear()

void coap_pdu_clear ( coap_pdu_t pdu,
size_t  size 
)

Clears any contents from pdu and resets used_size, and data pointers.

max_size is set to size, any other field is set to 0. Note that pdu must be a valid pointer to a coap_pdu_t object created e.g. by coap_pdu_init().

Definition at line 35 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_pdu_duplicate()

coap_pdu_t* coap_pdu_duplicate ( const coap_pdu_t old_pdu,
struct coap_session_t session,
size_t  token_length,
uint8_t *  token,
coap_opt_filter_t drop_options 
)

Duplicate an existing PDU.

Specific options can be ignored and not copied across. The PDU data payload is not copied across.

Parameters
old_pduThe PDU to duplicate
sessionThe session that will be using this PDU.
token_lengthThe length of the token to use in this duplicated PDU.
tokenThe token to use in this duplicated PDU.
drop_optionsA list of options not to copy into the duplicated PDU. If NULL, then all options are copied across.
Returns
The duplicated PDU or NULL if failure.

Definition at line 146 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_pdu_encode_header()

size_t coap_pdu_encode_header ( coap_pdu_t pdu,
coap_proto_t  proto 
)

Compose the protocol specific header for the specified PDU.

Parameters
pduA newly composed PDU.
protoThe target wire protocol.
Returns
Number of header bytes prepended before pdu->token or 0 on error.

Definition at line 1120 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_pdu_init()

coap_pdu_t* coap_pdu_init ( uint8_t  type,
uint8_t  code,
uint16_t  mid,
size_t  size 
)

Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.

The function returns a pointer to the node coap_pdu_t object on success, or NULL on error. The storage allocated for the result must be released with coap_delete_pdu() if coap_send() or coap_send_large() is not called.

Parameters
typeThe type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON, COAP_MESSAGE_ACK, COAP_MESSAGE_RST).
codeThe message code.
midThe transcation id to set or 0 if unknown / not applicable.
sizeThe maximum allowed number of byte for the message.
Returns
A pointer to the new PDU object or NULL on error.

Definition at line 85 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_pdu_parse()

int coap_pdu_parse ( coap_proto_t  proto,
const uint8_t *  data,
size_t  length,
coap_pdu_t pdu 
)

Parses data into the CoAP PDU structure given in result.

The target pdu must be large enough to This function returns 0 on error or a number greater than zero on success.

Parameters
protoSession's protocol
dataThe raw data to parse as CoAP PDU.
lengthThe actual size of data.
pduThe PDU structure to fill. Note that the structure must provide space to hold at least the token and options part of the message.
Returns
1 on success or 0 on error.

Definition at line 1095 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_pdu_parse_header()

int coap_pdu_parse_header ( coap_pdu_t pdu,
coap_proto_t  proto 
)

Decode the protocol specific header for the specified PDU.

Parameters
pduA newly received PDU.
protoThe target wire protocol.
Returns
1 for success or 0 on error.

Definition at line 818 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_pdu_parse_header_size()

size_t coap_pdu_parse_header_size ( coap_proto_t  proto,
const uint8_t *  data 
)

Interprets data to determine the number of bytes in the header.

This function returns 0 on error or a number greater than zero on success.

Parameters
protoSession's protocol
dataThe first byte of raw data to parse as CoAP PDU.
Returns
A value greater than zero on success or 0 on error.

Definition at line 763 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_pdu_parse_opt()

int coap_pdu_parse_opt ( coap_pdu_t pdu)

Verify consistency in the given CoAP PDU structure and locate the data.

This function returns 0 on error or a number greater than zero on success. This function only parses the token and options, up to the payload start marker.

Parameters
pduThe PDU structure to.
Returns
1 on success or 0 on error.

Definition at line 970 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_pdu_parse_opt_base()

static int coap_pdu_parse_opt_base ( coap_pdu_t pdu,
uint16_t  len 
)
static

Definition at line 904 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_pdu_parse_opt_csm()

static int coap_pdu_parse_opt_csm ( coap_pdu_t pdu,
uint16_t  len 
)
static

Definition at line 850 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_pdu_parse_size()

size_t coap_pdu_parse_size ( coap_proto_t  proto,
const uint8_t *  data,
size_t  length 
)

Parses data to extract the message size.

length must be at least coap_pdu_parse_header_size(proto, data). This function returns 0 on error or a number greater than zero on success.

Parameters
protoSession's protocol
dataThe raw data to parse as CoAP PDU.
lengthThe actual size of data.
Returns
A value greater than zero on success or 0 on error.

Definition at line 786 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_pdu_resize()

int coap_pdu_resize ( coap_pdu_t pdu,
size_t  new_size 
)

Dynamically grows the size of pdu to new_size.

The new size must not exceed the PDU's configure maximum size. On success, this function returns 1, otherwise 0.

Parameters
pduThe PDU to resize.
new_sizeThe new size in bytes.
Returns
1 if the operation succeeded, 0 otherwise.

Definition at line 198 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_remove_option()

int coap_remove_option ( coap_pdu_t pdu,
uint16_t  type 
)

Removes option of given type from the pdu.

Internal use only.

Definition at line 305 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_response_phrase()

const char* coap_response_phrase ( unsigned char  code)

Returns a human-readable response phrase for the specified CoAP response code.

This function returns NULL if not found.

Parameters
codeThe response code for which the literal phrase should be retrieved.
Returns
A zero-terminated string describing the error, or NULL if not found.

Definition at line 722 of file pdu.c.

+ Here is the caller graph for this function:

◆ coap_update_option()

size_t coap_update_option ( coap_pdu_t pdu,
uint16_t  type,
size_t  len,
const uint8_t *  data 
)

Updates existing first option of given type in the pdu with the new data.

Internal use only.

Definition at line 494 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_update_token()

int coap_update_token ( coap_pdu_t pdu,
size_t  len,
const uint8_t *  data 
)

Updates token in pdu with length len and data.

This function returns 0 on error or a value greater than zero on success.

Internal use only

Parameters
pduThe PDU where the token is to be updated.
lenThe length of the new token.
dataThe token to add.
Returns
A value greater than zero on success, or 0 on error.

Definition at line 273 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ next_option_safe()

static size_t next_option_safe ( coap_opt_t **  optp,
size_t *  length,
uint16_t *  max_opt 
)
static

Advances *optp to next option if still in PDU.

This function returns the number of bytes opt has been advanced or 0 on error.

Definition at line 738 of file pdu.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write_char()

static int write_char ( char **  obp,
size_t *  len,
char  c,
int  printable 
)
static

Definition at line 950 of file pdu.c.

+ Here is the caller graph for this function:

◆ write_prefix()

static int write_prefix ( char **  obp,
size_t *  len,
const char *  prf,
size_t  prflen 
)
static

Definition at line 937 of file pdu.c.

+ Here is the caller graph for this function:

Variable Documentation

◆ coap_error

error_desc_t coap_error[]
Initial value:
= {
{ COAP_RESPONSE_CODE(201), "Created" },
{ COAP_RESPONSE_CODE(202), "Deleted" },
{ COAP_RESPONSE_CODE(203), "Valid" },
{ COAP_RESPONSE_CODE(204), "Changed" },
{ COAP_RESPONSE_CODE(205), "Content" },
{ COAP_RESPONSE_CODE(231), "Continue" },
{ COAP_RESPONSE_CODE(400), "Bad Request" },
{ COAP_RESPONSE_CODE(401), "Unauthorized" },
{ COAP_RESPONSE_CODE(402), "Bad Option" },
{ COAP_RESPONSE_CODE(403), "Forbidden" },
{ COAP_RESPONSE_CODE(404), "Not Found" },
{ COAP_RESPONSE_CODE(405), "Method Not Allowed" },
{ COAP_RESPONSE_CODE(406), "Not Acceptable" },
{ COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
{ COAP_RESPONSE_CODE(412), "Precondition Failed" },
{ COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
{ COAP_RESPONSE_CODE(415), "Unsupported Content-Format" },
{ COAP_RESPONSE_CODE(500), "Internal Server Error" },
{ COAP_RESPONSE_CODE(501), "Not Implemented" },
{ COAP_RESPONSE_CODE(502), "Bad Gateway" },
{ COAP_RESPONSE_CODE(503), "Service Unavailable" },
{ COAP_RESPONSE_CODE(504), "Gateway Timeout" },
{ COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
{ COAP_RESPONSE_CODE(508), "Hop Limit Reached" },
{ 0, NULL }
}
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:156

Definition at line 693 of file pdu.c.