irda-app-brute-force.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "irda-app-brute-force.hpp"
  2. #include "irda/irda-app-file-parser.hpp"
  3. #include "m-string.h"
  4. #include <file-worker-cpp.h>
  5. #include <memory>
  6. void IrdaAppBruteForce::add_record(int index, const char* name) {
  7. records[name].index = index;
  8. records[name].amount = 0;
  9. }
  10. bool IrdaAppBruteForce::calculate_messages() {
  11. bool fs_res = false;
  12. furi_assert(!file_parser);
  13. file_parser = std::make_unique<IrdaAppFileParser>();
  14. fs_res = file_parser->open_irda_file_read(universal_db_filename);
  15. if(!fs_res) {
  16. file_parser.reset(nullptr);
  17. return false;
  18. }
  19. while(1) {
  20. auto file_signal = file_parser->read_signal();
  21. if(!file_signal) break;
  22. auto element = records.find(file_signal->name);
  23. if(element != records.cend()) {
  24. ++element->second.amount;
  25. }
  26. }
  27. file_parser->close();
  28. file_parser.reset(nullptr);
  29. return true;
  30. }
  31. void IrdaAppBruteForce::stop_bruteforce() {
  32. furi_assert((current_record.size()));
  33. if(current_record.size()) {
  34. furi_assert(file_parser);
  35. current_record.clear();
  36. file_parser->close();
  37. file_parser.reset(nullptr);
  38. }
  39. }
  40. // TODO: [FL-1418] replace with timer-chained consequence of messages.
  41. bool IrdaAppBruteForce::send_next_bruteforce(void) {
  42. furi_assert(current_record.size());
  43. furi_assert(file_parser);
  44. std::unique_ptr<IrdaAppFileParser::IrdaFileSignal> file_signal;
  45. do {
  46. file_signal = file_parser->read_signal();
  47. } while(file_signal && current_record.compare(file_signal->name));
  48. if(file_signal) {
  49. file_signal->signal.transmit();
  50. }
  51. return !!file_signal;
  52. }
  53. bool IrdaAppBruteForce::start_bruteforce(int index, int& record_amount) {
  54. bool result = false;
  55. record_amount = 0;
  56. for(const auto& it : records) {
  57. if(it.second.index == index) {
  58. record_amount = it.second.amount;
  59. if(record_amount) {
  60. current_record = it.first;
  61. }
  62. break;
  63. }
  64. }
  65. if(record_amount) {
  66. file_parser = std::make_unique<IrdaAppFileParser>();
  67. result = file_parser->open_irda_file_read(universal_db_filename);
  68. if(!result) {
  69. (void)file_parser.reset(nullptr);
  70. }
  71. }
  72. return result;
  73. }