lf-rfid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. typedef enum { EventTypeTick, EventTypeKey, EventTypeRx } EventType;
  4. typedef struct {
  5. bool value;
  6. uint32_t dwt_value;
  7. } RxEvent;
  8. typedef struct {
  9. union {
  10. InputEvent input;
  11. RxEvent rx;
  12. } value;
  13. EventType type;
  14. } AppEvent;
  15. typedef struct {
  16. uint32_t freq_khz;
  17. bool on;
  18. uint8_t customer_id;
  19. uint32_t em_data;
  20. bool dirty;
  21. } State;
  22. static void render_callback(Canvas* canvas, void* ctx) {
  23. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  24. canvas_clear(canvas);
  25. canvas_set_color(canvas, ColorBlack);
  26. canvas_set_font(canvas, FontPrimary);
  27. canvas_draw_str(canvas, 2, 12, "LF RFID");
  28. canvas_draw_str(canvas, 2, 24, state->on ? "Reading" : "Emulating");
  29. char buf[14];
  30. sprintf(buf, "%d kHz", (int)state->freq_khz);
  31. canvas_draw_str(canvas, 2, 36, buf);
  32. sprintf(buf, "%02d:%010ld", state->customer_id, state->em_data);
  33. canvas_draw_str(canvas, 2, 45, buf);
  34. release_mutex((ValueMutex*)ctx, state);
  35. }
  36. static void input_callback(InputEvent* input_event, void* ctx) {
  37. osMessageQueueId_t event_queue = ctx;
  38. AppEvent event;
  39. event.type = EventTypeKey;
  40. event.value.input = *input_event;
  41. osMessageQueuePut(event_queue, &event, 0, 0);
  42. }
  43. extern TIM_HandleTypeDef TIM_C;
  44. void em4100_emulation(uint8_t* data, GpioPin* pin);
  45. void prepare_data(uint32_t ID, uint32_t VENDOR, uint8_t* data);
  46. GpioPin debug_0 = {.pin = GPIO_PIN_2, .port = GPIOB};
  47. GpioPin debug_1 = {.pin = GPIO_PIN_3, .port = GPIOC};
  48. extern COMP_HandleTypeDef hcomp1;
  49. void comparator_trigger_callback(void* hcomp, void* comp_ctx) {
  50. if((COMP_HandleTypeDef*)hcomp != &hcomp1) return;
  51. // gpio_write(&debug_0, true);
  52. osMessageQueueId_t event_queue = comp_ctx;
  53. AppEvent event;
  54. event.type = EventTypeRx;
  55. event.value.rx.value = (HAL_COMP_GetOutputLevel(hcomp) == COMP_OUTPUT_LEVEL_HIGH);
  56. event.value.rx.dwt_value = DWT->CYCCNT;
  57. osMessageQueuePut(event_queue, &event, 0, 0);
  58. // gpio_write(&debug_0, false);
  59. }
  60. const uint8_t ROW_SIZE = 4;
  61. const uint8_t LINE_SIZE = 10;
  62. static bool even_check(uint8_t* buf) {
  63. uint8_t col_parity_sum[ROW_SIZE];
  64. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  65. col_parity_sum[col] = 0;
  66. }
  67. // line parity
  68. for(uint8_t line = 0; line < LINE_SIZE; line++) {
  69. printf("%d: ", line);
  70. uint8_t parity_sum = 0;
  71. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  72. parity_sum += buf[line * (ROW_SIZE + 1) + col];
  73. col_parity_sum[col] += buf[line * (ROW_SIZE + 1) + col];
  74. printf("%d ", buf[line * (ROW_SIZE + 1) + col]);
  75. }
  76. if((1 & parity_sum) != buf[line * (ROW_SIZE + 1) + ROW_SIZE]) {
  77. printf(
  78. "line parity fail at %d (%d : %d)\n",
  79. line,
  80. parity_sum,
  81. buf[line * (ROW_SIZE + 1) + ROW_SIZE]);
  82. return false;
  83. }
  84. printf("\r\n");
  85. }
  86. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  87. if((1 & col_parity_sum[col]) != buf[LINE_SIZE * (ROW_SIZE + 1) + col]) {
  88. printf(
  89. "col parity fail at %d (%d : %d)\n",
  90. col,
  91. col_parity_sum[col],
  92. buf[LINE_SIZE * (ROW_SIZE + 1) + col]);
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. static void extract_data(uint8_t* buf, uint8_t* customer, uint32_t* em_data) {
  99. uint32_t data = 0;
  100. uint8_t offset = 0;
  101. printf("customer: ");
  102. for(uint8_t line = 0; line < 2; line++) {
  103. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  104. uint32_t bit = buf[line * (ROW_SIZE + 1) + col];
  105. data |= bit << (7 - offset);
  106. printf("%ld ", bit);
  107. offset++;
  108. }
  109. }
  110. printf("\r\n");
  111. *customer = data;
  112. data = 0;
  113. offset = 0;
  114. printf("data: ");
  115. for(uint8_t line = 2; line < LINE_SIZE; line++) {
  116. for(uint8_t col = 0; col < ROW_SIZE; col++) {
  117. uint32_t bit = buf[line * (ROW_SIZE + 1) + col];
  118. data |= bit << (31 - offset);
  119. printf("%ld ", bit);
  120. offset++;
  121. }
  122. }
  123. printf("\r\n");
  124. *em_data = data;
  125. }
  126. void lf_rfid_workaround(void* p) {
  127. osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(AppEvent), NULL);
  128. // create pin
  129. GpioPin pull_pin = {.pin = RFID_PULL_Pin, .port = RFID_PULL_GPIO_Port};
  130. // TODO open record
  131. GpioPin* pull_pin_record = &pull_pin;
  132. gpio_init(pull_pin_record, GpioModeOutputPushPull);
  133. gpio_init(&debug_0, GpioModeOutputPushPull);
  134. gpio_init(&debug_1, GpioModeOutputPushPull);
  135. // pulldown iBtn pin to prevent interference from ibutton
  136. gpio_init((GpioPin*)&ibutton_gpio, GpioModeOutputOpenDrain);
  137. gpio_write((GpioPin*)&ibutton_gpio, false);
  138. // init ctx
  139. void* comp_ctx = (void*)event_queue;
  140. // start comp
  141. HAL_COMP_Start(&hcomp1);
  142. uint8_t emulation_data[64];
  143. State _state;
  144. _state.freq_khz = 125;
  145. _state.on = false;
  146. _state.customer_id = 00;
  147. _state.em_data = 4378151;
  148. _state.dirty = true;
  149. ValueMutex state_mutex;
  150. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  151. printf("cannot create mutex\r\n");
  152. furiac_exit(NULL);
  153. }
  154. ViewPort* view_port = view_port_alloc();
  155. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  156. view_port_input_callback_set(view_port, input_callback, event_queue);
  157. // Open GUI and register view_port
  158. Gui* gui = furi_record_open("gui");
  159. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  160. AppEvent event;
  161. uint32_t prev_dwt;
  162. int8_t symbol = -1; // init state
  163. bool center = false;
  164. size_t symbol_cnt = 0;
  165. GpioPin* led_record = (GpioPin*)&led_gpio[1];
  166. gpio_init(led_record, GpioModeOutputOpenDrain);
  167. uint8_t buf[64];
  168. for(size_t i = 0; i < 64; i++) {
  169. buf[i] = 0;
  170. }
  171. while(1) {
  172. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 1024 / 8);
  173. if(event.type == EventTypeRx && event_status == osOK) {
  174. uint32_t dt = (event.value.rx.dwt_value - prev_dwt) / (SystemCoreClock / 1000000.0f);
  175. prev_dwt = event.value.rx.dwt_value;
  176. if(dt > 384) {
  177. // change symbol 0->1 or 1->0
  178. symbol = event.value.rx.value;
  179. center = true;
  180. } else {
  181. // same symbol as prev or center
  182. center = !center;
  183. }
  184. /*
  185. gpio_write(&debug_1, true);
  186. delay_us(center ? 10 : 30);
  187. gpio_write(&debug_1, false);
  188. */
  189. if(center && symbol != -1) {
  190. /*
  191. gpio_write(&debug_0, true);
  192. delay_us(symbol ? 10 : 30);
  193. gpio_write(&debug_0, false);
  194. */
  195. buf[symbol_cnt] = symbol;
  196. symbol_cnt++;
  197. }
  198. // check preamble
  199. if(symbol_cnt <= 9 && symbol == 0) {
  200. symbol_cnt = 0;
  201. symbol = -1;
  202. }
  203. // check stop bit
  204. if(symbol_cnt == 64 && symbol == 1) {
  205. symbol_cnt = 0;
  206. symbol = -1;
  207. }
  208. if(symbol_cnt == 64) {
  209. if(even_check(&buf[9])) {
  210. State* state = (State*)acquire_mutex_block(&state_mutex);
  211. extract_data(&buf[9], &state->customer_id, &state->em_data);
  212. printf("customer: %02d, data: %010lu\n", state->customer_id, state->em_data);
  213. release_mutex(&state_mutex, state);
  214. gpio_write(led_record, false);
  215. osDelay(100);
  216. gpio_write(led_record, true);
  217. }
  218. symbol_cnt = 0;
  219. }
  220. } else {
  221. State* state = (State*)acquire_mutex_block(&state_mutex);
  222. if(event_status == osOK) {
  223. if(event.type == EventTypeKey) {
  224. // press events
  225. if(event.value.input.type == InputTypePress &&
  226. event.value.input.key == InputKeyBack) {
  227. hal_pwmn_stop(&TIM_C, TIM_CHANNEL_1); // TODO: move to furiac_onexit
  228. gpio_init(pull_pin_record, GpioModeInput);
  229. gpio_init((GpioPin*)&ibutton_gpio, GpioModeInput);
  230. // TODO remove all view_ports create by app
  231. view_port_enabled_set(view_port, false);
  232. furiac_exit(NULL);
  233. }
  234. if(event.value.input.type == InputTypePress &&
  235. event.value.input.key == InputKeyUp) {
  236. state->dirty = true;
  237. state->freq_khz += 10;
  238. }
  239. if(event.value.input.type == InputTypePress &&
  240. event.value.input.key == InputKeyDown) {
  241. state->dirty = true;
  242. state->freq_khz -= 10;
  243. }
  244. if(event.value.input.type == InputTypePress &&
  245. event.value.input.key == InputKeyLeft) {
  246. }
  247. if(event.value.input.type == InputTypePress &&
  248. event.value.input.key == InputKeyRight) {
  249. }
  250. if(event.value.input.type == InputTypePress &&
  251. event.value.input.key == InputKeyOk) {
  252. state->dirty = true;
  253. state->on = !state->on;
  254. }
  255. }
  256. } else {
  257. // event timeout
  258. }
  259. if(state->dirty) {
  260. if(!state->on) {
  261. prepare_data(state->em_data, state->customer_id, emulation_data);
  262. }
  263. if(state->on) {
  264. gpio_write(pull_pin_record, false);
  265. api_interrupt_add(
  266. comparator_trigger_callback, InterruptTypeComparatorTrigger, comp_ctx);
  267. } else {
  268. api_interrupt_remove(
  269. comparator_trigger_callback, InterruptTypeComparatorTrigger);
  270. }
  271. hal_pwmn_set(
  272. state->on ? 0.5 : 0.0, (float)(state->freq_khz * 1000), &LFRFID_TIM, LFRFID_CH);
  273. state->dirty = false;
  274. }
  275. if(!state->on) {
  276. em4100_emulation(emulation_data, pull_pin_record);
  277. }
  278. // common code, for example, force update UI
  279. view_port_update(view_port);
  280. release_mutex(&state_mutex, state);
  281. }
  282. }
  283. }