irda-app-brute-force.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "irda-app-brute-force.h"
  2. #include "irda/irda-app-file-parser.h"
  3. #include <memory>
  4. #include <m-string.h>
  5. #include <furi.h>
  6. #include <file-worker-cpp.h>
  7. void IrdaAppBruteForce::add_record(int index, const char* name) {
  8. records[name].index = index;
  9. records[name].amount = 0;
  10. }
  11. bool IrdaAppBruteForce::calculate_messages() {
  12. bool fs_res = false;
  13. furi_assert(!file_parser);
  14. file_parser = std::make_unique<IrdaAppFileParser>();
  15. fs_res = file_parser->open_irda_file_read(universal_db_filename);
  16. if(!fs_res) {
  17. file_parser.reset();
  18. return false;
  19. }
  20. while(1) {
  21. auto file_signal = file_parser->read_signal();
  22. if(!file_signal) break;
  23. auto element = records.find(file_signal->name);
  24. if(element != records.cend()) {
  25. ++element->second.amount;
  26. }
  27. }
  28. file_parser->close();
  29. file_parser.reset();
  30. return true;
  31. }
  32. void IrdaAppBruteForce::stop_bruteforce() {
  33. furi_assert((current_record.size()));
  34. if(current_record.size()) {
  35. furi_assert(file_parser);
  36. current_record.clear();
  37. file_parser->close();
  38. file_parser.reset();
  39. }
  40. }
  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. file_parser.reset();
  70. }
  71. }
  72. return result;
  73. }