protocol_jablotron.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <furi.h>
  2. #include "toolbox/level_duration.h"
  3. #include "protocol_jablotron.h"
  4. #include <toolbox/manchester_decoder.h>
  5. #include <lfrfid/tools/bit_lib.h>
  6. #include "lfrfid_protocols.h"
  7. #define JABLOTRON_ENCODED_BIT_SIZE (64)
  8. #define JABLOTRON_ENCODED_BYTE_SIZE (((JABLOTRON_ENCODED_BIT_SIZE) / 8))
  9. #define JABLOTRON_PREAMBLE_BIT_SIZE (16)
  10. #define JABLOTRON_PREAMBLE_BYTE_SIZE (2)
  11. #define JABLOTRON_ENCODED_BYTE_FULL_SIZE \
  12. (JABLOTRON_ENCODED_BYTE_SIZE + JABLOTRON_PREAMBLE_BYTE_SIZE)
  13. #define JABLOTRON_DECODED_DATA_SIZE (5)
  14. #define JABLOTRON_SHORT_TIME (256)
  15. #define JABLOTRON_LONG_TIME (512)
  16. #define JABLOTRON_JITTER_TIME (120)
  17. #define JABLOTRON_SHORT_TIME_LOW (JABLOTRON_SHORT_TIME - JABLOTRON_JITTER_TIME)
  18. #define JABLOTRON_SHORT_TIME_HIGH (JABLOTRON_SHORT_TIME + JABLOTRON_JITTER_TIME)
  19. #define JABLOTRON_LONG_TIME_LOW (JABLOTRON_LONG_TIME - JABLOTRON_JITTER_TIME)
  20. #define JABLOTRON_LONG_TIME_HIGH (JABLOTRON_LONG_TIME + JABLOTRON_JITTER_TIME)
  21. typedef struct {
  22. bool last_short;
  23. bool last_level;
  24. size_t encoded_index;
  25. uint8_t encoded_data[JABLOTRON_ENCODED_BYTE_FULL_SIZE];
  26. uint8_t data[JABLOTRON_DECODED_DATA_SIZE];
  27. } ProtocolJablotron;
  28. ProtocolJablotron* protocol_jablotron_alloc(void) {
  29. ProtocolJablotron* protocol = malloc(sizeof(ProtocolJablotron));
  30. return protocol;
  31. };
  32. void protocol_jablotron_free(ProtocolJablotron* protocol) {
  33. free(protocol);
  34. };
  35. uint8_t* protocol_jablotron_get_data(ProtocolJablotron* proto) {
  36. return proto->data;
  37. };
  38. void protocol_jablotron_decoder_start(ProtocolJablotron* protocol) {
  39. memset(protocol->encoded_data, 0, JABLOTRON_ENCODED_BYTE_FULL_SIZE);
  40. protocol->last_short = false;
  41. };
  42. uint8_t protocol_jablotron_checksum(uint8_t* bits) {
  43. uint8_t chksum = 0;
  44. for(uint8_t i = 16; i < 56; i += 8) {
  45. chksum += bit_lib_get_bits(bits, i, 8);
  46. }
  47. chksum ^= 0x3A;
  48. return chksum;
  49. }
  50. uint64_t protocol_jablotron_card_id(uint8_t* bytes) {
  51. uint64_t id = 0;
  52. for(int i = 0; i < 5; i++) {
  53. id *= 100;
  54. id += ((bytes[i] & 0xF0) >> 4) * 10 + (bytes[i] & 0x0F);
  55. }
  56. return id;
  57. }
  58. static bool protocol_jablotron_can_be_decoded(ProtocolJablotron* protocol) {
  59. // check 11 bits preamble
  60. if(bit_lib_get_bits_16(protocol->encoded_data, 0, 16) != 0b1111111111111111) return false;
  61. // check next 11 bits preamble
  62. if(bit_lib_get_bits_16(protocol->encoded_data, 64, 16) != 0b1111111111111111) return false;
  63. uint8_t checksum = bit_lib_get_bits(protocol->encoded_data, 56, 8);
  64. if(checksum != protocol_jablotron_checksum(protocol->encoded_data)) return false;
  65. return true;
  66. }
  67. void protocol_jablotron_decode(ProtocolJablotron* protocol) {
  68. bit_lib_copy_bits(protocol->data, 0, 40, protocol->encoded_data, 16);
  69. }
  70. bool protocol_jablotron_decoder_feed(ProtocolJablotron* protocol, bool level, uint32_t duration) {
  71. UNUSED(level);
  72. bool pushed = false;
  73. // Bi-Phase Manchester decoding
  74. if(duration >= JABLOTRON_SHORT_TIME_LOW && duration <= JABLOTRON_SHORT_TIME_HIGH) {
  75. if(protocol->last_short == false) {
  76. protocol->last_short = true;
  77. } else {
  78. pushed = true;
  79. bit_lib_push_bit(protocol->encoded_data, JABLOTRON_ENCODED_BYTE_FULL_SIZE, false);
  80. protocol->last_short = false;
  81. }
  82. } else if(duration >= JABLOTRON_LONG_TIME_LOW && duration <= JABLOTRON_LONG_TIME_HIGH) {
  83. if(protocol->last_short == false) {
  84. pushed = true;
  85. bit_lib_push_bit(protocol->encoded_data, JABLOTRON_ENCODED_BYTE_FULL_SIZE, true);
  86. } else {
  87. // reset
  88. protocol->last_short = false;
  89. }
  90. } else {
  91. // reset
  92. protocol->last_short = false;
  93. }
  94. if(pushed && protocol_jablotron_can_be_decoded(protocol)) {
  95. protocol_jablotron_decode(protocol);
  96. return true;
  97. }
  98. return false;
  99. };
  100. bool protocol_jablotron_encoder_start(ProtocolJablotron* protocol) {
  101. // preamble
  102. bit_lib_set_bits(protocol->encoded_data, 0, 0b11111111, 8);
  103. bit_lib_set_bits(protocol->encoded_data, 8, 0b11111111, 8);
  104. // Full code
  105. bit_lib_copy_bits(protocol->encoded_data, 16, 40, protocol->data, 0);
  106. // Checksum
  107. bit_lib_set_bits(
  108. protocol->encoded_data, 56, protocol_jablotron_checksum(protocol->encoded_data), 8);
  109. protocol->encoded_index = 0;
  110. protocol->last_short = false;
  111. protocol->last_level = false;
  112. return true;
  113. };
  114. LevelDuration protocol_jablotron_encoder_yield(ProtocolJablotron* protocol) {
  115. uint32_t duration;
  116. protocol->last_level = !protocol->last_level;
  117. bool bit = bit_lib_get_bit(protocol->encoded_data, protocol->encoded_index);
  118. // Bi-Phase Manchester encoder
  119. if(bit) {
  120. // one long pulse for 1
  121. duration = JABLOTRON_LONG_TIME / 8;
  122. bit_lib_increment_index(protocol->encoded_index, JABLOTRON_ENCODED_BIT_SIZE);
  123. } else {
  124. // two short pulses for 0
  125. duration = JABLOTRON_SHORT_TIME / 8;
  126. if(protocol->last_short) {
  127. bit_lib_increment_index(protocol->encoded_index, JABLOTRON_ENCODED_BIT_SIZE);
  128. protocol->last_short = false;
  129. } else {
  130. protocol->last_short = true;
  131. }
  132. }
  133. return level_duration_make(protocol->last_level, duration);
  134. };
  135. void protocol_jablotron_render_data(ProtocolJablotron* protocol, FuriString* result) {
  136. uint64_t id = protocol_jablotron_card_id(protocol->data);
  137. furi_string_printf(result, "ID: %llX\r\n", id);
  138. };
  139. bool protocol_jablotron_write_data(ProtocolJablotron* protocol, void* data) {
  140. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  141. bool result = false;
  142. // Correct protocol data by redecoding
  143. protocol_jablotron_encoder_start(protocol);
  144. protocol_jablotron_decode(protocol);
  145. protocol_jablotron_encoder_start(protocol);
  146. if(request->write_type == LFRFIDWriteTypeT5577) {
  147. request->t5577.block[0] = LFRFID_T5577_MODULATION_DIPHASE | LFRFID_T5577_BITRATE_RF_64 |
  148. (2 << LFRFID_T5577_MAXBLOCK_SHIFT);
  149. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  150. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  151. request->t5577.blocks_to_write = 3;
  152. result = true;
  153. }
  154. return result;
  155. };
  156. const ProtocolBase protocol_jablotron = {
  157. .name = "Jablotron",
  158. .manufacturer = "Jablotron",
  159. .data_size = JABLOTRON_DECODED_DATA_SIZE,
  160. .features = LFRFIDFeatureASK,
  161. .validate_count = 3,
  162. .alloc = (ProtocolAlloc)protocol_jablotron_alloc,
  163. .free = (ProtocolFree)protocol_jablotron_free,
  164. .get_data = (ProtocolGetData)protocol_jablotron_get_data,
  165. .decoder =
  166. {
  167. .start = (ProtocolDecoderStart)protocol_jablotron_decoder_start,
  168. .feed = (ProtocolDecoderFeed)protocol_jablotron_decoder_feed,
  169. },
  170. .encoder =
  171. {
  172. .start = (ProtocolEncoderStart)protocol_jablotron_encoder_start,
  173. .yield = (ProtocolEncoderYield)protocol_jablotron_encoder_yield,
  174. },
  175. .render_data = (ProtocolRenderData)protocol_jablotron_render_data,
  176. .render_brief_data = (ProtocolRenderData)protocol_jablotron_render_data,
  177. .write_data = (ProtocolWriteData)protocol_jablotron_write_data,
  178. };