protocol_paradox.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <furi.h>
  2. #include <toolbox/protocols/protocol.h>
  3. #include <lfrfid/tools/fsk_demod.h>
  4. #include <lfrfid/tools/fsk_osc.h>
  5. #include <lfrfid/tools/bit_lib.h>
  6. #include "lfrfid_protocols.h"
  7. #define JITTER_TIME (20)
  8. #define MIN_TIME (64 - JITTER_TIME)
  9. #define MAX_TIME (80 + JITTER_TIME)
  10. #define PARADOX_DECODED_DATA_SIZE (6)
  11. #define PARADOX_PREAMBLE_LENGTH (8)
  12. #define PARADOX_ENCODED_BIT_SIZE (96)
  13. #define PARADOX_ENCODED_DATA_SIZE (((PARADOX_ENCODED_BIT_SIZE) / 8) + 1)
  14. #define PARADOX_ENCODED_DATA_LAST (PARADOX_ENCODED_DATA_SIZE - 1)
  15. typedef struct {
  16. FSKDemod* fsk_demod;
  17. } ProtocolParadoxDecoder;
  18. typedef struct {
  19. FSKOsc* fsk_osc;
  20. uint8_t encoded_index;
  21. } ProtocolParadoxEncoder;
  22. typedef struct {
  23. ProtocolParadoxDecoder decoder;
  24. ProtocolParadoxEncoder encoder;
  25. uint8_t encoded_data[PARADOX_ENCODED_DATA_SIZE];
  26. uint8_t data[PARADOX_DECODED_DATA_SIZE];
  27. } ProtocolParadox;
  28. ProtocolParadox* protocol_paradox_alloc(void) {
  29. ProtocolParadox* protocol = malloc(sizeof(ProtocolParadox));
  30. protocol->decoder.fsk_demod = fsk_demod_alloc(MIN_TIME, 6, MAX_TIME, 5);
  31. protocol->encoder.fsk_osc = fsk_osc_alloc(8, 10, 50);
  32. return protocol;
  33. };
  34. void protocol_paradox_free(ProtocolParadox* protocol) {
  35. fsk_demod_free(protocol->decoder.fsk_demod);
  36. fsk_osc_free(protocol->encoder.fsk_osc);
  37. free(protocol);
  38. };
  39. uint8_t* protocol_paradox_get_data(ProtocolParadox* protocol) {
  40. return protocol->data;
  41. };
  42. void protocol_paradox_decoder_start(ProtocolParadox* protocol) {
  43. memset(protocol->encoded_data, 0, PARADOX_ENCODED_DATA_SIZE);
  44. };
  45. static bool protocol_paradox_can_be_decoded(ProtocolParadox* protocol) {
  46. // check preamble
  47. if(protocol->encoded_data[0] != 0b00001111 ||
  48. protocol->encoded_data[PARADOX_ENCODED_DATA_LAST] != 0b00001111)
  49. return false;
  50. for(uint32_t i = PARADOX_PREAMBLE_LENGTH; i < 96; i += 2) {
  51. if(bit_lib_get_bit(protocol->encoded_data, i) ==
  52. bit_lib_get_bit(protocol->encoded_data, i + 1)) {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. static void protocol_paradox_decode(uint8_t* encoded_data, uint8_t* decoded_data) {
  59. for(uint32_t i = PARADOX_PREAMBLE_LENGTH; i < 96; i += 2) {
  60. if(bit_lib_get_bits(encoded_data, i, 2) == 0b01) {
  61. bit_lib_push_bit(decoded_data, PARADOX_DECODED_DATA_SIZE, 0);
  62. } else if(bit_lib_get_bits(encoded_data, i, 2) == 0b10) {
  63. bit_lib_push_bit(decoded_data, PARADOX_DECODED_DATA_SIZE, 1);
  64. }
  65. }
  66. bit_lib_push_bit(decoded_data, PARADOX_DECODED_DATA_SIZE, 0);
  67. bit_lib_push_bit(decoded_data, PARADOX_DECODED_DATA_SIZE, 0);
  68. bit_lib_push_bit(decoded_data, PARADOX_DECODED_DATA_SIZE, 0);
  69. bit_lib_push_bit(decoded_data, PARADOX_DECODED_DATA_SIZE, 0);
  70. }
  71. bool protocol_paradox_decoder_feed(ProtocolParadox* protocol, bool level, uint32_t duration) {
  72. bool value;
  73. uint32_t count;
  74. fsk_demod_feed(protocol->decoder.fsk_demod, level, duration, &value, &count);
  75. if(count > 0) {
  76. for(size_t i = 0; i < count; i++) {
  77. bit_lib_push_bit(protocol->encoded_data, PARADOX_ENCODED_DATA_SIZE, value);
  78. if(protocol_paradox_can_be_decoded(protocol)) {
  79. protocol_paradox_decode(protocol->encoded_data, protocol->data);
  80. return true;
  81. }
  82. }
  83. }
  84. return false;
  85. };
  86. static void protocol_paradox_encode(const uint8_t* decoded_data, uint8_t* encoded_data) {
  87. // preamble
  88. bit_lib_set_bits(encoded_data, 0, 0b00001111, 8);
  89. for(size_t i = 0; i < 44; i++) {
  90. if(bit_lib_get_bit(decoded_data, i)) {
  91. bit_lib_set_bits(encoded_data, PARADOX_PREAMBLE_LENGTH + i * 2, 0b10, 2);
  92. } else {
  93. bit_lib_set_bits(encoded_data, PARADOX_PREAMBLE_LENGTH + i * 2, 0b01, 2);
  94. }
  95. }
  96. };
  97. bool protocol_paradox_encoder_start(ProtocolParadox* protocol) {
  98. protocol_paradox_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  99. protocol->encoder.encoded_index = 0;
  100. fsk_osc_reset(protocol->encoder.fsk_osc);
  101. return true;
  102. };
  103. LevelDuration protocol_paradox_encoder_yield(ProtocolParadox* protocol) {
  104. bool level;
  105. uint32_t duration;
  106. bool bit = bit_lib_get_bit(protocol->encoded_data, protocol->encoder.encoded_index);
  107. bool advance = fsk_osc_next_half(protocol->encoder.fsk_osc, bit, &level, &duration);
  108. if(advance) {
  109. bit_lib_increment_index(protocol->encoder.encoded_index, PARADOX_ENCODED_BIT_SIZE);
  110. }
  111. return level_duration_make(level, duration);
  112. };
  113. void protocol_paradox_render_data(ProtocolParadox* protocol, string_t result) {
  114. uint8_t* decoded_data = protocol->data;
  115. uint8_t fc = bit_lib_get_bits(decoded_data, 10, 8);
  116. uint16_t card_id = bit_lib_get_bits_16(decoded_data, 18, 16);
  117. string_cat_printf(result, "Facility: %u\r\n", fc);
  118. string_cat_printf(result, "Card: %lu\r\n", card_id);
  119. string_cat_printf(result, "Data: ");
  120. for(size_t i = 0; i < PARADOX_DECODED_DATA_SIZE; i++) {
  121. string_cat_printf(result, "%02X", decoded_data[i]);
  122. }
  123. };
  124. void protocol_paradox_render_brief_data(ProtocolParadox* protocol, string_t result) {
  125. uint8_t* decoded_data = protocol->data;
  126. uint8_t fc = bit_lib_get_bits(decoded_data, 10, 8);
  127. uint16_t card_id = bit_lib_get_bits_16(decoded_data, 18, 16);
  128. string_cat_printf(result, "FC: %03u, Card: %05u", fc, card_id);
  129. };
  130. bool protocol_paradox_write_data(ProtocolParadox* protocol, void* data) {
  131. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  132. bool result = false;
  133. // Correct protocol data by redecoding
  134. protocol_paradox_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  135. protocol_paradox_decode(protocol->encoded_data, protocol->data);
  136. protocol_paradox_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  137. if(request->write_type == LFRFIDWriteTypeT5577) {
  138. request->t5577.block[0] = LFRFID_T5577_MODULATION_FSK2a | LFRFID_T5577_BITRATE_RF_50 |
  139. (3 << LFRFID_T5577_MAXBLOCK_SHIFT);
  140. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  141. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  142. request->t5577.block[3] = bit_lib_get_bits_32(protocol->encoded_data, 64, 32);
  143. request->t5577.blocks_to_write = 4;
  144. result = true;
  145. }
  146. return result;
  147. };
  148. const ProtocolBase protocol_paradox = {
  149. .name = "Paradox",
  150. .manufacturer = "Paradox",
  151. .data_size = PARADOX_DECODED_DATA_SIZE,
  152. .features = LFRFIDFeatureASK,
  153. .validate_count = 3,
  154. .alloc = (ProtocolAlloc)protocol_paradox_alloc,
  155. .free = (ProtocolFree)protocol_paradox_free,
  156. .get_data = (ProtocolGetData)protocol_paradox_get_data,
  157. .decoder =
  158. {
  159. .start = (ProtocolDecoderStart)protocol_paradox_decoder_start,
  160. .feed = (ProtocolDecoderFeed)protocol_paradox_decoder_feed,
  161. },
  162. .encoder =
  163. {
  164. .start = (ProtocolEncoderStart)protocol_paradox_encoder_start,
  165. .yield = (ProtocolEncoderYield)protocol_paradox_encoder_yield,
  166. },
  167. .render_data = (ProtocolRenderData)protocol_paradox_render_data,
  168. .render_brief_data = (ProtocolRenderData)protocol_paradox_render_brief_data,
  169. .write_data = (ProtocolWriteData)protocol_paradox_write_data,
  170. };