ifttt_virtual_button.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. flipper_format_file_close(file);
  50. }
  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. flipper_format_file_close(file);
  73. }
  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(
  84. FuriHalSerialHandle* serial_handle,
  85. ESerialCommand command,
  86. Settings* settings) {
  87. uint8_t data[1] = {0};
  88. char config_tmp[100];
  89. strcpy(config_tmp, "config,");
  90. strcat(config_tmp, settings->save_key);
  91. char config_tmp2[5];
  92. strcpy(config_tmp2, config_tmp);
  93. strcat(config_tmp2, ",");
  94. char config_tmp3[100];
  95. strcpy(config_tmp3, config_tmp2);
  96. strcat(config_tmp3, settings->save_ssid);
  97. char config_tmp4[5];
  98. strcpy(config_tmp4, config_tmp3);
  99. strcat(config_tmp4, ",");
  100. char config_tmp5[100];
  101. strcpy(config_tmp5, config_tmp4);
  102. strcat(config_tmp5, settings->save_password);
  103. char config_tmp6[5];
  104. strcpy(config_tmp6, config_tmp5);
  105. strcat(config_tmp6, ",");
  106. char config[350];
  107. strcpy(config, config_tmp6);
  108. strcat(config, settings->save_event);
  109. int length = strlen(config);
  110. for(int i = 0; i < length; i++) {
  111. switch(command) {
  112. case ESerialCommand_Config:
  113. data[0] = config[i];
  114. break;
  115. default:
  116. return;
  117. }
  118. furi_hal_serial_tx(serial_handle, data, 1);
  119. }
  120. }
  121. static bool ifttt_virtual_button_custom_event_callback(void* context, uint32_t event) {
  122. furi_assert(context);
  123. VirtualButtonApp* app = context;
  124. return scene_manager_handle_custom_event(app->scene_manager, event);
  125. }
  126. static bool ifttt_virtual_button_back_event_callback(void* context) {
  127. furi_assert(context);
  128. VirtualButtonApp* app = context;
  129. return scene_manager_handle_back_event(app->scene_manager);
  130. }
  131. static void ifttt_virtual_button_tick_event_callback(void* context) {
  132. furi_assert(context);
  133. VirtualButtonApp* app = context;
  134. scene_manager_handle_tick_event(app->scene_manager);
  135. }
  136. VirtualButtonApp* ifttt_virtual_button_app_alloc(uint32_t first_scene) {
  137. VirtualButtonApp* app = malloc(sizeof(VirtualButtonApp));
  138. // Records
  139. app->gui = furi_record_open(RECORD_GUI);
  140. app->power = furi_record_open(RECORD_POWER);
  141. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  142. furi_check(app->serial_handle);
  143. furi_hal_serial_init(app->serial_handle, FLIPPERZERO_SERIAL_BAUD);
  144. // View dispatcher
  145. app->view_dispatcher = view_dispatcher_alloc();
  146. app->scene_manager = scene_manager_alloc(&virtual_button_scene_handlers, app);
  147. view_dispatcher_enable_queue(app->view_dispatcher);
  148. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  149. view_dispatcher_set_custom_event_callback(
  150. app->view_dispatcher, ifttt_virtual_button_custom_event_callback);
  151. view_dispatcher_set_navigation_event_callback(
  152. app->view_dispatcher, ifttt_virtual_button_back_event_callback);
  153. view_dispatcher_set_tick_event_callback(
  154. app->view_dispatcher, ifttt_virtual_button_tick_event_callback, 2000);
  155. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  156. // Views
  157. app->sen_view = send_view_alloc(app->serial_handle);
  158. view_dispatcher_add_view(
  159. app->view_dispatcher, VirtualButtonAppViewSendView, send_view_get_view(app->sen_view));
  160. app->abou_view = about_view_alloc();
  161. view_dispatcher_add_view(
  162. app->view_dispatcher, VirtualButtonAppViewAboutView, about_view_get_view(app->abou_view));
  163. app->submenu = submenu_alloc();
  164. view_dispatcher_add_view(
  165. app->view_dispatcher, VirtualButtonAppViewSubmenu, submenu_get_view(app->submenu));
  166. app->dialog = dialog_ex_alloc();
  167. view_dispatcher_add_view(
  168. app->view_dispatcher, VirtualButtonAppViewDialog, dialog_ex_get_view(app->dialog));
  169. // Set first scene
  170. scene_manager_next_scene(app->scene_manager, first_scene);
  171. return app;
  172. }
  173. void ifttt_virtual_button_app_free(VirtualButtonApp* app) {
  174. furi_assert(app);
  175. free(app->settings.save_ssid);
  176. free(app->settings.save_password);
  177. free(app->settings.save_key);
  178. // Views
  179. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSendView);
  180. send_view_free(app->sen_view);
  181. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewAboutView);
  182. about_view_free(app->abou_view);
  183. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSubmenu);
  184. submenu_free(app->submenu);
  185. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewDialog);
  186. dialog_ex_free(app->dialog);
  187. // View dispatcher
  188. view_dispatcher_free(app->view_dispatcher);
  189. scene_manager_free(app->scene_manager);
  190. // Records
  191. furi_record_close(RECORD_POWER);
  192. furi_record_close(RECORD_GUI);
  193. furi_hal_serial_deinit(app->serial_handle);
  194. furi_hal_serial_control_release(app->serial_handle);
  195. free(app);
  196. }
  197. int32_t ifttt_virtual_button_app(void* p) {
  198. UNUSED(p);
  199. Storage* storage = furi_record_open(RECORD_STORAGE);
  200. if(!storage_simply_mkdir(storage, IFTTT_FOLDER)) {
  201. }
  202. if(!storage_simply_mkdir(storage, IFTTT_CONFIG_FOLDER)) {
  203. }
  204. furi_record_close(RECORD_STORAGE);
  205. uint32_t first_scene = VirtualButtonAppSceneStart;
  206. VirtualButtonApp* app = ifttt_virtual_button_app_alloc(first_scene);
  207. memcpy(&app->settings, load_settings(), sizeof(Settings));
  208. send_serial_command_config(app->serial_handle, ESerialCommand_Config, &(app->settings));
  209. view_dispatcher_run(app->view_dispatcher);
  210. ifttt_virtual_button_app_free(app);
  211. return 0;
  212. }