irda-app-file-parser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "irda-app-file-parser.h"
  2. #include "irda-app-remote-manager.h"
  3. #include "irda-app-signal.h"
  4. #include <m-string.h>
  5. #include <cstdio>
  6. #include <text-store.h>
  7. #include <irda.h>
  8. #include <string_view>
  9. #include <furi.h>
  10. #include <furi-hal-irda.h>
  11. #include <file-worker-cpp.h>
  12. bool IrdaAppFileParser::open_irda_file_read(const char* name) {
  13. std::string full_filename;
  14. if(name[0] != '/')
  15. full_filename = make_full_name(name);
  16. else
  17. full_filename = name;
  18. return file_worker.open(full_filename.c_str(), FSAM_READ, FSOM_OPEN_EXISTING);
  19. }
  20. bool IrdaAppFileParser::open_irda_file_write(const char* name) {
  21. std::string dirname(irda_directory);
  22. auto full_filename = make_full_name(name);
  23. if(!file_worker.mkdir(dirname.c_str())) return false;
  24. return file_worker.open(full_filename.c_str(), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  25. }
  26. size_t IrdaAppFileParser::stringify_message(
  27. const IrdaAppSignal& signal,
  28. const char* name,
  29. char* buf,
  30. size_t buf_size) {
  31. auto message = signal.get_message();
  32. auto protocol = message.protocol;
  33. size_t written = 0;
  34. written += sniprintf(
  35. buf,
  36. buf_size,
  37. "%.31s %.31s A:%0*lX C:%0*lX\n",
  38. name,
  39. irda_get_protocol_name(protocol),
  40. ROUND_UP_TO(irda_get_protocol_address_length(protocol), 4),
  41. message.address,
  42. ROUND_UP_TO(irda_get_protocol_command_length(protocol), 4),
  43. message.command);
  44. furi_assert(written < buf_size);
  45. if(written >= buf_size) {
  46. written = 0;
  47. }
  48. return written;
  49. }
  50. size_t IrdaAppFileParser::stringify_raw_signal(
  51. const IrdaAppSignal& signal,
  52. const char* name,
  53. char* buf,
  54. size_t buf_size) {
  55. size_t written = 0;
  56. int duty_cycle = 100 * IRDA_COMMON_DUTY_CYCLE;
  57. written += sniprintf(
  58. &buf[written],
  59. max_line_length - written,
  60. "%.31s RAW F:%d DC:%d",
  61. name,
  62. IRDA_COMMON_CARRIER_FREQUENCY,
  63. duty_cycle);
  64. auto& raw_signal = signal.get_raw_signal();
  65. for(size_t i = 0; i < raw_signal.timings_cnt; ++i) {
  66. written += sniprintf(&buf[written], buf_size - written, " %ld", raw_signal.timings[i]);
  67. if(written > buf_size) {
  68. return false;
  69. }
  70. }
  71. written += snprintf(&buf[written], buf_size - written, "\n");
  72. furi_assert(written < buf_size);
  73. if(written >= buf_size) {
  74. written = 0;
  75. }
  76. return written;
  77. }
  78. bool IrdaAppFileParser::save_signal(const IrdaAppSignal& signal, const char* name) {
  79. char* buf = new char[max_line_length];
  80. size_t buf_cnt = 0;
  81. bool write_result = false;
  82. if(signal.is_raw()) {
  83. buf_cnt = stringify_raw_signal(signal, name, buf, max_line_length);
  84. } else {
  85. buf_cnt = stringify_message(signal, name, buf, max_line_length);
  86. }
  87. if(buf_cnt) {
  88. write_result = file_worker.write(buf, buf_cnt);
  89. }
  90. delete[] buf;
  91. return write_result;
  92. }
  93. std::unique_ptr<IrdaAppFileParser::IrdaFileSignal> IrdaAppFileParser::read_signal(void) {
  94. string_t line;
  95. string_init(line);
  96. string_reserve(line, max_line_length);
  97. std::unique_ptr<IrdaAppFileParser::IrdaFileSignal> file_signal;
  98. while(!file_signal &&
  99. file_worker.read_until_buffered(line, file_buf, &file_buf_cnt, sizeof(file_buf))) {
  100. if(string_empty_p(line)) {
  101. continue;
  102. }
  103. auto c_str = string_get_cstr(line);
  104. file_signal = parse_signal(c_str);
  105. if(!file_signal) {
  106. file_signal = parse_signal_raw(c_str);
  107. }
  108. }
  109. string_clear(line);
  110. return file_signal;
  111. }
  112. std::unique_ptr<IrdaAppFileParser::IrdaFileSignal>
  113. IrdaAppFileParser::parse_signal(const std::string& str) const {
  114. char protocol_name[32];
  115. uint32_t address;
  116. uint32_t command;
  117. auto irda_file_signal = std::make_unique<IrdaFileSignal>();
  118. int parsed = std::sscanf(
  119. str.c_str(),
  120. "%31s %31s A:%lX C:%lX",
  121. irda_file_signal->name,
  122. protocol_name,
  123. &address,
  124. &command);
  125. if(parsed != 4) {
  126. return nullptr;
  127. }
  128. IrdaProtocol protocol = irda_get_protocol_by_name(protocol_name);
  129. if(!irda_is_protocol_valid((IrdaProtocol)protocol)) {
  130. size_t end_of_str = MIN(str.find_last_not_of(" \t\r\n") + 1, (size_t)30);
  131. FURI_LOG_E(
  132. "IrdaFileParser",
  133. "Unknown protocol(\'%.*s...\'): \'%s\'",
  134. end_of_str,
  135. str.c_str(),
  136. protocol_name);
  137. return nullptr;
  138. }
  139. uint32_t address_length = irda_get_protocol_address_length(protocol);
  140. uint32_t address_mask = (1LU << address_length) - 1;
  141. if(address != (address & address_mask)) {
  142. size_t end_of_str = MIN(str.find_last_not_of(" \t\r\n") + 1, (size_t)30);
  143. FURI_LOG_E(
  144. "IrdaFileParser",
  145. "Signal(\'%.*s...\'): address is too long (mask for this protocol is 0x%08X): 0x%X",
  146. end_of_str,
  147. str.c_str(),
  148. address_mask,
  149. address);
  150. return nullptr;
  151. }
  152. uint32_t command_length = irda_get_protocol_command_length(protocol);
  153. uint32_t command_mask = (1LU << command_length) - 1;
  154. if(command != (command & command_mask)) {
  155. size_t end_of_str = MIN(str.find_last_not_of(" \t\r\n") + 1, (size_t)30);
  156. FURI_LOG_E(
  157. "IrdaFileParser",
  158. "Signal(\'%.*s...\'): command is too long (mask for this protocol is 0x%08X): 0x%X",
  159. end_of_str,
  160. str.c_str(),
  161. command_mask,
  162. command);
  163. return nullptr;
  164. }
  165. IrdaMessage message = {
  166. .protocol = protocol,
  167. .address = address,
  168. .command = command,
  169. .repeat = false,
  170. };
  171. irda_file_signal->signal.set_message(&message);
  172. return irda_file_signal;
  173. }
  174. const char* find_first_not_of(const char* str, char symbol) {
  175. const char* str_start = nullptr;
  176. while(str != str_start) {
  177. str_start = str;
  178. str = strchr(str, symbol);
  179. }
  180. return str;
  181. }
  182. static int remove_args32(std::string_view& str, size_t num) {
  183. int removed_length = 0;
  184. while(num--) {
  185. char buf[32];
  186. size_t index = str.find_first_not_of(" \t");
  187. if(index == std::string_view::npos) break;
  188. removed_length += index;
  189. str.remove_prefix(index);
  190. if(str.empty()) break;
  191. int parsed = std::sscanf(str.data(), "%31s", buf);
  192. if(!parsed) break;
  193. size_t len = strlen(buf);
  194. if(!len) break;
  195. removed_length += len;
  196. str.remove_prefix(len);
  197. if(str.empty()) break;
  198. }
  199. return removed_length;
  200. }
  201. std::unique_ptr<IrdaAppFileParser::IrdaFileSignal>
  202. IrdaAppFileParser::parse_signal_raw(const std::string& string) const {
  203. uint32_t frequency;
  204. uint32_t duty_cycle;
  205. std::string_view str(string.c_str());
  206. auto irda_file_signal = std::make_unique<IrdaFileSignal>();
  207. int parsed = std::sscanf(
  208. str.data(), "%31s RAW F:%ld DC:%ld", irda_file_signal->name, &frequency, &duty_cycle);
  209. if(parsed != 3) {
  210. return nullptr;
  211. }
  212. if((frequency < IRDA_MIN_FREQUENCY) || (frequency > IRDA_MAX_FREQUENCY)) {
  213. size_t end_of_str = MIN(string.find_last_not_of(" \t\r\n") + 1, (size_t)30);
  214. FURI_LOG_E(
  215. "IrdaFileParser",
  216. "RAW signal(\'%.*s...\'): frequency is out of bounds (%ld-%ld): %ld",
  217. end_of_str,
  218. string.c_str(),
  219. IRDA_MIN_FREQUENCY,
  220. IRDA_MAX_FREQUENCY,
  221. frequency);
  222. return nullptr;
  223. }
  224. if((duty_cycle == 0) || (duty_cycle > 100)) {
  225. size_t end_of_str = MIN(string.find_last_not_of(" \t\r\n") + 1, (size_t)30);
  226. FURI_LOG_E(
  227. "IrdaFileParser",
  228. "RAW signal(\'%.*s...\'): duty cycle is out of bounds (0-100): %ld",
  229. end_of_str,
  230. string.c_str(),
  231. duty_cycle);
  232. return nullptr;
  233. }
  234. int header_len = remove_args32(str, 4);
  235. size_t last_valid_ch = str.find_last_not_of(" \t\r\n");
  236. if(last_valid_ch != std::string_view::npos) {
  237. str.remove_suffix(str.size() - last_valid_ch - 1);
  238. } else {
  239. FURI_LOG_E(
  240. "IrdaFileParser", "RAW signal(\'%.*s\'): no timings", header_len, string.c_str());
  241. return nullptr;
  242. }
  243. /* move allocated timings into raw signal object */
  244. IrdaAppSignal::RawSignal raw_signal = {
  245. .timings_cnt = 0, .timings = new uint32_t[max_raw_timings_in_signal]};
  246. bool result = false;
  247. while(!str.empty()) {
  248. char buf[10];
  249. size_t index = str.find_first_not_of(" \t", 1);
  250. if(index == std::string_view::npos) {
  251. break;
  252. }
  253. str.remove_prefix(index);
  254. parsed = std::sscanf(str.data(), "%9s", buf);
  255. if(parsed != 1) {
  256. FURI_LOG_E(
  257. "IrdaFileParser",
  258. "RAW signal(\'%.*s...\'): failed on timing[%ld] \'%*s\'",
  259. header_len,
  260. string.c_str(),
  261. raw_signal.timings_cnt,
  262. str.size(),
  263. str.data());
  264. result = false;
  265. break;
  266. }
  267. str.remove_prefix(strlen(buf));
  268. int value = atoi(buf);
  269. if(value <= 0) {
  270. FURI_LOG_E(
  271. "IrdaFileParser",
  272. "RAW signal(\'%.*s...\'): failed on timing[%ld] \'%s\'",
  273. header_len,
  274. string.c_str(),
  275. raw_signal.timings_cnt,
  276. buf);
  277. result = false;
  278. break;
  279. }
  280. if(raw_signal.timings_cnt >= max_raw_timings_in_signal) {
  281. FURI_LOG_E(
  282. "IrdaFileParser",
  283. "RAW signal(\'%.*s...\'): too much timings (max %ld)",
  284. header_len,
  285. string.c_str(),
  286. max_raw_timings_in_signal);
  287. result = false;
  288. break;
  289. }
  290. raw_signal.timings[raw_signal.timings_cnt] = value;
  291. ++raw_signal.timings_cnt;
  292. result = true;
  293. }
  294. if(result) {
  295. /* copy timings instead of moving them to occupy less than max_raw_timings_in_signal */
  296. irda_file_signal->signal.copy_raw_signal(raw_signal.timings, raw_signal.timings_cnt);
  297. } else {
  298. irda_file_signal.reset();
  299. }
  300. delete[] raw_signal.timings;
  301. return irda_file_signal;
  302. }
  303. bool IrdaAppFileParser::is_irda_file_exist(const char* name, bool* exist) {
  304. std::string full_path = make_full_name(name);
  305. return file_worker.is_file_exist(full_path.c_str(), exist);
  306. }
  307. std::string IrdaAppFileParser::make_full_name(const std::string& remote_name) const {
  308. return std::string("") + irda_directory + "/" + remote_name + irda_extension;
  309. }
  310. std::string IrdaAppFileParser::make_name(const std::string& full_name) const {
  311. std::string str(full_name, full_name.find_last_of('/') + 1, full_name.size());
  312. str.erase(str.find_last_of('.'));
  313. return str;
  314. }
  315. bool IrdaAppFileParser::remove_irda_file(const char* name) {
  316. std::string full_filename = make_full_name(name);
  317. return file_worker.remove(full_filename.c_str());
  318. }
  319. bool IrdaAppFileParser::rename_irda_file(const char* old_name, const char* new_name) {
  320. std::string old_filename = make_full_name(old_name);
  321. std::string new_filename = make_full_name(new_name);
  322. return file_worker.rename(old_filename.c_str(), new_filename.c_str());
  323. }
  324. bool IrdaAppFileParser::close() {
  325. return file_worker.close();
  326. }
  327. bool IrdaAppFileParser::check_errors() {
  328. return file_worker.check_errors();
  329. }
  330. std::string IrdaAppFileParser::file_select(const char* selected) {
  331. auto filename_ts = std::make_unique<TextStore>(IrdaAppRemoteManager::max_remote_name_length);
  332. bool result;
  333. result = file_worker.file_select(
  334. irda_directory, irda_extension, filename_ts->text, filename_ts->text_size, selected);
  335. return result ? std::string(filename_ts->text) : std::string();
  336. }