Merge development & additional optimizations
This commit is contained in:
@@ -38,6 +38,8 @@
|
||||
#define MY_CAP_RADIO "N"
|
||||
#elif defined(MY_RADIO_RFM69)
|
||||
#define MY_CAP_RADIO "R"
|
||||
#elif defined(MY_RADIO_RFM95)
|
||||
#define MY_CAP_RADIO "L"
|
||||
#elif defined(MY_RS485)
|
||||
#define MY_CAP_RADIO "S"
|
||||
#else
|
||||
|
||||
@@ -25,90 +25,95 @@ extern MyMessage _msgTmp;
|
||||
|
||||
// local variables
|
||||
SPIFlash _flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID);
|
||||
NodeFirmwareConfig _fc;
|
||||
bool _fwUpdateOngoing;
|
||||
unsigned long _fwLastRequestTime;
|
||||
uint16_t _fwBlock;
|
||||
uint8_t _fwRetry;
|
||||
nodeFirmwareConfig_t _nodeFirmwareConfig;
|
||||
bool _firmwareUpdateOngoing;
|
||||
uint32_t _firmwareLastRequest;
|
||||
uint16_t _firmwareBlock;
|
||||
uint8_t _firmwareRetry;
|
||||
|
||||
void readFirmwareSettings() {
|
||||
hwReadConfigBlock((void*)&_fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(NodeFirmwareConfig));
|
||||
void readFirmwareSettings(void)
|
||||
{
|
||||
hwReadConfigBlock((void*)&_nodeFirmwareConfig, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(nodeFirmwareConfig_t));
|
||||
}
|
||||
|
||||
void firmwareOTAUpdateRequest() {
|
||||
unsigned long enter = hwMillis();
|
||||
if (_fwUpdateOngoing && (enter - _fwLastRequestTime > MY_OTA_RETRY_DELAY)) {
|
||||
if (!_fwRetry) {
|
||||
void firmwareOTAUpdateRequest(void)
|
||||
{
|
||||
const uint32_t enterMS = hwMillis();
|
||||
if (_firmwareUpdateOngoing && (enterMS - _firmwareLastRequest > MY_OTA_RETRY_DELAY)) {
|
||||
if (!_firmwareRetry) {
|
||||
setIndication(INDICATION_ERR_FW_TIMEOUT);
|
||||
OTA_DEBUG(PSTR("!OTA:FRQ:FW UPD FAIL\n")); // fw update failed
|
||||
// Give up. We have requested MY_OTA_RETRY times without any packet in return.
|
||||
_fwUpdateOngoing = false;
|
||||
_firmwareUpdateOngoing = false;
|
||||
return;
|
||||
}
|
||||
_fwRetry--;
|
||||
_fwLastRequestTime = enter;
|
||||
_firmwareRetry--;
|
||||
_firmwareLastRequest = enterMS;
|
||||
// Time to (re-)request firmware block from controller
|
||||
RequestFWBlock firmwareRequest;
|
||||
firmwareRequest.type = _fc.type;
|
||||
firmwareRequest.version = _fc.version;
|
||||
firmwareRequest.block = (_fwBlock - 1);
|
||||
OTA_DEBUG(PSTR("OTA:FRQ:FW REQ,T=%04X,V=%04X,B=%04X\n"),_fc.type,_fc.version,_fwBlock - 1); // request FW update block
|
||||
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, ST_FIRMWARE_REQUEST, false).set(&firmwareRequest,sizeof(RequestFWBlock)));
|
||||
requestFirmwareBlock_t firmwareRequest;
|
||||
firmwareRequest.type = _nodeFirmwareConfig.type;
|
||||
firmwareRequest.version = _nodeFirmwareConfig.version;
|
||||
firmwareRequest.block = (_firmwareBlock - 1);
|
||||
OTA_DEBUG(PSTR("OTA:FRQ:FW REQ,T=%04X,V=%04X,B=%04X\n"), _nodeFirmwareConfig.type, _nodeFirmwareConfig.version, _firmwareBlock - 1); // request FW update block
|
||||
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, ST_FIRMWARE_REQUEST, false).set(&firmwareRequest, sizeof(requestFirmwareBlock_t)));
|
||||
}
|
||||
}
|
||||
|
||||
bool firmwareOTAUpdateProcess() {
|
||||
bool firmwareOTAUpdateProcess(void)
|
||||
{
|
||||
if (_msg.type == ST_FIRMWARE_CONFIG_RESPONSE) {
|
||||
NodeFirmwareConfig *firmwareConfigResponse = (NodeFirmwareConfig *)_msg.data;
|
||||
// compare with current node configuration, if they differ, start fw fetch process
|
||||
if (memcmp(&_fc,firmwareConfigResponse,sizeof(NodeFirmwareConfig))) {
|
||||
nodeFirmwareConfig_t *firmwareConfigResponse = (nodeFirmwareConfig_t *)_msg.data;
|
||||
// compare with current node configuration, if they differ, start FW fetch process
|
||||
if (memcmp(&_nodeFirmwareConfig, firmwareConfigResponse, sizeof(nodeFirmwareConfig_t))) {
|
||||
setIndication(INDICATION_FW_UPDATE_START);
|
||||
OTA_DEBUG(PSTR("OTA:FWP:UPDATE\n")); // FW update initiated
|
||||
// copy new FW config
|
||||
memcpy(&_fc,firmwareConfigResponse,sizeof(NodeFirmwareConfig));
|
||||
(void)memcpy(&_nodeFirmwareConfig, firmwareConfigResponse, sizeof(nodeFirmwareConfig_t));
|
||||
// Init flash
|
||||
if (!_flash.initialize()) {
|
||||
setIndication(INDICATION_ERR_FW_FLASH_INIT);
|
||||
OTA_DEBUG(PSTR("!OTA:FWP:FLASH INIT FAIL\n")); // failed to initialise flash
|
||||
_fwUpdateOngoing = false;
|
||||
_firmwareUpdateOngoing = false;
|
||||
} else {
|
||||
// erase lower 32K -> max flash size for ATMEGA328
|
||||
_flash.blockErase32K(0);
|
||||
// wait until flash erased
|
||||
while ( _flash.busy() ) {}
|
||||
_fwBlock = _fc.blocks;
|
||||
_fwUpdateOngoing = true;
|
||||
_firmwareBlock = _nodeFirmwareConfig.blocks;
|
||||
_firmwareUpdateOngoing = true;
|
||||
// reset flags
|
||||
_fwRetry = MY_OTA_RETRY+1;
|
||||
_fwLastRequestTime = 0;
|
||||
_firmwareRetry = MY_OTA_RETRY + 1;
|
||||
_firmwareLastRequest = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
OTA_DEBUG(PSTR("OTA:FWP:UPDATE SKIPPED\n")); // FW update skipped, no newer version available
|
||||
} else if (_msg.type == ST_FIRMWARE_RESPONSE) {
|
||||
if (_fwUpdateOngoing) {
|
||||
if (_firmwareUpdateOngoing) {
|
||||
// Save block to flash
|
||||
setIndication(INDICATION_FW_UPDATE_RX);
|
||||
OTA_DEBUG(PSTR("OTA:FWP:RECV B=%04X\n"), _fwBlock); // received FW block
|
||||
OTA_DEBUG(PSTR("OTA:FWP:RECV B=%04X\n"), _firmwareBlock); // received FW block
|
||||
// extract FW block
|
||||
ReplyFWBlock *firmwareResponse = (ReplyFWBlock *)_msg.data;
|
||||
replyFirmwareBlock_t *firmwareResponse = (replyFirmwareBlock_t *)_msg.data;
|
||||
// write to flash
|
||||
_flash.writeBytes( ((_fwBlock - 1) * FIRMWARE_BLOCK_SIZE) + FIRMWARE_START_OFFSET, firmwareResponse->data, FIRMWARE_BLOCK_SIZE);
|
||||
_flash.writeBytes( ((_firmwareBlock - 1) * FIRMWARE_BLOCK_SIZE) + FIRMWARE_START_OFFSET, firmwareResponse->data, FIRMWARE_BLOCK_SIZE);
|
||||
// wait until flash written
|
||||
while ( _flash.busy() ) {}
|
||||
_fwBlock--;
|
||||
if (!_fwBlock) {
|
||||
while (_flash.busy()) {}
|
||||
_firmwareBlock--;
|
||||
if (!_firmwareBlock) {
|
||||
// We're finished! Do a checksum and reboot.
|
||||
OTA_DEBUG(PSTR("OTA:FWP:FW END\n")); // received FW block
|
||||
_fwUpdateOngoing = false;
|
||||
_firmwareUpdateOngoing = false;
|
||||
if (transportIsValidFirmware()) {
|
||||
OTA_DEBUG(PSTR("OTA:FWP:CRC OK\n")); // FW checksum ok
|
||||
// All seems ok, write size and signature to flash (DualOptiboot will pick this up and flash it)
|
||||
uint16_t fwsize = FIRMWARE_BLOCK_SIZE * _fc.blocks;
|
||||
uint8_t OTAbuffer[10] = {'F','L','X','I','M','G',':',(uint8_t)(fwsize >> 8),(uint8_t)(fwsize & 0xff),':'};
|
||||
_flash.writeBytes(0, OTAbuffer, 10);
|
||||
// Write the new firmware config to eeprom
|
||||
hwWriteConfigBlock((void*)&_fc, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(NodeFirmwareConfig));
|
||||
hwWriteConfigBlock((void*)&_nodeFirmwareConfig, (void*)EEPROM_FIRMWARE_TYPE_ADDRESS, sizeof(nodeFirmwareConfig_t));
|
||||
// All seems ok, write size and signature to flash (DualOptiboot will pick this up and flash it)
|
||||
const uint16_t firmwareSize = FIRMWARE_BLOCK_SIZE * _nodeFirmwareConfig.blocks;
|
||||
const uint8_t OTAbuffer[FIRMWARE_START_OFFSET] = {'F','L','X','I','M','G',':', (uint8_t)(firmwareSize >> 8), (uint8_t)(firmwareSize & 0xff),':'};
|
||||
_flash.writeBytes(0, OTAbuffer, FIRMWARE_START_OFFSET);
|
||||
// wait until flash ready
|
||||
while (_flash.busy()) {}
|
||||
hwReboot();
|
||||
} else {
|
||||
setIndication(INDICATION_ERR_FW_CHECKSUM);
|
||||
@@ -116,8 +121,8 @@ bool firmwareOTAUpdateProcess() {
|
||||
}
|
||||
}
|
||||
// reset flags
|
||||
_fwRetry = MY_OTA_RETRY+1;
|
||||
_fwLastRequestTime = 0;
|
||||
_firmwareRetry = MY_OTA_RETRY + 1;
|
||||
_firmwareLastRequest = 0;
|
||||
} else {
|
||||
OTA_DEBUG(PSTR("!OTA:FWP:NO UPDATE\n"));
|
||||
}
|
||||
@@ -126,32 +131,38 @@ bool firmwareOTAUpdateProcess() {
|
||||
return false;
|
||||
}
|
||||
|
||||
void presentBootloaderInformation(){
|
||||
RequestFirmwareConfig *reqFWConfig = (RequestFirmwareConfig *)_msgTmp.data;
|
||||
mSetLength(_msgTmp, sizeof(RequestFirmwareConfig));
|
||||
void presentBootloaderInformation(void)
|
||||
{
|
||||
requestFirmwareConfig_t *requestFirmwareConfig = (requestFirmwareConfig_t *)_msgTmp.data;
|
||||
mSetLength(_msgTmp, sizeof(requestFirmwareConfig_t));
|
||||
mSetCommand(_msgTmp, C_STREAM);
|
||||
mSetPayloadType(_msgTmp,P_CUSTOM);
|
||||
mSetPayloadType(_msgTmp, P_CUSTOM);
|
||||
// copy node settings to reqFWConfig
|
||||
memcpy(reqFWConfig,&_fc,sizeof(NodeFirmwareConfig));
|
||||
(void)memcpy(requestFirmwareConfig, &_nodeFirmwareConfig, sizeof(nodeFirmwareConfig_t));
|
||||
// add bootloader information
|
||||
reqFWConfig->BLVersion = MY_OTA_BOOTLOADER_VERSION;
|
||||
_fwUpdateOngoing = false;
|
||||
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, ST_FIRMWARE_CONFIG_REQUEST, false));
|
||||
requestFirmwareConfig->BLVersion = MY_OTA_BOOTLOADER_VERSION;
|
||||
_firmwareUpdateOngoing = false;
|
||||
(void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_STREAM, ST_FIRMWARE_CONFIG_REQUEST, false));
|
||||
}
|
||||
bool isFirmwareUpdateOngoing(void)
|
||||
{
|
||||
return _firmwareUpdateOngoing;
|
||||
}
|
||||
// do a crc16 on the whole received firmware
|
||||
bool transportIsValidFirmware() {
|
||||
bool transportIsValidFirmware(void)
|
||||
{
|
||||
// init crc
|
||||
uint16_t crc = ~0;
|
||||
for (uint16_t i = 0; i < _fc.blocks * FIRMWARE_BLOCK_SIZE; ++i) {
|
||||
for (uint16_t i = 0; i < _nodeFirmwareConfig.blocks * FIRMWARE_BLOCK_SIZE; ++i) {
|
||||
crc ^= _flash.readByte(i + FIRMWARE_START_OFFSET);
|
||||
for (int8_t j = 0; j < 8; ++j) {
|
||||
if (crc & 1) {
|
||||
crc = (crc >> 1) ^ 0xA001;
|
||||
}
|
||||
else {
|
||||
crc = (crc >> 1);
|
||||
}
|
||||
}
|
||||
for (int8_t j = 0; j < 8; ++j) {
|
||||
if (crc & 1) {
|
||||
crc = (crc >> 1) ^ 0xA001;
|
||||
}
|
||||
else {
|
||||
crc = (crc >> 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc == _fc.crc;
|
||||
return crc == _nodeFirmwareConfig.crc;
|
||||
}
|
||||
|
||||
+21
-18
@@ -56,18 +56,21 @@
|
||||
|
||||
#include "MySensorsCore.h"
|
||||
|
||||
#define FIRMWARE_BLOCK_SIZE (16) //!< Size of each firmware block
|
||||
#define FIRMWARE_MAX_REQUESTS (5) //!< Number of times a firmware block should be requested before giving up
|
||||
#define MY_OTA_RETRY (5) //!< Number of times to request a fw block before giving up
|
||||
#define MY_OTA_RETRY_DELAY (500) //!< Number of milliseconds before re-requesting a FW block
|
||||
#define FIRMWARE_START_OFFSET (10) //!< Start offset for firmware in flash (DualOptiboot wants to keeps a signature first)
|
||||
#define FIRMWARE_BLOCK_SIZE (16u) //!< Size of each firmware block
|
||||
#define FIRMWARE_MAX_REQUESTS (5u) //!< Number of times a firmware block should be requested before giving up
|
||||
#define MY_OTA_RETRY (5u) //!< Number of times to request a fw block before giving up
|
||||
#define MY_OTA_RETRY_DELAY (500u) //!< Number of milliseconds before re-requesting a FW block
|
||||
#define FIRMWARE_START_OFFSET (10u) //!< Start offset for firmware in flash (DualOptiboot wants to keeps a signature first)
|
||||
|
||||
#define MY_OTA_BOOTLOADER_MAJOR_VERSION (3) //!< Bootloader version major
|
||||
#define MY_OTA_BOOTLOADER_MINOR_VERSION (0) //!< Bootloader version minor
|
||||
#define MY_OTA_BOOTLOADER_MAJOR_VERSION (3u) //!< Bootloader version major
|
||||
#define MY_OTA_BOOTLOADER_MINOR_VERSION (0u) //!< Bootloader version minor
|
||||
#define MY_OTA_BOOTLOADER_VERSION (MY_OTA_BOOTLOADER_MINOR_VERSION * 256 + MY_OTA_BOOTLOADER_MAJOR_VERSION) //!< Bootloader version
|
||||
|
||||
#define OTA_DEBUG(x,...) debug(x, ##__VA_ARGS__) //!< debug
|
||||
|
||||
#if defined(MY_DEBUG)
|
||||
#define OTA_DEBUG(x,...) hwDebugPrint(x, ##__VA_ARGS__) //!< debug
|
||||
#else
|
||||
#define OTA_DEBUG(x,...) //!< debug NULL
|
||||
#endif
|
||||
/**
|
||||
* @brief FW config structure, stored in eeprom
|
||||
*/
|
||||
@@ -76,7 +79,7 @@ typedef struct {
|
||||
uint16_t version; //!< Version of config
|
||||
uint16_t blocks; //!< Number of blocks
|
||||
uint16_t crc; //!< CRC of block data
|
||||
} __attribute__((packed)) NodeFirmwareConfig;
|
||||
} __attribute__((packed)) nodeFirmwareConfig_t;
|
||||
|
||||
/**
|
||||
* @brief FW config request structure
|
||||
@@ -87,7 +90,7 @@ typedef struct {
|
||||
uint16_t blocks; //!< Number of blocks
|
||||
uint16_t crc; //!< CRC of block data
|
||||
uint16_t BLVersion; //!< Bootloader version
|
||||
} __attribute__((packed)) RequestFirmwareConfig;
|
||||
} __attribute__((packed)) requestFirmwareConfig_t;
|
||||
|
||||
/**
|
||||
* @brief FW block request structure
|
||||
@@ -96,7 +99,7 @@ typedef struct {
|
||||
uint16_t type; //!< Type of config
|
||||
uint16_t version; //!< Version of config
|
||||
uint16_t block; //!< Block index
|
||||
} __attribute__((packed)) RequestFWBlock;
|
||||
} __attribute__((packed)) requestFirmwareBlock_t;
|
||||
|
||||
/**
|
||||
* @brief FW block reply structure
|
||||
@@ -106,7 +109,7 @@ typedef struct {
|
||||
uint16_t version; //!< Version of config
|
||||
uint16_t block; //!< Block index
|
||||
uint8_t data[FIRMWARE_BLOCK_SIZE]; //!< Block data
|
||||
} __attribute__((packed)) ReplyFWBlock;
|
||||
} __attribute__((packed)) replyFirmwareBlock_t;
|
||||
|
||||
|
||||
/**
|
||||
@@ -114,27 +117,27 @@ typedef struct {
|
||||
*
|
||||
* Current firmware settings (type, version, crc, blocks) are read into _fc
|
||||
*/
|
||||
void readFirmwareSettings();
|
||||
void readFirmwareSettings(void);
|
||||
/**
|
||||
* @brief Handle OTA FW update requests
|
||||
*/
|
||||
void firmwareOTAUpdateRequest();
|
||||
void firmwareOTAUpdateRequest(void);
|
||||
/**
|
||||
* @brief Handle OTA FW update responses
|
||||
*
|
||||
* This function handles incoming OTA FW packets and stores them to external flash (Sensebender)
|
||||
*/
|
||||
bool firmwareOTAUpdateProcess();
|
||||
bool firmwareOTAUpdateProcess(void);
|
||||
/**
|
||||
* @brief Validate uploaded FW CRC
|
||||
*
|
||||
* This function verifies if uploaded FW CRC is valid
|
||||
*/
|
||||
bool transportIsValidFirmware();
|
||||
bool transportIsValidFirmware(void);
|
||||
/**
|
||||
* @brief Present bootloader/FW information upon startup
|
||||
*/
|
||||
void presentBootloaderInformation();
|
||||
void presentBootloaderInformation(void);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
+8
-16
@@ -36,7 +36,7 @@ static coreConfig_t _coreConfig;
|
||||
#endif
|
||||
|
||||
// Callback for transport=ok transition
|
||||
void _callbackTransportOk(void)
|
||||
void _callbackTransportReady(void)
|
||||
{
|
||||
if (!_coreConfig.presentationSent) {
|
||||
presentNode();
|
||||
@@ -114,23 +114,15 @@ void _begin(void) {
|
||||
#endif
|
||||
|
||||
#if defined(MY_SENSOR_NETWORK)
|
||||
// Save static parent id in eeprom (used by bootloader)
|
||||
// Save static parent ID in eeprom (used by bootloader)
|
||||
hwWriteConfig(EEPROM_PARENT_NODE_ID_ADDRESS, MY_PARENT_NODE_ID);
|
||||
// Register transport=OK callback
|
||||
transportRegisterOkCallback(_callbackTransportOk);
|
||||
// Initialise transport layer
|
||||
transportInitialise();
|
||||
// Register transport=ready callback
|
||||
transportRegisterReadyCallback(_callbackTransportReady);
|
||||
// wait until transport is ready
|
||||
#if !defined(MY_TRANSPORT_RELAXED)
|
||||
// check if transport ready
|
||||
while (!isTransportReady()) {
|
||||
transportProcess();
|
||||
doYield();
|
||||
}
|
||||
#else
|
||||
CORE_DEBUG(PSTR("MCO:BGN:MTR\n"));
|
||||
#endif
|
||||
#endif
|
||||
(void)transportWaitUntilReady(MY_TRANSPORT_WAIT_READY_MS);
|
||||
#endif
|
||||
|
||||
_checkNodeLock();
|
||||
|
||||
@@ -478,8 +470,8 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in
|
||||
CORE_DEBUG(PSTR("MCO:SLP:MS=%lu,SMS=%d,I1=%d,M1=%d,I2=%d,M2=%d\n"), sleepingMS, smartSleep, interrupt1, mode1, interrupt2, mode2);
|
||||
// OTA FW feature: do not sleep if FW update ongoing
|
||||
#if defined(MY_OTA_FIRMWARE_FEATURE)
|
||||
if (_fwUpdateOngoing) {
|
||||
CORE_DEBUG(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing
|
||||
if (isFirmwareUpdateOngoing()) {
|
||||
debug(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing
|
||||
wait(sleepingMS);
|
||||
return MY_SLEEP_NOT_POSSIBLE;
|
||||
}
|
||||
|
||||
+23
-12
@@ -30,11 +30,11 @@ static transportState_t stFailure = { stFailureTransition, stFailureUpdate };
|
||||
// transport SM variables
|
||||
static transportSM_t _transportSM;
|
||||
|
||||
// transport config
|
||||
// transport configuration
|
||||
static transportConfig_t _transportConfig;
|
||||
|
||||
// callbacks
|
||||
transportCallback_t transportOk_cb = NULL;
|
||||
// callback transportOk
|
||||
transportCallback_t transportReady_cb = NULL;
|
||||
|
||||
// global variables
|
||||
extern MyMessage _msg; // incoming message
|
||||
@@ -216,7 +216,7 @@ void stUplinkTransition(void) {
|
||||
setIndication(INDICATION_CHECK_UPLINK);
|
||||
_transportSM.pingResponse = INVALID_HOPS;
|
||||
_transportSM.pingActive = true;
|
||||
(void)transportRouteMessage(build(_msgTmp,GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_PING).set((uint8_t)1));
|
||||
(void)transportRouteMessage(build(_msgTmp,GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_PING).set((uint8_t)0x01));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -226,7 +226,6 @@ void stUplinkUpdate(void) {
|
||||
_transportSM.lastUplinkCheck = hwMillis();
|
||||
// uplink ok, i.e. GW replied
|
||||
TRANSPORT_DEBUG(PSTR("TSM:UPL:OK\n")); // uplink ok
|
||||
//_transportSM.pingActive = false; ==> set false upon receiving I_PONG
|
||||
if (_transportSM.pingResponse != _transportConfig.distanceGW) {
|
||||
TRANSPORT_DEBUG(PSTR("TSM:UPL:DGWC,O=%d,N=%d\n"), _transportConfig.distanceGW, _transportSM.pingResponse); // distance to GW changed
|
||||
_transportConfig.distanceGW = _transportSM.pingResponse;
|
||||
@@ -255,15 +254,14 @@ void stUplinkUpdate(void) {
|
||||
|
||||
void stReadyTransition(void) {
|
||||
// transport is ready and fully operational
|
||||
TRANSPORT_DEBUG(PSTR("TSM:READY\n")); // transport is ready
|
||||
TRANSPORT_DEBUG(PSTR("TSM:READY:ID=%d,PAR=%d,DIS=%d\n"), _transportConfig.nodeId, _transportConfig.parentNodeId, _transportConfig.distanceGW);
|
||||
_transportSM.uplinkOk = true;
|
||||
_transportSM.failureCounter = 0u; // reset failure counter
|
||||
_transportSM.failedUplinkTransmissions = 0u; // reset failed uplink TX counter
|
||||
// callback
|
||||
if (transportOk_cb) {
|
||||
transportOk_cb();
|
||||
if (transportReady_cb) {
|
||||
transportReady_cb();
|
||||
}
|
||||
TRANSPORT_DEBUG(PSTR("TSM:READY:PARAM,ID=%d,PAR=%d,DIS=%d\n"), _transportConfig.nodeId, _transportConfig.parentNodeId, _transportConfig.distanceGW);
|
||||
}
|
||||
|
||||
// stReadyUpdate: monitors link
|
||||
@@ -371,6 +369,19 @@ void transportInitialise(void) {
|
||||
transportSwitchSM(stInit);
|
||||
}
|
||||
|
||||
bool transportWaitUntilReady(const uint32_t waitingMS) {
|
||||
// check if transport ready
|
||||
TRANSPORT_DEBUG(PSTR("TSF:WUR:MS=%lu\n"), waitingMS); // timeout
|
||||
uint32_t enterMS = hwMillis();
|
||||
bool result = false;
|
||||
while (!result && ( hwMillis() - enterMS < waitingMS || !waitingMS)) {
|
||||
transportProcess();
|
||||
result = isTransportReady();
|
||||
doYield();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// update TSM and process incoming messages
|
||||
void transportProcess(void) {
|
||||
// update state machine
|
||||
@@ -520,7 +531,7 @@ uint8_t transportPingNode(const uint8_t targetId) {
|
||||
else {
|
||||
_transportSM.pingActive = true;
|
||||
_transportSM.pingResponse = INVALID_HOPS;
|
||||
(void)transportRouteMessage(build(_msgTmp, targetId, NODE_SENSOR_ID, C_INTERNAL, I_PING).set((uint8_t)1));
|
||||
(void)transportRouteMessage(build(_msgTmp, targetId, NODE_SENSOR_ID, C_INTERNAL, I_PING).set((uint8_t)0x01));
|
||||
// Wait for ping reply or timeout
|
||||
(void)transportWait(2000, C_INTERNAL, I_PONG);
|
||||
}
|
||||
@@ -849,9 +860,9 @@ bool transportSendWrite(const uint8_t to, MyMessage &message) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void transportRegisterOkCallback(transportCallback_t cb)
|
||||
void transportRegisterReadyCallback(transportCallback_t cb)
|
||||
{
|
||||
transportOk_cb = cb;
|
||||
transportReady_cb = cb;
|
||||
}
|
||||
|
||||
uint8_t transportGetNodeId(void)
|
||||
|
||||
+10
-2
@@ -41,6 +41,7 @@
|
||||
* - TSF:CHKUPL from @ref transportCheckUplink(), checks connection to GW
|
||||
* - TSF:ASID from @ref transportAssignNodeID(), assigns node ID
|
||||
* - TSF:PING from @ref transportPingNode(), pings a node
|
||||
* - TSF:WUR from @ref transportWaitUntilReady(), waits until transport is ready
|
||||
* - TSF:CRT from @ref transportClearRoutingTable(), clears routing table stored in EEPROM
|
||||
* - TSF:LRT from @ref transportLoadRoutingTable(), loads RAM routing table from EEPROM (only GW/repeaters)
|
||||
* - TSF:SRT from @ref transportSaveRoutingTable(), saves RAM routing table to EEPROM (only GW/repeaters)
|
||||
@@ -72,8 +73,8 @@
|
||||
* | | TSM | UPL | OK | Uplink OK, GW returned ping
|
||||
* | | TSF | UPL | DGWC,O=%%d,N=%%d | Uplink check revealed changed network topology, old distance (O), new distance (N)
|
||||
* |!| TSM | UPL | FAIL | Uplink check failed, i.e. GW could not be pinged
|
||||
* | | TSM | READY | | <b>Transition to stReady</b>, i.e. transport is ready and fully operational
|
||||
* | | TSM | READY | SRT | Save routing table
|
||||
* | | TSM | READY | ID=%%d,PAR=%%d,DIS=%%d| <b>Transition to stReady</b> Transport ready, node ID (ID), parent node ID (PAR), distance to GW (DIS)
|
||||
* |!| TSM | READY | UPL FAIL,SNP | Too many failed uplink transmissions, search new parent
|
||||
* |!| TSM | READY | FAIL,STATP | Too many failed uplink transmissions, static parent enforced
|
||||
* | | TSM | FAIL | CNT=%%d | <b>Transition to stFailure state</b>, consecutive failure counter (CNT)
|
||||
@@ -86,6 +87,7 @@
|
||||
* | | TSF | ASID | OK,ID=%%d | Node ID assigned
|
||||
* |!| TSF | ASID | FAIL,ID=%%d | Assigned ID is invalid
|
||||
* | | TSF | PING | SEND,TO=%%d | Send ping to destination (TO)
|
||||
* | | TSF | WUR | MS=%%lu | Wait until transport ready, timeout (MS)
|
||||
* | | TSF | MSG | ACK REQ | ACK message requested
|
||||
* | | TSF | MSG | ACK | ACK message, do not proceed but forward to callback
|
||||
* | | TSF | MSG | FPAR RES,ID=%%d,D=%%d | Response to find parent received from node (ID) with distance (D) to GW
|
||||
@@ -387,10 +389,16 @@ bool transportSendWrite(const uint8_t to, MyMessage &message);
|
||||
* @param force to override flood control timer
|
||||
* @return true if uplink ok
|
||||
*/
|
||||
bool transportCheckUplink(const bool force=false);
|
||||
bool transportCheckUplink(const bool force = false);
|
||||
|
||||
// PUBLIC functions
|
||||
|
||||
/**
|
||||
* @brief Wait until transport is ready
|
||||
* @param waitingMS timeout in MS, set 0 (default) for no timeout, i.e. wait indefinitely. For a node in standalone mode (optional network connection) set >0 to allow a node entering the main loop() function.
|
||||
* @return true if transport is ready
|
||||
*/
|
||||
bool transportWaitUntilReady(const uint32_t waitingMS = 0);
|
||||
/**
|
||||
* @brief Initialize transport and SM
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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 <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2016 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 "MyConfig.h"
|
||||
#include "MyTransport.h"
|
||||
#include "drivers/RFM95/RFM95.h"
|
||||
|
||||
bool transportInit(void) {
|
||||
const bool result = RFM95_initialise(MY_RFM95_FREQUENCY);
|
||||
#if !defined(MY_GATEWAY_FEATURE) && !defined(MY_RFM95_ATC_MODE_DISABLED)
|
||||
// only enable ATC mode nodes
|
||||
RFM95_ATCmode(true, MY_RFM95_ATC_TARGET_RSSI);
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
void transportSetAddress(uint8_t address) {
|
||||
RFM95_setAddress(address);
|
||||
}
|
||||
|
||||
uint8_t transportGetAddress(void) {
|
||||
return RFM95_getAddress();
|
||||
}
|
||||
|
||||
bool transportSend(uint8_t to, const void* data, uint8_t len) {
|
||||
return RFM95_sendWithRetry(to, data, len);
|
||||
}
|
||||
|
||||
bool transportAvailable(void) {
|
||||
return RFM95_available();
|
||||
}
|
||||
|
||||
bool transportSanityCheck(void) {
|
||||
return RFM95_sanityCheck();
|
||||
}
|
||||
|
||||
uint8_t transportReceive(void* data) {
|
||||
return RFM95_recv((uint8_t*)data);
|
||||
}
|
||||
|
||||
void transportPowerDown(void) {
|
||||
(void)RFM95_sleep();
|
||||
}
|
||||
|
||||
int16_t transportGetSignalStrength(void) {
|
||||
return RFM95_getRSSI();
|
||||
}
|
||||
Reference in New Issue
Block a user