subghz_protocol_nice_flor_s.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "subghz_protocol_nice_flor_s.h"
  2. #include <furi.h>
  3. #include "file-worker.h"
  4. /*
  5. * https://phreakerclub.com/1615
  6. * https://phreakerclub.com/forum/showthread.php?t=2360
  7. * https://vrtp.ru/index.php?showtopic=27867
  8. */
  9. struct SubGhzProtocolNiceFlorS {
  10. SubGhzProtocolCommon common;
  11. const char* rainbow_table_file_name;
  12. };
  13. SubGhzProtocolNiceFlorS* subghz_protocol_nice_flor_s_alloc() {
  14. SubGhzProtocolNiceFlorS* instance = furi_alloc(sizeof(SubGhzProtocolNiceFlorS));
  15. instance->common.name = "Nice FloR-S";
  16. instance->common.code_min_count_bit_for_found = 52;
  17. instance->common.te_shot = 500;
  18. instance->common.te_long = 1000;
  19. instance->common.te_delta = 300;
  20. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_nice_flor_s_to_str;
  21. return instance;
  22. }
  23. void subghz_protocol_nice_flor_s_free(SubGhzProtocolNiceFlorS* instance) {
  24. furi_assert(instance);
  25. free(instance);
  26. }
  27. void subghz_protocol_nice_flor_s_name_file(SubGhzProtocolNiceFlorS* instance, const char* name) {
  28. instance->rainbow_table_file_name = name;
  29. printf("Loading Nice FloR S rainbow table %s\r\n", name);
  30. }
  31. /** Send bit
  32. *
  33. * @param instance - SubGhzProtocolNiceFlorS instance
  34. * @param bit - bit
  35. */
  36. void subghz_protocol_nice_flor_s_send_bit(SubGhzProtocolNiceFlorS* instance, uint8_t bit) {
  37. if(bit) {
  38. //send bit 1
  39. SUBGHZ_TX_PIN_HIGTH();
  40. delay_us(instance->common.te_long);
  41. SUBGHZ_TX_PIN_LOW();
  42. delay_us(instance->common.te_shot);
  43. } else {
  44. //send bit 0
  45. SUBGHZ_TX_PIN_HIGTH();
  46. delay_us(instance->common.te_shot);
  47. SUBGHZ_TX_PIN_LOW();
  48. delay_us(instance->common.te_long);
  49. }
  50. }
  51. void subghz_protocol_nice_flor_s_send_key(
  52. SubGhzProtocolNiceFlorS* instance,
  53. uint64_t key,
  54. uint8_t bit,
  55. uint8_t repeat) {
  56. while(repeat--) {
  57. //Send header
  58. SUBGHZ_TX_PIN_LOW();
  59. delay_us(instance->common.te_shot * 34);
  60. //Send Start Bit
  61. SUBGHZ_TX_PIN_HIGTH();
  62. delay_us(instance->common.te_shot * 3);
  63. SUBGHZ_TX_PIN_LOW();
  64. delay_us(instance->common.te_shot * 3);
  65. //Send key data
  66. for(uint8_t i = bit; i > 0; i--) {
  67. subghz_protocol_nice_flor_s_send_bit(instance, bit_read(key, i - 1));
  68. }
  69. //Send Stop Bit
  70. SUBGHZ_TX_PIN_HIGTH();
  71. delay_us(instance->common.te_shot * 3);
  72. SUBGHZ_TX_PIN_LOW();
  73. delay_us(instance->common.te_shot * 3);
  74. }
  75. }
  76. /** Read bytes from rainbow table
  77. *
  78. * @param instance - SubGhzProtocolNiceFlorS* instance
  79. * @param address - address byte
  80. * @return byte data
  81. */
  82. uint8_t subghz_nice_flor_s_get_byte_in_file(SubGhzProtocolNiceFlorS* instance, uint32_t address) {
  83. if(!instance->rainbow_table_file_name)
  84. return 0;
  85. uint8_t buffer = 0;
  86. FileWorker* file_worker = file_worker_alloc(true);
  87. if(file_worker_open(file_worker, instance->rainbow_table_file_name, FSAM_READ, FSOM_OPEN_EXISTING)) {
  88. file_worker_seek(file_worker, address, true);
  89. file_worker_read(file_worker, &buffer, 1);
  90. // bool res = file_worker_read(file_worker, &buffer, 1);
  91. // furi_assert(res== true);
  92. }
  93. file_worker_close(file_worker);
  94. file_worker_free(file_worker);
  95. return buffer;
  96. }
  97. /** Decrypt protocol Nice Flor S
  98. *
  99. * @param instance - SubGhzProtocolNiceFlorS* instance
  100. */
  101. void subghz_nice_flor_s_decoder_decrypt(SubGhzProtocolNiceFlorS* instance) {
  102. /*
  103. * Packet format Nice Flor-s: START-P0-P1-P2-P3-P4-P5-P6-P7-STOP
  104. * P0 (4-bit) - button positional code - 1:0x1, 2:0x2, 3:0x4, 4:0x8;
  105. * P1 (4-bit) - batch repetition number, calculated by the formula:
  106. * P1 = 0xF ^ P0 ^ n; where n changes from 1 to 15, then 0, and then in a circle
  107. * key 1: {0xF,0xC,0xD,0xA,0xB,0x8,0x9,0x6,0x7,0x4,0x5,0x2,0x3,0x0,0x1,0xE};
  108. * key 2: {0xC,0xF,0xE,0x9,0x8,0xB,0xA,0x5,0x4,0x7,0x6,0x1,0x0,0x3,0x2,0xD};
  109. * key 3: {0xA,0x9,0x8,0xF,0xE,0xD,0xC,0x3,0x2,0x1,0x0,0x7,0x6,0x5,0x4,0xB};
  110. * P2 (4-bit) - part of the serial number, P2 = (K ^ S3) & 0xF;
  111. * P3 (byte) - the major part of the encrypted index
  112. * P4 (byte) - the low-order part of the encrypted index
  113. * P5 (byte) - part of the serial number, P5 = K ^ S2;
  114. * P6 (byte) - part of the serial number, P6 = K ^ S1;
  115. * P7 (byte) - part of the serial number, P7 = K ^ S0;
  116. * K (byte) - depends on P3 and P4, K = Fk(P3, P4);
  117. * S3,S2,S1,S0 - serial number of the console 28 bit.
  118. */
  119. uint16_t p3p4 = (uint16_t)(instance->common.code_found >> 24);
  120. instance->common.cnt = subghz_nice_flor_s_get_byte_in_file(instance,p3p4*2) << 8 | subghz_nice_flor_s_get_byte_in_file(instance,p3p4*2+1); //nice_flor_srainbow_table_for_search[p3p4]; тут надо считать поле с файла причем адрес надо у множить на 2
  121. uint8_t k =(uint8_t)(p3p4 & 0x00FF) ^subghz_nice_flor_s_get_byte_in_file(instance,(0x20000 |(instance->common.cnt &0x00ff))); //nice_flor_srainbow_table_for_search[0x10000|subghz_protocol_nice_flor_s.cnt & 0x00ff];
  122. uint8_t s3 = ((uint8_t)(instance->common.code_found >> 40) ^ k) & 0x0f;
  123. uint8_t s2 = ((uint8_t)(instance->common.code_found >> 16) ^ k);
  124. uint8_t s1 = ((uint8_t)(instance->common.code_found >> 8) ^ k);
  125. uint8_t s0 = ((uint8_t)(instance->common.code_found) ^ k);
  126. instance->common.serial = s3 << 24 | s2 << 16 | s1 << 8 | s0;
  127. instance->common.btn = (instance->common.code_found >> 48) & 0x0f;
  128. if(instance->common.callback) instance->common.callback((SubGhzProtocolCommon*)instance, instance->common.context);
  129. }
  130. void subghz_protocol_nice_flor_s_reset(SubGhzProtocolNiceFlorS* instance) {
  131. instance->common.parser_step = 0;
  132. }
  133. void subghz_protocol_nice_flor_s_parse(SubGhzProtocolNiceFlorS* instance, bool level, uint32_t duration) {
  134. switch(instance->common.parser_step) {
  135. case 0:
  136. if((!level)
  137. && (DURATION_DIFF(duration, instance->common.te_shot * 38) < instance->common.te_delta * 38)) {
  138. //Found start header Nice Flor-S
  139. instance->common.parser_step = 1;
  140. } else {
  141. instance->common.parser_step = 0;
  142. }
  143. break;
  144. case 1:
  145. if((level)
  146. && (DURATION_DIFF(duration, instance->common.te_shot * 3) < instance->common.te_delta * 3)) {
  147. //Found next header Nice Flor-S
  148. instance->common.parser_step = 2;
  149. } else {
  150. instance->common.parser_step = 0;
  151. }
  152. break;
  153. case 2:
  154. if((!level)
  155. && (DURATION_DIFF(duration, instance->common.te_shot * 3) < instance->common.te_delta * 3)) {
  156. //Found header Nice Flor-S
  157. instance->common.parser_step = 3;
  158. instance->common.code_found = 0;
  159. instance->common.code_count_bit = 0;
  160. } else {
  161. instance->common.parser_step = 0;
  162. }
  163. break;
  164. case 3:
  165. if(level) {
  166. if(DURATION_DIFF(duration, instance->common.te_shot * 3) < instance->common.te_delta) {
  167. //Found STOP bit
  168. instance->common.parser_step = 0;
  169. if(instance->common.code_count_bit >=instance->common.code_min_count_bit_for_found) {
  170. subghz_nice_flor_s_decoder_decrypt(instance);
  171. }
  172. break;
  173. } else {
  174. //save interval
  175. instance->common.te_last = duration;
  176. instance->common.parser_step = 4;
  177. }
  178. }
  179. break;
  180. case 4:
  181. if(!level) {
  182. if((DURATION_DIFF(instance->common.te_last, instance->common.te_shot) < instance->common.te_delta)
  183. &&(DURATION_DIFF(duration, instance->common.te_long) < instance->common.te_delta)) {
  184. subghz_protocol_common_add_bit(&instance->common, 0);
  185. instance->common.parser_step = 3;
  186. } else if(
  187. (DURATION_DIFF(instance->common.te_last, instance->common.te_long) < instance->common.te_delta)
  188. &&(DURATION_DIFF(duration, instance->common.te_shot) < instance->common.te_delta)) {
  189. subghz_protocol_common_add_bit(&instance->common, 1);
  190. instance->common.parser_step = 3;
  191. } else
  192. instance->common.parser_step = 0;
  193. } else {
  194. instance->common.parser_step = 0;
  195. }
  196. break;
  197. }
  198. }
  199. void subghz_protocol_nice_flor_s_to_str(SubGhzProtocolNiceFlorS* instance, string_t output) {
  200. uint32_t code_found_hi = instance->common.code_found >> 32;
  201. uint32_t code_found_lo = instance->common.code_found & 0x00000000ffffffff;
  202. string_cat_printf(
  203. output,
  204. "Protocol %s, %d Bit\r\n"
  205. " KEY:0x%lX%08lX\r\n"
  206. " SN:%05lX\r\n"
  207. " CNT:%04X BTN:%02lX\r\n",
  208. instance->common.name,
  209. instance->common.code_count_bit,
  210. code_found_hi,
  211. code_found_lo,
  212. instance->common.serial,
  213. instance->common.cnt,
  214. instance->common.btn
  215. );
  216. }