wiegand_data.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "../wiegand.h"
  2. void wiegand_add_info_4bit_8bit(FuriString* buffer) {
  3. if(bit_count == 8) {
  4. for(int i = 0; i < 4; i++) {
  5. furi_string_cat_printf(
  6. buffer, "\nbit %d: %d %d (bit %d)", i, data[i], data[i + 4], i + 4);
  7. if(data[i] == data[i + 4]) {
  8. furi_string_cat_printf(buffer, " - ERROR");
  9. } else {
  10. furi_string_cat_printf(buffer, " - OK");
  11. }
  12. }
  13. }
  14. if(bit_count == 4 || bit_count == 8) {
  15. int code = 0;
  16. int offset = bit_count == 4 ? 0 : 4;
  17. for(int i = 0; i < 4; i++) {
  18. code = code << 1;
  19. code += data[i + offset] ? 1 : 0;
  20. }
  21. if(code <= 9) {
  22. furi_string_cat_printf(buffer, "\nButton: %d", code);
  23. } else if(code == 10) {
  24. furi_string_cat_printf(buffer, "\nButton: Escape");
  25. } else if(code == 11) {
  26. furi_string_cat_printf(buffer, "\nButton: Enter");
  27. }
  28. }
  29. }
  30. void wiegand_add_info_26bit(FuriString* buffer) {
  31. // 26 bit wiegand, the first bit is the even parity bit, which is
  32. // based on the next 12 bits. The number of bits that are a 1 should
  33. // be even.
  34. int parity = 0;
  35. if(data[0]) {
  36. parity = 1;
  37. }
  38. for(int i = 1; i < 13; i++) {
  39. if(data[i]) {
  40. parity++;
  41. }
  42. }
  43. if(parity % 2 == 0) {
  44. furi_string_cat_printf(buffer, "\nEven Parity (%d): OK", parity);
  45. } else {
  46. furi_string_cat_printf(buffer, "\nEven Parity (%d): ERROR", parity);
  47. }
  48. // After the parity bit, the next 8 bits are the facility code.
  49. // Then the next 16 bits are the card id .
  50. furi_string_cat_printf(buffer, "\nFacility: 0x");
  51. int code = 0;
  52. int count = 0;
  53. for(int i = 1; i < 25; i++) {
  54. code = code << 1;
  55. code |= data[i] ? 1 : 0;
  56. if(++count % 4 == 0) {
  57. furi_string_cat_printf(buffer, "%X", code);
  58. code = 0;
  59. }
  60. // Parity, then 8 bit facility code, then id.
  61. if(i == 9) {
  62. furi_string_cat_printf(buffer, "\nId: 0x");
  63. }
  64. }
  65. if(data[13]) {
  66. parity = 1;
  67. } else {
  68. parity = 0;
  69. }
  70. for(int i = 14; i < 26; i++) {
  71. if(data[i]) {
  72. parity++;
  73. }
  74. }
  75. if(parity % 2 == 0) {
  76. furi_string_cat_printf(buffer, "\nOdd Parity (%d): ERROR", parity);
  77. } else {
  78. furi_string_cat_printf(buffer, "\nOdd Parity (%d): OK", parity);
  79. }
  80. }
  81. void wiegand_add_info_24bit(FuriString* buffer) {
  82. // 24 bit wiegand (no parity info).
  83. // The First 8 bits are the facility code.
  84. // Then the next 16 bits are the card id.
  85. furi_string_cat_printf(buffer, "\nFacility: 0x");
  86. int code = 0;
  87. int count = 0;
  88. for(int i = 0; i < 24; i++) {
  89. code = code << 1;
  90. code |= data[i] ? 1 : 0;
  91. if(++count % 4 == 0) {
  92. furi_string_cat_printf(buffer, "%X", code);
  93. code = 0;
  94. }
  95. // The first 8 bits are facility code, then comes id.
  96. if(i == 8) {
  97. furi_string_cat_printf(buffer, "\nId: 0x");
  98. }
  99. }
  100. }
  101. void wiegand_add_info(FuriString* buffer) {
  102. furi_string_push_back(buffer, '\n');
  103. if(bit_count == 4 || bit_count == 8) {
  104. wiegand_add_info_4bit_8bit(buffer);
  105. } else if(bit_count == 26) {
  106. wiegand_add_info_26bit(buffer);
  107. } else if(bit_count == 24) {
  108. wiegand_add_info_24bit(buffer);
  109. }
  110. furi_string_push_back(buffer, '\n');
  111. }
  112. void wiegand_button_callback(GuiButtonType result, InputType type, void* context) {
  113. App* app = context;
  114. if(type == InputTypeShort && result == GuiButtonTypeLeft) {
  115. view_dispatcher_send_custom_event(app->view_dispatcher, WiegandDataSceneSaveButtonEvent);
  116. } else if(type == InputTypeShort && result == GuiButtonTypeCenter) {
  117. view_dispatcher_send_custom_event(app->view_dispatcher, WiegandDataScenePlayButtonEvent);
  118. }
  119. }
  120. void wiegand_data_scene_on_enter(void* context) {
  121. App* app = context;
  122. widget_reset(app->widget);
  123. widget_add_string_element(app->widget, 0, 0, AlignLeft, AlignTop, FontPrimary, "Wiegand Data");
  124. FuriString* buffer = furi_string_alloc(1024);
  125. furi_string_printf(buffer, "Bits: %d\n", bit_count);
  126. for(int i = 0; i < bit_count; i++) {
  127. furi_string_push_back(buffer, data[i] ? '1' : '0');
  128. if((bit_count - i - 1) % 22 == 21) {
  129. furi_string_push_back(buffer, '\n');
  130. }
  131. }
  132. furi_string_cat_printf(buffer, "\nPulse: %ld us", (data_rise[0] - data_fall[0]) / 64);
  133. furi_string_cat_printf(buffer, "\nPeriod: %ld us", (data_fall[1] - data_fall[0]) / 64);
  134. wiegand_add_info(buffer);
  135. for(int i = 0; i < bit_count;) {
  136. uint32_t pulse = (data_rise[i] - data_fall[i]) / 64;
  137. i++;
  138. uint32_t period = (i < bit_count) ? (data_fall[i] - data_fall[i - 1]) / 64 : 0;
  139. furi_string_cat_printf(
  140. buffer, "\n%c : %ld us, %ld us", data[i] ? '1' : '0', pulse, period);
  141. }
  142. widget_add_text_scroll_element(app->widget, 0, 12, 128, 34, furi_string_get_cstr(buffer));
  143. if(!data_saved) {
  144. widget_add_button_element(
  145. app->widget, GuiButtonTypeLeft, "Save", wiegand_button_callback, app);
  146. }
  147. widget_add_button_element(
  148. app->widget, GuiButtonTypeCenter, "Play", wiegand_button_callback, app);
  149. view_dispatcher_switch_to_view(app->view_dispatcher, WiegandWidgetView);
  150. }
  151. bool wiegand_data_scene_on_event(void* context, SceneManagerEvent event) {
  152. App* app = context;
  153. bool consumed = false;
  154. switch(event.type) {
  155. case SceneManagerEventTypeCustom:
  156. switch(event.event) {
  157. case WiegandDataScenePlayButtonEvent:
  158. wiegand_play();
  159. consumed = true;
  160. break;
  161. case WiegandDataSceneSaveButtonEvent:
  162. scene_manager_next_scene(app->scene_manager, WiegandSaveScene);
  163. consumed = true;
  164. break;
  165. default:
  166. consumed = false;
  167. break;
  168. }
  169. break;
  170. default:
  171. break;
  172. }
  173. return consumed;
  174. }