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
sol-coap.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <sol-socket.h>
22 #include <sol-network.h>
23 #include <sol-str-slice.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
100 };
101 
116 };
117 
133 #define SOL_COAP_REQUEST_MASK 0x07
134 
172 };
173 
183 #define sol_coap_make_response_code(clas, det) ((clas << 5) | (det))
184 
213 };
214 
220 #define SOL_COAP_CODE_EMPTY (0)
221 
233 };
234 
242 };
243 
249 struct sol_coap_packet;
251 
257 struct sol_coap_server;
259 
268 typedef struct sol_coap_resource {
269 #ifndef SOL_NO_API_VERSION
270 #define SOL_COAP_RESOURCE_API_VERSION (1)
271 
272  uint16_t api_version;
273 #endif
274 
286  int (*get)(void *data, struct sol_coap_server *server,
287  const struct sol_coap_resource *resource,
288  struct sol_coap_packet *req,
289  const struct sol_network_link_addr *cliaddr);
302  int (*post)(void *data, struct sol_coap_server *server,
303  const struct sol_coap_resource *resource,
304  struct sol_coap_packet *req,
305  const struct sol_network_link_addr *cliaddr);
318  int (*put)(void *data, struct sol_coap_server *server,
319  const struct sol_coap_resource *resource,
320  struct sol_coap_packet *req,
321  const struct sol_network_link_addr *cliaddr);
334  int (*del)(void *data, struct sol_coap_server *server,
335  const struct sol_coap_resource *resource,
336  struct sol_coap_packet *req,
337  const struct sol_network_link_addr *cliaddr);
348  struct sol_str_slice path[];
350 
359 int sol_coap_header_get_version(const struct sol_coap_packet *pkt, uint8_t *version);
360 
369 int sol_coap_header_get_type(const struct sol_coap_packet *pkt, uint8_t *type);
370 
383 uint8_t *sol_coap_header_get_token(const struct sol_coap_packet *pkt, uint8_t *len);
384 
397 int sol_coap_header_get_code(const struct sol_coap_packet *pkt, uint8_t *code);
398 
407 int sol_coap_header_get_id(const struct sol_coap_packet *pkt, uint16_t *id);
408 
420 int sol_coap_header_set_version(struct sol_coap_packet *pkt, uint8_t ver);
421 
432 int sol_coap_header_set_type(struct sol_coap_packet *pkt, uint8_t type);
433 
450 int sol_coap_header_set_token(struct sol_coap_packet *pkt, uint8_t *token, uint8_t tokenlen);
451 
463 int sol_coap_header_set_code(struct sol_coap_packet *pkt, uint8_t code);
464 
478 int sol_coap_header_set_id(struct sol_coap_packet *pkt, uint16_t id);
479 
490 
512 struct sol_coap_server *sol_coap_server_new(const struct sol_network_link_addr *addr, bool secure);
513 
529  const struct sol_network_link_addr *addr,
530  enum sol_socket_dtls_cipher *cipher_suites,
531  uint16_t cipher_suites_len);
532 
543 
553 
570 
583 
602  struct sol_coap_resource *resource);
603 
614 
622 void sol_coap_packet_unref(struct sol_coap_packet *pkt);
623 
649 int sol_coap_packet_get_payload(struct sol_coap_packet *pkt, struct sol_buffer **buf, size_t *offset);
650 
662 
675 int sol_coap_add_option(struct sol_coap_packet *pkt, uint16_t code, const void *value, uint16_t len);
676 
685 int sol_coap_packet_add_uri_path_option(struct sol_coap_packet *pkt, const char *uri);
686 
697 const void *sol_coap_find_first_option(const struct sol_coap_packet *pkt, uint16_t code, uint16_t *len);
698 
710 int sol_coap_find_options(const struct sol_coap_packet *pkt, uint16_t code, struct sol_str_slice *vec, uint16_t veclen);
711 
733  const struct sol_network_link_addr *cliaddr);
734 
775  const struct sol_network_link_addr *cliaddr,
776  bool (*reply_cb)(void *data, struct sol_coap_server *server,
777  struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr), const void *data);
778 
797 int sol_coap_notify(struct sol_coap_server *server,
798  struct sol_coap_resource *resource, struct sol_coap_packet *pkt);
799 
821  struct sol_coap_resource *resource,
822  int (*cb)(void *cb_data, struct sol_coap_server *server,
823  struct sol_coap_resource *resource, struct sol_network_link_addr *addr,
824  struct sol_coap_packet **pkt),
825  const void *cb_data);
826 
841  const struct sol_coap_resource *resource, const void *data);
842 
857  const struct sol_coap_resource *resource);
858 
878 int sol_coap_path_to_buffer(const struct sol_str_slice path[],
879  struct sol_buffer *buf, size_t offset, size_t *size);
880 
895 int sol_coap_cancel_send_packet(struct sol_coap_server *server, struct sol_coap_packet *pkt, struct sol_network_link_addr *cliaddr);
896 
918 int sol_coap_unobserve_by_token(struct sol_coap_server *server, const struct sol_network_link_addr *cliaddr, uint8_t *token, uint8_t tkl);
919 
920 
934  int (*handler)(void *data, struct sol_coap_server *server,
935  struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr),
936  const void *data);
937 
945 #ifdef SOL_LOG_ENABLED
946 void sol_coap_packet_debug(struct sol_coap_packet *pkt);
947 #else
948 static inline void
950 {
951 }
952 #endif
953 
954 
959 #ifdef __cplusplus
960 }
961 #endif
Definition: sol-coap.h:193
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.
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.
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.
int(* post)(void *data, struct sol_coap_server *server, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr)
POST request.
Definition: sol-coap.h:302
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.
Definition: sol-coap.h:98
int sol_coap_header_set_code(struct sol_coap_packet *pkt, uint8_t code)
Sets the code of the message.
Confirmable messsage.
Definition: sol-coap.h:148
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.
Definition: sol-coap.h:211
sol_coap_message_type
CoAP packets may be of one of these types.
Definition: sol-coap.h:138
sol_coap_content_type
Some content-types available for use with the CONTENT_FORMAT option.
Definition: sol-coap.h:227
int(* put)(void *data, struct sol_coap_server *server, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr)
PUT request.
Definition: sol-coap.h:318
int sol_coap_header_set_id(struct sol_coap_packet *pkt, uint16_t id)
Sets the message ID.
Definition: sol-coap.h:200
int(* del)(void *data, struct sol_coap_server *server, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr)
DELETE request.
Definition: sol-coap.h:334
struct sol_coap_packet * sol_coap_packet_ref(struct sol_coap_packet *pkt)
Take a reference of the given packet.
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.
struct sol_coap_resource sol_coap_resource
Description for a CoAP resource.
Definition: sol-coap.h:201
Definition: sol-coap.h:229
Definition: sol-coap.h:195
Definition: sol-coap.h:89
Definition: sol-coap.h:228
struct sol_str_slice path[]
Path representing the resource.
Definition: sol-coap.h:348
int sol_coap_header_get_id(const struct sol_coap_packet *pkt, uint16_t *id)
Gets the message ID.
static struct sol_http_server * server
Definition: server-https.c:52
uint8_t * sol_coap_header_get_token(const struct sol_coap_packet *pkt, uint8_t *len)
Gets the token of the packet, if any.
static void sol_coap_packet_debug(struct sol_coap_packet *pkt)
Print information about the packet pkt.
Definition: sol-coap.h:949
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.
#define sol_coap_make_response_code(clas, det)
Macro to create a response code.
Definition: sol-coap.h:183
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.
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.
static struct sol_buffer value
Definition: server.c:42
Definition: sol-coap.h:91
Definition: sol-coap.h:232
void sol_coap_server_unref(struct sol_coap_server *server)
Release a reference from the given server.
int sol_coap_header_set_type(struct sol_coap_packet *pkt, uint8_t type)
Sets the message type in the packet's header.
String slice type.
Definition: sol-str-slice.h:84
A POST request.
Definition: sol-coap.h:111
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.
A DELETE request.
Definition: sol-coap.h:115
Definition: sol-coap.h:192
sol_coap_response_code
Set of response codes available for a response packet.
Definition: sol-coap.h:190
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.
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.
struct sol_coap_packet sol_coap_packet
Opaque handler for a CoAP packet.
Definition: sol-coap.h:250
int sol_coap_header_get_code(const struct sol_coap_packet *pkt, uint8_t *code)
Gets the request/response code in the packet.
struct sol_coap_packet * sol_coap_packet_new(struct sol_coap_packet *old)
Creates a new CoAP packet.
Definition: sol-coap.h:194
Definition: sol-coap.h:94
Definition: sol-coap.h:209
struct sol_coap_server * sol_coap_server_new(const struct sol_network_link_addr *addr, bool secure)
Creates a new CoAP server instance.
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.
Definition: sol-coap.h:239
static bool reply_cb(void *data, struct sol_coap_server *server, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr)
Definition: simple-client.c:72
int sol_coap_server_unregister_resource(struct sol_coap_server *server, const struct sol_coap_resource *resource)
Unregisters a resource from the server.
int sol_coap_header_set_token(struct sol_coap_packet *pkt, uint8_t *token, uint8_t tokenlen)
Sets a token in the packet.
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...
int sol_coap_header_set_version(struct sol_coap_packet *pkt, uint8_t ver)
Sets the CoAP protocol version in the packet's header.
If the resource should be exported in the CoRE well-known registry.
Definition: sol-coap.h:241
Reset.
Definition: sol-coap.h:171
Definition: sol-coap.h:203
Definition: sol-coap.h:202
sol_coap_method
Available request methods.
Definition: sol-coap.h:107
enum sol_coap_flags flags
Bitwise OR-ed flags from sol_coap_flags, if any is necessary.
Definition: sol-coap.h:341
Definition: sol-coap.h:196
struct sol_coap_server * sol_coap_server_ref(struct sol_coap_server *server)
Take a reference of the given server.
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.
sol_coap_flags
Flags accepted by a sol_coap_resource.
Definition: sol-coap.h:238
sol_coap_option
Set of CoAP packet options we are aware of.
Definition: sol-coap.h:84
Definition: sol-coap.h:92
void sol_coap_packet_unref(struct sol_coap_packet *pkt)
Release a reference from the given packet.
Description for a CoAP resource.
Definition: sol-coap.h:268
Definition: sol-coap.h:99
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. ...
These are routines that Soletta provides for its string slice implementation.
Definition: sol-coap.h:87
Definition: sol-coap.h:199
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.
Non-confirmable message.
Definition: sol-coap.h:157
Definition: sol-coap.h:198
Definition: sol-coap.h:85
Definition: sol-coap.h:95
bool sol_coap_packet_has_payload(struct sol_coap_packet *pkt)
Checks if the given packet contains a payload.
Definition: sol-coap.h:96
Definition: sol-coap.h:231
Definition: sol-coap.h:88
Definition: sol-coap.h:86
uint16_t api_version
API version.
Definition: sol-coap.h:272
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.
int sol_coap_header_get_version(const struct sol_coap_packet *pkt, uint8_t *version)
Gets the CoAP protocol version of the packet.
Definition: sol-coap.h:197
sol_socket_dtls_cipher
Represents supported Cipher Suites for use with DTLS.
Definition: sol-socket.h:47
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
A GET request.
Definition: sol-coap.h:109
Definition: sol-coap.h:97
A PUT request.
Definition: sol-coap.h:113
Definition: sol-coap.h:191
Definition: sol-coap.h:208
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.
Definition: sol-coap.h:207
Definition: sol-coap.h:90
struct sol_coap_server sol_coap_server
Opaque handler for a CoAP server.
Definition: sol-coap.h:258
Acknowledge.
Definition: sol-coap.h:164
These are routines that Soletta provides for handling network link interfaces, making it possible to ...
size_t len
Slice length.
Definition: sol-str-slice.h:85
Definition: sol-coap.h:93