web_crawler_i.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * @brief Function to allocate resources for the WebCrawlerApp.
  3. * @return Pointer to the initialized WebCrawlerApp, or NULL on failure.
  4. */
  5. // In web_crawler_app_alloc, after allocating and initializing 'app'
  6. static WebCrawlerApp *web_crawler_app_alloc()
  7. {
  8. WebCrawlerApp *app = (WebCrawlerApp *)malloc(sizeof(WebCrawlerApp));
  9. if (!app)
  10. {
  11. FURI_LOG_E(TAG, "Failed to allocate WebCrawlerApp");
  12. return NULL;
  13. }
  14. // Initialize the entire structure to zero to prevent undefined behavior
  15. memset(app, 0, sizeof(WebCrawlerApp));
  16. // Allocate and initialize temp_buffer and path
  17. app->temp_buffer_size_path = 128;
  18. app->temp_buffer_path = (char *)malloc(app->temp_buffer_size_path);
  19. if (!app->temp_buffer_path)
  20. {
  21. FURI_LOG_E(TAG, "Failed to allocate temp_buffer_path");
  22. free(app);
  23. return NULL;
  24. }
  25. app->temp_buffer_path[0] = '\0';
  26. // Allocate path
  27. app->path = (char *)malloc(app->temp_buffer_size_path);
  28. if (!app->path)
  29. {
  30. FURI_LOG_E(TAG, "Failed to allocate path");
  31. free(app->temp_buffer_path);
  32. free(app);
  33. return NULL;
  34. }
  35. app->path[0] = '\0';
  36. // Allocate and initialize temp_buffer_ssid
  37. app->temp_buffer_size_ssid = 128;
  38. app->temp_buffer_ssid = (char *)malloc(app->temp_buffer_size_ssid);
  39. if (!app->temp_buffer_ssid)
  40. {
  41. FURI_LOG_E(TAG, "Failed to allocate temp_buffer_ssid");
  42. free_buffers(app);
  43. return NULL;
  44. }
  45. app->temp_buffer_ssid[0] = '\0';
  46. // Allocate ssid
  47. app->ssid = (char *)malloc(app->temp_buffer_size_ssid);
  48. if (!app->ssid)
  49. {
  50. FURI_LOG_E(TAG, "Failed to allocate ssid");
  51. free_buffers(app);
  52. return NULL;
  53. }
  54. app->ssid[0] = '\0';
  55. // Allocate and initialize temp_buffer_password
  56. app->temp_buffer_size_password = 128;
  57. app->temp_buffer_password = (char *)malloc(app->temp_buffer_size_password);
  58. if (!app->temp_buffer_password)
  59. {
  60. FURI_LOG_E(TAG, "Failed to allocate temp_buffer_password");
  61. free_buffers(app);
  62. return NULL;
  63. }
  64. app->temp_buffer_password[0] = '\0';
  65. // Allocate password
  66. app->password = (char *)malloc(app->temp_buffer_size_password);
  67. if (!app->password)
  68. {
  69. FURI_LOG_E(TAG, "Failed to allocate password");
  70. free_buffers(app);
  71. return NULL;
  72. }
  73. app->password[0] = '\0';
  74. // Assign to global variable
  75. app_instance = app;
  76. // Open GUI
  77. Gui *gui = furi_record_open(RECORD_GUI);
  78. if (!gui)
  79. {
  80. FURI_LOG_E(TAG, "Failed to open GUI record");
  81. free_resources(app);
  82. return NULL;
  83. }
  84. // Allocate ViewDispatcher
  85. app->view_dispatcher = view_dispatcher_alloc();
  86. if (!app->view_dispatcher)
  87. {
  88. FURI_LOG_E(TAG, "Failed to allocate ViewDispatcher");
  89. furi_record_close(RECORD_GUI);
  90. free_resources(app);
  91. return NULL;
  92. }
  93. // Attach ViewDispatcher to GUI
  94. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  95. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  96. // Allocate TextInput views
  97. app->text_input_path = text_input_alloc();
  98. if (!app->text_input_path)
  99. {
  100. free_all(app, "Failed to allocate TextInput for Path");
  101. return NULL;
  102. }
  103. app->text_input_ssid = text_input_alloc();
  104. if (!app->text_input_ssid)
  105. {
  106. free_all(app, "Failed to allocate TextInput for SSID");
  107. return NULL;
  108. }
  109. app->text_input_password = text_input_alloc();
  110. if (!app->text_input_password)
  111. {
  112. free_all(app, "Failed to allocate TextInput for Password");
  113. return NULL;
  114. }
  115. // Add TextInput views with unique view IDs
  116. view_dispatcher_add_view(
  117. app->view_dispatcher,
  118. WebCrawlerViewTextInput,
  119. text_input_get_view(app->text_input_path));
  120. view_dispatcher_add_view(
  121. app->view_dispatcher,
  122. WebCrawlerViewTextInputSSID,
  123. text_input_get_view(app->text_input_ssid));
  124. view_dispatcher_add_view(
  125. app->view_dispatcher,
  126. WebCrawlerViewTextInputPassword,
  127. text_input_get_view(app->text_input_password));
  128. // Set previous callback for TextInput views to return to Configure screen
  129. view_set_previous_callback(
  130. text_input_get_view(app->text_input_path),
  131. web_crawler_back_to_configure_callback);
  132. view_set_previous_callback(
  133. text_input_get_view(app->text_input_ssid),
  134. web_crawler_back_to_configure_callback);
  135. view_set_previous_callback(
  136. text_input_get_view(app->text_input_password),
  137. web_crawler_back_to_configure_callback);
  138. // Allocate Configuration screen
  139. app->variable_item_list_config = variable_item_list_alloc();
  140. if (!app->variable_item_list_config)
  141. {
  142. free_all(app, "Failed to allocate VariableItemList for Configuration");
  143. return NULL;
  144. }
  145. variable_item_list_reset(app->variable_item_list_config);
  146. // Add "Path" item to the configuration screen
  147. app->path_item = variable_item_list_add(
  148. app->variable_item_list_config,
  149. "Path",
  150. 1, // Number of possible values (1 for a single text value)
  151. NULL, // No change callback needed
  152. NULL // No context needed
  153. );
  154. if (!app->path_item)
  155. {
  156. free_all(app, "Failed to add Path item to VariableItemList");
  157. return NULL;
  158. }
  159. variable_item_set_current_value_text(app->path_item, ""); // Initialize
  160. // Add "SSID" item to the configuration screen
  161. app->ssid_item = variable_item_list_add(
  162. app->variable_item_list_config,
  163. "SSID",
  164. 1, // Number of possible values (1 for a single text value)
  165. NULL, // No change callback needed
  166. NULL // No context needed
  167. );
  168. if (!app->ssid_item)
  169. {
  170. free_all(app, "Failed to add SSID item to VariableItemList");
  171. return NULL;
  172. }
  173. variable_item_set_current_value_text(app->ssid_item, ""); // Initialize
  174. // Add "Password" item to the configuration screen
  175. app->password_item = variable_item_list_add(
  176. app->variable_item_list_config,
  177. "Password",
  178. 1, // Number of possible values (1 for a single text value)
  179. NULL, // No change callback needed
  180. NULL // No context needed
  181. );
  182. if (!app->password_item)
  183. {
  184. free_all(app, "Failed to add Password item to VariableItemList");
  185. return NULL;
  186. }
  187. variable_item_set_current_value_text(app->password_item, ""); // Initialize
  188. // Set a single enter callback for all configuration items
  189. variable_item_list_set_enter_callback(
  190. app->variable_item_list_config,
  191. web_crawler_config_enter_callback,
  192. app);
  193. // Set previous callback for configuration screen
  194. view_set_previous_callback(
  195. variable_item_list_get_view(app->variable_item_list_config),
  196. web_crawler_back_to_main_callback);
  197. // Add Configuration view to ViewDispatcher
  198. view_dispatcher_add_view(
  199. app->view_dispatcher,
  200. WebCrawlerViewConfigure,
  201. variable_item_list_get_view(app->variable_item_list_config));
  202. // Allocate Submenu view
  203. app->submenu = submenu_alloc();
  204. if (!app->submenu)
  205. {
  206. free_all(app, "Failed to allocate Submenu");
  207. return NULL;
  208. }
  209. // Add items to Submenu
  210. submenu_add_item(app->submenu, "Run", WebCrawlerSubmenuIndexRun, web_crawler_submenu_callback, app);
  211. submenu_add_item(app->submenu, "About", WebCrawlerSubmenuIndexAbout, web_crawler_submenu_callback, app);
  212. submenu_add_item(app->submenu, "Configure", WebCrawlerSubmenuIndexSetPath, web_crawler_submenu_callback, app);
  213. // Set previous callback for Submenu
  214. view_set_previous_callback(submenu_get_view(app->submenu), web_crawler_exit_app_callback);
  215. // Initialize UART
  216. uart_init();
  217. // Add Submenu view to ViewDispatcher
  218. view_dispatcher_add_view(
  219. app->view_dispatcher,
  220. WebCrawlerViewSubmenu,
  221. submenu_get_view(app->submenu));
  222. // Allocate Main view
  223. app->view_main = view_alloc();
  224. if (!app->view_main)
  225. {
  226. free_all(app, "Failed to allocate Main view");
  227. return NULL;
  228. }
  229. view_set_draw_callback(app->view_main, web_crawler_view_draw_callback);
  230. view_set_input_callback(app->view_main, web_crawler_view_input_callback);
  231. view_set_previous_callback(app->view_main, web_crawler_back_to_main_callback);
  232. // Allocate and initialize the main view's model
  233. view_allocate_model(app->view_main, ViewModelTypeLockFree, sizeof(WebCrawlerMainModel));
  234. WebCrawlerMainModel *main_model = (WebCrawlerMainModel *)view_get_model(app->view_main);
  235. if (main_model)
  236. {
  237. strncpy(main_model->path, "", sizeof(main_model->path) - 1); // Initialize to empty
  238. strncpy(main_model->ssid, "", sizeof(main_model->ssid) - 1); // Initialize to empty
  239. strncpy(main_model->password, "", sizeof(main_model->password) - 1); // Initialize to empty
  240. main_model->path[sizeof(main_model->path) - 1] = '\0';
  241. main_model->ssid[sizeof(main_model->ssid) - 1] = '\0';
  242. main_model->password[sizeof(main_model->password) - 1] = '\0';
  243. }
  244. else
  245. {
  246. free_all(app, "Failed to allocate main view model");
  247. return NULL;
  248. }
  249. // Add Main view to ViewDispatcher
  250. view_dispatcher_add_view(app->view_dispatcher, WebCrawlerViewMain, app->view_main);
  251. // Allocate About view
  252. app->widget_about = widget_alloc();
  253. if (!app->widget_about)
  254. {
  255. free_all(app, "Failed to allocate About widget");
  256. return NULL;
  257. }
  258. // Add text to About widget
  259. widget_add_text_scroll_element(
  260. app->widget_about,
  261. 0,
  262. 0,
  263. 128,
  264. 64,
  265. "Web Crawler App\n---\nThis is a web crawler app for Flipper Zero.\n---\nVisit github.com/jblanked for more details.\n---\nPress BACK to return.");
  266. // Load settings
  267. if (!load_settings(app->path, app->temp_buffer_size_path, app->ssid, app->temp_buffer_size_ssid, app->password, app->temp_buffer_size_password, app))
  268. {
  269. FURI_LOG_E(TAG, "Failed to load settings");
  270. }
  271. else
  272. {
  273. // Update the main view's model
  274. WebCrawlerMainModel *main_model = (WebCrawlerMainModel *)view_get_model(app->view_main);
  275. if (main_model)
  276. {
  277. strncpy(main_model->path, app->path, sizeof(main_model->path) - 1);
  278. main_model->path[sizeof(main_model->path) - 1] = '\0';
  279. strncpy(main_model->ssid, app->ssid, sizeof(main_model->ssid) - 1);
  280. main_model->ssid[sizeof(main_model->ssid) - 1] = '\0';
  281. strncpy(main_model->password, app->password, sizeof(main_model->password) - 1);
  282. main_model->password[sizeof(main_model->password) - 1] = '\0';
  283. }
  284. }
  285. // Set previous callback for About view
  286. view_set_previous_callback(
  287. widget_get_view(app->widget_about),
  288. web_crawler_back_to_main_callback);
  289. // Add About view to ViewDispatcher
  290. view_dispatcher_add_view(
  291. app->view_dispatcher,
  292. WebCrawlerViewAbout,
  293. widget_get_view(app->widget_about));
  294. // Start with the Submenu view
  295. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenu);
  296. return app;
  297. }