Merge pull request #605 from tekka007/CoreCosmetics

Code harmonization
This commit is contained in:
Henrik Ekblad
2016-10-09 23:01:25 +02:00
committed by GitHub
7 changed files with 176 additions and 178 deletions
+5 -5
View File
@@ -54,7 +54,7 @@
// Enable radio "feature" if one of the radio types was enabled // Enable radio "feature" if one of the radio types was enabled
#if defined(MY_RADIO_NRF24) || defined(MY_RADIO_RFM69) || defined(MY_RS485) #if defined(MY_RADIO_NRF24) || defined(MY_RADIO_RFM69) || defined(MY_RS485)
#define MY_RADIO_FEATURE #define MY_SENSOR_NETWORK
#endif #endif
// HARDWARE // HARDWARE
@@ -167,7 +167,7 @@
#endif #endif
#if defined(MY_GATEWAY_MQTT_CLIENT) #if defined(MY_GATEWAY_MQTT_CLIENT)
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
// We assume that a gateway having a radio also should act as repeater // We assume that a gateway having a radio also should act as repeater
#define MY_REPEATER_FEATURE #define MY_REPEATER_FEATURE
#endif #endif
@@ -205,7 +205,7 @@
#include "core/MyProtocolMySensors.cpp" #include "core/MyProtocolMySensors.cpp"
// GATEWAY - CONFIGURATION // GATEWAY - CONFIGURATION
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
// We assume that a gateway having a radio also should act as repeater // We assume that a gateway having a radio also should act as repeater
#define MY_REPEATER_FEATURE #define MY_REPEATER_FEATURE
#endif #endif
@@ -308,7 +308,7 @@
#endif #endif
// Make sure to disable child features when parent feature is disabled // Make sure to disable child features when parent feature is disabled
#if !defined(MY_RADIO_FEATURE) #if !defined(MY_SENSOR_NETWORK)
#undef MY_OTA_FIRMWARE_FEATURE #undef MY_OTA_FIRMWARE_FEATURE
#undef MY_REPEATER_FEATURE #undef MY_REPEATER_FEATURE
#undef MY_SIGNING_NODE_WHITELISTING #undef MY_SIGNING_NODE_WHITELISTING
@@ -321,7 +321,7 @@
#endif #endif
#if !defined(MY_CORE_ONLY) #if !defined(MY_CORE_ONLY)
#if !defined(MY_GATEWAY_FEATURE) && !defined(MY_RADIO_FEATURE) #if !defined(MY_GATEWAY_FEATURE) && !defined(MY_SENSOR_NETWORK)
#error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless. #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
#endif #endif
#endif #endif
+1 -1
View File
@@ -61,7 +61,7 @@ inline void gatewayTransportProcess() {
} }
} }
} else { } else {
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
transportSendRoute(_msg); transportSendRoute(_msg);
#endif #endif
} }
+103 -110
View File
@@ -19,10 +19,10 @@
#include "MySensorsCore.h" #include "MySensorsCore.h"
ControllerConfig _cc; // Configuration coming from controller ControllerConfig _cc; // Configuration coming from controller
NodeConfig _nc; // Essential settings for node to work NodeConfig _nc; // Essential settings for node to work
MyMessage _msg; // Buffer for incoming messages. MyMessage _msg; // Buffer for incoming messages
MyMessage _msgTmp; // Buffer for temporary messages (acks and nonces among others). MyMessage _msgTmp; // Buffer for temporary messages (acks and nonces among others)
bool _nodeRegistered = false; bool _nodeRegistered = false;
@@ -32,7 +32,7 @@ bool _nodeRegistered = false;
void (*_timeCallback)(unsigned long); // Callback for requested time messages void (*_timeCallback)(unsigned long); // Callback for requested time messages
void _process() { void _process(void) {
hwWatchdogReset(); hwWatchdogReset();
#if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN) #if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
@@ -47,7 +47,7 @@ void _process() {
gatewayTransportProcess(); gatewayTransportProcess();
#endif #endif
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
transportProcess(); transportProcess();
#endif #endif
@@ -57,11 +57,9 @@ void _process() {
#endif #endif
} }
void _infiniteLoop() { void _infiniteLoop(void) {
while(1) { while(1) {
#if defined(ARDUINO_ARCH_ESP8266) yield();
yield();
#endif
#if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN) #if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
ledsProcess(); ledsProcess();
#endif #endif
@@ -71,7 +69,7 @@ void _infiniteLoop() {
} }
} }
void _begin() { void _begin(void) {
hwWatchdogReset(); hwWatchdogReset();
if (preHwInit) if (preHwInit)
@@ -102,16 +100,14 @@ void _begin() {
readFirmwareSettings(); readFirmwareSettings();
#endif #endif
#if defined(MY_RADIO_FEATURE) #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); hwWriteConfig(EEPROM_PARENT_NODE_ID_ADDRESS, MY_PARENT_NODE_ID);
transportInitialise(); transportInitialise();
while (!isTransportReady()) { while (!isTransportReady()) {
hwWatchdogReset(); hwWatchdogReset();
transportProcess(); transportProcess();
#if defined(ARDUINO_ARCH_ESP8266) yield();
yield();
#endif
} }
#endif #endif
@@ -176,7 +172,7 @@ void _begin() {
} }
void _registerNode() { void _registerNode(void) {
#if defined (MY_REGISTRATION_FEATURE) && !defined(MY_GATEWAY_FEATURE) #if defined (MY_REGISTRATION_FEATURE) && !defined(MY_GATEWAY_FEATURE)
debug(PSTR("MCO:REG:REQ\n")); // registration request debug(PSTR("MCO:REG:REQ\n")); // registration request
setIndication(INDICATION_REQ_REGISTRATION); setIndication(INDICATION_REQ_REGISTRATION);
@@ -184,7 +180,7 @@ void _registerNode() {
uint8_t counter = MY_REGISTRATION_RETRIES; uint8_t counter = MY_REGISTRATION_RETRIES;
// only proceed if register response received or retries exceeded // only proceed if register response received or retries exceeded
do { do {
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_REGISTRATION_REQUEST, false).set(MY_CORE_VERSION)); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_REGISTRATION_REQUEST).set(MY_CORE_VERSION));
} while (!wait(2000, C_INTERNAL, I_REGISTRATION_RESPONSE) && counter--); } while (!wait(2000, C_INTERNAL, I_REGISTRATION_RESPONSE) && counter--);
#else #else
@@ -193,20 +189,20 @@ void _registerNode() {
#endif #endif
} }
void presentNode() { void presentNode(void) {
setIndication(INDICATION_PRESENT); setIndication(INDICATION_PRESENT);
// Present node and request config // Present node and request config
#if defined(MY_GATEWAY_FEATURE) #if defined(MY_GATEWAY_FEATURE)
// Send presentation for this gateway device // Send presentation for this gateway device
#if defined(MY_REPEATER_FEATURE) #if defined(MY_REPEATER_FEATURE)
present(NODE_SENSOR_ID, S_ARDUINO_REPEATER_NODE); (void)present(NODE_SENSOR_ID, S_ARDUINO_REPEATER_NODE);
#else #else
present(NODE_SENSOR_ID, S_ARDUINO_NODE); (void)present(NODE_SENSOR_ID, S_ARDUINO_NODE);
#endif #endif
#else #else
#if defined(MY_OTA_FIRMWARE_FEATURE) #if defined(MY_OTA_FIRMWARE_FEATURE)
presentBootloaderInformation(); presentBootloaderInformation();
#endif #endif
// Send signing preferences for this node to the GW // Send signing preferences for this node to the GW
@@ -214,17 +210,17 @@ void presentNode() {
// Send presentation for this radio node // Send presentation for this radio node
#if defined(MY_REPEATER_FEATURE) #if defined(MY_REPEATER_FEATURE)
present(NODE_SENSOR_ID, S_ARDUINO_REPEATER_NODE); (void)present(NODE_SENSOR_ID, S_ARDUINO_REPEATER_NODE);
#else #else
present(NODE_SENSOR_ID, S_ARDUINO_NODE); (void)present(NODE_SENSOR_ID, S_ARDUINO_NODE);
#endif #endif
// Send a configuration exchange request to controller // Send a configuration exchange request to controller
// Node sends parent node. Controller answers with latest node configuration // Node sends parent node. Controller answers with latest node configuration
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG, false).set(_nc.parentNodeId)); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG).set(_nc.parentNodeId));
// Wait configuration reply. // Wait configuration reply.
wait(2000, C_INTERNAL, I_CONFIG); (void)wait(2000, C_INTERNAL, I_CONFIG);
#endif #endif
@@ -234,15 +230,15 @@ void presentNode() {
} }
uint8_t getNodeId() { uint8_t getNodeId(void) {
return _nc.nodeId; return _nc.nodeId;
} }
uint8_t getParentNodeId() { uint8_t getParentNodeId(void) {
return _nc.parentNodeId; return _nc.parentNodeId;
} }
ControllerConfig getConfig() { ControllerConfig getConfig(void) {
return _cc; return _cc;
} }
@@ -258,14 +254,14 @@ bool _sendRoute(MyMessage &message) {
return gatewayTransportSend(message); return gatewayTransportSend(message);
} }
#endif #endif
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
return transportSendRoute(message); return transportSendRoute(message);
#else #else
return false; return false;
#endif #endif
} }
bool send(MyMessage &message, bool enableAck) { bool send(MyMessage &message, const bool enableAck) {
message.sender = _nc.nodeId; message.sender = _nc.nodeId;
mSetCommand(message, C_SET); mSetCommand(message, C_SET);
mSetRequestAck(message, enableAck); mSetRequestAck(message, enableAck);
@@ -283,37 +279,42 @@ bool send(MyMessage &message, bool enableAck) {
#endif #endif
} }
void sendBatteryLevel(uint8_t value, bool enableAck) { bool sendBatteryLevel(const uint8_t value, const bool ack) {
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_BATTERY_LEVEL, enableAck).set(value)); return _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_BATTERY_LEVEL, ack).set(value));
} }
void sendHeartbeat(void) { bool sendHeartbeat(const bool ack) {
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
uint32_t heartbeat = transportGetHeartbeat(); const uint32_t heartbeat = transportGetHeartbeat();
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_HEARTBEAT_RESPONSE, false).set(heartbeat)); return _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_HEARTBEAT_RESPONSE, ack).set(heartbeat));
#else
(void)ack;
return false;
#endif #endif
} }
void present(uint8_t childSensorId, uint8_t sensorType, const char *description, bool enableAck) { bool present(const uint8_t childSensorId, const uint8_t sensorType, const char *description, const bool ack) {
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, childSensorId, C_PRESENTATION, sensorType, enableAck).set(childSensorId==NODE_SENSOR_ID?MYSENSORS_LIBRARY_VERSION:description)); return _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, childSensorId, C_PRESENTATION, sensorType, ack).set(childSensorId==NODE_SENSOR_ID?MYSENSORS_LIBRARY_VERSION:description));
} }
void sendSketchInfo(const char *name, const char *version, bool enableAck) { bool sendSketchInfo(const char *name, const char *version, const bool ack) {
if (name) _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_NAME, enableAck).set(name)); bool result = true;
if (version) _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_VERSION, enableAck).set(version)); if (name) result &= _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_NAME, ack).set(name));
if (version) result &= _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_VERSION, ack).set(version));
return result;
} }
void request(uint8_t childSensorId, uint8_t variableType, uint8_t destination) { bool request(const uint8_t childSensorId, const uint8_t variableType, const uint8_t destination) {
_sendRoute(build(_msgTmp, destination, childSensorId, C_REQ, variableType, false).set("")); return _sendRoute(build(_msgTmp, destination, childSensorId, C_REQ, variableType).set(""));
} }
void requestTime() { bool requestTime(const bool ack) {
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME, false).set("")); return _sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME, ack).set(""));
} }
// Message delivered through _msg // Message delivered through _msg
bool _processInternalMessages() { bool _processInternalMessages(void) {
uint8_t type = _msg.type; const uint8_t type = _msg.type;
if (_msg.sender == GATEWAY_ADDRESS) { if (_msg.sender == GATEWAY_ADDRESS) {
if (type == I_REBOOT) { if (type == I_REBOOT) {
@@ -340,7 +341,7 @@ bool _processInternalMessages() {
presentNode(); presentNode();
} }
else if (type == I_HEARTBEAT_REQUEST) { else if (type == I_HEARTBEAT_REQUEST) {
sendHeartbeat(); (void)sendHeartbeat();
} }
else if (type == I_TIME) { else if (type == I_TIME) {
// Deliver time to callback // Deliver time to callback
@@ -353,7 +354,7 @@ bool _processInternalMessages() {
// Clears child relay data for this node // Clears child relay data for this node
setIndication(INDICATION_CLEAR_ROUTING); setIndication(INDICATION_CLEAR_ROUTING);
transportClearRoutingTable(); transportClearRoutingTable();
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN, false).set("ok")); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN).set("OK"));
} }
#endif #endif
} }
@@ -367,23 +368,23 @@ bool _processInternalMessages() {
if (route != BROADCAST_ADDRESS) { if (route != BROADCAST_ADDRESS) {
debug(PSTR("MCO:PIM:ROUTE N=%d,R=%d\n"), cnt, route); debug(PSTR("MCO:PIM:ROUTE N=%d,R=%d\n"), cnt, route);
uint8_t outBuf[2] = { (uint8_t)cnt,route }; uint8_t outBuf[2] = { (uint8_t)cnt,route };
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG, false).set(outBuf, 2)); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG).set(outBuf, 2));
wait(200); wait(200);
} }
} }
#endif #endif
} }
else if (debug_msg == 'V') { // CPU voltage else if (debug_msg == 'V') { // CPU voltage
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG, false).set(hwCPUVoltage())); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG).set(hwCPUVoltage()));
} }
else if (debug_msg == 'F') { // CPU frequency in 1/10Mhz else if (debug_msg == 'F') { // CPU frequency in 1/10Mhz
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG, false).set(hwCPUFrequency())); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG).set(hwCPUFrequency()));
} }
else if (debug_msg == 'M') { // free memory else if (debug_msg == 'M') { // free memory
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG, false).set(hwFreeMem())); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG).set(hwFreeMem()));
} }
else if (debug_msg == 'E') { // clear MySensors eeprom area and reboot else if (debug_msg == 'E') { // clear MySensors eeprom area and reboot
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG, false).set("ok")); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DEBUG).set("OK"));
for (int i = EEPROM_START; i<EEPROM_LOCAL_CONFIG_ADDRESS; i++) hwWriteConfig(i, 0xFF); for (int i = EEPROM_START; i<EEPROM_LOCAL_CONFIG_ADDRESS; i++) hwWriteConfig(i, 0xFF);
setIndication(INDICATION_REBOOT); setIndication(INDICATION_REBOOT);
hwReboot(); hwReboot();
@@ -409,7 +410,7 @@ bool _processInternalMessages() {
// delay for fast GW and slow nodes // delay for fast GW and slow nodes
delay(5); delay(5);
#endif #endif
_sendRoute(build(_msgTmp, _msg.sender, NODE_SENSOR_ID, C_INTERNAL, I_REGISTRATION_RESPONSE, false).set(approveRegistration)); (void)_sendRoute(build(_msgTmp, _msg.sender, NODE_SENSOR_ID, C_INTERNAL, I_REGISTRATION_RESPONSE).set(approveRegistration));
#else #else
return false; // processing of this request via controller return false; // processing of this request via controller
#endif #endif
@@ -421,46 +422,42 @@ bool _processInternalMessages() {
} }
void saveState(uint8_t pos, uint8_t value) { void saveState(const uint8_t pos, const uint8_t value) {
hwWriteConfig(EEPROM_LOCAL_CONFIG_ADDRESS+pos, value); hwWriteConfig(EEPROM_LOCAL_CONFIG_ADDRESS+pos, value);
} }
uint8_t loadState(uint8_t pos) { uint8_t loadState(const uint8_t pos) {
return hwReadConfig(EEPROM_LOCAL_CONFIG_ADDRESS+pos); return hwReadConfig(EEPROM_LOCAL_CONFIG_ADDRESS+pos);
} }
void wait(unsigned long ms) { void wait(const uint32_t waitingMS) {
unsigned long enter = hwMillis(); const uint32_t enteringMS = hwMillis();
while (hwMillis() - enter < ms) { while (hwMillis() - enteringMS < waitingMS) {
_process(); _process();
#if defined(ARDUINO_ARCH_ESP8266) yield();
yield();
#endif
} }
} }
bool wait(unsigned long ms, uint8_t cmd, uint8_t msgtype) { bool wait(const uint32_t waitingMS, const uint8_t cmd, const uint8_t msgtype) {
unsigned long enter = hwMillis(); const uint32_t enteringMS = hwMillis();
// invalidate msg type // invalidate msg type
_msg.type = !msgtype; _msg.type = !msgtype;
bool expectedResponse = false; bool expectedResponse = false;
while ( (hwMillis() - enter < ms) && !expectedResponse ) { while ( (hwMillis() - enteringMS < waitingMS) && !expectedResponse ) {
_process(); _process();
#if defined(ARDUINO_ARCH_ESP8266) yield();
yield();
#endif
expectedResponse = (mGetCommand(_msg) == cmd && _msg.type == msgtype); expectedResponse = (mGetCommand(_msg) == cmd && _msg.type == msgtype);
} }
return expectedResponse; return expectedResponse;
} }
int8_t _sleep(const uint32_t ms, const bool smartSleep, const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2) { int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2) {
debug(PSTR("MCO:SLP:MS=%lu,SMS=%d,I1=%d,M1=%d,I2=%d,M2=%d\n"), ms, smartSleep, interrupt1, mode1, interrupt2, mode2); 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 // OTA FW feature: do not sleep if FW update ongoing
#if defined(MY_OTA_FIRMWARE_FEATURE) #if defined(MY_OTA_FIRMWARE_FEATURE)
if (_fwUpdateOngoing) { if (_fwUpdateOngoing) {
debug(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing debug(PSTR("!MCO:SLP:FWUPD\n")); // sleeping not possible, FW update ongoing
wait(ms); wait(sleepingMS);
return MY_SLEEP_NOT_POSSIBLE; return MY_SLEEP_NOT_POSSIBLE;
} }
#endif #endif
@@ -473,27 +470,25 @@ int8_t _sleep(const uint32_t ms, const bool smartSleep, const uint8_t interrupt1
(void)mode2; (void)mode2;
debug(PSTR("!MCO:SLP:REP\n")); // sleeping not possible, repeater feature enabled debug(PSTR("!MCO:SLP:REP\n")); // sleeping not possible, repeater feature enabled
wait(ms); wait(sleepingMS);
return MY_SLEEP_NOT_POSSIBLE; return MY_SLEEP_NOT_POSSIBLE;
#else #else
uint32_t sleepingTime = ms; uint32_t sleepingTimeMS = sleepingMS;
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
// Do not sleep if transport not ready // Do not sleep if transport not ready
if (!isTransportReady()) { if (!isTransportReady()) {
debug(PSTR("!MCO:SLP:TNR\n")); // sleeping not possible, transport not ready debug(PSTR("!MCO:SLP:TNR\n")); // sleeping not possible, transport not ready
uint32_t sleepEnter = hwMillis(); const uint32_t sleepEnterMS = hwMillis();
uint32_t sleepDelta = 0; uint32_t sleepDeltaMS = 0;
while (!isTransportReady() && (sleepDelta < sleepingTime) && (sleepDelta < MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS)) { while (!isTransportReady() && (sleepDeltaMS < sleepingTimeMS) && (sleepDeltaMS < MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS)) {
_process(); _process();
#if defined(ARDUINO_ARCH_ESP8266) yield();
yield(); sleepDeltaMS = hwMillis() - sleepEnterMS;
#endif
sleepDelta = hwMillis() - sleepEnter;
} }
// sleep remainder // sleep remainder
if (sleepDelta < sleepingTime) { if (sleepDeltaMS < sleepingTimeMS) {
sleepingTime -= sleepDelta; // calculate remaining sleeping time sleepingTimeMS -= sleepDeltaMS; // calculate remaining sleeping time
debug(PSTR("MCO:SLP:MS=%lu\n"), sleepingTime); debug(PSTR("MCO:SLP:MS=%lu\n"), sleepingTimeMS);
} }
else { else {
// no sleeping time left // no sleeping time left
@@ -504,65 +499,65 @@ int8_t _sleep(const uint32_t ms, const bool smartSleep, const uint8_t interrupt1
if (smartSleep) { if (smartSleep) {
// notify controller about going to sleep // notify controller about going to sleep
sendHeartbeat(); (void)sendHeartbeat();
wait(MY_SMART_SLEEP_WAIT_DURATION_MS); // listen for incoming messages wait(MY_SMART_SLEEP_WAIT_DURATION_MS); // listen for incoming messages
} }
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
debug(PSTR("MCO:SLP:TPD\n")); // sleep, power down transport debug(PSTR("MCO:SLP:TPD\n")); // sleep, power down transport
transportPowerDown(); transportPowerDown();
#endif #endif
setIndication(INDICATION_SLEEP); setIndication(INDICATION_SLEEP);
int8_t res = MY_SLEEP_NOT_POSSIBLE; // default int8_t result = MY_SLEEP_NOT_POSSIBLE; // default
if (interrupt1 != INTERRUPT_NOT_DEFINED && interrupt2 != INTERRUPT_NOT_DEFINED) { if (interrupt1 != INTERRUPT_NOT_DEFINED && interrupt2 != INTERRUPT_NOT_DEFINED) {
// both IRQs // both IRQs
res = hwSleep(interrupt1, mode1, interrupt2, mode2, sleepingTime); result = hwSleep(interrupt1, mode1, interrupt2, mode2, sleepingTimeMS);
} }
else if (interrupt1 != INTERRUPT_NOT_DEFINED && interrupt2 == INTERRUPT_NOT_DEFINED) { else if (interrupt1 != INTERRUPT_NOT_DEFINED && interrupt2 == INTERRUPT_NOT_DEFINED) {
// one IRQ // one IRQ
res = hwSleep(interrupt1, mode1, sleepingTime); result = hwSleep(interrupt1, mode1, sleepingTimeMS);
} }
else if (interrupt1 == INTERRUPT_NOT_DEFINED && interrupt2 == INTERRUPT_NOT_DEFINED) { else if (interrupt1 == INTERRUPT_NOT_DEFINED && interrupt2 == INTERRUPT_NOT_DEFINED) {
// no IRQ // no IRQ
res = hwSleep(sleepingTime); result = hwSleep(sleepingTimeMS);
} }
setIndication(INDICATION_WAKEUP); setIndication(INDICATION_WAKEUP);
debug(PSTR("MCO:SLP:WUP=%d\n"), res); // sleep wake-up debug(PSTR("MCO:SLP:WUP=%d\n"), result); // sleep wake-up
return res; return result;
#endif #endif
} }
// sleep functions // sleep functions
int8_t sleep(const uint32_t ms, const bool smartSleep) { int8_t sleep(const uint32_t sleepingMS, const bool smartSleep) {
return _sleep(ms, smartSleep); return _sleep(sleepingMS, smartSleep);
} }
int8_t sleep(const uint8_t interrupt, const uint8_t mode, const uint32_t ms, const bool smartSleep) { int8_t sleep(const uint8_t interrupt, const uint8_t mode, const uint32_t sleepingMS, const bool smartSleep) {
return _sleep(ms, smartSleep, interrupt, mode); return _sleep(sleepingMS, smartSleep, interrupt, mode);
} }
int8_t sleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t ms, const bool smartSleep) { int8_t sleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t sleepingMS, const bool smartSleep) {
return _sleep(ms, smartSleep, interrupt1, mode1, interrupt2, mode2); return _sleep(sleepingMS, smartSleep, interrupt1, mode1, interrupt2, mode2);
} }
// deprecated smartSleep() functions // deprecated smartSleep() functions
int8_t smartSleep(const uint32_t ms) { int8_t smartSleep(const uint32_t sleepingMS) {
// compatibility // compatibility
return _sleep(ms, true); return _sleep(sleepingMS, true);
} }
int8_t smartSleep(const uint8_t interrupt, const uint8_t mode, const uint32_t ms) { int8_t smartSleep(const uint8_t interrupt, const uint8_t mode, const uint32_t sleepingMS) {
// compatibility // compatibility
return _sleep(ms, true, interrupt, mode); return _sleep(sleepingMS, true, interrupt, mode);
} }
int8_t smartSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t ms) { int8_t smartSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t sleepingMS) {
// compatibility // compatibility
return _sleep(ms, true, interrupt1, mode1, interrupt2, mode2); return _sleep(sleepingMS, true, interrupt1, mode1, interrupt2, mode2);
} }
@@ -573,11 +568,9 @@ void nodeLock(const char* str) {
while (1) { while (1) {
setIndication(INDICATION_ERR_LOCKED); setIndication(INDICATION_ERR_LOCKED);
debug(PSTR("MCO:NLK:NODE LOCKED. TO UNLOCK, GND PIN %d AND RESET\n"), MY_NODE_UNLOCK_PIN); debug(PSTR("MCO:NLK:NODE LOCKED. TO UNLOCK, GND PIN %d AND RESET\n"), MY_NODE_UNLOCK_PIN);
#if defined(ARDUINO_ARCH_ESP8266) yield();
yield(); (void)_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID,C_INTERNAL, I_LOCKED).set(str));
#endif #if defined(MY_SENSOR_NETWORK)
_sendRoute(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID,C_INTERNAL, I_LOCKED, false).set(str));
#if defined(MY_RADIO_FEATURE)
transportPowerDown(); transportPowerDown();
debug(PSTR("MCO:NLK:TPD\n")); // power down transport debug(PSTR("MCO:NLK:TPD\n")); // power down transport
#endif #endif
+51 -46
View File
@@ -122,17 +122,17 @@ extern MyMessage _msgTmp; //!< Buffer for temporary messages (acks and nonc
/** /**
* Return this nodes id. * Return this nodes id.
*/ */
uint8_t getNodeId(); uint8_t getNodeId(void);
/** /**
* Return the parent node id. * Return the parent node id.
*/ */
uint8_t getParentNodeId(); uint8_t getParentNodeId(void);
/** /**
* Sends node information to the gateway. * Sends node information to the gateway.
*/ */
void presentNode(); void presentNode(void);
/** /**
* Each node must present all attached sensors before any values can be handled correctly by the controller. * Each node must present all attached sensors before any values can be handled correctly by the controller.
@@ -143,17 +143,18 @@ void presentNode();
* @param description A textual description of the sensor. * @param description A textual description of the sensor.
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack. * @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
* @param description A textual description of the sensor. * @param description A textual description of the sensor.
* @return true Returns true if message reached the first stop on its way to destination.
*/ */
void present(uint8_t sensorId, uint8_t sensorType, const char *description="", bool ack=false); bool present(const uint8_t sensorId, const uint8_t sensorType, const char *description="", const bool ack = false);
/** /**
* Sends sketch meta information to the gateway. Not mandatory but a nice thing to do. * Sends sketch meta information to the gateway. Not mandatory but a nice thing to do.
* @param name String containing a short Sketch name or NULL if not applicable * @param name String containing a short Sketch name or NULL if not applicable
* @param version String containing a short Sketch version or NULL if not applicable * @param version String containing a short Sketch version or NULL if not applicable
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack. * @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
* * @return true Returns true if message reached the first stop on its way to destination.
*/ */
void sendSketchInfo(const char *name, const char *version, bool ack=false); bool sendSketchInfo(const char *name, const char *version, const bool ack = false);
/** /**
* Sends a message to gateway or one of the other nodes in the radio network * Sends a message to gateway or one of the other nodes in the radio network
@@ -162,22 +163,24 @@ void sendSketchInfo(const char *name, const char *version, bool ack=false);
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack. * @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
* @return true Returns true if message reached the first stop on its way to destination. * @return true Returns true if message reached the first stop on its way to destination.
*/ */
bool send(MyMessage &msg, bool ack=false); bool send(MyMessage &msg, const bool ack = false);
/** /**
* Send this nodes battery level to gateway. * Send this nodes battery level to gateway.
* @param level Level between 0-100(%) * @param level Level between 0-100(%)
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack. * @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
* * @return true Returns true if message reached the first stop on its way to destination.
*/ */
void sendBatteryLevel(uint8_t level, bool ack=false); bool sendBatteryLevel(const uint8_t level, const bool ack = false);
/** /**
* Send a heartbeat message (I'm alive!) to the gateway/controller. * Send a heartbeat message (I'm alive!) to the gateway/controller.
* The payload will be an incremental 16 bit integer value starting at 1 when sensor is powered on. * The payload will be an incremental 16 bit integer value starting at 1 when sensor is powered on.
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
* @return true Returns true if message reached the first stop on its way to destination.
*/ */
void sendHeartbeat(void); bool sendHeartbeat(const bool ack = false);
/** /**
* Requests a value from gateway or some other sensor in the radio network. * Requests a value from gateway or some other sensor in the radio network.
@@ -186,19 +189,21 @@ void sendHeartbeat(void);
* @param childSensorId The unique child id for the different sensors connected to this Arduino. 0-254. * @param childSensorId The unique child id for the different sensors connected to this Arduino. 0-254.
* @param variableType The variableType to fetch * @param variableType The variableType to fetch
* @param destination The nodeId of other node in radio network. Default is gateway * @param destination The nodeId of other node in radio network. Default is gateway
* @return true Returns true if message reached the first stop on its way to destination.
*/ */
void request(uint8_t childSensorId, uint8_t variableType, uint8_t destination=GATEWAY_ADDRESS); bool request(const uint8_t childSensorId, const uint8_t variableType, const uint8_t destination = GATEWAY_ADDRESS);
/** /**
* Requests time from controller. Answer will be delivered to receiveTime function in sketch. * Requests time from controller. Answer will be delivered to receiveTime function in sketch.
* * @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
* @return true Returns true if message reached the first stop on its way to destination.
*/ */
void requestTime(); bool requestTime(const bool ack = false);
/** /**
* Returns the most recent node configuration received from controller * Returns the most recent node configuration received from controller
*/ */
ControllerConfig getConfig(); ControllerConfig getConfig(void);
/** /**
* Save a state (in local EEPROM). Good for actuators to "remember" state between * Save a state (in local EEPROM). Good for actuators to "remember" state between
@@ -210,7 +215,7 @@ ControllerConfig getConfig();
* @param pos The position to store value in (0-255) * @param pos The position to store value in (0-255)
* @param value to store in position * @param value to store in position
*/ */
void saveState(uint8_t pos, uint8_t value); void saveState(const uint8_t pos, const uint8_t value);
/** /**
* Load a state (from local EEPROM). * Load a state (from local EEPROM).
@@ -218,36 +223,36 @@ void saveState(uint8_t pos, uint8_t value);
* @param pos The position to fetch value from (0-255) * @param pos The position to fetch value from (0-255)
* @return Value to store in position * @return Value to store in position
*/ */
uint8_t loadState(uint8_t pos); uint8_t loadState(const uint8_t pos);
/** /**
* Wait for a specified amount of time to pass. Keeps process()ing. * Wait for a specified amount of time to pass. Keeps process()ing.
* This does not power-down the radio nor the Arduino. * This does not power-down the radio nor the Arduino.
* Because this calls process() in a loop, it is a good way to wait * Because this calls process() in a loop, it is a good way to wait
* in your loop() on a repeater node or sensor that listens to messages. * in your loop() on a repeater node or sensor that listens to messages.
* @param ms Number of milliseconds to sleep. * @param waitingMS Number of milliseconds to wait.
*/ */
void wait(unsigned long ms); void wait(const uint32_t waitingMS);
/** /**
* Wait for a specified amount of time to pass or until specified message received. Keeps process()ing. * Wait for a specified amount of time to pass or until specified message received. Keeps process()ing.
* This does not power-down the radio nor the Arduino. * This does not power-down the radio nor the Arduino.
* Because this calls process() in a loop, it is a good way to wait * Because this calls process() in a loop, it is a good way to wait
* in your loop() on a repeater node or sensor that listens to messages. * in your loop() on a repeater node or sensor that listens to messages.
* @param ms Number of milliseconds to sleep. * @param waitingMS Number of milliseconds to wait.
* @param cmd Command of incoming message. * @param cmd Command of incoming message.
* @param msgtype Message type. * @param msgtype Message type.
* @return True if specified message received * @return True if specified message received
*/ */
bool wait(unsigned long ms, uint8_t cmd, uint8_t msgtype); bool wait(const uint32_t waitingMS, const uint8_t cmd, const uint8_t msgtype);
/** /**
* Sleep (PowerDownMode) the MCU and radio. Wake up on timer. * Sleep (PowerDownMode) the MCU and radio. Wake up on timer.
* @param ms Number of milliseconds to sleep. * @param sleepingMS Number of milliseconds to sleep.
* @param smartSleep Set True if sending heartbeat and process incoming messages before going to sleep. * @param smartSleep Set True if sending heartbeat and process incoming messages before going to sleep.
* @return @ref MY_WAKE_UP_BY_TIMER if timer woke it up, @ref MY_SLEEP_NOT_POSSIBLE if not possible (e.g. ongoing FW update) * @return @ref MY_WAKE_UP_BY_TIMER if timer woke it up, @ref MY_SLEEP_NOT_POSSIBLE if not possible (e.g. ongoing FW update)
*/ */
int8_t sleep(const uint32_t ms, const bool smartSleep = false); int8_t sleep(const uint32_t sleepingMS, const bool smartSleep = false);
/** /**
* Sleep (PowerDownMode) the MCU and radio. Wake up on timer or pin change. * Sleep (PowerDownMode) the MCU and radio. Wake up on timer or pin change.
@@ -255,11 +260,11 @@ int8_t sleep(const uint32_t ms, const bool smartSleep = false);
* is assigned to what interrupt. On Nano/Pro Mini: 0=Pin2, 1=Pin3 * is assigned to what interrupt. On Nano/Pro Mini: 0=Pin2, 1=Pin3
* @param interrupt Interrupt that should trigger the wakeup * @param interrupt Interrupt that should trigger the wakeup
* @param mode RISING, FALLING, CHANGE * @param mode RISING, FALLING, CHANGE
* @param ms Number of milliseconds to sleep or 0 to sleep forever * @param sleepingMS Number of milliseconds to sleep or 0 to sleep forever
* @param smartSleep Set True if sending heartbeat and process incoming messages before going to sleep * @param smartSleep Set True if sending heartbeat and process incoming messages before going to sleep
* @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update) * @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update)
*/ */
int8_t sleep(const uint8_t interrupt, const uint8_t mode, const uint32_t ms=0, const bool smartSleep = false); int8_t sleep(const uint8_t interrupt, const uint8_t mode, const uint32_t sleepingMS = 0, const bool smartSleep = false);
/** /**
* Sleep (PowerDownMode) the MCU and radio. Wake up on timer or pin change for two separate interrupts. * Sleep (PowerDownMode) the MCU and radio. Wake up on timer or pin change for two separate interrupts.
@@ -269,20 +274,20 @@ int8_t sleep(const uint8_t interrupt, const uint8_t mode, const uint32_t ms=0, c
* @param mode1 Mode for first interrupt (RISING, FALLING, CHANGE) * @param mode1 Mode for first interrupt (RISING, FALLING, CHANGE)
* @param interrupt2 Second interrupt that should trigger the wakeup * @param interrupt2 Second interrupt that should trigger the wakeup
* @param mode2 Mode for second interrupt (RISING, FALLING, CHANGE) * @param mode2 Mode for second interrupt (RISING, FALLING, CHANGE)
* @param ms Number of milliseconds to sleep or 0 to sleep forever * @param sleepingMS Number of milliseconds to sleep or 0 to sleep forever
* @param smartSleep Set True if sending heartbeat and process incoming messages before going to sleep. * @param smartSleep Set True if sending heartbeat and process incoming messages before going to sleep.
* @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update) * @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update)
*/ */
int8_t sleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t ms=0, const bool smartSleep = false); int8_t sleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t sleepingMS = 0, const bool smartSleep = false);
/** /**
* \deprecated Use sleep(ms, true) instead * \deprecated Use sleep(ms, true) instead
* Same as sleep(), send heartbeat and process incoming messages before going to sleep. * Same as sleep(), send heartbeat and process incoming messages before going to sleep.
* Specify the time to wait for incoming messages by defining MY_SMART_SLEEP_WAIT_DURATION to a time (ms). * Specify the time to wait for incoming messages by defining MY_SMART_SLEEP_WAIT_DURATION to a time (ms).
* @param ms Number of milliseconds to sleep. * @param sleepingMS Number of milliseconds to sleep.
* @return @ref MY_WAKE_UP_BY_TIMER if timer woke it up, @ref MY_SLEEP_NOT_POSSIBLE if not possible (e.g. ongoing FW update) * @return @ref MY_WAKE_UP_BY_TIMER if timer woke it up, @ref MY_SLEEP_NOT_POSSIBLE if not possible (e.g. ongoing FW update)
*/ */
int8_t smartSleep(const uint32_t ms); int8_t smartSleep(const uint32_t sleepingMS);
/** /**
* \deprecated Use sleep(interrupt, mode, ms, true) instead * \deprecated Use sleep(interrupt, mode, ms, true) instead
@@ -290,10 +295,10 @@ int8_t smartSleep(const uint32_t ms);
* Specify the time to wait for incoming messages by defining MY_SMART_SLEEP_WAIT_DURATION to a time (ms). * Specify the time to wait for incoming messages by defining MY_SMART_SLEEP_WAIT_DURATION to a time (ms).
* @param interrupt Interrupt that should trigger the wakeup * @param interrupt Interrupt that should trigger the wakeup
* @param mode RISING, FALLING, CHANGE * @param mode RISING, FALLING, CHANGE
* @param ms Number of milliseconds to sleep or 0 to sleep forever * @param sleepingMS Number of milliseconds to sleep or 0 to sleep forever
* @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update) * @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update)
*/ */
int8_t smartSleep(const uint8_t interrupt, const uint8_t mode, const uint32_t ms = 0); int8_t smartSleep(const uint8_t interrupt, const uint8_t mode, const uint32_t sleepingMS = 0);
/** /**
* \deprecated Use sleep(interrupt1, mode1, interrupt2, mode2, ms, true) instead * \deprecated Use sleep(interrupt1, mode1, interrupt2, mode2, ms, true) instead
@@ -303,16 +308,16 @@ int8_t smartSleep(const uint8_t interrupt, const uint8_t mode, const uint32_t ms
* @param mode1 Mode for first interrupt (RISING, FALLING, CHANGE) * @param mode1 Mode for first interrupt (RISING, FALLING, CHANGE)
* @param interrupt2 Second interrupt that should trigger the wakeup * @param interrupt2 Second interrupt that should trigger the wakeup
* @param mode2 Mode for second interrupt (RISING, FALLING, CHANGE) * @param mode2 Mode for second interrupt (RISING, FALLING, CHANGE)
* @param ms Number of milliseconds to sleep or 0 to sleep forever * @param sleepingMS Number of milliseconds to sleep or 0 to sleep forever
* @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update) * @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update)
*/ */
int8_t smartSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t ms=0); int8_t smartSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2, const uint32_t sleepingMS = 0);
/** /**
* Sleep (PowerDownMode) the MCU and radio. Wake up on timer or pin change for two separate interrupts. * Sleep (PowerDownMode) the MCU and radio. Wake up on timer or pin change for two separate interrupts.
* See: http://arduino.cc/en/Reference/attachInterrupt for details on modes and which pin * See: http://arduino.cc/en/Reference/attachInterrupt for details on modes and which pin
* is assigned to what interrupt. On Nano/Pro Mini: 0=Pin2, 1=Pin3 * is assigned to what interrupt. On Nano/Pro Mini: 0=Pin2, 1=Pin3
* @param ms Number of milliseconds to sleep or 0 to sleep forever * @param sleepingMS Number of milliseconds to sleep or 0 to sleep forever
* @param interrupt1 (optional) First interrupt that should trigger the wakeup * @param interrupt1 (optional) First interrupt that should trigger the wakeup
* @param mode1 (optional) Mode for first interrupt (RISING, FALLING, CHANGE) * @param mode1 (optional) Mode for first interrupt (RISING, FALLING, CHANGE)
* @param interrupt2 (optional) Second interrupt that should trigger the wakeup * @param interrupt2 (optional) Second interrupt that should trigger the wakeup
@@ -320,7 +325,7 @@ int8_t smartSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t i
* @param smartSleep (optional) Set True if sending heartbeat and process incoming messages before going to sleep. * @param smartSleep (optional) Set True if sending heartbeat and process incoming messages before going to sleep.
* @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update) * @return Interrupt number if wake up was triggered by pin change, @ref MY_WAKE_UP_BY_TIMER if wake up was triggered by timer, @ref MY_SLEEP_NOT_POSSIBLE if sleep was not possible (e.g. ongoing FW update)
*/ */
int8_t _sleep(const uint32_t ms, const bool smartSleep = false, const uint8_t interrupt1 = INTERRUPT_NOT_DEFINED, const uint8_t mode1 = MODE_NOT_DEFINED, const uint8_t interrupt2 = INTERRUPT_NOT_DEFINED, const uint8_t mode2 = MODE_NOT_DEFINED); int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep = false, const uint8_t interrupt1 = INTERRUPT_NOT_DEFINED, const uint8_t mode1 = MODE_NOT_DEFINED, const uint8_t interrupt2 = INTERRUPT_NOT_DEFINED, const uint8_t mode2 = MODE_NOT_DEFINED);
#ifdef MY_NODE_LOCK_FEATURE #ifdef MY_NODE_LOCK_FEATURE
@@ -343,7 +348,7 @@ void nodeLock(const char* str);
/** /**
* @brief Node initialisation * @brief Node initialisation
*/ */
void _begin(); void _begin(void);
/** /**
* @brief Main framework process * @brief Main framework process
*/ */
@@ -352,19 +357,19 @@ void _process(void);
* @brief Processes internal messages * @brief Processes internal messages
* @return True if received message requires further processing * @return True if received message requires further processing
*/ */
bool _processInternalMessages(); bool _processInternalMessages(void);
/** /**
* @brief Puts node to a infinite loop if unrecoverable situation detected * @brief Puts node to a infinite loop if unrecoverable situation detected
*/ */
void _infiniteLoop(); void _infiniteLoop(void);
/** /**
* @brief Handles registration request * @brief Handles registration request
*/ */
void _registerNode(); void _registerNode(void);
/** /**
* @brief Sends message according to routing table * @brief Sends message according to routing table
* @param message * @param message
* @return True if message successfully sent to next node * @return true Returns true if message reached the first stop on its way to destination.
*/ */
bool _sendRoute(MyMessage &message); bool _sendRoute(MyMessage &message);
@@ -380,33 +385,33 @@ void receiveTime(unsigned long) __attribute__((weak));
/** /**
* @brief Node presenation * @brief Node presenation
*/ */
void presentation() __attribute__((weak)); void presentation(void) __attribute__((weak));
/** /**
* @brief Called before node initialises * @brief Called before node initialises
*/ */
void before() __attribute__((weak)); void before(void) __attribute__((weak));
/** /**
* @brief Called before any hwInitialization is done * @brief Called before any hwInitialization is done
*/ */
void preHwInit() __attribute__((weak)); void preHwInit(void) __attribute__((weak));
/** /**
* @brief Called after node initialises but before main loop * @brief Called after node initialises but before main loop
*/ */
void setup() __attribute__((weak)); void setup(void) __attribute__((weak));
/** /**
* @brief Main loop * @brief Main loop
*/ */
void loop() __attribute__((weak)); void loop(void) __attribute__((weak));
// Inline function and macros // Inline function and macros
static inline MyMessage& build(MyMessage &msg, const uint8_t destination, const uint8_t sensor, const uint8_t command, const uint8_t type, const bool enableAck) { static inline MyMessage& build(MyMessage &msg, const uint8_t destination, const uint8_t sensor, const uint8_t command, const uint8_t type, const bool ack = false) {
msg.sender = _nc.nodeId; msg.sender = _nc.nodeId;
msg.destination = destination; msg.destination = destination;
msg.sensor = sensor; msg.sensor = sensor;
msg.type = type; msg.type = type;
mSetCommand(msg,command); mSetCommand(msg,command);
mSetRequestAck(msg,enableAck); mSetRequestAck(msg,ack);
mSetAck(msg,false); mSetAck(msg,false);
return msg; return msg;
} }
+3 -3
View File
@@ -106,7 +106,7 @@ static bool skipSign(MyMessage &msg) {
// Helper to prepare a signing presentation message // Helper to prepare a signing presentation message
static void prepareSigningPresentation(MyMessage &msg, uint8_t destination) { static void prepareSigningPresentation(MyMessage &msg, uint8_t destination) {
// Only supports version 1 for now // Only supports version 1 for now
build(msg, destination, NODE_SENSOR_ID, C_INTERNAL, I_SIGNING_PRESENTATION, false).set(""); (void)build(msg, destination, NODE_SENSOR_ID, C_INTERNAL, I_SIGNING_PRESENTATION).set("");
mSetLength(msg, 2); mSetLength(msg, 2);
mSetPayloadType(msg, P_CUSTOM); // displayed as hex mSetPayloadType(msg, P_CUSTOM); // displayed as hex
msg.data[0] = SIGNING_PRESENTATION_VERSION_1; msg.data[0] = SIGNING_PRESENTATION_VERSION_1;
@@ -188,7 +188,7 @@ bool signerProcessInternal(MyMessage &msg) {
#if defined(MY_SIGNING_ATSHA204) #if defined(MY_SIGNING_ATSHA204)
if (signerAtsha204GetNonce(msg)) { if (signerAtsha204GetNonce(msg)) {
#endif #endif
if (!_sendRoute(build(msg, msg.sender, NODE_SENSOR_ID, C_INTERNAL, I_NONCE_RESPONSE, false))) { if (!_sendRoute(build(msg, msg.sender, NODE_SENSOR_ID, C_INTERNAL, I_NONCE_RESPONSE))) {
SIGN_DEBUG(PSTR("Failed to transmit nonce!\n")); SIGN_DEBUG(PSTR("Failed to transmit nonce!\n"));
} else { } else {
SIGN_DEBUG(PSTR("Transmitted nonce\n")); SIGN_DEBUG(PSTR("Transmitted nonce\n"));
@@ -314,7 +314,7 @@ bool signerSignMsg(MyMessage &msg) {
} else { } else {
// Send nonce-request // Send nonce-request
_signingNonceStatus=SIGN_WAITING_FOR_NONCE; _signingNonceStatus=SIGN_WAITING_FOR_NONCE;
if (!_sendRoute(build(_msgSign, msg.destination, msg.sensor, C_INTERNAL, I_NONCE_REQUEST, false).set(""))) { if (!_sendRoute(build(_msgSign, msg.destination, msg.sensor, C_INTERNAL, I_NONCE_REQUEST).set(""))) {
SIGN_DEBUG(PSTR("Failed to transmit nonce request!\n")); SIGN_DEBUG(PSTR("Failed to transmit nonce request!\n"));
return false; return false;
} }
+12 -12
View File
@@ -134,7 +134,7 @@ void stParentTransition(void) {
_nc.distance = DISTANCE_INVALID; // Set distance to max and invalidate parent node ID _nc.distance = DISTANCE_INVALID; // Set distance to max and invalidate parent node ID
_nc.parentNodeId = AUTO; _nc.parentNodeId = AUTO;
// Broadcast find parent request // Broadcast find parent request
(void)transportRouteMessage(build(_msgTmp, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_REQUEST, false).set("")); (void)transportRouteMessage(build(_msgTmp, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_REQUEST).set(""));
#endif #endif
} }
@@ -181,7 +181,7 @@ void stIDTransition(void) {
// send ID request // send ID request
setIndication(INDICATION_REQ_NODEID); setIndication(INDICATION_REQ_NODEID);
TRANSPORT_DEBUG(PSTR("TSM:ID:REQ\n")); // request node ID TRANSPORT_DEBUG(PSTR("TSM:ID:REQ\n")); // request node ID
(void)transportRouteMessage(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST, false).set("")); (void)transportRouteMessage(build(_msgTmp, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST).set(""));
} }
} }
@@ -214,7 +214,7 @@ void stUplinkTransition(void) {
setIndication(INDICATION_CHECK_UPLINK); setIndication(INDICATION_CHECK_UPLINK);
_transportSM.pingResponse = INVALID_HOPS; _transportSM.pingResponse = INVALID_HOPS;
_transportSM.pingActive = true; _transportSM.pingActive = true;
(void)transportRouteMessage(build(_msgTmp,GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_PING, false).set((uint8_t)1)); (void)transportRouteMessage(build(_msgTmp,GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_PING).set((uint8_t)1));
#endif #endif
} }
@@ -265,7 +265,7 @@ void stReadyUpdate(void) {
if (hwMillis() - _lastNetworkDiscovery > MY_TRANSPORT_DISCOVERY_INTERVAL_MS) { if (hwMillis() - _lastNetworkDiscovery > MY_TRANSPORT_DISCOVERY_INTERVAL_MS) {
_lastNetworkDiscovery = hwMillis(); _lastNetworkDiscovery = hwMillis();
TRANSPORT_DEBUG(PSTR("TSM:READY:NWD REQ\n")); // send transport network discovery TRANSPORT_DEBUG(PSTR("TSM:READY:NWD REQ\n")); // send transport network discovery
(void)transportRouteMessage(build(_msgTmp, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DISCOVER_REQUEST, false).set("")); (void)transportRouteMessage(build(_msgTmp, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_DISCOVER_REQUEST).set(""));
} }
#else #else
if (_transportSM.failedUplinkTransmissions > MY_TRANSPORT_MAX_TX_FAILURES) { if (_transportSM.failedUplinkTransmissions > MY_TRANSPORT_MAX_TX_FAILURES) {
@@ -298,7 +298,7 @@ void stFailureTransition(void) {
_transportSM.uplinkOk = false; // uplink nok _transportSM.uplinkOk = false; // uplink nok
_transportSM.transportActive = false; // transport inactive _transportSM.transportActive = false; // transport inactive
setIndication(INDICATION_ERR_INIT_TRANSPORT); setIndication(INDICATION_ERR_INIT_TRANSPORT);
#if defined(MY_RADIO_FEATURE) #if defined(MY_SENSOR_NETWORK)
TRANSPORT_DEBUG(PSTR("TSM:FAIL:PDT\n")); // power down transport, no need until re-init TRANSPORT_DEBUG(PSTR("TSM:FAIL:PDT\n")); // power down transport, no need until re-init
transportPowerDown(); transportPowerDown();
#endif #endif
@@ -344,11 +344,11 @@ bool isTransportSearchingParent(void) {
} }
bool isMessageReceived(void) { bool isMessageReceived(void) {
return _transportSM.MsgReceived; return _transportSM.msgReceived;
} }
void resetMessageReceived(void) { void resetMessageReceived(void) {
_transportSM.MsgReceived = false; _transportSM.msgReceived = false;
} }
@@ -507,7 +507,7 @@ uint8_t transportPingNode(const uint8_t targetId) {
else { else {
_transportSM.pingActive = true; _transportSM.pingActive = true;
_transportSM.pingResponse = INVALID_HOPS; _transportSM.pingResponse = INVALID_HOPS;
(void)transportRouteMessage(build(_msgTmp, targetId, NODE_SENSOR_ID, C_INTERNAL, I_PING, false).set((uint8_t)1)); (void)transportRouteMessage(build(_msgTmp, targetId, NODE_SENSOR_ID, C_INTERNAL, I_PING).set((uint8_t)1));
// Wait for ping reply or timeout // Wait for ping reply or timeout
(void)transportWait(2000, C_INTERNAL, I_PONG); (void)transportWait(2000, C_INTERNAL, I_PONG);
} }
@@ -579,7 +579,7 @@ void transportProcessMessage(void) {
#endif #endif
// set message received flag // set message received flag
_transportSM.MsgReceived = true; _transportSM.msgReceived = true;
// Is message addressed to this node? // Is message addressed to this node?
if (destination == _nc.nodeId) { if (destination == _nc.nodeId) {
@@ -647,7 +647,7 @@ void transportProcessMessage(void) {
// delay for fast GW and slow nodes // delay for fast GW and slow nodes
delay(5); delay(5);
#endif #endif
(void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_PONG, false).set((uint8_t)1)); (void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_PONG).set((uint8_t)1));
return; // no further processing required return; // no further processing required
} }
if (type == I_PONG) { if (type == I_PONG) {
@@ -700,7 +700,7 @@ void transportProcessMessage(void) {
TRANSPORT_DEBUG(PSTR("TSF:MSG:GWL OK\n")); // GW uplink ok TRANSPORT_DEBUG(PSTR("TSF:MSG:GWL OK\n")); // GW uplink ok
// random delay minimizes collisions // random delay minimizes collisions
delay(hwMillis() & 0x3ff); delay(hwMillis() & 0x3ff);
(void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(_nc.distance)); (void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE).set(_nc.distance));
} }
else { else {
TRANSPORT_DEBUG(PSTR("!TSF:MSG:GWL FAIL\n")); // GW uplink fail, do not respond to parent request TRANSPORT_DEBUG(PSTR("!TSF:MSG:GWL FAIL\n")); // GW uplink fail, do not respond to parent request
@@ -718,7 +718,7 @@ void transportProcessMessage(void) {
if (last == _nc.parentNodeId) { if (last == _nc.parentNodeId) {
// random wait to minimize collisions // random wait to minimize collisions
delay(hwMillis() & 0x3ff); delay(hwMillis() & 0x3ff);
(void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_DISCOVER_RESPONSE, false).set(_nc.parentNodeId)); (void)transportRouteMessage(build(_msgTmp, sender, NODE_SENSOR_ID, C_INTERNAL, I_DISCOVER_RESPONSE).set(_nc.parentNodeId));
// no return here (for fwd if repeater) // no return here (for fwd if repeater)
} }
} }
+1 -1
View File
@@ -236,7 +236,7 @@ typedef struct {
// 8 bits // 8 bits
uint8_t failedUplinkTransmissions : 4; //!< counter failed uplink transmissions (max 15) uint8_t failedUplinkTransmissions : 4; //!< counter failed uplink transmissions (max 15)
uint8_t failureCounter : 3; //!< counter for TSM failures (max 7) uint8_t failureCounter : 3; //!< counter for TSM failures (max 7)
bool MsgReceived : 1; //!< flag message received bool msgReceived : 1; //!< flag message received
// 8 bits // 8 bits
uint8_t pingResponse; //!< stores I_PONG hops uint8_t pingResponse; //!< stores I_PONG hops
} transportSM; } transportSM;