lf-rfid.c 11 KB

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