Skip to content

Commit

Permalink
add RF files
Browse files Browse the repository at this point in the history
  • Loading branch information
avp-avp committed Jul 3, 2017
1 parent 998ae99 commit 7c2f884
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 8 deletions.
4 changes: 4 additions & 0 deletions librf/RFParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "RFProtocolRubitek.h"
#include "RFProtocolVhome.h"
#include "RFProtocolMotionSensor.h"
#include "RFProtocolSmartHome.h"
#include "RFAnalyzer.h"

CRFParser::CRFParser(CLog *log, string SavePath)
Expand Down Expand Up @@ -50,6 +51,8 @@ void CRFParser::AddProtocol(string protocol)
AddProtocol(new CRFProtocolMotionSensor());
else if (protocol == "VHome")
AddProtocol(new CRFProtocolVhome());
else if (protocol == "SmartHome")
AddProtocol(new CRFProtocolSmartHome());
else if (protocol == "All")
{
AddProtocol(new CRFProtocolX10());
Expand All @@ -60,6 +63,7 @@ void CRFParser::AddProtocol(string protocol)
AddProtocol(new CRFProtocolNooLite());
AddProtocol(new CRFProtocolRubitek());
AddProtocol(new CRFProtocolVhome());
AddProtocol(new CRFProtocolSmartHome());

//AddProtocol(new CRFProtocolMotionSensor());
}
Expand Down
39 changes: 31 additions & 8 deletions librf/RFProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ string c2s(char c)
CRFProtocol::CRFProtocol(range_array_type zeroLengths, range_array_type pulseLengths, int bits, int minRepeat, string PacketDelimeter)
:m_ZeroLengths(zeroLengths), m_PulseLengths(pulseLengths), m_Bits(bits), m_MinRepeat(minRepeat), m_PacketDelimeter(PacketDelimeter), m_InvertPacket(true)
{
m_Debug = false;
m_Debug = true;
m_InvertPacket = false;
m_Log = CLog::Default();
}
Expand Down Expand Up @@ -87,6 +87,12 @@ string CRFProtocol::DecodeRaw(base_type* data, size_t dataLen)

if (isPulse(data[i]))
{
while (i < dataLen-1 && isPulse(data[i+1]))
{
i++;
len+= getLengh(data[i]);
}

int pos = 0;
for (; m_PulseLengths[pos][0]; pos++)
{
Expand All @@ -99,7 +105,7 @@ string CRFProtocol::DecodeRaw(base_type* data, size_t dataLen)

if (!m_PulseLengths[pos][0])
{
if (m_Debug) // Если включена отладка - явно пишем длины плохих пауз
if (m_Debug) // Если включена отладка - явно пишем длины плохих пульсов
decodedRaw += string("[") + itoa(len) + "}";
else
decodedRaw += "?";
Expand Down Expand Up @@ -128,6 +134,12 @@ string CRFProtocol::DecodeRaw(base_type* data, size_t dataLen)
}
else
{
while (i < dataLen - 1 && !isPulse(data[i + 1]))
{
i++;
len += getLengh(data[i]);
}

int pos = 0;
for (; m_ZeroLengths[pos][0]; pos++)
{
Expand Down Expand Up @@ -187,12 +199,23 @@ string CRFProtocol::DecodeBits(string_vector&rawPackets)

for_each(string_vector, rawPackets, s)
{
string packet;
size_t pos = s->find(m_Debug ? '[' : '?');
if (pos != string::npos)
packet = s->substr(0, pos);
else
packet = *s;
string packet = *s;
do
{
size_t pos = packet.find(m_Debug ? '[' : '?');
if (pos == 0)
{
if (m_Debug)
{
while (packet.length() && packet[0] != '}' && packet[0] != ']')
packet = packet.substr(1);
}
packet = packet.substr(1);
continue;
} else if (pos != string::npos)
packet = packet.substr(0, pos);

} while (0);

if (!packet.length())
continue;
Expand Down
107 changes: 107 additions & 0 deletions librf/RFProtocolSmartHome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "RFProtocolSmartHome.h"


//
static range_type g_timing_pause[7] =
{
{ 5400, 18000 }, // Разделитель
{ 140, 600 }, // Короткий
{ 800, 1200 }, // Длинный
{ 0,0 }
};

static range_type g_timing_pulse[8] =
{
{ 5400, 12000 }, // Разделитель
{ 140, 600 }, // Короткий
{ 800, 1200 }, // Длинный
{ 0,0 }
};

static const uint16_t g_transmit_data[] =
{
500, 200, 400, 0, // Pauses
500, 100, 300, 0 // Pulses
};

CRFProtocolSmartHome::CRFProtocolSmartHome()
:CRFProtocol(g_timing_pause, g_timing_pulse, 24, 2, "a")
{
SetTransmitTiming(g_transmit_data);
}



CRFProtocolSmartHome::~CRFProtocolSmartHome()
{
}


string CRFProtocolSmartHome::DecodePacket(const string& raw)
{
if (raw.length() < 10 )
return "";

string res;

for (int i = 0; i < raw.length()-1; i += 2)
{
if (raw.substr(i, 2) == "Cb")
res += "1";
else if (raw.substr(i, 2) == "Bc")
res += "0";
else
return "";
}

if (raw[raw.length() - 1] == 'B')
return res;

return "";
}

string CRFProtocolSmartHome::DecodeData(const string& bits)
{
int addr = bits2long(bits, 0, 16);
int cmd = bits2long(bits, 16, 7);

char buffer[100];
snprintf(buffer, sizeof(buffer), "addr=%04x cmd=%d", addr, cmd);
return buffer;
}


string CRFProtocolSmartHome::bits2timings(const string &bits)
{
string result = bits;


return result;
}

string CRFProtocolSmartHome::data2bits(const string &data)
{
string proto, dataDetail;
SplitPair(data, ':', proto, dataDetail);
if (proto != "SmartHome")
throw CHaException(CHaException::ErrBadParam, "Bad protocol in '" + data + "'");

string_map values;
SplitValues(dataDetail, values);

string sAddr = values["addr"];
string sCmd = values["cmd"];

if (!sAddr.length() || !sCmd.length())
throw CHaException(CHaException::ErrBadParam, "Bad command for Livolo:" + data);

uint16_t addr = (uint16_t)strtol(sAddr.c_str(), NULL, 16);
uint8_t cmd = atoi(sCmd);

if (!addr || !cmd)
throw CHaException(CHaException::ErrBadParam, "Bad command for Livolo:" + data);

string res = reverse(l2bits(addr, 16)) + reverse(l2bits(cmd, 7));

return res;
}
19 changes: 19 additions & 0 deletions librf/RFProtocolSmartHome.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "RFProtocol.h"
class RFLIB_API CRFProtocolSmartHome :
public CRFProtocol
{
public:
CRFProtocolSmartHome();
~CRFProtocolSmartHome();

virtual string getName() { return "SmartHome"; };
virtual string DecodePacket(const string&);
virtual string DecodeData(const string&);


virtual string bits2timings(const string &bits);
virtual string data2bits(const string &data);

};

0 comments on commit 7c2f884

Please sign in to comment.