Browse Source

compute the device id from a frame

Cyrille 10 years ago
parent
commit
d1002d15a5
3 changed files with 48 additions and 0 deletions
  1. 23 0
      emit/home_easy.c
  2. 9 0
      emit/home_easy.h
  3. 16 0
      emit/test/test.c

+ 23 - 0
emit/home_easy.c

@@ -173,6 +173,29 @@ void sendHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsi
     destroyByteBuffer(encoded);
 }
 
+/**
+ * retrieve the HomeEasy device ID from a frame
+ *
+ * @param buffer the buffer that hold the frame
+ *
+ * @return the device id
+ */
+unsigned long int getHomeEasyId(BYTE_BUFFER buffer)
+{
+    unsigned long int id = 0;
+    unsigned long int byte;
+    int i;
+    // check that the frame is 32 bits
+    if (buffer.size != 4) {
+        return 0;
+    }
+    for (i=buffer.size; i>0; i--) {
+        byte = (unsigned long int) buffer.data[i-1];
+        id += byte <<  (8 * (buffer.size - i));
+    }
+    return id >> 6;
+}
+
 /**
  * Configure the GPIO output pin
  *

+ 9 - 0
emit/home_easy.h

@@ -92,6 +92,15 @@ void setHomeEasyTransmittorPin(unsigned char pinNumber);
  */
 int initIO();
 
+/**
+ * retrieve the HomeEasy device ID from a frame
+ *
+ * @param buffer the buffer that hold the frame
+ *
+ * @return the device id
+ */
+unsigned long int getHomeEasyId(BYTE_BUFFER buffer);
+
 /**
  * read the GPIO output pin
  *

+ 16 - 0
emit/test/test.c

@@ -37,6 +37,20 @@ void testPrintBits()
     destroyByteBuffer(buffer);
 }
 
+void testGetId()
+{
+    unsigned long int id;
+    BYTE_BUFFER buffer;
+    char bytes[] = {0x28, 0x29, 0x01, 0x9a};
+    TEST_START;
+
+    buffer = createByteBuffer();
+    pushBytes(&buffer, bytes, 4);
+
+    id =  getHomeEasyId(buffer);
+    printf("Should 00A0A406\nGet %08X\n", id);
+}
+
 void testEncode()
 {
     unsigned char srcByte = 0x28;
@@ -134,5 +148,7 @@ int main()
 
     testHomeEasyCommand('D', 4, OFF);
 
+    testGetId();
+
     return 0;
 }