From 254e616f310afed02f358bd3bef2f7071b13eac6 Mon Sep 17 00:00:00 2001 From: Philippe LUC Date: Sun, 1 Oct 2017 15:50:25 +0200 Subject: [PATCH] Some code clean, and buffer overflow prevention (2) (#126) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove trailing whitespaces * RFM69toMQTT: Add the emitting nodeID to the topic, + - And remove radio.receiveDone() that is unecessary. - Send the ACK before the publish to MQTT, to be faster - Prevent buffer overflow, Terminate the data with a "\0" - Fix issue with the sender doesn't end its string with "\0" and rely only on DATALEN. * Change topic subjectGTWRFM69toMQTT to be distinct from the commands + comments * MQTTtoRFM69: deduplicate the code - Check once and only once if the topic is good. - Have or not RCV_ marked doesn't change the structure, as valueRCV is correctly set anyway + realign * MQTTtoRFM69: Limit the size of the data - Cut the packet to the limit of what can transmit RFM69 - and terminate the string * RFM69toMQTT: ACK MQTT message on subjectGTWRFM69toMQTT now contains the nodeID * RFM69: Publish to MQTT the RSSI on transmit and ACK --- ZgatewayRFM69.ino | 118 ++++++++++++++++++++++++++-------------------- config_RFM69.h | 30 +++++++----- 2 files changed, 83 insertions(+), 65 deletions(-) diff --git a/ZgatewayRFM69.ino b/ZgatewayRFM69.ino index 44be51a..099afcf 100644 --- a/ZgatewayRFM69.ino +++ b/ZgatewayRFM69.ino @@ -1,9 +1,9 @@ -/* - OpenMQTTGateway - ESP8266 or Arduino program for home automation +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation - Act as a wifi or ethernet gateway between your RF/infrared IR signal and a MQTT broker + Act as a wifi or ethernet gateway between your RF/infrared IR signal and a MQTT broker Send and receiving command by MQTT - + This gateway enables to: - receive MQTT data from a topic and send RF signal corresponding to the received MQTT data with an RFM69 module - publish MQTT data to a different topic related to received signal from an RFM69 module @@ -12,9 +12,9 @@ Library and code by Felix Rusu - felix@lowpowerlab.com Modification of the code nanohab from bbx10 https://github.com/bbx10/nanohab Copyright: (c)Florian ROBERT - + This file is part of OpenMQTTGateway. - + OpenMQTTGateway is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or @@ -103,7 +103,7 @@ void setupRFM69(void) { { trc(F("RFM69 initialization failed")); } - + if (GC_IS_RFM69HCW) { radio.setHighPower(); // Only for RFM69HCW & HW! } @@ -138,32 +138,52 @@ void setupRFM69(void) { } } +#ifdef subjectRFM69toMQTTrssi +void publishRSSI(int16_t rssi) { + // Send the value of the rssi to MQTT + char buff[sizeof(subjectRFM69toMQTTrssi)+4]; + sprintf(buff, "%s/%d", subjectRFM69toMQTTrssi, radio.SENDERID); + char buff_rssi[5]; + sprintf(buff_rssi, "%d", radio.RSSI); + boolean result = client.publish(buff, buff_rssi); +} +#else +#define publishRSSI(input) +#endif + boolean RFM69toMQTT(void) { //check if something was received (could be an interrupt from the radio) if (radio.receiveDone()) { - - uint8_t data[RF69_MAX_DATA_LEN]; + uint8_t data[RF69_MAX_DATA_LEN+1]; // For the null character + uint8_t SENDERID = radio.SENDERID; + uint8_t DATALEN = radio.DATALEN; + uint16_t RSSI = radio.RSSI; //save packet because it may be overwritten - memcpy(data, (void *)radio.DATA, radio.DATALEN); - client.publish(subjectRFM69toMQTT,(char *)data); - - trc(F("Data received")); - trc((const char *)data); + memcpy(data, (void *)radio.DATA, DATALEN); + data[DATALEN] = '\0'; // Terminate the string + // Ack as soon as possible //check if sender wanted an ACK if (radio.ACKRequested()) { radio.sendACK(); } - radio.receiveDone(); //put radio in RX mode //updateClients(senderId, rssi, (const char *)data); + trc(F("Data received")); + trc((const char *)data); + + char buff[sizeof(subjectRFM69toMQTT)+4]; + sprintf(buff, "%s/%d", subjectRFM69toMQTT, SENDERID); + client.publish(buff,(char *)data); + + publishRSSI(RSSI); + return true; - + } else { - radio.receiveDone(); //put radio in RX mode return false; } } @@ -172,53 +192,48 @@ boolean MQTTtoRFM69(char * topicOri, char * datacallback) { int loops; uint32_t startMillis; static uint32_t deltaMillis = 0; - // RF DATA ANALYSIS - //We look into the subject to see if a special RF protocol is defined + + bool good_topic = true; + for (int i=0; i