infrared.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #include "infrared_i.h"
  2. #include <string.h>
  3. #include <dolphin/dolphin.h>
  4. static const NotificationSequence* infrared_notification_sequences[] = {
  5. &sequence_success,
  6. &sequence_set_only_green_255,
  7. &sequence_reset_green,
  8. &sequence_solid_yellow,
  9. &sequence_reset_rgb,
  10. &sequence_blink_start_cyan,
  11. &sequence_blink_start_magenta,
  12. &sequence_blink_stop,
  13. };
  14. static void infrared_make_app_folder(Infrared* infrared) {
  15. if(!storage_simply_mkdir(infrared->storage, INFRARED_APP_FOLDER)) {
  16. dialog_message_show_storage_error(infrared->dialogs, "Cannot create\napp folder");
  17. }
  18. }
  19. static bool infrared_custom_event_callback(void* context, uint32_t event) {
  20. furi_assert(context);
  21. Infrared* infrared = context;
  22. return scene_manager_handle_custom_event(infrared->scene_manager, event);
  23. }
  24. static bool infrared_back_event_callback(void* context) {
  25. furi_assert(context);
  26. Infrared* infrared = context;
  27. return scene_manager_handle_back_event(infrared->scene_manager);
  28. }
  29. static void infrared_tick_event_callback(void* context) {
  30. furi_assert(context);
  31. Infrared* infrared = context;
  32. scene_manager_handle_tick_event(infrared->scene_manager);
  33. }
  34. static void infrared_rpc_command_callback(RpcAppSystemEvent event, void* context) {
  35. furi_assert(context);
  36. Infrared* infrared = context;
  37. furi_assert(infrared->rpc_ctx);
  38. if(event == RpcAppEventSessionClose) {
  39. view_dispatcher_send_custom_event(
  40. infrared->view_dispatcher, InfraredCustomEventTypeRpcSessionClose);
  41. rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
  42. infrared->rpc_ctx = NULL;
  43. } else if(event == RpcAppEventAppExit) {
  44. view_dispatcher_send_custom_event(
  45. infrared->view_dispatcher, InfraredCustomEventTypeRpcExit);
  46. } else if(event == RpcAppEventLoadFile) {
  47. view_dispatcher_send_custom_event(
  48. infrared->view_dispatcher, InfraredCustomEventTypeRpcLoad);
  49. } else if(event == RpcAppEventButtonPress) {
  50. view_dispatcher_send_custom_event(
  51. infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonPress);
  52. } else if(event == RpcAppEventButtonRelease) {
  53. view_dispatcher_send_custom_event(
  54. infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonRelease);
  55. } else {
  56. rpc_system_app_confirm(infrared->rpc_ctx, event, false);
  57. }
  58. }
  59. static void infrared_find_vacant_remote_name(FuriString* name, const char* path) {
  60. Storage* storage = furi_record_open(RECORD_STORAGE);
  61. FuriString* base_path;
  62. base_path = furi_string_alloc_set(path);
  63. if(furi_string_end_with(base_path, INFRARED_APP_EXTENSION)) {
  64. size_t filename_start = furi_string_search_rchar(base_path, '/');
  65. furi_string_left(base_path, filename_start);
  66. }
  67. furi_string_printf(
  68. base_path, "%s/%s%s", path, furi_string_get_cstr(name), INFRARED_APP_EXTENSION);
  69. FS_Error status = storage_common_stat(storage, furi_string_get_cstr(base_path), NULL);
  70. if(status == FSE_OK) {
  71. /* If the suggested name is occupied, try another one (name2, name3, etc) */
  72. size_t dot = furi_string_search_rchar(base_path, '.');
  73. furi_string_left(base_path, dot);
  74. FuriString* path_temp;
  75. path_temp = furi_string_alloc();
  76. uint32_t i = 1;
  77. do {
  78. furi_string_printf(
  79. path_temp, "%s%lu%s", furi_string_get_cstr(base_path), ++i, INFRARED_APP_EXTENSION);
  80. status = storage_common_stat(storage, furi_string_get_cstr(path_temp), NULL);
  81. } while(status == FSE_OK);
  82. furi_string_free(path_temp);
  83. if(status == FSE_NOT_EXIST) {
  84. furi_string_cat_printf(name, "%lu", i);
  85. }
  86. }
  87. furi_string_free(base_path);
  88. furi_record_close(RECORD_STORAGE);
  89. }
  90. static Infrared* infrared_alloc() {
  91. Infrared* infrared = malloc(sizeof(Infrared));
  92. infrared->file_path = furi_string_alloc();
  93. InfraredAppState* app_state = &infrared->app_state;
  94. app_state->is_learning_new_remote = false;
  95. app_state->is_debug_enabled = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
  96. app_state->edit_target = InfraredEditTargetNone;
  97. app_state->edit_mode = InfraredEditModeNone;
  98. app_state->current_button_index = InfraredButtonIndexNone;
  99. infrared->scene_manager = scene_manager_alloc(&infrared_scene_handlers, infrared);
  100. infrared->view_dispatcher = view_dispatcher_alloc();
  101. infrared->gui = furi_record_open(RECORD_GUI);
  102. ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
  103. view_dispatcher_enable_queue(view_dispatcher);
  104. view_dispatcher_set_event_callback_context(view_dispatcher, infrared);
  105. view_dispatcher_set_custom_event_callback(view_dispatcher, infrared_custom_event_callback);
  106. view_dispatcher_set_navigation_event_callback(view_dispatcher, infrared_back_event_callback);
  107. view_dispatcher_set_tick_event_callback(view_dispatcher, infrared_tick_event_callback, 100);
  108. infrared->storage = furi_record_open(RECORD_STORAGE);
  109. infrared->dialogs = furi_record_open(RECORD_DIALOGS);
  110. infrared->notifications = furi_record_open(RECORD_NOTIFICATION);
  111. infrared->worker = infrared_worker_alloc();
  112. infrared->remote = infrared_remote_alloc();
  113. infrared->received_signal = infrared_signal_alloc();
  114. infrared->brute_force = infrared_brute_force_alloc();
  115. infrared->submenu = submenu_alloc();
  116. view_dispatcher_add_view(
  117. view_dispatcher, InfraredViewSubmenu, submenu_get_view(infrared->submenu));
  118. infrared->text_input = text_input_alloc();
  119. view_dispatcher_add_view(
  120. view_dispatcher, InfraredViewTextInput, text_input_get_view(infrared->text_input));
  121. infrared->dialog_ex = dialog_ex_alloc();
  122. view_dispatcher_add_view(
  123. view_dispatcher, InfraredViewDialogEx, dialog_ex_get_view(infrared->dialog_ex));
  124. infrared->button_menu = button_menu_alloc();
  125. view_dispatcher_add_view(
  126. view_dispatcher, InfraredViewButtonMenu, button_menu_get_view(infrared->button_menu));
  127. infrared->popup = popup_alloc();
  128. view_dispatcher_add_view(view_dispatcher, InfraredViewPopup, popup_get_view(infrared->popup));
  129. infrared->view_stack = view_stack_alloc();
  130. view_dispatcher_add_view(
  131. view_dispatcher, InfraredViewStack, view_stack_get_view(infrared->view_stack));
  132. if(app_state->is_debug_enabled) {
  133. infrared->debug_view = infrared_debug_view_alloc();
  134. view_dispatcher_add_view(
  135. view_dispatcher,
  136. InfraredViewDebugView,
  137. infrared_debug_view_get_view(infrared->debug_view));
  138. }
  139. infrared->button_panel = button_panel_alloc();
  140. infrared->loading = loading_alloc();
  141. infrared->progress = infrared_progress_view_alloc();
  142. return infrared;
  143. }
  144. static void infrared_free(Infrared* infrared) {
  145. furi_assert(infrared);
  146. ViewDispatcher* view_dispatcher = infrared->view_dispatcher;
  147. InfraredAppState* app_state = &infrared->app_state;
  148. if(infrared->rpc_ctx) {
  149. rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
  150. rpc_system_app_send_exited(infrared->rpc_ctx);
  151. infrared->rpc_ctx = NULL;
  152. }
  153. view_dispatcher_remove_view(view_dispatcher, InfraredViewSubmenu);
  154. submenu_free(infrared->submenu);
  155. view_dispatcher_remove_view(view_dispatcher, InfraredViewTextInput);
  156. text_input_free(infrared->text_input);
  157. view_dispatcher_remove_view(view_dispatcher, InfraredViewDialogEx);
  158. dialog_ex_free(infrared->dialog_ex);
  159. view_dispatcher_remove_view(view_dispatcher, InfraredViewButtonMenu);
  160. button_menu_free(infrared->button_menu);
  161. view_dispatcher_remove_view(view_dispatcher, InfraredViewPopup);
  162. popup_free(infrared->popup);
  163. view_dispatcher_remove_view(view_dispatcher, InfraredViewStack);
  164. view_stack_free(infrared->view_stack);
  165. if(app_state->is_debug_enabled) {
  166. view_dispatcher_remove_view(view_dispatcher, InfraredViewDebugView);
  167. infrared_debug_view_free(infrared->debug_view);
  168. }
  169. button_panel_free(infrared->button_panel);
  170. loading_free(infrared->loading);
  171. infrared_progress_view_free(infrared->progress);
  172. view_dispatcher_free(view_dispatcher);
  173. scene_manager_free(infrared->scene_manager);
  174. infrared_brute_force_free(infrared->brute_force);
  175. infrared_signal_free(infrared->received_signal);
  176. infrared_remote_free(infrared->remote);
  177. infrared_worker_free(infrared->worker);
  178. furi_record_close(RECORD_NOTIFICATION);
  179. infrared->notifications = NULL;
  180. furi_record_close(RECORD_DIALOGS);
  181. infrared->dialogs = NULL;
  182. furi_record_close(RECORD_GUI);
  183. infrared->gui = NULL;
  184. furi_string_free(infrared->file_path);
  185. free(infrared);
  186. }
  187. bool infrared_add_remote_with_button(
  188. Infrared* infrared,
  189. const char* button_name,
  190. InfraredSignal* signal) {
  191. InfraredRemote* remote = infrared->remote;
  192. FuriString *new_name, *new_path;
  193. new_name = furi_string_alloc_set(INFRARED_DEFAULT_REMOTE_NAME);
  194. new_path = furi_string_alloc_set(INFRARED_APP_FOLDER);
  195. infrared_find_vacant_remote_name(new_name, furi_string_get_cstr(new_path));
  196. furi_string_cat_printf(
  197. new_path, "/%s%s", furi_string_get_cstr(new_name), INFRARED_APP_EXTENSION);
  198. infrared_remote_reset(remote);
  199. infrared_remote_set_name(remote, furi_string_get_cstr(new_name));
  200. infrared_remote_set_path(remote, furi_string_get_cstr(new_path));
  201. furi_string_free(new_name);
  202. furi_string_free(new_path);
  203. return infrared_remote_add_button(remote, button_name, signal);
  204. }
  205. bool infrared_rename_current_remote(Infrared* infrared, const char* name) {
  206. InfraredRemote* remote = infrared->remote;
  207. const char* remote_path = infrared_remote_get_path(remote);
  208. if(!strcmp(infrared_remote_get_name(remote), name)) {
  209. return true;
  210. }
  211. FuriString* new_name;
  212. new_name = furi_string_alloc_set(name);
  213. infrared_find_vacant_remote_name(new_name, remote_path);
  214. FuriString* new_path;
  215. new_path = furi_string_alloc_set(infrared_remote_get_path(remote));
  216. if(furi_string_end_with(new_path, INFRARED_APP_EXTENSION)) {
  217. size_t filename_start = furi_string_search_rchar(new_path, '/');
  218. furi_string_left(new_path, filename_start);
  219. }
  220. furi_string_cat_printf(
  221. new_path, "/%s%s", furi_string_get_cstr(new_name), INFRARED_APP_EXTENSION);
  222. Storage* storage = furi_record_open(RECORD_STORAGE);
  223. FS_Error status = storage_common_rename(
  224. storage, infrared_remote_get_path(remote), furi_string_get_cstr(new_path));
  225. infrared_remote_set_name(remote, furi_string_get_cstr(new_name));
  226. infrared_remote_set_path(remote, furi_string_get_cstr(new_path));
  227. furi_string_free(new_name);
  228. furi_string_free(new_path);
  229. furi_record_close(RECORD_STORAGE);
  230. return (status == FSE_OK || status == FSE_EXIST);
  231. }
  232. void infrared_tx_start_signal(Infrared* infrared, InfraredSignal* signal) {
  233. if(infrared->app_state.is_transmitting) {
  234. FURI_LOG_D(INFRARED_LOG_TAG, "Transmitter is already active");
  235. return;
  236. } else {
  237. infrared->app_state.is_transmitting = true;
  238. }
  239. if(infrared_signal_is_raw(signal)) {
  240. InfraredRawSignal* raw = infrared_signal_get_raw_signal(signal);
  241. infrared_worker_set_raw_signal(infrared->worker, raw->timings, raw->timings_size);
  242. } else {
  243. InfraredMessage* message = infrared_signal_get_message(signal);
  244. infrared_worker_set_decoded_signal(infrared->worker, message);
  245. }
  246. DOLPHIN_DEED(DolphinDeedIrSend);
  247. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
  248. infrared_worker_tx_set_get_signal_callback(
  249. infrared->worker, infrared_worker_tx_get_signal_steady_callback, infrared);
  250. infrared_worker_tx_start(infrared->worker);
  251. }
  252. void infrared_tx_start_button_index(Infrared* infrared, size_t button_index) {
  253. furi_assert(button_index < infrared_remote_get_button_count(infrared->remote));
  254. InfraredRemoteButton* button = infrared_remote_get_button(infrared->remote, button_index);
  255. InfraredSignal* signal = infrared_remote_button_get_signal(button);
  256. infrared_tx_start_signal(infrared, signal);
  257. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
  258. }
  259. void infrared_tx_start_received(Infrared* infrared) {
  260. infrared_tx_start_signal(infrared, infrared->received_signal);
  261. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStartSend);
  262. }
  263. void infrared_tx_stop(Infrared* infrared) {
  264. if(!infrared->app_state.is_transmitting) {
  265. FURI_LOG_D(INFRARED_LOG_TAG, "Transmitter is already stopped");
  266. return;
  267. } else {
  268. infrared->app_state.is_transmitting = false;
  269. }
  270. infrared_worker_tx_stop(infrared->worker);
  271. infrared_worker_tx_set_get_signal_callback(infrared->worker, NULL, NULL);
  272. infrared_play_notification_message(infrared, InfraredNotificationMessageBlinkStop);
  273. }
  274. void infrared_text_store_set(Infrared* infrared, uint32_t bank, const char* text, ...) {
  275. va_list args;
  276. va_start(args, text);
  277. vsnprintf(infrared->text_store[bank], INFRARED_TEXT_STORE_SIZE, text, args);
  278. va_end(args);
  279. }
  280. void infrared_text_store_clear(Infrared* infrared, uint32_t bank) {
  281. memset(infrared->text_store[bank], 0, INFRARED_TEXT_STORE_SIZE + 1);
  282. }
  283. void infrared_play_notification_message(Infrared* infrared, uint32_t message) {
  284. furi_assert(message < sizeof(infrared_notification_sequences) / sizeof(NotificationSequence*));
  285. notification_message(infrared->notifications, infrared_notification_sequences[message]);
  286. }
  287. void infrared_show_loading_popup(Infrared* infrared, bool show) {
  288. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  289. ViewStack* view_stack = infrared->view_stack;
  290. Loading* loading = infrared->loading;
  291. if(show) {
  292. // Raise timer priority so that animations can play
  293. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  294. view_stack_add_view(view_stack, loading_get_view(loading));
  295. } else {
  296. view_stack_remove_view(view_stack, loading_get_view(loading));
  297. // Restore default timer priority
  298. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  299. }
  300. }
  301. void infrared_signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
  302. furi_assert(context);
  303. Infrared* infrared = context;
  304. if(infrared_worker_signal_is_decoded(received_signal)) {
  305. infrared_signal_set_message(
  306. infrared->received_signal, infrared_worker_get_decoded_signal(received_signal));
  307. } else {
  308. const uint32_t* timings;
  309. size_t timings_size;
  310. infrared_worker_get_raw_signal(received_signal, &timings, &timings_size);
  311. infrared_signal_set_raw_signal(
  312. infrared->received_signal,
  313. timings,
  314. timings_size,
  315. INFRARED_COMMON_CARRIER_FREQUENCY,
  316. INFRARED_COMMON_DUTY_CYCLE);
  317. }
  318. view_dispatcher_send_custom_event(
  319. infrared->view_dispatcher, InfraredCustomEventTypeSignalReceived);
  320. }
  321. void infrared_text_input_callback(void* context) {
  322. furi_assert(context);
  323. Infrared* infrared = context;
  324. view_dispatcher_send_custom_event(
  325. infrared->view_dispatcher, InfraredCustomEventTypeTextEditDone);
  326. }
  327. void infrared_popup_closed_callback(void* context) {
  328. furi_assert(context);
  329. Infrared* infrared = context;
  330. view_dispatcher_send_custom_event(
  331. infrared->view_dispatcher, InfraredCustomEventTypePopupClosed);
  332. }
  333. int32_t infrared_app(void* p) {
  334. Infrared* infrared = infrared_alloc();
  335. infrared_make_app_folder(infrared);
  336. bool is_remote_loaded = false;
  337. bool is_rpc_mode = false;
  338. if(p && strlen(p)) {
  339. uint32_t rpc_ctx = 0;
  340. if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
  341. infrared->rpc_ctx = (void*)rpc_ctx;
  342. rpc_system_app_set_callback(
  343. infrared->rpc_ctx, infrared_rpc_command_callback, infrared);
  344. rpc_system_app_send_started(infrared->rpc_ctx);
  345. is_rpc_mode = true;
  346. } else {
  347. furi_string_set(infrared->file_path, (const char*)p);
  348. is_remote_loaded = infrared_remote_load(infrared->remote, infrared->file_path);
  349. if(!is_remote_loaded) {
  350. dialog_message_show_storage_error(
  351. infrared->dialogs, "Failed to load\nselected remote");
  352. return -1;
  353. }
  354. }
  355. }
  356. if(is_rpc_mode) {
  357. view_dispatcher_attach_to_gui(
  358. infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeDesktop);
  359. scene_manager_next_scene(infrared->scene_manager, InfraredSceneRpc);
  360. } else {
  361. view_dispatcher_attach_to_gui(
  362. infrared->view_dispatcher, infrared->gui, ViewDispatcherTypeFullscreen);
  363. if(is_remote_loaded) { //-V547
  364. scene_manager_next_scene(infrared->scene_manager, InfraredSceneRemote);
  365. } else {
  366. scene_manager_next_scene(infrared->scene_manager, InfraredSceneStart);
  367. }
  368. }
  369. view_dispatcher_run(infrared->view_dispatcher);
  370. infrared_free(infrared);
  371. return 0;
  372. }