protocol_pyramid.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_protocols.h"
  6. #include <lfrfid/tools/bit_lib.h>
  7. #define JITTER_TIME (20)
  8. #define MIN_TIME (64 - JITTER_TIME)
  9. #define MAX_TIME (80 + JITTER_TIME)
  10. #define PYRAMID_DATA_SIZE 13
  11. #define PYRAMID_PREAMBLE_SIZE 3
  12. #define PYRAMID_ENCODED_DATA_SIZE \
  13. (PYRAMID_PREAMBLE_SIZE + PYRAMID_DATA_SIZE + PYRAMID_PREAMBLE_SIZE)
  14. #define PYRAMID_ENCODED_BIT_SIZE ((PYRAMID_PREAMBLE_SIZE + PYRAMID_DATA_SIZE) * 8)
  15. #define PYRAMID_DECODED_DATA_SIZE (4)
  16. #define PYRAMID_DECODED_BIT_SIZE ((PYRAMID_ENCODED_BIT_SIZE - PYRAMID_PREAMBLE_SIZE * 8) / 2)
  17. typedef struct {
  18. FSKDemod* fsk_demod;
  19. } ProtocolPyramidDecoder;
  20. typedef struct {
  21. FSKOsc* fsk_osc;
  22. uint8_t encoded_index;
  23. uint32_t pulse;
  24. } ProtocolPyramidEncoder;
  25. typedef struct {
  26. ProtocolPyramidDecoder decoder;
  27. ProtocolPyramidEncoder encoder;
  28. uint8_t encoded_data[PYRAMID_ENCODED_DATA_SIZE];
  29. uint8_t data[PYRAMID_DECODED_DATA_SIZE];
  30. } ProtocolPyramid;
  31. ProtocolPyramid* protocol_pyramid_alloc(void) {
  32. ProtocolPyramid* protocol = malloc(sizeof(ProtocolPyramid));
  33. protocol->decoder.fsk_demod = fsk_demod_alloc(MIN_TIME, 6, MAX_TIME, 5);
  34. protocol->encoder.fsk_osc = fsk_osc_alloc(8, 10, 50);
  35. return protocol;
  36. };
  37. void protocol_pyramid_free(ProtocolPyramid* protocol) {
  38. fsk_demod_free(protocol->decoder.fsk_demod);
  39. fsk_osc_free(protocol->encoder.fsk_osc);
  40. free(protocol);
  41. };
  42. uint8_t* protocol_pyramid_get_data(ProtocolPyramid* protocol) {
  43. return protocol->data;
  44. };
  45. void protocol_pyramid_decoder_start(ProtocolPyramid* protocol) {
  46. memset(protocol->encoded_data, 0, PYRAMID_ENCODED_DATA_SIZE);
  47. };
  48. static bool protocol_pyramid_can_be_decoded(uint8_t* data) {
  49. // check preamble
  50. if(bit_lib_get_bits_16(data, 0, 16) != 0b0000000000000001 ||
  51. bit_lib_get_bits(data, 16, 8) != 0b00000001) {
  52. return false;
  53. }
  54. if(bit_lib_get_bits_16(data, 128, 16) != 0b0000000000000001 ||
  55. bit_lib_get_bits(data, 136, 8) != 0b00000001) {
  56. return false;
  57. }
  58. uint8_t checksum = bit_lib_get_bits(data, 120, 8);
  59. uint8_t checksum_data[13] = {0x00};
  60. for(uint8_t i = 0; i < 13; i++) {
  61. checksum_data[i] = bit_lib_get_bits(data, 16 + (i * 8), 8);
  62. }
  63. uint8_t calc_checksum = bit_lib_crc8(checksum_data, 13, 0x31, 0x00, true, true, 0x00);
  64. if(checksum != calc_checksum) return false;
  65. // Remove parity
  66. bit_lib_remove_bit_every_nth(data, 8, 15 * 8, 8);
  67. // Determine Startbit and format
  68. int j;
  69. for(j = 0; j < 105; ++j) {
  70. if(bit_lib_get_bit(data, j)) break;
  71. }
  72. uint8_t fmt_len = 105 - j;
  73. // Only suppport 26bit format for now
  74. if(fmt_len != 26) return false;
  75. return true;
  76. }
  77. static void protocol_pyramid_decode(ProtocolPyramid* protocol) {
  78. // Format
  79. bit_lib_set_bits(protocol->data, 0, 26, 8);
  80. // Facility Code
  81. bit_lib_copy_bits(protocol->data, 8, 8, protocol->encoded_data, 73 + 8);
  82. // Card Number
  83. bit_lib_copy_bits(protocol->data, 16, 16, protocol->encoded_data, 81 + 8);
  84. }
  85. bool protocol_pyramid_decoder_feed(ProtocolPyramid* protocol, bool level, uint32_t duration) {
  86. bool value;
  87. uint32_t count;
  88. bool result = false;
  89. fsk_demod_feed(protocol->decoder.fsk_demod, level, duration, &value, &count);
  90. if(count > 0) {
  91. for(size_t i = 0; i < count; i++) {
  92. bit_lib_push_bit(protocol->encoded_data, PYRAMID_ENCODED_DATA_SIZE, value);
  93. if(protocol_pyramid_can_be_decoded(protocol->encoded_data)) {
  94. protocol_pyramid_decode(protocol);
  95. result = true;
  96. }
  97. }
  98. }
  99. return result;
  100. };
  101. bool protocol_pyramid_get_parity(const uint8_t* bits, uint8_t type, int length) {
  102. int x;
  103. for(x = 0; length > 0; --length) x += bit_lib_get_bit(bits, length - 1);
  104. x %= 2;
  105. return x ^ type;
  106. }
  107. void protocol_pyramid_add_wiegand_parity(
  108. uint8_t* target,
  109. uint8_t target_position,
  110. uint8_t* source,
  111. uint8_t length) {
  112. bit_lib_set_bit(
  113. target, target_position, protocol_pyramid_get_parity(source, 0 /* even */, length / 2));
  114. bit_lib_copy_bits(target, target_position + 1, length, source, 0);
  115. bit_lib_set_bit(
  116. target,
  117. target_position + length + 1,
  118. protocol_pyramid_get_parity(source + length / 2, 1 /* odd */, length / 2));
  119. }
  120. static void protocol_pyramid_encode(ProtocolPyramid* protocol) {
  121. memset(protocol->encoded_data, 0, sizeof(protocol->encoded_data));
  122. uint8_t pre[16];
  123. memset(pre, 0, sizeof(pre));
  124. // Format start bit
  125. bit_lib_set_bit(pre, 79, 1);
  126. uint8_t wiegand[3];
  127. memset(wiegand, 0, sizeof(wiegand));
  128. // FC
  129. bit_lib_copy_bits(wiegand, 0, 8, protocol->data, 8);
  130. // CardNum
  131. bit_lib_copy_bits(wiegand, 8, 16, protocol->data, 16);
  132. // Wiegand parity
  133. protocol_pyramid_add_wiegand_parity(pre, 80, wiegand, 24);
  134. bit_lib_add_parity(pre, 8, protocol->encoded_data, 8, 102, 8, 1);
  135. // Add checksum
  136. uint8_t checksum_buffer[13];
  137. for(uint8_t i = 0; i < 13; i++)
  138. checksum_buffer[i] = bit_lib_get_bits(protocol->encoded_data, 16 + (i * 8), 8);
  139. uint8_t crc = bit_lib_crc8(checksum_buffer, 13, 0x31, 0x00, true, true, 0x00);
  140. bit_lib_set_bits(protocol->encoded_data, 120, crc, 8);
  141. }
  142. bool protocol_pyramid_encoder_start(ProtocolPyramid* protocol) {
  143. protocol->encoder.encoded_index = 0;
  144. protocol->encoder.pulse = 0;
  145. protocol_pyramid_encode(protocol);
  146. return true;
  147. };
  148. LevelDuration protocol_pyramid_encoder_yield(ProtocolPyramid* protocol) {
  149. bool level = 0;
  150. uint32_t duration = 0;
  151. // if pulse is zero, we need to output high, otherwise we need to output low
  152. if(protocol->encoder.pulse == 0) {
  153. // get bit
  154. uint8_t bit = bit_lib_get_bit(protocol->encoded_data, protocol->encoder.encoded_index);
  155. // get pulse from oscillator
  156. bool advance = fsk_osc_next(protocol->encoder.fsk_osc, bit, &duration);
  157. if(advance) {
  158. bit_lib_increment_index(protocol->encoder.encoded_index, PYRAMID_ENCODED_BIT_SIZE);
  159. }
  160. // duration diveded by 2 because we need to output high and low
  161. duration = duration / 2;
  162. protocol->encoder.pulse = duration;
  163. level = true;
  164. } else {
  165. // output low half and reset pulse
  166. duration = protocol->encoder.pulse;
  167. protocol->encoder.pulse = 0;
  168. level = false;
  169. }
  170. return level_duration_make(level, duration);
  171. };
  172. bool protocol_pyramid_write_data(ProtocolPyramid* protocol, void* data) {
  173. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  174. bool result = false;
  175. // Correct protocol data by redecoding
  176. protocol_pyramid_encode(protocol);
  177. bit_lib_remove_bit_every_nth(protocol->encoded_data, 8, 15 * 8, 8);
  178. protocol_pyramid_decode(protocol);
  179. protocol_pyramid_encoder_start(protocol);
  180. if(request->write_type == LFRFIDWriteTypeT5577) {
  181. request->t5577.block[0] = LFRFID_T5577_MODULATION_FSK2a | LFRFID_T5577_BITRATE_RF_50 |
  182. (4 << LFRFID_T5577_MAXBLOCK_SHIFT);
  183. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  184. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  185. request->t5577.block[3] = bit_lib_get_bits_32(protocol->encoded_data, 64, 32);
  186. request->t5577.block[4] = bit_lib_get_bits_32(protocol->encoded_data, 96, 32);
  187. request->t5577.blocks_to_write = 5;
  188. result = true;
  189. }
  190. return result;
  191. };
  192. void protocol_pyramid_render_data(ProtocolPyramid* protocol, FuriString* result) {
  193. uint8_t* decoded_data = protocol->data;
  194. uint8_t format_length = decoded_data[0];
  195. furi_string_cat_printf(result, "Format: %d\r\n", format_length);
  196. if(format_length == 26) {
  197. uint8_t facility;
  198. bit_lib_copy_bits(&facility, 0, 8, decoded_data, 8);
  199. uint16_t card_id;
  200. bit_lib_copy_bits((uint8_t*)&card_id, 8, 8, decoded_data, 16);
  201. bit_lib_copy_bits((uint8_t*)&card_id, 0, 8, decoded_data, 24);
  202. furi_string_cat_printf(result, "FC: %03u, Card: %05u", facility, card_id);
  203. } else {
  204. furi_string_cat_printf(result, "Data: unknown");
  205. }
  206. };
  207. const ProtocolBase protocol_pyramid = {
  208. .name = "Pyramid",
  209. .manufacturer = "Farpointe",
  210. .data_size = PYRAMID_DECODED_DATA_SIZE,
  211. .features = LFRFIDFeatureASK,
  212. .validate_count = 3,
  213. .alloc = (ProtocolAlloc)protocol_pyramid_alloc,
  214. .free = (ProtocolFree)protocol_pyramid_free,
  215. .get_data = (ProtocolGetData)protocol_pyramid_get_data,
  216. .decoder =
  217. {
  218. .start = (ProtocolDecoderStart)protocol_pyramid_decoder_start,
  219. .feed = (ProtocolDecoderFeed)protocol_pyramid_decoder_feed,
  220. },
  221. .encoder =
  222. {
  223. .start = (ProtocolEncoderStart)protocol_pyramid_encoder_start,
  224. .yield = (ProtocolEncoderYield)protocol_pyramid_encoder_yield,
  225. },
  226. .render_data = (ProtocolRenderData)protocol_pyramid_render_data,
  227. .render_brief_data = (ProtocolRenderData)protocol_pyramid_render_data,
  228. .write_data = (ProtocolWriteData)protocol_pyramid_write_data,
  229. };