#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.
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.
|
| 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).
|
| coap_pdu_t * | coap_new_pdu () |
| | Creates a new CoAP PDU.
|
| 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.
|
| size_t | coap_add_option (coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data) |
| | Adds option of given type to pdu that is passed as first parameter.
|
| 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.
|
| int | coap_get_data (coap_pdu_t *pdu, size_t *len, unsigned char **data) |
| | Retrieves the length and data pointer of specified PDU.
|
| char * | coap_response_phrase (unsigned char code) |
| | Returns a human-readable response phrase for the specified CoAP response code.
|
| static size_t | next_option_safe (coap_opt_t **optp, size_t *length) |
| | Advances *optp to next option if still in PDU.
|
| int | coap_pdu_parse (unsigned char *data, size_t length, coap_pdu_t *pdu) |
| | Parses data into the CoAP PDU structure given in result.
|
Function Documentation
| 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.
Definition at line 155 of file pdu.c.
| size_t coap_add_option |
( |
coap_pdu_t * |
pdu, |
|
|
unsigned short |
type, |
|
|
unsigned int |
len, |
|
|
const unsigned char * |
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 124 of file pdu.c.
| 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.
- Parameters
-
| pdu | The PDU where the token is to be added. |
| len | The length of the new token. |
| data | The token to add. |
- Returns
- A value greater than zero on success, or
0 on error.
Definition at line 107 of file pdu.c.
Definition at line 98 of file pdu.c.
| int coap_get_data |
( |
coap_pdu_t * |
pdu, |
|
|
size_t * |
len, |
|
|
unsigned char ** |
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 178 of file pdu.c.
| 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().
Definition at line 43 of file pdu.c.
| 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().
- Parameters
-
| 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. |
- Returns
- A pointer to the new PDU object or
NULL on error.
Definition at line 56 of file pdu.c.
| 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.
- Parameters
-
| 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. |
- Returns
- A value greater than zero on success or
0 on error.
Definition at line 262 of file pdu.c.
| 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
-
| code | The 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 227 of file pdu.c.
| 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 243 of file pdu.c.
Variable Documentation
Initial value:
Definition at line 202 of file pdu.c.