protocol_idteck.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include <furi.h>
  2. #include <toolbox/protocols/protocol.h>
  3. #include <lfrfid/tools/bit_lib.h>
  4. #include "lfrfid_protocols.h"
  5. // Example: 4944544B 351FBE4B
  6. // 01001001 01000100 01010100 01001011 00110101 00011111 10111110 01001011
  7. // 4 9 4 4 5 4 4 B 3 5 1 F B E 4 B
  8. // 0100 1001 0100 0100 0101 0100 0100 1011 0011 0101 0001 1111 1011 1110 0100 1011
  9. #define IDTECK_PREAMBLE_BIT_SIZE (32)
  10. #define IDTECK_PREAMBLE_DATA_SIZE (8)
  11. #define IDTECK_ENCODED_BIT_SIZE (64)
  12. #define IDTECK_ENCODED_DATA_SIZE (((IDTECK_ENCODED_BIT_SIZE) / 8) + IDTECK_PREAMBLE_DATA_SIZE)
  13. #define IDTECK_ENCODED_DATA_LAST ((IDTECK_ENCODED_BIT_SIZE) / 8)
  14. #define IDTECK_DECODED_BIT_SIZE (64)
  15. #define IDTECK_DECODED_DATA_SIZE (8)
  16. #define IDTECK_US_PER_BIT (255)
  17. #define IDTECK_ENCODER_PULSES_PER_BIT (16)
  18. typedef struct {
  19. uint8_t data_index;
  20. uint8_t bit_clock_index;
  21. bool last_bit;
  22. bool current_polarity;
  23. bool pulse_phase;
  24. } ProtocolIdteckEncoder;
  25. typedef struct {
  26. uint8_t encoded_data[IDTECK_ENCODED_DATA_SIZE];
  27. uint8_t negative_encoded_data[IDTECK_ENCODED_DATA_SIZE];
  28. uint8_t corrupted_encoded_data[IDTECK_ENCODED_DATA_SIZE];
  29. uint8_t corrupted_negative_encoded_data[IDTECK_ENCODED_DATA_SIZE];
  30. uint8_t data[IDTECK_DECODED_DATA_SIZE];
  31. ProtocolIdteckEncoder encoder;
  32. } ProtocolIdteck;
  33. ProtocolIdteck* protocol_idteck_alloc(void) {
  34. ProtocolIdteck* protocol = malloc(sizeof(ProtocolIdteck));
  35. return protocol;
  36. };
  37. void protocol_idteck_free(ProtocolIdteck* protocol) {
  38. free(protocol);
  39. };
  40. uint8_t* protocol_idteck_get_data(ProtocolIdteck* protocol) {
  41. return protocol->data;
  42. };
  43. void protocol_idteck_decoder_start(ProtocolIdteck* protocol) {
  44. memset(protocol->encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
  45. memset(protocol->negative_encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
  46. memset(protocol->corrupted_encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
  47. memset(protocol->corrupted_negative_encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
  48. };
  49. static bool protocol_idteck_check_preamble(uint8_t* data, size_t bit_index) {
  50. // Preamble 01001001 01000100 01010100 01001011
  51. if(*(uint32_t*)&data[bit_index / 8] != 0b01001011010101000100010001001001) return false;
  52. return true;
  53. }
  54. static bool protocol_idteck_can_be_decoded(uint8_t* data) {
  55. if(!protocol_idteck_check_preamble(data, 0)) return false;
  56. return true;
  57. }
  58. static bool protocol_idteck_decoder_feed_internal(bool polarity, uint32_t time, uint8_t* data) {
  59. time += (IDTECK_US_PER_BIT / 2);
  60. size_t bit_count = (time / IDTECK_US_PER_BIT);
  61. bool result = false;
  62. if(bit_count < IDTECK_ENCODED_BIT_SIZE) {
  63. for(size_t i = 0; i < bit_count; i++) {
  64. bit_lib_push_bit(data, IDTECK_ENCODED_DATA_SIZE, polarity);
  65. if(protocol_idteck_can_be_decoded(data)) {
  66. result = true;
  67. break;
  68. }
  69. }
  70. }
  71. return result;
  72. }
  73. static void protocol_idteck_decoder_save(uint8_t* data_to, const uint8_t* data_from) {
  74. bit_lib_copy_bits(data_to, 0, 64, data_from, 0);
  75. }
  76. bool protocol_idteck_decoder_feed(ProtocolIdteck* protocol, bool level, uint32_t duration) {
  77. bool result = false;
  78. if(duration > (IDTECK_US_PER_BIT / 2)) {
  79. if(protocol_idteck_decoder_feed_internal(level, duration, protocol->encoded_data)) {
  80. protocol_idteck_decoder_save(protocol->data, protocol->encoded_data);
  81. FURI_LOG_D("Idteck", "Positive");
  82. result = true;
  83. return result;
  84. }
  85. if(protocol_idteck_decoder_feed_internal(
  86. !level, duration, protocol->negative_encoded_data)) {
  87. protocol_idteck_decoder_save(protocol->data, protocol->negative_encoded_data);
  88. FURI_LOG_D("Idteck", "Negative");
  89. result = true;
  90. return result;
  91. }
  92. }
  93. if(duration > (IDTECK_US_PER_BIT / 4)) {
  94. // Try to decode wrong phase synced data
  95. if(level) {
  96. duration += 120;
  97. } else {
  98. if(duration > 120) {
  99. duration -= 120;
  100. }
  101. }
  102. if(protocol_idteck_decoder_feed_internal(
  103. level, duration, protocol->corrupted_encoded_data)) {
  104. protocol_idteck_decoder_save(protocol->data, protocol->corrupted_encoded_data);
  105. FURI_LOG_D("Idteck", "Positive Corrupted");
  106. result = true;
  107. return result;
  108. }
  109. if(protocol_idteck_decoder_feed_internal(
  110. !level, duration, protocol->corrupted_negative_encoded_data)) {
  111. protocol_idteck_decoder_save(
  112. protocol->data, protocol->corrupted_negative_encoded_data);
  113. FURI_LOG_D("Idteck", "Negative Corrupted");
  114. result = true;
  115. return result;
  116. }
  117. }
  118. return result;
  119. };
  120. bool protocol_idteck_encoder_start(ProtocolIdteck* protocol) {
  121. memset(protocol->encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
  122. *(uint32_t*)&protocol->encoded_data[0] = 0b01001011010101000100010001001001;
  123. bit_lib_copy_bits(protocol->encoded_data, 32, 32, protocol->data, 32);
  124. protocol->encoder.last_bit =
  125. bit_lib_get_bit(protocol->encoded_data, IDTECK_ENCODED_BIT_SIZE - 1);
  126. protocol->encoder.data_index = 0;
  127. protocol->encoder.current_polarity = true;
  128. protocol->encoder.pulse_phase = true;
  129. protocol->encoder.bit_clock_index = 0;
  130. return true;
  131. };
  132. LevelDuration protocol_idteck_encoder_yield(ProtocolIdteck* protocol) {
  133. LevelDuration level_duration;
  134. ProtocolIdteckEncoder* encoder = &protocol->encoder;
  135. if(encoder->pulse_phase) {
  136. level_duration = level_duration_make(encoder->current_polarity, 1);
  137. encoder->pulse_phase = false;
  138. } else {
  139. level_duration = level_duration_make(!encoder->current_polarity, 1);
  140. encoder->pulse_phase = true;
  141. encoder->bit_clock_index++;
  142. if(encoder->bit_clock_index >= IDTECK_ENCODER_PULSES_PER_BIT) {
  143. encoder->bit_clock_index = 0;
  144. bool current_bit = bit_lib_get_bit(protocol->encoded_data, encoder->data_index);
  145. if(current_bit != encoder->last_bit) {
  146. encoder->current_polarity = !encoder->current_polarity;
  147. }
  148. encoder->last_bit = current_bit;
  149. bit_lib_increment_index(encoder->data_index, IDTECK_ENCODED_BIT_SIZE);
  150. }
  151. }
  152. return level_duration;
  153. };
  154. // factory code
  155. static uint32_t get_fc(const uint8_t* data) {
  156. uint32_t fc = 0;
  157. fc = bit_lib_get_bits_32(data, 0, 32);
  158. return fc;
  159. }
  160. // card number
  161. static uint32_t get_card(const uint8_t* data) {
  162. uint32_t cn = 0;
  163. cn = bit_lib_get_bits_32(data, 32, 32);
  164. return cn;
  165. }
  166. void protocol_idteck_render_data_internal(ProtocolIdteck* protocol, FuriString* result, bool brief) {
  167. const uint32_t fc = get_fc(protocol->data);
  168. const uint32_t card = get_card(protocol->data);
  169. if(brief) {
  170. furi_string_printf(result, "FC: %08lX\r\nCard: %08lX", fc, card);
  171. } else {
  172. furi_string_printf(
  173. result,
  174. "FC: %08lX\r\n"
  175. "Card: %08lX\r\n",
  176. fc,
  177. card);
  178. }
  179. }
  180. void protocol_idteck_render_data(ProtocolIdteck* protocol, FuriString* result) {
  181. protocol_idteck_render_data_internal(protocol, result, false);
  182. }
  183. void protocol_idteck_render_brief_data(ProtocolIdteck* protocol, FuriString* result) {
  184. protocol_idteck_render_data_internal(protocol, result, true);
  185. }
  186. bool protocol_idteck_write_data(ProtocolIdteck* protocol, void* data) {
  187. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  188. bool result = false;
  189. protocol_idteck_encoder_start(protocol);
  190. if(request->write_type == LFRFIDWriteTypeT5577) {
  191. request->t5577.block[0] = LFRFID_T5577_BITRATE_RF_32 | LFRFID_T5577_MODULATION_PSK1 |
  192. (2 << LFRFID_T5577_MAXBLOCK_SHIFT);
  193. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  194. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  195. request->t5577.blocks_to_write = 3;
  196. result = true;
  197. }
  198. return result;
  199. };
  200. const ProtocolBase protocol_idteck = {
  201. .name = "Idteck",
  202. .manufacturer = "IDTECK",
  203. .data_size = IDTECK_DECODED_DATA_SIZE,
  204. .features = LFRFIDFeaturePSK,
  205. .validate_count = 6,
  206. .alloc = (ProtocolAlloc)protocol_idteck_alloc,
  207. .free = (ProtocolFree)protocol_idteck_free,
  208. .get_data = (ProtocolGetData)protocol_idteck_get_data,
  209. .decoder =
  210. {
  211. .start = (ProtocolDecoderStart)protocol_idteck_decoder_start,
  212. .feed = (ProtocolDecoderFeed)protocol_idteck_decoder_feed,
  213. },
  214. .encoder =
  215. {
  216. .start = (ProtocolEncoderStart)protocol_idteck_encoder_start,
  217. .yield = (ProtocolEncoderYield)protocol_idteck_encoder_yield,
  218. },
  219. .render_data = (ProtocolRenderData)protocol_idteck_render_data,
  220. .render_brief_data = (ProtocolRenderData)protocol_idteck_render_brief_data,
  221. .write_data = (ProtocolWriteData)protocol_idteck_write_data,
  222. };