From c9c652094ee4585ee89be6b7bbdbdce0a809ebc2 Mon Sep 17 00:00:00 2001
From: Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
Date: Tue, 12 Jul 2011 03:03:10 +0200
Subject: [PATCH] Compare RFXCOM X10 signal bursts by type, not by length

I've discovered an oddity in RFXCOM data processing which results in
random X10 RF signals happening to be reported more than once. After
more thorough investigation I've found that single X10 RF bursts can
happen to be decoded and reported by the RFXCOM receiver in a slightly
different way than other bursts of the same signal. This can be caused
by noise, but I suspect this may also have something to do with new
RFXCOM firmware, capable of decoding still more and more signal types,
which may lead to less restrictive noise filtering.

The content of such distorted bursts is still correct, but they are
reported to Heyu as being longer than others by 1 bit. While Heyu still
recognises them as valid X10 RF transmissions, comparing them with other
bursts by their length fails, which results in reporting them as
separate signals. If compared by their type instead, they no longer
cause multi-bursts series being broken into separate signals.

I've implemented this fix for both current shared receive queue, and
a work in progress separate receive queues code paths.

Created and tested against Heyu 2.9.3.

Signed-off-by: Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
---
 x10aux.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/x10aux.c b/x10aux.c
index 9546400..26a3cc1 100644
--- a/x10aux.c
+++ b/x10aux.c
@@ -1249,8 +1249,8 @@ int aux_rfxcomvl ( void )
    unsigned char  subindx, subtype;
    unsigned char  hcode, ucode, fcode, rfflood, addr, xmtr, rcvr;
 
-   unsigned char  typeq[2], nbitsq[2], trulenq[2];
-   unsigned char  *type, *nbits, *trulen;
+   unsigned char  typeq[2], lasttypeq[2], nbitsq[2], trulenq[2];
+   unsigned char  *type, *lasttype, *nbits, *trulen;
 
    unsigned char  xbuffq[2][64];
    unsigned char  *xbuff, *buff;
@@ -1309,6 +1309,7 @@ int aux_rfxcomvl ( void )
    countq[0] = countq[1] = 0;
    lastlenq[0] = lastlenq[1] = -1;
    typeq[0] = typeq[1] = 0;
+   lasttypeq[0] = lasttypeq[1] = 0;
    subindx = 0;
    trulenq[0] = trulenq[1] = 0;
    nseqq[0] = nseqq[1] = 0;
@@ -1373,6 +1374,7 @@ int aux_rfxcomvl ( void )
       bufflen = &bufflenq[rcvr];
       nbits = &nbitsq[rcvr];
       type = &typeq[rcvr];
+      lasttype = &lasttypeq[rcvr];
       sensor_flag = &sensor_flagq[rcvr];
       use_old_buffer = &use_old_bufferq[rcvr];
       saddr = &saddrq[rcvr];
@@ -1601,6 +1603,7 @@ int aux_rfxcomvl ( void )
       *use_old_buffer = 0;
 
       if ( *count == 0 ) {
+	 *lasttype = *type;
          memcpy(lastbuff, buff, *bufflen);
          *lastlen = *bufflen;
          (*count)++;
@@ -1672,7 +1675,7 @@ int aux_rfxcomvl ( void )
             send_virtual_aux_data(0, buff[0], *type, buff[1], 0, buff[2], buff[3]);
          }
       }
-      else if ( *bufflen == *lastlen && memcmp(buff, lastbuff, *bufflen) == 0 ) {
+      else if ( *type == *lasttype && memcmp(buff, lastbuff, *bufflen < *lastlen ? *bufflen : *lastlen) == 0 ) {
          /* Repeat of previous burst */
          (*count)++;
          nread = 0;
@@ -1746,7 +1749,7 @@ int aux_rfxcomvl ( void )
  +-------------------------------------------------------------*/
 int aux_rfxcomvl ( void )
 {
-   unsigned char  type, subindx, subtype, trulen;
+   unsigned char  type, lasttype, subindx, subtype, trulen;
    unsigned char  hcode, ucode, fcode, rfflood, nbits, addr, xmtr, rcvr;
    unsigned char  xbuff[64], *buff;
    unsigned char  lastbuff[64];
@@ -1778,6 +1781,7 @@ int aux_rfxcomvl ( void )
 
    lastlen = -1;
    type = 0;
+   lasttype = 0;
    subindx = 0;
    trulen = 0;
    nseq = 0;
@@ -2053,6 +2057,7 @@ int aux_rfxcomvl ( void )
       use_old_buffer = 0;
 
       if ( count == 0 ) {
+         lasttype = type;
          memcpy(lastbuff, buff, bufflen);
          lastlen = bufflen;
          count++;
@@ -2123,7 +2128,7 @@ int aux_rfxcomvl ( void )
             send_virtual_aux_data(0, buff[0], type, buff[1], 0, buff[2], buff[3]);
          }
       }
-      else if ( /*this_word == rfword */ bufflen == lastlen && memcmp(buff, lastbuff, bufflen) == 0 ) {
+      else if ( /*this_word == rfword */ type == lasttype && memcmp(buff, lastbuff, bufflen < lastlen ? bufflen : lastlen ) == 0 ) {
          /* Repeat of previous burst */
          count++;
          nread = 0;
-- 
1.7.3.4

