Contiki-NG
coap-engine.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * CoAP engine implementation.
35  * \author
36  * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37  */
38 
39 /**
40  * \addtogroup coap
41  * @{
42  */
43 
44 #ifndef COAP_ENGINE_H_
45 #define COAP_ENGINE_H_
46 
47 typedef struct coap_resource_s coap_resource_t;
48 typedef struct coap_periodic_resource_s coap_periodic_resource_t;
49 
50 #include "coap.h"
51 #include "coap-timer.h"
52 
53 typedef enum {
54  COAP_HANDLER_STATUS_CONTINUE,
55  COAP_HANDLER_STATUS_PROCESSED
56 } coap_handler_status_t;
57 
58 typedef coap_handler_status_t
59 (* coap_handler_callback_t)(coap_message_t *request,
60  coap_message_t *response,
61  uint8_t *buffer, uint16_t buffer_size,
62  int32_t *offset);
63 
64 typedef struct coap_handler coap_handler_t;
65 
66 struct coap_handler {
67  coap_handler_t *next;
68  coap_handler_callback_t handler;
69 };
70 
71 #define COAP_HANDLER(name, handler) \
72  coap_handler_t name = { NULL, handler }
73 
74 void coap_add_handler(coap_handler_t *handler);
75 void coap_remove_handler(coap_handler_t *handler);
76 
77 void coap_engine_init(void);
78 
79 int coap_receive(const coap_endpoint_t *src,
80  uint8_t *payload, uint16_t payload_length);
81 
82 coap_handler_status_t coap_call_handlers(coap_message_t *request,
83  coap_message_t *response,
84  uint8_t *buffer,
85  uint16_t buffer_size,
86  int32_t *offset);
87 /*---------------------------------------------------------------------------*/
88 /* signatures of handler functions */
89 typedef void (* coap_resource_handler_t)(coap_message_t *request,
90  coap_message_t *response,
91  uint8_t *buffer,
92  uint16_t preferred_size,
93  int32_t *offset);
94 typedef void (* coap_resource_periodic_handler_t)(void);
95 typedef void (* coap_resource_response_handler_t)(void *data,
96  coap_message_t *response);
97 typedef void (* coap_resource_trigger_handler_t)(void);
98 
99 /* data structure representing a resource in CoAP */
100 struct coap_resource_s {
101  coap_resource_t *next; /* for LIST, points to next resource defined */
102  const char *url; /*handled URL */
103  coap_resource_flags_t flags; /* handled CoAP methods */
104  const char *attributes; /* link-format attributes */
105  coap_resource_handler_t get_handler; /* handler function */
106  coap_resource_handler_t post_handler; /* handler function */
107  coap_resource_handler_t put_handler; /* handler function */
108  coap_resource_handler_t delete_handler; /* handler function */
109  union {
110  coap_periodic_resource_t *periodic; /* special data depending on flags */
111  coap_resource_trigger_handler_t trigger;
112  coap_resource_trigger_handler_t resume;
113  };
114 };
115 
116 struct coap_periodic_resource_s {
117  uint32_t period;
118  coap_timer_t periodic_timer;
119  const coap_resource_periodic_handler_t periodic_handler;
120 };
121 
122 /*
123  * Macro to define a CoAP resource.
124  * Resources are statically defined for the sake of efficiency and better memory management.
125  */
126 #define RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler) \
127  coap_resource_t name = { NULL, NULL, NO_FLAGS, attributes, get_handler, post_handler, put_handler, delete_handler, { NULL } }
128 
129 #define PARENT_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler) \
130  coap_resource_t name = { NULL, NULL, HAS_SUB_RESOURCES, attributes, get_handler, post_handler, put_handler, delete_handler, { NULL } }
131 
132 #define SEPARATE_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler, resume_handler) \
133  coap_resource_t name = { NULL, NULL, IS_SEPARATE, attributes, get_handler, post_handler, put_handler, delete_handler, { .resume = resume_handler } }
134 
135 #define EVENT_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler, event_handler) \
136  coap_resource_t name = { NULL, NULL, IS_OBSERVABLE, attributes, get_handler, post_handler, put_handler, delete_handler, { .trigger = event_handler } }
137 
138 /*
139  * Macro to define a periodic resource.
140  * The corresponding [name]_periodic_handler() function will be called every period.
141  * For instance polling a sensor and publishing a changed value to subscribed clients would be done there.
142  */
143 #define PERIODIC_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler, period, periodic_handler) \
144  static coap_periodic_resource_t periodic_##name = { period, { 0 }, periodic_handler }; \
145  coap_resource_t name = { NULL, NULL, IS_OBSERVABLE | IS_PERIODIC, attributes, get_handler, post_handler, put_handler, delete_handler, { .periodic = &periodic_##name } }
146 
147 /*---------------------------------------------------------------------------*/
148 /**
149  *
150  * \brief Resources wanted to be accessible should be activated with the following code.
151  * \param resource
152  * A CoAP resource defined through the RESOURCE macros.
153  * \param path
154  * The local URI path where to provide the resource.
155  */
156 void coap_activate_resource(coap_resource_t *resource, const char *path);
157 /*---------------------------------------------------------------------------*/
158 /**
159  * \brief Returns the first of registered CoAP resources.
160  * \return The first registered CoAP resource or NULL if none exists.
161  */
162 coap_resource_t *coap_get_first_resource(void);
163 /*---------------------------------------------------------------------------*/
164 /**
165  * \brief Returns the next registered CoAP resource.
166  * \return The next resource or NULL if no more exists.
167  */
168 coap_resource_t *coap_get_next_resource(coap_resource_t *resource);
169 /*---------------------------------------------------------------------------*/
170 
171 #include "coap-transactions.h"
172 #include "coap-observe.h"
173 #include "coap-separate.h"
174 #include "coap-observe-client.h"
175 #include "coap-transport.h"
176 
177 #endif /* COAP_ENGINE_H_ */
178 /** @} */
CoAP module for observing resources (draft-ietf-core-observe-11).
CoAP timer API.
void coap_activate_resource(coap_resource_t *resource, const char *path)
Makes a resource available under the given URI path.
Definition: coap-engine.c:399
CoAP module for separate responses.
CoAP module for reliable transport
An implementation of the Constrained Application Protocol (RFC 7252).
coap_resource_t * coap_get_first_resource(void)
Returns the first of registered CoAP resources.
Definition: coap-engine.c:424
coap_resource_t * coap_get_next_resource(coap_resource_t *resource)
Returns the next registered CoAP resource.
Definition: coap-engine.c:430
coap_resource_flags_t
Resource flags for allowed methods and special functionalities.
API for CoAP transport