decoder-indala.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "decoder-indala.h"
  2. #include <api-hal.h>
  3. constexpr uint32_t clocks_in_us = 64;
  4. constexpr uint32_t min_time_us = 25 * clocks_in_us;
  5. constexpr uint32_t mid_time_us = 45 * clocks_in_us;
  6. constexpr uint32_t max_time_us = 90 * clocks_in_us;
  7. bool DecoderIndala::read(uint8_t* data, uint8_t data_size) {
  8. bool result = false;
  9. if(ready) {
  10. result = true;
  11. printf("IND %02X %02X %02X\r\n", facility, (uint8_t)(number >> 8), (uint8_t)number);
  12. ready = false;
  13. }
  14. return result;
  15. }
  16. void DecoderIndala::process_front(bool polarity, uint32_t time) {
  17. if(ready) return;
  18. if(polarity == false) {
  19. last_pulse_time = time;
  20. } else {
  21. last_pulse_time += time;
  22. pulse_count++;
  23. if(last_pulse_time > min_time_us && last_pulse_time < max_time_us) {
  24. if(last_pulse_time > mid_time_us) {
  25. bool last_data = !(readed_data & 1);
  26. pulse_count = 0;
  27. readed_data = (readed_data << 1) | last_data;
  28. verify();
  29. } else if((pulse_count % 16) == 0) {
  30. bool last_data = readed_data & 1;
  31. pulse_count = 0;
  32. readed_data = (readed_data << 1) | last_data;
  33. verify();
  34. }
  35. }
  36. }
  37. }
  38. DecoderIndala::DecoderIndala() {
  39. }
  40. void DecoderIndala::reset_state() {
  41. }
  42. void DecoderIndala::verify() {
  43. // verify inverse
  44. readed_data = ~readed_data;
  45. verify_inner();
  46. // verify normal
  47. readed_data = ~readed_data;
  48. verify_inner();
  49. }
  50. typedef union {
  51. uint64_t raw;
  52. struct __attribute__((packed)) {
  53. uint8_t static0 : 3;
  54. uint8_t checksum : 2;
  55. uint8_t static1 : 2;
  56. uint8_t y14 : 1;
  57. uint8_t x8 : 1;
  58. uint8_t x1 : 1;
  59. uint8_t y13 : 1;
  60. uint8_t static2 : 1;
  61. uint8_t y12 : 1;
  62. uint8_t x6 : 1;
  63. uint8_t y5 : 1;
  64. uint8_t y8 : 1;
  65. uint8_t y15 : 1;
  66. uint8_t x2 : 1;
  67. uint8_t x5 : 1;
  68. uint8_t x4 : 1;
  69. uint8_t y9 : 1;
  70. uint8_t y2 : 1;
  71. uint8_t x3 : 1;
  72. uint8_t y3 : 1;
  73. uint8_t y1 : 1;
  74. uint8_t y16 : 1;
  75. uint8_t y4 : 1;
  76. uint8_t x7 : 1;
  77. uint8_t p2 : 1;
  78. uint8_t y11 : 1;
  79. uint8_t y6 : 1;
  80. uint8_t y7 : 1;
  81. uint8_t p1 : 1;
  82. uint8_t y10 : 1;
  83. uint32_t preamble : 30;
  84. };
  85. } IndalaFormat;
  86. void DecoderIndala::verify_inner() {
  87. IndalaFormat id;
  88. id.raw = readed_data;
  89. // preamble
  90. //if((data >> 34) != 0b000000000000000000000000000001) return;
  91. if(id.preamble != 1) return;
  92. // static data bits
  93. //if((data & 0b100001100111) != 0b101) return;
  94. if(id.static2 != 0 && id.static1 != 0 && id.static0 != 0b101) return;
  95. // Indala checksum
  96. uint8_t sum_to_check = id.y2 + id.y4 + id.y7 + id.y8 + id.y10 + id.y11 + id.y14 + id.y16;
  97. if(sum_to_check % 2 == 0) {
  98. if(id.checksum != 0b10) return;
  99. } else {
  100. if(id.checksum != 0b01) return;
  101. }
  102. // read facility number
  103. facility = (id.x1 << 7) + (id.x2 << 6) + (id.x3 << 5) + (id.x4 << 4) + (id.x5 << 3) +
  104. (id.x6 << 2) + (id.x7 << 1) + (id.x8 << 0);
  105. // read serial number
  106. number = (id.y1 << 15) + (id.y2 << 14) + (id.y3 << 13) + (id.y4 << 12) + (id.y5 << 11) +
  107. (id.y6 << 10) + (id.y7 << 9) + (id.y8 << 8) + (id.y9 << 7) + (id.y10 << 6) +
  108. (id.y11 << 5) + (id.y12 << 4) + (id.y13 << 3) + (id.y14 << 2) + (id.y15 << 1) +
  109. (id.y16 << 0);
  110. // Wiegand checksum left
  111. sum_to_check = 0;
  112. for(int8_t i = 0; i < 8; i--) {
  113. if((facility >> i) & 1) {
  114. sum_to_check += 1;
  115. }
  116. }
  117. for(int8_t i = 0; i < 4; i--) {
  118. if((number >> i) & 1) {
  119. sum_to_check += 1;
  120. }
  121. }
  122. if(id.p1) {
  123. sum_to_check += 1;
  124. }
  125. if((sum_to_check % 2) == 1) return;
  126. // Wiegand checksum right
  127. sum_to_check = 0;
  128. for(int8_t i = 0; i < 12; i--) {
  129. if((number >> (i + 4)) & 1) {
  130. sum_to_check += 1;
  131. }
  132. }
  133. if(id.p2) {
  134. sum_to_check += 1;
  135. }
  136. if((sum_to_check % 2) != 1) return;
  137. ready = true;
  138. }