#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "debug.h"
#include "pdu.h"
#include "option.h"
#include "encode.h"
#include "mem.h"
Go to the source code of this file.
Data Structures | |
struct | error_desc_t |
Functions | |
void | coap_pdu_clear (coap_pdu_t *pdu, size_t size) |
Clears any contents from pdu and resets version field, length and data pointers. More... | |
coap_pdu_t * | coap_pdu_init (unsigned char type, unsigned char code, unsigned short id, size_t size) |
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coap_hdr_t). More... | |
coap_pdu_t * | coap_new_pdu () |
Creates a new CoAP PDU. More... | |
void | coap_delete_pdu (coap_pdu_t *pdu) |
int | coap_add_token (coap_pdu_t *pdu, size_t len, const unsigned char *data) |
Adds token of length len to pdu . More... | |
size_t | coap_add_option (coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data) |
de-duplicate code with coap_add_option_later More... | |
unsigned char * | coap_add_option_later (coap_pdu_t *pdu, unsigned short type, unsigned int len) |
de-duplicate code with coap_add_option More... | |
int | coap_add_data (coap_pdu_t *pdu, unsigned int len, const unsigned char *data) |
Adds given data to the pdu that is passed as first parameter. More... | |
int | coap_get_data (coap_pdu_t *pdu, size_t *len, unsigned char **data) |
Retrieves the length and data pointer of specified PDU. More... | |
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... | |
int | coap_pdu_parse (unsigned char *data, size_t length, coap_pdu_t *pdu) |
Parses data into the CoAP PDU structure given in result . More... | |
Variables | |
error_desc_t | coap_error [] |
int coap_add_data | ( | coap_pdu_t * | pdu, |
unsigned int | len, | ||
const unsigned char * | 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.
size_t coap_add_option | ( | coap_pdu_t * | pdu, |
unsigned short | type, | ||
unsigned int | len, | ||
const unsigned char * | data | ||
) |
unsigned char* coap_add_option_later | ( | coap_pdu_t * | pdu, |
unsigned short | type, | ||
unsigned int | len | ||
) |
int coap_add_token | ( | coap_pdu_t * | pdu, |
size_t | len, | ||
const unsigned char * | 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 funtion 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. void coap_delete_pdu | ( | coap_pdu_t * | pdu | ) |
int coap_get_data | ( | coap_pdu_t * | pdu, |
size_t * | len, | ||
unsigned char ** | data | ||
) |
coap_pdu_t* coap_new_pdu | ( | ) |
Creates a new CoAP PDU.
The object is created on the heap and must be released using coap_delete_pdu();
void coap_pdu_clear | ( | coap_pdu_t * | pdu, |
size_t | size | ||
) |
Clears any contents from pdu
and resets version
field, length
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().
coap_pdu_t* coap_pdu_init | ( | unsigned char | type, |
unsigned char | code, | ||
unsigned short | id, | ||
size_t | size | ||
) |
Creates a new CoAP PDU of given size
(must be large enough to hold the basic CoAP message header (coap_hdr_t).
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().
type | The type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON, COAP_MESSAGE_ACK, COAP_MESSAGE_RST). |
code | The message code. |
id | The message id to set or COAP_INVALID_TID if unknown. |
size | The number of bytes to allocate for the actual message. |
NULL
on error. int coap_pdu_parse | ( | unsigned char * | data, |
size_t | length, | ||
coap_pdu_t * | result | ||
) |
Parses data
into the CoAP PDU structure given in result
.
This function returns 0
on error or a number greater than zero on success.
data | The raw data to parse as CoAP PDU |
length | The actual size of data |
result | The PDU structure to fill. Note that the structure must provide space for at least length bytes to hold the entire CoAP PDU. |
0
on error. 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.
|
static |
error_desc_t coap_error[] |