irda_app_file_parser.cpp 12 KB

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