infrared_app_brute_force.cpp 2.5 KB

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