Skip to content

Commit

Permalink
RFXTrx: Double checking received Type/Length
Browse files Browse the repository at this point in the history
Goodwe: remarked debug log
buienradar_rain_example.pl, reformatted comment text
  • Loading branch information
gizmocuz committed Aug 14, 2016
1 parent e15a828 commit cd0a8f1
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 86 deletions.
2 changes: 1 addition & 1 deletion hardware/GoodweAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@


#ifdef _DEBUG
#define DEBUG_GoodweAPIW 1
//#define DEBUG_GoodweAPIW 1
#endif


Expand Down
140 changes: 140 additions & 0 deletions hardware/RFXBase.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "stdafx.h"
#include "RFXBase.h"
#include "../main/Logger.h"
#include "../main/RFXtrx.h"

CRFXBase::CRFXBase()
{
Expand All @@ -9,3 +11,141 @@ CRFXBase::~CRFXBase()
{
}

bool CRFXBase::onInternalMessage(const unsigned char *pBuffer, const size_t Len)
{
if (!m_bEnableReceive)
return true; //receiving not enabled

size_t ii = 0;
while (ii < Len)
{
if (m_rxbufferpos == 0) //1st char of a packet received
{
if (pBuffer[ii] == 0) //ignore first char if 00
return true;
}
m_rxbuffer[m_rxbufferpos] = pBuffer[ii];
m_rxbufferpos++;
if (m_rxbufferpos >= sizeof(m_rxbuffer) - 1)
{
//something is out of sync here!!
//restart
_log.Log(LOG_ERROR, "RFXCOM: input buffer out of sync, going to restart!....");
m_rxbufferpos = 0;
return false;
}
if (m_rxbufferpos > m_rxbuffer[0])
{
if (CheckValidRFXData((uint8_t*)&m_rxbuffer))
sDecodeRXMessage(this, (const unsigned char *)&m_rxbuffer, NULL, -1);
else
_log.Log(LOG_ERROR, "RFXCOM: Invalid data received!....");

m_rxbufferpos = 0; //set to zero to receive next message
}
ii++;
}
return true;
}

bool CRFXBase::CheckValidRFXData(const uint8_t *pData)
{
uint8_t pLen = pData[0];
uint8_t pType = pData[1];
if (pLen < 1)
return false;
switch (pType)
{
case pTypeInterfaceControl:
return (pLen == 0x0D);
case pTypeInterfaceMessage:
return (pLen == 0x14);
case pTypeRecXmitMessage:
return (pLen == 0x04);
case pTypeUndecoded:
return (pLen > 2);
case pTypeLighting1:
return (pLen == 0x07);
case pTypeLighting2:
return (pLen == 0x0B);
case pTypeLighting3:
return (pLen == 0x08);
case pTypeLighting4:
return (pLen == 0x09);
case pTypeLighting5:
return (pLen == 0x0A);
case pTypeLighting6:
return (pLen == 0x0B);
case pTypeChime:
return (pLen == 0x07);
case pTypeFan:
return (pLen == 0x08);
case pTypeCurtain:
return (pLen == 0x07);
case pTypeBlinds:
return (pLen == 0x09);
case pTypeRFY:
return (pLen == 0x0C);
case pTypeHomeConfort:
return (pLen == 0x0C);
case pTypeSecurity1:
return (pLen == 0x08);
case pTypeSecurity2:
return (pLen == 0x1C);
case pTypeCamera:
return (pLen == 0x06);
case pTypeRemote:
return (pLen == 0x06);
case pTypeThermostat1:
return (pLen == 0x09);
case pTypeThermostat2:
return (pLen == 0x06);
case pTypeThermostat3:
return (pLen == 0x08);
case pTypeRadiator1:
return (pLen == 0x0C);
case pTypeBBQ:
return (pLen == 0x0A);
case pTypeTEMP_RAIN:
return (pLen == 0x0A);
case pTypeTEMP:
return (pLen == 0x08);
case pTypeHUM:
return (pLen == 0x08);
case pTypeTEMP_HUM:
return (pLen == 0x0A);
case pTypeBARO:
return (pLen == 0x09);
case pTypeTEMP_HUM_BARO:
return (pLen == 0x0D);
case pTypeRAIN:
return (pLen == 0x0B);
case pTypeWIND:
return (pLen == 0x10);
case pTypeUV:
return (pLen == 0x09);
case pTypeDT:
return (pLen == 0x0D);
case pTypeCURRENT:
return (pLen == 0x0D);
case pTypeENERGY:
return (pLen == 0x11);
case pTypeCURRENTENERGY:
return (pLen == 0x13);
case pTypePOWER:
return (pLen == 0x0F);
case pTypeWEIGHT:
return (pLen == 0x08);
case pTypeCARTELECTRONIC:
return (pLen == 0x15);
case pTypeRFXSensor:
return (pLen == 0x07);
case pTypeRFXMeter:
return (pLen == 0x0A);
case pTypeFS20:
return (pLen == 0x09);
default:
return false;//unknown Type
}
return false;
}
7 changes: 7 additions & 0 deletions hardware/RFXBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ class CRFXBase: public CDomoticzHardwareBase
{
friend class RFXComSerial;
friend class RFXComTCP;
friend class MainWorker;
public:
CRFXBase();
~CRFXBase();
std::string m_Version;
private:
bool onInternalMessage(const unsigned char *pBuffer, const size_t Len);
static bool CheckValidRFXData(const uint8_t *pData);
boost::shared_ptr<boost::thread> m_thread;
volatile bool m_stoprequested;
bool m_bReceiverStarted;
};

33 changes: 0 additions & 33 deletions hardware/RFXComSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,39 +786,6 @@ void RFXComSerial::readCallback(const char *data, size_t len)
}
}

bool RFXComSerial::onInternalMessage(const unsigned char *pBuffer, const size_t Len)
{
if (!m_bEnableReceive)
return true; //receiving not enabled

size_t ii = 0;
while (ii < Len)
{
if (m_rxbufferpos == 0) //1st char of a packet received
{
if (pBuffer[ii] == 0) //ignore first char if 00
return true;
}
m_rxbuffer[m_rxbufferpos] = pBuffer[ii];
m_rxbufferpos++;
if (m_rxbufferpos >= sizeof(m_rxbuffer)-1)
{
//something is out of sync here!!
//restart
_log.Log(LOG_ERROR, "RFXCOM: input buffer out of sync, going to restart!....");
m_rxbufferpos = 0;
return false;
}
if (m_rxbufferpos > m_rxbuffer[0])
{
sDecodeRXMessage(this, (const unsigned char *)&m_rxbuffer, NULL, -1);
m_rxbufferpos = 0; //set to zero to receive next message
}
ii++;
}
return true;
}

bool RFXComSerial::WriteToHardware(const char *pdata, const unsigned char length)
{
if (!isOpen())
Expand Down
7 changes: 0 additions & 7 deletions hardware/RFXComSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class RFXComSerial: public CRFXBase, AsyncSerial
public:
RFXComSerial(const int ID, const std::string& devname, unsigned int baud_rate);
~RFXComSerial();

bool m_bReceiverStarted;

bool WriteToHardware(const char *pdata, const unsigned char length);
bool UploadFirmware(const std::string &szFilename);
float GetUploadPercentage(); //returns -1 when failed
Expand All @@ -24,7 +21,6 @@ class RFXComSerial: public CRFXBase, AsyncSerial
bool StopHardware();
bool OpenSerialDevice(const bool bIsFirmwareUpgrade=false);
void Do_Work();
bool onInternalMessage(const unsigned char *pBuffer, const size_t Len);

bool UpgradeFirmware();
bool Write_TX_PKT(const unsigned char *pdata, size_t length, const int max_retry = 3);
Expand All @@ -44,9 +40,6 @@ class RFXComSerial: public CRFXBase, AsyncSerial

unsigned char m_rx_input_buffer[512];
int m_rx_tot_bytes;

boost::shared_ptr<boost::thread> m_thread;
volatile bool m_stoprequested;
int m_retrycntr;

/**
Expand Down
33 changes: 0 additions & 33 deletions hardware/RFXComTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,6 @@ void RFXComTCP::Do_Work()
_log.Log(LOG_STATUS,"RFXCOM: TCP/IP Worker stopped...");
}

bool RFXComTCP::onInternalMessage(const unsigned char *pBuffer, const size_t Len)
{
if (!m_bEnableReceive)
return true; //receiving not enabled

size_t ii = 0;
while (ii < Len)
{
if (m_rxbufferpos == 0) //1st char of a packet received
{
if (pBuffer[ii] == 0) //ignore first char if 00
return true;
}
m_rxbuffer[m_rxbufferpos] = pBuffer[ii];
m_rxbufferpos++;
if (m_rxbufferpos >= sizeof(m_rxbuffer)-1)
{
//something is out of sync here!!
//restart
_log.Log(LOG_ERROR, "RFXCOM: input buffer out of sync, going to restart!....");
m_rxbufferpos = 0;
return false;
}
if (m_rxbufferpos > m_rxbuffer[0])
{
sDecodeRXMessage(this, (const unsigned char *)&m_rxbuffer, NULL, -1);
m_rxbufferpos = 0; //set to zero to receive next message
}
ii++;
}
return true;
}

void RFXComTCP::OnData(const unsigned char *pData, size_t length)
{
boost::lock_guard<boost::mutex> l(readQueueMutex);
Expand Down
4 changes: 0 additions & 4 deletions hardware/RFXComTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class RFXComTCP : public CRFXBase, ASyncTCP
void OnData(const unsigned char *pData, size_t length);
void OnError(const std::exception e);
void OnError(const boost::system::error_code& error);
bool onInternalMessage(const unsigned char *pBuffer, const size_t Len);
void Do_Work();
boost::shared_ptr<boost::thread> m_thread;
volatile bool m_stoprequested;
bool m_bReceiverStarted;
};

14 changes: 11 additions & 3 deletions main/mainworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

//Hardware Devices
#include "../hardware/hardwaretypes.h"
#include "../hardware/RFXBase.h"
#include "../hardware/RFXComSerial.h"
#include "../hardware/RFXComTCP.h"
#include "../hardware/DomoticzTCP.h"
Expand Down Expand Up @@ -1297,9 +1298,16 @@ void MainWorker::ParseRFXLogFile()
}
if (ii==0)
continue;
pHardware->WriteToHardware((const char *)&rxbuffer,totbytes);
DecodeRXMessage(pHardware, (const unsigned char *)&rxbuffer, NULL, 255);
sleep_milliseconds(300);
if (CRFXBase::CheckValidRFXData((const uint8_t*)&rxbuffer))
{
pHardware->WriteToHardware((const char *)&rxbuffer, totbytes);
DecodeRXMessage(pHardware, (const unsigned char *)&rxbuffer, NULL, 255);
sleep_milliseconds(300);
}
else
{
_log.Log(LOG_ERROR, "Invalid data/length!");
}
}
#endif
}
Expand Down
8 changes: 3 additions & 5 deletions scripts/buienradar_rain_example.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ =head1 buienradar_rain.pl
Installation:
Perl, you need to install the LWP module:
sudo apt-get install libjson-perl
In Domoticz create a Dummy Percentage sensor.
Next go to the Devices overview, and write down the 'idx' value of the sensor.
Expand All @@ -12,11 +15,6 @@ =head1 buienradar_rain.pl
cp /home/pi/domoticz/scripts/buienradar_rain_example.pl /home/pi/domoticz/buienradar_rain.pl
nano /home/pi/domoticz/buienradar_rain.pl
You might have to install perl or additional modules
LWP: http://lwp.interglacial.com/ch01_03.htm
sudo apt-get install libjson-perl
Next add a Crontab rule:
crontab -e
Expand Down

0 comments on commit cd0a8f1

Please sign in to comment.