protocol_gallagher.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include <furi.h>
  2. #include <toolbox/protocols/protocol.h>
  3. #include <toolbox/manchester_decoder.h>
  4. #include <lfrfid/tools/bit_lib.h>
  5. #include "lfrfid_protocols.h"
  6. #define GALLAGHER_CLOCK_PER_BIT (32)
  7. #define GALLAGHER_ENCODED_BIT_SIZE (96)
  8. #define GALLAGHER_ENCODED_BYTE_SIZE ((GALLAGHER_ENCODED_BIT_SIZE) / 8)
  9. #define GALLAGHER_PREAMBLE_BIT_SIZE (16)
  10. #define GALLAGHER_PREAMBLE_BYTE_SIZE ((GALLAGHER_PREAMBLE_BIT_SIZE) / 8)
  11. #define GALLAGHER_ENCODED_BYTE_FULL_SIZE \
  12. (GALLAGHER_ENCODED_BYTE_SIZE + GALLAGHER_PREAMBLE_BYTE_SIZE)
  13. #define GALLAGHER_DECODED_DATA_SIZE 8
  14. #define GALLAGHER_READ_SHORT_TIME (128)
  15. #define GALLAGHER_READ_LONG_TIME (256)
  16. #define GALLAGHER_READ_JITTER_TIME (60)
  17. #define GALLAGHER_READ_SHORT_TIME_LOW (GALLAGHER_READ_SHORT_TIME - GALLAGHER_READ_JITTER_TIME)
  18. #define GALLAGHER_READ_SHORT_TIME_HIGH (GALLAGHER_READ_SHORT_TIME + GALLAGHER_READ_JITTER_TIME)
  19. #define GALLAGHER_READ_LONG_TIME_LOW (GALLAGHER_READ_LONG_TIME - GALLAGHER_READ_JITTER_TIME)
  20. #define GALLAGHER_READ_LONG_TIME_HIGH (GALLAGHER_READ_LONG_TIME + GALLAGHER_READ_JITTER_TIME)
  21. typedef struct {
  22. uint8_t data[GALLAGHER_DECODED_DATA_SIZE];
  23. uint8_t encoded_data[GALLAGHER_ENCODED_BYTE_FULL_SIZE];
  24. uint8_t encoded_data_index;
  25. bool encoded_polarity;
  26. ManchesterState decoder_manchester_state;
  27. } ProtocolGallagher;
  28. ProtocolGallagher* protocol_gallagher_alloc(void) {
  29. ProtocolGallagher* proto = malloc(sizeof(ProtocolGallagher));
  30. return (void*)proto;
  31. };
  32. void protocol_gallagher_free(ProtocolGallagher* protocol) {
  33. free(protocol);
  34. };
  35. uint8_t* protocol_gallagher_get_data(ProtocolGallagher* protocol) {
  36. return protocol->data;
  37. };
  38. static void protocol_gallagher_scramble(uint8_t* data, size_t length) {
  39. const uint8_t lut[] = {
  40. 0xa3, 0xb0, 0x80, 0xc6, 0xb2, 0xf4, 0x5c, 0x6c, 0x81, 0xf1, 0xbb, 0xeb, 0x55, 0x67, 0x3c,
  41. 0x05, 0x1a, 0x0e, 0x61, 0xf6, 0x22, 0xce, 0xaa, 0x8f, 0xbd, 0x3b, 0x1f, 0x5e, 0x44, 0x04,
  42. 0x51, 0x2e, 0x4d, 0x9a, 0x84, 0xea, 0xf8, 0x66, 0x74, 0x29, 0x7f, 0x70, 0xd8, 0x31, 0x7a,
  43. 0x6d, 0xa4, 0x00, 0x82, 0xb9, 0x5f, 0xb4, 0x16, 0xab, 0xff, 0xc2, 0x39, 0xdc, 0x19, 0x65,
  44. 0x57, 0x7c, 0x20, 0xfa, 0x5a, 0x49, 0x13, 0xd0, 0xfb, 0xa8, 0x91, 0x73, 0xb1, 0x33, 0x18,
  45. 0xbe, 0x21, 0x72, 0x48, 0xb6, 0xdb, 0xa0, 0x5d, 0xcc, 0xe6, 0x17, 0x27, 0xe5, 0xd4, 0x53,
  46. 0x42, 0xf3, 0xdd, 0x7b, 0x24, 0xac, 0x2b, 0x58, 0x1e, 0xa7, 0xe7, 0x86, 0x40, 0xd3, 0x98,
  47. 0x97, 0x71, 0xcb, 0x3a, 0x0f, 0x01, 0x9b, 0x6e, 0x1b, 0xfc, 0x34, 0xa6, 0xda, 0x07, 0x0c,
  48. 0xae, 0x37, 0xca, 0x54, 0xfd, 0x26, 0xfe, 0x0a, 0x45, 0xa2, 0x2a, 0xc4, 0x12, 0x0d, 0xf5,
  49. 0x4f, 0x69, 0xe0, 0x8a, 0x77, 0x60, 0x3f, 0x99, 0x95, 0xd2, 0x38, 0x36, 0x62, 0xb7, 0x32,
  50. 0x7e, 0x79, 0xc0, 0x46, 0x93, 0x2f, 0xa5, 0xba, 0x5b, 0xaf, 0x52, 0x1d, 0xc3, 0x75, 0xcf,
  51. 0xd6, 0x4c, 0x83, 0xe8, 0x3d, 0x30, 0x4e, 0xbc, 0x08, 0x2d, 0x09, 0x06, 0xd9, 0x25, 0x9e,
  52. 0x89, 0xf2, 0x96, 0x88, 0xc1, 0x8c, 0x94, 0x0b, 0x28, 0xf0, 0x47, 0x63, 0xd5, 0xb3, 0x68,
  53. 0x56, 0x9c, 0xf9, 0x6f, 0x41, 0x50, 0x85, 0x8b, 0x9d, 0x59, 0xbf, 0x9f, 0xe2, 0x8e, 0x6a,
  54. 0x11, 0x23, 0xa1, 0xcd, 0xb5, 0x7d, 0xc7, 0xa9, 0xc8, 0xef, 0xdf, 0x02, 0xb8, 0x03, 0x6b,
  55. 0x35, 0x3e, 0x2c, 0x76, 0xc9, 0xde, 0x1c, 0x4b, 0xd1, 0xed, 0x14, 0xc5, 0xad, 0xe9, 0x64,
  56. 0x4a, 0xec, 0x8d, 0xf7, 0x10, 0x43, 0x78, 0x15, 0x87, 0xe4, 0xd7, 0x92, 0xe1, 0xee, 0xe3,
  57. 0x90};
  58. for(size_t i = 0; i < length; i++) {
  59. data[i] = lut[data[i]];
  60. }
  61. }
  62. static void protocol_gallagher_descramble(uint8_t* data, size_t length) {
  63. const uint8_t lut[] = {
  64. 0x2f, 0x6e, 0xdd,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c1,
  76. 0x33, 0xd6, 0x4f, 0x94, 0xde, 0x31, 0x9d, 0x0a, 0xac, 0x18, 0x4b, 0xcd, 0x98, 0xb8, 0x37,
  77. 0xa2, 0x83, 0xec, 0x03, 0xd8, 0xda, 0xe5, 0x7a, 0x6b, 0x53, 0xd5, 0x15, 0xa4, 0x43, 0xe9,
  78. 0x90, 0x67, 0x58, 0xc0, 0xa5, 0xfa, 0x2a, 0xb1, 0x75, 0x50, 0x39, 0x5c, 0xe6, 0xdc, 0x89,
  79. 0xfc, 0xcf, 0xfe, 0xf9, 0x57, 0x54, 0x64, 0xa8, 0xee, 0x23, 0x0b, 0xf1, 0xea, 0xfd, 0xdb,
  80. 0xbd, 0x09, 0xb5, 0x5b, 0x05, 0x86, 0x13, 0xf3, 0x24, 0xc5, 0x3f, 0x44, 0x72, 0x7c, 0x7e,
  81. 0x36};
  82. for(size_t i = 0; i < length; i++) {
  83. data[i] = lut[data[i]];
  84. }
  85. }
  86. static void protocol_gallagher_decode(ProtocolGallagher* protocol) {
  87. bit_lib_remove_bit_every_nth(protocol->encoded_data, 16, 9 * 8, 9);
  88. protocol_gallagher_descramble(protocol->encoded_data + 2, 8);
  89. // Region code
  90. bit_lib_set_bits(protocol->data, 0, (protocol->encoded_data[5] & 0x1E) >> 1, 4);
  91. // Issue Level
  92. bit_lib_set_bits(protocol->data, 4, (protocol->encoded_data[9] & 0x0F), 4);
  93. // Facility Code
  94. uint32_t fc = (protocol->encoded_data[7] & 0x0F) << 12 | protocol->encoded_data[3] << 4 |
  95. ((protocol->encoded_data[9] >> 4) & 0x0F);
  96. protocol->data[3] = (uint8_t)fc;
  97. protocol->data[2] = (uint8_t)(fc >>= 8);
  98. protocol->data[1] = (uint8_t)(fc >>= 8);
  99. // Card Number
  100. uint32_t card = protocol->encoded_data[2] << 16 | (protocol->encoded_data[6] & 0x1F) << 11 |
  101. protocol->encoded_data[4] << 3 | (protocol->encoded_data[5] & 0xE0) >> 5;
  102. protocol->data[7] = (uint8_t)card;
  103. protocol->data[6] = (uint8_t)(card >>= 8);
  104. protocol->data[5] = (uint8_t)(card >>= 8);
  105. protocol->data[4] = (uint8_t)(card >>= 8);
  106. }
  107. static bool protocol_gallagher_can_be_decoded(ProtocolGallagher* protocol) {
  108. // check 16 bits preamble
  109. if(bit_lib_get_bits_16(protocol->encoded_data, 0, 16) != 0b0111111111101010) return false;
  110. // check next 16 bits preamble
  111. if(bit_lib_get_bits_16(protocol->encoded_data, 96, 16) != 0b0111111111101010) return false;
  112. uint8_t checksum_arr[8] = {0};
  113. for(int i = 0, pos = 0; i < 8; i++) {
  114. // Following the preamble, every 9th bit is a checksum-bit for the preceding byte
  115. pos = 16 + (9 * i);
  116. checksum_arr[i] = bit_lib_get_bits(protocol->encoded_data, pos, 8);
  117. }
  118. uint8_t crc = bit_lib_get_bits(protocol->encoded_data, 16 + (9 * 8), 8);
  119. uint8_t calc_crc = bit_lib_crc8(checksum_arr, 8, 0x7, 0x2c, false, false, 0x00);
  120. // crc
  121. if(crc != calc_crc) return false;
  122. return true;
  123. }
  124. void protocol_gallagher_decoder_start(ProtocolGallagher* protocol) {
  125. memset(protocol->encoded_data, 0, GALLAGHER_ENCODED_BYTE_FULL_SIZE);
  126. manchester_advance(
  127. protocol->decoder_manchester_state,
  128. ManchesterEventReset,
  129. &protocol->decoder_manchester_state,
  130. NULL);
  131. };
  132. bool protocol_gallagher_decoder_feed(ProtocolGallagher* protocol, bool level, uint32_t duration) {
  133. bool result = false;
  134. ManchesterEvent event = ManchesterEventReset;
  135. if(duration > GALLAGHER_READ_SHORT_TIME_LOW && duration < GALLAGHER_READ_SHORT_TIME_HIGH) {
  136. if(!level) {
  137. event = ManchesterEventShortHigh;
  138. } else {
  139. event = ManchesterEventShortLow;
  140. }
  141. } else if(duration > GALLAGHER_READ_LONG_TIME_LOW && duration < GALLAGHER_READ_LONG_TIME_HIGH) {
  142. if(!level) {
  143. event = ManchesterEventLongHigh;
  144. } else {
  145. event = ManchesterEventLongLow;
  146. }
  147. }
  148. if(event != ManchesterEventReset) {
  149. bool data;
  150. bool data_ok = manchester_advance(
  151. protocol->decoder_manchester_state, event, &protocol->decoder_manchester_state, &data);
  152. if(data_ok) {
  153. bit_lib_push_bit(protocol->encoded_data, GALLAGHER_ENCODED_BYTE_FULL_SIZE, data);
  154. if(protocol_gallagher_can_be_decoded(protocol)) {
  155. protocol_gallagher_decode(protocol);
  156. result = true;
  157. }
  158. }
  159. }
  160. return result;
  161. };
  162. bool protocol_gallagher_encoder_start(ProtocolGallagher* protocol) {
  163. // Preamble
  164. bit_lib_set_bits(protocol->encoded_data, 0, 0b01111111, 8);
  165. bit_lib_set_bits(protocol->encoded_data, 8, 0b11101010, 8);
  166. uint8_t rc = bit_lib_get_bits(protocol->data, 0, 4);
  167. uint8_t il = bit_lib_get_bits(protocol->data, 4, 4);
  168. uint32_t fc = bit_lib_get_bits_32(protocol->data, 8, 24);
  169. uint32_t cn = bit_lib_get_bits_32(protocol->data, 32, 32);
  170. uint8_t payload[8] = {0};
  171. payload[0] = (cn & 0xffffff) >> 16;
  172. payload[1] = (fc & 0xfff) >> 4;
  173. payload[2] = (cn & 0x7ff) >> 3;
  174. payload[3] = (cn & 0x7) << 5 | (rc & 0xf) << 1;
  175. payload[4] = (cn & 0xffff) >> 11;
  176. payload[5] = (fc & 0xffff) >> 12;
  177. payload[6] = 0;
  178. payload[7] = (fc & 0xf) << 4 | (il & 0xf);
  179. // Gallagher scramble
  180. protocol_gallagher_scramble(payload, 8);
  181. for(int i = 0; i < 8; i++) {
  182. // data byte
  183. bit_lib_set_bits(protocol->encoded_data, 16 + (i * 9), payload[i], 8);
  184. // every byte is followed by a bit which is the inverse of the last bit
  185. bit_lib_set_bit(protocol->encoded_data, 16 + (i * 9) + 8, !(payload[i] & 0x1));
  186. }
  187. // checksum
  188. uint8_t crc = bit_lib_crc8(payload, 8, 0x7, 0x2c, false, false, 0x00);
  189. bit_lib_set_bits(protocol->encoded_data, 16 + (9 * 8), crc, 8);
  190. return true;
  191. };
  192. LevelDuration protocol_gallagher_encoder_yield(ProtocolGallagher* protocol) {
  193. bool level = bit_lib_get_bit(protocol->encoded_data, protocol->encoded_data_index);
  194. uint32_t duration = GALLAGHER_CLOCK_PER_BIT / 2;
  195. if(protocol->encoded_polarity) {
  196. protocol->encoded_polarity = false;
  197. } else {
  198. level = !level;
  199. protocol->encoded_polarity = true;
  200. bit_lib_increment_index(protocol->encoded_data_index, GALLAGHER_ENCODED_BIT_SIZE);
  201. }
  202. return level_duration_make(level, duration);
  203. };
  204. bool protocol_gallagher_write_data(ProtocolGallagher* protocol, void* data) {
  205. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  206. bool result = false;
  207. // Correct protocol data by redecoding
  208. protocol_gallagher_encoder_start(protocol);
  209. protocol_gallagher_decode(protocol);
  210. protocol_gallagher_encoder_start(protocol);
  211. if(request->write_type == LFRFIDWriteTypeT5577) {
  212. request->t5577.block[0] =
  213. (LFRFID_T5577_MODULATION_MANCHESTER | LFRFID_T5577_BITRATE_RF_32 |
  214. (3 << LFRFID_T5577_MAXBLOCK_SHIFT));
  215. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  216. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  217. request->t5577.block[3] = bit_lib_get_bits_32(protocol->encoded_data, 64, 32);
  218. request->t5577.blocks_to_write = 4;
  219. result = true;
  220. }
  221. return result;
  222. };
  223. void protocol_gallagher_render_data(ProtocolGallagher* protocol, FuriString* result) {
  224. UNUSED(protocol);
  225. uint8_t rc = bit_lib_get_bits(protocol->data, 0, 4);
  226. uint8_t il = bit_lib_get_bits(protocol->data, 4, 4);
  227. uint32_t fc = bit_lib_get_bits_32(protocol->data, 8, 24);
  228. uint32_t card_id = bit_lib_get_bits_32(protocol->data, 32, 32);
  229. furi_string_cat_printf(result, "Region: %u, Issue Level: %u\r\n", rc, il);
  230. furi_string_cat_printf(result, "FC: %lu, C: %lu\r\n", fc, card_id);
  231. };
  232. const ProtocolBase protocol_gallagher = {
  233. .name = "Gallagher",
  234. .manufacturer = "Gallagher",
  235. .data_size = GALLAGHER_DECODED_DATA_SIZE,
  236. .features = LFRFIDFeatureASK,
  237. .validate_count = 3,
  238. .alloc = (ProtocolAlloc)protocol_gallagher_alloc,
  239. .free = (ProtocolFree)protocol_gallagher_free,
  240. .get_data = (ProtocolGetData)protocol_gallagher_get_data,
  241. .decoder =
  242. {
  243. .start = (ProtocolDecoderStart)protocol_gallagher_decoder_start,
  244. .feed = (ProtocolDecoderFeed)protocol_gallagher_decoder_feed,
  245. },
  246. .encoder =
  247. {
  248. .start = (ProtocolEncoderStart)protocol_gallagher_encoder_start,
  249. .yield = (ProtocolEncoderYield)protocol_gallagher_encoder_yield,
  250. },
  251. .render_data = (ProtocolRenderData)protocol_gallagher_render_data,
  252. .render_brief_data = (ProtocolRenderData)protocol_gallagher_render_data,
  253. .write_data = (ProtocolWriteData)protocol_gallagher_write_data,
  254. };