protocol_awid.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 AWID_DECODED_DATA_SIZE (9)
  11. #define AWID_ENCODED_BIT_SIZE (96)
  12. #define AWID_ENCODED_DATA_SIZE (((AWID_ENCODED_BIT_SIZE) / 8) + 1)
  13. #define AWID_ENCODED_DATA_LAST (AWID_ENCODED_DATA_SIZE - 1)
  14. typedef struct {
  15. FSKDemod* fsk_demod;
  16. } ProtocolAwidDecoder;
  17. typedef struct {
  18. FSKOsc* fsk_osc;
  19. uint8_t encoded_index;
  20. } ProtocolAwidEncoder;
  21. typedef struct {
  22. ProtocolAwidDecoder decoder;
  23. ProtocolAwidEncoder encoder;
  24. uint8_t encoded_data[AWID_ENCODED_DATA_SIZE];
  25. uint8_t data[AWID_DECODED_DATA_SIZE];
  26. } ProtocolAwid;
  27. ProtocolAwid* protocol_awid_alloc(void) {
  28. ProtocolAwid* protocol = malloc(sizeof(ProtocolAwid));
  29. protocol->decoder.fsk_demod = fsk_demod_alloc(MIN_TIME, 6, MAX_TIME, 5);
  30. protocol->encoder.fsk_osc = fsk_osc_alloc(8, 10, 50);
  31. return protocol;
  32. };
  33. void protocol_awid_free(ProtocolAwid* protocol) {
  34. fsk_demod_free(protocol->decoder.fsk_demod);
  35. fsk_osc_free(protocol->encoder.fsk_osc);
  36. free(protocol);
  37. };
  38. uint8_t* protocol_awid_get_data(ProtocolAwid* protocol) {
  39. return protocol->data;
  40. };
  41. void protocol_awid_decoder_start(ProtocolAwid* protocol) {
  42. memset(protocol->encoded_data, 0, AWID_ENCODED_DATA_SIZE);
  43. };
  44. static bool protocol_awid_can_be_decoded(uint8_t* data) {
  45. bool result = false;
  46. // Index map
  47. // 0 10 20 30 40 50 60
  48. // | | | | | | |
  49. // 01234567 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 - to 96
  50. // -----------------------------------------------------------------------------
  51. // 00000001 000 1 110 1 101 1 011 1 101 1 010 0 000 1 000 1 010 0 001 0 110 1 100 0 000 1 000 1
  52. // preamble bbb o bbb o bbw o fff o fff o ffc o ccc o ccc o ccc o ccc o ccc o wxx o xxx o xxx o - to 96
  53. // |---26 bit---| |-----117----||-------------142-------------|
  54. // b = format bit len, o = odd parity of last 3 bits
  55. // f = facility code, c = card number
  56. // w = wiegand parity
  57. // (26 bit format shown)
  58. do {
  59. // check preamble and spacing
  60. if(data[0] != 0b00000001 || data[AWID_ENCODED_DATA_LAST] != 0b00000001) break;
  61. // check odd parity for every 4 bits starting from the second byte
  62. bool parity_error = bit_lib_test_parity(data, 8, 88, BitLibParityOdd, 4);
  63. if(parity_error) break;
  64. bit_lib_remove_bit_every_nth(data, 8, 88, 4);
  65. // Avoid detection for invalid formats
  66. uint8_t len = bit_lib_get_bits(data, 8, 8);
  67. if(len != 26 && len != 50 && len != 37 && len != 34 && len != 36) break;
  68. result = true;
  69. } while(false);
  70. return result;
  71. }
  72. static void protocol_awid_decode(uint8_t* encoded_data, uint8_t* decoded_data) {
  73. bit_lib_copy_bits(decoded_data, 0, 66, encoded_data, 8);
  74. }
  75. bool protocol_awid_decoder_feed(ProtocolAwid* protocol, bool level, uint32_t duration) {
  76. bool value;
  77. uint32_t count;
  78. bool result = false;
  79. fsk_demod_feed(protocol->decoder.fsk_demod, level, duration, &value, &count);
  80. if(count > 0) {
  81. for(size_t i = 0; i < count; i++) {
  82. bit_lib_push_bit(protocol->encoded_data, AWID_ENCODED_DATA_SIZE, value);
  83. if(protocol_awid_can_be_decoded(protocol->encoded_data)) {
  84. protocol_awid_decode(protocol->encoded_data, protocol->data);
  85. result = true;
  86. break;
  87. }
  88. }
  89. }
  90. return result;
  91. };
  92. static void protocol_awid_encode(const uint8_t* decoded_data, uint8_t* encoded_data) {
  93. memset(encoded_data, 0, AWID_ENCODED_DATA_SIZE);
  94. // preamble
  95. bit_lib_set_bits(encoded_data, 0, 0b00000001, 8);
  96. for(size_t i = 0; i < 88 / 4; i++) {
  97. uint8_t value = bit_lib_get_bits(decoded_data, i * 3, 3) << 1;
  98. value |= bit_lib_test_parity_32(value, BitLibParityOdd);
  99. bit_lib_set_bits(encoded_data, 8 + i * 4, value, 4);
  100. }
  101. };
  102. bool protocol_awid_encoder_start(ProtocolAwid* protocol) {
  103. protocol_awid_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  104. protocol->encoder.encoded_index = 0;
  105. fsk_osc_reset(protocol->encoder.fsk_osc);
  106. return true;
  107. };
  108. LevelDuration protocol_awid_encoder_yield(ProtocolAwid* protocol) {
  109. bool level;
  110. uint32_t duration;
  111. bool bit = bit_lib_get_bit(protocol->encoded_data, protocol->encoder.encoded_index);
  112. bool advance = fsk_osc_next_half(protocol->encoder.fsk_osc, bit, &level, &duration);
  113. if(advance) {
  114. bit_lib_increment_index(protocol->encoder.encoded_index, AWID_ENCODED_BIT_SIZE);
  115. }
  116. return level_duration_make(level, duration);
  117. };
  118. void protocol_awid_render_data(ProtocolAwid* protocol, FuriString* result) {
  119. // Index map
  120. // 0 10 20 30 40 50 60
  121. // | | | | | | |
  122. // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456
  123. // ------------------------------------------------------------------------
  124. // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000
  125. // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  126. // |26 bit| |-117--| |-----142------|
  127. // b = format bit len, o = odd parity of last 3 bits
  128. // f = facility code, c = card number
  129. // w = wiegand parity
  130. // (26 bit format shown)
  131. uint8_t* decoded_data = protocol->data;
  132. uint8_t format_length = decoded_data[0];
  133. furi_string_cat_printf(result, "Format: %d\r\n", format_length);
  134. if(format_length == 26) {
  135. uint8_t facility;
  136. bit_lib_copy_bits(&facility, 0, 8, decoded_data, 9);
  137. uint16_t card_id;
  138. bit_lib_copy_bits((uint8_t*)&card_id, 8, 8, decoded_data, 17);
  139. bit_lib_copy_bits((uint8_t*)&card_id, 0, 8, decoded_data, 25);
  140. furi_string_cat_printf(result, "Facility: %d\r\n", facility);
  141. furi_string_cat_printf(result, "Card: %d", card_id);
  142. } else {
  143. // print 66 bits as hex
  144. furi_string_cat_printf(result, "Data: ");
  145. for(size_t i = 0; i < AWID_DECODED_DATA_SIZE; i++) {
  146. furi_string_cat_printf(result, "%02X", decoded_data[i]);
  147. }
  148. }
  149. };
  150. void protocol_awid_render_brief_data(ProtocolAwid* protocol, FuriString* result) {
  151. uint8_t* decoded_data = protocol->data;
  152. uint8_t format_length = decoded_data[0];
  153. furi_string_cat_printf(result, "Format: %d\r\n", format_length);
  154. if(format_length == 26) {
  155. uint8_t facility;
  156. bit_lib_copy_bits(&facility, 0, 8, decoded_data, 9);
  157. uint16_t card_id;
  158. bit_lib_copy_bits((uint8_t*)&card_id, 8, 8, decoded_data, 17);
  159. bit_lib_copy_bits((uint8_t*)&card_id, 0, 8, decoded_data, 25);
  160. furi_string_cat_printf(result, "ID: %03u,%05u", facility, card_id);
  161. } else {
  162. furi_string_cat_printf(result, "Data: unknown");
  163. }
  164. };
  165. bool protocol_awid_write_data(ProtocolAwid* protocol, void* data) {
  166. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  167. bool result = false;
  168. // Fix incorrect length byte
  169. if(protocol->data[0] != 26 && protocol->data[0] != 50 && protocol->data[0] != 37 &&
  170. protocol->data[0] != 34 && protocol->data[0] != 36) {
  171. protocol->data[0] = 26;
  172. }
  173. // Correct protocol data by redecoding
  174. protocol_awid_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  175. bit_lib_remove_bit_every_nth((uint8_t*)protocol->encoded_data, 8, 88, 4);
  176. protocol_awid_decode(protocol->encoded_data, protocol->data);
  177. protocol_awid_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  178. if(request->write_type == LFRFIDWriteTypeT5577) {
  179. request->t5577.block[0] = LFRFID_T5577_MODULATION_FSK2a | LFRFID_T5577_BITRATE_RF_50 |
  180. (3 << LFRFID_T5577_MAXBLOCK_SHIFT);
  181. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  182. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  183. request->t5577.block[3] = bit_lib_get_bits_32(protocol->encoded_data, 64, 32);
  184. request->t5577.blocks_to_write = 4;
  185. result = true;
  186. }
  187. return result;
  188. };
  189. const ProtocolBase protocol_awid = {
  190. .name = "AWID",
  191. .manufacturer = "AWID",
  192. .data_size = AWID_DECODED_DATA_SIZE,
  193. .features = LFRFIDFeatureASK,
  194. .validate_count = 3,
  195. .alloc = (ProtocolAlloc)protocol_awid_alloc,
  196. .free = (ProtocolFree)protocol_awid_free,
  197. .get_data = (ProtocolGetData)protocol_awid_get_data,
  198. .decoder =
  199. {
  200. .start = (ProtocolDecoderStart)protocol_awid_decoder_start,
  201. .feed = (ProtocolDecoderFeed)protocol_awid_decoder_feed,
  202. },
  203. .encoder =
  204. {
  205. .start = (ProtocolEncoderStart)protocol_awid_encoder_start,
  206. .yield = (ProtocolEncoderYield)protocol_awid_encoder_yield,
  207. },
  208. .render_data = (ProtocolRenderData)protocol_awid_render_data,
  209. .render_brief_data = (ProtocolRenderData)protocol_awid_render_brief_data,
  210. .write_data = (ProtocolWriteData)protocol_awid_write_data,
  211. };