infrared_app_brute_force.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "helpers/infrared_parser.h"
  2. #include "infrared_app_brute_force.h"
  3. #include "infrared_app_signal.h"
  4. #include <memory>
  5. #include <m-string.h>
  6. #include <furi.h>
  7. void InfraredAppBruteForce::add_record(int index, const char* name) {
  8. records[name].index = index;
  9. records[name].amount = 0;
  10. }
  11. bool InfraredAppBruteForce::calculate_messages() {
  12. bool result = false;
  13. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  14. FlipperFormat* ff = flipper_format_file_alloc(storage);
  15. result = flipper_format_file_open_existing(ff, universal_db_filename);
  16. if(result) {
  17. InfraredAppSignal signal;
  18. string_t signal_name;
  19. string_init(signal_name);
  20. while(flipper_format_read_string(ff, "name", signal_name)) {
  21. auto element = records.find(string_get_cstr(signal_name));
  22. if(element != records.cend()) {
  23. ++element->second.amount;
  24. }
  25. }
  26. string_clear(signal_name);
  27. }
  28. flipper_format_free(ff);
  29. furi_record_close("storage");
  30. return result;
  31. }
  32. void InfraredAppBruteForce::stop_bruteforce() {
  33. furi_assert((current_record.size()));
  34. if(current_record.size()) {
  35. furi_assert(ff);
  36. current_record.clear();
  37. flipper_format_free(ff);
  38. furi_record_close("storage");
  39. }
  40. }
  41. bool InfraredAppBruteForce::send_next_bruteforce(void) {
  42. furi_assert(current_record.size());
  43. furi_assert(ff);
  44. InfraredAppSignal signal;
  45. std::string signal_name;
  46. bool result = false;
  47. do {
  48. result = infrared_parser_read_signal(ff, signal, signal_name);
  49. } while(result && current_record.compare(signal_name));
  50. if(result) {
  51. signal.transmit();
  52. }
  53. return result;
  54. }
  55. bool InfraredAppBruteForce::start_bruteforce(int index, int& record_amount) {
  56. bool result = false;
  57. record_amount = 0;
  58. for(const auto& it : records) {
  59. if(it.second.index == index) {
  60. record_amount = it.second.amount;
  61. if(record_amount) {
  62. current_record = it.first;
  63. }
  64. break;
  65. }
  66. }
  67. if(record_amount) {
  68. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  69. ff = flipper_format_file_alloc(storage);
  70. result = flipper_format_file_open_existing(ff, universal_db_filename);
  71. if(!result) {
  72. flipper_format_free(ff);
  73. furi_record_close("storage");
  74. }
  75. }
  76. return result;
  77. }