irda-app-brute-force.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "irda-app-brute-force.h"
  2. #include "irda/irda-app-file-parser.h"
  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. bool IrdaAppBruteForce::send_next_bruteforce(void) {
  41. furi_assert(current_record.size());
  42. furi_assert(file_parser);
  43. std::unique_ptr<IrdaAppFileParser::IrdaFileSignal> file_signal;
  44. do {
  45. file_signal = file_parser->read_signal();
  46. } while(file_signal && current_record.compare(file_signal->name));
  47. if(file_signal) {
  48. file_signal->signal.transmit();
  49. }
  50. return !!file_signal;
  51. }
  52. bool IrdaAppBruteForce::start_bruteforce(int index, int& record_amount) {
  53. bool result = false;
  54. record_amount = 0;
  55. for(const auto& it : records) {
  56. if(it.second.index == index) {
  57. record_amount = it.second.amount;
  58. if(record_amount) {
  59. current_record = it.first;
  60. }
  61. break;
  62. }
  63. }
  64. if(record_amount) {
  65. file_parser = std::make_unique<IrdaAppFileParser>();
  66. result = file_parser->open_irda_file_read(universal_db_filename);
  67. if(!result) {
  68. (void)file_parser.reset(nullptr);
  69. }
  70. }
  71. return result;
  72. }