infrared_cli.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <m-string.h>
  2. #include <cli/cli.h>
  3. #include <infrared.h>
  4. #include <infrared_worker.h>
  5. #include <furi_hal_infrared.h>
  6. #include <flipper_format.h>
  7. #include <toolbox/args.h>
  8. #include "infrared_signal.h"
  9. #define INFRARED_CLI_BUF_SIZE 10
  10. static void infrared_cli_start_ir_rx(Cli* cli, string_t args);
  11. static void infrared_cli_start_ir_tx(Cli* cli, string_t args);
  12. static void infrared_cli_process_decode(Cli* cli, string_t args);
  13. static const struct {
  14. const char* cmd;
  15. void (*process_function)(Cli* cli, string_t args);
  16. } infrared_cli_commands[] = {
  17. {.cmd = "rx", .process_function = infrared_cli_start_ir_rx},
  18. {.cmd = "tx", .process_function = infrared_cli_start_ir_tx},
  19. {.cmd = "decode", .process_function = infrared_cli_process_decode},
  20. };
  21. static void signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
  22. furi_assert(received_signal);
  23. char buf[100];
  24. size_t buf_cnt;
  25. Cli* cli = (Cli*)context;
  26. if(infrared_worker_signal_is_decoded(received_signal)) {
  27. const InfraredMessage* message = infrared_worker_get_decoded_signal(received_signal);
  28. buf_cnt = snprintf(
  29. buf,
  30. sizeof(buf),
  31. "%s, A:0x%0*lX, C:0x%0*lX%s\r\n",
  32. infrared_get_protocol_name(message->protocol),
  33. ROUND_UP_TO(infrared_get_protocol_address_length(message->protocol), 4),
  34. message->address,
  35. ROUND_UP_TO(infrared_get_protocol_command_length(message->protocol), 4),
  36. message->command,
  37. message->repeat ? " R" : "");
  38. cli_write(cli, (uint8_t*)buf, buf_cnt);
  39. } else {
  40. const uint32_t* timings;
  41. size_t timings_cnt;
  42. infrared_worker_get_raw_signal(received_signal, &timings, &timings_cnt);
  43. buf_cnt = snprintf(buf, sizeof(buf), "RAW, %d samples:\r\n", timings_cnt);
  44. cli_write(cli, (uint8_t*)buf, buf_cnt);
  45. for(size_t i = 0; i < timings_cnt; ++i) {
  46. buf_cnt = snprintf(buf, sizeof(buf), "%lu ", timings[i]);
  47. cli_write(cli, (uint8_t*)buf, buf_cnt);
  48. }
  49. buf_cnt = snprintf(buf, sizeof(buf), "\r\n");
  50. cli_write(cli, (uint8_t*)buf, buf_cnt);
  51. }
  52. }
  53. static void infrared_cli_start_ir_rx(Cli* cli, string_t args) {
  54. UNUSED(cli);
  55. UNUSED(args);
  56. InfraredWorker* worker = infrared_worker_alloc();
  57. infrared_worker_rx_start(worker);
  58. infrared_worker_rx_set_received_signal_callback(worker, signal_received_callback, cli);
  59. printf("Receiving INFRARED...\r\nPress Ctrl+C to abort\r\n");
  60. while(!cli_cmd_interrupt_received(cli)) {
  61. furi_delay_ms(50);
  62. }
  63. infrared_worker_rx_stop(worker);
  64. infrared_worker_free(worker);
  65. }
  66. static void infrared_cli_print_usage(void) {
  67. printf("Usage:\r\n");
  68. printf("\tir rx\r\n");
  69. printf("\tir tx <protocol> <address> <command>\r\n");
  70. printf("\t<command> and <address> are hex-formatted\r\n");
  71. printf("\tAvailable protocols:");
  72. for(int i = 0; infrared_is_protocol_valid((InfraredProtocol)i); ++i) {
  73. printf(" %s", infrared_get_protocol_name((InfraredProtocol)i));
  74. }
  75. printf("\r\n");
  76. printf("\tRaw format:\r\n");
  77. printf("\tir tx RAW F:<frequency> DC:<duty_cycle> <sample0> <sample1>...\r\n");
  78. printf(
  79. "\tFrequency (%d - %d), Duty cycle (0 - 100), max 512 samples\r\n",
  80. INFRARED_MIN_FREQUENCY,
  81. INFRARED_MAX_FREQUENCY);
  82. printf("\tir decode <input_file> [<output_file>]\r\n");
  83. }
  84. static bool infrared_cli_parse_message(const char* str, InfraredSignal* signal) {
  85. char protocol_name[32];
  86. InfraredMessage message;
  87. int parsed = sscanf(str, "%31s %lX %lX", protocol_name, &message.address, &message.command);
  88. if(parsed != 3) {
  89. return false;
  90. }
  91. message.protocol = infrared_get_protocol_by_name(protocol_name);
  92. message.repeat = false;
  93. infrared_signal_set_message(signal, &message);
  94. return infrared_signal_is_valid(signal);
  95. }
  96. static bool infrared_cli_parse_raw(const char* str, InfraredSignal* signal) {
  97. char frequency_str[INFRARED_CLI_BUF_SIZE];
  98. char duty_cycle_str[INFRARED_CLI_BUF_SIZE];
  99. int parsed = sscanf(str, "RAW F:%9s DC:%9s", frequency_str, duty_cycle_str);
  100. if(parsed != 2) {
  101. return false;
  102. }
  103. uint32_t* timings = malloc(sizeof(uint32_t) * MAX_TIMINGS_AMOUNT);
  104. uint32_t frequency = atoi(frequency_str);
  105. float duty_cycle = (float)atoi(duty_cycle_str) / 100;
  106. str += strlen(frequency_str) + strlen(duty_cycle_str) + INFRARED_CLI_BUF_SIZE;
  107. size_t timings_size = 0;
  108. while(1) {
  109. while(*str == ' ') {
  110. ++str;
  111. }
  112. char timing_str[INFRARED_CLI_BUF_SIZE];
  113. if(sscanf(str, "%9s", timing_str) != 1) {
  114. break;
  115. }
  116. str += strlen(timing_str);
  117. uint32_t timing = atoi(timing_str);
  118. if((timing <= 0) || (timings_size >= MAX_TIMINGS_AMOUNT)) {
  119. break;
  120. }
  121. timings[timings_size] = timing;
  122. ++timings_size;
  123. }
  124. infrared_signal_set_raw_signal(signal, timings, timings_size, frequency, duty_cycle);
  125. free(timings);
  126. return infrared_signal_is_valid(signal);
  127. }
  128. static void infrared_cli_start_ir_tx(Cli* cli, string_t args) {
  129. UNUSED(cli);
  130. const char* str = string_get_cstr(args);
  131. InfraredSignal* signal = infrared_signal_alloc();
  132. bool success = infrared_cli_parse_message(str, signal) || infrared_cli_parse_raw(str, signal);
  133. if(success) {
  134. infrared_signal_transmit(signal);
  135. } else {
  136. printf("Wrong arguments.\r\n");
  137. infrared_cli_print_usage();
  138. }
  139. infrared_signal_free(signal);
  140. }
  141. static bool
  142. infrared_cli_save_signal(InfraredSignal* signal, FlipperFormat* file, const char* name) {
  143. bool ret = infrared_signal_save(signal, file, name);
  144. if(!ret) {
  145. printf("Failed to save signal: \"%s\"\r\n", name);
  146. }
  147. return ret;
  148. }
  149. static bool infrared_cli_decode_raw_signal(
  150. InfraredRawSignal* raw_signal,
  151. InfraredDecoderHandler* decoder,
  152. FlipperFormat* output_file,
  153. const char* signal_name) {
  154. InfraredSignal* signal = infrared_signal_alloc();
  155. bool ret = false, level = true, is_decoded = false;
  156. size_t i;
  157. for(i = 0; i < raw_signal->timings_size; ++i) {
  158. // TODO: Any infrared_check_decoder_ready() magic?
  159. const InfraredMessage* message = infrared_decode(decoder, level, raw_signal->timings[i]);
  160. if(message) {
  161. is_decoded = true;
  162. printf(
  163. "Protocol: %s address: 0x%lX command: 0x%lX %s\r\n",
  164. infrared_get_protocol_name(message->protocol),
  165. message->address,
  166. message->command,
  167. (message->repeat ? "R" : ""));
  168. if(output_file && !message->repeat) {
  169. infrared_signal_set_message(signal, message);
  170. if(!infrared_cli_save_signal(signal, output_file, signal_name)) break;
  171. }
  172. }
  173. level = !level;
  174. }
  175. if(i == raw_signal->timings_size) {
  176. if(!is_decoded && output_file) {
  177. infrared_signal_set_raw_signal(
  178. signal,
  179. raw_signal->timings,
  180. raw_signal->timings_size,
  181. raw_signal->frequency,
  182. raw_signal->duty_cycle);
  183. ret = infrared_cli_save_signal(signal, output_file, signal_name);
  184. } else {
  185. ret = true;
  186. }
  187. }
  188. infrared_reset_decoder(decoder);
  189. infrared_signal_free(signal);
  190. return ret;
  191. }
  192. static bool infrared_cli_decode_file(FlipperFormat* input_file, FlipperFormat* output_file) {
  193. bool ret = false;
  194. InfraredSignal* signal = infrared_signal_alloc();
  195. InfraredDecoderHandler* decoder = infrared_alloc_decoder();
  196. string_t tmp;
  197. string_init(tmp);
  198. while(infrared_signal_read(signal, input_file, tmp)) {
  199. ret = false;
  200. if(!infrared_signal_is_valid(signal)) {
  201. printf("Invalid signal\r\n");
  202. break;
  203. }
  204. if(!infrared_signal_is_raw(signal)) {
  205. if(output_file &&
  206. !infrared_cli_save_signal(signal, output_file, string_get_cstr(tmp))) {
  207. break;
  208. } else {
  209. printf("Skipping decoded signal\r\n");
  210. continue;
  211. }
  212. }
  213. InfraredRawSignal* raw_signal = infrared_signal_get_raw_signal(signal);
  214. printf("Raw signal: %s, %u samples\r\n", string_get_cstr(tmp), raw_signal->timings_size);
  215. if(!infrared_cli_decode_raw_signal(raw_signal, decoder, output_file, string_get_cstr(tmp)))
  216. break;
  217. ret = true;
  218. }
  219. infrared_free_decoder(decoder);
  220. infrared_signal_free(signal);
  221. string_clear(tmp);
  222. return ret;
  223. }
  224. static void infrared_cli_process_decode(Cli* cli, string_t args) {
  225. UNUSED(cli);
  226. Storage* storage = furi_record_open(RECORD_STORAGE);
  227. FlipperFormat* input_file = flipper_format_buffered_file_alloc(storage);
  228. FlipperFormat* output_file = NULL;
  229. uint32_t version;
  230. string_t tmp, header, input_path, output_path;
  231. string_init(tmp);
  232. string_init(header);
  233. string_init(input_path);
  234. string_init(output_path);
  235. do {
  236. if(!args_read_probably_quoted_string_and_trim(args, input_path)) {
  237. printf("Wrong arguments.\r\n");
  238. infrared_cli_print_usage();
  239. break;
  240. }
  241. args_read_probably_quoted_string_and_trim(args, output_path);
  242. if(!flipper_format_buffered_file_open_existing(input_file, string_get_cstr(input_path))) {
  243. printf("Failed to open file for reading: \"%s\"\r\n", string_get_cstr(input_path));
  244. break;
  245. }
  246. if(!flipper_format_read_header(input_file, header, &version) ||
  247. (!string_start_with_str_p(header, "IR")) || version != 1) {
  248. printf("Invalid or corrupted input file: \"%s\"\r\n", string_get_cstr(input_path));
  249. break;
  250. }
  251. if(!string_empty_p(output_path)) {
  252. printf("Writing output to file: \"%s\"\r\n", string_get_cstr(output_path));
  253. output_file = flipper_format_file_alloc(storage);
  254. }
  255. if(output_file &&
  256. !flipper_format_file_open_always(output_file, string_get_cstr(output_path))) {
  257. printf("Failed to open file for writing: \"%s\"\r\n", string_get_cstr(output_path));
  258. break;
  259. }
  260. if(output_file && !flipper_format_write_header(output_file, header, version)) {
  261. printf("Failed to write to the output file: \"%s\"\r\n", string_get_cstr(output_path));
  262. break;
  263. }
  264. if(!infrared_cli_decode_file(input_file, output_file)) {
  265. break;
  266. }
  267. printf("File successfully decoded.\r\n");
  268. } while(false);
  269. string_clear(tmp);
  270. string_clear(header);
  271. string_clear(input_path);
  272. string_clear(output_path);
  273. flipper_format_free(input_file);
  274. if(output_file) flipper_format_free(output_file);
  275. furi_record_close(RECORD_STORAGE);
  276. }
  277. static void infrared_cli_start_ir(Cli* cli, string_t args, void* context) {
  278. UNUSED(context);
  279. if(furi_hal_infrared_is_busy()) {
  280. printf("INFRARED is busy. Exiting.");
  281. return;
  282. }
  283. string_t command;
  284. string_init(command);
  285. args_read_string_and_trim(args, command);
  286. size_t i = 0;
  287. for(; i < COUNT_OF(infrared_cli_commands); ++i) {
  288. size_t cmd_len = strlen(infrared_cli_commands[i].cmd);
  289. if(!strncmp(string_get_cstr(command), infrared_cli_commands[i].cmd, cmd_len)) {
  290. break;
  291. }
  292. }
  293. if(i < COUNT_OF(infrared_cli_commands)) {
  294. infrared_cli_commands[i].process_function(cli, args);
  295. } else {
  296. infrared_cli_print_usage();
  297. }
  298. string_clear(command);
  299. }
  300. void infrared_on_system_start() {
  301. #ifdef SRV_CLI
  302. Cli* cli = (Cli*)furi_record_open(RECORD_CLI);
  303. cli_add_command(cli, "ir", CliCommandFlagDefault, infrared_cli_start_ir, NULL);
  304. furi_record_close(RECORD_CLI);
  305. #else
  306. UNUSED(infrared_cli_start_ir);
  307. #endif
  308. }