From 86596ac92f79b75ff00dde5ebb252a34c876be4b Mon Sep 17 00:00:00 2001 From: Marcus Priesch Date: Thu, 5 Oct 2017 16:53:26 +0200 Subject: [PATCH] moved to apps folder --- apps/ota-update/Makefile.ota-update | 1 + apps/ota-update/ota-update.h | 22 ++++ .../osd => apps}/ota-update/res_bootloader.c | 0 apps/ota-update/res_reboot.c | 108 ++++++++++++++++++ .../ota-update/res_upload_image.c | 0 examples/osd/ota-update/Makefile | 15 ++- examples/osd/ota-update/README.rst | 8 +- examples/osd/ota-update/resources.h | 8 -- examples/osd/ota-update/sketch.pde | 24 +--- 9 files changed, 155 insertions(+), 31 deletions(-) create mode 100644 apps/ota-update/Makefile.ota-update create mode 100644 apps/ota-update/ota-update.h rename {examples/osd => apps}/ota-update/res_bootloader.c (100%) create mode 100644 apps/ota-update/res_reboot.c rename {examples/osd => apps}/ota-update/res_upload_image.c (100%) delete mode 100644 examples/osd/ota-update/resources.h diff --git a/apps/ota-update/Makefile.ota-update b/apps/ota-update/Makefile.ota-update new file mode 100644 index 000000000..061395baf --- /dev/null +++ b/apps/ota-update/Makefile.ota-update @@ -0,0 +1 @@ +ota-update_src = res_bootloader.c res_reboot.c res_upload_image.c diff --git a/apps/ota-update/ota-update.h b/apps/ota-update/ota-update.h new file mode 100644 index 000000000..584a7fc07 --- /dev/null +++ b/apps/ota-update/ota-update.h @@ -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"); + diff --git a/examples/osd/ota-update/res_bootloader.c b/apps/ota-update/res_bootloader.c similarity index 100% rename from examples/osd/ota-update/res_bootloader.c rename to apps/ota-update/res_bootloader.c diff --git a/apps/ota-update/res_reboot.c b/apps/ota-update/res_reboot.c new file mode 100644 index 000000000..f7c6b69d4 --- /dev/null +++ b/apps/ota-update/res_reboot.c @@ -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 + */ + +#include +#include +#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 + ); + diff --git a/examples/osd/ota-update/res_upload_image.c b/apps/ota-update/res_upload_image.c similarity index 100% rename from examples/osd/ota-update/res_upload_image.c rename to apps/ota-update/res_upload_image.c diff --git a/examples/osd/ota-update/Makefile b/examples/osd/ota-update/Makefile index 4554248a0..7015ca0f6 100644 --- a/examples/osd/ota-update/Makefile +++ b/examples/osd/ota-update/Makefile @@ -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 diff --git a/examples/osd/ota-update/README.rst b/examples/osd/ota-update/README.rst index 0439a8e5a..0697b7a65 100644 --- a/examples/osd/ota-update/README.rst +++ b/examples/osd/ota-update/README.rst @@ -101,8 +101,14 @@ 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" to the Makefile +- add #include "ota-update.h" +- add OTA_ACTIVATE_RESOURCES() to your code to activate the resources Security ======== diff --git a/examples/osd/ota-update/resources.h b/examples/osd/ota-update/resources.h deleted file mode 100644 index 0477252f0..000000000 --- a/examples/osd/ota-update/resources.h +++ /dev/null @@ -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; diff --git a/examples/osd/ota-update/sketch.pde b/examples/osd/ota-update/sketch.pde index cfc78d356..7990fef6b 100644 --- a/examples/osd/ota-update/sketch.pde +++ b/examples/osd/ota-update/sketch.pde @@ -1,35 +1,19 @@ /* - * 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" { -#include -#include "contiki.h" -#include "contiki-net.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\n"); + printf ("just sitting round and waiting for ota update\n"); }