subbrute_device.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. #include "subbrute_device.h"
  2. #include <stdint.h>
  3. #include <storage/storage.h>
  4. #include <lib/toolbox/stream/stream.h>
  5. #include <lib/flipper_format/flipper_format.h>
  6. #include <lib/flipper_format/flipper_format_i.h>
  7. #define TAG "SubBruteDevice"
  8. #define SUBBRUTE_TX_TIMEOUT 5
  9. #define SUBBRUTE_MANUAL_TRANSMIT_INTERVAL 400
  10. /**
  11. * Values to not use less memory for packet parse operations
  12. */
  13. static const char* subbrute_key_file_start =
  14. "Filetype: Flipper SubGhz Key File\nVersion: 1\nFrequency: %u\nPreset: %s\nProtocol: %s\nBit: %d";
  15. static const char* subbrute_key_file_key = "%s\nKey: %s\nRepeat: %d\n";
  16. static const char* subbrute_key_file_key_with_tail = "%s\nKey: %s\nTE: %d\nRepeat: %d\n";
  17. static const char* subbrute_key_small_no_tail = "Bit: %d\nKey: %s\nRepeat: %d\nRepeat: %d\n";
  18. static const char* subbrute_key_small_with_tail = "Bit: %d\nKey: %s\nTE: %d\nRepeat: %d\n";
  19. SubBruteDevice* subbrute_device_alloc() {
  20. SubBruteDevice* instance = malloc(sizeof(SubBruteDevice));
  21. instance->state = SubBruteDeviceStateIDLE;
  22. instance->key_index = 0;
  23. instance->worker_running = false;
  24. instance->last_time_tx_data = 0;
  25. instance->thread = furi_thread_alloc();
  26. furi_thread_set_name(instance->thread, "SubBruteAttackWorker");
  27. furi_thread_set_stack_size(instance->thread, 2048);
  28. furi_thread_set_context(instance->thread, instance);
  29. furi_thread_set_callback(instance->thread, subbrute_worker_thread);
  30. instance->context = NULL;
  31. instance->callback = NULL;
  32. instance->protocol_info = NULL;
  33. instance->file_protocol_info = NULL;
  34. instance->decoder_result = NULL;
  35. instance->transmitter = NULL;
  36. instance->receiver = NULL;
  37. instance->environment = subghz_environment_alloc();
  38. subbrute_device_attack_set_default_values(instance, SubBruteAttackCAME12bit433);
  39. return instance;
  40. }
  41. void subbrute_device_free(SubBruteDevice* instance) {
  42. furi_assert(instance);
  43. // I don't know how to free this
  44. instance->decoder_result = NULL;
  45. if(instance->receiver != NULL) {
  46. subghz_receiver_free(instance->receiver);
  47. instance->receiver = NULL;
  48. }
  49. if(instance->transmitter != NULL) {
  50. subghz_transmitter_free(instance->transmitter);
  51. instance->transmitter = NULL;
  52. }
  53. subghz_environment_free(instance->environment);
  54. instance->environment = NULL;
  55. furi_thread_free(instance->thread);
  56. subbrute_device_free_protocol_info(instance);
  57. free(instance);
  58. }
  59. /**
  60. * Entrypoint for worker
  61. *
  62. * @param context SubBruteWorker*
  63. * @return 0 if ok
  64. */
  65. int32_t subbrute_worker_thread(void* context) {
  66. furi_assert(context);
  67. SubBruteDevice* instance = (SubBruteDevice*)context;
  68. if(!instance->worker_running) {
  69. FURI_LOG_W(TAG, "Worker is not set to running state!");
  70. return -1;
  71. }
  72. if(instance->state != SubBruteDeviceStateReady &&
  73. instance->state != SubBruteDeviceStateFinished) {
  74. FURI_LOG_W(TAG, "Invalid state for running worker! State: %d", instance->state);
  75. return -2;
  76. }
  77. #ifdef FURI_DEBUG
  78. FURI_LOG_I(TAG, "Worker start");
  79. #endif
  80. SubBruteDeviceState local_state = instance->state = SubBruteDeviceStateTx;
  81. subbrute_device_send_callback(instance);
  82. FlipperFormat* flipper_format = flipper_format_string_alloc();
  83. while(instance->worker_running) {
  84. if(!subbrute_device_create_packet_parsed(
  85. instance, flipper_format, instance->key_index, true)) {
  86. FURI_LOG_W(TAG, "Error creating packet! BREAK");
  87. instance->worker_running = false;
  88. local_state = SubBruteDeviceStateIDLE;
  89. break;
  90. }
  91. subbrute_device_subghz_transmit(instance, flipper_format);
  92. if(instance->key_index + 1 > instance->max_value) {
  93. #ifdef FURI_DEBUG
  94. FURI_LOG_I(TAG, "Worker finished to end");
  95. #endif
  96. local_state = SubBruteDeviceStateFinished;
  97. break;
  98. }
  99. instance->key_index++;
  100. furi_delay_ms(SUBBRUTE_TX_TIMEOUT);
  101. }
  102. flipper_format_free(flipper_format);
  103. instance->worker_running = false; // Because we have error states
  104. instance->state = local_state == SubBruteDeviceStateTx ? SubBruteDeviceStateReady :
  105. local_state;
  106. subbrute_device_send_callback(instance);
  107. #ifdef FURI_DEBUG
  108. FURI_LOG_I(TAG, "Worker stop");
  109. #endif
  110. return 0;
  111. }
  112. bool subbrute_worker_start(SubBruteDevice* instance) {
  113. furi_assert(instance);
  114. if(instance->worker_running) {
  115. FURI_LOG_W(TAG, "Worker is already running!");
  116. return false;
  117. }
  118. if(instance->state != SubBruteDeviceStateReady &&
  119. instance->state != SubBruteDeviceStateFinished) {
  120. FURI_LOG_W(TAG, "Worker cannot start, invalid device state: %d", instance->state);
  121. return false;
  122. }
  123. if((instance->protocol_info == NULL && instance->attack != SubBruteAttackLoadFile) ||
  124. (instance->attack == SubBruteAttackLoadFile && instance->file_protocol_info == NULL)) {
  125. FURI_LOG_W(TAG, "Worker cannot start, protocol_info is NULL!");
  126. return false;
  127. }
  128. instance->worker_running = true;
  129. furi_thread_start(instance->thread);
  130. return true;
  131. }
  132. void subbrute_worker_stop(SubBruteDevice* instance) {
  133. furi_assert(instance);
  134. instance->worker_running = false;
  135. furi_thread_join(instance->thread);
  136. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  137. furi_hal_subghz_sleep();
  138. }
  139. SubBruteAttacks subbrute_device_get_attack(SubBruteDevice* instance) {
  140. return instance->attack;
  141. }
  142. bool subbrute_device_is_worker_running(SubBruteDevice* instance) {
  143. return instance->worker_running;
  144. }
  145. uint64_t subbrute_device_get_max_value(SubBruteDevice* instance) {
  146. return instance->max_value;
  147. }
  148. uint64_t subbrute_device_get_step(SubBruteDevice* instance) {
  149. return instance->key_index;
  150. }
  151. const char* subbrute_device_get_file_key(SubBruteDevice* instance) {
  152. return instance->file_key;
  153. }
  154. uint64_t subbrute_device_add_step(SubBruteDevice* instance, int8_t step) {
  155. if(!subbrute_device_can_manual_transmit(instance)) {
  156. return instance->key_index;
  157. }
  158. if(step > 0) {
  159. if((instance->key_index + step) - instance->max_value == 1) {
  160. instance->key_index = 0x00;
  161. } else {
  162. uint64_t value = instance->key_index + step;
  163. if(value == instance->max_value) {
  164. instance->key_index = value;
  165. } else {
  166. instance->key_index = value % instance->max_value;
  167. }
  168. }
  169. } else {
  170. if(instance->key_index + step == 0) {
  171. instance->key_index = 0x00;
  172. } else if(instance->key_index == 0) {
  173. instance->key_index = instance->max_value;
  174. } else {
  175. uint64_t value = ((instance->key_index - step) + instance->max_value);
  176. if(value == instance->max_value) {
  177. instance->key_index = value;
  178. } else {
  179. instance->key_index = value % instance->max_value;
  180. }
  181. }
  182. }
  183. return instance->key_index;
  184. }
  185. void subbrute_device_set_load_index(SubBruteDevice* instance, uint64_t load_index) {
  186. instance->load_index = load_index;
  187. }
  188. void subbrute_device_reset_step(SubBruteDevice* instance) {
  189. instance->key_index = 0x00;
  190. }
  191. void subbrute_device_subghz_transmit(SubBruteDevice* instance, FlipperFormat* flipper_format) {
  192. instance->transmitter = subghz_transmitter_alloc_init(
  193. instance->environment, subbrute_protocol_name(instance->attack));
  194. subghz_transmitter_deserialize(instance->transmitter, flipper_format);
  195. furi_hal_subghz_reset();
  196. if(instance->attack == SubBruteAttackLoadFile) {
  197. furi_hal_subghz_load_preset(instance->file_protocol_info->preset);
  198. furi_hal_subghz_set_frequency_and_path(instance->file_protocol_info->preset);
  199. } else {
  200. furi_hal_subghz_load_preset(instance->protocol_info->preset);
  201. furi_hal_subghz_set_frequency_and_path(instance->protocol_info->preset);
  202. }
  203. furi_hal_subghz_start_async_tx(subghz_transmitter_yield, instance->transmitter);
  204. while(!furi_hal_subghz_is_async_tx_complete()) {
  205. furi_delay_ms(SUBBRUTE_TX_TIMEOUT);
  206. }
  207. furi_hal_subghz_stop_async_tx();
  208. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  209. furi_hal_subghz_sleep();
  210. subghz_transmitter_free(instance->transmitter);
  211. instance->transmitter = NULL;
  212. }
  213. bool subbrute_device_transmit_current_key(SubBruteDevice* instance) {
  214. furi_assert(instance);
  215. if(instance->worker_running) {
  216. FURI_LOG_W(TAG, "Worker in running state!");
  217. return false;
  218. }
  219. if(instance->state != SubBruteDeviceStateReady &&
  220. instance->state != SubBruteDeviceStateFinished) {
  221. FURI_LOG_W(TAG, "Invalid state for running worker! State: %d", instance->state);
  222. return false;
  223. }
  224. uint32_t ticks = furi_get_tick();
  225. if((ticks - instance->last_time_tx_data) < SUBBRUTE_MANUAL_TRANSMIT_INTERVAL) {
  226. #if FURI_DEBUG
  227. FURI_LOG_D(TAG, "Need to wait, current: %ld", ticks - instance->last_time_tx_data);
  228. #endif
  229. return false;
  230. }
  231. instance->last_time_tx_data = ticks;
  232. #ifdef FURI_DEBUG
  233. if(instance->attack == SubBruteAttackLoadFile) {
  234. FURI_LOG_D(
  235. TAG,
  236. "Protocol: %d, Frequency: %ld",
  237. instance->file_protocol_info->file,
  238. instance->file_protocol_info->frequency);
  239. } else {
  240. FURI_LOG_D(
  241. TAG,
  242. "Protocol: %d, Frequency: %ld",
  243. instance->protocol_info->file,
  244. instance->protocol_info->frequency);
  245. }
  246. #endif
  247. FlipperFormat* flipper_format = flipper_format_string_alloc();
  248. if(!subbrute_device_create_packet_parsed(instance, flipper_format, instance->key_index, true)) {
  249. FURI_LOG_W(TAG, "Error creating packet! EXIT");
  250. return false;
  251. }
  252. subbrute_device_subghz_transmit(instance, flipper_format);
  253. flipper_format_free(flipper_format);
  254. return true;
  255. }
  256. void subbrute_device_set_callback(
  257. SubBruteDevice* instance,
  258. SubBruteDeviceWorkerCallback callback,
  259. void* context) {
  260. furi_assert(instance);
  261. instance->callback = callback;
  262. instance->context = context;
  263. }
  264. bool subbrute_device_can_manual_transmit(SubBruteDevice* instance) {
  265. furi_assert(instance);
  266. return !instance->worker_running && instance->state != SubBruteDeviceStateIDLE &&
  267. instance->state != SubBruteDeviceStateTx &&
  268. ((furi_get_tick() - instance->last_time_tx_data) > SUBBRUTE_MANUAL_TRANSMIT_INTERVAL);
  269. }
  270. bool subbrute_device_save_file(SubBruteDevice* instance, const char* dev_file_name) {
  271. furi_assert(instance);
  272. if(instance->state != SubBruteDeviceStateReady &&
  273. instance->state != SubBruteDeviceStateFinished) {
  274. FURI_LOG_W(TAG, "Worker is not set to running state!");
  275. return false;
  276. }
  277. #ifdef FURI_DEBUG
  278. FURI_LOG_D(TAG, "subbrute_device_save_file: %s", dev_file_name);
  279. #endif
  280. Storage* storage = furi_record_open(RECORD_STORAGE);
  281. FlipperFormat* file = flipper_format_file_alloc(storage);
  282. bool result = false;
  283. do {
  284. if(!flipper_format_file_open_always(file, dev_file_name)) {
  285. break;
  286. }
  287. if(!subbrute_device_create_packet_parsed(instance, file, instance->key_index, false)) {
  288. FURI_LOG_E(TAG, "create_packet_parsed failed!");
  289. break;
  290. }
  291. result = true;
  292. } while(false);
  293. if(!result) {
  294. FURI_LOG_E(TAG, "flipper_format_file_open_always failed!");
  295. }
  296. flipper_format_free(file);
  297. furi_record_close(RECORD_STORAGE);
  298. return result;
  299. }
  300. bool subbrute_device_create_packet_parsed(
  301. SubBruteDevice* instance,
  302. FlipperFormat* flipper_format,
  303. uint64_t step,
  304. bool small) {
  305. furi_assert(instance);
  306. FuriString* candidate = furi_string_alloc();
  307. Stream* stream = flipper_format_get_raw_stream(flipper_format);
  308. stream_clean(stream);
  309. if(instance->attack == SubBruteAttackLoadFile) {
  310. if(step >= sizeof(instance->file_key)) {
  311. return false;
  312. }
  313. char subbrute_payload_byte[4];
  314. furi_string_set_str(candidate, instance->file_key);
  315. snprintf(subbrute_payload_byte, 4, "%02X ", (uint8_t)step);
  316. furi_string_replace_at(candidate, instance->load_index * 3, 3, subbrute_payload_byte);
  317. //snprintf(step_payload, sizeof(step_payload), "%02X", (uint8_t)instance->file_key[step]);
  318. if(small) {
  319. if(instance->file_protocol_info->te) {
  320. stream_write_format(
  321. stream,
  322. subbrute_key_small_with_tail,
  323. instance->file_protocol_info->bits,
  324. furi_string_get_cstr(candidate),
  325. instance->file_protocol_info->te,
  326. instance->file_protocol_info->repeat);
  327. } else {
  328. stream_write_format(
  329. stream,
  330. subbrute_key_small_no_tail,
  331. instance->file_protocol_info->bits,
  332. furi_string_get_cstr(candidate),
  333. instance->file_protocol_info->repeat);
  334. }
  335. } else {
  336. if(instance->file_protocol_info->te) {
  337. stream_write_format(
  338. stream,
  339. subbrute_key_file_key_with_tail,
  340. instance->file_template,
  341. furi_string_get_cstr(candidate),
  342. instance->file_protocol_info->te,
  343. instance->file_protocol_info->repeat);
  344. } else {
  345. stream_write_format(
  346. stream,
  347. subbrute_key_file_key,
  348. instance->file_template,
  349. furi_string_get_cstr(candidate),
  350. instance->file_protocol_info->repeat);
  351. }
  352. }
  353. } else {
  354. //snprintf(step_payload, sizeof(step_payload), "%16X", step);
  355. //snprintf(step_payload, sizeof(step_payload), "%016llX", step);
  356. FuriString* buffer = furi_string_alloc();
  357. furi_string_printf(buffer, "%16llX", step);
  358. int j = 0;
  359. furi_string_set_str(candidate, " ");
  360. for(uint8_t i = 0; i < 16; i++) {
  361. if(furi_string_get_char(buffer, i) != ' ') {
  362. furi_string_set_char(candidate, i + j, furi_string_get_char(buffer, i));
  363. } else {
  364. furi_string_set_char(candidate, i + j, '0');
  365. }
  366. if(i % 2 != 0) {
  367. j++;
  368. }
  369. }
  370. furi_string_free(buffer);
  371. #ifdef FURI_DEBUG
  372. FURI_LOG_D(TAG, "candidate: %s, step: %lld", furi_string_get_cstr(candidate), step);
  373. #endif
  374. if(small) {
  375. if(instance->protocol_info->te) {
  376. stream_write_format(
  377. stream,
  378. subbrute_key_small_with_tail,
  379. instance->protocol_info->bits,
  380. furi_string_get_cstr(candidate),
  381. instance->protocol_info->te,
  382. instance->protocol_info->repeat);
  383. } else {
  384. stream_write_format(
  385. stream,
  386. subbrute_key_small_no_tail,
  387. instance->protocol_info->bits,
  388. furi_string_get_cstr(candidate),
  389. instance->protocol_info->repeat);
  390. }
  391. } else {
  392. if(instance->protocol_info->te) {
  393. stream_write_format(
  394. stream,
  395. subbrute_key_file_key_with_tail,
  396. instance->file_template,
  397. furi_string_get_cstr(candidate),
  398. instance->protocol_info->te,
  399. instance->protocol_info->repeat);
  400. } else {
  401. stream_write_format(
  402. stream,
  403. subbrute_key_file_key,
  404. instance->file_template,
  405. furi_string_get_cstr(candidate),
  406. instance->protocol_info->repeat);
  407. }
  408. }
  409. #ifdef FURI_DEBUG
  410. FURI_LOG_D(TAG, "candidate: %s", furi_string_get_cstr(candidate));
  411. #endif
  412. }
  413. furi_string_free(candidate);
  414. return true;
  415. }
  416. SubBruteFileResult subbrute_device_attack_set(SubBruteDevice* instance, SubBruteAttacks type) {
  417. furi_assert(instance);
  418. #ifdef FURI_DEBUG
  419. FURI_LOG_D(TAG, "subbrute_device_attack_set: %d", type);
  420. #endif
  421. subbrute_device_attack_set_default_values(instance, type);
  422. if(type != SubBruteAttackLoadFile) {
  423. subbrute_device_free_protocol_info(instance);
  424. instance->protocol_info = subbrute_protocol(type);
  425. }
  426. // For non-file types we didn't set SubGhzProtocolDecoderBase
  427. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  428. subghz_receiver_set_filter(instance->receiver, SubGhzProtocolFlag_Decodable);
  429. furi_hal_subghz_reset();
  430. uint8_t protocol_check_result = SubBruteFileResultProtocolNotFound;
  431. if(type != SubBruteAttackLoadFile) {
  432. instance->decoder_result = subghz_receiver_search_decoder_base_by_name(
  433. instance->receiver, subbrute_protocol_file(instance->protocol_info->file));
  434. if(!instance->decoder_result ||
  435. instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  436. FURI_LOG_E(TAG, "Can't load SubGhzProtocolDecoderBase in phase non-file decoder set");
  437. } else {
  438. protocol_check_result = SubBruteFileResultOk;
  439. }
  440. } else {
  441. // And here we need to set preset enum
  442. protocol_check_result = SubBruteFileResultOk;
  443. }
  444. subghz_receiver_free(instance->receiver);
  445. instance->receiver = NULL;
  446. if(protocol_check_result != SubBruteFileResultOk) {
  447. return SubBruteFileResultProtocolNotFound;
  448. }
  449. // Calc max value
  450. if(instance->attack == SubBruteAttackLoadFile) {
  451. instance->max_value = 0x3F;
  452. // Now we are ready to set file template for using in the future with snprintf
  453. // for sending attack payload ONLY for files!
  454. snprintf(
  455. instance->file_template,
  456. sizeof(instance->file_template),
  457. subbrute_key_file_start,
  458. instance->file_protocol_info->frequency,
  459. subbrute_protocol_preset(instance->file_protocol_info->preset),
  460. subbrute_protocol_file(instance->file_protocol_info->file),
  461. instance->file_protocol_info->bits);
  462. } else {
  463. FuriString* max_value_s;
  464. max_value_s = furi_string_alloc();
  465. for(uint8_t i = 0; i < instance->protocol_info->bits; i++) {
  466. furi_string_cat_printf(max_value_s, "1");
  467. }
  468. instance->max_value = (uint64_t)strtol(furi_string_get_cstr(max_value_s), NULL, 2);
  469. furi_string_free(max_value_s);
  470. // Now we are ready to set file template for using in the future with snprintf
  471. // for sending attack payload
  472. snprintf(
  473. instance->file_template,
  474. sizeof(instance->file_template),
  475. subbrute_key_file_start,
  476. instance->protocol_info->frequency,
  477. subbrute_protocol_preset(instance->protocol_info->preset),
  478. subbrute_protocol_file(instance->protocol_info->file),
  479. instance->protocol_info->bits);
  480. #ifdef FURI_DEBUG
  481. FURI_LOG_D(
  482. TAG,
  483. "tail: %d, file_template: %s",
  484. instance->protocol_info->te,
  485. instance->file_template);
  486. #endif
  487. }
  488. // Init payload
  489. FlipperFormat* flipper_format = flipper_format_string_alloc();
  490. if(subbrute_device_create_packet_parsed(instance, flipper_format, instance->key_index, false)) {
  491. instance->state = SubBruteDeviceStateReady;
  492. subbrute_device_send_callback(instance);
  493. }
  494. flipper_format_free(flipper_format);
  495. return SubBruteFileResultOk;
  496. }
  497. uint8_t subbrute_device_load_from_file(SubBruteDevice* instance, const char* file_path) {
  498. furi_assert(instance);
  499. #ifdef FURI_DEBUG
  500. FURI_LOG_D(TAG, "subbrute_device_load_from_file: %s", file_path);
  501. #endif
  502. SubBruteFileResult result = SubBruteFileResultUnknown;
  503. Storage* storage = furi_record_open(RECORD_STORAGE);
  504. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  505. subbrute_device_free_protocol_info(instance);
  506. instance->file_protocol_info = malloc(sizeof(SubBruteProtocol));
  507. FuriString* temp_str;
  508. temp_str = furi_string_alloc();
  509. uint32_t temp_data32;
  510. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  511. subghz_receiver_set_filter(instance->receiver, SubGhzProtocolFlag_Decodable);
  512. furi_hal_subghz_reset();
  513. do {
  514. if(!flipper_format_file_open_existing(fff_data_file, file_path)) {
  515. FURI_LOG_E(TAG, "Error open file %s", file_path);
  516. result = SubBruteFileResultErrorOpenFile;
  517. break;
  518. }
  519. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  520. FURI_LOG_E(TAG, "Missing or incorrect header");
  521. result = SubBruteFileResultMissingOrIncorrectHeader;
  522. break;
  523. }
  524. // Frequency
  525. if(flipper_format_read_uint32(fff_data_file, "Frequency", &temp_data32, 1)) {
  526. instance->file_protocol_info->frequency = temp_data32;
  527. if(!furi_hal_subghz_is_tx_allowed(instance->file_protocol_info->frequency)) {
  528. result = SubBruteFileResultFrequencyNotAllowed;
  529. break;
  530. }
  531. } else {
  532. FURI_LOG_E(TAG, "Missing or incorrect Frequency");
  533. result = SubBruteFileResultMissingOrIncorrectFrequency;
  534. break;
  535. }
  536. // Preset
  537. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  538. FURI_LOG_E(TAG, "Preset FAIL");
  539. result = SubBruteFileResultPresetInvalid;
  540. } else {
  541. instance->file_protocol_info->preset = subbrute_protocol_convert_preset(temp_str);
  542. }
  543. const char* protocol_file = NULL;
  544. // Protocol
  545. if(!flipper_format_read_string(fff_data_file, "Protocol", temp_str)) {
  546. FURI_LOG_E(TAG, "Missing Protocol");
  547. result = SubBruteFileResultMissingProtocol;
  548. break;
  549. } else {
  550. instance->file_protocol_info->file = subbrute_protocol_file_protocol_name(temp_str);
  551. protocol_file = subbrute_protocol_file(instance->file_protocol_info->file);
  552. #ifdef FURI_DEBUG
  553. FURI_LOG_D(TAG, "Protocol: %s", protocol_file);
  554. #endif
  555. }
  556. instance->decoder_result =
  557. subghz_receiver_search_decoder_base_by_name(instance->receiver, protocol_file);
  558. if(!instance->decoder_result || strcmp(protocol_file, "RAW") == 0) {
  559. FURI_LOG_E(TAG, "RAW unsupported");
  560. result = SubBruteFileResultProtocolNotSupported;
  561. break;
  562. }
  563. if(instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  564. FURI_LOG_E(TAG, "Protocol is dynamic - not supported");
  565. result = SubBruteFileResultDynamicProtocolNotValid;
  566. break;
  567. }
  568. #ifdef FURI_DEBUG
  569. else {
  570. FURI_LOG_D(TAG, "Decoder: %s", instance->decoder_result->protocol->name);
  571. }
  572. #endif
  573. // Bit
  574. if(!flipper_format_read_uint32(fff_data_file, "Bit", &temp_data32, 1)) {
  575. FURI_LOG_E(TAG, "Missing or incorrect Bit");
  576. result = SubBruteFileResultMissingOrIncorrectBit;
  577. break;
  578. } else {
  579. instance->file_protocol_info->bits = temp_data32;
  580. #ifdef FURI_DEBUG
  581. FURI_LOG_D(TAG, "Bit: %d", instance->file_protocol_info->bits);
  582. #endif
  583. }
  584. // Key
  585. if(!flipper_format_read_string(fff_data_file, "Key", temp_str)) {
  586. FURI_LOG_E(TAG, "Missing or incorrect Key");
  587. result = SubBruteFileResultMissingOrIncorrectKey;
  588. break;
  589. } else {
  590. snprintf(
  591. instance->file_key,
  592. sizeof(instance->file_key),
  593. "%s",
  594. furi_string_get_cstr(temp_str));
  595. #ifdef FURI_DEBUG
  596. FURI_LOG_D(TAG, "Key: %s", instance->file_key);
  597. #endif
  598. }
  599. // TE
  600. if(!flipper_format_read_uint32(fff_data_file, "TE", &temp_data32, 1)) {
  601. FURI_LOG_E(TAG, "Missing or incorrect TE");
  602. //result = SubBruteFileResultMissingOrIncorrectTe;
  603. //break;
  604. } else {
  605. instance->file_protocol_info->te = temp_data32 != 0;
  606. }
  607. // Repeat
  608. if(flipper_format_read_uint32(fff_data_file, "Repeat", &temp_data32, 1)) {
  609. #ifdef FURI_DEBUG
  610. FURI_LOG_D(TAG, "Repeat: %ld", temp_data32);
  611. #endif
  612. instance->file_protocol_info->repeat = (uint8_t)temp_data32;
  613. } else {
  614. #ifdef FURI_DEBUG
  615. FURI_LOG_D(TAG, "Repeat: 3 (default)");
  616. #endif
  617. instance->file_protocol_info->repeat = 3;
  618. }
  619. result = SubBruteFileResultOk;
  620. } while(0);
  621. furi_string_free(temp_str);
  622. flipper_format_file_close(fff_data_file);
  623. flipper_format_free(fff_data_file);
  624. furi_record_close(RECORD_STORAGE);
  625. subghz_receiver_free(instance->receiver);
  626. instance->decoder_result = NULL;
  627. instance->receiver = NULL;
  628. if(result == SubBruteFileResultOk) {
  629. #ifdef FURI_DEBUG
  630. FURI_LOG_D(TAG, "Loaded successfully");
  631. #endif
  632. } else {
  633. subbrute_device_free_protocol_info(instance);
  634. }
  635. return result;
  636. }
  637. void subbrute_device_attack_set_default_values(
  638. SubBruteDevice* instance,
  639. SubBruteAttacks default_attack) {
  640. furi_assert(instance);
  641. #ifdef FURI_DEBUG
  642. FURI_LOG_D(TAG, "subbrute_device_attack_set_default_values");
  643. #endif
  644. instance->attack = default_attack;
  645. instance->key_index = 0x00;
  646. instance->load_index = 0x00;
  647. memset(instance->file_template, 0, sizeof(instance->file_template));
  648. memset(instance->current_key, 0, sizeof(instance->current_key));
  649. if(default_attack != SubBruteAttackLoadFile) {
  650. memset(instance->file_key, 0, sizeof(instance->file_key));
  651. instance->max_value = (uint64_t)0x00;
  652. }
  653. }
  654. void subbrute_device_send_callback(SubBruteDevice* instance) {
  655. if(instance->callback != NULL) {
  656. instance->callback(instance->context, instance->state);
  657. }
  658. }
  659. const char* subbrute_device_error_get_desc(SubBruteFileResult error_id) {
  660. const char* result;
  661. switch(error_id) {
  662. case(SubBruteFileResultOk):
  663. result = "OK";
  664. break;
  665. case(SubBruteFileResultErrorOpenFile):
  666. result = "invalid name/path";
  667. break;
  668. case(SubBruteFileResultMissingOrIncorrectHeader):
  669. result = "Missing or incorrect header";
  670. break;
  671. case(SubBruteFileResultFrequencyNotAllowed):
  672. result = "Invalid frequency!";
  673. break;
  674. case(SubBruteFileResultMissingOrIncorrectFrequency):
  675. result = "Missing or incorrect Frequency";
  676. break;
  677. case(SubBruteFileResultPresetInvalid):
  678. result = "Preset FAIL";
  679. break;
  680. case(SubBruteFileResultMissingProtocol):
  681. result = "Missing Protocol";
  682. break;
  683. case(SubBruteFileResultProtocolNotSupported):
  684. result = "RAW unsupported";
  685. break;
  686. case(SubBruteFileResultDynamicProtocolNotValid):
  687. result = "Dynamic protocol unsupported";
  688. break;
  689. case(SubBruteFileResultProtocolNotFound):
  690. result = "Protocol not found";
  691. break;
  692. case(SubBruteFileResultMissingOrIncorrectBit):
  693. result = "Missing or incorrect Bit";
  694. break;
  695. case(SubBruteFileResultMissingOrIncorrectKey):
  696. result = "Missing or incorrect Key";
  697. break;
  698. case(SubBruteFileResultMissingOrIncorrectTe):
  699. result = "Missing or incorrect TE";
  700. break;
  701. case SubBruteFileResultUnknown:
  702. default:
  703. result = "Unknown error";
  704. break;
  705. }
  706. return result;
  707. }
  708. void subbrute_device_free_protocol_info(SubBruteDevice* instance) {
  709. furi_assert(instance);
  710. instance->protocol_info = NULL;
  711. if(instance->file_protocol_info) {
  712. free(instance->file_protocol_info);
  713. }
  714. instance->file_protocol_info = NULL;
  715. }