subbrute_device.c 24 KB

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