Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Enumerations | Functions

API to handle Constrained Application Protocol (CoAP) protocol. More...

Data Structures

struct  sol_coap_resource
 Description for a CoAP resource. More...
 

Macros

#define SOL_COAP_CODE_EMPTY   (0)
 Macro to indicates that the header code was not set. More...
 
#define sol_coap_make_response_code(clas, det)   ((clas << 5) | (det))
 Macro to create a response code. More...
 
#define SOL_COAP_REQUEST_MASK   0x07
 Mask of CoAP requests. More...
 

Typedefs

typedef struct sol_coap_packet sol_coap_packet
 Opaque handler for a CoAP packet. More...
 
typedef struct sol_coap_resource sol_coap_resource
 Description for a CoAP resource. More...
 
typedef struct sol_coap_server sol_coap_server
 Opaque handler for a CoAP server. More...
 

Enumerations

enum  sol_coap_content_type {
  SOL_COAP_CONTENT_TYPE_NONE = -1, SOL_COAP_CONTENT_TYPE_TEXT_PLAIN = 0, SOL_COAP_CONTENT_TYPE_APPLICATION_LINK_FORMAT = 40, SOL_COAP_CONTENT_TYPE_APPLICATION_CBOR = 60,
  SOL_COAP_CONTENT_TYPE_APPLICATION_JSON = 50
}
 Some content-types available for use with the CONTENT_FORMAT option. More...
 
enum  sol_coap_flags { SOL_COAP_FLAGS_NONE = 0, SOL_COAP_FLAGS_WELL_KNOWN = (1 << 1) }
 Flags accepted by a sol_coap_resource. More...
 
enum  sol_coap_message_type { SOL_COAP_MESSAGE_TYPE_CON = 0, SOL_COAP_MESSAGE_TYPE_NON_CON = 1, SOL_COAP_MESSAGE_TYPE_ACK = 2, SOL_COAP_MESSAGE_TYPE_RESET = 3 }
 CoAP packets may be of one of these types. More...
 
enum  sol_coap_method { SOL_COAP_METHOD_GET = 1, SOL_COAP_METHOD_POST = 2, SOL_COAP_METHOD_PUT = 3, SOL_COAP_METHOD_DELETE = 4 }
 Available request methods. More...
 
enum  sol_coap_option {
  SOL_COAP_OPTION_IF_MATCH = 1, SOL_COAP_OPTION_URI_HOST = 3, SOL_COAP_OPTION_ETAG = 4, SOL_COAP_OPTION_IF_NONE_MATCH = 5,
  SOL_COAP_OPTION_OBSERVE = 6, SOL_COAP_OPTION_URI_PORT = 7, SOL_COAP_OPTION_LOCATION_PATH = 8, SOL_COAP_OPTION_URI_PATH = 11,
  SOL_COAP_OPTION_CONTENT_FORMAT = 12, SOL_COAP_OPTION_MAX_AGE = 14, SOL_COAP_OPTION_URI_QUERY = 15, SOL_COAP_OPTION_ACCEPT = 17,
  SOL_COAP_OPTION_LOCATION_QUERY = 20, SOL_COAP_OPTION_PROXY_URI = 35, SOL_COAP_OPTION_PROXY_SCHEME = 39
}
 Set of CoAP packet options we are aware of. More...
 
enum  sol_coap_response_code {
  SOL_COAP_RESPONSE_CODE_OK = sol_coap_make_response_code(2, 0), SOL_COAP_RESPONSE_CODE_CREATED = sol_coap_make_response_code(2, 1), SOL_COAP_RESPONSE_CODE_DELETED = sol_coap_make_response_code(2, 2), SOL_COAP_RESPONSE_CODE_VALID = sol_coap_make_response_code(2, 3),
  SOL_COAP_RESPONSE_CODE_CHANGED = sol_coap_make_response_code(2, 4), SOL_COAP_RESPONSE_CODE_CONTENT = sol_coap_make_response_code(2, 5), SOL_COAP_RESPONSE_CODE_BAD_REQUEST = sol_coap_make_response_code(4, 0), SOL_COAP_RESPONSE_CODE_UNAUTHORIZED = sol_coap_make_response_code(4, 1),
  SOL_COAP_RESPONSE_CODE_BAD_OPTION = sol_coap_make_response_code(4, 2), SOL_COAP_RESPONSE_CODE_FORBIDDEN = sol_coap_make_response_code(4, 3), SOL_COAP_RESPONSE_CODE_NOT_FOUND = sol_coap_make_response_code(4, 4), SOL_COAP_RESPONSE_CODE_NOT_ALLOWED = sol_coap_make_response_code(4, 5),
  SOL_COAP_RESPONSE_CODE_NOT_ACCEPTABLE = sol_coap_make_response_code(4, 6), SOL_COAP_RESPONSE_CODE_PRECONDITION_FAILED = sol_coap_make_response_code(4, 12), SOL_COAP_RESPONSE_CODE_REQUEST_TOO_LARGE = sol_coap_make_response_code(4, 13), SOL_COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT = sol_coap_make_response_code(4, 15),
  SOL_COAP_RESPONSE_CODE_INTERNAL_ERROR = sol_coap_make_response_code(5, 0), SOL_COAP_RESPONSE_CODE_NOT_IMPLEMENTED = sol_coap_make_response_code(5, 1), SOL_COAP_RESPONSE_CODE_BAD_GATEWAY = sol_coap_make_response_code(5, 2), SOL_COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE = sol_coap_make_response_code(5, 3),
  SOL_COAP_RESPONSE_CODE_GATEWAY_TIMEOUT = sol_coap_make_response_code(5, 4), SOL_COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED = sol_coap_make_response_code(5, 5)
}
 Set of response codes available for a response packet. More...
 

Functions

int sol_coap_add_option (struct sol_coap_packet *pkt, uint16_t code, const void *value, uint16_t len)
 Adds an option to the CoAP packet. More...
 
int sol_coap_cancel_send_packet (struct sol_coap_server *server, struct sol_coap_packet *pkt, struct sol_network_link_addr *cliaddr)
 Cancel a packet sent using sol_coap_send_packet() or sol_coap_send_packet_with_reply() functions. More...
 
const void * sol_coap_find_first_option (const struct sol_coap_packet *pkt, uint16_t code, uint16_t *len)
 Finds the first apeearence of the specified option in a packet. More...
 
int sol_coap_find_options (const struct sol_coap_packet *pkt, uint16_t code, struct sol_str_slice *vec, uint16_t veclen)
 Gets a number of specified option in a packet. More...
 
int sol_coap_header_get_code (const struct sol_coap_packet *pkt, uint8_t *code)
 Gets the request/response code in the packet. More...
 
int sol_coap_header_get_id (const struct sol_coap_packet *pkt, uint16_t *id)
 Gets the message ID. More...
 
uint8_t * sol_coap_header_get_token (const struct sol_coap_packet *pkt, uint8_t *len)
 Gets the token of the packet, if any. More...
 
int sol_coap_header_get_type (const struct sol_coap_packet *pkt, uint8_t *type)
 Gets the type of the message container in the packet. More...
 
int sol_coap_header_get_version (const struct sol_coap_packet *pkt, uint8_t *version)
 Gets the CoAP protocol version of the packet. More...
 
int sol_coap_header_set_code (struct sol_coap_packet *pkt, uint8_t code)
 Sets the code of the message. More...
 
int sol_coap_header_set_id (struct sol_coap_packet *pkt, uint16_t id)
 Sets the message ID. More...
 
int sol_coap_header_set_token (struct sol_coap_packet *pkt, uint8_t *token, uint8_t tokenlen)
 Sets a token in the packet. More...
 
int sol_coap_header_set_type (struct sol_coap_packet *pkt, uint8_t type)
 Sets the message type in the packet's header. More...
 
int sol_coap_header_set_version (struct sol_coap_packet *pkt, uint8_t ver)
 Sets the CoAP protocol version in the packet's header. More...
 
int sol_coap_notify (struct sol_coap_server *server, struct sol_coap_resource *resource, struct sol_coap_packet *pkt)
 Sends the notification packet to all registered observers. More...
 
int sol_coap_notify_by_callback (struct sol_coap_server *server, struct sol_coap_resource *resource, int(*cb)(void *cb_data, struct sol_coap_server *server, struct sol_coap_resource *resource, struct sol_network_link_addr *addr, struct sol_coap_packet **pkt), const void *cb_data)
 Sends the notification packet returned by a callback to all registered observers. More...
 
int sol_coap_packet_add_uri_path_option (struct sol_coap_packet *pkt, const char *uri)
 Convenience function to add the the SOL_COAP_OPTION_URI_PATH from a string. More...
 
static void sol_coap_packet_debug (struct sol_coap_packet *pkt)
 Print information about the packet pkt. More...
 
int sol_coap_packet_get_payload (struct sol_coap_packet *pkt, struct sol_buffer **buf, size_t *offset)
 Gets a pointer to the packet's payload. More...
 
bool sol_coap_packet_has_payload (struct sol_coap_packet *pkt)
 Checks if the given packet contains a payload. More...
 
struct sol_coap_packetsol_coap_packet_new (struct sol_coap_packet *old)
 Creates a new CoAP packet. More...
 
struct sol_coap_packetsol_coap_packet_new_notification (struct sol_coap_server *server, struct sol_coap_resource *resource)
 Convenience function to create a packet suitable to send as a notification. More...
 
struct sol_coap_packetsol_coap_packet_new_request (enum sol_coap_method method, enum sol_coap_message_type type)
 Convenience function to create a new packet for a request. More...
 
struct sol_coap_packetsol_coap_packet_ref (struct sol_coap_packet *pkt)
 Take a reference of the given packet. More...
 
void sol_coap_packet_unref (struct sol_coap_packet *pkt)
 Release a reference from the given packet. More...
 
int sol_coap_path_to_buffer (const struct sol_str_slice path[], struct sol_buffer *buf, size_t offset, size_t *size)
 Inserts a path given in slices form to a sol-buffer, at a given offset. More...
 
int sol_coap_send_packet (struct sol_coap_server *server, struct sol_coap_packet *pkt, const struct sol_network_link_addr *cliaddr)
 Sends a packet to the given address. More...
 
int sol_coap_send_packet_with_reply (struct sol_coap_server *server, struct sol_coap_packet *pkt, const struct sol_network_link_addr *cliaddr, bool(*reply_cb)(void *data, struct sol_coap_server *server, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr), const void *data)
 Sends a packet to the given address. More...
 
bool sol_coap_server_is_secure (const struct sol_coap_server *server)
 Check if a given CoAP server instance is running over DTLS or not. More...
 
struct sol_coap_serversol_coap_server_new (const struct sol_network_link_addr *addr, bool secure)
 Creates a new CoAP server instance. More...
 
struct sol_coap_serversol_coap_server_new_by_cipher_suites (const struct sol_network_link_addr *addr, enum sol_socket_dtls_cipher *cipher_suites, uint16_t cipher_suites_len)
 Creates a new DTLS-Secured CoAP server instance. More...
 
struct sol_coap_serversol_coap_server_ref (struct sol_coap_server *server)
 Take a reference of the given server. More...
 
int sol_coap_server_register_resource (struct sol_coap_server *server, const struct sol_coap_resource *resource, const void *data)
 Register a sol_coap_resource with the server. More...
 
int sol_coap_server_set_unknown_resource_handler (struct sol_coap_server *server, int(*handler)(void *data, struct sol_coap_server *server, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr), const void *data)
 Register a unknown handler callback. More...
 
void sol_coap_server_unref (struct sol_coap_server *server)
 Release a reference from the given server. More...
 
int sol_coap_server_unregister_resource (struct sol_coap_server *server, const struct sol_coap_resource *resource)
 Unregisters a resource from the server. More...
 
int sol_coap_unobserve_by_token (struct sol_coap_server *server, const struct sol_network_link_addr *cliaddr, uint8_t *token, uint8_t tkl)
 Remove observation identified by token from server. More...
 

Detailed Description

API to handle Constrained Application Protocol (CoAP) protocol.

The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained (e.g., low-power, lossy) networks. The nodes often have 8-bit microcontrollers with small amounts of ROM and RAM, while constrained networks such as IPv6 over Low-Power Wireless Personal Area Networks (6LoWPANs) often have high packet error rates and a typical throughput of 10s of kbit/s. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.

CoAP provides a request/response interaction model between application endpoints, supports built-in discovery of services and resources, and includes key concepts of the Web such as URIs and Internet media types. CoAP is designed to easily interface with HTTP for integration with the Web while meeting specialized requirements such as multicast support, very low overhead, and simplicity for constrained environments.

Relevant RFCs:

Macro Definition Documentation

#define SOL_COAP_CODE_EMPTY   (0)

Macro to indicates that the header code was not set.

To be used with sol_coap_header_set_code()

#define sol_coap_make_response_code (   clas,
  det 
)    ((clas << 5) | (det))

Macro to create a response code.

CoAP message code field is a 8-bit unsigned integer, composed by 3 most significants bits representing class (0-7) and 5 bits representing detail (00-31).

It's usually represented as "c.dd". E.g.: 2.00 for "OK".

#define SOL_COAP_REQUEST_MASK   0x07

Mask of CoAP requests.

It may be used to identify, given a code, if it's a request or a response.

Example to identify a request:

struct sol_coap_packet *req = X;
{
// do something
}

Typedef Documentation

Opaque handler for a CoAP packet.

Description for a CoAP resource.

CoAP servers will want to register resources, so that clients can act on them, by fetching their state or requesting updates to them. These resources are registered using this struct and the sol_coap_server_register_resource() function.

Opaque handler for a CoAP server.

Enumeration Type Documentation

Some content-types available for use with the CONTENT_FORMAT option.

Refer to RFC 7252, section 12.3 for more information.

Enumerator
SOL_COAP_CONTENT_TYPE_NONE 
SOL_COAP_CONTENT_TYPE_TEXT_PLAIN 
SOL_COAP_CONTENT_TYPE_APPLICATION_LINK_FORMAT 
SOL_COAP_CONTENT_TYPE_APPLICATION_CBOR 
SOL_COAP_CONTENT_TYPE_APPLICATION_JSON 

Flags accepted by a sol_coap_resource.

Enumerator
SOL_COAP_FLAGS_NONE 
SOL_COAP_FLAGS_WELL_KNOWN 

If the resource should be exported in the CoRE well-known registry.

CoAP packets may be of one of these types.

Enumerator
SOL_COAP_MESSAGE_TYPE_CON 

Confirmable messsage.

Packet is a request or response that the destination end-point must acknowledge. If received and processed properly, it will receive a response of matching id and type SOL_COAP_MESSAGE_TYPE_ACK. If the recipient could not process the request, it will reply with a matching id and type SOL_COAP_MESSAGE_TYPE_RESET.

SOL_COAP_MESSAGE_TYPE_NON_CON 

Non-confirmable message.

Packet is a request or response that does not need acknowledge. Destinataries should not reply to them with an ACK, but may respond with a message of type SOL_COAP_MESSAGE_TYPE_RESET if the package could not be processed due to being faulty.

SOL_COAP_MESSAGE_TYPE_ACK 

Acknowledge.

When a confirmable message is received, a response should be sent to the source with the same id and this type.

SOL_COAP_MESSAGE_TYPE_RESET 

Reset.

Rejecting a packet for any reason is done by sending a message of this type with the id of the corresponding source message.

Available request methods.

To be used with sol_coap_header_set_code() when crafting a request.

Enumerator
SOL_COAP_METHOD_GET 

A GET request.

SOL_COAP_METHOD_POST 

A POST request.

SOL_COAP_METHOD_PUT 

A PUT request.

SOL_COAP_METHOD_DELETE 

A DELETE request.

Set of CoAP packet options we are aware of.

Users may add options other than these to their packets, provided they know how to format them correctly. The only restriction is that all options must be added to a packet in numeric order.

See Also
sol_coap_find_first_option()

Refer to RFC 7252, section 12.2 for more information.

Enumerator
SOL_COAP_OPTION_IF_MATCH 
SOL_COAP_OPTION_URI_HOST 
SOL_COAP_OPTION_ETAG 
SOL_COAP_OPTION_IF_NONE_MATCH 
SOL_COAP_OPTION_OBSERVE 
SOL_COAP_OPTION_URI_PORT 
SOL_COAP_OPTION_LOCATION_PATH 
SOL_COAP_OPTION_URI_PATH 
SOL_COAP_OPTION_CONTENT_FORMAT 
SOL_COAP_OPTION_MAX_AGE 
SOL_COAP_OPTION_URI_QUERY 
SOL_COAP_OPTION_ACCEPT 
SOL_COAP_OPTION_LOCATION_QUERY 
SOL_COAP_OPTION_PROXY_URI 
SOL_COAP_OPTION_PROXY_SCHEME 

Set of response codes available for a response packet.

To be used with sol_coap_header_set_code() when crafting a reply.

Enumerator
SOL_COAP_RESPONSE_CODE_OK 
SOL_COAP_RESPONSE_CODE_CREATED 
SOL_COAP_RESPONSE_CODE_DELETED 
SOL_COAP_RESPONSE_CODE_VALID 
SOL_COAP_RESPONSE_CODE_CHANGED 
SOL_COAP_RESPONSE_CODE_CONTENT 
SOL_COAP_RESPONSE_CODE_BAD_REQUEST 
SOL_COAP_RESPONSE_CODE_UNAUTHORIZED 
SOL_COAP_RESPONSE_CODE_BAD_OPTION 
SOL_COAP_RESPONSE_CODE_FORBIDDEN 
SOL_COAP_RESPONSE_CODE_NOT_FOUND 
SOL_COAP_RESPONSE_CODE_NOT_ALLOWED 
SOL_COAP_RESPONSE_CODE_NOT_ACCEPTABLE 
SOL_COAP_RESPONSE_CODE_PRECONDITION_FAILED 
SOL_COAP_RESPONSE_CODE_REQUEST_TOO_LARGE 
SOL_COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT 
SOL_COAP_RESPONSE_CODE_INTERNAL_ERROR 
SOL_COAP_RESPONSE_CODE_NOT_IMPLEMENTED 
SOL_COAP_RESPONSE_CODE_BAD_GATEWAY 
SOL_COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE 
SOL_COAP_RESPONSE_CODE_GATEWAY_TIMEOUT 
SOL_COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED 

Function Documentation

int sol_coap_add_option ( struct sol_coap_packet pkt,
uint16_t  code,
const void *  value,
uint16_t  len 
)

Adds an option to the CoAP packet.

Options must be added in order, according to their numeric definitions.

Parameters
pktThe packet to which the option will be added.
codeThe option code, one of sol_coap_option.
valuePointer to the value of the option, or NULL if none.
lenLength in bytes of value, or 0 if value is NULL.
Returns
0 on success, -errno on failure.
Examples:
/src/samples/coap/simple-client.c.

Referenced by disable_observing(), and main().

int sol_coap_cancel_send_packet ( struct sol_coap_server server,
struct sol_coap_packet pkt,
struct sol_network_link_addr cliaddr 
)

Cancel a packet sent using sol_coap_send_packet() or sol_coap_send_packet_with_reply() functions.

For observating packets, an unobserve packet will be sent to server and no more replies will be processed.

Parameters
serverThe server through which the packet was sent.
pktThe packet sent.
cliaddrThe source address of the sent packet.
Returns
0 on success -ENOENT if the packet was already canceled -EINVAL if some parameter is invalid.
const void* sol_coap_find_first_option ( const struct sol_coap_packet pkt,
uint16_t  code,
uint16_t *  len 
)

Finds the first apeearence of the specified option in a packet.

Parameters
pktThe packet holding the options.
codeThe option code to look for. Pass one of the sol_coap_option values or any custom option code.
lenThe length in bytes of the option's value.
Returns
Pointer to the option's value, or NULL if not found.
int sol_coap_find_options ( const struct sol_coap_packet pkt,
uint16_t  code,
struct sol_str_slice vec,
uint16_t  veclen 
)

Gets a number of specified option in a packet.

Parameters
pktThe packet holding the options.
codeThe option code to look for. Pass one of the sol_coap_option values or any custom option code.
vecAn vector of struct sol_str_slice to hold the options.
veclenThe length of vec.
Returns
The number of options found, negative errno otherwise.
int sol_coap_header_get_code ( const struct sol_coap_packet pkt,
uint8_t *  code 
)

Gets the request/response code in the packet.

If the packet is a request, the code returned is one of sol_coap_method. If it's a response, it will be one of sol_coap_response_code. If the code was not set, the returned code will be SOL_COAP_CODE_EMPTY.

Parameters
pktThe packet to get the code from.
codeThe request/response code.
Returns
0 on success, negative number on error.
int sol_coap_header_get_id ( const struct sol_coap_packet pkt,
uint16_t *  id 
)

Gets the message ID.

Parameters
pktThe packet from which to get the message ID.
idThe message ID.
Returns
0 on success, negative number on error.
uint8_t* sol_coap_header_get_token ( const struct sol_coap_packet pkt,
uint8_t *  len 
)

Gets the token of the packet, if any.

If the packet contains a token, this function can retrieve it.

Parameters
pktThe packet with the token.
lenPointer where to store the length in bytes of the token.
Returns
NULL in case of error (when errno will be set to EINVAL), otherwise a pointer to an internal buffer containint the token. It must not be modified.
int sol_coap_header_get_type ( const struct sol_coap_packet pkt,
uint8_t *  type 
)

Gets the type of the message container in the packet.

Parameters
pktThe packet containing the message.
typeThe type of the message, one of sol_coap_message_type.
Returns
0 on success, negative number on error.
int sol_coap_header_get_version ( const struct sol_coap_packet pkt,
uint8_t *  version 
)

Gets the CoAP protocol version of the packet.

Parameters
pktThe packet to get version from.
versionThe CoAP protocol version the packet implements.
Returns
0 on success, negative number on error.
int sol_coap_header_set_code ( struct sol_coap_packet pkt,
uint8_t  code 
)

Sets the code of the message.

For requests, it must be one of sol_coap_method. For responses, it must be one of sol_coap_response_code.

Parameters
pktThe packet on which to set the code.
codeThe request/response code.
Returns
0 on success, negative error code on failure.
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by disable_observing(), light_method_get(), light_method_put(), and update_light().

int sol_coap_header_set_id ( struct sol_coap_packet pkt,
uint16_t  id 
)

Sets the message ID.

CoAP packets require an ID to identify a request, so that their response can be matched by it. It's not usually necessary to call this function, as packets created by sol_coap_packet_new() will have generated an ID already,

Parameters
pktThe packet on which to set the ID.
idThe ID to set.
Returns
0 on success, negative error code on failure.
int sol_coap_header_set_token ( struct sol_coap_packet pkt,
uint8_t *  token,
uint8_t  tokenlen 
)

Sets a token in the packet.

Tokens can be used, besides the ID, to identify a specific request. For OBSERVE requests, the server will send notifications with the same token used to make the request.

Parameters
pktThe packet on which to set the token.
tokenThe token to set, can be any array of bytes.
tokenlenThe length in bytes of token.
Returns
0 on success, negative error code on failure.
Warning
This function is meant to be used only once per packet, more than that will lead to undefined behavior
Examples:
/src/samples/coap/simple-client.c.

Referenced by main().

int sol_coap_header_set_type ( struct sol_coap_packet pkt,
uint8_t  type 
)

Sets the message type in the packet's header.

Must be one of sol_coap_message_type.

Parameters
pktThe packet to set the type to.
typeThe message type to set.
Returns
0 on success, negative error code on failure.
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by disable_observing(), light_method_get(), and light_method_put().

int sol_coap_header_set_version ( struct sol_coap_packet pkt,
uint8_t  ver 
)

Sets the CoAP protocol version in the packet's header.

Usually not needed, as packets created with sol_coap_packet_new() will already have the version set.

Parameters
pktThe packet to set the version.
verThe version to set.
Returns
0 on success, negative error code on failure.
int sol_coap_notify ( struct sol_coap_server server,
struct sol_coap_resource resource,
struct sol_coap_packet pkt 
)

Sends the notification packet to all registered observers.

Sends the notification pkt to all the registered observers for the resource resource, setting the correct token for each instance.

Parameters
serverThe server through which the notifications will be sent.
resourceThe resource the notification is about.
pktThe notification packet to send.
Returns
0 on success, -errno on failure.
Note
This function will take the reference of the given pkt and do a release its memory even on errors.
See Also
sol_coap_send_packet()
sol_coap_send_packet_with_reply()
Examples:
/src/samples/coap/simple-server.c.

Referenced by update_light().

int sol_coap_notify_by_callback ( struct sol_coap_server server,
struct sol_coap_resource resource,
int(*)(void *cb_data, struct sol_coap_server *server, struct sol_coap_resource *resource, struct sol_network_link_addr *addr, struct sol_coap_packet **pkt)  cb,
const void *  cb_data 
)

Sends the notification packet returned by a callback to all registered observers.

Sends the notification pkt to all the registered observers for the resource resource, setting the correct token for each instance. The pkt will be filled by a callback cb given to this function, probably based on the observer's address and/or any other used data.

Parameters
serverThe server through which the notifications will be sent.
resourceThe resource the notification is about.
cbA callback that is used to create the pkt - data User data; server The server through which the notifications will be sent; resource The resource the notification is about; addr The address of the observer; pkt The pkt to be filled.
cb_dataThe user data to cb.
Returns
0 on success, -errno on failure.
See Also
sol_coap_notify()
sol_coap_send_packet()
sol_coap_send_packet_with_reply()
int sol_coap_packet_add_uri_path_option ( struct sol_coap_packet pkt,
const char *  uri 
)

Convenience function to add the the SOL_COAP_OPTION_URI_PATH from a string.

Parameters
pktThe packet to the add the path to.
uriThe path to add, must start with '/'.
Returns
0 on success, -errno on failure.
static void sol_coap_packet_debug ( struct sol_coap_packet pkt)
inlinestatic

Print information about the packet pkt.

Used for debug purposes.

Parameters
pktThe packet to be debuged.
int sol_coap_packet_get_payload ( struct sol_coap_packet pkt,
struct sol_buffer **  buf,
size_t *  offset 
)

Gets a pointer to the packet's payload.

When creating a packet, first set all the packet header parameters and options, then use this function to get a pointer to the packet's payload buffer. One may then append content to the payload buffer (all sol-buffer appending functions will do). Note that only appending is permitted, otherwise the packet headers/options – that are also payload – will get corrupted.

Getting the payload pointer marks the end of the header and options in the packet, so after that point, it's no longer possible to set any of those.

When receiving a packet, first check if it contains a payload with sol_coap_packet_has_payload(), and if so, this function will return in buf the packet's buffer and in offset, where in that buffer the user's payload actually begins.

Parameters
pktThe packet to fetch the payload of.
bufWhere to store the address of the payload buffer.
offsetWhere to store the offset, in buf, where the actual payload starts.
Returns
0 on success, a negative error code on failure.
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by light_method_get(), light_method_put(), reply_cb(), and update_light().

bool sol_coap_packet_has_payload ( struct sol_coap_packet pkt)

Checks if the given packet contains a payload.

For packets received as a request or response, this function checks if they contain a payload. It should be used before calling sol_coap_packet_get_payload().

Parameters
pktThe packet to check.
Returns
True if the packet contains a payload, false otherwise.
struct sol_coap_packet* sol_coap_packet_new ( struct sol_coap_packet old)

Creates a new CoAP packet.

Creates a packet to send as a request or response. If old is not NULL, its ID and token (if any) will be copied to the new packet. This is useful when crafting a new packet as a response to old. It's also important to note that if old has sol_coap_message_type equal to SOL_COAP_MESSAGE_TYPE_CON, the new packet message type will be set to SOL_COAP_MESSAGE_TYPE_ACK. If it has it equal to SOL_COAP_MESSAGE_TYPE_NON_CON, the new packet message type will be set to SOL_COAP_MESSAGE_TYPE_NON_CON.

Parameters
oldAn optional packet to use as basis.
Returns
A new packet, or NULL on failure.
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by disable_observing(), light_method_get(), and light_method_put().

struct sol_coap_packet* sol_coap_packet_new_notification ( struct sol_coap_server server,
struct sol_coap_resource resource 
)

Convenience function to create a packet suitable to send as a notification.

When notifying observing clients of changes in a resource, this function simplifies the creation of the notification packet by handling the management of the resource age (and setting the id) and type of the packet.

It should be used along sol_coap_notify() to ensure that the correct token is added to the packet sent to the clients.

Parameters
serverThe server holding the resource that changed.
resourceThe resource the notification is about.
Returns
A new packet, with id and type set accordingly, or NULL on failure.
See Also
sol_coap_notify()
Examples:
/src/samples/coap/simple-server.c.

Referenced by update_light().

struct sol_coap_packet* sol_coap_packet_new_request ( enum sol_coap_method  method,
enum sol_coap_message_type  type 
)

Convenience function to create a new packet for a request.

Creates a new packet, automatically setting a new id, and setting the request method to method and message type to type.

Parameters
methodThe method to use for the request.
typeThe message type.
Returns
A new packet, with id, method and type already set.
Examples:
/src/samples/coap/simple-client.c.

Referenced by main().

struct sol_coap_packet* sol_coap_packet_ref ( struct sol_coap_packet pkt)

Take a reference of the given packet.

Increment the reference count of the packet, if it's still valid.

Parameters
pktThe packet to reference.
Returns
The same packet, with refcount increased, or NULL if invalid.
void sol_coap_packet_unref ( struct sol_coap_packet pkt)

Release a reference from the given packet.

When the last reference is released, the packet will be freed.

Parameters
pktThe packet to release.
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by disable_observing(), light_method_get(), light_method_put(), and update_light().

int sol_coap_path_to_buffer ( const struct sol_str_slice  path[],
struct sol_buffer buf,
size_t  offset,
size_t *  size 
)

Inserts a path given in slices form to a sol-buffer, at a given offset.

Takes a path, as respresented by sol_coap_resource and inserts all its components, in order, separated by '/', to buf's offset position.

Parameters
pathArray of slices, last one empty, representing the path.
bufBuffer where to append the string version of the path.
offsetWhere to insert the paths string into buf.
sizePointer where to store the number of bytes written to buf If NULL, it will be ignored.
Returns
0 on success, negative error code on failure
Warning
buf has be initialized before this call and must be free/finished afterwards
Examples:
/src/samples/coap/simple-server.c.

Referenced by light_resource_to_rep().

int sol_coap_send_packet ( struct sol_coap_server server,
struct sol_coap_packet pkt,
const struct sol_network_link_addr cliaddr 
)

Sends a packet to the given address.

Sends the packet pkt to the destination address especified by cliaddr, which may be a multicast address for discovery purposes.

If a response is expected, then sol_coap_send_packet_with_reply() should be used instead.

Note
This function will take the reference of the given pkt and do a release its memory even on errors.
Parameters
serverThe server through which the packet will be sent.
pktThe packet to send.
cliaddrThe recipient address.
Returns
0 on success, -errno otherwise.
See Also
sol_coap_send_packet_with_reply()
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by disable_observing(), light_method_get(), and light_method_put().

int sol_coap_send_packet_with_reply ( struct sol_coap_server server,
struct sol_coap_packet pkt,
const struct sol_network_link_addr cliaddr,
bool(*)(void *data, struct sol_coap_server *server, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr)  reply_cb,
const void *  data 
)

Sends a packet to the given address.

Sends the packet pkt to the destination address especified by cliaddr, which may be a multicast address for discovery purposes, and issues a given callback when a response arrives.

If reply_cb is NULL, this function will behave exactly like sol_coap_send_packet(). If a valid function is passed, when a response is received, the function reply_cb will be called. As long as this function returns true, server will continue waiting for more responses. When it returns false, the internal response handler will be freed and any new reply that may arrive for this request will be ignored. For unobserving packets, server will also be notified using an unobserve packet.

After an internal timeout is reached, reply_cb will be called with NULL req and cliaddr. The same behavior is expected for its return: if true, server will issue a new timeout and continue waiting responses until it ends, otherwise server will terminate response waiting.

Warning
If pkt has the SOL_COAP_OPTION_OBSERVE option and at least one response arrives before the internal timeout and reply_cb returns true, that will be interpreted as if the user wishes to wait for responses indefinetely and no timeout will apply anymore. The user is then responsible for cancelling the request with sol_coap_cancel_send_packet().
Note
This function will take the reference of the given pkt.
Parameters
serverThe server through which the packet will be sent.
pktThe packet to send.
cliaddrThe recipient address.
reply_cbThe function to call when a response is received.
dataThe user data pointer to pass to the reply_cb function.
Returns
0 on success, a negative error code otherwise.
Examples:
/src/samples/coap/simple-client.c.

Referenced by main().

bool sol_coap_server_is_secure ( const struct sol_coap_server server)

Check if a given CoAP server instance is running over DTLS or not.

Parameters
serverThe server to be checked.
Returns
True if this server uses DTLS to encrypt communication with its endpoints; false otherwise.
struct sol_coap_server* sol_coap_server_new ( const struct sol_network_link_addr addr,
bool  secure 
)

Creates a new CoAP server instance.

Creates a new, CoAP server instance listening on address addr. If the server cannot be created, NULL will be returned and errno will be set to indicate the reason.

Parameters
addrThe address where the server will listen on.
secureSet to true to create a server that will encrypt communication with its endpoints using DTLS or false otherwise.
Note
If secure is true, this function will create a sol_coap_server over a DTLS Socket supporting only the SOL_SOCKET_DTLS_CIPHER_PSK_AES128_CCM8 Cipher Suite. The recommended function for creating secure sol_coap_server is sol_coap_server_new_by_cipher_suites.
Returns
A new server instance, or NULL in case of failure.
See Also
sol_coap_server_new_by_cipher_suites
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by main().

struct sol_coap_server* sol_coap_server_new_by_cipher_suites ( const struct sol_network_link_addr addr,
enum sol_socket_dtls_cipher cipher_suites,
uint16_t  cipher_suites_len 
)

Creates a new DTLS-Secured CoAP server instance.

Creates a new, DTLS-Secured CoAP server instance listening on address addr. If the server cannot be created, NULL will be returned and errno will be set to indicate the reason.

Parameters
addrThe address where the server will listen on.
cipher_suitesIndicates the desired cipher suites to use.
cipher_suites_lenIndicates the length of the cipher_suites array.
Returns
A new server instance, or NULL in case of failure.
struct sol_coap_server* sol_coap_server_ref ( struct sol_coap_server server)

Take a reference of the given server.

Increment the reference count of the server, if it's still valid.

Parameters
serverThe server to reference.
Returns
The same server, with refcount increased, or NULL if invalid.
int sol_coap_server_register_resource ( struct sol_coap_server server,
const struct sol_coap_resource resource,
const void *  data 
)

Register a sol_coap_resource with the server.

Registers the given resource with the server, in order to be able to handle requests related to it made by clients. The same resource can be registered on multiple servers.

Parameters
serverThe server on which the resource wil be registered.
resourceThe resource to register.
dataUser data pointer that will be passed to the requests callbacks.
Returns
0 on success, negative errno on error.
Examples:
/src/samples/coap/simple-server.c.

Referenced by main().

int sol_coap_server_set_unknown_resource_handler ( struct sol_coap_server server,
int(*)(void *data, struct sol_coap_server *server, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr)  handler,
const void *  data 
)

Register a unknown handler callback.

Every time the server receives a request for a sol_coap_resource that was not registered with sol_coap_server_register_resource() the handler will be called.

Parameters
serverThe server to register the unknown resource handler.
handlerThe unknown handler callback or NULL to unregister.
dataThe data to handler
Returns
0 on success -EINVAL if server is NULL.
void sol_coap_server_unref ( struct sol_coap_server server)

Release a reference from the given server.

When the last reference is released, the server and all resources associated with it will be freed.

Parameters
serverThe server to release.
Examples:
/src/samples/coap/simple-client.c, and /src/samples/coap/simple-server.c.

Referenced by main().

int sol_coap_server_unregister_resource ( struct sol_coap_server server,
const struct sol_coap_resource resource 
)

Unregisters a resource from the server.

Unregisters a resource previously registered with sol_coap_server_register_resource(). This specific server will no longer handle requests made to the given resource, but if its registered in other servers, it may still be found by clients.

Parameters
serverThe server from which to unregister the resource.
resourceThe resource to unregister.
Returns
0 on success, -ENOENT if the resource was no registered to this server. -EINVAL if some parameter is invalid.
int sol_coap_unobserve_by_token ( struct sol_coap_server server,
const struct sol_network_link_addr cliaddr,
uint8_t *  token,
uint8_t  tkl 
)

Remove observation identified by token from server.

Send to server an unobserve packet so client identified by token will be removed from the server's observation list. We are suppose to stop receiving new notifications.

If observation was added using sol_coap_send_packet_with_reply() function we will have no more calls to reply_cb.

Parameters
serverThe server through which the observation packet was sent.
cliaddrThe source address of the observation packet sent.
tokenThe observation token
tklThe observation token length
Returns
0 on success. -ENOENT if there is no observe packet registered with token and no unobserve packet was sent. -ENOMEM Out of memory -EINVAL if some parameter is invalid.