libcoap  4.2.1
pdu.c File Reference
#include "coap_internal.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.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 tid, 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...
 
int coap_pdu_resize (coap_pdu_t *pdu, size_t new_size)
 Dynamically grows the size of pdu to new_size. More...
 
static int coap_pdu_check_resize (coap_pdu_t *pdu, size_t size)
 
int coap_add_token (coap_pdu_t *pdu, size_t len, const uint8_t *data)
 Adds token of length len to pdu. 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...
 
uint8_tcoap_add_option_later (coap_pdu_t *pdu, uint16_t type, size_t len)
 Adds option of given type to pdu that is passed as first parameter, but does not write a value. 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_tcoap_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 copyt 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...
 
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)
 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...
 
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 30 of file pdu.c.

◆ min

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

Definition at line 26 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 288 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 copyt 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 300 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 215 of file pdu.c.

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

◆ coap_add_option_later()

uint8_t* coap_add_option_later ( coap_pdu_t pdu,
uint16_t  type,
size_t  len 
)

Adds option of given type to pdu that is passed as first parameter, but does not write a value.

It works like coap_add_option with respect to calling sequence (i.e. after token and before data). This function returns a memory address to which the option data has to be written before the PDU can be sent, or NULL on error.

Definition at line 252 of file pdu.c.

+ Here is the call 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 191 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 )

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(), coap_delete_pdu() will be called automatically for you.

Definition at line 127 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 318 of file pdu.c.

+ 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.

Definition at line 119 of file pdu.c.

+ Here is the call graph for this function:

◆ coap_pdu_check_resize()

static int coap_pdu_check_resize ( coap_pdu_t pdu,
size_t  size 
)
static

Definition at line 174 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 34 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 568 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  tid,
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() 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.
tidThe 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 79 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 543 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 460 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 405 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 492 of file pdu.c.

+ Here is the call graph for this function:
+ 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 428 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 140 of file pdu.c.

+ 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 370 of file pdu.c.

+ Here is the caller graph for this function:

◆ next_option_safe()

static size_t next_option_safe ( coap_opt_t **  optp,
size_t *  length 
)
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 386 of file pdu.c.

+ Here is the call graph for this function:
+ 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" },
{ 0, NULL }
}
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:132

Definition at line 342 of file pdu.c.