ifttt_virtual_button.c 9.8 KB

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