ifttt_virtual_button.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "ifttt_virtual_button.h"
  2. #include <expansion/expansion.h>
  3. #define IFTTT_FOLDER "/ext/apps_data/ifttt"
  4. #define IFTTT_CONFIG_FOLDER "/ext/apps_data/ifttt/config"
  5. const char* CONFIG_FILE_PATH = "/ext/apps_data/ifttt/config/config.settings";
  6. #define FLIPPERZERO_SERIAL_BAUD 115200
  7. typedef enum ESerialCommand { ESerialCommand_Config } ESerialCommand;
  8. Settings save_settings(Settings settings) {
  9. Storage* storage = furi_record_open(RECORD_STORAGE);
  10. FlipperFormat* file = flipper_format_file_alloc(storage);
  11. if(flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
  12. flipper_format_update_string_cstr(file, CONF_SSID, settings.save_ssid);
  13. flipper_format_update_string_cstr(file, CONF_PASSWORD, settings.save_password);
  14. flipper_format_update_string_cstr(file, CONF_KEY, settings.save_key);
  15. flipper_format_update_string_cstr(file, CONF_EVENT, settings.save_event);
  16. } else {
  17. }
  18. flipper_format_file_close(file);
  19. flipper_format_free(file);
  20. furi_record_close(RECORD_STORAGE);
  21. return settings;
  22. }
  23. void save_settings_file(FlipperFormat* file, Settings* settings) {
  24. flipper_format_write_header_cstr(file, CONFIG_FILE_HEADER, CONFIG_FILE_VERSION);
  25. flipper_format_write_comment_cstr(file, "Enter here the SSID of the wifi network");
  26. flipper_format_write_string_cstr(file, CONF_SSID, settings->save_ssid);
  27. flipper_format_write_comment_cstr(file, "Enter here the PASSWORD of the wifi network");
  28. flipper_format_write_string_cstr(file, CONF_PASSWORD, settings->save_password);
  29. flipper_format_write_comment_cstr(file, "Enter here the WEBHOOKS of your IFTTT account");
  30. flipper_format_write_string_cstr(file, CONF_KEY, settings->save_key);
  31. flipper_format_write_comment_cstr(file, "Enter here the EVENT name of your trigger");
  32. flipper_format_write_string_cstr(file, CONF_EVENT, settings->save_event);
  33. }
  34. Settings* load_settings(Settings* 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 = (char*)furi_string_get_cstr(text_ssid_value);
  57. }
  58. if(flipper_format_read_string(file, CONF_PASSWORD, text_password_value)) {
  59. settings->save_password = (char*)furi_string_get_cstr(text_password_value);
  60. }
  61. if(flipper_format_read_string(file, CONF_KEY, text_key_value)) {
  62. settings->save_key = (char*)furi_string_get_cstr(text_key_value);
  63. }
  64. if(flipper_format_read_string(file, CONF_EVENT, text_event_value)) {
  65. settings->save_event = (char*)furi_string_get_cstr(text_event_value);
  66. }
  67. }
  68. flipper_format_file_close(file);
  69. }
  70. }
  71. // We assigned some constant / internal buffer of furi string, so duplicate it and free at exit
  72. settings->save_ssid = strdup(settings->save_ssid);
  73. settings->save_password = strdup(settings->save_password);
  74. settings->save_key = strdup(settings->save_key);
  75. settings->save_event = strdup(settings->save_event);
  76. furi_string_free(text_ssid_value);
  77. furi_string_free(text_password_value);
  78. furi_string_free(text_key_value);
  79. furi_string_free(text_event_value);
  80. flipper_format_free(file);
  81. furi_record_close(RECORD_STORAGE);
  82. return settings;
  83. }
  84. void send_serial_command_config(
  85. FuriHalSerialHandle* serial_handle,
  86. ESerialCommand command,
  87. Settings* settings) {
  88. uint8_t data[1] = {0};
  89. char config_tmp[100];
  90. strcpy(config_tmp, "config,");
  91. strcat(config_tmp, settings->save_key);
  92. char config_tmp2[5];
  93. strcpy(config_tmp2, config_tmp);
  94. strcat(config_tmp2, ",");
  95. char config_tmp3[100];
  96. strcpy(config_tmp3, config_tmp2);
  97. strcat(config_tmp3, settings->save_ssid);
  98. char config_tmp4[5];
  99. strcpy(config_tmp4, config_tmp3);
  100. strcat(config_tmp4, ",");
  101. char config_tmp5[100];
  102. strcpy(config_tmp5, config_tmp4);
  103. strcat(config_tmp5, settings->save_password);
  104. char config_tmp6[5];
  105. strcpy(config_tmp6, config_tmp5);
  106. strcat(config_tmp6, ",");
  107. char config[350];
  108. strcpy(config, config_tmp6);
  109. strcat(config, settings->save_event);
  110. int length = strlen(config);
  111. for(int i = 0; i < length; i++) {
  112. switch(command) {
  113. case ESerialCommand_Config:
  114. data[0] = config[i];
  115. break;
  116. default:
  117. return;
  118. }
  119. furi_hal_serial_tx(serial_handle, data, 1);
  120. }
  121. }
  122. static bool ifttt_virtual_button_custom_event_callback(void* context, uint32_t event) {
  123. furi_assert(context);
  124. VirtualButtonApp* app = context;
  125. return scene_manager_handle_custom_event(app->scene_manager, event);
  126. }
  127. static bool ifttt_virtual_button_back_event_callback(void* context) {
  128. furi_assert(context);
  129. VirtualButtonApp* app = context;
  130. return scene_manager_handle_back_event(app->scene_manager);
  131. }
  132. static void ifttt_virtual_button_tick_event_callback(void* context) {
  133. furi_assert(context);
  134. VirtualButtonApp* app = context;
  135. scene_manager_handle_tick_event(app->scene_manager);
  136. }
  137. VirtualButtonApp* ifttt_virtual_button_app_alloc(uint32_t first_scene) {
  138. VirtualButtonApp* app = malloc(sizeof(VirtualButtonApp));
  139. // Records
  140. app->gui = furi_record_open(RECORD_GUI);
  141. app->power = furi_record_open(RECORD_POWER);
  142. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  143. furi_check(app->serial_handle);
  144. furi_hal_serial_init(app->serial_handle, FLIPPERZERO_SERIAL_BAUD);
  145. // View dispatcher
  146. app->view_dispatcher = view_dispatcher_alloc();
  147. app->scene_manager = scene_manager_alloc(&virtual_button_scene_handlers, app);
  148. view_dispatcher_enable_queue(app->view_dispatcher);
  149. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  150. view_dispatcher_set_custom_event_callback(
  151. app->view_dispatcher, ifttt_virtual_button_custom_event_callback);
  152. view_dispatcher_set_navigation_event_callback(
  153. app->view_dispatcher, ifttt_virtual_button_back_event_callback);
  154. view_dispatcher_set_tick_event_callback(
  155. app->view_dispatcher, ifttt_virtual_button_tick_event_callback, 2000);
  156. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  157. // Views
  158. app->sen_view = send_view_alloc(app->serial_handle);
  159. view_dispatcher_add_view(
  160. app->view_dispatcher, VirtualButtonAppViewSendView, send_view_get_view(app->sen_view));
  161. app->abou_view = about_view_alloc();
  162. view_dispatcher_add_view(
  163. app->view_dispatcher, VirtualButtonAppViewAboutView, about_view_get_view(app->abou_view));
  164. app->submenu = submenu_alloc();
  165. view_dispatcher_add_view(
  166. app->view_dispatcher, VirtualButtonAppViewSubmenu, submenu_get_view(app->submenu));
  167. app->dialog = dialog_ex_alloc();
  168. view_dispatcher_add_view(
  169. app->view_dispatcher, VirtualButtonAppViewDialog, dialog_ex_get_view(app->dialog));
  170. // Set first scene
  171. scene_manager_next_scene(app->scene_manager, first_scene);
  172. return app;
  173. }
  174. void ifttt_virtual_button_app_free(VirtualButtonApp* app) {
  175. furi_assert(app);
  176. free(app->settings.save_ssid);
  177. free(app->settings.save_password);
  178. free(app->settings.save_key);
  179. free(app->settings.save_event);
  180. // Views
  181. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSendView);
  182. send_view_free(app->sen_view);
  183. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewAboutView);
  184. about_view_free(app->abou_view);
  185. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSubmenu);
  186. submenu_free(app->submenu);
  187. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewDialog);
  188. dialog_ex_free(app->dialog);
  189. // View dispatcher
  190. view_dispatcher_free(app->view_dispatcher);
  191. scene_manager_free(app->scene_manager);
  192. // Records
  193. furi_record_close(RECORD_POWER);
  194. furi_record_close(RECORD_GUI);
  195. furi_hal_serial_deinit(app->serial_handle);
  196. furi_hal_serial_control_release(app->serial_handle);
  197. free(app);
  198. }
  199. int32_t ifttt_virtual_button_app(void* p) {
  200. UNUSED(p);
  201. // Disable expansion protocol to avoid interference with UART Handle
  202. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  203. expansion_disable(expansion);
  204. Storage* storage = furi_record_open(RECORD_STORAGE);
  205. if(!storage_simply_mkdir(storage, IFTTT_FOLDER)) {
  206. }
  207. if(!storage_simply_mkdir(storage, IFTTT_CONFIG_FOLDER)) {
  208. }
  209. furi_record_close(RECORD_STORAGE);
  210. uint32_t first_scene = VirtualButtonAppSceneStart;
  211. VirtualButtonApp* app = ifttt_virtual_button_app_alloc(first_scene);
  212. load_settings(&app->settings);
  213. send_serial_command_config(app->serial_handle, ESerialCommand_Config, &(app->settings));
  214. view_dispatcher_run(app->view_dispatcher);
  215. ifttt_virtual_button_app_free(app);
  216. // Return previous state of expansion
  217. expansion_enable(expansion);
  218. furi_record_close(RECORD_EXPANSION);
  219. return 0;
  220. }