|
| 1 | +/* |
| 2 | + * The MySensors Arduino library handles the wireless radio link and protocol |
| 3 | + * between your home built sensors/actuators and HA controller of choice. |
| 4 | + * The sensors forms a self healing radio network with optional repeaters. Each |
| 5 | + * repeater and gateway builds a routing tables in EEPROM which keeps track of the |
| 6 | + * network topology allowing messages to be routed to nodes. |
| 7 | + * |
| 8 | + * Created by Henrik Ekblad <[email protected]> |
| 9 | + * Copyright (C) 2013-2016 Sensnology AB |
| 10 | + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors |
| 11 | + * |
| 12 | + * Documentation: http://www.mysensors.org |
| 13 | + * Support Forum: http://forum.mysensors.org |
| 14 | + * |
| 15 | + * This program is free software; you can redistribute it and/or |
| 16 | + * modify it under the terms of the GNU General Public License |
| 17 | + * version 2 as published by the Free Software Foundation. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "MyOTAFirmwareUpdate.h" |
| 21 | + |
| 22 | +SPIFlash _flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID); |
| 23 | +NodeFirmwareConfig _fc; |
| 24 | +bool _fwUpdateOngoing; |
| 25 | +unsigned long _fwLastRequestTime; |
| 26 | +uint16_t _fwBlock; |
| 27 | +uint8_t _fwRetry; |
| 28 | + |
| 29 | +inline void readFirmwareSettings() { |
| 30 | + hwReadConfigBlock((void*)&_fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(NodeFirmwareConfig)); |
| 31 | +} |
| 32 | + |
| 33 | +inline void firmwareOTAUpdateRequest() { |
| 34 | + unsigned long enter = hwMillis(); |
| 35 | + if (_fwUpdateOngoing && (enter - _fwLastRequestTime > MY_OTA_RETRY_DELAY)) { |
| 36 | + if (!_fwRetry) { |
| 37 | + debug(PSTR("fw upd fail\n")); |
| 38 | + // Give up. We have requested MY_OTA_RETRY times without any packet in return. |
| 39 | + _fwUpdateOngoing = false; |
| 40 | + ledBlinkErr(1); |
| 41 | + return; |
| 42 | + } |
| 43 | + _fwRetry--; |
| 44 | + _fwLastRequestTime = enter; |
| 45 | + // Time to (re-)request firmware block from controller |
| 46 | + RequestFWBlock firmwareRequest; |
| 47 | + firmwareRequest.type = _fc.type; |
| 48 | + firmwareRequest.version = _fc.version; |
| 49 | + firmwareRequest.block = (_fwBlock - 1); |
| 50 | + debug(PSTR("req FW: T=%02X, V=%02X, B=%04X\n"),_fc.type,_fc.version,_fwBlock - 1); |
| 51 | + _sendRoute(build(_msgTmp, _nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, ST_FIRMWARE_REQUEST, false).set(&firmwareRequest,sizeof(RequestFWBlock))); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +inline bool firmwareOTAUpdateProcess() { |
| 56 | + if (_msg.type == ST_FIRMWARE_CONFIG_RESPONSE) { |
| 57 | + NodeFirmwareConfig *firmwareConfigResponse = (NodeFirmwareConfig *)_msg.data; |
| 58 | + // compare with current node configuration, if they differ, start fw fetch process |
| 59 | + if (memcmp(&_fc,firmwareConfigResponse,sizeof(NodeFirmwareConfig))) { |
| 60 | + debug(PSTR("fw update\n")); |
| 61 | + // copy new FW config |
| 62 | + memcpy(&_fc,firmwareConfigResponse,sizeof(NodeFirmwareConfig)); |
| 63 | + // Init flash |
| 64 | + if (!_flash.initialize()) { |
| 65 | + debug(PSTR("flash init fail\n")); |
| 66 | + _fwUpdateOngoing = false; |
| 67 | + } else { |
| 68 | + // erase lower 32K -> max flash size for ATMEGA328 |
| 69 | + _flash.blockErase32K(0); |
| 70 | + // wait until flash erased |
| 71 | + while ( _flash.busy() ); |
| 72 | + _fwBlock = _fc.blocks; |
| 73 | + _fwUpdateOngoing = true; |
| 74 | + // reset flags |
| 75 | + _fwRetry = MY_OTA_RETRY+1; |
| 76 | + _fwLastRequestTime = 0; |
| 77 | + } |
| 78 | + return true; |
| 79 | + } |
| 80 | + debug(PSTR("fw update skipped\n")); |
| 81 | + } else if (_msg.type == ST_FIRMWARE_RESPONSE) { |
| 82 | + if (_fwUpdateOngoing) { |
| 83 | + // Save block to flash |
| 84 | + debug(PSTR("fw block %d\n"), _fwBlock); |
| 85 | + // extract FW block |
| 86 | + ReplyFWBlock *firmwareResponse = (ReplyFWBlock *)_msg.data; |
| 87 | + // write to flash |
| 88 | + _flash.writeBytes( ((_fwBlock - 1) * FIRMWARE_BLOCK_SIZE) + FIRMWARE_START_OFFSET, firmwareResponse->data, FIRMWARE_BLOCK_SIZE); |
| 89 | + // wait until flash written |
| 90 | + while ( _flash.busy() ); |
| 91 | + _fwBlock--; |
| 92 | + if (!_fwBlock) { |
| 93 | + // We're finished! Do a checksum and reboot. |
| 94 | + _fwUpdateOngoing = false; |
| 95 | + if (transportIsValidFirmware()) { |
| 96 | + debug(PSTR("fw checksum ok\n")); |
| 97 | + // All seems ok, write size and signature to flash (DualOptiboot will pick this up and flash it) |
| 98 | + uint16_t fwsize = FIRMWARE_BLOCK_SIZE * _fc.blocks; |
| 99 | + uint8_t OTAbuffer[10] = {'F','L','X','I','M','G',':',(uint8_t)(fwsize >> 8),(uint8_t)(fwsize & 0xff),':'}; |
| 100 | + _flash.writeBytes(0, OTAbuffer, 10); |
| 101 | + // Write the new firmware config to eeprom |
| 102 | + hwWriteConfigBlock((void*)&_fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(NodeFirmwareConfig)); |
| 103 | + hwReboot(); |
| 104 | + } else { |
| 105 | + debug(PSTR("fw checksum fail\n")); |
| 106 | + } |
| 107 | + } |
| 108 | + // reset flags |
| 109 | + _fwRetry = MY_OTA_RETRY+1; |
| 110 | + _fwLastRequestTime = 0; |
| 111 | + } else { |
| 112 | + debug(PSTR("No fw update ongoing\n")); |
| 113 | + } |
| 114 | + return true; |
| 115 | + } |
| 116 | + return false; |
| 117 | +} |
| 118 | + |
| 119 | +inline void presentBootloaderInformation(){ |
| 120 | + RequestFirmwareConfig *reqFWConfig = (RequestFirmwareConfig *)_msgTmp.data; |
| 121 | + mSetLength(_msgTmp, sizeof(RequestFirmwareConfig)); |
| 122 | + mSetCommand(_msgTmp, C_STREAM); |
| 123 | + mSetPayloadType(_msgTmp,P_CUSTOM); |
| 124 | + // copy node settings to reqFWConfig |
| 125 | + memcpy(reqFWConfig,&_fc,sizeof(NodeFirmwareConfig)); |
| 126 | + // add bootloader information |
| 127 | + reqFWConfig->BLVersion = MY_OTA_BOOTLOADER_VERSION; |
| 128 | + _fwUpdateOngoing = false; |
| 129 | + _sendRoute(build(_msgTmp, _nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, ST_FIRMWARE_CONFIG_REQUEST, false)); |
| 130 | +} |
| 131 | +// do a crc16 on the whole received firmware |
| 132 | +inline bool transportIsValidFirmware() { |
| 133 | + // init crc |
| 134 | + uint16_t crc = ~0; |
| 135 | + for (uint16_t i = 0; i < _fc.blocks * FIRMWARE_BLOCK_SIZE; ++i) { |
| 136 | + crc ^= _flash.readByte(i + FIRMWARE_START_OFFSET); |
| 137 | + for (int8_t j = 0; j < 8; ++j) { |
| 138 | + if (crc & 1) |
| 139 | + crc = (crc >> 1) ^ 0xA001; |
| 140 | + else |
| 141 | + crc = (crc >> 1); |
| 142 | + } |
| 143 | + } |
| 144 | + return crc == _fc.crc; |
| 145 | +} |
0 commit comments