analyze.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. BYTE_BUFFER readFrame;
  28. unsigned long int readDWord;
  29. readFrame = createByteBuffer();
  30. while (cursor<buffer.size) {
  31. totalLengthFrame = frameSize(&data[cursor], &highLengthFrame, &lowLengthFrame);
  32. if (currentMarker>0) {
  33. /* Try to detect a start frame marker */
  34. if ((lowLengthFrame > 20) && (lowLengthFrame<50)) {
  35. detectMarker++;
  36. bitNumber = 0;
  37. currentByte = 0;
  38. if (readFrame.size == 8) {
  39. readDWord = homeEasyDecode(&readFrame);
  40. fprintf(output, "Decoded command: %08X\nID found: %08X\n", readDWord, getHomeEasyId(readDWord));
  41. } else {
  42. fprintf(output, "Nothing to decode with %d byte(s)\n", readFrame.size);
  43. }
  44. readFrame.size = 0;
  45. fprintf(output, "\n%02d: ", detectMarker);
  46. } else {
  47. currentMarker = detectMarker;
  48. }
  49. /* we are in the data range */
  50. if (currentMarker == detectMarker) {
  51. /* Push bit */
  52. currentByte = currentByte << 1;
  53. if ((lowLengthFrame<2) || (highLengthFrame>5)) {
  54. fprintf(output, "There might be an error from here\n");
  55. if (!all) return;
  56. }
  57. currentByte += (lowLengthFrame < 5 ? 0 : 1);
  58. /* check if the byte is completed */
  59. if (bitNumber == 7) {
  60. bitNumber = 0;
  61. fprintf(output, "%02X ", currentByte);
  62. pushByte(&readFrame, currentByte);
  63. currentByte = 0;
  64. } else {
  65. bitNumber++;
  66. }
  67. }
  68. /* We are in the data range */
  69. } else {
  70. if ((lowLengthFrame > 20) && (lowLengthFrame<50) && (startData)) {
  71. currentMarker = 1;
  72. currentByte = 0;
  73. bitNumber = 0;
  74. fprintf(output, "DataStart at %lu\n", cursor);
  75. fprintf(output, "\n%02d: ", detectMarker);
  76. }
  77. if (lowLengthFrame > 100) {
  78. startData = 1;
  79. }
  80. }
  81. cursor += totalLengthFrame;
  82. }
  83. destroyByteBuffer(readFrame);
  84. }