web_crawler_callback.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Define the GPIO pins available on the Flipper Zero
  2. GpioPin test_pins[9] = {
  3. {.port = GPIOA, .pin = LL_GPIO_PIN_7}, // PB7 - USART1_RX
  4. {.port = GPIOA, .pin = LL_GPIO_PIN_6}, // PB6 - USART1_TX
  5. {.port = GPIOA, .pin = LL_GPIO_PIN_5},
  6. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  7. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  8. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  9. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  10. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  11. {.port = GPIOC, .pin = LL_GPIO_PIN_0}};
  12. // Forward declaration of callback functions
  13. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index);
  14. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index);
  15. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index);
  16. /**
  17. * @brief Navigation callback to handle exiting from other views to the submenu.
  18. * @param context The context - WebCrawlerApp object.
  19. * @return WebCrawlerViewSubmenu
  20. */
  21. static uint32_t web_crawler_back_to_main_callback(void *context)
  22. {
  23. UNUSED(context);
  24. return WebCrawlerViewSubmenu; // Return to the main submenu view
  25. }
  26. /**
  27. * @brief Navigation callback to handle returning to the Configure screen.
  28. * @param context The context - WebCrawlerApp object.
  29. * @return WebCrawlerViewConfigure
  30. */
  31. static uint32_t web_crawler_back_to_configure_callback(void *context)
  32. {
  33. UNUSED(context);
  34. return WebCrawlerViewConfigure; // Return to the Configure screen
  35. }
  36. /**
  37. * @brief Navigation callback to handle exiting the app from the main submenu.
  38. * @param context The context - unused
  39. * @return VIEW_NONE to exit the app
  40. */
  41. static uint32_t web_crawler_exit_app_callback(void *context)
  42. {
  43. UNUSED(context);
  44. return VIEW_NONE; // Exit the app
  45. }
  46. /**
  47. * @brief Handle submenu item selection.
  48. * @param context The context - WebCrawlerApp object.
  49. * @param index The WebCrawlerSubmenuIndex item that was clicked.
  50. */
  51. static void web_crawler_submenu_callback(void *context, uint32_t index)
  52. {
  53. WebCrawlerApp *app = (WebCrawlerApp *)context;
  54. switch (index)
  55. {
  56. case WebCrawlerSubmenuIndexRun:
  57. // Switch to the main view where the saved path will be displayed
  58. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewMain);
  59. break;
  60. case WebCrawlerSubmenuIndexAbout:
  61. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewAbout);
  62. break;
  63. case WebCrawlerSubmenuIndexSetPath:
  64. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewConfigure);
  65. break;
  66. default:
  67. FURI_LOG_E(TAG, "Unknown submenu index");
  68. break;
  69. }
  70. }
  71. /**
  72. * @brief Configuration enter callback to handle different items.
  73. * @param context The context - WebCrawlerApp object.
  74. * @param index The index of the item that was clicked.
  75. */
  76. static void web_crawler_config_enter_callback(void *context, uint32_t index)
  77. {
  78. switch (index)
  79. {
  80. case 0:
  81. web_crawler_setting_item_path_clicked(context, index);
  82. break;
  83. case 1:
  84. web_crawler_setting_item_ssid_clicked(context, index);
  85. break;
  86. case 2:
  87. web_crawler_setting_item_password_clicked(context, index);
  88. break;
  89. default:
  90. FURI_LOG_E(TAG, "Unknown configuration item index");
  91. break;
  92. }
  93. }
  94. // At the top of your file, after includes and defines
  95. static WebCrawlerApp *app_instance = NULL;
  96. // Modify the draw callback function to match the expected signature
  97. static void web_crawler_view_draw_callback(Canvas *canvas, void *model)
  98. {
  99. WebCrawlerMainModel *main_model = (WebCrawlerMainModel *)model; // Cast model to WebCrawlerMainModel
  100. canvas_clear(canvas);
  101. canvas_set_font(canvas, FontPrimary);
  102. if (main_model->path[0] != '\0')
  103. {
  104. canvas_draw_str(canvas, 1, 10, "Sending GET request...");
  105. // Initialize the GPIO pin for output mode
  106. furi_hal_gpio_init_simple(&test_pins[1], GpioModeOutputPushPull);
  107. // Set GPIO pin high
  108. furi_hal_gpio_write(&test_pins[1], true);
  109. canvas_draw_str(canvas, 1, 20, "Sending Wifi settings..");
  110. // Send settings via UART
  111. send_settings_via_uart(main_model->path, main_model->ssid, main_model->password);
  112. furi_delay_ms(1000); // Delay for 1 second
  113. // Read data from UART sent by the dev board
  114. if (read_data_from_uart_and_save(canvas))
  115. {
  116. furi_hal_gpio_write(&test_pins[1], false); // Set GPIO pin low
  117. canvas_draw_str(canvas, 1, 80, "Data received and saved");
  118. // Switch back to submenu view
  119. if (app_instance)
  120. {
  121. view_dispatcher_switch_to_view(app_instance->view_dispatcher, WebCrawlerViewSubmenu);
  122. }
  123. }
  124. else
  125. {
  126. furi_hal_gpio_write(&test_pins[1], false); // Set GPIO pin low
  127. }
  128. }
  129. else
  130. {
  131. canvas_draw_str(canvas, 1, 10, "No path saved.");
  132. }
  133. }
  134. /**
  135. * @brief Input callback for the main screen.
  136. * @param event The input event.
  137. * @param context The context - WebCrawlerApp object.
  138. * @return true if the event was handled, false otherwise.
  139. */
  140. static bool web_crawler_view_input_callback(InputEvent *event, void *context)
  141. {
  142. UNUSED(event);
  143. UNUSED(context);
  144. return false;
  145. }
  146. /**
  147. * @brief Callback for when the user finishes entering the URL.
  148. * @param context The context - WebCrawlerApp object.
  149. */
  150. static void web_crawler_set_path_updated(void *context)
  151. {
  152. WebCrawlerApp *app = (WebCrawlerApp *)context;
  153. // Store the entered URL from temp_buffer_path to path
  154. strncpy(app->path, app->temp_buffer_path, app->temp_buffer_size_path - 1);
  155. // Ensure null-termination
  156. app->path[app->temp_buffer_size_path - 1] = '\0';
  157. if (app->path_item)
  158. {
  159. variable_item_set_current_value_text(app->path_item, app->path);
  160. // Save the URL to the settings
  161. save_settings(app->path, app->ssid, app->password);
  162. FURI_LOG_D(TAG, "URL saved: %s", app->path);
  163. }
  164. // Update the main view's model
  165. WebCrawlerMainModel *main_model = (WebCrawlerMainModel *)view_get_model(app->view_main);
  166. if (main_model)
  167. {
  168. strncpy(main_model->path, app->path, sizeof(main_model->path) - 1);
  169. main_model->path[sizeof(main_model->path) - 1] = '\0';
  170. }
  171. // Return to the Configure view
  172. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewConfigure);
  173. }
  174. /**
  175. * @brief Callback for when the user finishes entering the SSID.
  176. * @param context The context - WebCrawlerApp object.
  177. */
  178. static void web_crawler_set_ssid_updated(void *context)
  179. {
  180. WebCrawlerApp *app = (WebCrawlerApp *)context;
  181. // Store the entered SSID from temp_buffer_ssid to ssid
  182. strncpy(app->ssid, app->temp_buffer_ssid, app->temp_buffer_size_ssid - 1);
  183. // Ensure null-termination
  184. app->ssid[app->temp_buffer_size_ssid - 1] = '\0';
  185. if (app->ssid_item)
  186. {
  187. variable_item_set_current_value_text(app->ssid_item, app->ssid);
  188. // Save the SSID to the settings
  189. save_settings(app->path, app->ssid, app->password);
  190. FURI_LOG_D(TAG, "SSID saved: %s", app->ssid);
  191. }
  192. // Update the main view's model
  193. WebCrawlerMainModel *main_model = (WebCrawlerMainModel *)view_get_model(app->view_main);
  194. if (main_model)
  195. {
  196. strncpy(main_model->ssid, app->ssid, sizeof(main_model->ssid) - 1);
  197. main_model->ssid[sizeof(main_model->ssid) - 1] = '\0';
  198. }
  199. // Return to the Configure view
  200. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewConfigure);
  201. }
  202. /**
  203. * @brief Callback for when the user finishes entering the Password.
  204. * @param context The context - WebCrawlerApp object.
  205. */
  206. static void web_crawler_set_password_update(void *context)
  207. {
  208. WebCrawlerApp *app = (WebCrawlerApp *)context;
  209. // Store the entered Password from temp_buffer_password to password
  210. strncpy(app->password, app->temp_buffer_password, app->temp_buffer_size_password - 1);
  211. // Ensure null-termination
  212. app->password[app->temp_buffer_size_password - 1] = '\0';
  213. if (app->password_item)
  214. {
  215. variable_item_set_current_value_text(app->password_item, app->password);
  216. // Save the Password to the settings
  217. save_settings(app->path, app->ssid, app->password);
  218. FURI_LOG_D(TAG, "Password saved: %s", app->password);
  219. }
  220. // Update the main view's model
  221. WebCrawlerMainModel *main_model = (WebCrawlerMainModel *)view_get_model(app->view_main);
  222. if (main_model)
  223. {
  224. strncpy(main_model->password, app->password, sizeof(main_model->password) - 1);
  225. main_model->password[sizeof(main_model->password) - 1] = '\0';
  226. }
  227. // Return to the Configure view
  228. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewConfigure);
  229. }
  230. /**
  231. * @brief Handler for Path configuration item click.
  232. * @param context The context - WebCrawlerApp object.
  233. * @param index The index of the item that was clicked.
  234. */
  235. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  236. {
  237. WebCrawlerApp *app = (WebCrawlerApp *)context;
  238. UNUSED(index);
  239. // Set up the text input
  240. text_input_set_header_text(app->text_input_path, "Enter URL");
  241. // Initialize temp_buffer with existing path
  242. strncpy(app->temp_buffer_path, "https://www.x.com/", app->temp_buffer_size_path - 1);
  243. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  244. // Configure the text input
  245. bool clear_previous_text = false;
  246. text_input_set_result_callback(
  247. app->text_input_path,
  248. web_crawler_set_path_updated,
  249. app,
  250. app->temp_buffer_path,
  251. app->temp_buffer_size_path,
  252. clear_previous_text);
  253. // Set the previous callback to return to Configure screen
  254. view_set_previous_callback(
  255. text_input_get_view(app->text_input_path),
  256. web_crawler_back_to_configure_callback);
  257. // Show text input dialog
  258. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInput);
  259. }
  260. /**
  261. * @brief Handler for SSID configuration item click.
  262. * @param context The context - WebCrawlerApp object.
  263. * @param index The index of the item that was clicked.
  264. */
  265. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  266. {
  267. WebCrawlerApp *app = (WebCrawlerApp *)context;
  268. UNUSED(index);
  269. // Set up the text input
  270. text_input_set_header_text(app->text_input_ssid, "Enter SSID");
  271. // Initialize temp_buffer with existing SSID
  272. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  273. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  274. // Configure the text input
  275. bool clear_previous_text = false;
  276. text_input_set_result_callback(
  277. app->text_input_ssid,
  278. web_crawler_set_ssid_updated,
  279. app,
  280. app->temp_buffer_ssid,
  281. app->temp_buffer_size_ssid,
  282. clear_previous_text);
  283. // Set the previous callback to return to Configure screen
  284. view_set_previous_callback(
  285. text_input_get_view(app->text_input_ssid),
  286. web_crawler_back_to_configure_callback);
  287. // Show text input dialog
  288. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputSSID);
  289. }
  290. /**
  291. * @brief Handler for Password configuration item click.
  292. * @param context The context - WebCrawlerApp object.
  293. * @param index The index of the item that was clicked.
  294. */
  295. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  296. {
  297. WebCrawlerApp *app = (WebCrawlerApp *)context;
  298. UNUSED(index);
  299. // Set up the text input
  300. text_input_set_header_text(app->text_input_password, "Enter Password");
  301. // Initialize temp_buffer with existing password
  302. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  303. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  304. // Configure the text input
  305. bool clear_previous_text = false;
  306. text_input_set_result_callback(
  307. app->text_input_password,
  308. web_crawler_set_password_update,
  309. app,
  310. app->temp_buffer_password,
  311. app->temp_buffer_size_password,
  312. clear_previous_text);
  313. // Set the previous callback to return to Configure screen
  314. view_set_previous_callback(
  315. text_input_get_view(app->text_input_password),
  316. web_crawler_back_to_configure_callback);
  317. // Show text input dialog
  318. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPassword);
  319. }