analyze.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <stdio.h>
  2. #include <wiringPi.h>
  3. #include <unistd.h>
  4. #include <sys/time.h>
  5. #include <malloc.h>
  6. #include "home_easy.h"
  7. #include "analyze.h"
  8. /**
  9. * Analyse the data
  10. *
  11. * @param buffer data to analyze
  12. * @param output output to render the result
  13. * @param all if false the analyze stops at the first error encoutered
  14. */
  15. void analyse(BYTE_BUFFER buffer, FILE* output, unsigned char all)
  16. {
  17. unsigned long int cursor=0;
  18. int detectMarker = 1;
  19. int currentMarker = 0;
  20. unsigned int lowLengthFrame;
  21. unsigned int highLengthFrame;
  22. unsigned int totalLengthFrame;
  23. unsigned char currentByte = 0;
  24. unsigned char bitNumber = 0;
  25. unsigned char startData = 0;
  26. unsigned char* data = buffer.data;
  27. unsigned char extractedSection;
  28. unsigned char extractedNumber;
  29. unsigned char extractedOnOff;
  30. BYTE_BUFFER readFrame;
  31. unsigned long int readDWord;
  32. readFrame = createByteBuffer();
  33. while (cursor<buffer.size) {
  34. totalLengthFrame = frameSize(&data[cursor], &highLengthFrame, &lowLengthFrame);
  35. if (currentMarker>0) {
  36. /* Try to detect a start frame marker */
  37. if ((lowLengthFrame > 20) && (lowLengthFrame<50)) {
  38. detectMarker++;
  39. bitNumber = 0;
  40. currentByte = 0;
  41. if (readFrame.size == 8) {
  42. readDWord = homeEasyDecode(&readFrame);
  43. getHomeEasyInfo(readDWord, 0, &extractedOnOff, &extractedSection, &extractedNumber);
  44. fprintf(output, "\nDecoded command: %08X\nID found: %08X\n", readDWord, getHomeEasyId(readDWord));
  45. fprintf(output, "Button %c%d - %s pressed", extractedSection, extractedNumber, (extractedOnOff ? "OFF" : "ON"));
  46. } else {
  47. fprintf(output, "Nothing to decode with %d byte(s)\n", readFrame.size);
  48. }
  49. readFrame.size = 0;
  50. fprintf(output, "\n%02d: ", detectMarker);
  51. } else {
  52. currentMarker = detectMarker;
  53. }
  54. /* we are in the data range */
  55. if (currentMarker == detectMarker) {
  56. /* Push bit */
  57. currentByte = currentByte << 1;
  58. if ((lowLengthFrame<2) || (highLengthFrame>5)) {
  59. fprintf(output, "There might be an error from here\n");
  60. if (!all) return;
  61. }
  62. currentByte += (lowLengthFrame < 5 ? 0 : 1);
  63. /* check if the byte is completed */
  64. if (bitNumber == 7) {
  65. bitNumber = 0;
  66. fprintf(output, "%02X ", currentByte);
  67. pushByte(&readFrame, currentByte);
  68. currentByte = 0;
  69. } else {
  70. bitNumber++;
  71. }
  72. }
  73. /* We are in the data range */
  74. } else {
  75. if ((lowLengthFrame > 20) && (lowLengthFrame<50) && (startData)) {
  76. currentMarker = 1;
  77. currentByte = 0;
  78. bitNumber = 0;
  79. fprintf(output, "DataStart at %lu\n", cursor);
  80. fprintf(output, "\n%02d: ", detectMarker);
  81. }
  82. if (lowLengthFrame > 100) {
  83. startData = 1;
  84. }
  85. }
  86. cursor += totalLengthFrame;
  87. }
  88. destroyByteBuffer(readFrame);
  89. }