wiegand_data.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. uint32_t dec = 0;
  54. for(int i = 1; i < 25; i++) {
  55. code = code << 1;
  56. dec = dec << 1;
  57. code |= data[i] ? 1 : 0;
  58. dec |= data[i] ? 1 : 0;
  59. if(++count % 4 == 0) {
  60. furi_string_cat_printf(buffer, "%X", code);
  61. code = 0;
  62. }
  63. if(i == 8) {
  64. furi_string_cat_printf(buffer, " (%ld)", dec);
  65. dec = 0;
  66. }
  67. // Parity, then 8 bit facility code, then id.
  68. if(i == 9) {
  69. furi_string_cat_printf(buffer, "\nId: 0x");
  70. }
  71. }
  72. furi_string_cat_printf(buffer, " (%ld)", dec);
  73. if(data[13]) {
  74. parity = 1;
  75. } else {
  76. parity = 0;
  77. }
  78. for(int i = 14; i < 26; i++) {
  79. if(data[i]) {
  80. parity++;
  81. }
  82. }
  83. if(parity % 2 == 0) {
  84. furi_string_cat_printf(buffer, "\nOdd Parity (%d): ERROR", parity);
  85. } else {
  86. furi_string_cat_printf(buffer, "\nOdd Parity (%d): OK", parity);
  87. }
  88. }
  89. void wiegand_add_info_24bit(FuriString* buffer) {
  90. // 24 bit wiegand (no parity info).
  91. // The First 8 bits are the facility code.
  92. // Then the next 16 bits are the card id.
  93. furi_string_cat_printf(buffer, "\nFacility: 0x");
  94. int code = 0;
  95. int count = 0;
  96. uint32_t dec = 0;
  97. for(int i = 0; i < 24; i++) {
  98. code = code << 1;
  99. dec = dec << 1;
  100. code |= data[i] ? 1 : 0;
  101. dec |= data[i] ? 1 : 0;
  102. if(++count % 4 == 0) {
  103. furi_string_cat_printf(buffer, "%X", code);
  104. code = 0;
  105. }
  106. // The first 8 bits are facility code, then comes id.
  107. if(i == 8) {
  108. furi_string_cat_printf(buffer, " (%ld)", dec);
  109. dec = 0;
  110. furi_string_cat_printf(buffer, "\nId: 0x");
  111. }
  112. }
  113. furi_string_cat_printf(buffer, " (%ld)", dec);
  114. }
  115. void wiegand_add_info_48bit(FuriString* buffer) {
  116. // We assume this is HID 48 bit Corporate 1000 - H2004064 format.
  117. // The first bit is odd parity 2 (based on bits 2-48).
  118. // The next bit is even parity (based on 4-5,7-8,10-11,...,46-47).
  119. // Then 22 bit company code.
  120. // Then 23 bit card id.
  121. /// Then odd parity 1 (based on 3-4,6-7,9-10,...,45-46).
  122. // 22 bits company code (bits 3-24; data[2..23])
  123. uint32_t code = 0;
  124. for(int i = 2; i <= 23; i++) {
  125. code = code << 1;
  126. code |= data[i] ? 1 : 0;
  127. }
  128. furi_string_cat_printf(buffer, "\nCompany: %lX (%ld)", code, code);
  129. // 23 bit card id (bits 25-47; data[24..46]).
  130. code = 0;
  131. for(int i = 24; i <= 46; i++) {
  132. code = code << 1;
  133. code |= data[i] ? 1 : 0;
  134. }
  135. furi_string_cat_printf(buffer, "\nCard: %lX (%ld)", code, code);
  136. // TODO: Add the 3 parity checks.
  137. }
  138. void wiegand_add_info(FuriString* buffer) {
  139. furi_string_push_back(buffer, '\n');
  140. if(bit_count == 4 || bit_count == 8) {
  141. wiegand_add_info_4bit_8bit(buffer);
  142. } else if(bit_count == 26) {
  143. wiegand_add_info_26bit(buffer);
  144. } else if(bit_count == 24) {
  145. wiegand_add_info_24bit(buffer);
  146. } else if(bit_count == 48) {
  147. wiegand_add_info_48bit(buffer);
  148. }
  149. furi_string_push_back(buffer, '\n');
  150. }
  151. void wiegand_button_callback(GuiButtonType result, InputType type, void* context) {
  152. App* app = context;
  153. if(type == InputTypeShort && result == GuiButtonTypeLeft) {
  154. view_dispatcher_send_custom_event(app->view_dispatcher, WiegandDataSceneSaveButtonEvent);
  155. } else if(type == InputTypeShort && result == GuiButtonTypeCenter) {
  156. view_dispatcher_send_custom_event(app->view_dispatcher, WiegandDataScenePlayButtonEvent);
  157. }
  158. }
  159. void wiegand_data_scene_on_enter(void* context) {
  160. App* app = context;
  161. widget_reset(app->widget);
  162. widget_add_string_element(app->widget, 0, 0, AlignLeft, AlignTop, FontPrimary, "Wiegand Data");
  163. FuriString* buffer = furi_string_alloc(1024);
  164. furi_string_printf(buffer, "Bits: %d\n", bit_count);
  165. for(int i = 0; i < bit_count; i++) {
  166. furi_string_push_back(buffer, data[i] ? '1' : '0');
  167. if((bit_count - i - 1) % 22 == 21) {
  168. furi_string_push_back(buffer, '\n');
  169. }
  170. }
  171. furi_string_cat_printf(buffer, "\nPulse: %ld us", (data_rise[0] - data_fall[0]) / 64);
  172. furi_string_cat_printf(buffer, "\nPeriod: %ld us", (data_fall[1] - data_fall[0]) / 64);
  173. wiegand_add_info(buffer);
  174. for(int i = 0; i < bit_count;) {
  175. uint32_t pulse = (data_rise[i] - data_fall[i]) / 64;
  176. i++;
  177. uint32_t period = (i < bit_count) ? (data_fall[i] - data_fall[i - 1]) / 64 : 0;
  178. furi_string_cat_printf(
  179. buffer, "\n%c : %ld us, %ld us", data[i] ? '1' : '0', pulse, period);
  180. }
  181. widget_add_text_scroll_element(app->widget, 0, 12, 128, 34, furi_string_get_cstr(buffer));
  182. if(!data_saved) {
  183. widget_add_button_element(
  184. app->widget, GuiButtonTypeLeft, "Save", wiegand_button_callback, app);
  185. }
  186. widget_add_button_element(
  187. app->widget, GuiButtonTypeCenter, "Play", wiegand_button_callback, app);
  188. view_dispatcher_switch_to_view(app->view_dispatcher, WiegandWidgetView);
  189. }
  190. bool wiegand_data_scene_on_event(void* context, SceneManagerEvent event) {
  191. App* app = context;
  192. bool consumed = false;
  193. switch(event.type) {
  194. case SceneManagerEventTypeCustom:
  195. switch(event.event) {
  196. case WiegandDataScenePlayButtonEvent:
  197. wiegand_play();
  198. consumed = true;
  199. break;
  200. case WiegandDataSceneSaveButtonEvent:
  201. scene_manager_next_scene(app->scene_manager, WiegandSaveScene);
  202. consumed = true;
  203. break;
  204. default:
  205. consumed = false;
  206. break;
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. return consumed;
  213. }