Bug fixes
[rtl-433.git] / src / rtl_433.c
index f2e1b8eaba5f1c4f5429d71edf56401df7d29950..ca0a4fee9538b3182e8fa2ab962fe2b31b9fbbd5 100755 (executable)
@@ -161,11 +161,31 @@ uint8_t crc8( uint8_t *addr, uint8_t len)
   return crc;
 }
 
+void printBits(size_t const size, void const * const ptr)
+{
+    unsigned char *b = (unsigned char*) ptr;
+    unsigned char byte;
+    int i, j;
+            
+    for (i=0;i<size;i++) {
+        
+        fprintf(stderr,"%2x:",b[i]);
+        
+        for (j=0;j<7;j++) {
+            byte = b[i] & (1<<j);
+            byte >>= j;
+            fprintf(stderr,"%u", byte);
+        }
+        fprintf(stderr," ");
+    }
+    fprintf(stderr,"\n");
+}
+
 static int wh2_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
-    int j,k;
+    int i,j,k;
 
     uint8_t payload[4];
-    uint8_t received_crc8,payload_crc8;
+    int received_crc8,payload_crc8;
 
     int wh2_id;
     float wh2_temp;
@@ -182,7 +202,10 @@ static int wh2_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
 
     payload_crc8 = crc8(payload,4);
     
-    if (payload_crc8 != received_crc8) return 0;
+    if (payload_crc8 != received_crc8) {
+        fprintf(stderr,"Bad WH2 payload CRC, skipping...\n");
+        return 0;
+    }
     
     wh2_id = (payload[0] << 4) + (payload[1] >> 4);
     wh2_temp = ((payload[1] & 0x7) << 8) + payload[2];
@@ -196,6 +219,7 @@ static int wh2_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
     fprintf(stdout, "SENSOR:TYPE=WH2,ID=%X,HUMIDITY=%g,TEMPERATURE=%g\n",wh2_id,wh2_humidity,wh2_temp);
 
     return 1;
+        
 }
 
 static int silvercrest_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
@@ -625,7 +649,7 @@ r_device tech_line_fws_500 = {
     /* .short_limit    = */ 3500/4,
     /* .long_limit     = */ 7000/4,
     /* .reset_limit    = */ 15000/4,
-    // /* .json_callback  = */ &rubicson_callback,
+    /* .json_callback  = */ &rubicson_callback,
 };
 
 r_device generic_hx2262 = {
@@ -635,7 +659,7 @@ r_device generic_hx2262 = {
     /* .short_limit    = */ 1300/4,
     /* .long_limit     = */ 10000/4,
     /* .reset_limit    = */ 40000/4,
-    // /* .json_callback  = */ &silvercrest_callback,
+    /* .json_callback  = */ &silvercrest_callback,
 };
 
 r_device technoline_ws9118 = {
@@ -1305,20 +1329,23 @@ static void pwm_d_decode(struct dm_state *demod, struct protocol_state* p, int16
 
 static void pwm_p_decode(struct dm_state *demod, struct protocol_state* p, int16_t *buf, uint32_t len) {
     unsigned int i;
-
+    
     for (i=0 ; i<len ; i++) {
         if (buf[i] > demod->level_limit && !p->start_bit) {
             /* start bit detected */
             p->start_bit      = 1;
             p->start_c        = 1;
             p->sample_counter = 0;
-//            fprintf(stderr, "start bit pulse start detected\n");
+            if (debug_output) 
+              fprintf(stderr, "start bit pulse start detected\n");
         }
 
         if (!p->real_bits && p->start_bit && (buf[i] < demod->level_limit)) {
             /* end of startbit */
             p->real_bits = 1;
-//            fprintf(stderr, "start bit pulse end detected\n");
+            p->pulse_length = 0;
+            if (debug_output)
+              fprintf(stderr, "start bit pulse end detected\n");
         }
         if (p->start_c) p->sample_counter++;
 
@@ -1326,7 +1353,8 @@ static void pwm_p_decode(struct dm_state *demod, struct protocol_state* p, int16
         if (!p->pulse_start && p->real_bits && (buf[i] > demod->level_limit)) {
             /* save the pulse start, it will never be zero */
             p->pulse_start = p->sample_counter;
-//           fprintf(stderr, "real bit pulse start detected\n");
+            if (debug_output)
+              fprintf(stderr, "real bit pulse start detected\n");
 
         }
 
@@ -1334,19 +1362,27 @@ static void pwm_p_decode(struct dm_state *demod, struct protocol_state* p, int16
             /* end of pulse */
 
             p->pulse_length = p->sample_counter-p->pulse_start;
-//           fprintf(stderr, "real bit pulse end detected %d\n", p->pulse_length);
-//           fprintf(stderr, "space duration %d\n", p->sample_counter);
+            if (debug_output) {
+              fprintf(stderr, "real bit pulse end detected %d\n", p->pulse_length);
+              fprintf(stderr, "space duration %d\n", p->sample_counter);
+            }
 
             if (p->pulse_length <= p->short_limit) {
+                if (debug_output)
+                  fprintf(stderr, "1 received\n");
                 demod_add_bit(p, 1);
-            } else if (p->pulse_length > p->short_limit) {
+            } else {
+                if (debug_output)
+                  fprintf(stderr, "0 received\n");
                 demod_add_bit(p, 0);
             }
             p->sample_counter = 0;
             p->pulse_start    = 0;
         }
 
-        if (p->real_bits && p->pulse_length > p->long_limit) {
+        if (p->real_bits && (p->pulse_length > p->long_limit)) {
+            if (debug_output)
+              fprintf(stderr, "End of packet\n");
             demod_next_bits_packet(p);
 
             p->start_bit = 0;