subbrute_device.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. string_t candidate;
  290. string_init(candidate);
  291. if(instance->attack == SubBruteAttackLoadFile) {
  292. if(step >= sizeof(instance->file_key)) {
  293. return false;
  294. }
  295. char subbrute_payload_byte[4];
  296. string_set_str(candidate, instance->file_key);
  297. snprintf(subbrute_payload_byte, 4, "%02X ", (uint8_t)step);
  298. string_replace_at(candidate, instance->load_index * 3, 3, subbrute_payload_byte);
  299. //snprintf(step_payload, sizeof(step_payload), "%02X", (uint8_t)instance->file_key[step]);
  300. } else {
  301. //snprintf(step_payload, sizeof(step_payload), "%16X", step);
  302. //snprintf(step_payload, sizeof(step_payload), "%016llX", step);
  303. string_t buffer;
  304. string_init(buffer);
  305. string_init_printf(buffer, "%16X", step);
  306. int j = 0;
  307. string_set_str(candidate, " ");
  308. for(uint8_t i = 0; i < 16; i++) {
  309. if(string_get_char(buffer, i) != ' ') {
  310. string_set_char(candidate, i + j, string_get_char(buffer, i));
  311. } else {
  312. string_set_char(candidate, i + j, '0');
  313. }
  314. if(i % 2 != 0) {
  315. j++;
  316. }
  317. }
  318. string_clear(buffer);
  319. }
  320. #ifdef FURI_DEBUG
  321. FURI_LOG_D(TAG, "candidate: %s, step: %d", string_get_cstr(candidate), step);
  322. #endif
  323. Stream* stream = flipper_format_get_raw_stream(flipper_format);
  324. stream_clean(stream);
  325. if(small) {
  326. if(instance->protocol_info->te) {
  327. stream_write_format(
  328. stream,
  329. subbrute_key_small_with_tail,
  330. instance->protocol_info->bits,
  331. string_get_cstr(candidate),
  332. instance->protocol_info->te,
  333. instance->protocol_info->repeat);
  334. } else {
  335. stream_write_format(
  336. stream,
  337. subbrute_key_small_no_tail,
  338. instance->protocol_info->bits,
  339. string_get_cstr(candidate),
  340. instance->protocol_info->repeat);
  341. }
  342. } else {
  343. if(instance->protocol_info->te) {
  344. stream_write_format(
  345. stream,
  346. subbrute_key_file_key_with_tail,
  347. instance->file_template,
  348. string_get_cstr(candidate),
  349. instance->protocol_info->te,
  350. instance->protocol_info->repeat);
  351. } else {
  352. stream_write_format(
  353. stream,
  354. subbrute_key_file_key,
  355. instance->file_template,
  356. string_get_cstr(candidate),
  357. instance->protocol_info->repeat);
  358. }
  359. }
  360. #ifdef FURI_DEBUG
  361. //FURI_LOG_D(TAG, "payload: %s", instance->payload);
  362. #endif
  363. string_clear(candidate);
  364. return true;
  365. }
  366. SubBruteFileResult subbrute_device_attack_set(SubBruteDevice* instance, SubBruteAttacks type) {
  367. furi_assert(instance);
  368. #ifdef FURI_DEBUG
  369. FURI_LOG_D(TAG, "subbrute_device_attack_set: %d", type);
  370. #endif
  371. subbrute_device_attack_set_default_values(instance, type);
  372. if(type != SubBruteAttackLoadFile) {
  373. subbrute_device_free_protocol_info(instance);
  374. instance->protocol_info = subbrute_protocol(type);
  375. }
  376. // For non-file types we didn't set SubGhzProtocolDecoderBase
  377. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  378. subghz_receiver_set_filter(instance->receiver, SubGhzProtocolFlag_Decodable);
  379. furi_hal_subghz_reset();
  380. uint8_t protocol_check_result = SubBruteFileResultProtocolNotFound;
  381. if(type != SubBruteAttackLoadFile) {
  382. instance->decoder_result = subghz_receiver_search_decoder_base_by_name(
  383. instance->receiver, subbrute_protocol_file(instance->protocol_info->file));
  384. if(!instance->decoder_result ||
  385. instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  386. FURI_LOG_E(TAG, "Can't load SubGhzProtocolDecoderBase in phase non-file decoder set");
  387. } else {
  388. protocol_check_result = SubBruteFileResultOk;
  389. }
  390. } else {
  391. // And here we need to set preset enum
  392. protocol_check_result = SubBruteFileResultOk;
  393. }
  394. subghz_receiver_free(instance->receiver);
  395. instance->receiver = NULL;
  396. if(protocol_check_result != SubBruteFileResultOk) {
  397. return SubBruteFileResultProtocolNotFound;
  398. }
  399. // Calc max value
  400. if(instance->attack == SubBruteAttackLoadFile) {
  401. instance->max_value = 0xFF;
  402. } else {
  403. string_t max_value_s;
  404. string_init(max_value_s);
  405. for(uint8_t i = 0; i < instance->protocol_info->bits; i++) {
  406. string_cat_printf(max_value_s, "1");
  407. }
  408. instance->max_value = (uint64_t)strtol(string_get_cstr(max_value_s), NULL, 2);
  409. string_clear(max_value_s);
  410. }
  411. // Now we are ready to set file template for using in the future with snprintf
  412. // for sending attack payload
  413. snprintf(
  414. instance->file_template,
  415. sizeof(instance->file_template),
  416. subbrute_key_file_start,
  417. instance->protocol_info->frequency,
  418. subbrute_protocol_preset(instance->protocol_info->preset),
  419. subbrute_protocol_file(instance->protocol_info->file),
  420. instance->protocol_info->bits);
  421. #ifdef FURI_DEBUG
  422. FURI_LOG_D(
  423. TAG, "tail: %d, file_template: %s", instance->protocol_info->te, instance->file_template);
  424. #endif
  425. // Init payload
  426. FlipperFormat* flipper_format = flipper_format_string_alloc();
  427. if(subbrute_device_create_packet_parsed(instance, flipper_format, instance->key_index, false)) {
  428. instance->state = SubBruteDeviceStateReady;
  429. subbrute_device_send_callback(instance);
  430. }
  431. flipper_format_free(flipper_format);
  432. return SubBruteFileResultOk;
  433. }
  434. uint8_t subbrute_device_load_from_file(SubBruteDevice* instance, string_t file_path) {
  435. furi_assert(instance);
  436. #ifdef FURI_DEBUG
  437. FURI_LOG_D(TAG, "subbrute_device_load_from_file: %s", string_get_cstr(file_path));
  438. #endif
  439. SubBruteFileResult result = SubBruteFileResultUnknown;
  440. Storage* storage = furi_record_open(RECORD_STORAGE);
  441. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  442. string_t temp_str;
  443. string_init(temp_str);
  444. uint32_t temp_data32;
  445. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  446. subghz_receiver_set_filter(instance->receiver, SubGhzProtocolFlag_Decodable);
  447. furi_hal_subghz_reset();
  448. do {
  449. if(!flipper_format_file_open_existing(fff_data_file, string_get_cstr(file_path))) {
  450. FURI_LOG_E(TAG, "Error open file %s", string_get_cstr(file_path));
  451. result = SubBruteFileResultErrorOpenFile;
  452. break;
  453. }
  454. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  455. FURI_LOG_E(TAG, "Missing or incorrect header");
  456. result = SubBruteFileResultMissingOrIncorrectHeader;
  457. break;
  458. }
  459. // Frequency
  460. if(flipper_format_read_uint32(fff_data_file, "Frequency", &temp_data32, 1)) {
  461. instance->protocol_info->frequency = temp_data32;
  462. if(!furi_hal_subghz_is_tx_allowed(instance->protocol_info->frequency)) {
  463. result = SubBruteFileResultFrequencyNotAllowed;
  464. break;
  465. }
  466. } else {
  467. FURI_LOG_E(TAG, "Missing or incorrect Frequency");
  468. result = SubBruteFileResultMissingOrIncorrectFrequency;
  469. break;
  470. }
  471. // Preset
  472. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  473. FURI_LOG_E(TAG, "Preset FAIL");
  474. result = SubBruteFileResultPresetInvalid;
  475. } else {
  476. instance->protocol_info->preset = subbrute_protocol_convert_preset(temp_str);
  477. }
  478. const char* protocol_file = NULL;
  479. // Protocol
  480. if(!flipper_format_read_string(fff_data_file, "Protocol", temp_str)) {
  481. FURI_LOG_E(TAG, "Missing Protocol");
  482. result = SubBruteFileResultMissingProtocol;
  483. break;
  484. } else {
  485. instance->protocol_info->file = subbrute_protocol_file_protocol_name(temp_str);
  486. protocol_file = subbrute_protocol_file(instance->protocol_info->file);
  487. #ifdef FURI_DEBUG
  488. FURI_LOG_D(TAG, "Protocol: %s", protocol_file);
  489. #endif
  490. }
  491. instance->decoder_result =
  492. subghz_receiver_search_decoder_base_by_name(instance->receiver, protocol_file);
  493. if(!instance->decoder_result || strcmp(protocol_file, "RAW") == 0) {
  494. FURI_LOG_E(TAG, "RAW unsupported");
  495. result = SubBruteFileResultProtocolNotSupported;
  496. break;
  497. }
  498. if(instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  499. FURI_LOG_E(TAG, "Protocol is dynamic - not supported");
  500. result = SubBruteFileResultDynamicProtocolNotValid;
  501. break;
  502. }
  503. #ifdef FURI_DEBUG
  504. else {
  505. FURI_LOG_D(TAG, "Decoder: %s", instance->decoder_result->protocol->name);
  506. }
  507. #endif
  508. // Bit
  509. if(!flipper_format_read_uint32(fff_data_file, "Bit", &temp_data32, 1)) {
  510. FURI_LOG_E(TAG, "Missing or incorrect Bit");
  511. result = SubBruteFileResultMissingOrIncorrectBit;
  512. break;
  513. } else {
  514. instance->protocol_info->bits = temp_data32;
  515. #ifdef FURI_DEBUG
  516. FURI_LOG_D(TAG, "Bit: %d", instance->protocol_info->bits);
  517. #endif
  518. }
  519. // Key
  520. if(!flipper_format_read_string(fff_data_file, "Key", temp_str)) {
  521. FURI_LOG_E(TAG, "Missing or incorrect Key");
  522. result = SubBruteFileResultMissingOrIncorrectKey;
  523. break;
  524. } else {
  525. snprintf(
  526. instance->file_key, sizeof(instance->file_key), "%s", string_get_cstr(temp_str));
  527. #ifdef FURI_DEBUG
  528. FURI_LOG_D(TAG, "Key: %s", instance->file_key);
  529. #endif
  530. }
  531. // TE
  532. if(!flipper_format_read_uint32(fff_data_file, "TE", &temp_data32, 1)) {
  533. FURI_LOG_E(TAG, "Missing or incorrect TE");
  534. //result = SubBruteFileResultMissingOrIncorrectTe;
  535. //break;
  536. } else {
  537. instance->protocol_info->te = temp_data32 != 0;
  538. }
  539. // Repeat
  540. if(flipper_format_read_uint32(fff_data_file, "Repeat", &temp_data32, 1)) {
  541. #ifdef FURI_DEBUG
  542. FURI_LOG_D(TAG, "Repeat: %d", temp_data32);
  543. #endif
  544. instance->protocol_info->repeat = (uint8_t)temp_data32;
  545. } else {
  546. #ifdef FURI_DEBUG
  547. FURI_LOG_D(TAG, "Repeat: 3 (default)");
  548. #endif
  549. instance->protocol_info->repeat = 3;
  550. }
  551. result = SubBruteFileResultOk;
  552. } while(0);
  553. string_clear(temp_str);
  554. flipper_format_file_close(fff_data_file);
  555. flipper_format_free(fff_data_file);
  556. furi_record_close(RECORD_STORAGE);
  557. subghz_receiver_free(instance->receiver);
  558. instance->decoder_result = NULL;
  559. instance->receiver = NULL;
  560. if(result == SubBruteFileResultOk) {
  561. #ifdef FURI_DEBUG
  562. FURI_LOG_D(TAG, "Loaded successfully");
  563. #endif
  564. }
  565. return result;
  566. }
  567. void subbrute_device_attack_set_default_values(
  568. SubBruteDevice* instance,
  569. SubBruteAttacks default_attack) {
  570. furi_assert(instance);
  571. #ifdef FURI_DEBUG
  572. FURI_LOG_D(TAG, "subbrute_device_attack_set_default_values");
  573. #endif
  574. instance->attack = default_attack;
  575. instance->key_index = 0x00;
  576. instance->load_index = 0x00;
  577. memset(instance->file_template, 0, sizeof(instance->file_template));
  578. memset(instance->current_key, 0, sizeof(instance->current_key));
  579. if(default_attack != SubBruteAttackLoadFile) {
  580. memset(instance->file_key, 0, sizeof(instance->file_key));
  581. instance->max_value = (uint64_t)0x00;
  582. }
  583. }
  584. void subbrute_device_send_callback(SubBruteDevice* instance) {
  585. if(instance->callback != NULL) {
  586. instance->callback(instance->context, instance->state);
  587. }
  588. }
  589. const char* subbrute_device_error_get_desc(SubBruteFileResult error_id) {
  590. const char* result;
  591. switch(error_id) {
  592. case(SubBruteFileResultOk):
  593. result = "OK";
  594. break;
  595. case(SubBruteFileResultErrorOpenFile):
  596. result = "invalid name/path";
  597. break;
  598. case(SubBruteFileResultMissingOrIncorrectHeader):
  599. result = "Missing or incorrect header";
  600. break;
  601. case(SubBruteFileResultFrequencyNotAllowed):
  602. result = "Invalid frequency!";
  603. break;
  604. case(SubBruteFileResultMissingOrIncorrectFrequency):
  605. result = "Missing or incorrect Frequency";
  606. break;
  607. case(SubBruteFileResultPresetInvalid):
  608. result = "Preset FAIL";
  609. break;
  610. case(SubBruteFileResultMissingProtocol):
  611. result = "Missing Protocol";
  612. break;
  613. case(SubBruteFileResultProtocolNotSupported):
  614. result = "RAW unsupported";
  615. break;
  616. case(SubBruteFileResultDynamicProtocolNotValid):
  617. result = "Dynamic protocol unsupported";
  618. break;
  619. case(SubBruteFileResultProtocolNotFound):
  620. result = "Protocol not found";
  621. break;
  622. case(SubBruteFileResultMissingOrIncorrectBit):
  623. result = "Missing or incorrect Bit";
  624. break;
  625. case(SubBruteFileResultMissingOrIncorrectKey):
  626. result = "Missing or incorrect Key";
  627. break;
  628. case(SubBruteFileResultMissingOrIncorrectTe):
  629. result = "Missing or incorrect TE";
  630. break;
  631. case SubBruteFileResultUnknown:
  632. default:
  633. result = "Unknown error";
  634. break;
  635. }
  636. return result;
  637. }