ifttt_virtual_button.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "ifttt_virtual_button.h"
  2. #define IFTTT_FOLDER "/ext/apps_data/ifttt"
  3. #define IFTTT_CONFIG_FOLDER "/ext/apps_data/ifttt/config"
  4. const char* CONFIG_FILE_PATH = "/ext/apps_data/ifttt/config/config.settings";
  5. #define FLIPPERZERO_SERIAL_BAUD 115200
  6. typedef enum ESerialCommand { ESerialCommand_Config } ESerialCommand;
  7. Settings save_settings(Settings settings) {
  8. Storage* storage = furi_record_open(RECORD_STORAGE);
  9. FlipperFormat* file = flipper_format_file_alloc(storage);
  10. if(flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
  11. flipper_format_update_string_cstr(file, CONF_SSID, settings.save_ssid);
  12. flipper_format_update_string_cstr(file, CONF_PASSWORD, settings.save_password);
  13. flipper_format_update_string_cstr(file, CONF_KEY, settings.save_key);
  14. flipper_format_update_string_cstr(file, CONF_EVENT, settings.save_event);
  15. } else {
  16. }
  17. flipper_format_file_close(file);
  18. flipper_format_free(file);
  19. furi_record_close(RECORD_STORAGE);
  20. return settings;
  21. }
  22. void save_settings_file(FlipperFormat* file, Settings* settings) {
  23. flipper_format_write_header_cstr(file, CONFIG_FILE_HEADER, CONFIG_FILE_VERSION);
  24. flipper_format_write_comment_cstr(file, "Enter here the SSID of the wifi network");
  25. flipper_format_write_string_cstr(file, CONF_SSID, settings->save_ssid);
  26. flipper_format_write_comment_cstr(file, "Enter here the PASSWORD of the wifi network");
  27. flipper_format_write_string_cstr(file, CONF_PASSWORD, settings->save_password);
  28. flipper_format_write_comment_cstr(file, "Enter here the WEBHOOKS of your IFTTT account");
  29. flipper_format_write_string_cstr(file, CONF_KEY, settings->save_key);
  30. flipper_format_write_comment_cstr(file, "Enter here the EVENT name of your trigger");
  31. flipper_format_write_string_cstr(file, CONF_EVENT, settings->save_event);
  32. }
  33. Settings* load_settings() {
  34. Settings* settings = malloc(sizeof(Settings));
  35. settings->save_ssid = "";
  36. settings->save_password = "";
  37. settings->save_key = "";
  38. settings->save_event = "";
  39. Storage* storage = furi_record_open(RECORD_STORAGE);
  40. FlipperFormat* file = flipper_format_file_alloc(storage);
  41. FuriString* string_value = furi_string_alloc();
  42. FuriString* text_ssid_value = furi_string_alloc();
  43. FuriString* text_password_value = furi_string_alloc();
  44. FuriString* text_key_value = furi_string_alloc();
  45. FuriString* text_event_value = furi_string_alloc();
  46. if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) != FSE_OK) {
  47. if(flipper_format_file_open_new(file, CONFIG_FILE_PATH)) {
  48. save_settings_file(file, settings);
  49. }
  50. flipper_format_file_close(file);
  51. } else {
  52. if(flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
  53. uint32_t value;
  54. if(flipper_format_read_header(file, string_value, &value)) {
  55. if(flipper_format_read_string(file, CONF_SSID, text_ssid_value)) {
  56. settings->save_ssid = malloc(furi_string_size(text_ssid_value) + 1);
  57. strcpy(settings->save_ssid, furi_string_get_cstr(text_ssid_value));
  58. }
  59. if(flipper_format_read_string(file, CONF_PASSWORD, text_password_value)) {
  60. settings->save_password = malloc(furi_string_size(text_password_value) + 1);
  61. strcpy(settings->save_password, furi_string_get_cstr(text_password_value));
  62. }
  63. if(flipper_format_read_string(file, CONF_KEY, text_key_value)) {
  64. settings->save_key = malloc(furi_string_size(text_key_value) + 1);
  65. strcpy(settings->save_key, furi_string_get_cstr(text_key_value));
  66. }
  67. if(flipper_format_read_string(file, CONF_EVENT, text_event_value)) {
  68. settings->save_event = malloc(furi_string_size(text_event_value) + 1);
  69. strcpy(settings->save_event, furi_string_get_cstr(text_event_value));
  70. }
  71. }
  72. }
  73. flipper_format_file_close(file);
  74. }
  75. furi_string_free(text_ssid_value);
  76. furi_string_free(text_password_value);
  77. furi_string_free(text_key_value);
  78. furi_string_free(text_event_value);
  79. flipper_format_free(file);
  80. furi_record_close(RECORD_STORAGE);
  81. return settings;
  82. }
  83. void send_serial_command_config(ESerialCommand command, Settings* settings) {
  84. uint8_t data[1] = {0};
  85. char config_tmp[100];
  86. strcpy(config_tmp, "config,");
  87. strcat(config_tmp, settings->save_key);
  88. char config_tmp2[5];
  89. strcpy(config_tmp2, config_tmp);
  90. strcat(config_tmp2, ",");
  91. char config_tmp3[100];
  92. strcpy(config_tmp3, config_tmp2);
  93. strcat(config_tmp3, settings->save_ssid);
  94. char config_tmp4[5];
  95. strcpy(config_tmp4, config_tmp3);
  96. strcat(config_tmp4, ",");
  97. char config_tmp5[100];
  98. strcpy(config_tmp5, config_tmp4);
  99. strcat(config_tmp5, settings->save_password);
  100. char config_tmp6[5];
  101. strcpy(config_tmp6, config_tmp5);
  102. strcat(config_tmp6, ",");
  103. char config[350];
  104. strcpy(config, config_tmp6);
  105. strcat(config, settings->save_event);
  106. int length = strlen(config);
  107. for(int i = 0; i < length; i++) {
  108. switch(command) {
  109. case ESerialCommand_Config:
  110. data[0] = config[i];
  111. break;
  112. default:
  113. return;
  114. }
  115. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  116. }
  117. }
  118. static bool ifttt_virtual_button_custom_event_callback(void* context, uint32_t event) {
  119. furi_assert(context);
  120. VirtualButtonApp* app = context;
  121. return scene_manager_handle_custom_event(app->scene_manager, event);
  122. }
  123. static bool ifttt_virtual_button_back_event_callback(void* context) {
  124. furi_assert(context);
  125. VirtualButtonApp* app = context;
  126. return scene_manager_handle_back_event(app->scene_manager);
  127. }
  128. static void ifttt_virtual_button_tick_event_callback(void* context) {
  129. furi_assert(context);
  130. VirtualButtonApp* app = context;
  131. scene_manager_handle_tick_event(app->scene_manager);
  132. }
  133. VirtualButtonApp* ifttt_virtual_button_app_alloc(uint32_t first_scene) {
  134. VirtualButtonApp* app = malloc(sizeof(VirtualButtonApp));
  135. // Records
  136. app->gui = furi_record_open(RECORD_GUI);
  137. app->power = furi_record_open(RECORD_POWER);
  138. // View dispatcher
  139. app->view_dispatcher = view_dispatcher_alloc();
  140. app->scene_manager = scene_manager_alloc(&virtual_button_scene_handlers, app);
  141. view_dispatcher_enable_queue(app->view_dispatcher);
  142. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  143. view_dispatcher_set_custom_event_callback(
  144. app->view_dispatcher, ifttt_virtual_button_custom_event_callback);
  145. view_dispatcher_set_navigation_event_callback(
  146. app->view_dispatcher, ifttt_virtual_button_back_event_callback);
  147. view_dispatcher_set_tick_event_callback(
  148. app->view_dispatcher, ifttt_virtual_button_tick_event_callback, 2000);
  149. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  150. // Views
  151. app->sen_view = send_view_alloc();
  152. view_dispatcher_add_view(
  153. app->view_dispatcher, VirtualButtonAppViewSendView, send_view_get_view(app->sen_view));
  154. app->abou_view = about_view_alloc();
  155. view_dispatcher_add_view(
  156. app->view_dispatcher, VirtualButtonAppViewAboutView, about_view_get_view(app->abou_view));
  157. app->submenu = submenu_alloc();
  158. view_dispatcher_add_view(
  159. app->view_dispatcher, VirtualButtonAppViewSubmenu, submenu_get_view(app->submenu));
  160. app->dialog = dialog_ex_alloc();
  161. view_dispatcher_add_view(
  162. app->view_dispatcher, VirtualButtonAppViewDialog, dialog_ex_get_view(app->dialog));
  163. // Set first scene
  164. scene_manager_next_scene(app->scene_manager, first_scene);
  165. return app;
  166. }
  167. void ifttt_virtual_button_app_free(VirtualButtonApp* app) {
  168. furi_assert(app);
  169. free(app->settings.save_ssid);
  170. free(app->settings.save_password);
  171. free(app->settings.save_key);
  172. // Views
  173. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSendView);
  174. send_view_free(app->sen_view);
  175. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewAboutView);
  176. about_view_free(app->abou_view);
  177. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSubmenu);
  178. submenu_free(app->submenu);
  179. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewDialog);
  180. dialog_ex_free(app->dialog);
  181. // View dispatcher
  182. view_dispatcher_free(app->view_dispatcher);
  183. scene_manager_free(app->scene_manager);
  184. // Records
  185. furi_record_close(RECORD_POWER);
  186. furi_record_close(RECORD_GUI);
  187. free(app);
  188. }
  189. int32_t ifttt_virtual_button_app(void* p) {
  190. UNUSED(p);
  191. Storage* storage = furi_record_open(RECORD_STORAGE);
  192. if(!storage_simply_mkdir(storage, IFTTT_FOLDER)) {
  193. }
  194. if(!storage_simply_mkdir(storage, IFTTT_CONFIG_FOLDER)) {
  195. }
  196. furi_record_close(RECORD_STORAGE);
  197. uint32_t first_scene = VirtualButtonAppSceneStart;
  198. VirtualButtonApp* app = ifttt_virtual_button_app_alloc(first_scene);
  199. memcpy(&app->settings, load_settings(), sizeof(Settings));
  200. send_serial_command_config(ESerialCommand_Config, &(app->settings));
  201. view_dispatcher_run(app->view_dispatcher);
  202. ifttt_virtual_button_app_free(app);
  203. return 0;
  204. }