ifttt_virtual_button.c 9.7 KB

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