osd-contiki/examples/osd/embedd-vm-merkurboard/embedd-vm-server.c
2014-04-15 07:27:30 +02:00

530 lines
16 KiB
C

/*
* Copyright (c) 2011, Matthias Kovatsch and other contributors.
* 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
* Erbium (Er) REST Engine example (with CoAP-specific code)
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "contiki.h"
#include "contiki-net.h"
/* Define which resources to include to meet memory constraints. */
#define REST_RES_INFO 1
#define REST_RES_EVM 1
#define REST_RES_CHUNKS 0
#define REST_RES_BATTERY 1
#define REST_RES_LEDS 1
#define REST_RES_TOGGLE 1
#if !UIP_CONF_IPV6_RPL && !defined (CONTIKI_TARGET_MINIMAL_NET) && !defined (CONTIKI_TARGET_NATIVE)
#warning "Compiling with static routing!"
#include "static-routing.h"
#endif
#include "erbium.h"
#if defined (PLATFORM_HAS_BUTTON)
#include "dev/button-sensor.h"
#endif
#if defined (PLATFORM_HAS_LEDS)
#include "dev/leds.h"
#endif
#if defined (PLATFORM_HAS_BATTERY)
#include "dev/battery-sensor.h"
#endif
// embedd-vm
#include "embedvm.h"
/* For CoAP-specific example: not required for normal RESTful Web service. */
#if WITH_COAP == 3
#include "er-coap-03.h"
#elif WITH_COAP == 7
#include "er-coap-07.h"
#elif WITH_COAP == 12
#include "er-coap-12.h"
#elif WITH_COAP == 13
#include "er-coap-13.h"
#else
#warning "Erbium example without CoAP-specifc functionality"
#endif /* CoAP-specific example */
#define DEBUG 1
#if DEBUG
#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
/******************************************************************************/
#if REST_RES_INFO
/*
* 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\" : \"V0.1\",\n");
index += sprintf(message + index," \"name\" : \"embedd-vm demo\"\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);
}
#endif
/******************************************************************************/
#if REST_RES_CHUNKS
/*
* For data larger than REST_MAX_CHUNK_SIZE (e.g., stored in flash) resources must be aware of the buffer limitation
* and split their responses by themselves. To transfer the complete resource through a TCP stream or CoAP's blockwise transfer,
* the byte offset where to continue is provided to the handler as int32_t pointer.
* These chunk-wise resources must set the offset value to its new position or -1 of the end is reached.
* (The offset for CoAP's blockwise transfer can go up to 2'147'481'600 = ~2047 M for block size 2048 (reduced to 1024 in observe-03.)
*/
RESOURCE(chunks, METHOD_GET, "test/chunks", "title=\"Blockwise demo\";rt=\"Data\"");
#define CHUNKS_TOTAL 2050
void
chunks_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int32_t strpos = 0;
/* Check the offset for boundaries of the resource data. */
if (*offset>=CHUNKS_TOTAL)
{
REST.set_response_status(response, REST.status.BAD_OPTION);
/* A block error message should not exceed the minimum block size (16). */
const char *error_msg = "BlockOutOfScope";
REST.set_response_payload(response, error_msg, strlen(error_msg));
return;
}
/* Generate data until reaching CHUNKS_TOTAL. */
while (strpos<preferred_size)
{
strpos += snprintf((char *)buffer+strpos, preferred_size-strpos+1, "|%ld|", *offset);
}
/* snprintf() does not adjust return value if truncated by size. */
if (strpos > preferred_size)
{
strpos = preferred_size;
}
/* Truncate if above CHUNKS_TOTAL bytes. */
if (*offset+(int32_t)strpos > CHUNKS_TOTAL)
{
strpos = CHUNKS_TOTAL - *offset;
}
REST.set_response_payload(response, buffer, strpos);
/* IMPORTANT for chunk-wise resources: Signal chunk awareness to REST engine. */
*offset += strpos;
/* Signal end of resource representation. */
if (*offset>=CHUNKS_TOTAL)
{
*offset = -1;
}
}
#endif
/******************************************************************************/
#if defined (PLATFORM_HAS_LEDS)
/******************************************************************************/
#if REST_RES_LEDS
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(leds, METHOD_POST | METHOD_PUT , "actuators/leds", "title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"");
void
leds_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
/******************************************************************************/
#if REST_RES_TOGGLE
/* A simple actuator example. Toggles the red led */
RESOURCE(toggle, METHOD_GET | METHOD_PUT | METHOD_POST, "actuators/toggle", "title=\"Red LED\";rt=\"Control\"");
void
toggle_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
leds_toggle(LEDS_RED);
}
#endif
#endif /* PLATFORM_HAS_LEDS */
/******************************************************************************/
#if REST_RES_BATTERY && defined (PLATFORM_HAS_BATTERY)
/* A simple getter example. Returns the reading from light sensor with a simple etag */
RESOURCE(battery, METHOD_GET, "sensors/battery", "title=\"Battery status\";rt=\"Battery\"");
void
battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int battery = battery_sensor.value(0);
const uint16_t *accept = NULL;
int num = REST.get_header_accept(request, &accept);
if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN))
{
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", battery);
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
}
else if (num && (accept[0]==REST.type.APPLICATION_JSON))
{
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d}", battery);
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 */
#include "vmcode.hdr"
#define UNUSED __attribute__((unused))
uint8_t vm_mem[256] = { EMBEDVM_SECT_SRAM_DATA };
struct embedvm_s vm = { };
// Global Variable coap answear
char gmessage[100];
int gindex = 0;
/******************************************************************************/
#if defined (PLATFORM_HAS_EVM)
/******************************************************************************/
#if REST_RES_EVM
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
RESOURCE(evm, METHOD_GET | METHOD_POST | METHOD_PUT , "evm", "title=\"Embedd-VM, POST/PUT player=0/9999\";rt=\"Embedd-VM\"");
void
evm_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *player = NULL;
int success = 1;
//int length = 0; /* |<-------->| */
int16_t val = 0;
uint16_t old_ip=0;
const uint16_t *accept = NULL;
switch(REST.get_method_type(request)){
case METHOD_GET:
gindex=0;
gindex += sprintf(gmessage + gindex,"Number-Quiz!\nI think at a Number between 0 and 9999 :-)\n");
break;
case METHOD_PUT:
if ((len=REST.get_post_variable(request, "player", &player))) {
PRINTF("player %.*s\n", len, player);
gindex=0;
val = atoi(player);
embedvm_push(&vm, 1);
embedvm_push(&vm, val);
old_ip=vm.ip;
embedvm_interrupt(&vm, EMBEDVM_SYM_play);
while (vm.ip != old_ip) {
// printf("<%X>",vm.ip);
embedvm_exec(&vm);
}
embedvm_pop(&vm);
embedvm_pop(&vm);
} else {
success = 0;
}
break;
default:
success = 0;
}
int num = REST.get_header_accept(request, &accept);
if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN))
{
if(success){
//length = strlen(gmessage);
memcpy(buffer, gmessage,gindex );
buffer[gindex]=0;
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
else
{
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain";
REST.set_response_payload(response, msg, strlen(msg));
}
}
#endif
#endif
int16_t mem_read(uint16_t addr, bool is16bit, void *ctx)
{
if (addr + (is16bit ? 1 : 0) >= sizeof(vm_mem))
return 0;
if (is16bit)
return (vm_mem[addr] << 8) | vm_mem[addr+1];
return vm_mem[addr];
}
void mem_write(uint16_t addr, int16_t value, bool is16bit, void *ctx)
{
if (addr + (is16bit ? 1 : 0) >= sizeof(vm_mem))
return;
if (is16bit) {
vm_mem[addr] = value >> 8;
vm_mem[addr+1] = value;
} else
vm_mem[addr] = value;
}
extern uint8_t rng_get_uint8(void);
int16_t call_user(uint8_t funcid, uint8_t argc, int16_t *argv, void *ctx)
{
if (funcid == 0)
return (random()%99)+1;
if (funcid == 1 && argc >= 1) {
if (argv[0] > 0){
gindex += sprintf(gmessage + gindex,"Try larger numbers.\n");
PRINTF("Try larger numbers.\n");
}else{
gindex += sprintf(gmessage + gindex,"Try smaler numbers.\n");
PRINTF("Try smaller numbers.\n");
}
return 0;
}
if (funcid == 2) {
gindex += sprintf(gmessage + gindex,"This is correct!\n");
PRINTF("This is correct!\n");
return 0;
}
if (funcid == 3) {
gindex += sprintf(gmessage + gindex,"\nYou currently have %d points.\n", mem_read(EMBEDVM_SYM_points, true, ctx));
PRINTF("\n");
PRINTF("You currently have %d points.\n", mem_read(EMBEDVM_SYM_points, true, ctx));
return 0;
}
if (funcid == 4) {
PRINTF("\n");
PRINTF("D: %d\n", mem_read(EMBEDVM_SYM_dbg, true, ctx));
return 0;
}
if (funcid == 10) {
PRINTF("D: LED-RED off\n");
leds_off(LEDS_RED);
return 0;
}
if (funcid == 11) {
PRINTF("D: LED-RED on\n");
leds_on(LEDS_RED);
return 0;
}
if (funcid == 12) {
PRINTF("D: LED-RED Toggle\n");
leds_toggle(LEDS_RED);
return 0;
}
return 1;
}
void vm_setup()
{
PRINTF("Initializing embedded-vm...\n");
vm.ip = EMBEDVM_SYM_main;
vm.sp = vm.sfp = sizeof(vm_mem);
vm.mem_read = &mem_read;
vm.mem_write = &mem_write;
vm.call_user = &call_user;
embedvm_push(&vm, 0);
while (vm.ip > 0) {
// printf("<%X>",vm.ip);
embedvm_exec(&vm);
}
}
void
hw_init()
{
#if defined (PLATFORM_HAS_LEDS)
leds_off(LEDS_RED);
#endif
vm_setup();
}
PROCESS(rest_server_example, "Erbium Example Server");
AUTOSTART_PROCESSES(&rest_server_example);
PROCESS_THREAD(rest_server_example, ev, data)
{
PROCESS_BEGIN();
PRINTF("Starting Embedd-VM Example Server\n");
#ifdef RF_CHANNEL
PRINTF("RF channel: %u\n", RF_CHANNEL);
#endif
#ifdef IEEE802154_PANID
PRINTF("PAN ID: 0x%04X\n", IEEE802154_PANID);
#endif
PRINTF("uIP buffer: %u\n", UIP_BUFSIZE);
PRINTF("LL header: %u\n", UIP_LLH_LEN);
PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN);
PRINTF("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE);
/* if static routes are used rather than RPL */
#if !UIP_CONF_IPV6_RPL && !defined (CONTIKI_TARGET_MINIMAL_NET) && !defined (CONTIKI_TARGET_NATIVE)
set_global_address();
configure_routing();
#endif
/* Initialize the OSD Hardware. */
hw_init();
/* Initialize the REST engine. */
rest_init_engine();
/* Activate the application-specific resources. */
#if REST_RES_INFO
rest_activate_resource(&resource_info);
#endif
#if REST_RES_CHUNKS
rest_activate_resource(&resource_chunks);
#endif
#if defined (PLATFORM_HAS_LEDS)
#if REST_RES_LEDS
rest_activate_resource(&resource_leds);
#endif
#if REST_RES_TOGGLE
rest_activate_resource(&resource_toggle);
#endif
#endif /* PLATFORM_HAS_LEDS */
#if REST_RES_EVM
rest_activate_resource(&resource_evm);
#endif
#if defined (PLATFORM_HAS_BATTERY) && REST_RES_BATTERY
SENSORS_ACTIVATE(battery_sensor);
rest_activate_resource(&resource_battery);
#endif
while(1) {
PROCESS_WAIT_EVENT();
} /* while (1) */
PROCESS_END();
}