encoder-indala-40134.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "encoder-indala-40134.h"
  2. #include <furi.h>
  3. void EncoderIndala_40134::init(const uint8_t* data, const uint8_t data_size) {
  4. furi_check(data_size == 3);
  5. uint32_t fc_and_card = (data[0] << 16) | (data[1] << 8) | data[2];
  6. card_data = 0;
  7. // preamble
  8. set_bit(1, 0);
  9. set_bit(1, 2);
  10. set_bit(1, 32);
  11. // factory code
  12. set_bit(((fc_and_card >> 23) & 1), 57);
  13. set_bit(((fc_and_card >> 22) & 1), 49);
  14. set_bit(((fc_and_card >> 21) & 1), 44);
  15. set_bit(((fc_and_card >> 20) & 1), 47);
  16. set_bit(((fc_and_card >> 19) & 1), 48);
  17. set_bit(((fc_and_card >> 18) & 1), 53);
  18. set_bit(((fc_and_card >> 17) & 1), 39);
  19. set_bit(((fc_and_card >> 16) & 1), 58);
  20. // card number
  21. set_bit(((fc_and_card >> 15) & 1), 42);
  22. set_bit(((fc_and_card >> 14) & 1), 45);
  23. set_bit(((fc_and_card >> 13) & 1), 43);
  24. set_bit(((fc_and_card >> 12) & 1), 40);
  25. set_bit(((fc_and_card >> 11) & 1), 52);
  26. set_bit(((fc_and_card >> 10) & 1), 36);
  27. set_bit(((fc_and_card >> 9) & 1), 35);
  28. set_bit(((fc_and_card >> 8) & 1), 51);
  29. set_bit(((fc_and_card >> 7) & 1), 46);
  30. set_bit(((fc_and_card >> 6) & 1), 33);
  31. set_bit(((fc_and_card >> 5) & 1), 37);
  32. set_bit(((fc_and_card >> 4) & 1), 54);
  33. set_bit(((fc_and_card >> 3) & 1), 56);
  34. set_bit(((fc_and_card >> 2) & 1), 59);
  35. set_bit(((fc_and_card >> 1) & 1), 50);
  36. set_bit(((fc_and_card >> 0) & 1), 41);
  37. // checksum
  38. uint8_t checksum = 0;
  39. checksum += ((fc_and_card >> 14) & 1);
  40. checksum += ((fc_and_card >> 12) & 1);
  41. checksum += ((fc_and_card >> 9) & 1);
  42. checksum += ((fc_and_card >> 8) & 1);
  43. checksum += ((fc_and_card >> 6) & 1);
  44. checksum += ((fc_and_card >> 5) & 1);
  45. checksum += ((fc_and_card >> 2) & 1);
  46. checksum += ((fc_and_card >> 0) & 1);
  47. // wiegand parity bits
  48. // even parity sum calculation (high 12 bits of data)
  49. uint8_t even_parity_sum = 0;
  50. for(int8_t i = 12; i < 24; i++) {
  51. if(((fc_and_card >> i) & 1) == 1) {
  52. even_parity_sum++;
  53. }
  54. }
  55. // odd parity sum calculation (low 12 bits of data)
  56. uint8_t odd_parity_sum = 1;
  57. for(int8_t i = 0; i < 12; i++) {
  58. if(((fc_and_card >> i) & 1) == 1) {
  59. odd_parity_sum++;
  60. }
  61. }
  62. // even parity bit
  63. set_bit((even_parity_sum % 2), 34);
  64. // odd parity bit
  65. set_bit((odd_parity_sum % 2), 38);
  66. // checksum
  67. if((checksum & 1) == 1) {
  68. set_bit(0, 62);
  69. set_bit(1, 63);
  70. } else {
  71. set_bit(1, 62);
  72. set_bit(0, 63);
  73. }
  74. last_bit = card_data & 1;
  75. card_data_index = 0;
  76. current_polarity = true;
  77. }
  78. void EncoderIndala_40134::set_bit(bool bit, uint8_t position) {
  79. position = 63 - position;
  80. if(bit) {
  81. card_data |= 1ull << position;
  82. } else {
  83. card_data &= ~(1ull << position);
  84. }
  85. }
  86. void EncoderIndala_40134::get_next(bool* polarity, uint16_t* period, uint16_t* pulse) {
  87. *period = 2;
  88. *pulse = 1;
  89. *polarity = current_polarity;
  90. bit_clock_index++;
  91. if(bit_clock_index >= clock_per_bit) {
  92. bit_clock_index = 0;
  93. bool current_bit = (card_data >> (63 - card_data_index)) & 1;
  94. if(current_bit != last_bit) {
  95. current_polarity = !current_polarity;
  96. }
  97. last_bit = current_bit;
  98. card_data_index++;
  99. if(card_data_index >= 64) {
  100. card_data_index = 0;
  101. }
  102. }
  103. }