protocol-indala-40134.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "protocol-indala-40134.h"
  2. #include <furi.h>
  3. typedef uint64_t Indala40134CardData;
  4. static void set_bit(bool bit, uint8_t position, Indala40134CardData* card_data) {
  5. position = (sizeof(Indala40134CardData) * 8) - 1 - position;
  6. if(bit) {
  7. *card_data |= 1ull << position;
  8. } else {
  9. *card_data &= ~(1ull << position);
  10. }
  11. }
  12. uint8_t ProtocolIndala40134::get_encoded_data_size() {
  13. return sizeof(Indala40134CardData);
  14. }
  15. uint8_t ProtocolIndala40134::get_decoded_data_size() {
  16. return 3;
  17. }
  18. void ProtocolIndala40134::encode(
  19. const uint8_t* decoded_data,
  20. const uint8_t decoded_data_size,
  21. uint8_t* encoded_data,
  22. const uint8_t encoded_data_size) {
  23. furi_check(decoded_data_size >= get_decoded_data_size());
  24. furi_check(encoded_data_size >= get_encoded_data_size());
  25. uint32_t fc_and_card = (decoded_data[0] << 16) | (decoded_data[1] << 8) | decoded_data[2];
  26. Indala40134CardData card_data = 0;
  27. // preamble
  28. set_bit(1, 0, &card_data);
  29. set_bit(1, 2, &card_data);
  30. set_bit(1, 32, &card_data);
  31. // factory code
  32. set_bit(((fc_and_card >> 23) & 1), 57, &card_data);
  33. set_bit(((fc_and_card >> 22) & 1), 49, &card_data);
  34. set_bit(((fc_and_card >> 21) & 1), 44, &card_data);
  35. set_bit(((fc_and_card >> 20) & 1), 47, &card_data);
  36. set_bit(((fc_and_card >> 19) & 1), 48, &card_data);
  37. set_bit(((fc_and_card >> 18) & 1), 53, &card_data);
  38. set_bit(((fc_and_card >> 17) & 1), 39, &card_data);
  39. set_bit(((fc_and_card >> 16) & 1), 58, &card_data);
  40. // card number
  41. set_bit(((fc_and_card >> 15) & 1), 42, &card_data);
  42. set_bit(((fc_and_card >> 14) & 1), 45, &card_data);
  43. set_bit(((fc_and_card >> 13) & 1), 43, &card_data);
  44. set_bit(((fc_and_card >> 12) & 1), 40, &card_data);
  45. set_bit(((fc_and_card >> 11) & 1), 52, &card_data);
  46. set_bit(((fc_and_card >> 10) & 1), 36, &card_data);
  47. set_bit(((fc_and_card >> 9) & 1), 35, &card_data);
  48. set_bit(((fc_and_card >> 8) & 1), 51, &card_data);
  49. set_bit(((fc_and_card >> 7) & 1), 46, &card_data);
  50. set_bit(((fc_and_card >> 6) & 1), 33, &card_data);
  51. set_bit(((fc_and_card >> 5) & 1), 37, &card_data);
  52. set_bit(((fc_and_card >> 4) & 1), 54, &card_data);
  53. set_bit(((fc_and_card >> 3) & 1), 56, &card_data);
  54. set_bit(((fc_and_card >> 2) & 1), 59, &card_data);
  55. set_bit(((fc_and_card >> 1) & 1), 50, &card_data);
  56. set_bit(((fc_and_card >> 0) & 1), 41, &card_data);
  57. // checksum
  58. uint8_t checksum = 0;
  59. checksum += ((fc_and_card >> 14) & 1);
  60. checksum += ((fc_and_card >> 12) & 1);
  61. checksum += ((fc_and_card >> 9) & 1);
  62. checksum += ((fc_and_card >> 8) & 1);
  63. checksum += ((fc_and_card >> 6) & 1);
  64. checksum += ((fc_and_card >> 5) & 1);
  65. checksum += ((fc_and_card >> 2) & 1);
  66. checksum += ((fc_and_card >> 0) & 1);
  67. // wiegand parity bits
  68. // even parity sum calculation (high 12 bits of data)
  69. uint8_t even_parity_sum = 0;
  70. for(int8_t i = 12; i < 24; i++) {
  71. if(((fc_and_card >> i) & 1) == 1) {
  72. even_parity_sum++;
  73. }
  74. }
  75. // odd parity sum calculation (low 12 bits of data)
  76. uint8_t odd_parity_sum = 1;
  77. for(int8_t i = 0; i < 12; i++) {
  78. if(((fc_and_card >> i) & 1) == 1) {
  79. odd_parity_sum++;
  80. }
  81. }
  82. // even parity bit
  83. set_bit((even_parity_sum % 2), 34, &card_data);
  84. // odd parity bit
  85. set_bit((odd_parity_sum % 2), 38, &card_data);
  86. // checksum
  87. if((checksum & 1) == 1) {
  88. set_bit(0, 62, &card_data);
  89. set_bit(1, 63, &card_data);
  90. } else {
  91. set_bit(1, 62, &card_data);
  92. set_bit(0, 63, &card_data);
  93. }
  94. memcpy(encoded_data, &card_data, get_encoded_data_size());
  95. }
  96. void ProtocolIndala40134::decode(
  97. const uint8_t* encoded_data,
  98. const uint8_t encoded_data_size,
  99. uint8_t* decoded_data,
  100. const uint8_t decoded_data_size) {
  101. furi_check(decoded_data_size >= get_decoded_data_size());
  102. furi_check(encoded_data_size >= get_encoded_data_size());
  103. // TODO implement decoding
  104. furi_check(0);
  105. }
  106. bool ProtocolIndala40134::can_be_decoded(
  107. const uint8_t* encoded_data,
  108. const uint8_t encoded_data_size) {
  109. furi_check(encoded_data_size >= get_encoded_data_size());
  110. // TODO implement decoding
  111. furi_check(0);
  112. return false;
  113. }