irda-app-remote-manager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "sys/_stdint.h"
  8. #include <cstdio>
  9. #include <string>
  10. #include <utility>
  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. IrdaAppRemoteManager::IrdaAppRemoteManager() {
  33. sd_ex_api = static_cast<SdCard_Api*>(furi_record_open("sdcard-ex"));
  34. fs_api = static_cast<FS_Api*>(furi_record_open("sdcard"));
  35. }
  36. IrdaAppRemoteManager::~IrdaAppRemoteManager() {
  37. furi_record_close("sdcard");
  38. furi_record_close("sdcard-ex");
  39. }
  40. bool IrdaAppRemoteManager::add_button(const char* button_name, const IrdaMessage* message) {
  41. remote->buttons.emplace_back(button_name, message);
  42. return store();
  43. }
  44. bool IrdaAppRemoteManager::add_remote_with_button(
  45. const char* button_name,
  46. const IrdaMessage* message) {
  47. furi_check(button_name != nullptr);
  48. furi_check(message != nullptr);
  49. std::vector<std::string> remote_list;
  50. bool result = get_remote_list(remote_list);
  51. if(!result) return false;
  52. auto new_name = find_vacant_name(remote_list, default_remote_name);
  53. remote = std::make_unique<IrdaAppRemote>(new_name);
  54. return add_button(button_name, message);
  55. }
  56. IrdaAppRemote::IrdaAppRemote(const std::string& name)
  57. : name(name) {
  58. }
  59. std::vector<std::string> IrdaAppRemoteManager::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 IrdaMessage* IrdaAppRemoteManager::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).message;
  73. }
  74. std::string IrdaAppRemoteManager::make_filename(const std::string& name) const {
  75. return std::string("/") + irda_directory + "/" + name + irda_extension;
  76. }
  77. bool IrdaAppRemoteManager::delete_remote() {
  78. FS_Error fs_res;
  79. fs_res = fs_api->common.remove(make_filename(remote->name).c_str());
  80. if(fs_res != FSE_OK) {
  81. show_file_error_message("Error deleting file");
  82. return false;
  83. }
  84. remote.reset();
  85. return true;
  86. }
  87. bool IrdaAppRemoteManager::delete_button(uint32_t index) {
  88. furi_check(remote.get() != nullptr);
  89. auto& buttons = remote->buttons;
  90. furi_check(index < buttons.size());
  91. buttons.erase(buttons.begin() + index);
  92. return store();
  93. }
  94. std::string IrdaAppRemoteManager::get_button_name(uint32_t index) {
  95. furi_check(remote.get() != nullptr);
  96. auto& buttons = remote->buttons;
  97. furi_check(index < buttons.size());
  98. return buttons[index].name;
  99. }
  100. std::string IrdaAppRemoteManager::get_remote_name() {
  101. furi_check(remote.get() != nullptr);
  102. return remote->name;
  103. }
  104. int IrdaAppRemoteManager::find_remote_name(const std::vector<std::string>& strings) {
  105. int i = 0;
  106. for(const auto& str : strings) {
  107. if(!str.compare(remote->name)) {
  108. return i;
  109. }
  110. ++i;
  111. }
  112. return -1;
  113. }
  114. bool IrdaAppRemoteManager::rename_remote(const char* str) {
  115. furi_check(str != nullptr);
  116. furi_check(remote.get() != nullptr);
  117. if(!remote->name.compare(str)) return true;
  118. std::vector<std::string> remote_list;
  119. bool result = get_remote_list(remote_list);
  120. if(!result) return false;
  121. auto new_name = find_vacant_name(remote_list, str);
  122. FS_Error fs_err = fs_api->common.rename(
  123. make_filename(remote->name).c_str(), make_filename(new_name).c_str());
  124. remote->name = new_name;
  125. if(fs_err != FSE_OK) {
  126. show_file_error_message("Error renaming\nremote file");
  127. }
  128. return fs_err == FSE_OK;
  129. }
  130. bool IrdaAppRemoteManager::rename_button(uint32_t index, const char* str) {
  131. furi_check(remote.get() != nullptr);
  132. auto& buttons = remote->buttons;
  133. furi_check(index < buttons.size());
  134. buttons[index].name = str;
  135. return store();
  136. }
  137. size_t IrdaAppRemoteManager::get_number_of_buttons() {
  138. furi_check(remote.get() != nullptr);
  139. return remote->buttons.size();
  140. }
  141. void IrdaAppRemoteManager::show_file_error_message(const char* error_text) const {
  142. sd_ex_api->show_error(sd_ex_api->context, error_text);
  143. }
  144. bool IrdaAppRemoteManager::store(void) {
  145. File file;
  146. uint16_t write_count;
  147. std::string dirname(std::string("/") + irda_directory);
  148. FS_Error fs_err = fs_api->common.mkdir(dirname.c_str());
  149. if((fs_err != FSE_OK) && (fs_err != FSE_EXIST)) {
  150. show_file_error_message("Can't create directory");
  151. return false;
  152. }
  153. std::string filename = dirname + "/" + remote->name + irda_extension;
  154. bool res = fs_api->file.open(&file, filename.c_str(), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  155. if(!res) {
  156. show_file_error_message("Cannot create\nnew remote file");
  157. return false;
  158. }
  159. char content[128];
  160. for(const auto& button : remote->buttons) {
  161. auto protocol = button.message.protocol;
  162. sniprintf(
  163. content,
  164. sizeof(content),
  165. "%.31s %.31s A:%0*lX C:%0*lX\n",
  166. button.name.c_str(),
  167. irda_get_protocol_name(protocol),
  168. irda_get_protocol_address_length(protocol),
  169. button.message.address,
  170. irda_get_protocol_command_length(protocol),
  171. button.message.command);
  172. auto content_len = strlen(content);
  173. write_count = fs_api->file.write(&file, content, content_len);
  174. if(file.error_id != FSE_OK || write_count != content_len) {
  175. show_file_error_message("Cannot write\nto key file");
  176. fs_api->file.close(&file);
  177. return false;
  178. }
  179. }
  180. fs_api->file.close(&file);
  181. sd_ex_api->check_error(sd_ex_api->context);
  182. return true;
  183. }
  184. bool IrdaAppRemoteManager::parse_button(std::string& str) {
  185. char button_name[32];
  186. char protocol_name[32];
  187. uint32_t address;
  188. uint32_t command;
  189. int parsed = std::sscanf(
  190. str.c_str(), "%31s %31s A:%lX C:%lX", button_name, protocol_name, &address, &command);
  191. if(parsed != 4) {
  192. return false;
  193. }
  194. IrdaProtocol protocol = irda_get_protocol_by_name(protocol_name);
  195. if(!irda_is_protocol_valid((IrdaProtocol)protocol)) {
  196. return false;
  197. }
  198. int address_length = irda_get_protocol_address_length(protocol);
  199. uint32_t address_mask = (1LU << (4 * address_length)) - 1;
  200. if(address != (address & address_mask)) {
  201. return false;
  202. }
  203. int command_length = irda_get_protocol_command_length(protocol);
  204. uint32_t command_mask = (1LU << (4 * command_length)) - 1;
  205. if(command != (command & command_mask)) {
  206. return false;
  207. }
  208. IrdaMessage irda_message = {
  209. .protocol = protocol,
  210. .address = address,
  211. .command = command,
  212. .repeat = false,
  213. };
  214. remote->buttons.emplace_back(button_name, &irda_message);
  215. return true;
  216. }
  217. std::string getline(
  218. const FS_Api* fs_api,
  219. File& file,
  220. char file_buf[],
  221. size_t file_buf_size,
  222. size_t& file_buf_cnt) {
  223. std::string str;
  224. size_t newline_index = 0;
  225. bool found_eol = false;
  226. while(1) {
  227. if(file_buf_cnt > 0) {
  228. size_t end_index = 0;
  229. char* endline_ptr = (char*)memchr(file_buf, '\n', file_buf_cnt);
  230. newline_index = endline_ptr - file_buf;
  231. if(endline_ptr == 0) {
  232. end_index = file_buf_cnt;
  233. } else if(newline_index < file_buf_cnt) {
  234. end_index = newline_index + 1;
  235. found_eol = true;
  236. } else {
  237. furi_assert(0);
  238. }
  239. str.append(file_buf, end_index);
  240. memmove(file_buf, &file_buf[end_index], file_buf_cnt - end_index);
  241. file_buf_cnt = file_buf_cnt - end_index;
  242. if(found_eol) break;
  243. }
  244. file_buf_cnt +=
  245. fs_api->file.read(&file, &file_buf[file_buf_cnt], file_buf_size - file_buf_cnt);
  246. if(file_buf_cnt == 0) {
  247. break; // end of reading
  248. }
  249. }
  250. return str;
  251. }
  252. bool IrdaAppRemoteManager::get_remote_list(std::vector<std::string>& remote_names) const {
  253. bool fs_res = false;
  254. char name[128];
  255. File dir;
  256. std::string dirname(std::string("/") + irda_directory);
  257. remote_names.clear();
  258. fs_res = fs_api->dir.open(&dir, dirname.c_str());
  259. if(!fs_res) {
  260. if(!check_fs()) {
  261. show_file_error_message("Cannot open\napplication directory");
  262. return false;
  263. } else {
  264. return true; // SD ok, but no files written yet
  265. }
  266. }
  267. while(fs_api->dir.read(&dir, nullptr, name, sizeof(name)) && strlen(name)) {
  268. std::string filename(name);
  269. auto extension_index = filename.rfind(irda_extension);
  270. if((extension_index == std::string::npos) ||
  271. (extension_index + strlen(irda_extension) != filename.size())) {
  272. continue;
  273. }
  274. remote_names.push_back(filename.erase(extension_index));
  275. }
  276. fs_api->dir.close(&dir);
  277. return true;
  278. }
  279. bool IrdaAppRemoteManager::load(const std::string& name) {
  280. bool fs_res = false;
  281. File file;
  282. fs_res = fs_api->file.open(&file, make_filename(name).c_str(), FSAM_READ, FSOM_OPEN_EXISTING);
  283. if(!fs_res) {
  284. show_file_error_message("Error opening file");
  285. return false;
  286. }
  287. remote = std::make_unique<IrdaAppRemote>(name);
  288. while(1) {
  289. auto str = getline(fs_api, file, file_buf, sizeof(file_buf), file_buf_cnt);
  290. if(str.empty()) break;
  291. parse_button(str);
  292. }
  293. fs_api->file.close(&file);
  294. return true;
  295. }
  296. bool IrdaAppRemoteManager::check_fs() const {
  297. // TODO: [FL-1431] Add return value to sd_ex_api->check_error() and replace get_fs_info().
  298. auto fs_err = fs_api->common.get_fs_info(nullptr, nullptr);
  299. if(fs_err != FSE_OK) show_file_error_message("SD card not found");
  300. return fs_err == FSE_OK;
  301. }