Browse Source

correct implementation of the protocol

Cyrille Meichel 10 years ago
parent
commit
fe4d3943b9
6 changed files with 315 additions and 78 deletions
  1. 1 1
      emit/Makefile
  2. 22 14
      emit/emit.c
  3. 95 31
      emit/emitlib.c
  4. 46 31
      emit/emitlib.h
  5. 1 1
      emit/test/Makefile
  6. 150 0
      emit/test/test.c

+ 1 - 1
emit/Makefile

@@ -7,7 +7,7 @@ emit.o : emit.c
 	gcc -c emit.c
 
 emitlib.o : emitlib.c emitlib.h
-	gcc -c emitlib.c
+	gcc -c emitlib.c -o emitlib.o
 
 clean:
 	rm -f emit *.o

+ 22 - 14
emit/emit.c

@@ -39,13 +39,14 @@ void usage(char** argv)
  */
 int main(int argc, char** argv)
 {
-    BIT_BUFFER buffer;
+    //BIT_BUFFER buffer;
+    BYTE_BUFFER command;
+    BYTE_BUFFER encoded;
     int i;
     char optstring[] = "xvb:d:f:r:";
     int option;
-    unsigned char global=NO_GLOBAL;
-    unsigned char number = NUMBER1;
-    unsigned char section = SECTION_A;
+    unsigned char number = 1;
+    unsigned char section = 'A';
     char *idString=0;
     unsigned long int frequency = DEFAULT_FREQ;
     unsigned char onOff = ON;
@@ -59,14 +60,10 @@ int main(int argc, char** argv)
         switch (option) {
             case 'b':
                 // Decode the button ie : A3
-                section = optarg[0] - (optarg[0]<'Z' ? 'A' : 'a');
+                section = optarg[0] - (optarg[0]<'Z' ? 0 : 'a' - 'A');
                 if (strlen(optarg)>1) {
                     number =  optarg[1] - '0';
                 }
-                if (section== 6) {
-                    // G was requested
-                    global = GLOBAL;
-                }
                 break;
             case 'r':
                 // repeatition code
@@ -108,16 +105,26 @@ int main(int argc, char** argv)
     }
 
     // Show informations
-    printf("Frequency config on SPI: %dHz\n", frequency);
+    printf("Frequency config on SPI: %luHz\n", frequency);
 
-    if (global) {
+    if (section == 'G') {
         printf("Sending command G: %s\n", (onOff == OFF) ? "OFF" : "ON");
     } else {
-        printf("Sending command %c%d: %s\n", section+'A', number, (onOff == OFF) ? "OFF" : "ON");
+        printf("Sending command %c%d: %s\n", section, number, (onOff == OFF) ? "OFF" : "ON");
     }
+    
+    command = createHomeEasyCommand(idString, section, number, onOff);
+    printfByteBuffer(command);
+    
+    printf("Code to emit:\n");
+    encoded = homeEasyEncode(&command);
+    printfByteBuffer(encoded);
+    
+    destroyByteBuffer(command);
+    destroyByteBuffer(encoded);
 
     // Preparing the buffer
-    buffer = createBitBuffer();
+    /*buffer = createBitBuffer();
 
     // Building the data
     for (i=0; i<repeat; i++) {
@@ -141,6 +148,7 @@ int main(int argc, char** argv)
 #endif
 
     // release memory
-    destroyBitBuffer(buffer);
+    destroyBitBuffer(buffer);*/
+    
     return 0;
 }

+ 95 - 31
emit/emitlib.c

@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <malloc.h>
+#include <string.h>
 #include "emitlib.h"
 
 
@@ -43,7 +44,7 @@ void destroyBitBuffer(BIT_BUFFER buffer)
  *
  * @return the created buffer
  */
-BIT_BUFFER createByteBuffer()
+BYTE_BUFFER createByteBuffer()
 {
     BYTE_BUFFER buffer;
     buffer.size = 0;
@@ -56,7 +57,7 @@ BIT_BUFFER createByteBuffer()
  *
  * @param buffer the buffer to destroy
  */
-void destroyByteBuffer(BIT_BUFFER buffer)
+void destroyByteBuffer(BYTE_BUFFER buffer)
 {
     free(buffer.data);
 }
@@ -66,14 +67,14 @@ void destroyByteBuffer(BIT_BUFFER buffer)
  *
  * @param buffer the buffer holding the data
  */
-void printfBitBuffer(BIT_BUFFER buffer)
+/*void printfBitBuffer(BIT_BUFFER buffer)
 {
-    printf("bytes: %d\nbits: %d\n", buffer.byteSize, buffer.bitSize);
+    printf("bytes: %lu\nbits: %d\n", buffer.byteSize, buffer.bitSize);
     unsigned char byte;
     unsigned char bit;
     unsigned int x;
     int i;
-    for(x=0; x<buffer.byteSize; x++) {
+    for(x=0; x<(buffer.bitSize ? buffer.byteSize : buffer.byteSize-1) ; x++) {
         byte = buffer.data[x];
         for(i=0x80; i>0; i>>=1) {
             if ((bit==0) && (byte & i)) {
@@ -83,8 +84,31 @@ void printfBitBuffer(BIT_BUFFER buffer)
             fprintf(stdout, "%d", bit);
         }
     }
+}*/
+void printfBitBuffer(BYTE_BUFFER buffer)
+{
+    printf("bytes: %lu\nbits: %lu\n", buffer.size, buffer.size*8);
+    unsigned int x;
+    for(x=0; x<buffer.size ; x++) {
+        printfBit(buffer.data[x]);
+        printf("\n");
+    }
+}
+
+/**
+ * Print a byte in binary
+ * 
+ * @param byte the byte to print
+ */
+void printfBit(unsigned char byte)
+{
+    int i;
+    for(i=0x80; i>0; i>>=1) {
+        fprintf(stdout, "%d", ((byte & i) == i));
+    }
 }
 
+
 /**
  * Print all the bytes from a buffer
  *
@@ -94,8 +118,9 @@ void printfByteBuffer(BYTE_BUFFER buffer)
 {
     unsigned int i;
     for(i=0; i<buffer.size; i++) {
-        fprintf(stdout, "%02X ", buffer.data[i];
+        fprintf(stdout, "%02X ", (unsigned char)buffer.data[i]);
     }
+    fprintf(stdout, "\n");
 }
 
 /**
@@ -104,7 +129,7 @@ void printfByteBuffer(BYTE_BUFFER buffer)
  * @param buffer the buffer holding the data
  * @param bit the bit to push
  */
-void pushBit(BIT_BUFFER* buffer, unsigned char bit)
+/*void bitPushBit(BIT_BUFFER* buffer, unsigned char bit)
 {
     buffer->data[buffer->byteSize-1] |= bit << (7-buffer->bitSize);
     buffer->bitSize++;
@@ -114,7 +139,7 @@ void pushBit(BIT_BUFFER* buffer, unsigned char bit)
         buffer->data = (char*)realloc(buffer->data, buffer->byteSize);
         buffer->data[buffer->byteSize-1] = 0;
     }
-}
+}*/
 
 /**
  * Push a byte in a buffer
@@ -124,8 +149,8 @@ void pushBit(BIT_BUFFER* buffer, unsigned char bit)
  */
 void pushByte(BYTE_BUFFER* buffer, unsigned char byte)
 {
-    buffer->size++
-    buffer->data = (char*)realloc(buffer->size);
+    buffer->size++;
+    buffer->data = (char*)realloc(buffer->data, buffer->size);
     buffer->data[buffer->size-1] = byte;
 }
 
@@ -137,7 +162,7 @@ void pushByte(BYTE_BUFFER* buffer, unsigned char byte)
  */
 void pushWord(BYTE_BUFFER* buffer, unsigned short int word)
 {
-    unsigned char* data = &word;
+    unsigned char* data = (unsigned char*)&word;
     pushByte(buffer, data[1]);
     pushByte(buffer, data[0]);
 }
@@ -150,7 +175,7 @@ void pushWord(BYTE_BUFFER* buffer, unsigned short int word)
  */
 void pushBytes(BYTE_BUFFER* buffer, unsigned char *byte, unsigned int len)
 {
-    buffer->data = (char*)realloc(buffer->size+len);
+    buffer->data = (char*)realloc(buffer->data, buffer->size+len);
     memcpy(&buffer->data[buffer->size], byte, len);
     buffer->size += len;
 }
@@ -165,12 +190,9 @@ void pushBytes(BYTE_BUFFER* buffer, unsigned char *byte, unsigned int len)
 BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer)
 {
     BYTE_BUFFER result = createByteBuffer();
-    unsigned char byte;
-    unsigned char encodedByte;
     unsigned int i;
     for(i=0; i<buffer->size; i++) {
-        byte = buffer->data[i];
-        pushWord(buffer, encodeByte(byte);
+        pushWord(&result, encodeByte(buffer->data[i]));
     }
     return result;
 }
@@ -186,12 +208,12 @@ BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer)
 {
     BYTE_BUFFER result = createByteBuffer();
     unsigned short int word;
-    unsigned char *byte = &word;
+    unsigned char *byte = (unsigned char*)&word;
     unsigned int i;
     for(i=0; i<(buffer->size+1)/2; i++) {
         byte[1] = buffer->data[2*i];
         byte[0] = (2*i+1 < buffer->size ? buffer->data[2*i+1]:0);
-        pushByte(buffer, decodeByte(word));
+        pushByte(&result, decodeByte(word));
     }
     return result;
 }
@@ -203,13 +225,13 @@ BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer)
  * 
  * @return the decoded byte
  */
-unsigned char decodeByte(unsigned short int byte)
+unsigned char decodeByte(unsigned short int word)
 {
     unsigned char decodedChar=0;
     int j;
-    int shift = 6;
+    int shift = 7;
     for(j=0x8000;j>0; j>>=2) {
-      decodedChar |= (((byte & j) == j) ? 0x01 : 0x00) << shift;
+      decodedChar |= (((word & j) == j) ? 0x01 : 0x00) << shift;
       shift -=1;
     }
     return decodedChar;
@@ -226,7 +248,7 @@ unsigned short int encodeByte(unsigned char byte)
 {
     unsigned short int encodedChar=0;
     int j;
-    int shift = 6;
+    int shift = 14;
     for(j=0x80;j>0; j>>=1) {
       encodedChar |= (((byte & j) == j) ? 0x02 : 0x01) << shift;
       shift -=2;
@@ -234,6 +256,48 @@ unsigned short int encodeByte(unsigned char byte)
     return encodedChar;
 }
 
+/**
+ * Gives the bit at a specific position
+ * 
+ * @param buffer the buffer to evaluate
+ * @param n the bit position
+ * 
+ * @return the bit value
+ */
+unsigned int bitAt(BYTE_BUFFER buffer, unsigned long int n)
+{
+    unsigned char byte = buffer.data[n / 8];
+    return (byte & (0x80 >> (n % 8)) != 0);
+}
+
+/**
+ * Append a complete command according to Chacon protocole
+ *
+ * @param id command id (refer to your remote)
+ * @param section button section ('A' | 'B'  | 'C'  | 'D'  | 'G')
+ * @param nb button number(1, 2, 3, 4)
+ * @param on boolean for on/off
+ */
+BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on)
+{
+    unsigned char last = id[3] & 0xc0;
+    BYTE_BUFFER command;
+    char formatedSection = (section<='Z' ? section : section + 'A' - 'a');
+    // adding global
+    last |= (formatedSection == 'G' ? 0x20 : 0);
+    // adding on/off
+    last |= (on ? 0x10 : 0);
+    // adding section
+    last |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c;
+    // adding num
+    last |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;;
+    //Preparing output buffer
+    command = createByteBuffer();
+    pushBytes(&command, id, 3);
+    pushByte(&command, last);
+    return command;
+}
+
 /**
  * Append a bit that will be emitted for a specific time
  *
@@ -242,13 +306,13 @@ unsigned short int encodeByte(unsigned char byte)
  * @param usec time in µs
  * @param clock frequency
  */
-void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq)
+/*void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq)
 {
     unsigned int i;
     for(i=0; i<(usec * freq) / 1e6; i++) {
-        pushBit(buffer, bit);
+        bitPushBit(buffer, bit);
     }
-}
+}*/
 
 /**
  * Append data according to Chacon protocole
@@ -257,11 +321,11 @@ void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigne
  * @param type data type (BIT0 | BIT1 | START_OF_FRAME | END_OF_DATA)
  * @param clock frequency
  */
-void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq)
+/*void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq)
 {
     appendBit(buffer, 1, timings[type][0], freq);
     appendBit(buffer, 0, timings[type][1], freq);
-}
+}*/
 
 /**
  * Append a byte according to Chacon protocole
@@ -270,14 +334,14 @@ void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq)
  * @param byte the byte to append
  * @param clock frequency
  */
-void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq)
+/*void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq)
 {
     int i;
     for(i=0x80; i>0; i>>=1) {
         appendData(buffer, ((byte & i) == i), freq);
     }
     printf("%02X ", byte);
-}
+}*/
 
 
 /**
@@ -291,7 +355,7 @@ void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq)
  * @param global if true G button is selected (nb will be ignore)
  * @param clock frequency
  */
-void pushCode(BIT_BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq)
+/*void pushCode(BIT_BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq)
 {
     unsigned char byte6 = (id[6] & 0xf0) + numbering[on + (global ? 2 : 0)];
     unsigned char byte7 = (numbering[section] << 4) + numbering[nb];
@@ -308,4 +372,4 @@ void pushCode(BIT_BUFFER* buffer, unsigned char* id, unsigned char section, unsi
     appendByte(buffer, byte7, freq);
     appendData(buffer, END_OF_DATA, freq);
     printf("EOD ");
-}
+}*/

+ 46 - 31
emit/emitlib.h

@@ -2,14 +2,14 @@
 
 
 typedef struct {
+    char* data;
     unsigned long int byteSize;
     unsigned int bitSize;
-    char* data;
 } BIT_BUFFER;
 
 typedef struct {
-    unsigned long int size;
     char* data;
+    unsigned long int size;
 } BYTE_BUFFER;
 
 #define BIT0 0
@@ -20,19 +20,6 @@ typedef struct {
 #define ON 1
 #define OFF 0
 
-#define NUMBER1 0
-#define NUMBER2 1
-#define NUMBER3 2
-#define NUMBER4 3
-
-#define SECTION_A 0
-#define SECTION_B 1
-#define SECTION_C 2
-#define SECTION_D 3
-
-#define GLOBAL 1
-#define NO_GLOBAL 0
-
 /**
  * Create a new bit buffer
  *
@@ -41,32 +28,40 @@ typedef struct {
 BIT_BUFFER createBitBuffer();
 
 /**
- * Release the memory
+ * Create a new byte buffer
  *
- * @param buffer the buffer to destroy
+ * @return the created buffer
  */
-void destroyBitBuffer(BIT_BUFFER buffer);
+BYTE_BUFFER createByteBuffer();
 
 /**
- * Create a new byte buffer
+ * Release the memory
  *
- * @return the created buffer
+ * @param buffer the buffer to destroy
  */
-BIT_BUFFER createByteBuffer();
+void destroyBitBuffer(BIT_BUFFER buffer);
 
 /**
  * Release the memory
  *
  * @param buffer the buffer to destroy
  */
-void destroyByteBuffer(BIT_BUFFER buffer);
+void destroyByteBuffer(BYTE_BUFFER buffer);
 
 /**
  * Print all the bits from a buffer
  *
  * @param buffer the buffer holding the data
  */
-void printfBitBuffer(BIT_BUFFER buffer);
+//void printfBitBuffer(BIT_BUFFER buffer);
+void printfBitBuffer(BYTE_BUFFER buffer);
+
+/**
+ * Print a byte in binary
+ * 
+ * @param byte the byte to print
+ */
+void printfBit(unsigned char byte);
 
 /**
  * Print all the bytes from a buffer
@@ -81,7 +76,7 @@ void printfByteBuffer(BYTE_BUFFER buffer);
  * @param buffer the buffer holding the data
  * @param bit the bit to push
  */
-void pushBit(BIT_BUFFER* buffer, unsigned char bit);
+//void bitPushBit(BIT_BUFFER* buffer, unsigned char bit);
 
 /**
  * Push a byte in a buffer
@@ -97,7 +92,7 @@ void pushByte(BYTE_BUFFER* buffer, unsigned char byte);
  * @param buffer the buffer holding the data
  * @param word the word to push
  */
-void pushWord(BYTE_BUFFER* buffer, unsigned short int word)
+void pushWord(BYTE_BUFFER* buffer, unsigned short int word);
 
 /**
  * Push some bytes in a buffer
@@ -133,7 +128,7 @@ BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer);
  * 
  * @return the encoded byte
  */
-unsigned char encodeByte(unsigned char byte);
+unsigned short int encodeByte(unsigned char byte);
 
 /**
  * Decode a byte according to HomeEasy
@@ -142,7 +137,27 @@ unsigned char encodeByte(unsigned char byte);
  * 
  * @return the decoded byte
  */
-unsigned char decodeByte(unsigned char byte);
+unsigned char decodeByte(unsigned short int word);
+
+/**
+ * Gives the bit at a specific position
+ * 
+ * @param buffer the buffer to evaluate
+ * @param n the bit position
+ * 
+ * @return the bit value
+ */
+unsigned int bitAt(BYTE_BUFFER buffer, unsigned long int n);
+
+/**
+ * Append a complete command according to Chacon protocole
+ *
+ * @param id command id (refer to your remote)
+ * @param section button section ('A' | 'B'  | 'C'  | 'D'  | 'G')
+ * @param nb button number(1, 2, 3, 4)
+ * @param on boolean for on/off
+ */
+BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on);
 
 /**
  * Append a bit that will be emitted for a specific time
@@ -152,7 +167,7 @@ unsigned char decodeByte(unsigned char byte);
  * @param usec time in µs
  * @param clock frequency
  */
-void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq);
+//void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigned int freq);
 
 /**
  * Append data according to Chacon protocole
@@ -161,7 +176,7 @@ void appendBit(BIT_BUFFER* buffer, unsigned char bit, unsigned int usec, unsigne
  * @param type data type (BIT0 | BIT1 | START_OF_FRAME | END_OF_DATA)
  * @param clock frequency
  */
-void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq);
+//void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq);
 
 /**
  * Append a byte according to Chacon protocole
@@ -170,7 +185,7 @@ void appendData(BIT_BUFFER* buffer, unsigned int type, unsigned int freq);
  * @param byte the byte to append
  * @param clock frequency
  */
-void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq);
+//void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq);
 
 /**
  * Append a complete command according to Chacon protocole
@@ -183,4 +198,4 @@ void appendByte(BIT_BUFFER* buffer, unsigned char byte, unsigned int freq);
  * @param global if true G button is selected (nb will be ignore)
  * @param clock frequency
  */
-void pushCode(BIT_BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq);
+//void pushCode(BIT_BUFFER* buffer, unsigned char* id, unsigned char section, unsigned char nb, unsigned char on, unsigned char global, unsigned int freq);

+ 1 - 1
emit/test/Makefile

@@ -6,7 +6,7 @@ test : test.o emitlib.o
 test.o : test.c
 	gcc -c test.c -o test.o
 
-emitlib.o : emitlib.c emitlib.h
+emitlib.o : ../emitlib.c ../emitlib.h
 	gcc -c ../emitlib.c -o emitlib.o
 
 clean:

+ 150 - 0
emit/test/test.c

@@ -1,12 +1,162 @@
 #include <stdio.h>
 #include "../emitlib.h"
 
+#define TEST_START printf("\n=== %s ===\n", __FUNCTION__)
 
 
+void testByteBuffer()
+{
+    BYTE_BUFFER buffer;
+    char bytes[] = {0xcd, 0xef, 0x12};
+    TEST_START;
+    
+    buffer = createByteBuffer();
+    
+    pushByte(&buffer, 0xab);
+    pushBytes(&buffer, bytes, 3);
+    pushWord(&buffer, 0x3456);
+    
+    printf("Should: AB CD EF 12 34 56\nGet: ");
+    printfByteBuffer(buffer);
+    
+    destroyByteBuffer(buffer);
+}
+
+void testPrintBits()
+{
+    BYTE_BUFFER buffer;
+    char bytes[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
+    TEST_START;
+    
+    buffer = createByteBuffer();
+    pushBytes(&buffer, bytes, 8);
+
+    printfBitBuffer(buffer);
+    
+    destroyByteBuffer(buffer);
+}
+
+/*void testBitBuffer()
+{
+    BIT_BUFFER buffer;
+    TEST_START;
+    buffer = createBitBuffer();
+    
+    bitPushBit(&buffer, 1);
+    bitPushBit(&buffer, 1);
+    bitPushBit(&buffer, 1);
+    bitPushBit(&buffer, 1);
+    bitPushBit(&buffer, 0);
+    bitPushBit(&buffer, 0);
+    bitPushBit(&buffer, 0);
+    bitPushBit(&buffer, 0);
+    
+    printf("Should: 11110000\nGet: ");
+    printfBitBuffer(buffer);
+    printf("\n");
+    
+    destroyBitBuffer(buffer);
+}*/
+
+void testEncode()
+{
+    unsigned char srcByte = 0x28;
+    unsigned short int encWord;
+    TEST_START;
+    
+    encWord = encodeByte(srcByte);
+    printf("Input: %02X\nShould: 0x5995\nGet: %04X\n", srcByte, encWord);
+    
+}
+
 
+void testDecode()
+{
+    unsigned short int srcWord = 0x5995;
+    unsigned char decByte;
+    TEST_START;
+    
+    decByte = decodeByte(srcWord);
+    printf("Input: %04X\nShould: 0x28\nGet: %02X\n", srcWord, decByte);
+    
+}
+
+void testHomeEasyEncode()
+{
+    BYTE_BUFFER source;
+    BYTE_BUFFER encoded;
+    unsigned char bytes[] = {0x28, 0x28, 0x01};
+    TEST_START;
+    
+    source = createByteBuffer();
+    pushBytes(&source, bytes, 3);
+    printf("Input: ");
+    printfByteBuffer(source);
+    printf("Should: 59 95 59 95 55 56\nGet:    ");
+    
+    encoded = homeEasyEncode(&source);
+    printfByteBuffer(encoded);
+    
+    destroyByteBuffer(source);
+    destroyByteBuffer(encoded);
+}
+
+void testHomeEasyDecode()
+{
+    BYTE_BUFFER source;
+    BYTE_BUFFER decoded;
+    unsigned char bytes[] = {0x59, 0x95, 0x59, 0x95, 0x55, 0x56};
+    TEST_START;
+    
+    source = createByteBuffer();
+    pushBytes(&source, bytes, 6);
+    printf("Input: ");
+    printfByteBuffer(source);
+    printf("Should: 28 28 01\nGet:    ");
+    
+    decoded = homeEasyDecode(&source);
+    printfByteBuffer(decoded);
+    
+    destroyByteBuffer(source);
+    destroyByteBuffer(decoded);
+}
+
+
+void testHomeEasyCommand(char section, unsigned char num, unsigned char onOff)
+{
+    BYTE_BUFFER command;
+    BYTE_BUFFER encoded;
+    unsigned char id[] = {0x28, 0x28, 0x01, 0x80};
+    TEST_START;
+    
+    command = createHomeEasyCommand(id, section, num, onOff);
+    printfByteBuffer(command);
+    
+    encoded = homeEasyEncode(&command);
+    printfByteBuffer(encoded);
+    
+    destroyByteBuffer(command);
+    destroyByteBuffer(encoded);
+}
 
 int main()
 {
     printf("Test\n");
+    
+    testByteBuffer();
+    
+    //testBitBuffer();
+    testPrintBits();
+    
+    testEncode();
+    
+    testDecode();
+    
+    testHomeEasyEncode();
+    
+    testHomeEasyDecode();
+    
+    testHomeEasyCommand('D', 4, ON);
+    
     return 0;
 }