protocol_h10301.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #define JITTER_TIME (20)
  7. #define MIN_TIME (64 - JITTER_TIME)
  8. #define MAX_TIME (80 + JITTER_TIME)
  9. #define H10301_DECODED_DATA_SIZE (3)
  10. #define H10301_ENCODED_DATA_SIZE_U32 (3)
  11. #define H10301_ENCODED_DATA_SIZE (sizeof(uint32_t) * H10301_ENCODED_DATA_SIZE_U32)
  12. #define H10301_BIT_SIZE (sizeof(uint32_t) * 8)
  13. #define H10301_BIT_MAX_SIZE (H10301_BIT_SIZE * H10301_DECODED_DATA_SIZE)
  14. typedef struct {
  15. FSKDemod* fsk_demod;
  16. } ProtocolH10301Decoder;
  17. typedef struct {
  18. FSKOsc* fsk_osc;
  19. uint8_t encoded_index;
  20. uint32_t pulse;
  21. } ProtocolH10301Encoder;
  22. typedef struct {
  23. ProtocolH10301Decoder decoder;
  24. ProtocolH10301Encoder encoder;
  25. uint32_t encoded_data[H10301_ENCODED_DATA_SIZE_U32];
  26. uint8_t data[H10301_DECODED_DATA_SIZE];
  27. } ProtocolH10301;
  28. ProtocolH10301* protocol_h10301_alloc(void) {
  29. ProtocolH10301* protocol = malloc(sizeof(ProtocolH10301));
  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_h10301_free(ProtocolH10301* 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_h10301_get_data(ProtocolH10301* protocol) {
  40. return protocol->data;
  41. };
  42. void protocol_h10301_decoder_start(ProtocolH10301* protocol) {
  43. memset(protocol->encoded_data, 0, sizeof(uint32_t) * 3);
  44. };
  45. static void protocol_h10301_decoder_store_data(ProtocolH10301* protocol, bool data) {
  46. protocol->encoded_data[0] = (protocol->encoded_data[0] << 1) |
  47. ((protocol->encoded_data[1] >> 31) & 1);
  48. protocol->encoded_data[1] = (protocol->encoded_data[1] << 1) |
  49. ((protocol->encoded_data[2] >> 31) & 1);
  50. protocol->encoded_data[2] = (protocol->encoded_data[2] << 1) | data;
  51. }
  52. static bool protocol_h10301_can_be_decoded(const uint32_t* card_data) {
  53. const uint8_t* encoded_data = (const uint8_t*)card_data;
  54. // packet preamble
  55. // raw data
  56. if(*(encoded_data + 3) != 0x1D) {
  57. return false;
  58. }
  59. // encoded company/oem
  60. // coded with 01 = 0, 10 = 1 transitions
  61. // stored in word 0
  62. if((*card_data >> 10 & 0x3FFF) != 0x1556) {
  63. return false;
  64. }
  65. // encoded format/length
  66. // coded with 01 = 0, 10 = 1 transitions
  67. // stored in word 0 and word 1
  68. if((((*card_data & 0x3FF) << 12) | ((*(card_data + 1) >> 20) & 0xFFF)) != 0x155556) {
  69. return false;
  70. }
  71. // data decoding
  72. uint32_t result = 0;
  73. // decode from word 1
  74. // coded with 01 = 0, 10 = 1 transitions
  75. for(int8_t i = 9; i >= 0; i--) {
  76. switch((*(card_data + 1) >> (2 * i)) & 0b11) {
  77. case 0b01:
  78. result = (result << 1) | 0;
  79. break;
  80. case 0b10:
  81. result = (result << 1) | 1;
  82. break;
  83. default:
  84. return false;
  85. break;
  86. }
  87. }
  88. // decode from word 2
  89. // coded with 01 = 0, 10 = 1 transitions
  90. for(int8_t i = 15; i >= 0; i--) {
  91. switch((*(card_data + 2) >> (2 * i)) & 0b11) {
  92. case 0b01:
  93. result = (result << 1) | 0;
  94. break;
  95. case 0b10:
  96. result = (result << 1) | 1;
  97. break;
  98. default:
  99. return false;
  100. break;
  101. }
  102. }
  103. // trailing parity (odd) test
  104. uint8_t parity_sum = 0;
  105. for(int8_t i = 0; i < 13; i++) {
  106. if(((result >> i) & 1) == 1) {
  107. parity_sum++;
  108. }
  109. }
  110. if((parity_sum % 2) != 1) {
  111. return false;
  112. }
  113. // leading parity (even) test
  114. parity_sum = 0;
  115. for(int8_t i = 13; i < 26; i++) {
  116. if(((result >> i) & 1) == 1) {
  117. parity_sum++;
  118. }
  119. }
  120. if((parity_sum % 2) == 1) {
  121. return false;
  122. }
  123. return true;
  124. }
  125. static void protocol_h10301_decode(const uint32_t* card_data, uint8_t* decoded_data) {
  126. // data decoding
  127. uint32_t result = 0;
  128. // decode from word 1
  129. // coded with 01 = 0, 10 = 1 transitions
  130. for(int8_t i = 9; i >= 0; i--) {
  131. switch((*(card_data + 1) >> (2 * i)) & 0b11) {
  132. case 0b01:
  133. result = (result << 1) | 0;
  134. break;
  135. case 0b10:
  136. result = (result << 1) | 1;
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. // decode from word 2
  143. // coded with 01 = 0, 10 = 1 transitions
  144. for(int8_t i = 15; i >= 0; i--) {
  145. switch((*(card_data + 2) >> (2 * i)) & 0b11) {
  146. case 0b01:
  147. result = (result << 1) | 0;
  148. break;
  149. case 0b10:
  150. result = (result << 1) | 1;
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. uint8_t data[H10301_DECODED_DATA_SIZE] = {
  157. (uint8_t)(result >> 17), (uint8_t)(result >> 9), (uint8_t)(result >> 1)};
  158. memcpy(decoded_data, &data, H10301_DECODED_DATA_SIZE);
  159. }
  160. bool protocol_h10301_decoder_feed(ProtocolH10301* protocol, bool level, uint32_t duration) {
  161. bool value;
  162. uint32_t count;
  163. bool result = false;
  164. fsk_demod_feed(protocol->decoder.fsk_demod, level, duration, &value, &count);
  165. if(count > 0) {
  166. for(size_t i = 0; i < count; i++) {
  167. protocol_h10301_decoder_store_data(protocol, value);
  168. if(protocol_h10301_can_be_decoded(protocol->encoded_data)) {
  169. protocol_h10301_decode(protocol->encoded_data, protocol->data);
  170. result = true;
  171. break;
  172. }
  173. }
  174. }
  175. return result;
  176. };
  177. static void protocol_h10301_write_raw_bit(bool bit, uint8_t position, uint32_t* card_data) {
  178. if(bit) {
  179. card_data[position / H10301_BIT_SIZE] |=
  180. 1UL << (H10301_BIT_SIZE - (position % H10301_BIT_SIZE) - 1);
  181. } else {
  182. card_data[position / H10301_BIT_SIZE] &=
  183. ~(1UL << (H10301_BIT_SIZE - (position % H10301_BIT_SIZE) - 1));
  184. }
  185. }
  186. static void protocol_h10301_write_bit(bool bit, uint8_t position, uint32_t* card_data) {
  187. protocol_h10301_write_raw_bit(bit, position + 0, card_data);
  188. protocol_h10301_write_raw_bit(!bit, position + 1, card_data);
  189. }
  190. void protocol_h10301_encode(const uint8_t* decoded_data, uint8_t* encoded_data) {
  191. uint32_t card_data[H10301_DECODED_DATA_SIZE] = {0, 0, 0};
  192. uint32_t fc_cn = (decoded_data[0] << 16) | (decoded_data[1] << 8) | decoded_data[2];
  193. // even parity sum calculation (high 12 bits of data)
  194. uint8_t even_parity_sum = 0;
  195. for(int8_t i = 12; i < 24; i++) {
  196. if(((fc_cn >> i) & 1) == 1) {
  197. even_parity_sum++;
  198. }
  199. }
  200. // odd parity sum calculation (low 12 bits of data)
  201. uint8_t odd_parity_sum = 1;
  202. for(int8_t i = 0; i < 12; i++) {
  203. if(((fc_cn >> i) & 1) == 1) {
  204. odd_parity_sum++;
  205. }
  206. }
  207. // 0x1D preamble
  208. protocol_h10301_write_raw_bit(0, 0, card_data);
  209. protocol_h10301_write_raw_bit(0, 1, card_data);
  210. protocol_h10301_write_raw_bit(0, 2, card_data);
  211. protocol_h10301_write_raw_bit(1, 3, card_data);
  212. protocol_h10301_write_raw_bit(1, 4, card_data);
  213. protocol_h10301_write_raw_bit(1, 5, card_data);
  214. protocol_h10301_write_raw_bit(0, 6, card_data);
  215. protocol_h10301_write_raw_bit(1, 7, card_data);
  216. // company / OEM code 1
  217. protocol_h10301_write_bit(0, 8, card_data);
  218. protocol_h10301_write_bit(0, 10, card_data);
  219. protocol_h10301_write_bit(0, 12, card_data);
  220. protocol_h10301_write_bit(0, 14, card_data);
  221. protocol_h10301_write_bit(0, 16, card_data);
  222. protocol_h10301_write_bit(0, 18, card_data);
  223. protocol_h10301_write_bit(1, 20, card_data);
  224. // card format / length 1
  225. protocol_h10301_write_bit(0, 22, card_data);
  226. protocol_h10301_write_bit(0, 24, card_data);
  227. protocol_h10301_write_bit(0, 26, card_data);
  228. protocol_h10301_write_bit(0, 28, card_data);
  229. protocol_h10301_write_bit(0, 30, card_data);
  230. protocol_h10301_write_bit(0, 32, card_data);
  231. protocol_h10301_write_bit(0, 34, card_data);
  232. protocol_h10301_write_bit(0, 36, card_data);
  233. protocol_h10301_write_bit(0, 38, card_data);
  234. protocol_h10301_write_bit(0, 40, card_data);
  235. protocol_h10301_write_bit(1, 42, card_data);
  236. // even parity bit
  237. protocol_h10301_write_bit((even_parity_sum % 2), 44, card_data);
  238. // data
  239. for(uint8_t i = 0; i < 24; i++) {
  240. protocol_h10301_write_bit((fc_cn >> (23 - i)) & 1, 46 + (i * 2), card_data);
  241. }
  242. // odd parity bit
  243. protocol_h10301_write_bit((odd_parity_sum % 2), 94, card_data);
  244. memcpy(encoded_data, &card_data, H10301_ENCODED_DATA_SIZE);
  245. }
  246. bool protocol_h10301_encoder_start(ProtocolH10301* protocol) {
  247. protocol_h10301_encode(protocol->data, (uint8_t*)protocol->encoded_data);
  248. protocol->encoder.encoded_index = 0;
  249. protocol->encoder.pulse = 0;
  250. return true;
  251. };
  252. LevelDuration protocol_h10301_encoder_yield(ProtocolH10301* protocol) {
  253. bool level = 0;
  254. uint32_t duration = 0;
  255. // if pulse is zero, we need to output high, otherwise we need to output low
  256. if(protocol->encoder.pulse == 0) {
  257. // get bit
  258. uint8_t bit =
  259. (protocol->encoded_data[protocol->encoder.encoded_index / H10301_BIT_SIZE] >>
  260. ((H10301_BIT_SIZE - 1) - (protocol->encoder.encoded_index % H10301_BIT_SIZE))) &
  261. 1;
  262. // get pulse from oscillator
  263. bool advance = fsk_osc_next(protocol->encoder.fsk_osc, bit, &duration);
  264. if(advance) {
  265. protocol->encoder.encoded_index++;
  266. if(protocol->encoder.encoded_index >= (H10301_BIT_MAX_SIZE)) {
  267. protocol->encoder.encoded_index = 0;
  268. }
  269. }
  270. // duration diveded by 2 because we need to output high and low
  271. duration = duration / 2;
  272. protocol->encoder.pulse = duration;
  273. level = true;
  274. } else {
  275. // output low half and reset pulse
  276. duration = protocol->encoder.pulse;
  277. protocol->encoder.pulse = 0;
  278. level = false;
  279. }
  280. return level_duration_make(level, duration);
  281. };
  282. bool protocol_h10301_write_data(ProtocolH10301* protocol, void* data) {
  283. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  284. bool result = false;
  285. // Correct protocol data by redecoding
  286. protocol_h10301_encoder_start(protocol);
  287. protocol_h10301_decode(protocol->encoded_data, protocol->data);
  288. protocol_h10301_encoder_start(protocol);
  289. if(request->write_type == LFRFIDWriteTypeT5577) {
  290. request->t5577.block[0] = LFRFID_T5577_MODULATION_FSK2a | LFRFID_T5577_BITRATE_RF_50 |
  291. (3 << LFRFID_T5577_MAXBLOCK_SHIFT);
  292. request->t5577.block[1] = protocol->encoded_data[0];
  293. request->t5577.block[2] = protocol->encoded_data[1];
  294. request->t5577.block[3] = protocol->encoded_data[2];
  295. request->t5577.blocks_to_write = 4;
  296. result = true;
  297. }
  298. return result;
  299. };
  300. void protocol_h10301_render_data(ProtocolH10301* protocol, FuriString* result) {
  301. uint8_t* data = protocol->data;
  302. furi_string_printf(
  303. result,
  304. "FC: %u\r\n"
  305. "Card: %u",
  306. data[0],
  307. (uint16_t)((data[1] << 8) | (data[2])));
  308. };
  309. const ProtocolBase protocol_h10301 = {
  310. .name = "H10301",
  311. .manufacturer = "HID",
  312. .data_size = H10301_DECODED_DATA_SIZE,
  313. .features = LFRFIDFeatureASK,
  314. .validate_count = 3,
  315. .alloc = (ProtocolAlloc)protocol_h10301_alloc,
  316. .free = (ProtocolFree)protocol_h10301_free,
  317. .get_data = (ProtocolGetData)protocol_h10301_get_data,
  318. .decoder =
  319. {
  320. .start = (ProtocolDecoderStart)protocol_h10301_decoder_start,
  321. .feed = (ProtocolDecoderFeed)protocol_h10301_decoder_feed,
  322. },
  323. .encoder =
  324. {
  325. .start = (ProtocolEncoderStart)protocol_h10301_encoder_start,
  326. .yield = (ProtocolEncoderYield)protocol_h10301_encoder_yield,
  327. },
  328. .render_data = (ProtocolRenderData)protocol_h10301_render_data,
  329. .render_brief_data = (ProtocolRenderData)protocol_h10301_render_data,
  330. .write_data = (ProtocolWriteData)protocol_h10301_write_data,
  331. };