subbrute_device.c 24 KB

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