Added {} to control structures for clarity (#600)
This commit is contained in:
@@ -437,8 +437,9 @@ void gatewayTransportRenewIP()
|
||||
unsigned long now = hwMillis();
|
||||
|
||||
// http://playground.arduino.cc/Code/TimingRollover
|
||||
if ((long)(now - next_time) < 0)
|
||||
if ((long)(now - next_time) < 0) {
|
||||
return;
|
||||
}
|
||||
if (Ethernet.maintain() & ~(0x06)) {
|
||||
debug(PSTR("IP was not renewed correctly\n"));
|
||||
/* Error occured -> IP was not renewed */
|
||||
|
||||
@@ -49,8 +49,9 @@ static bool _MQTT_available = false;
|
||||
static MyMessage _MQTT_msg;
|
||||
|
||||
bool gatewayTransportSend(MyMessage &message) {
|
||||
if (!_MQTT_client.connected())
|
||||
if (!_MQTT_client.connected()) {
|
||||
return false;
|
||||
}
|
||||
setIndication(INDICATION_GW_TX);
|
||||
char *topic = protocolFormatMQTTTopic(MY_MQTT_PUBLISH_TOPIC_PREFIX, message);
|
||||
debug(PSTR("Sending message on topic: %s\n"), topic);
|
||||
@@ -144,15 +145,16 @@ bool gatewayTransportInit() {
|
||||
}
|
||||
|
||||
bool gatewayTransportAvailable() {
|
||||
if (_MQTT_connecting)
|
||||
if (_MQTT_connecting) {
|
||||
return false;
|
||||
|
||||
}
|
||||
//keep lease on dhcp address
|
||||
//Ethernet.maintain();
|
||||
if (!_MQTT_client.connected()) {
|
||||
//reinitialise client
|
||||
if (gatewayTransportConnect())
|
||||
if (gatewayTransportConnect()) {
|
||||
reconnectMQTT();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
_MQTT_client.loop();
|
||||
|
||||
+17
-8
@@ -127,8 +127,12 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
|
||||
// attach interrupts
|
||||
_wakeUp1Interrupt = interrupt1;
|
||||
_wakeUp2Interrupt = interrupt2;
|
||||
if (interrupt1 != INVALID_INTERRUPT_NUM) attachInterrupt(interrupt1, wakeUp1, mode1);
|
||||
if (interrupt2 != INVALID_INTERRUPT_NUM) attachInterrupt(interrupt2, wakeUp2, mode2);
|
||||
if (interrupt1 != INVALID_INTERRUPT_NUM) {
|
||||
attachInterrupt(interrupt1, wakeUp1, mode1);
|
||||
}
|
||||
if (interrupt2 != INVALID_INTERRUPT_NUM) {
|
||||
attachInterrupt(interrupt2, wakeUp2, mode2);
|
||||
}
|
||||
|
||||
if (ms>0) {
|
||||
// sleep for defined time
|
||||
@@ -139,13 +143,18 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
|
||||
}
|
||||
|
||||
// Assure any interrupts attached, will get detached when they did not occur.
|
||||
if (interrupt1 != INVALID_INTERRUPT_NUM) detachInterrupt(interrupt1);
|
||||
if (interrupt2 != INVALID_INTERRUPT_NUM) detachInterrupt(interrupt2);
|
||||
if (interrupt1 != INVALID_INTERRUPT_NUM) {
|
||||
detachInterrupt(interrupt1);
|
||||
}
|
||||
if (interrupt2 != INVALID_INTERRUPT_NUM) {
|
||||
detachInterrupt(interrupt2);
|
||||
}
|
||||
|
||||
// Return what woke the mcu.
|
||||
int8_t ret = MY_WAKE_UP_BY_TIMER; // default: no interrupt triggered, timer wake up
|
||||
if (interruptWakeUp()) ret = static_cast<int8_t>(_wokeUpByInterrupt);
|
||||
|
||||
if (interruptWakeUp()) {
|
||||
ret = static_cast<int8_t>(_wokeUpByInterrupt);
|
||||
}
|
||||
// Clear woke-up-by-interrupt flag, so next sleeps won't return immediately.
|
||||
_wokeUpByInterrupt = INVALID_INTERRUPT_NUM;
|
||||
|
||||
@@ -167,7 +176,7 @@ uint16_t hwCPUVoltage() {
|
||||
delay(70);
|
||||
// Do conversion
|
||||
ADCSRA |= _BV(ADSC);
|
||||
while (bit_is_set(ADCSRA,ADSC));
|
||||
while (bit_is_set(ADCSRA,ADSC)) {};
|
||||
// return Vcc in mV
|
||||
return (1125300UL) / ADC;
|
||||
}
|
||||
@@ -188,7 +197,7 @@ uint16_t hwCPUFrequency() {
|
||||
// start timer1 with 1024 prescaling
|
||||
TCCR1B = _BV(CS12) | _BV(CS10);
|
||||
// wait until wdt interrupt
|
||||
while (bit_is_clear(WDTCSR,WDIF));
|
||||
while (bit_is_clear(WDTCSR,WDIF)) {};
|
||||
// stop timer
|
||||
TCCR1B = 0;
|
||||
// restore WDT settings
|
||||
|
||||
+3
-1
@@ -58,7 +58,9 @@ byte i2c_eeprom_read_byte(unsigned int eeaddress ) {
|
||||
Wire.write((int)(eeaddress & 0xFF)); // LSB
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(I2C_EEP_ADDRESS,1);
|
||||
if (Wire.available()) rdata = Wire.read();
|
||||
if (Wire.available()) {
|
||||
rdata = Wire.read();
|
||||
}
|
||||
return rdata;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ void setIndication( const indication_t ind )
|
||||
ledsBlinkErr(ind-INDICATION_ERR_START);
|
||||
}
|
||||
#endif
|
||||
if (indication)
|
||||
indication(ind);
|
||||
if (indication) {
|
||||
indication(ind);
|
||||
}
|
||||
}
|
||||
|
||||
+11
-4
@@ -51,8 +51,9 @@ inline void ledsInit()
|
||||
|
||||
void ledsProcess() {
|
||||
// Just return if it is not the time...
|
||||
if ((hwMillis() - prevTime) < LED_PROCESS_INTERVAL_MS)
|
||||
if ((hwMillis() - prevTime) < LED_PROCESS_INTERVAL_MS) {
|
||||
return;
|
||||
}
|
||||
|
||||
prevTime += LED_PROCESS_INTERVAL_MS;
|
||||
|
||||
@@ -63,19 +64,25 @@ void ledsProcess() {
|
||||
#if defined(MY_DEFAULT_RX_LED_PIN)
|
||||
state = (countRx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, state);
|
||||
if (countRx) --countRx;
|
||||
if (countRx) {
|
||||
--countRx;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MY_DEFAULT_TX_LED_PIN)
|
||||
state = (countTx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, state);
|
||||
if (countTx) --countTx;
|
||||
if (countTx) {
|
||||
--countTx;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MY_DEFAULT_ERR_LED_PIN)
|
||||
state = (countErr & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF;
|
||||
hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, state);
|
||||
if (countErr) --countErr;
|
||||
if (countErr) {
|
||||
--countErr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
+25
-2
@@ -1,3 +1,22 @@
|
||||
/**
|
||||
* 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-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.
|
||||
*/
|
||||
|
||||
// Initialize library and handle sketch functions like we want to
|
||||
|
||||
int main(void) {
|
||||
@@ -12,8 +31,12 @@ int main(void) {
|
||||
|
||||
for(;;) {
|
||||
_process(); // Process incoming data
|
||||
if (loop) loop(); // Call sketch loop
|
||||
if (serialEventRun) serialEventRun();
|
||||
if (loop) {
|
||||
loop(); // Call sketch loop
|
||||
}
|
||||
if (serialEventRun) {
|
||||
serialEventRun();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
+20
-1
@@ -1,3 +1,21 @@
|
||||
/**
|
||||
* 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-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.
|
||||
*/
|
||||
|
||||
//This may be used to change user task stack size:
|
||||
//#define CONT_STACKSIZE 4096
|
||||
@@ -109,8 +127,9 @@ static void loop_task(os_event_t *events) {
|
||||
|
||||
static void do_global_ctors(void) {
|
||||
void(**p)(void) = &__init_array_end;
|
||||
while (p != &__init_array_start)
|
||||
while (p != &__init_array_start) {
|
||||
(*--p)();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void __gdb_init() {}
|
||||
|
||||
+22
-2
@@ -1,4 +1,22 @@
|
||||
// Initialize library and handle sketch functions like we want to
|
||||
/**
|
||||
* 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-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.
|
||||
*/
|
||||
// Initialize library and handle sketch functions like we want to
|
||||
|
||||
#include <stdio.h>
|
||||
#include <csignal>
|
||||
@@ -129,7 +147,9 @@ int main(int argc, char *argv[])
|
||||
|
||||
for (;;) {
|
||||
_process(); // Process incoming data
|
||||
if (loop) loop(); // Call sketch loop
|
||||
if (loop) {
|
||||
loop(); // Call sketch loop
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
+4
-2
@@ -75,10 +75,12 @@ const char* MyMessage::getString() const {
|
||||
// handles single character hex (0 - 15)
|
||||
char MyMessage::i2h(uint8_t i) const {
|
||||
uint8_t k = i & 0x0F;
|
||||
if (k <= 9)
|
||||
if (k <= 9) {
|
||||
return '0' + k;
|
||||
else
|
||||
}
|
||||
else {
|
||||
return 'A' + k - 10;
|
||||
}
|
||||
}
|
||||
|
||||
char* MyMessage::getCustomString(char *buffer) const {
|
||||
|
||||
@@ -76,7 +76,7 @@ bool firmwareOTAUpdateProcess() {
|
||||
// erase lower 32K -> max flash size for ATMEGA328
|
||||
_flash.blockErase32K(0);
|
||||
// wait until flash erased
|
||||
while ( _flash.busy() );
|
||||
while ( _flash.busy() ) {}
|
||||
_fwBlock = _fc.blocks;
|
||||
_fwUpdateOngoing = true;
|
||||
// reset flags
|
||||
@@ -96,7 +96,7 @@ bool firmwareOTAUpdateProcess() {
|
||||
// write to flash
|
||||
_flash.writeBytes( ((_fwBlock - 1) * FIRMWARE_BLOCK_SIZE) + FIRMWARE_START_OFFSET, firmwareResponse->data, FIRMWARE_BLOCK_SIZE);
|
||||
// wait until flash written
|
||||
while ( _flash.busy() );
|
||||
while ( _flash.busy() ) {}
|
||||
_fwBlock--;
|
||||
if (!_fwBlock) {
|
||||
// We're finished! Do a checksum and reboot.
|
||||
@@ -146,10 +146,12 @@ bool transportIsValidFirmware() {
|
||||
for (uint16_t i = 0; i < _fc.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);
|
||||
if (crc & 1) {
|
||||
crc = (crc >> 1) ^ 0xA001;
|
||||
}
|
||||
else {
|
||||
crc = (crc >> 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc == _fc.crc;
|
||||
|
||||
@@ -70,10 +70,12 @@ bool protocolParse(MyMessage &message, char *inputString) {
|
||||
value = str;
|
||||
// Remove trailing carriage return and newline character (if it exists)
|
||||
uint8_t lastCharacter = strlen(value)-1;
|
||||
if (value[lastCharacter] == '\r')
|
||||
if (value[lastCharacter] == '\r') {
|
||||
value[lastCharacter] = 0;
|
||||
if (value[lastCharacter] == '\n')
|
||||
}
|
||||
if (value[lastCharacter] == '\n') {
|
||||
value[lastCharacter] = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -81,16 +83,18 @@ bool protocolParse(MyMessage &message, char *inputString) {
|
||||
}
|
||||
//debug(PSTR("Received %d"), i);
|
||||
// Check for invalid input
|
||||
if (i < 5)
|
||||
if (i < 5) {
|
||||
return false;
|
||||
|
||||
}
|
||||
message.sender = GATEWAY_ADDRESS;
|
||||
message.last = GATEWAY_ADDRESS;
|
||||
mSetAck(message, false);
|
||||
if (command == C_STREAM)
|
||||
if (command == C_STREAM) {
|
||||
message.set(bvalue, blen);
|
||||
else
|
||||
}
|
||||
else {
|
||||
message.set(value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -158,8 +162,9 @@ bool protocolMQTTParse(MyMessage &message, char* topic, uint8_t* payload, unsign
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i != 6)
|
||||
if (i != 6) {
|
||||
return false;
|
||||
}
|
||||
|
||||
message.sender = GATEWAY_ADDRESS;
|
||||
message.last = GATEWAY_ADDRESS;
|
||||
@@ -190,11 +195,14 @@ bool protocolMQTTParse(MyMessage &message, char* topic, uint8_t* payload, unsign
|
||||
|
||||
uint8_t protocolH2i(char c) {
|
||||
uint8_t i = 0;
|
||||
if (c <= '9')
|
||||
if (c <= '9') {
|
||||
i += c - '0';
|
||||
else if (c >= 'a')
|
||||
}
|
||||
else if (c >= 'a') {
|
||||
i += c - 'a' + 10;
|
||||
else
|
||||
}
|
||||
else {
|
||||
i += c - 'A' + 10;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
+16
-8
@@ -72,8 +72,9 @@ void _infiniteLoop(void) {
|
||||
void _begin(void) {
|
||||
hwWatchdogReset();
|
||||
|
||||
if (preHwInit)
|
||||
if (preHwInit) {
|
||||
preHwInit();
|
||||
}
|
||||
|
||||
hwInit();
|
||||
|
||||
@@ -120,7 +121,7 @@ void _begin(void) {
|
||||
pinMode(MY_NODE_UNLOCK_PIN, INPUT_PULLUP);
|
||||
// Make a short delay so we are sure any large external nets are fully pulled
|
||||
unsigned long enter = hwMillis();
|
||||
while (hwMillis() - enter < 2);
|
||||
while (hwMillis() - enter < 2) {}
|
||||
if (digitalRead(MY_NODE_UNLOCK_PIN) == 0) {
|
||||
// Pin is grounded, reset lock counter
|
||||
hwWriteConfig(EEPROM_NODE_LOCK_COUNTER, MY_NODE_LOCK_COUNTER_MAX);
|
||||
@@ -224,9 +225,9 @@ void presentNode(void) {
|
||||
|
||||
#endif
|
||||
|
||||
if (presentation)
|
||||
if (presentation) {
|
||||
presentation();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -299,8 +300,12 @@ bool present(const uint8_t childSensorId, const uint8_t sensorType, const char *
|
||||
|
||||
bool sendSketchInfo(const char *name, const char *version, const bool ack) {
|
||||
bool result = true;
|
||||
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));
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -345,8 +350,9 @@ bool _processInternalMessages(void) {
|
||||
}
|
||||
else if (type == I_TIME) {
|
||||
// Deliver time to callback
|
||||
if (receiveTime)
|
||||
if (receiveTime) {
|
||||
receiveTime(_msg.getULong());
|
||||
}
|
||||
}
|
||||
else if (type == I_CHILDREN) {
|
||||
#if defined(MY_REPEATER_FEATURE)
|
||||
@@ -385,7 +391,9 @@ bool _processInternalMessages(void) {
|
||||
}
|
||||
else if (debug_msg == 'E') { // clear MySensors eeprom area and reboot
|
||||
(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);
|
||||
hwReboot();
|
||||
}
|
||||
|
||||
@@ -52,10 +52,12 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz);
|
||||
static char i2h(uint8_t i)
|
||||
{
|
||||
uint8_t k = i & 0x0F;
|
||||
if (k <= 9)
|
||||
if (k <= 9) {
|
||||
return '0' + k;
|
||||
else
|
||||
}
|
||||
else {
|
||||
return 'A' + k - 10;
|
||||
}
|
||||
}
|
||||
|
||||
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz) {
|
||||
@@ -132,7 +134,9 @@ bool signerAtsha204GetNonce(MyMessage &msg) {
|
||||
// Be a little fancy to handle turnover (prolong the time allowed to timeout after turnover)
|
||||
// Note that if message is "too" quick, and arrives before turnover, it will be rejected
|
||||
// but this is consider such a rare case that it is accepted and rejects are 'safe'
|
||||
if (_signing_timestamp + MY_VERIFICATION_TIMEOUT_MS < hwMillis()) _signing_timestamp = 0;
|
||||
if (_signing_timestamp + MY_VERIFICATION_TIMEOUT_MS < hwMillis()) {
|
||||
_signing_timestamp = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,10 +56,12 @@ static void signerCalculateSignature(MyMessage &msg, bool signing);
|
||||
static char i2h(uint8_t i)
|
||||
{
|
||||
uint8_t k = i & 0x0F;
|
||||
if (k <= 9)
|
||||
if (k <= 9) {
|
||||
return '0' + k;
|
||||
else
|
||||
}
|
||||
else {
|
||||
return 'A' + k - 10;
|
||||
}
|
||||
}
|
||||
|
||||
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz) {
|
||||
@@ -134,7 +136,9 @@ bool signerAtsha204SoftGetNonce(MyMessage &msg) {
|
||||
// Be a little fancy to handle turnover (prolong the time allowed to timeout after turnover)
|
||||
// Note that if message is "too" quick, and arrives before turnover, it will be rejected
|
||||
// but this is consider such a rare case that it is accepted and rejects are 'safe'
|
||||
if (_signing_timestamp + MY_VERIFICATION_TIMEOUT_MS < hwMillis()) _signing_timestamp = 0;
|
||||
if (_signing_timestamp + MY_VERIFICATION_TIMEOUT_MS < hwMillis()) {
|
||||
_signing_timestamp = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -160,9 +164,13 @@ bool signerAtsha204SoftSignMsg(MyMessage &msg) {
|
||||
if (DO_WHITELIST(msg.destination)) {
|
||||
// Salt the signature with the senders nodeId and the (hopefully) unique serial The Creator has provided
|
||||
_signing_sha256.init();
|
||||
for (int i=0; i<32; i++) _signing_sha256.write(_signing_hmac[i]);
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_hmac[i]);
|
||||
}
|
||||
_signing_sha256.write(msg.sender);
|
||||
for (int i=0; i<SHA204_SERIAL_SZ; i++) _signing_sha256.write(_signing_node_serial_info[i]);
|
||||
for (int i=0; i<SHA204_SERIAL_SZ; i++) {
|
||||
_signing_sha256.write(_signing_node_serial_info[i]);
|
||||
}
|
||||
memcpy(_signing_hmac, _signing_sha256.result(), 32);
|
||||
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), _signing_hmac, 32);
|
||||
DEBUG_SIGNING_PRINTBUF(F("Signature salted with serial"), NULL, 0);
|
||||
@@ -206,9 +214,13 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg) {
|
||||
if (_signing_whitelist[j].nodeId == msg.sender) {
|
||||
DEBUG_SIGNING_PRINTBUF(F("Sender found in whitelist"), NULL, 0);
|
||||
_signing_sha256.init();
|
||||
for (int i=0; i<32; i++) _signing_sha256.write(_signing_hmac[i]);
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_hmac[i]);
|
||||
}
|
||||
_signing_sha256.write(msg.sender);
|
||||
for (int i=0; i<SHA204_SERIAL_SZ; i++) _signing_sha256.write(_signing_whitelist[j].serial[i]);
|
||||
for (int i=0; i<SHA204_SERIAL_SZ; i++) {
|
||||
_signing_sha256.write(_signing_whitelist[j].serial[i]);
|
||||
}
|
||||
memcpy(_signing_hmac, _signing_sha256.result(), 32);
|
||||
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), _signing_hmac, 32);
|
||||
break;
|
||||
@@ -268,7 +280,9 @@ static void signerCalculateSignature(MyMessage &msg, bool signing) {
|
||||
|
||||
// Calculate message digest first
|
||||
_signing_sha256.init();
|
||||
for (int i=0; i<32; i++) _signing_sha256.write(_signing_temp_message[i]);
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_temp_message[i]);
|
||||
}
|
||||
_signing_sha256.write(0x15); // OPCODE
|
||||
_signing_sha256.write(0x02); // param1
|
||||
_signing_sha256.write(0x08); // param2(1)
|
||||
@@ -276,26 +290,40 @@ static void signerCalculateSignature(MyMessage &msg, bool signing) {
|
||||
_signing_sha256.write(0xEE); // SN[8]
|
||||
_signing_sha256.write(0x01); // SN[0]
|
||||
_signing_sha256.write(0x23); // SN[1]
|
||||
for (int i=0; i<25; i++) _signing_sha256.write(0x00);
|
||||
for (int i=0; i<32; i++) _signing_sha256.write(signing ? _signing_signing_nonce[i] : _signing_verifying_nonce[i]);
|
||||
for (int i=0; i<25; i++) {
|
||||
_signing_sha256.write(0x00);
|
||||
}
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(signing ? _signing_signing_nonce[i] : _signing_verifying_nonce[i]);
|
||||
}
|
||||
// Purge nonce when used
|
||||
memset(signing ? _signing_signing_nonce : _signing_verifying_nonce, 0xAA, 32);
|
||||
memcpy(_signing_temp_message, _signing_sha256.result(), 32);
|
||||
|
||||
// Feed "message" to HMAC calculator
|
||||
_signing_sha256.initHmac(_signing_hmac_key,32); // Set the key to use
|
||||
for (int i=0; i<32; i++) _signing_sha256.write(0x00); // 32 bytes zeroes
|
||||
for (int i=0; i<32; i++) _signing_sha256.write(_signing_temp_message[i]); // 32 bytes digest
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(0x00); // 32 bytes zeroes
|
||||
}
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_temp_message[i]); // 32 bytes digest
|
||||
}
|
||||
_signing_sha256.write(0x11); // OPCODE
|
||||
_signing_sha256.write(0x04); // Mode
|
||||
_signing_sha256.write(0x00); // SlotID(1)
|
||||
_signing_sha256.write(0x00); // SlotID(2)
|
||||
for (int i=0; i<11; i++) _signing_sha256.write(0x00); // 11 bytes zeroes
|
||||
for (int i=0; i<11; i++) {
|
||||
_signing_sha256.write(0x00); // 11 bytes zeroes
|
||||
}
|
||||
_signing_sha256.write(0xEE); // SN[8]
|
||||
for (int i=0; i<4; i++) _signing_sha256.write(0x00); // 4 bytes zeroes
|
||||
for (int i=0; i<4; i++) {
|
||||
_signing_sha256.write(0x00); // 4 bytes zeroes
|
||||
}
|
||||
_signing_sha256.write(0x01); // SN[0]
|
||||
_signing_sha256.write(0x23); // SN[1]
|
||||
for (int i=0; i<2; i++) _signing_sha256.write(0x00); // 2 bytes zeroes
|
||||
for (int i=0; i<2; i++) {
|
||||
_signing_sha256.write(0x00); // 2 bytes zeroes
|
||||
}
|
||||
|
||||
memcpy(_signing_hmac, _signing_sha256.resultHmac(), 32);
|
||||
|
||||
|
||||
+12
-7
@@ -319,7 +319,9 @@ void transportSwitchSM(transportState& newState) {
|
||||
else {
|
||||
_transportSM.stateRetries++; // increment retries
|
||||
}
|
||||
if (_transportSM.currentState->Transition) _transportSM.currentState->Transition(); // State transition
|
||||
if (_transportSM.currentState->Transition) {
|
||||
_transportSM.currentState->Transition(); // State transition
|
||||
}
|
||||
_transportSM.stateEnter = hwMillis(); // save time
|
||||
}
|
||||
|
||||
@@ -328,7 +330,9 @@ uint32_t transportTimeInState(void) {
|
||||
}
|
||||
|
||||
void transportUpdateSM(void) {
|
||||
if (_transportSM.currentState->Update) _transportSM.currentState->Update();
|
||||
if (_transportSM.currentState->Update) {
|
||||
_transportSM.currentState->Update();
|
||||
}
|
||||
}
|
||||
|
||||
bool isTransportReady(void) {
|
||||
@@ -464,7 +468,9 @@ bool transportRouteMessage(MyMessage &message) {
|
||||
else _transportSM.failedUplinkTransmissions = 0u;
|
||||
}
|
||||
#else
|
||||
if(!result) setIndication(INDICATION_ERR_TX);
|
||||
if(!result) {
|
||||
setIndication(INDICATION_ERR_TX);
|
||||
}
|
||||
#endif
|
||||
|
||||
return result;
|
||||
@@ -737,7 +743,9 @@ void transportProcessMessage(void) {
|
||||
if (command != C_INTERNAL) {
|
||||
#if !defined(MY_GATEWAY_FEATURE)
|
||||
// only proceed if message received from parent
|
||||
if (last != _nc.parentNodeId) return;
|
||||
if (last != _nc.parentNodeId) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#if defined(MY_GATEWAY_FEATURE)
|
||||
// Hand over message to controller
|
||||
@@ -873,6 +881,3 @@ uint8_t transportGetRoute(const uint8_t node) {
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// EOF MyTransport.cpp
|
||||
@@ -125,7 +125,9 @@ bool _serialProcess()
|
||||
{
|
||||
char inch;
|
||||
unsigned char i;
|
||||
if (!_dev.available()) return false;
|
||||
if (!_dev.available()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while(_dev.available()) {
|
||||
inch = _dev.read();
|
||||
@@ -263,7 +265,9 @@ bool transportSend(uint8_t to, const void* data, uint8_t len)
|
||||
#endif
|
||||
|
||||
// Start of header by writing multiple SOH
|
||||
for(byte w=0;w<1;w++) _dev.write(SOH);
|
||||
for(byte w=0;w<1;w++) {
|
||||
_dev.write(SOH);
|
||||
}
|
||||
_dev.write(to); // Destination address
|
||||
cs += to;
|
||||
_dev.write(_nodeId); // Source address
|
||||
@@ -351,5 +355,3 @@ uint8_t transportReceive(void* data) {
|
||||
void transportPowerDown() {
|
||||
// Nothing to shut down here
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
/**
|
||||
* 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-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.
|
||||
*/
|
||||
/***
|
||||
* This file defines the Sensor library version number
|
||||
* Normally, contributors should not modify this directly
|
||||
|
||||
+16
-8
@@ -281,8 +281,9 @@ byte AES::set_key (byte key [], int keylen)
|
||||
byte next = keylen ;
|
||||
for (byte cc = keylen, rc = 1 ; cc < hi ; cc += N_COL)
|
||||
{
|
||||
for (byte i = 0 ; i < N_COL ; i++)
|
||||
for (byte i = 0 ; i < N_COL ; i++) {
|
||||
t[i] = key_sched [cc-4+i] ;
|
||||
}
|
||||
if (cc == next)
|
||||
{
|
||||
next += keylen ;
|
||||
@@ -295,12 +296,14 @@ byte AES::set_key (byte key [], int keylen)
|
||||
}
|
||||
else if (keylen == 32 && (cc & 31) == 16)
|
||||
{
|
||||
for (byte i = 0 ; i < 4 ; i++)
|
||||
for (byte i = 0 ; i < 4 ; i++) {
|
||||
t[i] = s_box (t[i]) ;
|
||||
}
|
||||
}
|
||||
byte tt = cc - keylen ;
|
||||
for (byte i = 0 ; i < N_COL ; i++)
|
||||
for (byte i = 0 ; i < N_COL ; i++) {
|
||||
key_sched [cc + i] = key_sched [tt + i] ^ t[i] ;
|
||||
}
|
||||
}
|
||||
return AES_SUCCESS ;
|
||||
}
|
||||
@@ -309,8 +312,9 @@ byte AES::set_key (byte key [], int keylen)
|
||||
|
||||
void AES::clean ()
|
||||
{
|
||||
for (byte i = 0 ; i < KEY_SCHEDULE_BYTES ; i++)
|
||||
for (byte i = 0 ; i < KEY_SCHEDULE_BYTES ; i++) {
|
||||
key_sched [i] = 0 ;
|
||||
}
|
||||
round = 0 ;
|
||||
}
|
||||
|
||||
@@ -360,8 +364,9 @@ byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block, byte iv [N_BLOC
|
||||
while (n_block--)
|
||||
{
|
||||
xor_block (iv, plain) ;
|
||||
if (encrypt (iv, iv) != AES_SUCCESS)
|
||||
if (encrypt (iv, iv) != AES_SUCCESS) {
|
||||
return AES_FAILURE ;
|
||||
}
|
||||
copy_n_bytes (cipher, iv, N_BLOCK) ;
|
||||
plain += N_BLOCK ;
|
||||
cipher += N_BLOCK ;
|
||||
@@ -376,8 +381,9 @@ byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block)
|
||||
while (n_block--)
|
||||
{
|
||||
xor_block (iv, plain) ;
|
||||
if (encrypt (iv, iv) != AES_SUCCESS)
|
||||
if (encrypt (iv, iv) != AES_SUCCESS) {
|
||||
return AES_FAILURE ;
|
||||
}
|
||||
copy_n_bytes (cipher, iv, N_BLOCK) ;
|
||||
plain += N_BLOCK ;
|
||||
cipher += N_BLOCK ;
|
||||
@@ -416,8 +422,9 @@ byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOC
|
||||
{
|
||||
byte tmp [N_BLOCK] ;
|
||||
copy_n_bytes (tmp, cipher, N_BLOCK) ;
|
||||
if (decrypt (cipher, plain) != AES_SUCCESS)
|
||||
if (decrypt (cipher, plain) != AES_SUCCESS) {
|
||||
return AES_FAILURE ;
|
||||
}
|
||||
xor_block (plain, iv) ;
|
||||
copy_n_bytes (iv, tmp, N_BLOCK) ;
|
||||
plain += N_BLOCK ;
|
||||
@@ -434,8 +441,9 @@ byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block)
|
||||
{
|
||||
byte tmp [N_BLOCK] ;
|
||||
copy_n_bytes (tmp, cipher, N_BLOCK) ;
|
||||
if (decrypt (cipher, plain) != AES_SUCCESS)
|
||||
if (decrypt (cipher, plain) != AES_SUCCESS) {
|
||||
return AES_FAILURE ;
|
||||
}
|
||||
xor_block (plain, iv) ;
|
||||
copy_n_bytes (iv, tmp, N_BLOCK) ;
|
||||
plain += N_BLOCK ;
|
||||
|
||||
@@ -24,10 +24,12 @@ static void swi_set_signal_pin(uint8_t is_high)
|
||||
{
|
||||
SHA204_SET_OUTPUT();
|
||||
|
||||
if (is_high)
|
||||
SHA204_POUT_HIGH();
|
||||
else
|
||||
SHA204_POUT_LOW();
|
||||
if (is_high) {
|
||||
SHA204_POUT_HIGH();
|
||||
}
|
||||
else {
|
||||
SHA204_POUT_LOW();
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer)
|
||||
@@ -109,8 +111,9 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
while (--timeout_count > 0)
|
||||
{
|
||||
// Wait for falling edge.
|
||||
if (SHA204_PIN_READ() == 0)
|
||||
if (SHA204_PIN_READ() == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (timeout_count == 0)
|
||||
@@ -159,8 +162,9 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
{
|
||||
do
|
||||
{
|
||||
if (SHA204_PIN_READ() != 0)
|
||||
if (SHA204_PIN_READ() != 0) {
|
||||
break;
|
||||
}
|
||||
} while (timeout_count-- > 0);
|
||||
}
|
||||
|
||||
@@ -169,16 +173,18 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
buffer[i] |= bit_mask; // received "one" bit
|
||||
}
|
||||
|
||||
if (status != SWI_FUNCTION_RETCODE_SUCCESS)
|
||||
if (status != SWI_FUNCTION_RETCODE_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
interrupts(); //swi_enable_interrupts();
|
||||
|
||||
if (status == SWI_FUNCTION_RETCODE_TIMEOUT)
|
||||
{
|
||||
if (i > 0)
|
||||
// Indicate that we timed out after having received at least one byte.
|
||||
status = SWI_FUNCTION_RETCODE_RX_FAIL;
|
||||
if (i > 0) {
|
||||
// Indicate that we timed out after having received at least one byte.
|
||||
status = SWI_FUNCTION_RETCODE_RX_FAIL;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -191,8 +197,9 @@ static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response)
|
||||
uint8_t i;
|
||||
uint8_t ret_code;
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
for (i = 0; i < size; i++) {
|
||||
response[i] = 0;
|
||||
}
|
||||
|
||||
(void) swi_send_byte(SHA204_SWI_FLAG_TX);
|
||||
|
||||
@@ -200,8 +207,9 @@ static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response)
|
||||
if (ret_code == SWI_FUNCTION_RETCODE_SUCCESS || ret_code == SWI_FUNCTION_RETCODE_RX_FAIL)
|
||||
{
|
||||
count_byte = response[SHA204_BUFFER_POS_COUNT];
|
||||
if ((count_byte < SHA204_RSP_SIZE_MIN) || (count_byte > size))
|
||||
if ((count_byte < SHA204_RSP_SIZE_MIN) || (count_byte > size)) {
|
||||
return SHA204_INVALID_SIZE;
|
||||
}
|
||||
|
||||
return SHA204_SUCCESS;
|
||||
}
|
||||
@@ -209,10 +217,12 @@ static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response)
|
||||
// Translate error so that the Communication layer
|
||||
// can distinguish between a real error or the
|
||||
// device being busy executing a command.
|
||||
if (ret_code == SWI_FUNCTION_RETCODE_TIMEOUT)
|
||||
if (ret_code == SWI_FUNCTION_RETCODE_TIMEOUT) {
|
||||
return SHA204_RX_NO_RESPONSE;
|
||||
else
|
||||
}
|
||||
else {
|
||||
return SHA204_RX_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Communication functions */
|
||||
@@ -223,8 +233,9 @@ static uint8_t sha204c_resync(uint8_t size, uint8_t *response)
|
||||
// (step 1 of the re-synchronization process).
|
||||
delay(SHA204_SYNC_TIMEOUT);
|
||||
uint8_t ret_code = sha204p_receive_response(size, response);
|
||||
if (ret_code == SHA204_SUCCESS)
|
||||
if (ret_code == SHA204_SUCCESS) {
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// We lost communication. Send a Wake pulse and try
|
||||
// to receive a response (steps 2 and 3 of the
|
||||
@@ -261,17 +272,21 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
|
||||
{
|
||||
// Send command.
|
||||
ret_code = swi_send_byte(SHA204_SWI_FLAG_CMD);
|
||||
if (ret_code != SWI_FUNCTION_RETCODE_SUCCESS)
|
||||
if (ret_code != SWI_FUNCTION_RETCODE_SUCCESS) {
|
||||
ret_code = SHA204_COMM_FAIL;
|
||||
else
|
||||
}
|
||||
else {
|
||||
ret_code = swi_send_bytes(count, tx_buffer);
|
||||
}
|
||||
|
||||
if (ret_code != SHA204_SUCCESS)
|
||||
{
|
||||
if (sha204c_resync(rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE)
|
||||
if (sha204c_resync(rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE) {
|
||||
return ret_code; // The device seems to be dead in the water.
|
||||
else
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait minimum command execution time and then start polling for a response.
|
||||
@@ -282,8 +297,9 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
|
||||
while (n_retries_receive-- > 0)
|
||||
{
|
||||
// Reset response buffer.
|
||||
for (i = 0; i < rx_size; i++)
|
||||
for (i = 0; i < rx_size; i++) {
|
||||
rx_buffer[i] = 0;
|
||||
}
|
||||
|
||||
// Poll for response.
|
||||
timeout_countdown = execution_timeout_us;
|
||||
@@ -297,11 +313,13 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
|
||||
if (ret_code == SHA204_RX_NO_RESPONSE)
|
||||
{
|
||||
// We did not receive a response. Re-synchronize and send command again.
|
||||
if (sha204c_resync(rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE)
|
||||
if (sha204c_resync(rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE) {
|
||||
// The device seems to be dead in the water.
|
||||
return ret_code;
|
||||
else
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check whether we received a valid response.
|
||||
@@ -309,16 +327,19 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
|
||||
{
|
||||
// We see 0xFF for the count when communication got out of sync.
|
||||
ret_code_resync = sha204c_resync(rx_size, rx_buffer);
|
||||
if (ret_code_resync == SHA204_SUCCESS)
|
||||
if (ret_code_resync == SHA204_SUCCESS) {
|
||||
// We did not have to wake up the device. Try receiving response again.
|
||||
continue;
|
||||
if (ret_code_resync == SHA204_RESYNC_WITH_WAKEUP)
|
||||
}
|
||||
if (ret_code_resync == SHA204_RESYNC_WITH_WAKEUP) {
|
||||
// We could re-synchronize, but only after waking up the device.
|
||||
// Re-send command.
|
||||
break;
|
||||
else
|
||||
}
|
||||
else {
|
||||
// We failed to re-synchronize.
|
||||
return ret_code;
|
||||
}
|
||||
}
|
||||
|
||||
// We received a response of valid size.
|
||||
@@ -327,19 +348,22 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
|
||||
if (ret_code == SHA204_SUCCESS)
|
||||
{
|
||||
// Received valid response.
|
||||
if (rx_buffer[SHA204_BUFFER_POS_COUNT] > SHA204_RSP_SIZE_MIN)
|
||||
if (rx_buffer[SHA204_BUFFER_POS_COUNT] > SHA204_RSP_SIZE_MIN) {
|
||||
// Received non-status response. We are done.
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// Received status response.
|
||||
status_byte = rx_buffer[SHA204_BUFFER_POS_STATUS];
|
||||
|
||||
// Translate the three possible device status error codes
|
||||
// into library return codes.
|
||||
if (status_byte == SHA204_STATUS_BYTE_PARSE)
|
||||
if (status_byte == SHA204_STATUS_BYTE_PARSE) {
|
||||
return SHA204_PARSE_ERROR;
|
||||
if (status_byte == SHA204_STATUS_BYTE_EXEC)
|
||||
}
|
||||
if (status_byte == SHA204_STATUS_BYTE_EXEC) {
|
||||
return SHA204_CMD_FAIL;
|
||||
}
|
||||
if (status_byte == SHA204_STATUS_BYTE_COMM)
|
||||
{
|
||||
// In case of the device status byte indicating a communication
|
||||
@@ -359,16 +383,19 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
|
||||
{
|
||||
// Received response with incorrect CRC.
|
||||
ret_code_resync = sha204c_resync(rx_size, rx_buffer);
|
||||
if (ret_code_resync == SHA204_SUCCESS)
|
||||
if (ret_code_resync == SHA204_SUCCESS) {
|
||||
// We did not have to wake up the device. Try receiving response again.
|
||||
continue;
|
||||
if (ret_code_resync == SHA204_RESYNC_WITH_WAKEUP)
|
||||
}
|
||||
if (ret_code_resync == SHA204_RESYNC_WITH_WAKEUP) {
|
||||
// We could re-synchronize, but only after waking up the device.
|
||||
// Re-send command.
|
||||
break;
|
||||
else
|
||||
}
|
||||
else {
|
||||
// We failed to re-synchronize.
|
||||
return ret_code;
|
||||
}
|
||||
} // block end of check response consistency
|
||||
|
||||
} // block end of receive retry loop
|
||||
@@ -418,8 +445,9 @@ static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc)
|
||||
// Shift CRC to the left by 1.
|
||||
crc_register <<= 1;
|
||||
|
||||
if ((data_bit ^ crc_bit) != 0)
|
||||
if ((data_bit ^ crc_bit) != 0) {
|
||||
crc_register ^= polynom;
|
||||
}
|
||||
}
|
||||
}
|
||||
crc[0] = (uint8_t) (crc_register & 0x00FF);
|
||||
@@ -475,22 +503,27 @@ uint8_t atsha204_wakeup(uint8_t *response)
|
||||
delay(SHA204_WAKEUP_DELAY);
|
||||
|
||||
uint8_t ret_code = sha204p_receive_response(SHA204_RSP_SIZE_MIN, response);
|
||||
if (ret_code != SHA204_SUCCESS)
|
||||
if (ret_code != SHA204_SUCCESS) {
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// Verify status response.
|
||||
if (response[SHA204_BUFFER_POS_COUNT] != SHA204_RSP_SIZE_MIN)
|
||||
if (response[SHA204_BUFFER_POS_COUNT] != SHA204_RSP_SIZE_MIN) {
|
||||
ret_code = SHA204_INVALID_SIZE;
|
||||
else if (response[SHA204_BUFFER_POS_STATUS] != SHA204_STATUS_BYTE_WAKEUP)
|
||||
}
|
||||
else if (response[SHA204_BUFFER_POS_STATUS] != SHA204_STATUS_BYTE_WAKEUP) {
|
||||
ret_code = SHA204_COMM_FAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((response[SHA204_RSP_SIZE_MIN - SHA204_CRC_SIZE] != 0x33)
|
||||
|| (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43))
|
||||
|| (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43)) {
|
||||
ret_code = SHA204_BAD_CRC;
|
||||
}
|
||||
}
|
||||
if (ret_code != SHA204_SUCCESS)
|
||||
if (ret_code != SHA204_SUCCESS) {
|
||||
delay(SHA204_COMMAND_EXEC_MAX);
|
||||
}
|
||||
|
||||
return ret_code;
|
||||
}
|
||||
@@ -580,14 +613,16 @@ uint8_t atsha204_getSerialNumber(uint8_t * response)
|
||||
uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
|
||||
if (!returnCode)
|
||||
{
|
||||
for (int i=0; i<4; i++) // store bytes 0-3 into respones array
|
||||
response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
|
||||
for (int i=0; i<4; i++) {// store bytes 0-3 into respones array
|
||||
response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
|
||||
}
|
||||
|
||||
/* read from bytes 8->11 of config zone */
|
||||
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
|
||||
|
||||
for (int i=4; i<8; i++) // store bytes 4-7 of SN into response array
|
||||
for (int i=4; i<8; i++) {// store bytes 4-7 of SN into response array
|
||||
response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4)];
|
||||
}
|
||||
|
||||
if (!returnCode)
|
||||
{ /* Finally if last two reads were successful, read byte 8 of the SN */
|
||||
|
||||
@@ -102,7 +102,9 @@ void Sha256Class::pad() {
|
||||
|
||||
// Pad with 0x80 followed by 0x00 until the end of the block
|
||||
addUncounted(0x80);
|
||||
while (bufferOffset != 56) addUncounted(0x00);
|
||||
while (bufferOffset != 56) {
|
||||
addUncounted(0x00);
|
||||
}
|
||||
|
||||
// Append length in the last 8 bytes
|
||||
addUncounted(0); // We're only using 32 bit lengths
|
||||
@@ -147,7 +149,9 @@ void Sha256Class::initHmac(const uint8_t* key, int keyLength) {
|
||||
if (keyLength > BLOCK_LENGTH) {
|
||||
// Hash long keys
|
||||
init();
|
||||
for (;keyLength--;) write(*key++);
|
||||
for (;keyLength--;) {
|
||||
write(*key++);
|
||||
}
|
||||
memcpy(keyBuffer,result(),HASH_LENGTH);
|
||||
} else {
|
||||
// Block length keys are used as is
|
||||
@@ -166,7 +170,11 @@ uint8_t* Sha256Class::resultHmac(void) {
|
||||
memcpy(innerHash,result(),HASH_LENGTH);
|
||||
// Calculate outer hash
|
||||
init();
|
||||
for (i=0; i<BLOCK_LENGTH; i++) write(keyBuffer[i] ^ HMAC_OPAD);
|
||||
for (i=0; i<HASH_LENGTH; i++) write(innerHash[i]);
|
||||
for (i=0; i<BLOCK_LENGTH; i++) {
|
||||
write(keyBuffer[i] ^ HMAC_OPAD);
|
||||
}
|
||||
for (i=0; i<HASH_LENGTH; i++) {
|
||||
write(innerHash[i]);
|
||||
}
|
||||
return result();
|
||||
}
|
||||
|
||||
@@ -108,8 +108,10 @@ void AltSoftSerial::writeByte(uint8_t b)
|
||||
uint8_t intr_state, head;
|
||||
|
||||
head = tx_buffer_head + 1;
|
||||
if (head >= TX_BUFFER_SIZE) head = 0;
|
||||
while (tx_buffer_tail == head) ; // wait until space in buffer
|
||||
if (head >= TX_BUFFER_SIZE) {
|
||||
head = 0;
|
||||
}
|
||||
while (tx_buffer_tail == head) {} // wait until space in buffer
|
||||
intr_state = SREG;
|
||||
cli();
|
||||
if (tx_state) {
|
||||
@@ -168,7 +170,9 @@ ISR(COMPARE_A_INTERRUPT)
|
||||
DISABLE_INT_COMPARE_A();
|
||||
} else {
|
||||
tx_state = 1;
|
||||
if (++tail >= TX_BUFFER_SIZE) tail = 0;
|
||||
if (++tail >= TX_BUFFER_SIZE) {
|
||||
tail = 0;
|
||||
}
|
||||
tx_buffer_tail = tail;
|
||||
tx_byte = tx_buffer[tail];
|
||||
tx_bit = 0;
|
||||
@@ -180,7 +184,7 @@ ISR(COMPARE_A_INTERRUPT)
|
||||
|
||||
void AltSoftSerial::flushOutput(void)
|
||||
{
|
||||
while (tx_state) /* wait */ ;
|
||||
while (tx_state) {}/* wait */
|
||||
}
|
||||
|
||||
|
||||
@@ -216,14 +220,18 @@ ISR(CAPTURE_INTERRUPT)
|
||||
target = rx_target;
|
||||
while (1) {
|
||||
offset = capture - target;
|
||||
if (offset < 0) break;
|
||||
if (offset < 0) {
|
||||
break;
|
||||
}
|
||||
rx_byte = (rx_byte >> 1) | rx_bit;
|
||||
target += ticks_per_bit;
|
||||
state++;
|
||||
if (state >= 9) {
|
||||
DISABLE_INT_COMPARE_B();
|
||||
head = rx_buffer_head + 1;
|
||||
if (head >= RX_BUFFER_SIZE) head = 0;
|
||||
if (head >= RX_BUFFER_SIZE) {
|
||||
head = 0;
|
||||
}
|
||||
if (head != rx_buffer_tail) {
|
||||
rx_buffer[head] = rx_byte;
|
||||
rx_buffer_head = head;
|
||||
@@ -253,7 +261,9 @@ ISR(COMPARE_B_INTERRUPT)
|
||||
state++;
|
||||
}
|
||||
head = rx_buffer_head + 1;
|
||||
if (head >= RX_BUFFER_SIZE) head = 0;
|
||||
if (head >= RX_BUFFER_SIZE) {
|
||||
head = 0;
|
||||
}
|
||||
if (head != rx_buffer_tail) {
|
||||
rx_buffer[head] = rx_byte;
|
||||
rx_buffer_head = head;
|
||||
@@ -271,8 +281,12 @@ int AltSoftSerial::read(void)
|
||||
|
||||
head = rx_buffer_head;
|
||||
tail = rx_buffer_tail;
|
||||
if (head == tail) return -1;
|
||||
if (++tail >= RX_BUFFER_SIZE) tail = 0;
|
||||
if (head == tail) {
|
||||
return -1;
|
||||
}
|
||||
if (++tail >= RX_BUFFER_SIZE) {
|
||||
tail = 0;
|
||||
}
|
||||
out = rx_buffer[tail];
|
||||
rx_buffer_tail = tail;
|
||||
return out;
|
||||
@@ -284,7 +298,9 @@ int AltSoftSerial::peek(void)
|
||||
|
||||
head = rx_buffer_head;
|
||||
tail = rx_buffer_tail;
|
||||
if (head == tail) return -1;
|
||||
if (head == tail) {
|
||||
return -1;
|
||||
}
|
||||
return rx_buffer[tail];
|
||||
}
|
||||
|
||||
@@ -294,7 +310,9 @@ int AltSoftSerial::available(void)
|
||||
|
||||
head = rx_buffer_head;
|
||||
tail = rx_buffer_tail;
|
||||
if (head >= tail) return head - tail;
|
||||
if (head >= tail) {
|
||||
return head - tail;
|
||||
}
|
||||
return RX_BUFFER_SIZE + head - tail;
|
||||
}
|
||||
|
||||
@@ -309,9 +327,14 @@ void ftm0_isr(void)
|
||||
{
|
||||
uint32_t flags = FTM0_STATUS;
|
||||
FTM0_STATUS = 0;
|
||||
if (flags & (1<<5)) altss_capture_interrupt();
|
||||
if (flags & (1<<6)) altss_compare_a_interrupt();
|
||||
if (flags & (1<<0)) altss_compare_b_interrupt();
|
||||
if (flags & (1<<5)) {
|
||||
altss_capture_interrupt();
|
||||
}
|
||||
if (flags & (1<<6)) {
|
||||
altss_compare_a_interrupt();
|
||||
}
|
||||
if (flags & (1<<0)) {
|
||||
altss_compare_b_interrupt();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -120,7 +120,9 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
|
||||
}
|
||||
|
||||
size_t EthernetClient::write(const char *str) {
|
||||
if (str == NULL) return 0;
|
||||
if (str == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return write((const uint8_t *)str, strlen(str));
|
||||
}
|
||||
size_t EthernetClient::write(const char *buffer, size_t size) {
|
||||
@@ -165,8 +167,9 @@ void EthernetClient::flush() {
|
||||
}
|
||||
|
||||
void EthernetClient::stop() {
|
||||
if (_sock == -1)
|
||||
if (_sock == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// attempt to close the connection gracefully (send a FIN to other side)
|
||||
shutdown(_sock, SHUT_RDWR);
|
||||
@@ -178,21 +181,24 @@ void EthernetClient::stop() {
|
||||
uint8_t s;
|
||||
do {
|
||||
s = status();
|
||||
if (s == ETHERNETCLIENT_W5100_CLOSED)
|
||||
if (s == ETHERNETCLIENT_W5100_CLOSED) {
|
||||
break; // exit the loop
|
||||
}
|
||||
usleep(1000);
|
||||
gettimeofday(&curTime, NULL);
|
||||
} while (((curTime.tv_sec - startTime.tv_sec) * 1000000) + (curTime.tv_usec - startTime.tv_usec) < 1000000);
|
||||
|
||||
// if it hasn't closed, close it forcefully
|
||||
if (s != ETHERNETCLIENT_W5100_CLOSED)
|
||||
if (s != ETHERNETCLIENT_W5100_CLOSED) {
|
||||
close(_sock);
|
||||
|
||||
}
|
||||
_sock = -1;
|
||||
}
|
||||
|
||||
uint8_t EthernetClient::status() {
|
||||
if (_sock == -1) return ETHERNETCLIENT_W5100_CLOSED;
|
||||
if (_sock == -1) {
|
||||
return ETHERNETCLIENT_W5100_CLOSED;
|
||||
}
|
||||
|
||||
struct tcp_info tcp_info;
|
||||
int tcp_info_length = sizeof(tcp_info);
|
||||
@@ -227,7 +233,9 @@ uint8_t EthernetClient::status() {
|
||||
}
|
||||
|
||||
uint8_t EthernetClient::connected() {
|
||||
if (_sock == -1) return 0;
|
||||
if (_sock == -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (peek() < 0) {
|
||||
if (errno == EAGAIN) {
|
||||
|
||||
@@ -151,7 +151,9 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
|
||||
|
||||
size_t EthernetServer::write(const char *str)
|
||||
{
|
||||
if (str == NULL) return 0;
|
||||
if (str == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return write((const uint8_t *)str, strlen(str));
|
||||
}
|
||||
|
||||
|
||||
+16
-8
@@ -105,10 +105,12 @@ size_t Print::print(long n, int base) {
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned long n, int base) {
|
||||
if(base == 0)
|
||||
if(base == 0) {
|
||||
return write(n);
|
||||
else
|
||||
}
|
||||
else {
|
||||
return printNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(double n, int digits) {
|
||||
@@ -182,8 +184,9 @@ size_t Print::printNumber(unsigned long n, uint8_t base) {
|
||||
*str = '\0';
|
||||
|
||||
// prevent crash if called with base == 1
|
||||
if(base < 2)
|
||||
if(base < 2) {
|
||||
base = 10;
|
||||
}
|
||||
|
||||
do {
|
||||
unsigned long m = n;
|
||||
@@ -198,14 +201,18 @@ size_t Print::printNumber(unsigned long n, uint8_t base) {
|
||||
size_t Print::printFloat(double number, uint8_t digits) {
|
||||
size_t n = 0;
|
||||
|
||||
if(std::isnan(number))
|
||||
if(std::isnan(number)) {
|
||||
return print("nan");
|
||||
if(std::isinf(number))
|
||||
}
|
||||
if(std::isinf(number)) {
|
||||
return print("inf");
|
||||
if(number > 4294967040.0)
|
||||
}
|
||||
if(number > 4294967040.0) {
|
||||
return print("ovf"); // constant determined empirically
|
||||
if(number < -4294967040.0)
|
||||
}
|
||||
if(number < -4294967040.0) {
|
||||
return print("ovf"); // constant determined empirically
|
||||
}
|
||||
|
||||
// Handle negative numbers
|
||||
if(number < 0.0) {
|
||||
@@ -215,8 +222,9 @@ size_t Print::printFloat(double number, uint8_t digits) {
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
double rounding = 0.5;
|
||||
for(uint8_t i = 0; i < digits; ++i)
|
||||
for(uint8_t i = 0; i < digits; ++i) {
|
||||
rounding /= 10.0;
|
||||
}
|
||||
|
||||
number += rounding;
|
||||
|
||||
|
||||
@@ -212,7 +212,9 @@ int SerialPort::peek()
|
||||
{
|
||||
FILE * f = fdopen(sd, "r+");
|
||||
int c = getc(f);
|
||||
if (c == EOF) return -1;
|
||||
if (c == EOF) {
|
||||
return -1;
|
||||
}
|
||||
ungetc(c, f);
|
||||
return c;
|
||||
}
|
||||
|
||||
+53
-29
@@ -33,8 +33,9 @@ int Stream::timedRead() {
|
||||
_startMillis = millis();
|
||||
do {
|
||||
c = read();
|
||||
if(c >= 0)
|
||||
return c;
|
||||
if(c >= 0) {
|
||||
return c;
|
||||
}
|
||||
yield();
|
||||
} while(millis() - _startMillis < _timeout);
|
||||
return -1; // -1 indicates timeout
|
||||
@@ -46,8 +47,9 @@ int Stream::timedPeek() {
|
||||
_startMillis = millis();
|
||||
do {
|
||||
c = peek();
|
||||
if(c >= 0)
|
||||
if(c >= 0) {
|
||||
return c;
|
||||
}
|
||||
yield();
|
||||
} while(millis() - _startMillis < _timeout);
|
||||
return -1; // -1 indicates timeout
|
||||
@@ -59,12 +61,15 @@ int Stream::peekNextDigit() {
|
||||
int c;
|
||||
while(1) {
|
||||
c = timedPeek();
|
||||
if(c < 0)
|
||||
if(c < 0) {
|
||||
return c; // timeout
|
||||
if(c == '-')
|
||||
}
|
||||
if(c == '-') {
|
||||
return c;
|
||||
if(c >= '0' && c <= '9')
|
||||
}
|
||||
if(c >= '0' && c <= '9') {
|
||||
return c;
|
||||
}
|
||||
read(); // discard non-numeric
|
||||
}
|
||||
}
|
||||
@@ -101,13 +106,14 @@ bool Stream::findUntil(const char *target, size_t targetLen, const char *termina
|
||||
size_t termIndex = 0;
|
||||
int c;
|
||||
|
||||
if(*target == 0)
|
||||
if(*target == 0) {
|
||||
return true; // return true if target is a null string
|
||||
}
|
||||
while((c = timedRead()) > 0) {
|
||||
|
||||
if(c != target[index])
|
||||
if(c != target[index]) {
|
||||
index = 0; // reset index if any char does not match
|
||||
|
||||
}
|
||||
if(c == target[index]) {
|
||||
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
|
||||
if(++index >= targetLen) { // return true if all chars in the target match
|
||||
@@ -116,10 +122,12 @@ bool Stream::findUntil(const char *target, size_t targetLen, const char *termina
|
||||
}
|
||||
|
||||
if(termLen > 0 && c == terminator[termIndex]) {
|
||||
if(++termIndex >= termLen)
|
||||
if(++termIndex >= termLen) {
|
||||
return false; // return false if terminate string found before target string
|
||||
} else
|
||||
}
|
||||
} else {
|
||||
termIndex = 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -140,22 +148,27 @@ long Stream::parseInt(char skipChar) {
|
||||
|
||||
c = peekNextDigit();
|
||||
// ignore non numeric leading characters
|
||||
if(c < 0)
|
||||
if(c < 0) {
|
||||
return 0; // zero returned if timeout
|
||||
}
|
||||
|
||||
do {
|
||||
if(c == skipChar)
|
||||
; // ignore this charactor
|
||||
else if(c == '-')
|
||||
if(c == skipChar) {
|
||||
// ignore this charactor
|
||||
}
|
||||
else if(c == '-') {
|
||||
isNegative = true;
|
||||
else if(c >= '0' && c <= '9') // is c a digit?
|
||||
}
|
||||
else if(c >= '0' && c <= '9') { // is c a digit?
|
||||
value = value * 10 + c - '0';
|
||||
}
|
||||
read(); // consume the character we got with peek
|
||||
c = timedPeek();
|
||||
} while((c >= '0' && c <= '9') || c == skipChar);
|
||||
|
||||
if(isNegative)
|
||||
if(isNegative) {
|
||||
value = -value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -175,31 +188,39 @@ float Stream::parseFloat(char skipChar) {
|
||||
|
||||
c = peekNextDigit();
|
||||
// ignore non numeric leading characters
|
||||
if(c < 0)
|
||||
if(c < 0) {
|
||||
return 0; // zero returned if timeout
|
||||
}
|
||||
|
||||
do {
|
||||
if(c == skipChar)
|
||||
; // ignore
|
||||
else if(c == '-')
|
||||
if(c == skipChar) {
|
||||
// ignore
|
||||
}
|
||||
else if(c == '-') {
|
||||
isNegative = true;
|
||||
else if(c == '.')
|
||||
}
|
||||
else if(c == '.') {
|
||||
isFraction = true;
|
||||
}
|
||||
else if(c >= '0' && c <= '9') { // is c a digit?
|
||||
value = value * 10 + c - '0';
|
||||
if(isFraction)
|
||||
if(isFraction) {
|
||||
fraction *= 0.1;
|
||||
}
|
||||
}
|
||||
read(); // consume the character we got with peek
|
||||
c = timedPeek();
|
||||
} while((c >= '0' && c <= '9') || c == '.' || c == skipChar);
|
||||
|
||||
if(isNegative)
|
||||
if(isNegative) {
|
||||
value = -value;
|
||||
if(isFraction)
|
||||
}
|
||||
if(isFraction) {
|
||||
return value * fraction;
|
||||
else
|
||||
}
|
||||
else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
// read characters from stream into buffer
|
||||
@@ -211,8 +232,9 @@ size_t Stream::readBytes(char *buffer, size_t length) {
|
||||
size_t count = 0;
|
||||
while(count < length) {
|
||||
int c = timedRead();
|
||||
if(c < 0)
|
||||
if(c < 0) {
|
||||
break;
|
||||
}
|
||||
*buffer++ = (char) c;
|
||||
count++;
|
||||
}
|
||||
@@ -224,13 +246,15 @@ size_t Stream::readBytes(char *buffer, size_t length) {
|
||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||
|
||||
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) {
|
||||
if(length < 1)
|
||||
if(length < 1) {
|
||||
return 0;
|
||||
}
|
||||
size_t index = 0;
|
||||
while(index < length) {
|
||||
int c = timedRead();
|
||||
if(c < 0 || c == terminator)
|
||||
if(c < 0 || c == terminator) {
|
||||
break;
|
||||
}
|
||||
*buffer++ = (char) c;
|
||||
index++;
|
||||
}
|
||||
|
||||
@@ -61,8 +61,9 @@ char* itoa(int value, char* result, int base) {
|
||||
} while(quotient);
|
||||
|
||||
// Apply negative sign
|
||||
if(value < 0)
|
||||
if(value < 0) {
|
||||
*out++ = '-';
|
||||
}
|
||||
|
||||
reverse(result, out);
|
||||
*out = 0;
|
||||
@@ -112,8 +113,9 @@ char* ltoa(long value, char* result, int base) {
|
||||
} while(quotient);
|
||||
|
||||
// Apply negative sign
|
||||
if(value < 0)
|
||||
if(value < 0) {
|
||||
*out++ = '-';
|
||||
}
|
||||
|
||||
reverse(result, out);
|
||||
*out = 0;
|
||||
|
||||
@@ -231,7 +231,9 @@ boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
|
||||
|
||||
uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||
uint16_t len = 0;
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
if(!readByte(buffer, &len)) {
|
||||
return 0;
|
||||
}
|
||||
bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH;
|
||||
uint32_t multiplier = 1;
|
||||
uint16_t length = 0;
|
||||
@@ -240,7 +242,9 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||
uint8_t start = 0;
|
||||
|
||||
do {
|
||||
if(!readByte(&digit)) return 0;
|
||||
if(!readByte(&digit)) {
|
||||
return 0;
|
||||
}
|
||||
buffer[len++] = digit;
|
||||
length += (digit & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
@@ -249,8 +253,12 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||
|
||||
if (isPublish) {
|
||||
// Read in topic length to calculate bytes to skip over for Stream writing
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
if(!readByte(buffer, &len)) {
|
||||
return 0;
|
||||
}
|
||||
if(!readByte(buffer, &len)) {
|
||||
return 0;
|
||||
}
|
||||
skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2];
|
||||
start = 2;
|
||||
if (buffer[0]&MQTTQOS1) {
|
||||
@@ -260,7 +268,9 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||
}
|
||||
|
||||
for (uint16_t i = start;i<length;i++) {
|
||||
if(!readByte(&digit)) return 0;
|
||||
if(!readByte(&digit)) {
|
||||
return 0;
|
||||
}
|
||||
if (this->stream) {
|
||||
if (isPublish && len-*lengthLength-2>skip) {
|
||||
this->stream->write(digit);
|
||||
|
||||
@@ -81,7 +81,9 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(uint8_t cmd, uint8_t* buf, uint8_t len,
|
||||
while ( len-- ) {
|
||||
if (aReadMode) {
|
||||
status = _SPI.transfer( NOP );
|
||||
if (buf != NULL) *current++ = status;
|
||||
if (buf != NULL) {
|
||||
*current++ = status;
|
||||
}
|
||||
} else status = _SPI.transfer(*current++);
|
||||
}
|
||||
#endif
|
||||
@@ -194,7 +196,9 @@ LOCAL void RF24_startListening(void) {
|
||||
// toggle PRX
|
||||
RF24_setRFConfiguration(MY_RF24_CONFIGURATION | _BV(PWR_UP) | _BV(PRIM_RX) );
|
||||
// all RX pipe addresses must be unique, therefore skip if node ID is 0xFF
|
||||
if(MY_RF24_NODE_ADDRESS!=AUTO) RF24_setPipeLSB(RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
|
||||
if(MY_RF24_NODE_ADDRESS!=AUTO) {
|
||||
RF24_setPipeLSB(RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
|
||||
}
|
||||
// start listening
|
||||
RF24_ce(HIGH);
|
||||
}
|
||||
@@ -395,4 +399,3 @@ LOCAL bool RF24_initialize(void) {
|
||||
RF24_setStatus(_BV(TX_DS) | _BV(MAX_RT) | _BV(RX_DR));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+49
-24
@@ -102,8 +102,9 @@ bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID)
|
||||
yield();
|
||||
} while (readReg(REG_SYNCVALUE1) != 0x55 && millis()-start < timeout);
|
||||
|
||||
for (uint8_t i = 0; CONFIG[i][0] != 255; i++)
|
||||
for (uint8_t i = 0; CONFIG[i][0] != 255; i++) {
|
||||
writeReg(CONFIG[i][0], CONFIG[i][1]);
|
||||
}
|
||||
|
||||
// Encryption is persistent between resets and can trip you up during debugging.
|
||||
// Disable it during initialization so we always start from a known state.
|
||||
@@ -113,10 +114,11 @@ bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID)
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
start = millis();
|
||||
while (((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) && millis()-start < timeout) {
|
||||
yield();
|
||||
yield();
|
||||
} // wait for ModeReady
|
||||
if (millis()-start >= timeout)
|
||||
if (millis()-start >= timeout) {
|
||||
return false;
|
||||
}
|
||||
attachInterrupt(_interruptNum, RFM69::isr0, RISING);
|
||||
|
||||
selfPointer = this;
|
||||
@@ -149,17 +151,22 @@ void RFM69::setFrequency(uint32_t freqHz)
|
||||
|
||||
void RFM69::setMode(uint8_t newMode)
|
||||
{
|
||||
if (newMode == _mode)
|
||||
if (newMode == _mode) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (newMode) {
|
||||
case RF69_MODE_TX:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_TRANSMITTER);
|
||||
if (_isRFM69HW) setHighPowerRegs(true);
|
||||
if (_isRFM69HW) {
|
||||
setHighPowerRegs(true);
|
||||
}
|
||||
break;
|
||||
case RF69_MODE_RX:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_RECEIVER);
|
||||
if (_isRFM69HW) setHighPowerRegs(false);
|
||||
if (_isRFM69HW) {
|
||||
setHighPowerRegs(false);
|
||||
}
|
||||
break;
|
||||
case RF69_MODE_SYNTH:
|
||||
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SYNTHESIZER);
|
||||
@@ -209,7 +216,9 @@ void RFM69::setNetwork(uint8_t networkID)
|
||||
void RFM69::setPowerLevel(uint8_t powerLevel)
|
||||
{
|
||||
_powerLevel = (powerLevel > 31 ? 31 : powerLevel);
|
||||
if (_isRFM69HW) _powerLevel /= 2;
|
||||
if (_isRFM69HW) {
|
||||
_powerLevel /= 2;
|
||||
}
|
||||
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0xE0) | _powerLevel);
|
||||
}
|
||||
|
||||
@@ -227,7 +236,9 @@ void RFM69::send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool
|
||||
{
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
|
||||
uint32_t now = millis();
|
||||
while (!canSend() && millis() - now < RF69_CSMA_LIMIT_MS) receiveDone();
|
||||
while (!canSend() && millis() - now < RF69_CSMA_LIMIT_MS) {
|
||||
receiveDone();
|
||||
}
|
||||
sendFrame(toAddress, buffer, bufferSize, requestACK, false);
|
||||
}
|
||||
|
||||
@@ -258,8 +269,9 @@ bool RFM69::sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferS
|
||||
|
||||
// should be polled immediately after sending a packet with ACK request
|
||||
bool RFM69::ACKReceived(uint8_t fromNodeID) {
|
||||
if (receiveDone())
|
||||
if (receiveDone()) {
|
||||
return (SENDERID == fromNodeID || fromNodeID == RF69_BROADCAST_ADDR) && ACK_RECEIVED;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -292,16 +304,20 @@ void RFM69::interruptHook(uint8_t CTLbyte) {
|
||||
void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK)
|
||||
{
|
||||
setMode(RF69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo
|
||||
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
|
||||
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) {} // wait for ModeReady
|
||||
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
|
||||
if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN;
|
||||
if (bufferSize > RF69_MAX_DATA_LEN) {
|
||||
bufferSize = RF69_MAX_DATA_LEN;
|
||||
}
|
||||
|
||||
// control byte
|
||||
uint8_t CTLbyte = 0x00;
|
||||
if (sendACK)
|
||||
if (sendACK) {
|
||||
CTLbyte = RFM69_CTL_SENDACK;
|
||||
else if (requestACK)
|
||||
}
|
||||
else if (requestACK) {
|
||||
CTLbyte = RFM69_CTL_REQACK;
|
||||
}
|
||||
|
||||
// write to FIFO
|
||||
select();
|
||||
@@ -311,14 +327,15 @@ void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
|
||||
SPI.transfer(_address);
|
||||
SPI.transfer(CTLbyte);
|
||||
|
||||
for (uint8_t i = 0; i < bufferSize; i++)
|
||||
for (uint8_t i = 0; i < bufferSize; i++) {
|
||||
SPI.transfer(((uint8_t*) buffer)[i]);
|
||||
}
|
||||
unselect();
|
||||
|
||||
// no need to wait for transmit mode to be ready since its handled by the radio
|
||||
setMode(RF69_MODE_TX);
|
||||
uint32_t txStart = millis();
|
||||
while (hwDigitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish
|
||||
while (hwDigitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS) {} // wait for DIO0 to turn HIGH signalling transmission finish
|
||||
//while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
}
|
||||
@@ -359,7 +376,9 @@ void RFM69::interruptHandler() {
|
||||
{
|
||||
DATA[i] = SPI.transfer(0);
|
||||
}
|
||||
if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string
|
||||
if (DATALEN < RF69_MAX_DATA_LEN) {
|
||||
DATA[DATALEN] = 0; // add null at end of string
|
||||
}
|
||||
unselect();
|
||||
setMode(RF69_MODE_RX);
|
||||
}
|
||||
@@ -379,8 +398,9 @@ void RFM69::receiveBegin() {
|
||||
ACK_REQUESTED = 0;
|
||||
ACK_RECEIVED = 0;
|
||||
RSSI = 0;
|
||||
if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)
|
||||
if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) {
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
|
||||
}
|
||||
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); // set DIO0 to "PAYLOADREADY" in receive mode
|
||||
setMode(RF69_MODE_RX);
|
||||
}
|
||||
@@ -414,8 +434,9 @@ void RFM69::encrypt(const char* key) {
|
||||
{
|
||||
select();
|
||||
SPI.transfer(REG_AESKEY1 | 0x80);
|
||||
for (uint8_t i = 0; i < 16; i++)
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
SPI.transfer(key[i]);
|
||||
}
|
||||
unselect();
|
||||
}
|
||||
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFE) | (key ? 1 : 0));
|
||||
@@ -428,7 +449,7 @@ int16_t RFM69::readRSSI(bool forceTrigger) {
|
||||
{
|
||||
// RSSI trigger not needed if DAGC is in continuous mode
|
||||
writeReg(REG_RSSICONFIG, RF_RSSI_START);
|
||||
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00); // wait for RSSI_Ready
|
||||
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00) {} // wait for RSSI_Ready
|
||||
}
|
||||
rssi = -readReg(REG_RSSIVALUE);
|
||||
rssi >>= 1;
|
||||
@@ -489,10 +510,12 @@ void RFM69::promiscuous(bool onOff) {
|
||||
void RFM69::setHighPower(bool onOff) {
|
||||
_isRFM69HW = onOff;
|
||||
writeReg(REG_OCP, _isRFM69HW ? RF_OCP_OFF : RF_OCP_ON);
|
||||
if (_isRFM69HW) // turning ON
|
||||
if (_isRFM69HW) { // turning ON
|
||||
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0x1F) | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON); // enable P1 & P2 amplifier stages
|
||||
else
|
||||
}
|
||||
else {
|
||||
writeReg(REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | _powerLevel); // enable P0 only
|
||||
}
|
||||
}
|
||||
|
||||
// internal function
|
||||
@@ -519,7 +542,9 @@ void SerialWrite ( uint8_t c ) {
|
||||
}
|
||||
|
||||
void SerialPrint_P(PGM_P str, void (*f)(uint8_t) = SerialWrite ) {
|
||||
for (uint8_t c; (c = pgm_read_byte(str)); str++) (*f)(c);
|
||||
for (uint8_t c; (c = pgm_read_byte(str)); str++) {
|
||||
(*f)(c);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -794,12 +819,12 @@ uint8_t RFM69::readTemperature(uint8_t calFactor) // returns centigrade
|
||||
{
|
||||
setMode(RF69_MODE_STANDBY);
|
||||
writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
|
||||
while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING));
|
||||
while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING)) {}
|
||||
return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; // 'complement' corrects the slope, rising temp = rising val
|
||||
} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
|
||||
|
||||
void RFM69::rcCalibration()
|
||||
{
|
||||
writeReg(REG_OSC1, RF_OSC1_RCCAL_START);
|
||||
while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00);
|
||||
while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00) {}
|
||||
}
|
||||
+9
-3
@@ -79,9 +79,15 @@ void SPIClass::setClockDivider(uint16_t divider) {
|
||||
}
|
||||
|
||||
void SPIClass::chipSelect(int csn_pin) {
|
||||
if (csn_pin == RPI_GPIO_P1_26) csn_pin = BCM2835_SPI_CS1;
|
||||
else if (csn_pin == RPI_GPIO_P1_24) csn_pin = BCM2835_SPI_CS0;
|
||||
else csn_pin = BCM2835_SPI_CS0;
|
||||
if (csn_pin == RPI_GPIO_P1_26) {
|
||||
csn_pin = BCM2835_SPI_CS1;
|
||||
}
|
||||
else if (csn_pin == RPI_GPIO_P1_24) {
|
||||
csn_pin = BCM2835_SPI_CS0;
|
||||
}
|
||||
else {
|
||||
csn_pin = BCM2835_SPI_CS0;
|
||||
}
|
||||
bcm2835_spi_chipSelect(csn_pin);
|
||||
delayMicroseconds(5);
|
||||
}
|
||||
|
||||
@@ -142,8 +142,9 @@ uint8_t* SPIFlash::readUniqueId()
|
||||
SPI.transfer(0);
|
||||
SPI.transfer(0);
|
||||
SPI.transfer(0);
|
||||
for (uint8_t i=0;i<8;i++)
|
||||
for (uint8_t i=0;i<8;i++) {
|
||||
UNIQUEID[i] = SPI.transfer(0);
|
||||
}
|
||||
unselect();
|
||||
return UNIQUEID;
|
||||
}
|
||||
@@ -166,8 +167,9 @@ void SPIFlash::readBytes(uint32_t addr, void* buf, uint16_t len) {
|
||||
SPI.transfer(addr >> 8);
|
||||
SPI.transfer(addr);
|
||||
SPI.transfer(0); //"dont care"
|
||||
for (uint16_t i = 0; i < len; ++i)
|
||||
for (uint16_t i = 0; i < len; ++i) {
|
||||
((uint8_t*) buf)[i] = SPI.transfer(0);
|
||||
}
|
||||
unselect();
|
||||
}
|
||||
|
||||
@@ -252,12 +254,16 @@ void SPIFlash::writeBytes(uint32_t addr, const void* buf, uint16_t len) {
|
||||
unselect();
|
||||
oddAdr=1; //following addresses must all be shifted one off
|
||||
len--;
|
||||
if (len > 0) command(SPIFLASH_AAIWORDPROGRAM); //If for loop will run issue Wordprogram command
|
||||
if (len > 0) {
|
||||
command(SPIFLASH_AAIWORDPROGRAM); //If for loop will run issue Wordprogram command
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0;i<(len/2);i++){
|
||||
//AAI command must be set before every new word
|
||||
if (i>0) command(SPIFLASH_AAIWORDPROGRAM); //Wordprogram command for first write has been issued before
|
||||
if (i>0) {
|
||||
command(SPIFLASH_AAIWORDPROGRAM); //Wordprogram command for first write has been issued before
|
||||
}
|
||||
SPI.transfer(((uint8_t*) buf)[i*2+oddAdr]);
|
||||
SPI.transfer(((uint8_t*) buf)[i*2+1+oddAdr]);
|
||||
unselect();
|
||||
@@ -265,7 +271,9 @@ void SPIFlash::writeBytes(uint32_t addr, const void* buf, uint16_t len) {
|
||||
|
||||
if (len-i*2 == 1){
|
||||
//There is one byte (i.e. half word) left. This happens if len was odd or (len was even and addr odd)
|
||||
if (i>0) command(SPIFLASH_AAIWORDPROGRAM); //if for loop had not run wordprogram command from before is still valid
|
||||
if (i>0) {
|
||||
command(SPIFLASH_AAIWORDPROGRAM); //if for loop had not run wordprogram command from before is still valid
|
||||
}
|
||||
SPI.transfer(((uint8_t*) buf)[i*2+oddAdr]);
|
||||
SPI.transfer(0xff);
|
||||
unselect();
|
||||
@@ -285,8 +293,9 @@ void SPIFlash::writeBytes(uint32_t addr, const void* buf, uint16_t len) {
|
||||
SPI.transfer(addr >> 8);
|
||||
SPI.transfer(addr);
|
||||
|
||||
for (uint16_t i = 0; i < n; i++)
|
||||
SPI.transfer(((uint8_t*) buf)[offset + i]);
|
||||
for (uint16_t i = 0; i < n; i++) {
|
||||
SPI.transfer(((uint8_t*) buf)[offset + i]);
|
||||
}
|
||||
unselect();
|
||||
|
||||
addr+=n; // adjust the addresses and remaining bytes by what we've just transferred.
|
||||
|
||||
Reference in New Issue
Block a user