libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Data Structures | Functions | Variables
pdu.c File Reference
#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_tcoap_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_tcoap_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 []
 

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

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

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

void coap_delete_pdu ( coap_pdu_t pdu)

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

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();

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

Definition at line 126 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
typeThe type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON, COAP_MESSAGE_ACK, COAP_MESSAGE_RST).
codeThe message code.
idThe message id to set or COAP_INVALID_TID if unknown.
sizeThe number of bytes to allocate for the actual message.
Returns
A pointer to the new PDU object or NULL on error.

Definition at line 82 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
dataThe raw data to parse as CoAP PDU
lengthThe actual size of data
resultThe 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 345 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
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 310 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 326 of file pdu.c.

Variable Documentation

error_desc_t coap_error[]
Initial value:
= {
{ COAP_RESPONSE_CODE(65), "2.01 Created" },
{ COAP_RESPONSE_CODE(66), "2.02 Deleted" },
{ COAP_RESPONSE_CODE(67), "2.03 Valid" },
{ COAP_RESPONSE_CODE(68), "2.04 Changed" },
{ COAP_RESPONSE_CODE(69), "2.05 Content" },
{ 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(408), "Request Entity Incomplete" },
{ COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
{ COAP_RESPONSE_CODE(415), "Unsupported Media Type" },
{ 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:92

Definition at line 285 of file pdu.c.