cyfral_reader_comp.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #pragma once
  2. #include "flipper.h"
  3. #include "flipper_v2.h"
  4. #include "callback-connector.h"
  5. #include <atomic>
  6. enum class CyfralReaderCompError : uint8_t {
  7. NO_ERROR = 0,
  8. UNABLE_TO_DETECT = 1,
  9. RAW_DATA_SIZE_ERROR = 2,
  10. UNKNOWN_NIBBLE_VALUE = 3,
  11. NO_START_NIBBLE = 4,
  12. NOT_ENOUGH_DATA = 5,
  13. };
  14. extern COMP_HandleTypeDef hcomp1;
  15. typedef struct {
  16. bool value;
  17. uint32_t dwt_value;
  18. } CompEvent;
  19. class CyfralReaderComp {
  20. private:
  21. bool capture_data(bool* data, uint16_t capture_size);
  22. bool parse_data(bool* raw_data, uint16_t capture_size, uint8_t* data, uint8_t count);
  23. uint32_t search_array_in_array(
  24. const bool* haystack,
  25. const uint32_t haystack_size,
  26. const bool* needle,
  27. const uint32_t needle_size);
  28. // key is 9 nibbles
  29. static const uint16_t bits_in_nibble = 4;
  30. static const uint16_t key_length = 9;
  31. static const uint32_t capture_size = key_length * bits_in_nibble * 2;
  32. CyfralReaderCompError error;
  33. const GpioPin* pin_record;
  34. std::atomic<bool> ready_to_process;
  35. void comparator_trigger_callback(void* hcomp, void* comp_ctx);
  36. osMessageQueueId_t comp_event_queue;
  37. public:
  38. CyfralReaderComp(const GpioPin* emulate_pin);
  39. ~CyfralReaderComp();
  40. void start(void);
  41. void stop(void);
  42. bool read(uint8_t* data, uint8_t count);
  43. };
  44. bool CyfralReaderComp::capture_data(bool* data, uint16_t capture_size) {
  45. uint32_t prev_timing = 0;
  46. uint16_t data_index = 0;
  47. CompEvent event_0, event_1;
  48. osStatus_t status;
  49. // read first event to get initial timing
  50. status = osMessageQueueGet(comp_event_queue, &event_0, NULL, 0);
  51. if(status != osOK) {
  52. return false;
  53. }
  54. prev_timing = event_0.dwt_value;
  55. // read second event until we get 0
  56. while(1) {
  57. status = osMessageQueueGet(comp_event_queue, &event_0, NULL, 0);
  58. if(status != osOK) {
  59. return false;
  60. }
  61. prev_timing = event_0.dwt_value;
  62. if(event_0.value == 0) break;
  63. }
  64. while(1) {
  65. // if event "zero" correct
  66. if(status == osOK && event_0.value == 0) {
  67. // get timing
  68. event_0.dwt_value -= prev_timing;
  69. prev_timing += event_0.dwt_value;
  70. // read next event
  71. status = osMessageQueueGet(comp_event_queue, &event_1, NULL, 0);
  72. // if event "one" correct
  73. if(status == osOK && event_1.value == 1) {
  74. // get timing
  75. event_1.dwt_value -= prev_timing;
  76. prev_timing += event_1.dwt_value;
  77. // calculate percentage of event "one" to full timing
  78. uint32_t full_timing = event_0.dwt_value + event_1.dwt_value;
  79. uint32_t percentage_1 = 1000000 / full_timing * event_1.dwt_value;
  80. // write captured data
  81. data[data_index] = percentage_1 > 500000 ? 0 : 1;
  82. data_index++;
  83. if(data_index >= capture_size) return true;
  84. status = osMessageQueueGet(comp_event_queue, &event_0, NULL, 0);
  85. } else {
  86. return false;
  87. }
  88. } else {
  89. return false;
  90. }
  91. }
  92. osMessageQueueReset(comp_event_queue);
  93. }
  94. uint32_t CyfralReaderComp::search_array_in_array(
  95. const bool* haystack,
  96. const uint32_t haystack_size,
  97. const bool* needle,
  98. const uint32_t needle_size) {
  99. uint32_t haystack_index = 0, needle_index = 0;
  100. while(haystack_index < haystack_size && needle_index < needle_size) {
  101. if(haystack[haystack_index] == needle[needle_index]) {
  102. haystack_index++;
  103. needle_index++;
  104. if(needle_index == needle_size) {
  105. return (haystack_index - needle_size);
  106. };
  107. } else {
  108. haystack_index = haystack_index - needle_index + 1;
  109. needle_index = 0;
  110. }
  111. }
  112. return haystack_index;
  113. }
  114. void CyfralReaderComp::comparator_trigger_callback(void* hcomp, void* comp_ctx) {
  115. CyfralReaderComp* _this = static_cast<CyfralReaderComp*>(comp_ctx);
  116. COMP_HandleTypeDef* _hcomp = static_cast<COMP_HandleTypeDef*>(hcomp);
  117. // check that hw is comparator 1
  118. if(_hcomp != &hcomp1) return;
  119. // if queue if not full
  120. if(_this->ready_to_process == false) {
  121. // send event to queue
  122. CompEvent event;
  123. event.value = (HAL_COMP_GetOutputLevel(_hcomp) == COMP_OUTPUT_LEVEL_HIGH);
  124. event.dwt_value = DWT->CYCCNT;
  125. osStatus_t status = osMessageQueuePut(_this->comp_event_queue, &event, 0, 0);
  126. // queue is full, so we need to process data
  127. if(status != osOK) {
  128. _this->ready_to_process = true;
  129. };
  130. }
  131. }
  132. bool CyfralReaderComp::parse_data(
  133. bool* raw_data,
  134. uint16_t capture_size,
  135. uint8_t* data,
  136. uint8_t count) {
  137. const bool start_nibble[bits_in_nibble] = {1, 1, 1, 0};
  138. uint32_t start_position =
  139. search_array_in_array(raw_data, capture_size, start_nibble, bits_in_nibble);
  140. uint32_t end_position = 0;
  141. memset(data, 0, count);
  142. if(start_position < capture_size) {
  143. start_position = start_position + bits_in_nibble;
  144. end_position = start_position + count * 2 * bits_in_nibble;
  145. if(end_position >= capture_size) {
  146. error = CyfralReaderCompError::RAW_DATA_SIZE_ERROR;
  147. return false;
  148. }
  149. bool first_nibble = true;
  150. uint8_t data_position = 0;
  151. uint8_t nibble_value = 0;
  152. while(data_position < count) {
  153. nibble_value = !raw_data[start_position] << 3 | !raw_data[start_position + 1] << 2 |
  154. !raw_data[start_position + 2] << 1 | !raw_data[start_position + 3];
  155. switch(nibble_value) {
  156. case(0x7):
  157. case(0xB):
  158. case(0xD):
  159. case(0xE):
  160. break;
  161. default:
  162. error = CyfralReaderCompError::UNKNOWN_NIBBLE_VALUE;
  163. return false;
  164. break;
  165. }
  166. if(first_nibble) {
  167. data[data_position] |= nibble_value << 4;
  168. } else {
  169. data[data_position] |= nibble_value;
  170. }
  171. first_nibble = !first_nibble;
  172. if(first_nibble) {
  173. data_position++;
  174. }
  175. start_position = start_position + bits_in_nibble;
  176. }
  177. error = CyfralReaderCompError::NO_ERROR;
  178. return true;
  179. }
  180. error = CyfralReaderCompError::NO_START_NIBBLE;
  181. return false;
  182. }
  183. CyfralReaderComp::CyfralReaderComp(const GpioPin* gpio_pin) {
  184. pin_record = gpio_pin;
  185. }
  186. CyfralReaderComp::~CyfralReaderComp() {
  187. }
  188. void CyfralReaderComp::start(void) {
  189. // pulldown lf-rfid pins to prevent interference
  190. // TODO open record
  191. GpioPin rfid_pull_pin = {.port = RFID_PULL_GPIO_Port, .pin = RFID_PULL_Pin};
  192. gpio_init((GpioPin*)&rfid_pull_pin, GpioModeOutputOpenDrain);
  193. gpio_write((GpioPin*)&rfid_pull_pin, false);
  194. // TODO open record
  195. GpioPin rfid_out_pin = {.port = RFID_OUT_GPIO_Port, .pin = RFID_OUT_Pin};
  196. gpio_init((GpioPin*)&rfid_out_pin, GpioModeOutputOpenDrain);
  197. gpio_write((GpioPin*)&rfid_out_pin, false);
  198. // connect comparator callback
  199. void* comp_ctx = this;
  200. comp_event_queue = osMessageQueueNew(capture_size * 2 + 2, sizeof(CompEvent), NULL);
  201. ready_to_process = false;
  202. auto cmp_cb = cbc::obtain_connector(this, &CyfralReaderComp::comparator_trigger_callback);
  203. api_interrupt_add(cmp_cb, InterruptTypeComparatorTrigger, comp_ctx);
  204. // start comaparator
  205. HAL_COMP_Start(&hcomp1);
  206. }
  207. void CyfralReaderComp::stop(void) {
  208. // stop comaparator
  209. HAL_COMP_Stop(&hcomp1);
  210. // disconnect comparator callback
  211. auto cmp_cb = cbc::obtain_connector(this, &CyfralReaderComp::comparator_trigger_callback);
  212. api_interrupt_remove(cmp_cb);
  213. osMessageQueueDelete(comp_event_queue);
  214. }
  215. bool CyfralReaderComp::read(uint8_t* data, uint8_t count) {
  216. bool raw_data[capture_size];
  217. bool result = false;
  218. error = CyfralReaderCompError::NO_ERROR;
  219. if(ready_to_process == false) {
  220. error = CyfralReaderCompError::NOT_ENOUGH_DATA;
  221. } else {
  222. memset(raw_data, 0, sizeof(bool) * capture_size);
  223. if(capture_data(raw_data, capture_size)) {
  224. if(parse_data(raw_data, capture_size, data, count)) {
  225. result = true;
  226. }
  227. }
  228. ready_to_process = false;
  229. }
  230. return result;
  231. }