ifttt_virtual_button.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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,
  171. VirtualButtonAppViewAboutView,
  172. about_view_get_view(app->abou_view));
  173. app->submenu = submenu_alloc();
  174. view_dispatcher_add_view(
  175. app->view_dispatcher, VirtualButtonAppViewSubmenu, submenu_get_view(app->submenu));
  176. app->dialog = dialog_ex_alloc();
  177. view_dispatcher_add_view(
  178. app->view_dispatcher, VirtualButtonAppViewDialog, dialog_ex_get_view(app->dialog));
  179. // Set first scene
  180. scene_manager_next_scene(app->scene_manager, first_scene);
  181. return app;
  182. }
  183. void ifttt_virtual_button_app_free(VirtualButtonApp* app) {
  184. furi_assert(app);
  185. free(app->settings.save_ssid);
  186. free(app->settings.save_password);
  187. free(app->settings.save_key);
  188. // Views
  189. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSendView);
  190. send_view_free(app->sen_view);
  191. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewAboutView);
  192. about_view_free(app->abou_view);
  193. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSubmenu);
  194. submenu_free(app->submenu);
  195. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewDialog);
  196. dialog_ex_free(app->dialog);
  197. // View dispatcher
  198. view_dispatcher_free(app->view_dispatcher);
  199. scene_manager_free(app->scene_manager);
  200. // Records
  201. furi_record_close(RECORD_POWER);
  202. furi_record_close(RECORD_GUI);
  203. free(app);
  204. }
  205. int32_t ifttt_virtual_button_app(void* p) {
  206. UNUSED(p);
  207. Storage* storage = furi_record_open(RECORD_STORAGE);
  208. if(!storage_simply_mkdir(storage, IFTTT_FOLDER)) {
  209. }
  210. if(!storage_simply_mkdir(storage, IFTTT_CONFIG_FOLDER)) {
  211. }
  212. furi_record_close(RECORD_STORAGE);
  213. uint32_t first_scene = VirtualButtonAppSceneStart;
  214. VirtualButtonApp* app = ifttt_virtual_button_app_alloc(first_scene);
  215. memcpy(&app->settings,load_settings(),sizeof(Settings));
  216. send_serial_command_config(ESerialCommand_Config, &(app->settings));
  217. view_dispatcher_run(app->view_dispatcher);
  218. ifttt_virtual_button_app_free(app);
  219. return 0;
  220. }