lf-rfid.c 9.9 KB

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