protocol_paradox.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. static uint8_t protocol_paradox_calculate_checksum(uint8_t fc, uint16_t card_id) {
  114. uint8_t card_hi = (card_id >> 8) & 0xff;
  115. uint8_t card_lo = card_id & 0xff;
  116. uint8_t arr[5] = {0, 0, fc, card_hi, card_lo};
  117. uint8_t manchester[9];
  118. bit_lib_push_bit(manchester, 9, false);
  119. bit_lib_push_bit(manchester, 9, false);
  120. bit_lib_push_bit(manchester, 9, false);
  121. bit_lib_push_bit(manchester, 9, false);
  122. for(uint8_t i = 6; i < 40; i += 1) {
  123. if(bit_lib_get_bit(arr, i) == 0b1) {
  124. bit_lib_push_bit(manchester, 9, true);
  125. bit_lib_push_bit(manchester, 9, false);
  126. } else {
  127. bit_lib_push_bit(manchester, 9, false);
  128. bit_lib_push_bit(manchester, 9, true);
  129. }
  130. }
  131. uint8_t output = bit_lib_crc8(manchester, 9, 0x31, 0x00, true, true, 0x06);
  132. return output;
  133. }
  134. void protocol_paradox_render_data(ProtocolParadox* protocol, FuriString* result) {
  135. uint8_t* decoded_data = protocol->data;
  136. uint8_t fc = bit_lib_get_bits(decoded_data, 10, 8);
  137. uint16_t card_id = bit_lib_get_bits_16(decoded_data, 18, 16);
  138. uint8_t card_crc = bit_lib_get_bits_16(decoded_data, 34, 8);
  139. uint8_t calc_crc = protocol_paradox_calculate_checksum(fc, card_id);
  140. furi_string_cat_printf(result, "Facility: %u\r\n", fc);
  141. furi_string_cat_printf(result, "Card: %u\r\n", card_id);
  142. furi_string_cat_printf(result, "CRC: %u Calc CRC: %u\r\n", card_crc, calc_crc);
  143. if(card_crc != calc_crc) furi_string_cat_printf(result, "CRC Mismatch, Invalid Card!\r\n");
  144. };
  145. void protocol_paradox_render_brief_data(ProtocolParadox* protocol, FuriString* result) {
  146. uint8_t* decoded_data = protocol->data;
  147. uint8_t fc = bit_lib_get_bits(decoded_data, 10, 8);
  148. uint16_t card_id = bit_lib_get_bits_16(decoded_data, 18, 16);
  149. uint8_t card_crc = bit_lib_get_bits_16(decoded_data, 34, 8);
  150. uint8_t calc_crc = protocol_paradox_calculate_checksum(fc, card_id);
  151. furi_string_cat_printf(result, "FC: %03u, Card: %05u\r\n", fc, card_id);
  152. if(calc_crc == card_crc) {
  153. furi_string_cat_printf(result, "CRC : %03u", card_crc);
  154. } else {
  155. furi_string_cat_printf(result, "Card is Invalid!");
  156. }
  157. };
  158. bool protocol_paradox_write_data(ProtocolParadox* protocol, void* data) {
  159. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  160. bool result = false;
  161. // Correct protocol data by redecoding
  162. protocol_paradox_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  163. protocol_paradox_decode(protocol->encoded_data, protocol->data);
  164. protocol_paradox_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  165. if(request->write_type == LFRFIDWriteTypeT5577) {
  166. request->t5577.block[0] = LFRFID_T5577_MODULATION_FSK2a | LFRFID_T5577_BITRATE_RF_50 |
  167. (3 << LFRFID_T5577_MAXBLOCK_SHIFT);
  168. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  169. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  170. request->t5577.block[3] = bit_lib_get_bits_32(protocol->encoded_data, 64, 32);
  171. request->t5577.blocks_to_write = 4;
  172. result = true;
  173. }
  174. return result;
  175. };
  176. const ProtocolBase protocol_paradox = {
  177. .name = "Paradox",
  178. .manufacturer = "Paradox",
  179. .data_size = PARADOX_DECODED_DATA_SIZE,
  180. .features = LFRFIDFeatureASK,
  181. .validate_count = 3,
  182. .alloc = (ProtocolAlloc)protocol_paradox_alloc,
  183. .free = (ProtocolFree)protocol_paradox_free,
  184. .get_data = (ProtocolGetData)protocol_paradox_get_data,
  185. .decoder =
  186. {
  187. .start = (ProtocolDecoderStart)protocol_paradox_decoder_start,
  188. .feed = (ProtocolDecoderFeed)protocol_paradox_decoder_feed,
  189. },
  190. .encoder =
  191. {
  192. .start = (ProtocolEncoderStart)protocol_paradox_encoder_start,
  193. .yield = (ProtocolEncoderYield)protocol_paradox_encoder_yield,
  194. },
  195. .render_data = (ProtocolRenderData)protocol_paradox_render_data,
  196. .render_brief_data = (ProtocolRenderData)protocol_paradox_render_brief_data,
  197. .write_data = (ProtocolWriteData)protocol_paradox_write_data,
  198. };