Updated to CoAP Proposed Standard and removed draft versions.
This commit is contained in:
parent
443bbd3948
commit
7d79b69ada
88 changed files with 7325 additions and 14628 deletions
1
apps/rest-engine/Makefile.rest-engine
Executable file
1
apps/rest-engine/Makefile.rest-engine
Executable file
|
@ -0,0 +1 @@
|
|||
rest-engine_src = rest-engine.c
|
118
apps/rest-engine/rest-constants.h
Executable file
118
apps/rest-engine/rest-constants.h
Executable file
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Constants for the REST Engine (Erbium).
|
||||
* \author
|
||||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#ifndef REST_CONSTANTS_H_
|
||||
#define REST_CONSTANTS_H_
|
||||
|
||||
/**
|
||||
* Generic status codes that are mapped to either HTTP or CoAP codes.
|
||||
*/
|
||||
struct rest_implementation_status {
|
||||
const unsigned int OK; /* CONTENT_2_05, OK_200 */
|
||||
const unsigned int CREATED; /* CREATED_2_01, CREATED_201 */
|
||||
const unsigned int CHANGED; /* CHANGED_2_04, NO_CONTENT_204 */
|
||||
const unsigned int DELETED; /* DELETED_2_02, NO_CONTENT_204 */
|
||||
const unsigned int NOT_MODIFIED; /* VALID_2_03, NOT_MODIFIED_304 */
|
||||
|
||||
const unsigned int BAD_REQUEST; /* BAD_REQUEST_4_00, BAD_REQUEST_400 */
|
||||
const unsigned int UNAUTHORIZED; /* UNAUTHORIZED_4_01, UNAUTHORIZED_401 */
|
||||
const unsigned int BAD_OPTION; /* BAD_OPTION_4_02, BAD_REQUEST_400 */
|
||||
const unsigned int FORBIDDEN; /* FORBIDDEN_4_03, FORBIDDEN_403 */
|
||||
const unsigned int NOT_FOUND; /* NOT_FOUND_4_04, NOT_FOUND_404 */
|
||||
const unsigned int METHOD_NOT_ALLOWED; /* METHOD_NOT_ALLOWED_4_05, METHOD_NOT_ALLOWED_405 */
|
||||
const unsigned int NOT_ACCEPTABLE; /* NOT_ACCEPTABLE_4_06, NOT_ACCEPTABLE_406 */
|
||||
const unsigned int REQUEST_ENTITY_TOO_LARGE; /* REQUEST_ENTITY_TOO_LARGE_4_13, REQUEST_ENTITY_TOO_LARGE_413 */
|
||||
const unsigned int UNSUPPORTED_MEDIA_TYPE; /* UNSUPPORTED_MEDIA_TYPE_4_15, UNSUPPORTED_MEDIA_TYPE_415 */
|
||||
|
||||
const unsigned int INTERNAL_SERVER_ERROR; /* INTERNAL_SERVER_ERROR_5_00, INTERNAL_SERVER_ERROR_500 */
|
||||
const unsigned int NOT_IMPLEMENTED; /* NOT_IMPLEMENTED_5_01, NOT_IMPLEMENTED_501 */
|
||||
const unsigned int BAD_GATEWAY; /* BAD_GATEWAY_5_02, BAD_GATEWAY_502 */
|
||||
const unsigned int SERVICE_UNAVAILABLE; /* SERVICE_UNAVAILABLE_5_03, SERVICE_UNAVAILABLE_503 */
|
||||
const unsigned int GATEWAY_TIMEOUT; /* GATEWAY_TIMEOUT_5_04, GATEWAY_TIMEOUT_504 */
|
||||
const unsigned int PROXYING_NOT_SUPPORTED; /* PROXYING_NOT_SUPPORTED_5_05, INTERNAL_SERVER_ERROR_500 */
|
||||
};
|
||||
|
||||
/**
|
||||
* List of Content-Formats which are Internet Media Types plus encoding.
|
||||
* TODO This should be a constant enum taken from CoAP for both CoAP and HTTP.
|
||||
*/
|
||||
struct rest_implementation_type {
|
||||
unsigned int TEXT_PLAIN;
|
||||
unsigned int TEXT_XML;
|
||||
unsigned int TEXT_CSV;
|
||||
unsigned int TEXT_HTML;
|
||||
unsigned int IMAGE_GIF;
|
||||
unsigned int IMAGE_JPEG;
|
||||
unsigned int IMAGE_PNG;
|
||||
unsigned int IMAGE_TIFF;
|
||||
unsigned int AUDIO_RAW;
|
||||
unsigned int VIDEO_RAW;
|
||||
unsigned int APPLICATION_LINK_FORMAT;
|
||||
unsigned int APPLICATION_XML;
|
||||
unsigned int APPLICATION_OCTET_STREAM;
|
||||
unsigned int APPLICATION_RDF_XML;
|
||||
unsigned int APPLICATION_SOAP_XML;
|
||||
unsigned int APPLICATION_ATOM_XML;
|
||||
unsigned int APPLICATION_XMPP_XML;
|
||||
unsigned int APPLICATION_EXI;
|
||||
unsigned int APPLICATION_FASTINFOSET;
|
||||
unsigned int APPLICATION_SOAP_FASTINFOSET;
|
||||
unsigned int APPLICATION_JSON;
|
||||
unsigned int APPLICATION_X_OBIX_BINARY;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resource flags for allowed methods and special functionalities.
|
||||
*/
|
||||
typedef enum {
|
||||
NO_FLAGS = 0,
|
||||
|
||||
/* methods to handle */
|
||||
METHOD_GET = (1 << 0),
|
||||
METHOD_POST = (1 << 1),
|
||||
METHOD_PUT = (1 << 2),
|
||||
METHOD_DELETE = (1 << 3),
|
||||
|
||||
/* special flags */
|
||||
HAS_SUB_RESOURCES = (1 << 4),
|
||||
IS_SEPARATE = (1 << 5),
|
||||
IS_OBSERVABLE = (1 << 6),
|
||||
IS_PERIODIC = (1 << 7)
|
||||
} rest_resource_flags_t;
|
||||
|
||||
#endif /* REST_CONSTANTS_H_ */
|
212
apps/rest-engine/rest-engine.c
Executable file
212
apps/rest-engine/rest-engine.c
Executable file
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* An abstraction layer for RESTful Web services (Erbium).
|
||||
* Inspired by RESTful Contiki by Dogan Yazar.
|
||||
* \author
|
||||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
|
||||
#define DEBUG DEBUG_NONE
|
||||
#include "net/uip-debug.h"
|
||||
|
||||
PROCESS(rest_engine_process, "REST Engine");
|
||||
/*---------------------------------------------------------------------------*/
|
||||
LIST(restful_services);
|
||||
LIST(restful_periodic_services);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/*- REST Engine API ---------------------------------------------------------*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Initializes and starts the REST Engine process
|
||||
*
|
||||
* This function must be called by server processes before any resources are
|
||||
* registered through rest_activate_resource().
|
||||
*/
|
||||
void
|
||||
rest_init_engine(void)
|
||||
{
|
||||
list_init(restful_services);
|
||||
|
||||
REST.set_service_callback(rest_invoke_restful_service);
|
||||
|
||||
/* Start the RESTful server implementation. */
|
||||
REST.init();
|
||||
|
||||
/*Start REST engine process */
|
||||
process_start(&rest_engine_process, NULL);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Makes a resource available under the given URI path
|
||||
* \param resource A pointer to a resource implementation
|
||||
* \param path The URI path string for this resource
|
||||
*
|
||||
* The resource implementation must be imported first using the
|
||||
* extern keyword. The build system takes care of compiling every
|
||||
* *.c file in the ./resources/ sub-directory (see example Makefile).
|
||||
*/
|
||||
void
|
||||
rest_activate_resource(resource_t * resource, char *path)
|
||||
{
|
||||
resource->url = path;
|
||||
list_add(restful_services, resource);
|
||||
|
||||
PRINTF("Activating: %s\n", resource->url);
|
||||
|
||||
/* Only add periodic resources with a periodic_handler and a period > 0. */
|
||||
if(resource->flags & IS_PERIODIC && resource->periodic->periodic_handler
|
||||
&& resource->periodic->period) {
|
||||
PRINTF("Periodic resource: %p (%s)\n", resource->periodic,
|
||||
resource->periodic->resource->url);
|
||||
list_add(restful_periodic_services, resource->periodic);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/*- Internal API ------------------------------------------------------------*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
list_t
|
||||
rest_get_resources(void)
|
||||
{
|
||||
return restful_services;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
rest_invoke_restful_service(void *request, void *response, uint8_t * buffer,
|
||||
uint16_t buffer_size, int32_t * offset)
|
||||
{
|
||||
uint8_t found = 0;
|
||||
uint8_t allowed = 1;
|
||||
|
||||
resource_t *resource = NULL;
|
||||
const char *url = NULL;
|
||||
|
||||
for(resource = (resource_t *) list_head(restful_services);
|
||||
resource; resource = resource->next) {
|
||||
|
||||
/* if the web service handles that kind of requests and urls matches */
|
||||
if((REST.get_url(request, &url) == strlen(resource->url)
|
||||
|| (REST.get_url(request, &url) > strlen(resource->url)
|
||||
&& (resource->flags & HAS_SUB_RESOURCES)))
|
||||
&& strncmp(resource->url, url, strlen(resource->url)) == 0) {
|
||||
found = 1;
|
||||
rest_resource_flags_t method = REST.get_method_type(request);
|
||||
|
||||
PRINTF("/%s, method %u, resource->flags %u\n", resource->url,
|
||||
(uint16_t) method, resource->flags);
|
||||
|
||||
if((method & METHOD_GET) && resource->get_handler != NULL) {
|
||||
/* call handler function */
|
||||
resource->get_handler(request, response, buffer, buffer_size, offset);
|
||||
} else if((method & METHOD_POST) && resource->post_handler != NULL) {
|
||||
/* call handler function */
|
||||
resource->post_handler(request, response, buffer, buffer_size,
|
||||
offset);
|
||||
} else if((method & METHOD_PUT) && resource->put_handler != NULL) {
|
||||
/* call handler function */
|
||||
resource->put_handler(request, response, buffer, buffer_size, offset);
|
||||
} else if((method & METHOD_DELETE) && resource->delete_handler != NULL) {
|
||||
/* call handler function */
|
||||
resource->delete_handler(request, response, buffer, buffer_size,
|
||||
offset);
|
||||
} else {
|
||||
allowed = 0;
|
||||
REST.set_response_status(response, REST.status.METHOD_NOT_ALLOWED);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) {
|
||||
REST.set_response_status(response, REST.status.NOT_FOUND);
|
||||
} else if(allowed) {
|
||||
/* final handler for special flags */
|
||||
if(resource->flags & IS_OBSERVABLE) {
|
||||
REST.subscription_handler(resource, request, response);
|
||||
}
|
||||
}
|
||||
|
||||
return found & allowed;
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(rest_engine_process, ev, data)
|
||||
{
|
||||
PROCESS_BEGIN();
|
||||
|
||||
/* pause to let REST server finish adding resources. */
|
||||
PROCESS_PAUSE();
|
||||
|
||||
/* initialize the PERIODIC_RESOURCE timers, which will be handled by this process. */
|
||||
periodic_resource_t *periodic_resource = NULL;
|
||||
|
||||
for(periodic_resource =
|
||||
(periodic_resource_t *) list_head(restful_periodic_services);
|
||||
periodic_resource; periodic_resource = periodic_resource->next) {
|
||||
if(periodic_resource->periodic_handler && periodic_resource->period) {
|
||||
PRINTF("Periodic: Set timer for /%s to %lu\n",
|
||||
periodic_resource->resource->url, periodic_resource->period);
|
||||
etimer_set(&periodic_resource->periodic_timer,
|
||||
periodic_resource->period);
|
||||
}
|
||||
}
|
||||
|
||||
while(1) {
|
||||
PROCESS_WAIT_EVENT();
|
||||
|
||||
if(ev == PROCESS_EVENT_TIMER) {
|
||||
for(periodic_resource =
|
||||
(periodic_resource_t *) list_head(restful_periodic_services);
|
||||
periodic_resource; periodic_resource = periodic_resource->next) {
|
||||
if(periodic_resource->period
|
||||
&& etimer_expired(&periodic_resource->periodic_timer)) {
|
||||
|
||||
PRINTF("Periodic: etimer expired for /%s (period: %lu)\n",
|
||||
periodic_resource->resource->url, periodic_resource->period);
|
||||
|
||||
/* Call the periodic_handler function, which was checked during adding to list. */
|
||||
(periodic_resource->periodic_handler) ();
|
||||
|
||||
etimer_reset(&periodic_resource->periodic_timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
263
apps/rest-engine/rest-engine.h
Executable file
263
apps/rest-engine/rest-engine.h
Executable file
|
@ -0,0 +1,263 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* An abstraction layer for RESTful Web services (Erbium).
|
||||
* Inspired by RESTful Contiki by Dogan Yazar.
|
||||
* \author
|
||||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#ifndef REST_ENGINE_H_
|
||||
#define REST_ENGINE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include "contiki.h"
|
||||
#include "contiki-lib.h"
|
||||
#include "rest-constants.h"
|
||||
|
||||
/* list of valid REST Enigne implementations */
|
||||
#define REGISTERED_ENGINE_ERBIUM coap_rest_implementation
|
||||
#define REGISTERED_ENGINE_HELIUM http_rest_implementation
|
||||
|
||||
/* sanity check for configured implementation */
|
||||
#if !defined(REST) || (REST!=REGISTERED_ENGINE_ERBIUM && REST!=REGISTERED_ENGINE_HELIUM)
|
||||
#error "Define a valid REST Engine implementation (REST define)!"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* The maximum buffer size that is provided for resource responses and must be respected due to the limited IP buffer.
|
||||
* Larger data must be handled by the resource and will be sent chunk-wise through a TCP stream or CoAP blocks.
|
||||
*/
|
||||
#ifndef REST_MAX_CHUNK_SIZE
|
||||
#define REST_MAX_CHUNK_SIZE 64
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) ((a) < (b)? (a) : (b))
|
||||
#endif /* MIN */
|
||||
|
||||
|
||||
struct resource_s;
|
||||
struct periodic_resource_s;
|
||||
|
||||
/* signatures of handler functions */
|
||||
typedef void (*restful_handler) (void *request, void *response,
|
||||
uint8_t * buffer, uint16_t preferred_size,
|
||||
int32_t * offset);
|
||||
typedef void (*restful_final_handler) (struct resource_s * resource,
|
||||
void *request, void *response);
|
||||
typedef void (*restful_periodic_handler) (void);
|
||||
typedef void (*restful_response_handler) (void *data, void *response);
|
||||
typedef void (*restful_trigger_handler) (void);
|
||||
|
||||
/* signature of the rest-engine service function */
|
||||
typedef int (*service_callback_t) (void *request, void *response,
|
||||
uint8_t * buffer, uint16_t preferred_size,
|
||||
int32_t * offset);
|
||||
|
||||
/* data structure representing a resource in REST */
|
||||
struct resource_s {
|
||||
struct resource_s *next; /* for LIST, points to next resource defined */
|
||||
const char *url; /*handled URL */
|
||||
rest_resource_flags_t flags; /* handled RESTful methods */
|
||||
const char *attributes; /* link-format attributes */
|
||||
restful_handler get_handler; /* handler function */
|
||||
restful_handler post_handler; /* handler function */
|
||||
restful_handler put_handler; /* handler function */
|
||||
restful_handler delete_handler; /* handler function */
|
||||
union {
|
||||
struct periodic_resource_s *periodic; /* special data depending on flags */
|
||||
restful_trigger_handler trigger;
|
||||
restful_trigger_handler resume;
|
||||
};
|
||||
};
|
||||
typedef struct resource_s resource_t;
|
||||
|
||||
struct periodic_resource_s {
|
||||
struct periodic_resource_s *next; /* for LIST, points to next resource defined */
|
||||
const resource_t *resource;
|
||||
uint32_t period;
|
||||
struct etimer periodic_timer;
|
||||
const restful_periodic_handler periodic_handler;
|
||||
};
|
||||
typedef struct periodic_resource_s periodic_resource_t;
|
||||
|
||||
/*
|
||||
* Macro to define a RESTful resource.
|
||||
* Resources are statically defined for the sake of efficiency and better memory management.
|
||||
*/
|
||||
#define RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler) \
|
||||
resource_t name = {NULL, NULL, NO_FLAGS, attributes, get_handler, post_handler, put_handler, delete_handler, {NULL}}
|
||||
|
||||
#define PARENT_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler) \
|
||||
resource_t name = {NULL, NULL, HAS_SUB_RESOURCES, attributes, get_handler, post_handler, put_handler, delete_handler, {NULL}}
|
||||
|
||||
#define SEPARATE_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler, resume_handler) \
|
||||
resource_t name = {NULL, NULL, IS_SEPARATE, attributes, get_handler, post_handler, put_handler, delete_handler, { .resume = resume_handler }}
|
||||
|
||||
#define EVENT_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler, event_handler) \
|
||||
resource_t name = {NULL, NULL, IS_OBSERVABLE, attributes, get_handler, post_handler, put_handler, delete_handler, { .trigger = event_handler }}
|
||||
|
||||
/*
|
||||
* Macro to define a periodic resource.
|
||||
* The corresponding [name]_periodic_handler() function will be called every period.
|
||||
* For instance polling a sensor and publishing a changed value to subscribed clients would be done there.
|
||||
* The subscriber list will be maintained by the final_handler rest_subscription_handler() (see rest-mapping header file).
|
||||
*/
|
||||
#define PERIODIC_RESOURCE(name, attributes, get_handler, post_handler, put_handler, delete_handler, period, periodic_handler) \
|
||||
periodic_resource_t periodic_##name; \
|
||||
resource_t name = {NULL, NULL, IS_OBSERVABLE | IS_PERIODIC, attributes, get_handler, post_handler, put_handler, delete_handler, { .periodic = &periodic_##name }}; \
|
||||
periodic_resource_t periodic_##name = {NULL, &name, period, {{0}}, periodic_handler};
|
||||
|
||||
struct rest_implementation {
|
||||
char *name;
|
||||
|
||||
/** Initialize the REST implementation. */
|
||||
void (*init) (void);
|
||||
|
||||
/** Register the RESTful service callback at implementation. */
|
||||
void (*set_service_callback) (service_callback_t callback);
|
||||
|
||||
/** Get request URI path. */
|
||||
int (*get_url) (void *request, const char **url);
|
||||
|
||||
/** Get the method of a request. */
|
||||
rest_resource_flags_t (*get_method_type) (void *request);
|
||||
|
||||
/** Set the status code of a response. */
|
||||
int (*set_response_status) (void *response, unsigned int code);
|
||||
|
||||
/** Get the content-type of a request. */
|
||||
int (*get_header_content_type) (void *request,
|
||||
unsigned int *content_format);
|
||||
|
||||
/** Set the Content-Type of a response. */
|
||||
int (*set_header_content_type) (void *response,
|
||||
unsigned int content_format);
|
||||
|
||||
/** Get the Accept types of a request. */
|
||||
int (*get_header_accept) (void *request, unsigned int *accept);
|
||||
|
||||
/** Get the Length option of a request. */
|
||||
int (*get_header_length) (void *request, uint32_t * size);
|
||||
|
||||
/** Set the Length option of a response. */
|
||||
int (*set_header_length) (void *response, uint32_t size);
|
||||
|
||||
/** Get the Max-Age option of a request. */
|
||||
int (*get_header_max_age) (void *request, uint32_t * age);
|
||||
|
||||
/** Set the Max-Age option of a response. */
|
||||
int (*set_header_max_age) (void *response, uint32_t age);
|
||||
|
||||
/** Set the ETag option of a response. */
|
||||
int (*set_header_etag) (void *response, const uint8_t * etag,
|
||||
size_t length);
|
||||
|
||||
/** Get the If-Match option of a request. */
|
||||
int (*get_header_if_match) (void *request, const uint8_t ** etag);
|
||||
|
||||
/** Get the If-Match option of a request. */
|
||||
int (*get_header_if_none_match) (void *request);
|
||||
|
||||
/** Get the Host option of a request. */
|
||||
int (*get_header_host) (void *request, const char **host);
|
||||
|
||||
/** Set the location option of a response. */
|
||||
int (*set_header_location) (void *response, const char *location);
|
||||
|
||||
/** Get the payload option of a request. */
|
||||
int (*get_request_payload) (void *request, const uint8_t ** payload);
|
||||
|
||||
/** Set the payload option of a response. */
|
||||
int (*set_response_payload) (void *response, const void *payload,
|
||||
size_t length);
|
||||
|
||||
/** Get the query string of a request. */
|
||||
int (*get_query) (void *request, const char **value);
|
||||
|
||||
/** Get the value of a request query key-value pair. */
|
||||
int (*get_query_variable) (void *request, const char *name,
|
||||
const char **value);
|
||||
|
||||
/** Get the value of a request POST key-value pair. */
|
||||
int (*get_post_variable) (void *request, const char *name,
|
||||
const char **value);
|
||||
|
||||
/** Send the payload to all subscribers of the resource at url. */
|
||||
void (*notify_subscribers) (resource_t * resource);
|
||||
|
||||
/** The handler for resource subscriptions. */
|
||||
restful_final_handler subscription_handler;
|
||||
|
||||
/* REST status codes. */
|
||||
const struct rest_implementation_status status;
|
||||
|
||||
/* REST content-types. */
|
||||
const struct rest_implementation_type type;
|
||||
};
|
||||
|
||||
/* instance of REST implementation */
|
||||
extern const struct rest_implementation REST;
|
||||
|
||||
/*
|
||||
* To be called by HTTP/COAP server as a callback function when a new service request appears.
|
||||
* This function dispatches the corresponding RESTful service.
|
||||
*/
|
||||
int rest_invoke_restful_service(void *request, void *response,
|
||||
uint8_t * buffer, uint16_t buffer_size,
|
||||
int32_t * offset);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Initializes REST framework and starts the HTTP or CoAP process.
|
||||
*/
|
||||
void rest_init_engine(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
*
|
||||
* \brief Resources wanted to be accessible should be activated with the following code.
|
||||
* \param resource
|
||||
* A RESTful resource defined through the RESOURCE macros.
|
||||
* \param path
|
||||
* The local URI path where to provide the resource.
|
||||
*/
|
||||
void rest_activate_resource(resource_t * resource, char *path);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Returns the list of registered RESTful resources.
|
||||
* \return The resource list.
|
||||
*/
|
||||
list_t rest_get_resources(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
#endif /*REST_ENGINE_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue