protocol_awid.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(const 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. result = true;
  65. } while(false);
  66. return result;
  67. }
  68. static void protocol_awid_decode(uint8_t* encoded_data, uint8_t* decoded_data) {
  69. bit_lib_remove_bit_every_nth(encoded_data, 8, 88, 4);
  70. bit_lib_copy_bits(decoded_data, 0, 66, encoded_data, 8);
  71. }
  72. bool protocol_awid_decoder_feed(ProtocolAwid* protocol, bool level, uint32_t duration) {
  73. bool value;
  74. uint32_t count;
  75. bool result = false;
  76. fsk_demod_feed(protocol->decoder.fsk_demod, level, duration, &value, &count);
  77. if(count > 0) {
  78. for(size_t i = 0; i < count; i++) {
  79. bit_lib_push_bit(protocol->encoded_data, AWID_ENCODED_DATA_SIZE, value);
  80. if(protocol_awid_can_be_decoded(protocol->encoded_data)) {
  81. protocol_awid_decode(protocol->encoded_data, protocol->data);
  82. result = true;
  83. break;
  84. }
  85. }
  86. }
  87. return result;
  88. };
  89. static void protocol_awid_encode(const uint8_t* decoded_data, uint8_t* encoded_data) {
  90. memset(encoded_data, 0, AWID_ENCODED_DATA_SIZE);
  91. // preamble
  92. bit_lib_set_bits(encoded_data, 0, 0b00000001, 8);
  93. for(size_t i = 0; i < 88 / 4; i++) {
  94. uint8_t value = bit_lib_get_bits(decoded_data, i * 3, 3) << 1;
  95. value |= bit_lib_test_parity_32(value, BitLibParityOdd);
  96. bit_lib_set_bits(encoded_data, 8 + i * 4, value, 4);
  97. }
  98. };
  99. bool protocol_awid_encoder_start(ProtocolAwid* protocol) {
  100. protocol_awid_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  101. protocol->encoder.encoded_index = 0;
  102. fsk_osc_reset(protocol->encoder.fsk_osc);
  103. return true;
  104. };
  105. LevelDuration protocol_awid_encoder_yield(ProtocolAwid* protocol) {
  106. bool level;
  107. uint32_t duration;
  108. bool bit = bit_lib_get_bit(protocol->encoded_data, protocol->encoder.encoded_index);
  109. bool advance = fsk_osc_next_half(protocol->encoder.fsk_osc, bit, &level, &duration);
  110. if(advance) {
  111. bit_lib_increment_index(protocol->encoder.encoded_index, AWID_ENCODED_BIT_SIZE);
  112. }
  113. return level_duration_make(level, duration);
  114. };
  115. void protocol_awid_render_data(ProtocolAwid* protocol, string_t result) {
  116. // Index map
  117. // 0 10 20 30 40 50 60
  118. // | | | | | | |
  119. // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456
  120. // ------------------------------------------------------------------------
  121. // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000
  122. // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  123. // |26 bit| |-117--| |-----142------|
  124. // b = format bit len, o = odd parity of last 3 bits
  125. // f = facility code, c = card number
  126. // w = wiegand parity
  127. // (26 bit format shown)
  128. uint8_t* decoded_data = protocol->data;
  129. uint8_t format_length = decoded_data[0];
  130. string_cat_printf(result, "Format: %d\r\n", format_length);
  131. if(format_length == 26) {
  132. uint8_t facility;
  133. bit_lib_copy_bits(&facility, 0, 8, decoded_data, 9);
  134. uint16_t card_id;
  135. bit_lib_copy_bits((uint8_t*)&card_id, 8, 8, decoded_data, 17);
  136. bit_lib_copy_bits((uint8_t*)&card_id, 0, 8, decoded_data, 25);
  137. string_cat_printf(result, "Facility: %d\r\n", facility);
  138. string_cat_printf(result, "Card: %d", card_id);
  139. } else {
  140. // print 66 bits as hex
  141. string_cat_printf(result, "Data: ");
  142. for(size_t i = 0; i < AWID_DECODED_DATA_SIZE; i++) {
  143. string_cat_printf(result, "%02X", decoded_data[i]);
  144. }
  145. }
  146. };
  147. void protocol_awid_render_brief_data(ProtocolAwid* protocol, string_t result) {
  148. uint8_t* decoded_data = protocol->data;
  149. uint8_t format_length = decoded_data[0];
  150. string_cat_printf(result, "Format: %d\r\n", format_length);
  151. if(format_length == 26) {
  152. uint8_t facility;
  153. bit_lib_copy_bits(&facility, 0, 8, decoded_data, 9);
  154. uint16_t card_id;
  155. bit_lib_copy_bits((uint8_t*)&card_id, 8, 8, decoded_data, 17);
  156. bit_lib_copy_bits((uint8_t*)&card_id, 0, 8, decoded_data, 25);
  157. string_cat_printf(result, "ID: %03u,%05u", facility, card_id);
  158. } else {
  159. string_cat_printf(result, "Data: unknown");
  160. }
  161. };
  162. bool protocol_awid_write_data(ProtocolAwid* protocol, void* data) {
  163. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  164. bool result = false;
  165. protocol_awid_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  166. if(request->write_type == LFRFIDWriteTypeT5577) {
  167. request->t5577.block[0] = LFRFID_T5577_MODULATION_FSK2a | LFRFID_T5577_BITRATE_RF_50 |
  168. (3 << LFRFID_T5577_MAXBLOCK_SHIFT);
  169. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  170. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  171. request->t5577.block[3] = bit_lib_get_bits_32(protocol->encoded_data, 64, 32);
  172. request->t5577.blocks_to_write = 4;
  173. result = true;
  174. }
  175. return result;
  176. };
  177. const ProtocolBase protocol_awid = {
  178. .name = "AWID",
  179. .manufacturer = "AWIG",
  180. .data_size = AWID_DECODED_DATA_SIZE,
  181. .features = LFRFIDFeatureASK,
  182. .validate_count = 3,
  183. .alloc = (ProtocolAlloc)protocol_awid_alloc,
  184. .free = (ProtocolFree)protocol_awid_free,
  185. .get_data = (ProtocolGetData)protocol_awid_get_data,
  186. .decoder =
  187. {
  188. .start = (ProtocolDecoderStart)protocol_awid_decoder_start,
  189. .feed = (ProtocolDecoderFeed)protocol_awid_decoder_feed,
  190. },
  191. .encoder =
  192. {
  193. .start = (ProtocolEncoderStart)protocol_awid_encoder_start,
  194. .yield = (ProtocolEncoderYield)protocol_awid_encoder_yield,
  195. },
  196. .render_data = (ProtocolRenderData)protocol_awid_render_data,
  197. .render_brief_data = (ProtocolRenderData)protocol_awid_render_brief_data,
  198. .write_data = (ProtocolWriteData)protocol_awid_write_data,
  199. };