Harald Pichler 2017-10-06 13:34:48 +02:00
commit a9cb7ab7be
14 changed files with 279 additions and 84 deletions

View File

@ -0,0 +1 @@
ota-update_src = res_bootloader.c res_reboot.c res_upload_image.c

View File

@ -0,0 +1,22 @@
extern resource_t res_upload_image;
extern resource_t res_part_count;
extern resource_t res_part_size;
extern resource_t res_boot_default;
extern resource_t res_boot_next;
extern resource_t res_active_part;
extern resource_t res_part_start;
extern resource_t res_part_ok;
extern resource_t res_reboot;
#define OTA_ACTIVATE_RESOURCES() \
static char resname[] = "ota/update";\
rest_activate_resource (&res_upload_image, resname);\
rest_activate_resource (&res_part_count, (char *)"ota/part_count");\
rest_activate_resource (&res_part_size, (char *)"ota/part_size");\
rest_activate_resource (&res_boot_default, (char *)"ota/boot_default");\
rest_activate_resource (&res_boot_next, (char *)"ota/boot_next");\
rest_activate_resource (&res_active_part, (char *)"ota/active_part");\
rest_activate_resource (&res_part_start, (char *)"ota/part_start");\
rest_activate_resource (&res_part_ok, (char *)"ota/part_ok");\
rest_activate_resource (&res_reboot, (char *)"ota/reboot");

View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2017, Marcus Priesch Open Source Consulting
* 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
* Reboot ressource
* \author
* Marcus Priesch <marcus@priesch.co.at>
*/
#include <stdio.h>
#include <string.h>
#include "contiki.h"
#include "er-coap-engine.h"
#include "generic_resource.h"
#include "dev/watchdog.h"
PROCESS(reboot_process, "reboot");
PROCESS_THREAD(reboot_process, ev, data)
{
static struct etimer etimer;
//PROCESS_EXITHANDLER(leds_off(LEDS_ALL);)
PROCESS_BEGIN();
//shell_output_str(&reboot_command,
// "Rebooting the node in four seconds...", "");
etimer_set(&etimer, CLOCK_SECOND * 4);
PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
//leds_on(LEDS_RED);
//etimer_reset(&etimer);
//PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
//leds_on(LEDS_GREEN);
//etimer_reset(&etimer);
//PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
//leds_on(LEDS_BLUE);
//etimer_reset(&etimer);
//PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
watchdog_reboot();
PROCESS_END();
}
static size_t
get_reboot
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
return snprintf (buf, bsize, "put 'OK' to reboot.");
}
static int
do_reboot
(const char *name, const char *uri, const char *query, const char *s)
{
if (strncmp (s, "OK", 2) == 0) {
process_start (&reboot_process, NULL);
return 0;
}
return 0;
}
GENERIC_RESOURCE
( reboot
, Reboot node
, count
, 0
, do_reboot
, get_reboot
);

View File

@ -46,7 +46,7 @@ SMALL=1
# REST Engine shall use Erbium CoAP implementation
APPS += er-coap
APPS += rest-engine
APPS += arduino
APPS += arduino json-resource json ota-update
include $(CONTIKI)/Makefile.include
include $(CONTIKI)/apps/arduino/Makefile.include

View File

@ -32,24 +32,13 @@
#ifndef PROJECT_RPL_WEB_CONF_H_
#define PROJECT_RPL_WEB_CONF_H_
#ifdef IEEE802154_CONF_PANID
#undef IEEE802154_CONF_PANID
#define IEEE802154_CONF_PANID 0xA001 // default panid
#endif // IEEE802154_CONF_PANID
#ifdef CHANNEL_802_15_4
#undef CHANNEL_802_15_4
#define CHANNEL_802_15_4 25 // default frequency (11-26)
#endif
//#define PLATFORM_HAS_LEDS 1
//#define PLATFORM_HAS_BUTTON 1
//#define PLATFORM_HAS_BATTERY 1
#define SICSLOWPAN_CONF_FRAG 1
#define LOOP_INTERVAL (CLOCK_SECOND)
#define LOOP_INTERVAL (CLOCK_SECOND/4)
#define WIND_SPEEDS 30
/* Save energy */

View File

@ -13,45 +13,65 @@ extern "C" {
#include <stdio.h>
#include "arduino-process.h"
#include "rest-engine.h"
#include "resources.h"
#include "ota-update.h"
extern resource_t res_wind, res_wind_speed_max, res_wind_status,
res_wind_speed, res_wind_direction, res_power_supply;
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
extern resource_t
res_wind,
res_wind_speed_max,
res_wind_status,
res_wind_speed,
res_wind_direction,
res_power_supply;
#define STX 0x02
#define ETX 0x03
#define STRING_TERMINATOR 0x00
#define LED_PIN 4
}
int state = 0;
char ws_status [32];
int ws_speed_hi = 0;
int ws_speed_lo = 0;
char ws_unit [8];
int ws_direction = 0;
int state = 0;
char ws_status [32];
int ws_speed_hi = 0;
int ws_speed_lo = 0;
char ws_unit [8];
int ws_direction = 0;
int supply_voltage_hi = 0;
int supply_voltage_lo = 0;
int ws_max_speed = 0;
int ws_max_speed_lo = 0;
int ws_max_speed_hi = 0;
int supply_voltage_lo = 0;
int ws_max_speed = 0;
int ws_max_speed_lo = 0;
int ws_max_speed_hi = 0;
int wind_speeds [WIND_SPEEDS]; // every 2 seconds we get a value
int wind_speed_idx = 0;
uint32_t idx;
int d, count, msg_length, state0_counter = 0;
unsigned char msg_buffer [128], checksum_buffer [3], checksum;
void set_no_data (void)
{
printf ("set no data\n");
PRINTF ("set no data\n");
strcpy (ws_status, "No data from sensor");
ws_speed_lo = 000;
ws_speed_hi = 000;
ws_direction = 000;
ws_max_speed = 0;
ws_speed_lo = 0;
ws_speed_hi = 0;
ws_direction = 0;
ws_max_speed = 0;
ws_max_speed_hi = 0;
ws_max_speed_lo = 0;
wind_speed_idx = 0;
wind_speed_idx = 0;
strcpy (ws_unit, "");
for (idx = 0; idx < WIND_SPEEDS; idx++)
@ -71,15 +91,16 @@ void setup (void)
rest_init_engine ();
#pragma GCC diagnostic ignored "-Wwrite-strings"
rest_activate_resource (&res_wind, "s/wind");
rest_activate_resource (&res_wind_speed, "s/wind_speed");
rest_activate_resource (&res_wind, "s/wind");
rest_activate_resource (&res_wind_speed, "s/wind_speed");
rest_activate_resource (&res_wind_speed_max, "s/wind_speed_max");
rest_activate_resource (&res_wind_direction, "s/wind_direction");
rest_activate_resource (&res_wind_status, "s/wind_status");
rest_activate_resource (&res_power_supply, "s/power_supply");
rest_activate_resource (&res_wind_status, "s/wind_status");
rest_activate_resource (&res_power_supply, "s/power_supply");
OTA_ACTIVATE_RESOURCES();
#pragma GCC diagnostic pop
//mcu_sleep_set(128); // Power consumtion 278uA; average over 20 minutes
mcu_sleep_set(0); // Power consumtion 278uA; average over 20 minutes
Serial1.begin (9600);
set_no_data ();
@ -87,14 +108,10 @@ void setup (void)
void loop (void)
{
static int d, count, msg_length, state0_counter = 0;
static unsigned char msg_buffer [128], checksum_buffer [3], checksum;
printf ("state = %d", state);
switch (state) {
case 0 :
printf ("state0\n");
PRINTF ("state0\n");
strcpy (ws_status, "0");
state0_counter ++;
if (state0_counter > 10) {
set_no_data ();
@ -103,7 +120,7 @@ void loop (void)
// wait for stx
while (Serial1.available () > 0) {
d = Serial1.read ();
printf ("%d ", d);
PRINTF ("%d ", d);
if (d == STX) {
state = 1;
count = 0;
@ -114,13 +131,14 @@ void loop (void)
break;
case 1 :
printf ("state1\n");
PRINTF ("state1\n");
strcpy (ws_status, "1");
state0_counter = 0;
// wait for etx and read message
while (Serial1.available () > 0) {
d = Serial1.read ();
printf ("%d ", d);
PRINTF ("%d ", d);
if (d == ETX) {
msg_length = count;
msg_buffer [count] = STRING_TERMINATOR;
@ -137,16 +155,17 @@ void loop (void)
}
}
}
if (state == 1)
if (state == 1 || state == 0)
break;
case 2 :
printf ("state2\n");
PRINTF ("state2\n");
strcpy (ws_status, "2");
// read checksum
while (Serial1.available () > 0) {
d = Serial1.read ();
printf ("%d ", d);
printf (" %d (%d)\n", d, count);
PRINTF ("%d ", d);
PRINTF (" %d (%d)\n", d, count);
checksum_buffer [count] = (unsigned char) (d & 0xff);
count ++;
if (count == 2) {
@ -162,7 +181,8 @@ void loop (void)
unsigned int checksum_rx;
int cnt;
printf ("state3\n");
PRINTF ("state3\n");
strcpy (ws_status, "3");
// calc and compare checksum
checksum = 0;
sscanf ((const char*)checksum_buffer, "%2X", &checksum_rx);
@ -171,7 +191,8 @@ void loop (void)
}
if (checksum_rx != checksum) {
printf ("checksum mismatch %02x != %02x\n", checksum, checksum_rx);
PRINTF ("checksum mismatch %02x != %02x\n", checksum, checksum_rx);
sprintf (ws_status, "CS: %02x!=%02x,%s", checksum, checksum_rx,msg_buffer);
state = 0;
break;
} else {
@ -183,8 +204,9 @@ void loop (void)
char unit [1];
int status;
printf ("state4\n");
printf ("parse messge: %s\n", msg_buffer);
PRINTF ("state4\n");
strcpy (ws_status, "4");
PRINTF ("parse messge: %s\n", msg_buffer);
sscanf
( (const char*) msg_buffer
, "Q,%3d,%3d.%2d,%c,%2d"
@ -257,7 +279,7 @@ void loop (void)
ws_max_speed_lo = ws_max_speed % 100;
ws_max_speed_hi = ws_max_speed / 100;
printf ("speed: %d.%02d %s, dir: %d, status: %s, max: %d.%02d\n"
PRINTF ("speed: %d.%02d %s, dir: %d, status: %s, max: %d.%02d\n"
, ws_speed_hi, ws_speed_lo, ws_unit, ws_direction, ws_status
, ws_max_speed_hi, ws_max_speed_lo);

View File

@ -11,7 +11,18 @@ CONTIKI_WITH_IPV6 = 1
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
PROJECT_SOURCEFILES += res_upload_image.c res_bootloader.c ${SKETCH}.cpp
PROJECT_SOURCEFILES += ${SKETCH}.cpp
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
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) $(REST_RESOURCES_DIR_COMMON)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# variable for Makefile.include
ifneq ($(TARGET), minimal-net)
@ -33,7 +44,7 @@ SMALL=1
# REST Engine shall use Erbium CoAP implementation
APPS += er-coap
APPS += rest-engine
APPS += arduino json-resource json #time
APPS += arduino json-resource json ota-update
include $(CONTIKI)/Makefile.include
include $(CONTIKI)/apps/arduino/Makefile.include

View File

@ -101,8 +101,18 @@ that are not kept in the directory:
- ``/part_start``: This resource needs an additional query-parameter
indicating the partition number, e.g., ``/part_start?part=1`` and
returns the partition start address in flash.
- ``active_part``: The partition that is currently booted.
- ``/active_part``: The partition that is currently booted.
How to use in your own code
===========================
- add app ``ota-update`` (and possibly ``json`` and ``json-resource``)
to your Makefile
- add ``#include "ota-update.h"`` to your source file
- add ``OTA_ACTIVATE_RESOURCES();`` to your code to activate the
resources
All above described resources are prefixed with ``ota/``.
Security
========

View File

@ -1,8 +0,0 @@
extern resource_t res_upload_image;
extern resource_t res_part_count;
extern resource_t res_part_size;
extern resource_t res_boot_default;
extern resource_t res_boot_next;
extern resource_t res_active_part;
extern resource_t res_part_start;
extern resource_t res_part_ok;

View File

@ -1,9 +1,5 @@
/*
* Gardena 9V Magnet-Valve
* We have a CoAP Resource for the Valve, it can be in state 1 (on) and
* 0 (off).
* Transition on-off outputs a negative pulse
* Transition off-on outputs a positive pulse
* Simple example with ota-update only
*/
extern "C" {
@ -12,25 +8,17 @@ extern "C" {
#include "contiki-net.h"
#include "bootloader_if.h"
#include "er-coap.h"
#include "resources.h"
char resname[] = "update";
#include "ota-update.h"
}
void setup (void)
{
rest_init_engine ();
rest_activate_resource (&res_upload_image, resname);
rest_activate_resource (&res_part_count, (char *)"part_count");
rest_activate_resource (&res_part_size, (char *)"part_size");
rest_activate_resource (&res_boot_default, (char *)"boot_default");
rest_activate_resource (&res_boot_next, (char *)"boot_next");
rest_activate_resource (&res_active_part, (char *)"active_part");
rest_activate_resource (&res_part_start, (char *)"part_start");
rest_activate_resource (&res_part_ok, (char *)"part_ok");
OTA_ACTIVATE_RESOURCES();
}
void loop (void)
{
printf ("Hello %lu\n",bootloader_get_active_part());
printf ("just sitting round and waiting for ota update\n");
}

View File

@ -89,6 +89,11 @@ AUTOSTART_PROCESSES(&border_router_process,&webserver_nogui_process);
#define BUF_USES_STACK 1
#endif
#define WEBSERVER_CONF_COPPER_LINKS 1
#if WEBSERVER_CONF_COPPER_LINKS
#define BUF_USES_STACK 1
#endif
PROCESS(webserver_nogui_process, "Web server");
PROCESS_THREAD(webserver_nogui_process, ev, data)
{
@ -247,11 +252,39 @@ PT_THREAD(generate_routes(struct httpd_state *s))
ADD("/%u (via ", r->length);
ipaddr_add(uip_ds6_route_nexthop(r));
if(1 || (r->state.lifetime < 600)) {
ADD(") %lus\n", (unsigned long)r->state.lifetime);
ADD(") %lus", (unsigned long)r->state.lifetime);
} else {
ADD(")\n");
ADD(")");
}
SEND_STRING(&s->sout, buf);
#if BUF_USES_STACK
bufptr = buf; bufend = bufptr + sizeof(buf);
#else
blen = 0;
#endif
#if BUF_USES_STACK
#if WEBSERVER_CONF_COPPER_LINKS
ADD(" -&gt; <a href=\"coap://[");
ipaddr_add(&r->ipaddr);
ADD("]\" target=\"_new\">copper");
ADD("</a>");
#endif
#else
#if WEBSERVER_CONF_COPPER_LINKS
ADD(" -&gt; <a href=http://[");
ipaddr_add(&r->ipaddr);
ADD("]\" target=\"_new\">copper");
SEND_STRING(&s->sout, buf); //TODO: why tunslip6 needs an output here, wpcapslip does not
blen = 0;
ADD("</a>");
#endif
#endif
ADD("\n");
SEND_STRING(&s->sout, buf);
#if BUF_USES_STACK
bufptr = buf; bufend = bufptr + sizeof(buf);
#else
@ -267,7 +300,7 @@ PT_THREAD(generate_routes(struct httpd_state *s))
#if WEBSERVER_CONF_LOADTIME
numticks = clock_time() - numticks + 1;
ADD(" <i>(%u.%02u sec)</i>",numticks/CLOCK_SECOND,(100*(numticks%CLOCK_SECOND))/CLOCK_SECOND));
ADD(" <i>(%u.%02u sec)</i>",numticks/CLOCK_SECOND,(100*(numticks%CLOCK_SECOND))/CLOCK_SECOND);
#endif
SEND_STRING(&s->sout, buf);

View File

@ -315,6 +315,25 @@ typedef unsigned short uip_stats_t;
#define CC_CONF_INLINE inline
#endif
/* mpr: avr-gcc 5.4.0 on gentoo needs this */
#ifndef DDRE5
#define DDRE5 DDE5
#endif
/* mpr: next is for having rpl mesh-networking working */
#if 1
//#define RPL_CONF_DEFAULT_LIFETIME 5
#define RPL_CONF_WITH_MC 1
//#define RPL_CONF_WITH_DAO_ACK 1
//#define RPL_CONF_RPL_REPAIR_ON_DAO_NACK 1
#define RPL_CONF_DIO_REFRESH_DAO_ROUTES 1
#define RPL_CONF_WITH_PROBING 1
#define RPL_MRHOF_CONF_SQUARED_ETX 1
#define RPL_CONF_DAG_MC RPL_DAG_MC_ETX
#define LINK_STATS_CONF_INIT_ETX(stats) guess_etx_from_rssi(stats)
#endif
/* include the project config */
/* PROJECT_CONF_H might be defined in the project Makefile */
#ifdef PROJECT_CONF_H