136 lines
3.9 KiB
C
136 lines
3.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "contiki.h"
|
|
#include "contiki-net.h"
|
|
#include "dev/leds.h"
|
|
|
|
#include "rest.h"
|
|
|
|
PROCESS(rest_example, "Rest Example");
|
|
AUTOSTART_PROCESSES(&rest_example);
|
|
|
|
#define DEBUG 1
|
|
#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 ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_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
|
|
|
|
char temp[100];
|
|
|
|
/* Resources are defined by RESOURCE macro, signature: resource name, the http methods it handles and its url*/
|
|
RESOURCE(helloworld, METHOD_GET, "helloworld");
|
|
|
|
/* For each resource defined, there corresponds an handler method which should be defined too.
|
|
* Name of the handler method should be [resource name]_handler
|
|
* */
|
|
void
|
|
helloworld_handler(REQUEST* request, RESPONSE* response)
|
|
{
|
|
sprintf(temp,"Hello World!\n");
|
|
|
|
rest_set_header_content_type(response, TEXT_PLAIN);
|
|
rest_set_payload(response, temp, strlen(temp));
|
|
}
|
|
|
|
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
|
|
RESOURCE(led, METHOD_POST | METHOD_PUT , "led");
|
|
|
|
void
|
|
led_handler(REQUEST* request, RESPONSE* response)
|
|
{
|
|
char color[10];
|
|
char mode[10];
|
|
uint8_t led = 0;
|
|
int success = 1;
|
|
|
|
if (rest_get_query_variable(request, "color", color, 10)) {
|
|
PRINTF("color %s\n", color);
|
|
|
|
if (!strcmp(color,"red")) {
|
|
led = LEDS_RED;
|
|
} else if(!strcmp(color,"green")) {
|
|
led = LEDS_GREEN;
|
|
} else if ( !strcmp(color,"blue") ) {
|
|
led = LEDS_BLUE;
|
|
} else {
|
|
success = 0;
|
|
}
|
|
} else {
|
|
success = 0;
|
|
}
|
|
|
|
if (success && rest_get_post_variable(request, "mode", mode, 10)) {
|
|
PRINTF("mode %s\n", mode);
|
|
|
|
if (!strcmp(mode, "on")) {
|
|
leds_on(led);
|
|
} else if (!strcmp(mode, "off")) {
|
|
leds_off(led);
|
|
} else {
|
|
success = 0;
|
|
}
|
|
} else {
|
|
success = 0;
|
|
}
|
|
|
|
if (!success) {
|
|
rest_set_response_status(response, BAD_REQUEST_400);
|
|
}
|
|
}
|
|
|
|
/*A simple getter example. Returns the reading from light sensor with a simple etag*/
|
|
RESOURCE(light, METHOD_GET, "light");
|
|
void
|
|
light_handler(REQUEST* request, RESPONSE* response)
|
|
{
|
|
sprintf(temp,"%d.%d", 131,6);
|
|
|
|
char etag[4] = "ABCD";
|
|
rest_set_header_content_type(response, TEXT_PLAIN);
|
|
rest_set_header_etag(response, etag, sizeof(etag));
|
|
rest_set_payload(response, temp, strlen(temp));
|
|
}
|
|
|
|
/*A simple actuator example. Toggles the red led*/
|
|
RESOURCE(toggle, METHOD_GET | METHOD_PUT | METHOD_POST, "toggle");
|
|
void
|
|
toggle_handler(REQUEST* request, RESPONSE* response)
|
|
{
|
|
leds_toggle(LEDS_RED);
|
|
}
|
|
|
|
RESOURCE(discover, METHOD_GET, ".well-known/core");
|
|
void
|
|
discover_handler(REQUEST* request, RESPONSE* response)
|
|
{
|
|
char temp[100];
|
|
int index = 0;
|
|
index += sprintf(temp + index, "%s\n", "<helloworld>;n=\"HelloWorld\"");
|
|
index += sprintf(temp + index, "%s\n", "<led>;n=\"LedControl\"");
|
|
index += sprintf(temp + index, "%s\n", "<light>;n=\"Light\"");
|
|
|
|
rest_set_payload(response, temp, strlen(temp));
|
|
rest_set_header_content_type(response, APPLICATION_LINK_FORMAT);
|
|
}
|
|
|
|
PROCESS_THREAD(rest_example, ev, data)
|
|
{
|
|
PROCESS_BEGIN();
|
|
PRINTF("REST Example\n");
|
|
|
|
rest_init();
|
|
|
|
rest_activate_resource(&resource_helloworld);
|
|
rest_activate_resource(&resource_led);
|
|
rest_activate_resource(&resource_light);
|
|
rest_activate_resource(&resource_toggle);
|
|
rest_activate_resource(&resource_discover);
|
|
|
|
PROCESS_END();
|
|
}
|