plugin 233 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. commit dde619248681e51a1097c5a0eba7eae91aeddb9d
  2. Author: Eric Betts <bettse@fastmail.fm>
  3. Date: Sat Jun 8 12:08:33 2024 -0700
  4. Add H10302, fix H10304
  5. diff --git a/wiegand.c b/wiegand.c
  6. index 1d26ed506..a362f2b59 100644
  7. --- a/wiegand.c
  8. +++ b/wiegand.c
  9. @@ -51,15 +51,14 @@ uint64_t get_linear_field(wiegand_message_t* data, uint8_t firstBit, uint8_t len
  10. }
  11. static int wiegand_C1k35s_parse(uint8_t bit_length, uint64_t bits, FuriString* description) {
  12. - if(bit_length != 35) {
  13. - return 0;
  14. - }
  15. -
  16. wiegand_message_t value;
  17. + value.Length = bit_length;
  18. value.Mid = bits >> 32;
  19. value.Bot = bits;
  20. wiegand_message_t* packed = &value;
  21. + if(packed->Length != 35) return false; // Wrong length? Stop here.
  22. +
  23. uint32_t cn = (packed->Bot >> 1) & 0x000FFFFF;
  24. uint32_t fc = ((packed->Mid & 1) << 11) | ((packed->Bot >> 21));
  25. bool valid = (evenparity32((packed->Mid & 0x1) ^ (packed->Bot & 0xB6DB6DB6)) ==
  26. @@ -72,6 +71,8 @@ static int wiegand_C1k35s_parse(uint8_t bit_length, uint64_t bits, FuriString* d
  27. if(valid) {
  28. furi_string_cat_printf(description, "C1k35s\nFC: %ld CN: %ld\n", fc, cn);
  29. return 1;
  30. + } else {
  31. + FURI_LOG_D(PLUGIN_APP_ID, "C1k35s invalid");
  32. }
  33. return 0;
  34. @@ -113,21 +114,22 @@ static int wiegand_h10301_parse(uint8_t bit_length, uint64_t bits, FuriString* d
  35. furi_string_cat_printf(description, "H10301\nFC: %d CN: %d\n", fc, cn);
  36. return 1;
  37. + } else {
  38. + FURI_LOG_D(PLUGIN_APP_ID, "H10301 invalid");
  39. }
  40. return 0;
  41. }
  42. static int wiegand_H10304_parse(uint8_t bit_length, uint64_t bits, FuriString* description) {
  43. - if(bit_length != 37) {
  44. - return 0;
  45. - }
  46. -
  47. wiegand_message_t value;
  48. + value.Length = bit_length;
  49. value.Mid = bits >> 32;
  50. value.Bot = bits;
  51. wiegand_message_t* packed = &value;
  52. + if(packed->Length != 37) return false; // Wrong length? Stop here.
  53. +
  54. uint32_t fc = get_linear_field(packed, 1, 16);
  55. uint32_t cn = get_linear_field(packed, 17, 19);
  56. bool valid =
  57. @@ -137,6 +139,32 @@ static int wiegand_H10304_parse(uint8_t bit_length, uint64_t bits, FuriString* d
  58. if(valid) {
  59. furi_string_cat_printf(description, "H10304\nFC: %ld CN: %ld\n", fc, cn);
  60. return 1;
  61. + } else {
  62. + FURI_LOG_D(PLUGIN_APP_ID, "H10304 invalid");
  63. + }
  64. +
  65. + return 0;
  66. +}
  67. +
  68. +static int wiegand_H10302_parse(uint8_t bit_length, uint64_t bits, FuriString* description) {
  69. + wiegand_message_t value;
  70. + value.Length = bit_length;
  71. + value.Mid = bits >> 32;
  72. + value.Bot = bits;
  73. + wiegand_message_t* packed = &value;
  74. +
  75. + if(packed->Length != 37) return false; // Wrong length? Stop here.
  76. +
  77. + uint64_t cn = get_linear_field(packed, 1, 35);
  78. + bool valid =
  79. + (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
  80. + (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18)));
  81. +
  82. + if(valid) {
  83. + furi_string_cat_printf(description, "H10302\nCN: %lld\n", cn);
  84. + return 1;
  85. + } else {
  86. + FURI_LOG_D(PLUGIN_APP_ID, "H10302 invalid");
  87. }
  88. return 0;
  89. @@ -148,13 +176,17 @@ static int wiegand_format_count(uint8_t bit_length, uint64_t bits) {
  90. int count = 0;
  91. FuriString* ignore = furi_string_alloc();
  92. + // NOTE: Always update the `total` and add to the wiegand_format_description function
  93. + // TODO: Make this into a function pointer array
  94. count += wiegand_h10301_parse(bit_length, bits, ignore);
  95. count += wiegand_C1k35s_parse(bit_length, bits, ignore);
  96. + count += wiegand_H10302_parse(bit_length, bits, ignore);
  97. count += wiegand_H10304_parse(bit_length, bits, ignore);
  98. + int total = 4;
  99. furi_string_free(ignore);
  100. - FURI_LOG_I(PLUGIN_APP_ID, "count: %i", count);
  101. + FURI_LOG_I(PLUGIN_APP_ID, "count: %i/%i", count, total);
  102. return count;
  103. }
  104. @@ -164,21 +196,16 @@ static void wiegand_format_description(
  105. size_t index,
  106. FuriString* description) {
  107. FURI_LOG_I(PLUGIN_APP_ID, "description %d", index);
  108. - UNUSED(bit_length);
  109. - UNUSED(bits);
  110. - size_t i = 0;
  111. -
  112. - i += wiegand_h10301_parse(bit_length, bits, description);
  113. - if(i - 1 == index) {
  114. - return;
  115. - }
  116. - i += wiegand_C1k35s_parse(bit_length, bits, description);
  117. - if(i - 1 == index) {
  118. + // Turns out I did this wrong and trying to use the index means the results get repeated. Instead, just return the results for index == 0
  119. + if(index != 0) {
  120. return;
  121. }
  122. - furi_string_cat_printf(description, "[%i] <name> FC: CN:", index);
  123. + wiegand_h10301_parse(bit_length, bits, description);
  124. + wiegand_C1k35s_parse(bit_length, bits, description);
  125. + wiegand_H10302_parse(bit_length, bits, description);
  126. + wiegand_H10304_parse(bit_length, bits, description);
  127. }
  128. /* Actual implementation of app<>plugin interface */