infrared_app_remote_manager.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <flipper_format/flipper_format.h>
  2. #include "infrared_app_remote_manager.h"
  3. #include "infrared/helpers/infrared_parser.h"
  4. #include "infrared/infrared_app_signal.h"
  5. #include <utility>
  6. #include <infrared.h>
  7. #include <cstdio>
  8. #include <furi.h>
  9. #include <gui/modules/button_menu.h>
  10. #include <storage/storage.h>
  11. #include "infrared_app.h"
  12. static const char* default_remote_name = "remote";
  13. std::string InfraredAppRemoteManager::make_full_name(
  14. const std::string& path,
  15. const std::string& remote_name) const {
  16. return std::string("") + path + "/" + remote_name + InfraredApp::infrared_extension;
  17. }
  18. std::string InfraredAppRemoteManager::find_vacant_remote_name(const std::string& name) {
  19. std::string result_name;
  20. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  21. FS_Error error = storage_common_stat(
  22. storage, make_full_name(InfraredApp::infrared_directory, name).c_str(), NULL);
  23. if(error == FSE_NOT_EXIST) {
  24. result_name = name;
  25. } else if(error != FSE_OK) {
  26. result_name = std::string();
  27. } else {
  28. /* if suggested name is occupied, try another one (name2, name3, etc) */
  29. uint32_t i = 1;
  30. std::string new_name;
  31. do {
  32. new_name = make_full_name(InfraredApp::infrared_directory, name + std::to_string(++i));
  33. error = storage_common_stat(storage, new_name.c_str(), NULL);
  34. } while(error == FSE_OK);
  35. if(error == FSE_NOT_EXIST) {
  36. result_name = name + std::to_string(i);
  37. } else {
  38. result_name = std::string();
  39. }
  40. }
  41. furi_record_close("storage");
  42. return result_name;
  43. }
  44. bool InfraredAppRemoteManager::add_button(const char* button_name, const InfraredAppSignal& signal) {
  45. remote->buttons.emplace_back(button_name, signal);
  46. return store();
  47. }
  48. bool InfraredAppRemoteManager::add_remote_with_button(
  49. const char* button_name,
  50. const InfraredAppSignal& signal) {
  51. furi_check(button_name != nullptr);
  52. auto new_name = find_vacant_remote_name(default_remote_name);
  53. if(new_name.empty()) {
  54. return false;
  55. }
  56. remote = std::make_unique<InfraredAppRemote>(InfraredApp::infrared_directory, new_name);
  57. return add_button(button_name, signal);
  58. }
  59. std::vector<std::string> InfraredAppRemoteManager::get_button_list(void) const {
  60. std::vector<std::string> name_vector;
  61. name_vector.reserve(remote->buttons.size());
  62. for(const auto& it : remote->buttons) {
  63. name_vector.emplace_back(it.name);
  64. }
  65. // copy elision
  66. return name_vector;
  67. }
  68. const InfraredAppSignal& InfraredAppRemoteManager::get_button_data(size_t index) const {
  69. furi_check(remote.get() != nullptr);
  70. auto& buttons = remote->buttons;
  71. furi_check(index < buttons.size());
  72. return buttons.at(index).signal;
  73. }
  74. bool InfraredAppRemoteManager::delete_remote() {
  75. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  76. FS_Error error =
  77. storage_common_remove(storage, make_full_name(remote->path, remote->name).c_str());
  78. reset_remote();
  79. furi_record_close("storage");
  80. return (error == FSE_OK || error == FSE_NOT_EXIST);
  81. }
  82. void InfraredAppRemoteManager::reset_remote() {
  83. remote.reset();
  84. }
  85. bool InfraredAppRemoteManager::delete_button(uint32_t index) {
  86. furi_check(remote.get() != nullptr);
  87. auto& buttons = remote->buttons;
  88. furi_check(index < buttons.size());
  89. buttons.erase(buttons.begin() + index);
  90. return store();
  91. }
  92. std::string InfraredAppRemoteManager::get_button_name(uint32_t index) {
  93. furi_check(remote.get() != nullptr);
  94. auto& buttons = remote->buttons;
  95. furi_check(index < buttons.size());
  96. return buttons[index].name.c_str();
  97. }
  98. std::string InfraredAppRemoteManager::get_remote_name() {
  99. return remote.get() ? remote->name : std::string();
  100. }
  101. bool InfraredAppRemoteManager::rename_remote(const char* str) {
  102. furi_check(str != nullptr);
  103. furi_check(remote.get() != nullptr);
  104. if(!remote->name.compare(str)) {
  105. return true;
  106. }
  107. auto new_name = find_vacant_remote_name(str);
  108. if(new_name.empty()) {
  109. return false;
  110. }
  111. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  112. std::string old_filename = make_full_name(remote->path, remote->name);
  113. std::string new_filename = make_full_name(remote->path, new_name);
  114. FS_Error error = storage_common_rename(storage, old_filename.c_str(), new_filename.c_str());
  115. remote->name = new_name;
  116. furi_record_close("storage");
  117. return (error == FSE_OK || error == FSE_EXIST);
  118. }
  119. bool InfraredAppRemoteManager::rename_button(uint32_t index, const char* str) {
  120. furi_check(remote.get() != nullptr);
  121. auto& buttons = remote->buttons;
  122. furi_check(index < buttons.size());
  123. buttons[index].name = str;
  124. return store();
  125. }
  126. size_t InfraredAppRemoteManager::get_number_of_buttons() {
  127. furi_check(remote.get() != nullptr);
  128. return remote->buttons.size();
  129. }
  130. bool InfraredAppRemoteManager::store(void) {
  131. bool result = false;
  132. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  133. if(!storage_simply_mkdir(storage, InfraredApp::infrared_directory)) return false;
  134. FlipperFormat* ff = flipper_format_file_alloc(storage);
  135. FURI_LOG_I(
  136. "RemoteManager", "store file: \'%s\'", make_full_name(remote->path, remote->name).c_str());
  137. result =
  138. flipper_format_file_open_always(ff, make_full_name(remote->path, remote->name).c_str());
  139. if(result) {
  140. result = flipper_format_write_header_cstr(ff, "IR signals file", 1);
  141. }
  142. if(result) {
  143. for(const auto& button : remote->buttons) {
  144. result = infrared_parser_save_signal(ff, button.signal, button.name.c_str());
  145. if(!result) {
  146. break;
  147. }
  148. }
  149. }
  150. flipper_format_free(ff);
  151. furi_record_close("storage");
  152. return result;
  153. }
  154. bool InfraredAppRemoteManager::load(const std::string& path, const std::string& remote_name) {
  155. bool result = false;
  156. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  157. FlipperFormat* ff = flipper_format_file_alloc(storage);
  158. FURI_LOG_I("RemoteManager", "load file: \'%s\'", make_full_name(path, remote_name).c_str());
  159. result = flipper_format_file_open_existing(ff, make_full_name(path, remote_name).c_str());
  160. if(result) {
  161. string_t header;
  162. string_init(header);
  163. uint32_t version;
  164. result = flipper_format_read_header(ff, header, &version);
  165. if(result) {
  166. result = !string_cmp_str(header, "IR signals file") && (version == 1);
  167. }
  168. string_clear(header);
  169. }
  170. if(result) {
  171. remote = std::make_unique<InfraredAppRemote>(path, remote_name);
  172. InfraredAppSignal signal;
  173. std::string signal_name;
  174. while(infrared_parser_read_signal(ff, signal, signal_name)) {
  175. remote->buttons.emplace_back(signal_name.c_str(), std::move(signal));
  176. }
  177. }
  178. flipper_format_free(ff);
  179. furi_record_close("storage");
  180. return result;
  181. }