subbrute_device.c 27 KB

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