Factor/Fix generic resources

Now the old GENERIC_RESOURCE macros works again (but usage has changed).
Common resources (battery, leds, radio) are now in resources-common.
This commit is contained in:
Ralf Schlatterbeck 2015-01-21 15:09:31 +01:00
parent 76e39b2b5d
commit baaa2c5741
59 changed files with 440 additions and 2705 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Ralf Schlatterbeck Open Source Consulting
* Copyright (c) 2014-15, Ralf Schlatterbeck Open Source Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -105,7 +105,72 @@ json_parse_variable
return 0;
}
void generic_handler
void generic_get_handler
( void *request
, void *response
, uint8_t *buffer
, uint16_t preferred_size
, int32_t *offset
, char *name
, size_t (*to_str)(const char *name, uint8_t is_json, char *buf, size_t bsize)
)
{
int success = 1;
char temp [100];
size_t len = 0;
unsigned int accept = -1;
REST.get_header_accept (request, &accept);
if ( accept != -1
&& accept != REST.type.TEXT_PLAIN
&& accept != REST.type.APPLICATION_JSON
)
{
success = 0;
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
return;
}
// TEXT format
if (accept == REST.type.APPLICATION_JSON) {
len += snprintf
(temp + len, sizeof (temp) - len, "{\n \"%s\" : ", name);
if (len > sizeof (temp)) {
success = 0;
goto out;
}
len += to_str (name, 1, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
goto out;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n}\n");
if (len > sizeof (temp)) {
success = 0;
goto out;
}
} else { // TEXT Format
len += to_str (name, 0, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
goto out;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n");
if (len > sizeof (temp)) {
success = 0;
goto out;
}
}
memcpy (buffer, temp, len);
REST.set_header_content_type (response, accept);
REST.set_response_payload (response, buffer, len);
out :
if (!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
void generic_put_handler
( void *request
, void *response
, uint8_t *buffer
@ -113,88 +178,31 @@ void generic_handler
, int32_t *offset
, char *name
, void (*from_str)(const char *name, const char *s)
, size_t (*to_str)(const char *name, uint8_t is_json, char *buf, size_t bsize)
)
{
int success = 1;
char temp [100];
int i = 0;
size_t len = 0;
int n_acc = 0;
const uint8_t *bytes = NULL;
const uint16_t *accept = NULL;
uint16_t a_ctype = REST.type.APPLICATION_JSON;
uint16_t c_ctype;
n_acc = REST.get_header_content_type (request, &c_ctype);
REST.get_header_content_type (request, &c_ctype);
/* Seems like accepted type is currently unsupported? */
n_acc = REST.get_header_accept (request, &accept);
for (i=0; i<n_acc; i++) {
if ( accept [i] == REST.type.TEXT_PLAIN
|| accept [i] == REST.type.APPLICATION_JSON
)
{
a_ctype = accept [i];
break;
}
}
switch(REST.get_method_type(request)) {
case METHOD_GET:
// TEXT format
if (a_ctype == REST.type.TEXT_PLAIN) {
len += to_str (name, 0, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
break;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n");
if (len > sizeof (temp)) {
success = 0;
break;
}
} else { // jSON Format
len += snprintf
(temp + len, sizeof (temp) - len, "{\n \"%s\" : ", name);
if (len > sizeof (temp)) {
success = 0;
break;
}
len += to_str (name, 1, temp + len, sizeof (temp) - len);
if (len > sizeof (temp)) {
success = 0;
break;
}
len += snprintf (temp + len, sizeof (temp) - len, "\n}\n");
if (len > sizeof (temp)) {
success = 0;
break;
}
}
memcpy (buffer, temp, len);
REST.set_header_content_type (response, a_ctype);
REST.set_response_payload (response, buffer, len);
break;
case METHOD_PUT:
if (from_str && (len = coap_get_payload(request, &bytes))) {
if (c_ctype == REST.type.TEXT_PLAIN) {
temp [sizeof (temp) - 1] = 0;
strncpy (temp, (const char *)bytes, MIN (len, sizeof (temp) - 1));
} else { // jSON Format
if (json_parse_variable (bytes, len, name, temp, sizeof (temp)) < 0) {
success = 0;
break;
}
}
from_str (name, temp);
REST.set_response_status(response, REST.status.CHANGED);
} else {
if (from_str && (len = coap_get_payload(request, &bytes))) {
if (c_ctype == REST.type.TEXT_PLAIN) {
temp [sizeof (temp) - 1] = 0;
strncpy (temp, (const char *)bytes, MIN (len, sizeof (temp) - 1));
} else { // jSON Format
if (json_parse_variable (bytes, len, name, temp, sizeof (temp)) < 0) {
success = 0;
goto out;
}
break;
default:
success = 0;
}
from_str (name, temp);
REST.set_response_status(response, REST.status.CHANGED);
} else {
success = 0;
}
out:
if (!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Ralf Schlatterbeck Open Source Consulting
* Copyright (c) 2014-15, Ralf Schlatterbeck Open Source Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -60,8 +60,8 @@
* Yes, this *is* a hack. But I hate boilerplate code.
*/
#define GENERIC_RESOURCE(name, methods, path, title, unit, fs, ts) \
void name##_handler \
#define GENERIC_RESOURCE(name, title, unit, fs, ts) \
static void name##_get_handler \
( void *request \
, void *response \
, uint8_t *buffer \
@ -69,16 +69,34 @@
, int32_t *offset \
) \
{ \
generic_handler \
(request, response, buffer, ps, offset, STR_(name), fs, ts); \
generic_get_handler \
(request, response, buffer, ps, offset, STR_(name), ts); \
} \
static void name##_put_handler \
( void *request \
, void *response \
, uint8_t *buffer \
, uint16_t ps \
, int32_t *offset \
) \
{ \
generic_put_handler \
(request, response, buffer, ps, offset, STR_(name), fs); \
} \
\
RESOURCE ( name, methods, path \
RESOURCE ( res_##name \
, "title=\"" STR_(title) "\"" \
";rt=UCUM:\"" STR_(unit) "\"" \
";ct=\"0 5\"" \
, (ts) ? name##_get_handler : NULL \
, NULL /* POST */ \
, (fs) ? name##_put_handler : NULL \
, NULL /* DELETE */ \
)
/* Ignore constant pointer tests above */
#pragma GCC diagnostic ignored "-Waddress"
/**
* \brief Parse a resource in json format
* \param bytes: Input string received via coap
@ -94,11 +112,8 @@ extern int8_t json_parse_variable
(const uint8_t *bytes, size_t len, char *name, char *buf, size_t buflen);
/**
* \brief Generic coap resource handler
* \brief Generic coap GET resource handler
* \param name: The name of the variable in json
* \param from_str: Application method to parse value from string
* and act on it, may be NULL in which case the resource only
* supports GET not PUT
* \param to_str: Application method to format value for output;
* the function may chose to format differently for coap or text
* The other parameters are the same as a normal resource handler
@ -106,13 +121,36 @@ extern int8_t json_parse_variable
*
* The callback functions get the name of the parameter as a first
* argument, this allows to re-use the same function for different
* parameters. The from_str in addition gets the string to parse.
* parameters.
* For the to_str function the is_json flag allows to generate a
* different string depending on the content-type. In addition it gets a
* buffer and the size of the buffer. It needs to return the number of
* bytes output, similar to sprintf.
*/
extern void generic_handler
extern void generic_get_handler
( void *request
, void *response
, uint8_t *buffer
, uint16_t preferred_size
, int32_t *offset
, char *name
, size_t (*to_str)(const char *name, uint8_t is_json, char *buf, size_t bsize)
);
/**
* \brief Generic coap PUT resource handler
* \param name: The name of the variable in json
* \param from_str: Application method to parse value from string
* and act on it, may be NULL in which case the resource only
* supports GET not PUT
* The other parameters are the same as a normal resource handler
* This helps avoid boilerplate code for request handlers
*
* The callback functions get the name of the parameter as a first
* argument, this allows to re-use the same function for different
* parameters. The from_str in addition gets the string to parse.
*/
extern void generic_put_handler
( void *request
, void *response
, uint8_t *buffer
@ -120,7 +158,6 @@ extern void generic_handler
, int32_t *offset
, char *name
, void (*from_str)(const char *name, const char *s)
, size_t (*to_str)(const char *name, uint8_t is_json, char *buf, size_t bsize)
);
/*

View file

@ -40,8 +40,6 @@ size_t time_to_string (const char *name, uint8_t is_json, char *buf, size_t bs)
GENERIC_RESOURCE \
( localtime
, METHOD_GET
, "clock/localtime"
, Local time
, formatted time
, NULL
@ -50,8 +48,6 @@ GENERIC_RESOURCE \
GENERIC_RESOURCE \
( utc
, METHOD_GET
, "clock/utc"
, UTC
, formatted time
, NULL

View file

@ -18,8 +18,7 @@
#include "time.h"
#include "time_resource.h"
#include "jsonparse.h"
/* Only coap 13 for now */
#include "er-coap-13.h"
#include "er-coap.h"
#include "generic_resource.h"
void timestamp_from_string (const char *name, const char *s)
@ -46,8 +45,6 @@ timestamp_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
GENERIC_RESOURCE
( timestamp
, METHOD_GET | METHOD_PUT
, "clock/timestamp"
, Time
, s
, timestamp_from_string

View file

@ -19,9 +19,9 @@
#include "contiki.h"
#include "rest-engine.h"
extern resource_t resource_timestamp;
extern resource_t resource_localtime;
extern resource_t resource_utc;
extern resource_t res_timestamp;
extern resource_t res_localtime;
extern resource_t res_utc;
#endif // time_resource_h
/** @} */

View file

@ -1,8 +1,12 @@
# Set this to the name of your sketch (without extension .pde)
SKETCH=sketch
all: arduino-example \
arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size arduino-example.osd-merkur.hex \
arduino-example.osd-merkur.eep
endif
all: arduino-example $(PLATFORM_FILES)
CONTIKI=../../..
@ -15,17 +19,13 @@ PROJECT_SOURCEFILES += ${SKETCH}.cpp
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
endif
endif
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES= $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# variable for Makefile.include
@ -54,7 +54,7 @@ APPS += arduino
include $(CONTIKI)/Makefile.include
include $(CONTIKI)/apps/arduino/Makefile.include
arduino-example: arduino-example.osd-merkur
avr-size: arduino-example.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 arduino-example.osd-merkur
arduino-example.osd-merkur.hex: arduino-example.osd-merkur
@ -71,7 +71,7 @@ flash: arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
flash:w:arduino-example.osd-merkur.hex:a -U \
eeprom:w:arduino-example.osd-merkur.eep:a
.PHONY: flash
.PHONY: flash avr-size
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */

View file

@ -1,8 +1,12 @@
# Set this to the name of your sketch (without extension .pde)
SKETCH=sketch
all: arduino-example \
arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size arduino-example.osd-merkur.hex \
arduino-example.osd-merkur.eep
endif
all: arduino-example $(PLATFORM_FILES)
CONTIKI=../../..
@ -13,19 +17,16 @@ CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
PROJECT_SOURCEFILES += ${SKETCH}.cpp
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
endif
endif
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES= $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# variable for Makefile.include
@ -54,7 +55,7 @@ APPS += arduino
include $(CONTIKI)/Makefile.include
include $(CONTIKI)/apps/arduino/Makefile.include
arduino-example: arduino-example.osd-merkur
avr-size: arduino-example.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 arduino-example.osd-merkur
arduino-example.osd-merkur.hex: arduino-example.osd-merkur
@ -71,7 +72,7 @@ flash: arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
flash:w:arduino-example.osd-merkur.hex:a -U \
eeprom:w:arduino-example.osd-merkur.eep:a
.PHONY: flash
.PHONY: flash avr-size
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */

View file

@ -1,19 +1,18 @@
# Set this to the name of your sketch (without extension .pde)
SKETCH=sketch
all: arduino-example \
arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size arduino-example.osd-merkur.hex \
arduino-example.osd-merkur.eep
endif
# variable for this Makefile
# configure CoAP implementation (3|7|12|13) (er-coap-07 also supports CoAP draft 08)
WITH_COAP=13
# for some platforms
UIP_CONF_IPV6=1
# IPv6 make config disappeared completely
CFLAGS += -DUIP_CONF_IPV6=1
all: arduino-example $(PLATFORM_FILES)
CONTIKI=../../..
# Contiki IPv6 configuration
CONTIKI_WITH_IPV6 = 1
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
PROJECT_SOURCEFILES += resource_led_pwm.c ${SKETCH}.cpp
@ -35,44 +34,17 @@ endif
# linker optimizations
SMALL=1
# REST framework, requires WITH_COAP
ifeq ($(WITH_COAP), 13)
${info INFO: compiling with CoAP-13}
CFLAGS += -DWITH_COAP=13
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-13
else ifeq ($(WITH_COAP), 12)
${info INFO: compiling with CoAP-12}
CFLAGS += -DWITH_COAP=12
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-12
else ifeq ($(WITH_COAP), 7)
${info INFO: compiling with CoAP-08}
CFLAGS += -DWITH_COAP=7
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-07
else ifeq ($(WITH_COAP), 3)
${info INFO: compiling with CoAP-03}
CFLAGS += -DWITH_COAP=3
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-03
else
${info INFO: compiling with HTTP}
CFLAGS += -DWITH_HTTP
CFLAGS += -DREST=http_rest_implementation
CFLAGS += -DUIP_CONF_TCP=1
APPS += er-http-engine
endif
APPS += erbium time json arduino json-resource
# REST Engine shall use Erbium CoAP implementation
APPS += er-coap
APPS += rest-engine
APPS += time json arduino json-resource
include $(CONTIKI)/Makefile.include
include $(CONTIKI)/apps/arduino/Makefile.include
avr-size: arduino-example.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 arduino-example.osd-merkur
arduino-example.osd-merkur.hex: arduino-example.osd-merkur
avr-objcopy -j .text -j .data -O ihex arduino-example.osd-merkur \
arduino-example.osd-merkur.hex
@ -87,7 +59,7 @@ flash: arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
flash:w:arduino-example.osd-merkur.hex:a -U \
eeprom:w:arduino-example.osd-merkur.eep:a
.PHONY: flash
.PHONY: flash avr-size
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)

View file

@ -18,18 +18,17 @@
#define led_pwm_h
#include "contiki.h"
#include "contiki-net.h"
#include "erbium.h"
#include "er-coap-13.h"
#include "er-coap.h"
extern uint8_t pwm;
extern uint8_t period_100ms;
extern uint16_t analog2_voltage;
extern uint16_t analog5_voltage;
extern resource_t resource_led_pwm;
extern resource_t resource_led_period;
extern resource_t resource_analog2_voltage;
extern resource_t resource_analog5_voltage;
extern resource_t res_led_pwm;
extern resource_t res_led_period;
extern resource_t res_analog2_voltage;
extern resource_t res_analog5_voltage;
#endif // led_pwm_h
/** @} */

View file

@ -13,8 +13,7 @@
#include <string.h>
#include "contiki.h"
#include "jsonparse.h"
/* Only coap 13 for now */
#include "er-coap-13.h"
#include "er-coap.h"
#include "generic_resource.h"
#include "led_pwm.h"
@ -34,8 +33,7 @@ pwm_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
}
GENERIC_RESOURCE \
( led_pwm, METHOD_GET | METHOD_PUT
, "led/pwm"
( led_pwm
, LED PWM
, duty-cycle
, pwm_from_string
@ -61,8 +59,7 @@ period_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
}
GENERIC_RESOURCE \
( led_period, METHOD_GET | METHOD_PUT
, "led/period"
( led_period
, LED Period
, ms
, period_from_string
@ -77,8 +74,7 @@ analog2_v (const char *name, uint8_t is_json, char *buf, size_t bufsize)
}
GENERIC_RESOURCE \
( analog2_voltage, METHOD_GET
, "analog/2"
( analog2_voltage
, Analog 2 voltage
, V
, NULL
@ -93,8 +89,7 @@ analog5_v (const char *name, uint8_t is_json, char *buf, size_t bufsize)
}
GENERIC_RESOURCE \
( analog5_voltage, METHOD_GET
, "analog/5"
( analog5_voltage
, Analog 5 voltage
, V
, NULL

View file

@ -25,10 +25,10 @@ void setup (void)
{
arduino_pwm_timer_init ();
rest_init_engine ();
rest_activate_resource (&resource_led_pwm);
rest_activate_resource (&resource_led_period);
rest_activate_resource (&resource_analog2_voltage);
rest_activate_resource (&resource_analog5_voltage);
rest_activate_resource (&res_led_pwm, "led/pwm");
rest_activate_resource (&res_led_period, "led/period");
rest_activate_resource (&res_analog2_voltage, "analog/2");
rest_activate_resource (&res_analog5_voltage, "analog/5");
}
void loop (void)

View file

@ -1,8 +1,12 @@
# Set this to the name of your sketch (without extension .pde)
SKETCH=sketch
all: arduino-example \
arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size arduino-example.osd-merkur.hex \
arduino-example.osd-merkur.eep
endif
all: arduino-example $(PLATFORM_FILES)
CONTIKI=../../..
@ -15,17 +19,13 @@ PROJECT_SOURCEFILES += ${SKETCH}.cpp
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
endif
endif
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES= $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# variable for Makefile.include
@ -54,7 +54,7 @@ APPS += arduino
include $(CONTIKI)/Makefile.include
include $(CONTIKI)/apps/arduino/Makefile.include
arduino-example: arduino-example.osd-merkur
avr-size: arduino-example.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 arduino-example.osd-merkur
arduino-example.osd-merkur.hex: arduino-example.osd-merkur
@ -71,7 +71,7 @@ flash: arduino-example.osd-merkur.hex arduino-example.osd-merkur.eep
flash:w:arduino-example.osd-merkur.hex:a -U \
eeprom:w:arduino-example.osd-merkur.eep:a
.PHONY: flash
.PHONY: flash avr-size
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */

View file

@ -1,4 +1,9 @@
all: er-example-server
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size er-example-server.osd-merkur.hex \
er-example-server.osd-merkur.eep
endif
all: er-example-server $(PLATFORM_FILES)
# use target "er-plugtest-server" explicitly when requried
CONTIKI=../../..
@ -13,17 +18,13 @@ CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
endif
endif
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES= $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# linker optimizations
@ -72,3 +73,22 @@ connect-router-native: $(CONTIKI)/examples/ipv6/native-border-router/border-rout
connect-minimal:
sudo ip address add fdfd::1/64 dev tap0
avr-size: er-example-server.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
er-example-server.osd-merkur.hex: er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur \
er-example-server.osd-merkur.hex
er-example-server.osd-merkur.eep: er-example-server.osd-merkur
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex \
er-example-server.osd-merkur er-example-server.osd-merkur.eep
flash: er-example-server.osd-merkur.hex er-example-server.osd-merkur.eep
avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U \
flash:w:er-example-server.osd-merkur.hex:a -U \
eeprom:w:er-example-server.osd-merkur.eep:a
.PHONY: flash avr-size

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */

View file

@ -3,6 +3,3 @@
# BOOTLOADER_GET_MAC=0x0001ff80 (which is the current default)
make clean TARGET=osd-merkur
make TARGET=osd-merkur BOOTLOADER_GET_MAC=0x0001f3a0
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.eep

View file

@ -1,4 +1,9 @@
all: er-example-server
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size er-example-server.osd-merkur.hex \
er-example-server.osd-merkur.eep
endif
all: er-example-server $(PLATFORM_FILES)
# use target "er-plugtest-server" explicitly when requried
CONTIKI=../../..
@ -13,17 +18,13 @@ CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
endif
endif
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES= $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# linker optimizations
@ -72,3 +73,22 @@ connect-router-native: $(CONTIKI)/examples/ipv6/native-border-router/border-rout
connect-minimal:
sudo ip address add fdfd::1/64 dev tap0
avr-size: er-example-server.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
er-example-server.osd-merkur.hex: er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur \
er-example-server.osd-merkur.hex
er-example-server.osd-merkur.eep: er-example-server.osd-merkur
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex \
er-example-server.osd-merkur er-example-server.osd-merkur.eep
flash: er-example-server.osd-merkur.hex er-example-server.osd-merkur.eep
avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U \
flash:w:er-example-server.osd-merkur.hex:a -U \
eeprom:w:er-example-server.osd-merkur.eep:a
.PHONY: flash avr-size

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */

View file

@ -3,6 +3,3 @@
# BOOTLOADER_GET_MAC=0x0001ff80 (which is the current default)
make clean TARGET=osd-merkur
make TARGET=osd-merkur BOOTLOADER_GET_MAC=0x0001f3a0
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.eep

View file

@ -1,23 +1,36 @@
all: er-example-server er-example-client
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size-server er-example-server.osd-merkur.hex \
er-example-server.osd-merkur.eep \
avr-size-client er-example-client.osd-merkur.hex \
er-example-client.osd-merkur.eep
endif
all: er-example-server er-example-client $(PLATFORM_FILES)
# use target "er-plugtest-server" explicitly when requried
CONTIKI=../../..
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
# automatically build RESTful resources including common resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
REST_EXCLUDE=
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
REST_EXCLUDE=
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
REST_EXCLUDE= ! -name 'res-plugtest*'
endif
endif
REST_RESOURCES_DIR = ./resources
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES = $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c' $(REST_EXCLUDE)) \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# linker optimizations
@ -67,3 +80,39 @@ connect-router-native: $(CONTIKI)/examples/ipv6/native-border-router/border-rout
connect-minimal:
sudo ip address add fdfd::1/64 dev tap0
avr-size-server: er-example-server.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
er-example-server.osd-merkur.hex: er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur \
er-example-server.osd-merkur.hex
er-example-server.osd-merkur.eep: er-example-server.osd-merkur
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex \
er-example-server.osd-merkur er-example-server.osd-merkur.eep
flash-server: er-example-server.osd-merkur.hex er-example-server.osd-merkur.eep
avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U \
flash:w:er-example-server.osd-merkur.hex:a -U \
eeprom:w:er-example-server.osd-merkur.eep:a
avr-size-client: er-example-client.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-client.osd-merkur
er-example-client.osd-merkur.hex: er-example-client.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-client.osd-merkur \
er-example-client.osd-merkur.hex
er-example-client.osd-merkur.eep: er-example-client.osd-merkur
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex \
er-example-client.osd-merkur er-example-client.osd-merkur.eep
flash-client: er-example-client.osd-merkur.hex er-example-client.osd-merkur.eep
avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U \
flash:w:er-example-client.osd-merkur.hex:a -U \
eeprom:w:er-example-client.osd-merkur.eep:a
.PHONY: flash-client avr-size-client flash-server avr-size-server

View file

@ -1,2 +1,2 @@
#!/bin/bash
sudo avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U flash:w:er-example-server.osd-merkur.hex:a -U eeprom:w:er-example-server.osd-merkur.eep:a
sudo make flash-server

View file

@ -1,2 +1,2 @@
#!/bin/bash
sudo avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U flash:w:er-example-client.osd-merkur.hex:a -U eeprom:w:er-example-client.osd-merkur.eep:a
sudo make flash-client

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */

View file

@ -3,6 +3,3 @@
# BOOTLOADER_GET_MAC=0x0001ff80 (which is the current default)
make clean TARGET=osd-merkur
make TARGET=osd-merkur BOOTLOADER_GET_MAC=0x0001f3a0
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.eep

View file

@ -3,6 +3,3 @@
# BOOTLOADER_GET_MAC=0x0001ff80 (which is the current default)
make clean TARGET=osd-merkur
make TARGET=osd-merkur BOOTLOADER_GET_MAC=0x0001f3a0
avr-size -C --mcu=MCU=atmega128rfa1 er-example-client.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-client.osd-merkur er-example-client.osd-merkur.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex er-example-client.osd-merkur er-example-client.osd-merkur.eep

View file

@ -1,23 +1,27 @@
all: er-example-server er-example-client
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size-server er-example-server.osd-merkur.hex \
er-example-server.osd-merkur.eep \
avr-size-client er-example-client.osd-merkur.hex \
er-example-client.osd-merkur.eep
endif
all: er-example-server er-example-client $(PLATFORM_FILES)
# use target "er-plugtest-server" explicitly when requried
CONTIKI=../../..
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
# automatically build RESTful resources
# automatically build RESTful resources including common resources
REST_RESOURCES_DIR = ./resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
endif
endif
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES = $(notdir \
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# linker optimizations
@ -67,3 +71,39 @@ connect-router-native: $(CONTIKI)/examples/ipv6/native-border-router/border-rout
connect-minimal:
sudo ip address add fdfd::1/64 dev tap0
avr-size-server: er-example-server.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
er-example-server.osd-merkur.hex: er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur \
er-example-server.osd-merkur.hex
er-example-server.osd-merkur.eep: er-example-server.osd-merkur
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex \
er-example-server.osd-merkur er-example-server.osd-merkur.eep
flash-server: er-example-server.osd-merkur.hex er-example-server.osd-merkur.eep
avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U \
flash:w:er-example-server.osd-merkur.hex:a -U \
eeprom:w:er-example-server.osd-merkur.eep:a
avr-size-client: er-example-client.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-client.osd-merkur
er-example-client.osd-merkur.hex: er-example-client.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-client.osd-merkur \
er-example-client.osd-merkur.hex
er-example-client.osd-merkur.eep: er-example-client.osd-merkur
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex \
er-example-client.osd-merkur er-example-client.osd-merkur.eep
flash-client: er-example-client.osd-merkur.hex er-example-client.osd-merkur.eep
avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U \
flash:w:er-example-client.osd-merkur.hex:a -U \
eeprom:w:er-example-client.osd-merkur.eep:a
.PHONY: flash-client avr-size-client flash-server avr-size-server

View file

@ -1,2 +1,2 @@
#!/bin/bash
sudo avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U flash:w:er-example-server.osd-merkur.hex:a -U eeprom:w:er-example-server.osd-merkur.eep:a
sudo make flash-server

View file

@ -1,2 +1,2 @@
#!/bin/bash
sudo avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U flash:w:er-example-client.osd-merkur.hex:a -U eeprom:w:er-example-client.osd-merkur.eep:a
sudo make flash-client

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */

View file

@ -3,6 +3,3 @@
# BOOTLOADER_GET_MAC=0x0001ff80 (which is the current default)
make clean TARGET=osd-merkur
make TARGET=osd-merkur BOOTLOADER_GET_MAC=0x0001f3a0
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex er-example-server.osd-merkur er-example-server.osd-merkur.eep

View file

@ -3,6 +3,3 @@
# BOOTLOADER_GET_MAC=0x0001ff80 (which is the current default)
make clean TARGET=osd-merkur
make TARGET=osd-merkur BOOTLOADER_GET_MAC=0x0001f3a0
avr-size -C --mcu=MCU=atmega128rfa1 er-example-client.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-client.osd-merkur er-example-client.osd-merkur.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex er-example-client.osd-merkur er-example-client.osd-merkur.eep

View file

@ -1,17 +1,16 @@
all: er-example-server \
er-example-server.osd-merkur.hex er-example-server.osd-merkur.eep
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size er-example-server.osd-merkur.hex \
er-example-server.osd-merkur.eep
endif
all: er-example-server $(PLATFORM_FILES)
# use this target explicitly if requried: er-plugtest-server
# variable for this Makefile
# configure CoAP implementation (3|7|12|13) (er-coap-07 also supports CoAP draft 08)
WITH_COAP=13
# for some platforms
UIP_CONF_IPV6=1
# IPv6 make config disappeared completely
CFLAGS += -DUIP_CONF_IPV6=1
CONTIKI=../../..
# Contiki IPv6 configuration
CONTIKI_WITH_IPV6 = 1
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
PROJECT_SOURCEFILES += resource_led_pwm.c
@ -33,43 +32,16 @@ endif
# linker optimizations
SMALL=1
# REST framework, requires WITH_COAP
ifeq ($(WITH_COAP), 13)
${info INFO: compiling with CoAP-13}
CFLAGS += -DWITH_COAP=13
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-13
else ifeq ($(WITH_COAP), 12)
${info INFO: compiling with CoAP-12}
CFLAGS += -DWITH_COAP=12
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-12
else ifeq ($(WITH_COAP), 7)
${info INFO: compiling with CoAP-08}
CFLAGS += -DWITH_COAP=7
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-07
else ifeq ($(WITH_COAP), 3)
${info INFO: compiling with CoAP-03}
CFLAGS += -DWITH_COAP=3
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-03
else
${info INFO: compiling with HTTP}
CFLAGS += -DWITH_HTTP
CFLAGS += -DREST=http_rest_implementation
CFLAGS += -DUIP_CONF_TCP=1
APPS += er-http-engine
endif
APPS += erbium json json-resource
# REST Engine shall use Erbium CoAP implementation
APPS += er-coap
APPS += rest-engine
APPS += json json-resource
include $(CONTIKI)/Makefile.include
avr-size: er-example-server.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
er-example-server.osd-merkur.hex: er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur \
er-example-server.osd-merkur.hex
@ -84,7 +56,7 @@ flash: er-example-server.osd-merkur.hex er-example-server.osd-merkur.eep
flash:w:er-example-server.osd-merkur.hex:a -U \
eeprom:w:er-example-server.osd-merkur.eep:a
.PHONY: flash
.PHONY: flash avr-size
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)

View file

@ -43,9 +43,7 @@
#include "contiki.h"
#include "contiki-net.h"
#include "jsonparse.h"
#include "erbium.h"
#include "er-coap-13.h"
#include "er-coap.h"
#include "led_pwm.h"
@ -58,44 +56,6 @@
/******************************************************************************/
/*
* Resources are defined by the RESOURCE macro.
* Signature: resource name, the RESTful methods it handles, and its URI
* path (omitting the leading slash).
*/
RESOURCE(info, METHOD_GET, "info", "title=\"Info\";rt=\"text\"");
/*
* A handler function named [resource name]_handler must be implemented
* for each RESOURCE. A buffer for the response payload is provided
* through the buffer pointer. Simple resources can ignore
* preferred_size and offset, but must respect the REST_MAX_CHUNK_SIZE
* limit for the buffer. If a smaller block size is requested for CoAP,
* the REST framework automatically splits the data.
*/
void
info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
char message[100];
int index = 0;
int length = 0; /* |<-------->| */
/*
* Some data that has the length up to REST_MAX_CHUNK_SIZE. For more,
* see the chunk resource.
*/
// jSON Format
index += sprintf(message + index,"{\n \"Version\" : \"V1.0pre1\",\n");
index += sprintf(message + index," \"name\" : \"PWM\"\n");
index += sprintf(message + index,"}\n");
length = strlen(message);
memcpy(buffer, message,length );
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
REST.set_response_payload(response, buffer, length);
}
void
hw_init()
{
@ -137,8 +97,7 @@ PROCESS_THREAD(rest_server_example, ev, data)
rest_init_engine();
/* Activate the application-specific resources. */
rest_activate_resource(&resource_info);
rest_activate_resource(&resource_led_pwm);
rest_activate_resource(&res_led_pwm, "led/pwm");
/* Define application-specific events here. */
while(1) {

View file

@ -17,9 +17,8 @@
#ifndef led_pwm_h
#define led_pwm_h
#include "contiki.h"
#include "erbium.h"
extern resource_t resource_led_pwm;
extern resource_t res_led_pwm;
extern void led_pwm_init (void);
#endif // led_pwm_h

View file

@ -12,8 +12,7 @@
#include <string.h>
#include "contiki.h"
#include "jsonparse.h"
/* Only coap 13 for now */
#include "er-coap-13.h"
#include "er-coap.h"
#include "hw_timer.h"
#include "generic_resource.h"
@ -59,8 +58,7 @@ size_t pwm_to_string (const char *n, uint8_t is_json, char *buf, size_t bufsize)
}
GENERIC_RESOURCE \
( led_pwm, METHOD_GET | METHOD_PUT
, "led/pwm"
( led_pwm
, LED PWM
, duty-cycle
, pwm_from_string

View file

@ -1,4 +1,9 @@
all: er-example-server er-example-client
ifeq ($(TARGET), osd-merkur)
PLATFORM_FILES= avr-size er-example-server.osd-merkur.hex \
er-example-server.osd-merkur.eep
endif
all: er-example-server $(PLATFORM_FILES)
# use target "er-plugtest-server" explicitly when requried
CONTIKI=../../..
@ -7,17 +12,12 @@ CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
ifndef TARGET
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
ifeq ($(TARGET), native)
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c'))
else
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
endif
endif
REST_RESOURCES_DIR_COMMON = ../resources-common
REST_RESOURCES_FILES= $(notdir \
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
)
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# linker optimizations
@ -26,6 +26,7 @@ SMALL=1
# REST Engine shall use Erbium CoAP implementation
APPS += er-coap
APPS += rest-engine
APPS += json json-resource time
# optional rules to get assembly
#CUSTOM_RULE_C_TO_OBJECTDIR_O = 1
@ -67,3 +68,22 @@ connect-router-native: $(CONTIKI)/examples/ipv6/native-border-router/border-rout
connect-minimal:
sudo ip address add fdfd::1/64 dev tap0
avr-size: er-example-server.osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-server.osd-merkur
er-example-server.osd-merkur.hex: er-example-server.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-server.osd-merkur \
er-example-server.osd-merkur.hex
er-example-server.osd-merkur.eep: er-example-server.osd-merkur
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex \
er-example-server.osd-merkur er-example-server.osd-merkur.eep
flash: er-example-server.osd-merkur.hex er-example-server.osd-merkur.eep
avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U \
flash:w:er-example-server.osd-merkur.hex:a -U \
eeprom:w:er-example-server.osd-merkur.eep:a
.PHONY: flash avr-size

View file

@ -122,7 +122,6 @@ PROCESS_THREAD(rest_server_example, ev, data)
rest_init_engine();
/* Activate the application-specific resources. */
rest_activate_resource(&res_info);
rest_activate_resource(&res_leds, "s/leds");
@ -131,9 +130,9 @@ PROCESS_THREAD(rest_server_example, ev, data)
rest_activate_resource(&res_battery, "s/battery");
#endif
rest_activate_resource(&res_timestamp, "s/timestamp");
rest_activate_resource(&res_localtime, "localtime");
rest_activate_resource(&res_utc, "utc");
rest_activate_resource(&res_timestamp, "clock/timestamp");
rest_activate_resource(&res_localtime, "clock/localtime");
rest_activate_resource(&res_utc, "clock/utc");
/* Define application-specific events here. */
while(1) {

View file

@ -1,81 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_BATTERY
#include <string.h>
#include "rest-engine.h"
#include "dev/battery-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(res_battery,
"title=\"Battery status\";rt=\"Battery\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif /* PLATFORM_HAS_BATTERY */

View file

@ -1,108 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_LEDS
#include <string.h>
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(res_leds,
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
NULL,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *color = NULL;
const char *mode = NULL;
uint8_t led = 0;
int success = 1;
if((len = REST.get_query_variable(request, "color", &color))) {
PRINTF("color %.*s\n", len, color);
if(strncmp(color, "r", len) == 0) {
led = LEDS_RED;
} else if(strncmp(color, "g", len) == 0) {
led = LEDS_GREEN;
} else if(strncmp(color, "b", len) == 0) {
led = LEDS_BLUE;
} else {
success = 0;
}
} else {
success = 0;
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
PRINTF("mode %s\n", mode);
if(strncmp(mode, "on", len) == 0) {
leds_on(led);
} else if(strncmp(mode, "off", len) == 0) {
leds_off(led);
} else {
success = 0;
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_LEDS */

View file

@ -1,102 +0,0 @@
/*
* 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
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include "contiki.h"
#if PLATFORM_HAS_RADIO
#include <string.h>
#include "rest-engine.h"
#include "dev/radio-sensor.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
RESOURCE(res_radio,
"title=\"RADIO: ?p=lqi|rssi\";rt=\"RadioSensor\"",
res_get_handler,
NULL,
NULL,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *p = NULL;
uint8_t param = 0;
int success = 1;
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if((len = REST.get_query_variable(request, "p", &p))) {
if(strncmp(p, "lqi", len) == 0) {
param = RADIO_SENSOR_LAST_VALUE;
} else if(strncmp(p, "rssi", len) == 0) {
param = RADIO_SENSOR_LAST_PACKET;
} else {
success = 0;
}
} else {
success = 0;
} if(success) {
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", radio_sensor.value(param));
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
if(param == RADIO_SENSOR_LAST_VALUE) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'lqi':%d}", radio_sensor.value(param));
} else if(param == RADIO_SENSOR_LAST_PACKET) {
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", radio_sensor.value(param));
}
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
#endif /* PLATFORM_HAS_RADIO */