Contiki 3.x
rest.h
1 #ifndef REST_H_
2 #define REST_H_
3 
4 /*includes*/
5 #include "contiki.h"
6 #include "contiki-lib.h"
7 
8 #ifdef WITH_COAP
9  #include "coap-common.h"
10 #include "coap-server.h"
11  #define REQUEST coap_packet_t
12  #define RESPONSE coap_packet_t
13  #define SERVER_PROCESS (&coap_server)
14 #else /*WITH_COAP*/
15  /*WITH_HTTP*/
16  #include "http-common.h"
17  #include "http-server.h"
18  #define REQUEST http_request_t
19  #define RESPONSE http_response_t
20  #define SERVER_PROCESS (&http_server)
21 #endif /*WITH_COAP*/
22 
23 struct resource_t;
24 
25 /*REST method types*/
26 typedef enum {
27  METHOD_GET = (1 << 0),
28  METHOD_POST = (1 << 1),
29  METHOD_PUT = (1 << 2),
30  METHOD_DELETE = (1 << 3)
31 } method_t;
32 
33 /*Signature of handler functions*/
34 typedef void (*restful_handler) (REQUEST* request, RESPONSE* response);
35 typedef int (*restful_pre_handler) (REQUEST* request, RESPONSE* response);
36 typedef void (*restful_post_handler) (REQUEST* request, RESPONSE* response);
37 
38 typedef int (*restful_periodic_handler) (struct resource_t* resource);
39 typedef void (*restful_periodic_request_generator) (REQUEST* request);
40 
41 /*
42  * Data structure representing a resource in REST.
43  */
44 struct resource_t {
45  struct resource_t *next; /*points to next resource defined*/
46  method_t methods_to_handle; /*handled HTTP methods*/
47  const char* url; /*handled URL*/
48  restful_handler handler; /*handler function*/
49  restful_pre_handler pre_handler; /*to be called before handler, may perform initializations*/
50  restful_post_handler post_handler; /*to be called after handler, may perform finalizations (cleanup, etc)*/
51  void* user_data; /*pointer to user specific data*/
52 };
53 typedef struct resource_t resource_t;
54 
55 struct periodic_resource_t {
56  struct periodic_resource_t *next;
57  resource_t *resource;
58  uint32_t period;
59  struct etimer* handler_cb_timer;
60  struct stimer* lifetime_timer;
61  restful_periodic_handler periodic_handler;
62  restful_periodic_request_generator periodic_request_generator;
63  uint32_t lifetime;
64  uip_ipaddr_t addr;
65  struct uip_udp_conn *client_conn;
66 };
67 typedef struct periodic_resource_t periodic_resource_t;
68 
69 /*
70  * Macro to define a Resource
71  * Resources are statically defined for the sake of efficiency and better memory management.
72  */
73 #define RESOURCE(name, methods_to_handle, url) \
74 void name##_handler(REQUEST*, RESPONSE*); \
75 resource_t resource_##name = {NULL, methods_to_handle, url, name##_handler, NULL, NULL, NULL}
76 
77 /*
78  * Macro to define a Periodic Resource
79  */
80 #define PERIODIC_RESOURCE(name, methods_to_handle, url, period) \
81 RESOURCE(name, methods_to_handle, url); \
82 int name##_periodic_handler(resource_t*); \
83 void name##_periodic_request_generator(REQUEST*); \
84 struct etimer handler_cb_timer_##name; \
85 struct stimer lifetime_timer_##name; \
86 periodic_resource_t periodic_resource_##name = {NULL, &resource_##name, period, &handler_cb_timer_##name, &lifetime_timer_##name, name##_periodic_handler, name##_periodic_request_generator, 0}
87 
88 
89 /*
90  * Initializes REST framework and starts HTTP or COAP process
91  */
92 void rest_init(void);
93 
94 /*
95  * Resources wanted to be accessible should be activated with the following code.
96  */
97 void rest_activate_resource(resource_t* resource);
98 
99 void rest_activate_periodic_resource(periodic_resource_t* periodic_resource);
100 
101 /*
102  * To be called by HTTP/COAP server as a callback function when a new service request appears.
103  * This function dispatches the corresponding RESTful service.
104  */
105 int rest_invoke_restful_service(REQUEST* request, RESPONSE* response);
106 
107 /*
108  * Returns the resource list
109  */
111 
112 /*
113  * Returns query variable in the URL.
114  * Returns true if the variable found, false otherwise.
115  * Variable is put in the buffer provided.
116  */
117 int rest_get_query_variable(REQUEST* request, const char *name, char* output, uint16_t output_size);
118 
119 /*
120  * Returns variable in the Post Data/Payload.
121  * Returns true if the variable found, false otherwise.
122  * Variable is put in the buffer provided.
123  */
124 int rest_get_post_variable(REQUEST* request, const char *name, char* output, uint16_t output_size);
125 
126 method_t rest_get_method_type(REQUEST* request);
127 void rest_set_method_type(REQUEST* request, method_t method);
128 
129 /*
130  * Getter for the request content type
131  */
132 content_type_t rest_get_header_content_type(REQUEST* request);
133 
134 /*
135  * Setter for the response content type
136  */
137 int rest_set_header_content_type(RESPONSE* response, content_type_t content_type);
138 
139 /*
140  * Setter for the response etag header
141  */
142 int rest_set_header_etag(RESPONSE* response, uint8_t* etag, uint8_t size);
143 
144 /*
145  * Setter for the status code (200, 201, etc) of the response.
146  */
147 void rest_set_response_status(RESPONSE* response, status_code_t status);
148 
149 /*
150  * Setter for the payload of the request and response
151  */
152 void rest_set_request_payload(RESPONSE* response, uint8_t* payload, uint16_t size);
153 void rest_set_response_payload(RESPONSE* response, uint8_t* payload, uint16_t size);
154 
155 /*
156  * Getter method for user specific data.
157  */
158 void* rest_get_user_data(resource_t* resource);
159 
160 /*
161  * Setter method for user specific data.
162  */
163 void rest_set_user_data(resource_t* resource, void* user_data);
164 
165 /*
166  * Sets the pre handler function of the Resource.
167  * If set, this function will be called just before the original handler function.
168  * Can be used to setup work before resource handling.
169  */
170 void rest_set_pre_handler(resource_t* resource, restful_pre_handler pre_handler);
171 
172 /*
173  * Sets the post handler function of the Resource.
174  * If set, this function will be called just after the original handler function.
175  * Can be used to do cleanup (deallocate memory, etc) after resource handling.
176  */
177 void rest_set_post_handler(resource_t* resource, restful_post_handler post_handler);
178 
179 #endif /*REST_H_*/
list_t rest_get_resources(void)
Returns the list of registered RESTful resources.
Definition: rest-engine.c:113
void ** list_t
The linked list type.
Definition: list.h:133
void rest_activate_resource(resource_t *resource, char *path)
Makes a resource available under the given URI path.
Definition: rest-engine.c:94
A timer.
Definition: stimer.h:81
Representation of a uIP UDP connection.
Definition: uip.h:1394
A timer.
Definition: etimer.h:76