irda-app-remote-manager.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "irda-app-remote-manager.hpp"
  2. #include "filesystem-api.h"
  3. #include "furi.h"
  4. #include "furi/check.h"
  5. #include "gui/modules/button_menu.h"
  6. #include "irda.h"
  7. #include <cstdio>
  8. #include <string>
  9. #include <utility>
  10. #include "irda-app-file-parser.hpp"
  11. const char* IrdaAppRemoteManager::irda_directory = "irda";
  12. const char* IrdaAppRemoteManager::irda_extension = ".ir";
  13. static const std::string default_remote_name = "remote";
  14. static bool find_string(const std::vector<std::string>& strings, const std::string& match_string) {
  15. for(const auto& string : strings) {
  16. if(!string.compare(match_string)) return true;
  17. }
  18. return false;
  19. }
  20. static std::string
  21. find_vacant_name(const std::vector<std::string>& strings, const std::string& name) {
  22. // if suggested name is occupied, try another one (name2, name3, etc)
  23. if(find_string(strings, name)) {
  24. int i = 1;
  25. while(find_string(strings, name + std::to_string(++i)))
  26. ;
  27. return name + std::to_string(i);
  28. } else {
  29. return name;
  30. }
  31. }
  32. bool IrdaAppRemoteManager::add_button(const char* button_name, const IrdaMessage* message) {
  33. remote->buttons.emplace_back(button_name, message);
  34. return store();
  35. }
  36. bool IrdaAppRemoteManager::add_remote_with_button(
  37. const char* button_name,
  38. const IrdaMessage* message) {
  39. furi_check(button_name != nullptr);
  40. furi_check(message != nullptr);
  41. std::vector<std::string> remote_list;
  42. bool result = get_remote_list(remote_list);
  43. if(!result) return false;
  44. auto new_name = find_vacant_name(remote_list, default_remote_name);
  45. remote = std::make_unique<IrdaAppRemote>(new_name);
  46. return add_button(button_name, message);
  47. }
  48. IrdaAppRemote::IrdaAppRemote(const std::string& name)
  49. : name(name) {
  50. }
  51. std::vector<std::string> IrdaAppRemoteManager::get_button_list(void) const {
  52. std::vector<std::string> name_vector;
  53. name_vector.reserve(remote->buttons.size());
  54. for(const auto& it : remote->buttons) {
  55. name_vector.emplace_back(it.name);
  56. }
  57. // copy elision
  58. return name_vector;
  59. }
  60. const IrdaMessage* IrdaAppRemoteManager::get_button_data(size_t index) const {
  61. furi_check(remote.get() != nullptr);
  62. auto& buttons = remote->buttons;
  63. furi_check(index < buttons.size());
  64. return &buttons.at(index).message;
  65. }
  66. std::string IrdaAppRemoteManager::make_filename(const std::string& name) const {
  67. return std::string("/") + irda_directory + "/" + name + irda_extension;
  68. }
  69. bool IrdaAppRemoteManager::delete_remote() {
  70. FS_Error fs_res;
  71. IrdaAppFileParser file_parser;
  72. fs_res = file_parser.get_fs_api().common.remove(make_filename(remote->name).c_str());
  73. if(fs_res != FSE_OK) {
  74. file_parser.get_sd_api().show_error(
  75. file_parser.get_sd_api().context, "Error deleting file");
  76. return false;
  77. }
  78. remote.reset();
  79. return true;
  80. }
  81. bool IrdaAppRemoteManager::delete_button(uint32_t index) {
  82. furi_check(remote.get() != nullptr);
  83. auto& buttons = remote->buttons;
  84. furi_check(index < buttons.size());
  85. buttons.erase(buttons.begin() + index);
  86. return store();
  87. }
  88. std::string IrdaAppRemoteManager::get_button_name(uint32_t index) {
  89. furi_check(remote.get() != nullptr);
  90. auto& buttons = remote->buttons;
  91. furi_check(index < buttons.size());
  92. return buttons[index].name;
  93. }
  94. std::string IrdaAppRemoteManager::get_remote_name() {
  95. furi_check(remote.get() != nullptr);
  96. return remote->name;
  97. }
  98. int IrdaAppRemoteManager::find_remote_name(const std::vector<std::string>& strings) {
  99. int i = 0;
  100. for(const auto& str : strings) {
  101. if(!str.compare(remote->name)) {
  102. return i;
  103. }
  104. ++i;
  105. }
  106. return -1;
  107. }
  108. bool IrdaAppRemoteManager::rename_remote(const char* str) {
  109. furi_check(str != nullptr);
  110. furi_check(remote.get() != nullptr);
  111. if(!remote->name.compare(str)) return true;
  112. std::vector<std::string> remote_list;
  113. bool result = get_remote_list(remote_list);
  114. if(!result) return false;
  115. auto new_name = find_vacant_name(remote_list, str);
  116. IrdaAppFileParser file_parser;
  117. FS_Error fs_err = file_parser.get_fs_api().common.rename(
  118. make_filename(remote->name).c_str(), make_filename(new_name).c_str());
  119. remote->name = new_name;
  120. if(fs_err != FSE_OK) {
  121. file_parser.get_sd_api().show_error(
  122. file_parser.get_sd_api().context, "Error renaming\nremote file");
  123. }
  124. return fs_err == FSE_OK;
  125. }
  126. bool IrdaAppRemoteManager::rename_button(uint32_t index, const char* str) {
  127. furi_check(remote.get() != nullptr);
  128. auto& buttons = remote->buttons;
  129. furi_check(index < buttons.size());
  130. buttons[index].name = str;
  131. return store();
  132. }
  133. size_t IrdaAppRemoteManager::get_number_of_buttons() {
  134. furi_check(remote.get() != nullptr);
  135. return remote->buttons.size();
  136. }
  137. bool IrdaAppRemoteManager::store(void) {
  138. File file;
  139. uint16_t write_count;
  140. std::string dirname(std::string("/") + irda_directory);
  141. IrdaAppFileParser file_parser;
  142. FS_Error fs_err = file_parser.get_fs_api().common.mkdir(dirname.c_str());
  143. if((fs_err != FSE_OK) && (fs_err != FSE_EXIST)) {
  144. file_parser.get_sd_api().show_error(
  145. file_parser.get_sd_api().context, "Can't create directory");
  146. return false;
  147. }
  148. bool res = file_parser.get_fs_api().file.open(
  149. &file, make_filename(remote->name).c_str(), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  150. if(!res) {
  151. file_parser.get_sd_api().show_error(
  152. file_parser.get_sd_api().context, "Cannot create\nnew remote file");
  153. return false;
  154. }
  155. char content[128];
  156. for(const auto& button : remote->buttons) {
  157. auto protocol = button.message.protocol;
  158. sniprintf(
  159. content,
  160. sizeof(content),
  161. "%.31s %.31s A:%0*lX C:%0*lX\n",
  162. button.name.c_str(),
  163. irda_get_protocol_name(protocol),
  164. irda_get_protocol_address_length(protocol),
  165. button.message.address,
  166. irda_get_protocol_command_length(protocol),
  167. button.message.command);
  168. auto content_len = strlen(content);
  169. write_count = file_parser.get_fs_api().file.write(&file, content, content_len);
  170. if(file.error_id != FSE_OK || write_count != content_len) {
  171. file_parser.get_sd_api().show_error(
  172. file_parser.get_sd_api().context, "Cannot write\nto key file");
  173. file_parser.get_fs_api().file.close(&file);
  174. return false;
  175. }
  176. }
  177. file_parser.get_fs_api().file.close(&file);
  178. file_parser.get_sd_api().check_error(file_parser.get_sd_api().context);
  179. return true;
  180. }
  181. bool IrdaAppRemoteManager::get_remote_list(std::vector<std::string>& remote_names) const {
  182. bool fs_res = false;
  183. char name[128];
  184. File dir;
  185. std::string dirname(std::string("/") + irda_directory);
  186. remote_names.clear();
  187. IrdaAppFileParser file_parser;
  188. fs_res = file_parser.get_fs_api().dir.open(&dir, dirname.c_str());
  189. if(!fs_res) {
  190. if(!check_fs()) {
  191. file_parser.get_sd_api().show_error(
  192. file_parser.get_sd_api().context, "Cannot open\napplication directory");
  193. return false;
  194. } else {
  195. return true; // SD ok, but no files written yet
  196. }
  197. }
  198. while(file_parser.get_fs_api().dir.read(&dir, nullptr, name, sizeof(name)) && strlen(name)) {
  199. std::string filename(name);
  200. auto extension_index = filename.rfind(irda_extension);
  201. if((extension_index == std::string::npos) ||
  202. (extension_index + strlen(irda_extension) != filename.size())) {
  203. continue;
  204. }
  205. remote_names.push_back(filename.erase(extension_index));
  206. }
  207. file_parser.get_fs_api().dir.close(&dir);
  208. return true;
  209. }
  210. bool IrdaAppRemoteManager::load(const std::string& name) {
  211. bool fs_res = false;
  212. IrdaAppFileParser file_parser;
  213. File file;
  214. fs_res = file_parser.get_fs_api().file.open(
  215. &file, make_filename(name).c_str(), FSAM_READ, FSOM_OPEN_EXISTING);
  216. if(!fs_res) {
  217. file_parser.get_sd_api().show_error(
  218. file_parser.get_sd_api().context, "Error opening file");
  219. return false;
  220. }
  221. remote = std::make_unique<IrdaAppRemote>(name);
  222. while(1) {
  223. auto message = file_parser.read_message(&file);
  224. if(!message) break;
  225. remote->buttons.emplace_back(message->name, &message->message);
  226. }
  227. file_parser.get_fs_api().file.close(&file);
  228. return true;
  229. }
  230. bool IrdaAppRemoteManager::check_fs() const {
  231. // TODO: [FL-1431] Add return value to file_parser.get_sd_api().check_error() and replace get_fs_info().
  232. IrdaAppFileParser file_parser;
  233. auto fs_err = file_parser.get_fs_api().common.get_fs_info(nullptr, nullptr);
  234. if(fs_err != FSE_OK)
  235. file_parser.get_sd_api().show_error(file_parser.get_sd_api().context, "SD card not found");
  236. return fs_err == FSE_OK;
  237. }