From aa5cecfca1767d9fbab563c3d810b43ff00a5a3e Mon Sep 17 00:00:00 2001 From: Olivier Date: Tue, 3 Jan 2017 20:19:00 +0100 Subject: [PATCH] OTA: Add incoming FW block check (#718) --- core/MyIndication.h | 1 + core/MyOTAFirmwareUpdate.cpp | 17 ++++++++---- core/MyOTAFirmwareUpdate.h | 1 + drivers/SPIFlash/SPIFlash.cpp | 6 ++--- .../ota_firmware_update_nrf24.ino | 26 +++++++++++++++++++ 5 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 tests/Arduino/sketches/ota_firmware_update_nrf24/ota_firmware_update_nrf24.ino diff --git a/core/MyIndication.h b/core/MyIndication.h index 0e2f8ea2..7d6b7bac 100644 --- a/core/MyIndication.h +++ b/core/MyIndication.h @@ -44,6 +44,7 @@ typedef enum { INDICATION_WAKEUP, //!< Node just woke from sleep. INDICATION_FW_UPDATE_START, //!< Start of OTA firmware update process. INDICATION_FW_UPDATE_RX, //!< Received a piece of firmware data. + INDICATION_FW_UPDATE_RX_ERR, //!< Received wrong piece of firmware data. INDICATION_ERR_START = 100, INDICATION_ERR_TX, //!< Failed to transmit message. diff --git a/core/MyOTAFirmwareUpdate.cpp b/core/MyOTAFirmwareUpdate.cpp index 1757c6b8..2ac5f210 100644 --- a/core/MyOTAFirmwareUpdate.cpp +++ b/core/MyOTAFirmwareUpdate.cpp @@ -93,19 +93,26 @@ bool firmwareOTAUpdateProcess(void) OTA_DEBUG(PSTR("OTA:FWP:UPDATE SKIPPED\n")); // FW update skipped, no newer version available } else if (_msg.type == ST_FIRMWARE_RESPONSE) { if (_firmwareUpdateOngoing) { - // Save block to flash - setIndication(INDICATION_FW_UPDATE_RX); - OTA_DEBUG(PSTR("OTA:FWP:RECV B=%04X\n"), _firmwareBlock); // received FW block // extract FW block replyFirmwareBlock_t *firmwareResponse = (replyFirmwareBlock_t *)_msg.data; - // write to flash + + OTA_DEBUG(PSTR("OTA:FWP:RECV B=%04X\n"), firmwareResponse->block); // received FW block + if (firmwareResponse->block != _firmwareBlock - 1) { + OTA_DEBUG(PSTR("!OTA:FWP:WRONG FWB\n")); // received FW block + // wrong firmware block received + setIndication(INDICATION_FW_UPDATE_RX_ERR); + // no further processing required + return true; + } + setIndication(INDICATION_FW_UPDATE_RX); + // Save block to flash _flash.writeBytes( ((_firmwareBlock - 1) * FIRMWARE_BLOCK_SIZE) + FIRMWARE_START_OFFSET, firmwareResponse->data, FIRMWARE_BLOCK_SIZE); // wait until flash written while (_flash.busy()) {} _firmwareBlock--; if (!_firmwareBlock) { - // We're finished! Do a checksum and reboot. + // We're done! Do a checksum and reboot. OTA_DEBUG(PSTR("OTA:FWP:FW END\n")); // received FW block _firmwareUpdateOngoing = false; if (transportIsValidFirmware()) { diff --git a/core/MyOTAFirmwareUpdate.h b/core/MyOTAFirmwareUpdate.h index 9372fd2c..cb177571 100644 --- a/core/MyOTAFirmwareUpdate.h +++ b/core/MyOTAFirmwareUpdate.h @@ -40,6 +40,7 @@ * |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash * | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available * | | OTA | FWP | RECV B=%04X | Received FW block (B) +* |!| OTA | FWP | WRONG FWB | Wrong FW block received * | | OTA | FWP | FW END | FW received, proceed to CRC verification * | | OTA | FWP | CRC OK | FW CRC verification OK * |!| OTA | FWP | CRC FAIL | FW CRC verification failed diff --git a/drivers/SPIFlash/SPIFlash.cpp b/drivers/SPIFlash/SPIFlash.cpp index cd3d8738..f1427667 100644 --- a/drivers/SPIFlash/SPIFlash.cpp +++ b/drivers/SPIFlash/SPIFlash.cpp @@ -60,7 +60,7 @@ void SPIFlash::select() #ifndef SPI_HAS_TRANSACTION noInterrupts(); #endif -#ifndef ESP8266 +#if defined(SPCR) && defined(SPSR) _SPCR = SPCR; _SPSR = SPSR; #endif @@ -88,7 +88,7 @@ void SPIFlash::unselect() #else interrupts(); #endif -#ifndef ESP8266 +#if defined(SPCR) && defined(SPSR) SPCR = _SPCR; SPSR = _SPSR; #endif @@ -97,7 +97,7 @@ void SPIFlash::unselect() /// setup SPI, read device ID etc... boolean SPIFlash::initialize() { -#ifndef ESP8266 +#if defined(SPCR) && defined(SPSR) _SPCR = SPCR; _SPSR = SPSR; #endif diff --git a/tests/Arduino/sketches/ota_firmware_update_nrf24/ota_firmware_update_nrf24.ino b/tests/Arduino/sketches/ota_firmware_update_nrf24/ota_firmware_update_nrf24.ino new file mode 100644 index 00000000..038cb37e --- /dev/null +++ b/tests/Arduino/sketches/ota_firmware_update_nrf24/ota_firmware_update_nrf24.ino @@ -0,0 +1,26 @@ +/* + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2015 Sensnology AB + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + ******************************* + */ +#include +#include +#define MY_DEBUG +#define MY_RADIO_NRF24 +#define MY_OTA_FIRMWARE_FEATURE +#include