libcoap
4.3.0rc1
|
#include "coap2/coap_internal.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
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_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. More... | |
coap_pdu_t * | coap_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_t * | coap_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 [] |
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.
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.
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.
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.
pdu | The PDU where the token is to be added. |
len | The length of the new token. |
data | The token to add. |
0
on error. Definition at line 249 of file pdu.c.
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.
pdu | The PDU for free off. |
Definition at line 133 of file pdu.c.
int coap_get_data | ( | const coap_pdu_t * | pdu, |
size_t * | len, | ||
uint8_t ** | data | ||
) |
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.
pdu | The specified PDU. |
len | Returns the length of the current data |
data | Returns the ptr to the current data |
offset | Returns the offset of the current data from the start of the body comprising of many blocks (RFC7959) |
total | Returns the total size of the body. If offset + length < total, then there is more data to follow. |
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.
size_t coap_insert_option | ( | coap_pdu_t * | pdu, |
uint16_t | type, | ||
size_t | len, | ||
const uint8_t * | data | ||
) |
coap_pdu_t* coap_new_pdu | ( | const struct coap_session_t * | session | ) |
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.
pdu | The PDU to resize. |
new_size | The new size in bytes. |
Definition at line 232 of file pdu.c.
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.
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.
old_pdu | The PDU to duplicate |
session | The session that will be using this PDU. |
token_length | The length of the token to use in this duplicated PDU. |
token | The token to use in this duplicated PDU. |
drop_options | A list of options not to copy into the duplicated PDU. If NULL , then all options are copied across. |
NULL
if failure. Definition at line 146 of file pdu.c.
size_t coap_pdu_encode_header | ( | coap_pdu_t * | pdu, |
coap_proto_t | proto | ||
) |
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.
type | The type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON, COAP_MESSAGE_ACK, COAP_MESSAGE_RST). |
code | The message code. |
mid | The transcation id to set or 0 if unknown / not applicable. |
size | The maximum allowed number of byte for the message. |
NULL
on error. Definition at line 85 of file pdu.c.
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.
proto | Session's protocol |
data | The raw data to parse as CoAP PDU. |
length | The actual size of data . |
pdu | The PDU structure to fill. Note that the structure must provide space to hold at least the token and options part of the message. |
0
on error. Definition at line 1095 of file pdu.c.
int coap_pdu_parse_header | ( | coap_pdu_t * | pdu, |
coap_proto_t | proto | ||
) |
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.
proto | Session's protocol |
data | The first byte of raw data to parse as CoAP PDU. |
0
on error. Definition at line 763 of file pdu.c.
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.
pdu | The PDU structure to. |
0
on error. Definition at line 970 of file pdu.c.
|
static |
|
static |
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.
proto | Session's protocol |
data | The raw data to parse as CoAP PDU. |
length | The actual size of data . |
0
on error. Definition at line 786 of file pdu.c.
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.
pdu | The PDU to resize. |
new_size | The new size in bytes. |
Definition at line 198 of file pdu.c.
int coap_remove_option | ( | coap_pdu_t * | pdu, |
uint16_t | type | ||
) |
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.
code | The response code for which the literal phrase should be retrieved. |
NULL
if not found. Definition at line 722 of file pdu.c.
size_t coap_update_option | ( | coap_pdu_t * | pdu, |
uint16_t | type, | ||
size_t | len, | ||
const uint8_t * | data | ||
) |
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
pdu | The PDU where the token is to be updated. |
len | The length of the new token. |
data | The token to add. |
0
on error. Definition at line 273 of file pdu.c.
|
static |
|
static |
|
static |
error_desc_t coap_error[] |