Browse Source

listen command

Cyrille 10 years ago
parent
commit
a4c768ddd4
6 changed files with 205 additions and 1 deletions
  1. 7 1
      emit/Makefile
  2. 87 0
      emit/analyze.c
  3. 11 0
      emit/analyze.h
  4. 3 0
      emit/home_easy.c
  5. BIN
      emit/listen
  6. 97 0
      emit/listen.c

+ 7 - 1
emit/Makefile

@@ -1,8 +1,14 @@
-all : emit
+all : emit listen
+
+listen : listen.o buffer.o home_easy.o utils.o analyze.o
+	gcc -o $@ $^ -lwiringPi
 
 emit : emit.o buffer.o home_easy.o utils.o
 	gcc -o $@ $^ -lwiringPi
 
+listen.o : listen.c
+	gcc -c $< -o $@
+
 emit.o : emit.c
 	gcc -c $< -o $@
 

+ 87 - 0
emit/analyze.c

@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <wiringPi.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <malloc.h>
+#include "home_easy.h"
+#include "analyze.h"
+
+
+/**
+ * Analyse the data
+ *
+ * @param buffer data to analyze
+ * @param output output to render the result
+ * @param all if false the analyze stops at the first error encoutered
+ */
+void analyse(BYTE_BUFFER buffer, FILE* output, unsigned char all)
+{
+    unsigned long int cursor=0;
+    int detectMarker = 1;
+    int currentMarker = 0;
+    unsigned int lowLengthFrame;
+    unsigned int highLengthFrame;
+    unsigned int totalLengthFrame;
+    unsigned char currentByte = 0;
+    unsigned char bitNumber = 0;
+    unsigned char startData = 0;
+    unsigned char* data = buffer.data;
+    BYTE_BUFFER readFrame;
+    unsigned long int readDWord;
+    readFrame = createByteBuffer();
+    while (cursor<buffer.size) {
+        totalLengthFrame = frameSize(&data[cursor], &highLengthFrame, &lowLengthFrame);
+        if (currentMarker>0) {
+            /* Try to detect a start frame marker */
+            if ((lowLengthFrame > 20) && (lowLengthFrame<50)) {
+                detectMarker++;
+                bitNumber = 0;
+                currentByte = 0;
+                if (readFrame.size == 8) {
+                    readDWord = homeEasyDecode(&readFrame);
+                    fprintf(output, "Decoded command: %08X\nID found: %08X\n", readDWord, getHomeEasyId(readDWord));
+                } else {
+                    fprintf(output, "Nothing to decode with %d byte(s)\n", readFrame.size);
+                }
+                readFrame.size = 0;
+                fprintf(output, "\n%02d: ", detectMarker);
+            } else {
+                currentMarker = detectMarker;
+            }
+            /* we are in the data range */
+            if (currentMarker == detectMarker) {
+                /* Push bit */
+                currentByte = currentByte << 1;
+                if ((lowLengthFrame<2) || (highLengthFrame>5)) {
+                    fprintf(output, "There might be an error from here\n");
+                    if (!all) return;
+		}
+                currentByte += (lowLengthFrame < 5 ? 0 : 1);
+                /* check if the byte is completed */
+                if (bitNumber == 7) {
+                    bitNumber = 0;
+                    fprintf(output, "%02X ", currentByte);
+                    pushByte(&readFrame, currentByte);
+                    currentByte = 0;
+                } else {
+                   bitNumber++;
+                }
+            }
+            /* We are in the data range */
+        } else {
+            if ((lowLengthFrame > 20) && (lowLengthFrame<50) && (startData)) {
+                currentMarker = 1;
+                currentByte = 0;
+                bitNumber = 0;
+                fprintf(output, "DataStart at %lu\n", cursor);
+                fprintf(output, "\n%02d: ", detectMarker);
+            }
+            if (lowLengthFrame > 100) {
+                startData = 1;
+            }
+        }
+        cursor += totalLengthFrame;
+    }
+    destroyByteBuffer(readFrame);
+}
+

+ 11 - 0
emit/analyze.h

@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include "buffer.h"
+
+/**
+ * Analyse the data
+ *
+ * @param buffer data to analyze
+ * @param output output to render the result
+ * @param all if false the analyze stops at the first error encoutered
+ */
+void analyse(BYTE_BUFFER buffer, FILE* output, unsigned char all);

+ 3 - 0
emit/home_easy.c

@@ -333,11 +333,14 @@ BYTE_BUFFER readData(unsigned long int samples, unsigned int duration)
     result.size = samples;
     result.data = (char*) realloc(result.data, samples);
 
+    scheduler_realtime();
     start = showTime(0);
     for(i=0; i<samples; i++) {
         result.data[i] = digitalRead(homeEasyPinIn);
         delayMicroseconds(duration);
     }
     showTime(start);
+    scheduler_standard();
+
     return result;
 }

BIN
emit/listen


+ 97 - 0
emit/listen.c

@@ -0,0 +1,97 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <malloc.h>
+#include "home_easy.h"
+#include "buffer.h"
+
+/**
+ * Usage of this program
+ */
+void usage(char** argv)
+{
+    fprintf(stderr, "Chacon analyzer V1.0\nC. Meichel\n2013, October\n");
+    fprintf(stderr, "Syntaxe : %s [options]\n", argv[0]);
+    fprintf(stderr, "\t-o output:\n\t\toutput file\n");
+    fprintf(stderr, "\t-n number:\n\t\tnumber of bit to analyze (default 100000)\n");
+    fprintf(stderr, "\t-t number:\n\t\ttempo between samples in µs (default 10)\n");
+    fprintf(stderr, "\t-v:\n\t\tverbose\n");
+    fprintf(stderr, "\t-a:\n\t\tperform a complete analyse even if an error was encountered\n");
+}
+
+/**
+ * Main program
+ *
+ * @param argc number of arguments passed to the program
+ * @param argv array of arguments passed to the program
+ *
+ * @return status
+ */
+int main (int argc, char** argv)
+{
+    char optstring[] = "n:t:o:va";
+    unsigned int i;
+    unsigned char previousBit;
+    struct timeval* start;
+    int option;
+    unsigned int duration = 80;
+    unsigned long int samples = 10000;
+    unsigned char verbose = 0;
+    unsigned char all = 0;
+    FILE* output=0;
+    BYTE_BUFFER buffer;
+
+    /* reading options */
+    opterr=0; /* Pas de message d'erreur automatique */
+    while ((option = getopt(argc, argv, optstring)) != -1) {
+        switch (option) {
+            case 't':
+                sscanf(optarg, "%d", &duration);
+                break;
+            case 'n':
+                sscanf(optarg, "%lu", &samples);
+                break;
+            case 'a':
+                all = 1;
+                break;
+            case 'v':
+                verbose = 1;
+                break;
+            case 'o':
+                output = fopen(optarg, "w");
+                if (output==0) {
+                    fprintf(stderr, "Could not open file %s\n", optarg);
+                }
+                break;
+            default:
+                usage(argv);
+                return 0;
+                break;
+        }
+    }
+
+    /* Configure the GPIO */
+    initIO();
+
+    /* Read the data */
+    fprintf(stderr, "Reading data\n");
+    buffer = readData(samples, duration);
+
+    /* Analyzing the data */
+    fprintf(stderr, "Analyzing\n");
+    analyse(buffer, output ? output : stdout, all);
+
+    if (verbose) {
+        fprintf(output ? output : stdout, "\n\nRawData\n");
+        previousBit=0;
+        for(i=0; i<buffer.size; i++) {
+           if ((previousBit == 0) && (buffer.data[i] == 1))
+               fprintf(output ? output : stdout, "\n");
+           fprintf(output ? output : stdout, "%c", buffer.data[i]+'0');
+           previousBit = buffer.data[i];
+       }
+    }
+
+    destroyByteBuffer(buffer);
+    return 0 ;
+}