ifttt_virtual_button.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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() {
  35. Settings* settings = malloc(sizeof(Settings));
  36. settings->save_ssid = "";
  37. settings->save_password = "";
  38. settings->save_key = "";
  39. settings->save_event = "";
  40. Storage* storage = furi_record_open(RECORD_STORAGE);
  41. FlipperFormat* file = flipper_format_file_alloc(storage);
  42. FuriString* string_value = furi_string_alloc();
  43. FuriString* text_ssid_value = furi_string_alloc();
  44. FuriString* text_password_value = furi_string_alloc();
  45. FuriString* text_key_value = furi_string_alloc();
  46. FuriString* 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. save_settings_file(file, settings);
  50. flipper_format_file_close(file);
  51. }
  52. } else {
  53. if(flipper_format_file_open_existing(file, CONFIG_FILE_PATH)) {
  54. uint32_t value;
  55. if(flipper_format_read_header(file, string_value, &value)) {
  56. if(flipper_format_read_string(file, CONF_SSID, text_ssid_value)) {
  57. settings->save_ssid = malloc(furi_string_size(text_ssid_value) + 1);
  58. strcpy(settings->save_ssid, furi_string_get_cstr(text_ssid_value));
  59. }
  60. if(flipper_format_read_string(file, CONF_PASSWORD, text_password_value)) {
  61. settings->save_password = malloc(furi_string_size(text_password_value) + 1);
  62. strcpy(settings->save_password, furi_string_get_cstr(text_password_value));
  63. }
  64. if(flipper_format_read_string(file, CONF_KEY, text_key_value)) {
  65. settings->save_key = malloc(furi_string_size(text_key_value) + 1);
  66. strcpy(settings->save_key, furi_string_get_cstr(text_key_value));
  67. }
  68. if(flipper_format_read_string(file, CONF_EVENT, text_event_value)) {
  69. settings->save_event = malloc(furi_string_size(text_event_value) + 1);
  70. strcpy(settings->save_event, furi_string_get_cstr(text_event_value));
  71. }
  72. }
  73. flipper_format_file_close(file);
  74. }
  75. }
  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. // Views
  180. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSendView);
  181. send_view_free(app->sen_view);
  182. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewAboutView);
  183. about_view_free(app->abou_view);
  184. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewSubmenu);
  185. submenu_free(app->submenu);
  186. view_dispatcher_remove_view(app->view_dispatcher, VirtualButtonAppViewDialog);
  187. dialog_ex_free(app->dialog);
  188. // View dispatcher
  189. view_dispatcher_free(app->view_dispatcher);
  190. scene_manager_free(app->scene_manager);
  191. // Records
  192. furi_record_close(RECORD_POWER);
  193. furi_record_close(RECORD_GUI);
  194. furi_hal_serial_deinit(app->serial_handle);
  195. furi_hal_serial_control_release(app->serial_handle);
  196. free(app);
  197. }
  198. int32_t ifttt_virtual_button_app(void* p) {
  199. UNUSED(p);
  200. // Disable expansion protocol to avoid interference with UART Handle
  201. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  202. expansion_disable(expansion);
  203. Storage* storage = furi_record_open(RECORD_STORAGE);
  204. if(!storage_simply_mkdir(storage, IFTTT_FOLDER)) {
  205. }
  206. if(!storage_simply_mkdir(storage, IFTTT_CONFIG_FOLDER)) {
  207. }
  208. furi_record_close(RECORD_STORAGE);
  209. uint32_t first_scene = VirtualButtonAppSceneStart;
  210. VirtualButtonApp* app = ifttt_virtual_button_app_alloc(first_scene);
  211. memcpy(&app->settings, load_settings(), sizeof(Settings));
  212. send_serial_command_config(app->serial_handle, ESerialCommand_Config, &(app->settings));
  213. view_dispatcher_run(app->view_dispatcher);
  214. ifttt_virtual_button_app_free(app);
  215. // Return previous state of expansion
  216. expansion_enable(expansion);
  217. furi_record_close(RECORD_EXPANSION);
  218. return 0;
  219. }