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

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,260 +0,0 @@
/*
* Copyright (c) 2015, Ralf Schlatterbeck 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
* Bootloader ressources
* \author
* Ralf Schlatterbeck <rsc@runtux.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "contiki.h"
#include "contiki-net.h"
#include "rest-engine.h"
#include "er-coap-engine.h"
#include "uiplib.h"
#include "generic_resource.h"
#include "bootloader_if.h"
#include "Arduino.h"
/*
* Resources to be activated need to be imported through the extern keyword.
* The build system automatically compiles the resources in the
* corresponding sub-directory.
*/
static size_t
part_count
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
return snprintf (buf, bsize, "%ld", bootloader_get_part_count ());
}
GENERIC_RESOURCE
( part_count
, Partition Count
, count
, 0
, NULL
, part_count
);
static size_t
part_size
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
return snprintf (buf, bsize, "%ld", bootloader_get_part_size ());
}
GENERIC_RESOURCE
( part_size
, Partition Size
, count
, 0
, NULL
, part_size
);
static size_t
get_boot_default
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
return snprintf (buf, bsize, "%ld", bootloader_get_boot_default ());
}
static int
set_boot_default
(const char *name, const char *uri, const char *query, const char *s)
{
uint32_t tmp = strtoul (s, NULL, 10);
bootloader_set_boot_default (tmp);
return 0;
}
GENERIC_RESOURCE
( boot_default
, Default boot partition
, count
, 0
, set_boot_default
, get_boot_default
);
static size_t
get_boot_next
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
return snprintf (buf, bsize, "%ld", bootloader_get_boot_next ());
}
static int
set_boot_next
(const char *name, const char *uri, const char *query, const char *s)
{
uint32_t tmp = strtoul (s, NULL, 10);
bootloader_set_boot_next (tmp);
return 0;
}
GENERIC_RESOURCE
( boot_next
, Next boot partition
, count
, 0
, set_boot_next
, get_boot_next
);
static size_t
get_active_part
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
return snprintf (buf, bsize, "%ld", bootloader_get_active_part ());
}
GENERIC_RESOURCE
( active_part
, Currently active partition
, count
, 0
, NULL
, get_active_part
);
/*
* Parse query info. We insist that the query starts with 'part='
* Then we parse the integer following the part= string and return the
* number. The number is always positive, if something goes wrong we
* return a negative number.
*/
static int get_query_partition (const char *query)
{
if (strncmp (query, "part=", 5)) {
return -1;
}
return strtoul (query + 5, NULL, 10);
}
static size_t
get_part_start
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
int idx = get_query_partition (query);
if (idx < 0) {
return snprintf (buf, bsize, "Invalid: \"%s\" use part=N query", query);
}
return snprintf (buf, bsize, "%ld", bootloader_get_part_start (idx));
}
GENERIC_RESOURCE
( part_start
, Start of partition
, count
, 0
, NULL
, get_part_start
);
static int
set_part_ok
(const char *name, const char *uri, const char *query, const char *s)
{
uint32_t tmp = strtoul (s, NULL, 10);
int idx = get_query_partition (query);
if (idx < 0) {
return -1;
}
if (tmp) {
bootloader_set_part_ok (idx);
} else {
bootloader_clr_part_ok (idx);
}
return 0;
}
static size_t
get_part_ok
( const char *name
, const char *uri
, const char *query
, char *buf
, size_t bsize
)
{
int idx = get_query_partition (query);
if (idx < 0) {
return snprintf (buf, bsize, "Invalid: \"%s\" use part=N query", query);
}
return snprintf (buf, bsize, "%ld", bootloader_get_part_ok (idx));
}
GENERIC_RESOURCE
( part_ok
, Set/Clear Partition OK flag
, count
, 0
, set_part_ok
, get_part_ok
);

View file

@ -1,243 +0,0 @@
/*
* Copyright (C) 2017, Marcus Priesch, Ralf Schlatterbeck
* with code from the res-plugtest-large-update.c by
* 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
* Over-the-air update using blockwise transfer
* \author
* Marcus Priesch <marcus@priesch.co.at>
* Ralf Schlatterbeck <rsc@runtux.com>
*/
#include <string.h>
#include "sys/cc.h"
#include "rest-engine.h"
#include "er-coap.h"
#include "contiki.h"
#include "contiki-net.h"
#include "er-coap.h"
#include "Arduino.h"
#include <avr/interrupt.h>
#include "bootloader_if.h"
#if 1
#include <stdio.h>
#define PRINTF(x) printf x
#else
#define PRINTF(x)
#endif
// We allocate this statically, otherwise we cannot flash a new image
// when ram is exhausted!
static uint8_t current_page [256];
static uint32_t current_offset = 0;
#define PAGESIZE (sizeof (current_page))
/*
* Note that the current code relies on the fact that the bootloader
* used only supports two images. This may change in the future. We
* mainly need to relax some of the checks and use a different algorithm
* for computing imgidx, the index of the partition to be overwritten.
* If the bootloader supports more than two partitions at some point we
* may want the uploader to explicitly define the partition to be used.
*/
static void
res_put_handler
( void *request
, void *response
, uint8_t *buffer
, uint16_t preferred_size
, int32_t *offset
)
{
coap_packet_t *const packet = (coap_packet_t *)request;
uint8_t *in_data = NULL;
size_t len = 0;
uint32_t partition_start = 0;
const uint32_t partition_size = bootloader_get_part_size ();
uint32_t imgidx = 0;
unsigned int ct = -1;
/* If the currently-booted partition is not the default partition we
* do not allow overwriting a partition: Neither the currently-booted
* one (this would crash) nor the only partition that is marked
* bootable. We also insist that boot_next == boot_default.
*/
if (bootloader_get_boot_default () != bootloader_get_boot_next ()) {
REST.set_response_status (response, REST.status.BAD_REQUEST);
const char *error_msg = "Won't overwrite boot_next";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
if (bootloader_get_boot_default () != bootloader_get_active_part ()) {
REST.set_response_status (response, REST.status.BAD_REQUEST);
const char *error_msg = "Won't overwrite current";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
imgidx = !bootloader_get_active_part ();
partition_start = bootloader_get_part_start (imgidx);
REST.get_header_content_type (request, &ct);
/* Require content_type APPLICATION_OCTET_STREAM */
if (ct != REST.type.APPLICATION_OCTET_STREAM) {
REST.set_response_status (response, REST.status.BAD_REQUEST);
const char *error_msg = "ContentType";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
len = REST.get_request_payload (request, (const uint8_t **)&in_data);
PRINTF (("cur: %lu len: %lu, offset: %lu\n",
(uint32_t)current_offset, (uint32_t)len, (uint32_t)*offset));
PRINTF (("b1-offs: %lu, b1-size: %u, b1-num: %lu b1-more: %d b1-size1: %lu\n",
packet->block1_offset, packet->block1_size, packet->block1_num,
packet->block1_more, packet->size1));
if (len == 0 || NULL == in_data) {
REST.set_response_status (response, REST.status.BAD_REQUEST);
const char *error_msg = "NoPayload";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
/* if the block1_offset is 0 a new transmission has started */
if (!packet->block1_offset) {
current_offset = 0;
}
if (packet->block1_offset > current_offset) {
REST.set_response_status (response, REST.status.REQUEST_ENTITY_INCOMPLETE);
const char *error_msg = "OutOfSequence";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
/* Old packet or retransmission, immediately confirm */
if (packet->block1_offset && packet->block1_offset + len <= current_offset) {
REST.set_response_status (response, REST.status.CHANGED);
coap_set_header_block1
(response, packet->block1_num, 0, packet->block1_size);
return;
}
// FIXME: blocksize may be larger than our flash page size
if (len > PAGESIZE) {
REST.set_response_status (response, REST.status.INTERNAL_SERVER_ERROR);
const char *error_msg = "GRMPF: PageSize";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
// FIXME: blocksize may be larger than our flash page size
// So we should handle this case and repeatedly flash a block until the
// received data is written.
if (current_offset % PAGESIZE + len > PAGESIZE) {
REST.set_response_status (response, REST.status.INTERNAL_SERVER_ERROR);
const char *error_msg = "GRMPF: blocksize";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
// Should never happen, we test for < and > earlier.
if (packet->block1_offset != current_offset) {
REST.set_response_status (response, REST.status.INTERNAL_SERVER_ERROR);
const char *error_msg = "GRMPF: Offset";
REST.set_response_payload (response, error_msg, strlen (error_msg));
return;
}
if (packet->block1_offset + len > partition_size) {
REST.set_response_status
(response, REST.status.REQUEST_ENTITY_TOO_LARGE);
REST.set_response_payload
(response, buffer, sprintf ((char *)buffer, "%luB max.", partition_size));
return;
}
memcpy (current_page + current_offset % PAGESIZE, in_data, len);
/* Whenever an upload is started for a partition mark it as not ok */
if (current_offset == 0) {
PRINTF (("Clear partition_ok: %ld\n", imgidx));
bootloader_clr_part_ok (imgidx);
}
current_offset += len;
if (current_offset % PAGESIZE == 0) {
uint32_t dst_address = partition_start + current_offset - PAGESIZE;
/* Special case: Flash irq vectors to backup position */
if (current_offset - PAGESIZE < PART_IRQVEC_SIZE) {
/* Only for images not at position 0 write first PART_IRQVEC_SIZE
* bytes also to original position. For partition 0 it will be
* copied there anyway *and* we would crash if we wrote to the
* active memory!
*/
if (partition_start != 0) {
PRINTF (("Flashing: %lx to %lx\n", (uint32_t)PAGESIZE, dst_address));
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
}
/* Note: The partition_size returned by the bootloader does *NOT*
* include the PART_IRQVEC_SIZE
*/
dst_address = partition_start + partition_size
+ current_offset - PAGESIZE;
}
PRINTF (("Flashing: %lx to %lx\n", (uint32_t)PAGESIZE, dst_address));
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
} else if (!packet->block1_more) {
uint32_t dst_address =
partition_start + (current_offset / PAGESIZE) * PAGESIZE;
PRINTF (("Flashing: last %lx to %lx\n", (uint32_t)PAGESIZE, dst_address));
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
}
if (!packet->block1_more) {
// we are finished
bootloader_set_boot_next (imgidx);
current_offset = 0;
}
REST.set_response_status (response, REST.status.CHANGED);
coap_set_header_block1 (response, packet->block1_num, 0, packet->block1_size);
}
RESOURCE(
res_upload_image
, "title=\"Flash memory upgrade\";rt=\"block\""
, NULL
, NULL
, res_put_handler
, NULL
);

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);