pdu.c File Reference
`#include "coap_config.h"`
`#include <stdlib.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include "debug.h"`
`#include "pdu.h"`
`#include "option.h"`
`#include "encode.h"`
`#include "mem.h"`
Include dependency graph for pdu.c:

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 (void)
| 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 []

## Function Documentation

## coap_add_data()

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

Here is the caller graph for this function:

## coap_add_option()

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

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

Definition at line 171 of file pdu.c.

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

## coap_add_option_later()

unsigned char* coap_add_option_later | ( | coap_pdu_t * | pdu,
|  | unsigned short | type,
|  | unsigned int | len
| ) |  |

de-duplicate code with coap_add_option

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

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

Here is the caller graph for this function:

## coap_delete_pdu()

void coap_delete_pdu | ( | coap_pdu_t * | pdu | ) |

Definition at line 136 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 | ( | 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 257 of file pdu.c.

Here is the caller graph for this function:

## coap_new_pdu()

coap_pdu_t* coap_new_pdu | ( | void |  | ) |

Creates a new CoAP PDU.

The object is created on the heap and must be released using coap_delete_pdu();

Deprecated:
  This function allocates the maximum storage for each PDU. Use coap_pdu_init() instead.

Definition at line 119 of file pdu.c.

Here is the call 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 `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 29 of file pdu.c.

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

## 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().

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 75 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 | ( | 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 344 of file pdu.c.

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

## coap_response_phrase()

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 309 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 325 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 }
}
COAP_RESPONSE_CODE
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:96

Definition at line 281 of file pdu.c.