wiegand_data.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // After the parity bit, the next 8 bits are the facility code.
  35. // Then the next 16 bits are the card id .
  36. furi_string_cat_printf(buffer, "\nFacility: ");
  37. int code = 0;
  38. int count = 0;
  39. uint32_t dec = 0;
  40. for(int i = 1; i < 25; i++) {
  41. code = code << 1;
  42. dec = dec << 1;
  43. code |= data[i] ? 1 : 0;
  44. dec |= data[i] ? 1 : 0;
  45. if(++count % 4 == 0) {
  46. furi_string_cat_printf(buffer, "%X", code);
  47. code = 0;
  48. }
  49. if(i == 8) {
  50. furi_string_cat_printf(buffer, " (%ld)", dec);
  51. dec = 0;
  52. }
  53. // Parity, then 8 bit facility code, then id.
  54. if(i == 9) {
  55. furi_string_cat_printf(buffer, "\nCard: ");
  56. }
  57. }
  58. furi_string_cat_printf(buffer, " (%ld)", dec);
  59. int parity = 0;
  60. if(data[0]) {
  61. parity = 1;
  62. }
  63. for(int i = 1; i < 13; i++) {
  64. if(data[i]) {
  65. parity++;
  66. }
  67. }
  68. if(parity % 2 == 0) {
  69. furi_string_cat_printf(buffer, "\nEven Parity (%d): OK", parity);
  70. } else {
  71. furi_string_cat_printf(buffer, "\nEven Parity (%d): ERROR", parity);
  72. }
  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, "\nCard: 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, "\nFacility: %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_35bit(FuriString* buffer) {
  139. // We assume this is HID 35 bit Corporate 1000 - C1k35s format.
  140. // 12 bits company code
  141. uint32_t code = 0;
  142. for(int i = 2; i <= 13; i++) {
  143. code = code << 1;
  144. code |= data[i] ? 1 : 0;
  145. }
  146. furi_string_cat_printf(buffer, "\nFacility: %lX (%ld)", code, code);
  147. // 20 bit card id
  148. code = 0;
  149. for(int i = 14; i <= 33; i++) {
  150. code = code << 1;
  151. code |= data[i] ? 1 : 0;
  152. }
  153. furi_string_cat_printf(buffer, "\nCard: %lX (%ld)", code, code);
  154. }
  155. void wiegand_add_info_36bit(FuriString* buffer) {
  156. // We assume this is HID 36 bit Keyscan - C15001 format.
  157. // 10 bits OEM
  158. uint32_t oem = 0;
  159. for(int i = 1; i <= 10; i++) {
  160. oem = (oem << 1) | (data[i] ? 1 : 0);
  161. }
  162. furi_string_cat_printf(buffer, "\nOEM: %lX (%ld)", oem, oem);
  163. // 8 bits facility code
  164. uint32_t facilityCode = 0;
  165. for(int i = 11; i <= 18; i++) {
  166. facilityCode = (facilityCode << 1) | (data[i] ? 1 : 0);
  167. }
  168. furi_string_cat_printf(buffer, "\nFacility: %lX (%ld)", facilityCode, facilityCode);
  169. // 16 bits card ID
  170. uint32_t cardID = 0;
  171. for(int i = 19; i <= 34; i++) {
  172. cardID = (cardID << 1) | (data[i] ? 1 : 0);
  173. }
  174. furi_string_cat_printf(buffer, "\nCard: %lX (%ld)", cardID, cardID);
  175. }
  176. void wiegand_add_info(FuriString* buffer) {
  177. furi_string_push_back(buffer, '\n');
  178. if(bit_count == 4 || bit_count == 8) {
  179. wiegand_add_info_4bit_8bit(buffer);
  180. } else if(bit_count == 26) {
  181. wiegand_add_info_26bit(buffer);
  182. } else if(bit_count == 24) {
  183. wiegand_add_info_24bit(buffer);
  184. } else if(bit_count == 35) {
  185. wiegand_add_info_35bit(buffer);
  186. } else if(bit_count == 36) {
  187. wiegand_add_info_36bit(buffer);
  188. } else if(bit_count == 48) {
  189. wiegand_add_info_48bit(buffer);
  190. }
  191. furi_string_push_back(buffer, '\n');
  192. }
  193. void wiegand_button_callback(GuiButtonType result, InputType type, void* context) {
  194. App* app = context;
  195. if(type == InputTypeShort && result == GuiButtonTypeLeft) {
  196. view_dispatcher_send_custom_event(app->view_dispatcher, WiegandDataSceneSaveButtonEvent);
  197. } else if(type == InputTypeShort && result == GuiButtonTypeCenter) {
  198. view_dispatcher_send_custom_event(app->view_dispatcher, WiegandDataScenePlayButtonEvent);
  199. }
  200. }
  201. void wiegand_data_scene_on_enter(void* context) {
  202. App* app = context;
  203. widget_reset(app->widget);
  204. widget_add_string_element(app->widget, 0, 0, AlignLeft, AlignTop, FontPrimary, "Wiegand Data");
  205. FuriString* buffer = furi_string_alloc(1024);
  206. furi_string_printf(buffer, "Bits: %d\n", bit_count);
  207. for(int i = 0; i < bit_count; i++) {
  208. furi_string_push_back(buffer, data[i] ? '1' : '0');
  209. if((bit_count - i - 1) % 22 == 21) {
  210. furi_string_push_back(buffer, '\n');
  211. }
  212. }
  213. // furi_string_cat_printf(buffer, "\nPulse: %ld us", (data_rise[0] - data_fall[0]) / 64);
  214. // furi_string_cat_printf(buffer, "\nPeriod: %ld us", (data_fall[1] - data_fall[0]) / 64);
  215. wiegand_add_info(buffer);
  216. for(int i = 0; i < bit_count;) {
  217. uint32_t pulse = (data_rise[i] - data_fall[i]) / 64;
  218. i++;
  219. uint32_t period = (i < bit_count) ? (data_fall[i] - data_fall[i - 1]) / 64 : 0;
  220. furi_string_cat_printf(
  221. buffer, "\n%c : %ld us, %ld us", data[i] ? '1' : '0', pulse, period);
  222. }
  223. widget_add_text_scroll_element(app->widget, 0, 12, 128, 34, furi_string_get_cstr(buffer));
  224. if(!data_saved) {
  225. widget_add_button_element(
  226. app->widget, GuiButtonTypeLeft, "Save", wiegand_button_callback, app);
  227. }
  228. widget_add_button_element(
  229. app->widget, GuiButtonTypeCenter, "Play", wiegand_button_callback, app);
  230. view_dispatcher_switch_to_view(app->view_dispatcher, WiegandWidgetView);
  231. }
  232. bool wiegand_data_scene_on_event(void* context, SceneManagerEvent event) {
  233. App* app = context;
  234. bool consumed = false;
  235. switch(event.type) {
  236. case SceneManagerEventTypeCustom:
  237. switch(event.event) {
  238. case WiegandDataScenePlayButtonEvent:
  239. wiegand_play();
  240. consumed = true;
  241. break;
  242. case WiegandDataSceneSaveButtonEvent:
  243. scene_manager_next_scene(app->scene_manager, WiegandSaveScene);
  244. consumed = true;
  245. break;
  246. default:
  247. consumed = false;
  248. break;
  249. }
  250. break;
  251. default:
  252. break;
  253. }
  254. return consumed;
  255. }