Modules#

CoAP type definitions

CoAP#

The Wi-SUN CoAP component provides an application layer implementation on top of the Wi-SUN stack and its socket API. The CoAP protocol is designed as a specialized Internet Application Protocol for constrained devices in lossy networks (refer to RFC 7252 for additional information). The Wi-SUN FAN networks fit exactly this definition. The CoAP implementation relies on the UDP transport layer to send and receive packets.

A number of helper functions are part of the component to assist with building CoAP payloads, parsing CoAP packets, comparing URI (Uniform Resource Identifier) paths, freeing CoAP packets, and more. The CoAP component can rely on either a static or dynamic memory pool to store CoAP packets. The dynamic implementation uses OS-dependent memory allocation and memory release APIs. The static implementation is only available when using FreeRTOS.

To use the CoAP component in your application, add it to your project and initialize it with sl_wisun_coap_init().

Functions#

bool
sl_wisun_coap_init(const sl_wisun_coap_tx_callback tx_callback, const sl_wisun_coap_rx_callback rx_callback, const sl_wisun_coap_version_t version)

Initialize Wi-SUN CoAP.

bool

Initialize Wi-SUN CoAP default.

bool

Get lib state.

void *
sl_wisun_coap_malloc(uint16_t size)

Implement malloc.

void
sl_wisun_coap_free(void *addr)

Free Wi-SUN CoAP.

sl_wisun_coap_parser(uint16_t packet_data_len, uint8_t *packet_data)

CoAP parser wrapper function.

uint16_t
sl_wisun_coap_builder_calc_size(const sl_wisun_coap_packet_t *message)

CoAP packet calc size wrapper.

int16_t
sl_wisun_coap_builder(uint8_t *dest_buff, const sl_wisun_coap_packet_t *message)

CoAP message builder Wi-SUN.

sl_wisun_coap_build_response(const sl_wisun_coap_packet_t *req, sl_wisun_coap_message_code_t msg_code)

Build generic response for request wrapper function.

void
sl_wisun_coap_print_packet(const sl_wisun_coap_packet_t *packet, const bool hex_format)

Print CoAP packets and all of attached buffer, payload, token, uri_path.

const char *
sl_wisun_coap_get_parsed_uri_path(char *dest_buff, const uint16_t buff_length)

Get the last parsed URI path.

bool
sl_wisun_coap_get_payload(const sl_wisun_coap_packet_t *source_packet, uint8_t *dest_buff, const uint16_t buff_length)

Get payload.

void
sl_wisun_coap_set_payload(sl_wisun_coap_packet_t *dest_packet, uint8_t *source_buff, const uint16_t buff_length)

Set payload.

bool
sl_wisun_coap_get_token(const sl_wisun_coap_packet_t *source_packet, uint8_t *dest_buff, const uint16_t buff_length)

Get token.

void
sl_wisun_coap_set_token(sl_wisun_coap_packet_t *dest_packet, uint8_t *source_buff, const uint8_t buff_length)

Set token.

bool
sl_wisun_coap_get_options_list(const sl_wisun_coap_packet_t *source_packet, sl_wisun_coap_option_list_t *dest)

Get options list.

void
sl_wisun_coap_set_options_list(sl_wisun_coap_packet_t *dest_packet, sl_wisun_coap_option_list_t *source)

Set options list.

Get the library handler pointer from the internal handler structure.

void
sl_wisun_coap_destroy_packet(sl_wisun_coap_packet_t *packet)

Destroy packet.

bool
sl_wisun_coap_compare_uri_path(const char *uri_path, const uint16_t buff_max_len)

Compare URI path with the stored from the previous parsed packet.

Function Documentation#

sl_wisun_coap_init#

bool sl_wisun_coap_init (const sl_wisun_coap_tx_callback tx_callback, const sl_wisun_coap_rx_callback rx_callback, const sl_wisun_coap_version_t version)

Initialize Wi-SUN CoAP.

Parameters
[in]tx_callback

TX callback, if it's NULL, the default callback is applied

[in]rx_callback

RX callback, if it's NULL, the default callback is applied

[in]version

CoAP version

Set the Wi-SUN CoAP internal descriptor Returns

  • true Proper initialization

  • false Error


Definition at line 130 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_init_default#

static bool sl_wisun_coap_init_default (void)

Initialize Wi-SUN CoAP default.

Parameters
N/A

initialize the internal descriptor with default values Returns

  • true Proper initialization

  • false Error


Definition at line 140 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_is_initialized#

bool sl_wisun_coap_is_initialized (void)

Get lib state.

Parameters
N/A

Get initialized state of the CoAP library to decide init is need or not Returns

  • true Initialized

  • false Not initialized


Definition at line 151 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_malloc#

void* sl_wisun_coap_malloc (uint16_t size)

Implement malloc.

Parameters
N/Asize

size for malloc

OS dependent thread-safe implementation Returns

  • void* the memory pointer


Definition at line 159 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_free#

void sl_wisun_coap_free (void *addr)

Free Wi-SUN CoAP.

Parameters
N/Aaddr

address ptr

OS dependent thread-safe implementation


Definition at line 166 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_parser#

sl_wisun_coap_packet_t* sl_wisun_coap_parser (uint16_t packet_data_len, uint8_t *packet_data)

CoAP parser wrapper function.

Parameters
[in]packet_data_len

packet data size

[in]packet_data

packet data ptr

Used sn_coap_parser


Definition at line 174 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_builder_calc_size#

uint16_t sl_wisun_coap_builder_calc_size (const sl_wisun_coap_packet_t *message)

CoAP packet calc size wrapper.

Parameters
[in]message

message ptr

Used sn_coap_builder_calc_needed_packet_data_size Returns

  • uint16_t size


Definition at line 183 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_builder#

int16_t sl_wisun_coap_builder (uint8_t *dest_buff, const sl_wisun_coap_packet_t *message)

CoAP message builder Wi-SUN.

Parameters
[out]dest_buff

destination buffer for raw message

[in]message

message structure

Used sl_wisun_coap_builder Returns

  • int16_t On success bytes of built message, on failure -1 if coap header structure is wrong -2 if NULL ptr set as argument


Definition at line 194 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_build_response#

sl_wisun_coap_packet_t* sl_wisun_coap_build_response (const sl_wisun_coap_packet_t *req, sl_wisun_coap_message_code_t msg_code)

Build generic response for request wrapper function.

Parameters
[in]req

request

[in]msg_code

message code to build

Used sn_coap_build_response Returns

  • sl_wisun_coap_header_t* built packet ptr on the heap


Definition at line 204 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_print_packet#

void sl_wisun_coap_print_packet (const sl_wisun_coap_packet_t *packet, const bool hex_format)

Print CoAP packets and all of attached buffer, payload, token, uri_path.

Parameters
[in]packet

packet to print

[in]hex_format

hex format bool to decide buffer print format

Pretty printer function, with hex format option for buffers


Definition at line 213 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_get_parsed_uri_path#

const char* sl_wisun_coap_get_parsed_uri_path (char *dest_buff, const uint16_t buff_length)

Get the last parsed URI path.

Parameters
N/Adest_buff

destination buff

N/Abuff_length

destination buffer size

Thread safe way to get the URI path from last parsed packet Returns

  • const char* On success, pointer to the destination buffer, NULL on failure


Definition at line 223 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_get_payload#

bool sl_wisun_coap_get_payload (const sl_wisun_coap_packet_t *source_packet, uint8_t *dest_buff, const uint16_t buff_length)

Get payload.

Parameters
[in]source_packet

source packet

[out]dest_buff

destination buffer

[in]buff_length

length of destination buffer

Thread safe deep copy Returns

  • true Success

  • false Failure


Definition at line 235 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_set_payload#

void sl_wisun_coap_set_payload (sl_wisun_coap_packet_t *dest_packet, uint8_t *source_buff, const uint16_t buff_length)

Set payload.

Parameters
[out]dest_packet

destination packet

[in]source_buff

source buffer

[in]buff_length

source buffer length

Set the corresponding packet member ptr and length


Definition at line 245 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_get_token#

bool sl_wisun_coap_get_token (const sl_wisun_coap_packet_t *source_packet, uint8_t *dest_buff, const uint16_t buff_length)

Get token.

Parameters
[in]source_packet

source packet

[out]dest_buff

destination buffer

[in]buff_length

length of destination buffer

Thread safe deep copy Returns

  • true Success

  • false Failure


Definition at line 257 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_set_token#

void sl_wisun_coap_set_token (sl_wisun_coap_packet_t *dest_packet, uint8_t *source_buff, const uint8_t buff_length)

Set token.

Parameters
N/Adest_packet

destination packet

N/Asource_buff

source buffer

N/Abuff_length

source buffer length

Set the corresponding packet member ptr and length


Definition at line 268 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_get_options_list#

bool sl_wisun_coap_get_options_list (const sl_wisun_coap_packet_t *source_packet, sl_wisun_coap_option_list_t *dest)

Get options list.

Parameters
N/Asource_packet

source packet

N/Adest

destination option list structure

Thread safe deep copy Returns

  • true Success

  • false Failure


Definition at line 280 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_set_options_list#

void sl_wisun_coap_set_options_list (sl_wisun_coap_packet_t *dest_packet, sl_wisun_coap_option_list_t *source)

Set options list.

Parameters
[out]dest_packet

destination packet ptr

[in]source

source option list

Set the option list pointer.


Definition at line 289 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_get_lib_handler#

const sl_wisun_coap_handle_t* sl_wisun_coap_get_lib_handler (void)

Get the library handler pointer from the internal handler structure.

Parameters
N/A

Not thread safe! It is needed only to use Pelion mbed-coap library functions Returns

  • const sl_wisun_coap_handle_t* pointer to the lib handler


Definition at line 297 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_destroy_packet#

void sl_wisun_coap_destroy_packet (sl_wisun_coap_packet_t *packet)

Destroy packet.

Parameters
N/Apacket

packet

It must be used to avoid memory leaks! Free the all of allocated buffer for packet


Definition at line 305 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h

sl_wisun_coap_compare_uri_path#

bool sl_wisun_coap_compare_uri_path (const char *uri_path, const uint16_t buff_max_len)

Compare URI path with the stored from the previous parsed packet.

Parameters
N/Auri_path

URI path

N/Abuff_max_len

buffer size of the URI path

Thread safe way to compare URI pathes. The buffer length must be set, eg strlen(uri_path) + 1 Returns

  • true Matched

  • false Not matched


Definition at line 316 of file C:/silabs/wisun_gsdk/app/wisun/component/coap/sl_wisun_coap.h