web_crawler_callback.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. // web_crawler_callback.h
  2. static bool sent_http_request = false;
  3. static bool get_success = false;
  4. static bool already_success = false;
  5. static WebCrawlerApp *app_instance = NULL;
  6. // Forward declaration of callback functions
  7. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index);
  8. static void web_crawler_setting_item_headers_clicked(void *context, uint32_t index);
  9. static void web_crawler_setting_item_payload_clicked(void *context, uint32_t index);
  10. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index);
  11. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index);
  12. static void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index);
  13. static void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index);
  14. static void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index);
  15. static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index);
  16. static void web_crawler_http_method_change(VariableItem *item)
  17. {
  18. uint8_t index = variable_item_get_current_value_index(item);
  19. variable_item_set_current_value_text(item, http_method_names[index]);
  20. // save the http method
  21. if (app_instance)
  22. {
  23. strncpy(app_instance->http_method, http_method_names[index], strlen(http_method_names[index]) + 1);
  24. // save the settings
  25. save_settings(
  26. app_instance->path,
  27. app_instance->ssid,
  28. app_instance->password,
  29. app_instance->file_rename,
  30. app_instance->file_type,
  31. app_instance->http_method,
  32. app_instance->headers,
  33. app_instance->payload);
  34. }
  35. }
  36. static void web_crawler_view_draw_callback(Canvas *canvas, void *context)
  37. {
  38. UNUSED(context);
  39. if (!app_instance)
  40. {
  41. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  42. return;
  43. }
  44. if (!canvas)
  45. {
  46. FURI_LOG_E(TAG, "Canvas is NULL");
  47. return;
  48. }
  49. canvas_clear(canvas);
  50. canvas_set_font(canvas, FontSecondary);
  51. if (fhttp.state == INACTIVE)
  52. {
  53. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  54. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  55. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  56. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  57. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  58. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  59. return;
  60. }
  61. if (app_instance->path)
  62. {
  63. if (!sent_http_request)
  64. {
  65. if (strstr(app_instance->http_method, "GET") != NULL)
  66. {
  67. canvas_draw_str(canvas, 0, 10, "Sending GET request...");
  68. fhttp.is_bytes_request = false;
  69. // Perform GET request and handle the response
  70. if (app_instance->headers == NULL || app_instance->headers[0] == '\0' || strstr(app_instance->headers, " ") == NULL)
  71. {
  72. get_success = flipper_http_get_request(app_instance->path);
  73. }
  74. else
  75. {
  76. get_success = flipper_http_get_request_with_headers(app_instance->path, app_instance->headers);
  77. }
  78. }
  79. else if (strstr(app_instance->http_method, "POST") != NULL)
  80. {
  81. canvas_draw_str(canvas, 0, 10, "Sending POST request...");
  82. fhttp.is_bytes_request = false;
  83. // Perform POST request and handle the response
  84. get_success = flipper_http_post_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  85. }
  86. else if (strstr(app_instance->http_method, "PUT") != NULL)
  87. {
  88. canvas_draw_str(canvas, 0, 10, "Sending PUT request...");
  89. fhttp.is_bytes_request = false;
  90. // Perform PUT request and handle the response
  91. get_success = flipper_http_put_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  92. }
  93. else if (strstr(app_instance->http_method, "DELETE") != NULL)
  94. {
  95. canvas_draw_str(canvas, 0, 10, "Sending DELETE request...");
  96. fhttp.is_bytes_request = false;
  97. // Perform DELETE request and handle the response
  98. get_success = flipper_http_delete_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  99. }
  100. else
  101. {
  102. // download file
  103. canvas_draw_str(canvas, 0, 10, "Downloading file...");
  104. fhttp.is_bytes_request = true;
  105. // Perform GET request and handle the response
  106. get_success = flipper_http_get_request_bytes(app_instance->path, app_instance->headers);
  107. }
  108. canvas_draw_str(canvas, 0, 20, "Sent!");
  109. if (get_success)
  110. {
  111. canvas_draw_str(canvas, 0, 30, "Receiving data...");
  112. // Set the state to RECEIVING to ensure we continue to see the receiving message
  113. fhttp.state = RECEIVING;
  114. }
  115. else
  116. {
  117. canvas_draw_str(canvas, 0, 30, "Failed.");
  118. }
  119. sent_http_request = true;
  120. }
  121. else
  122. {
  123. // print state
  124. if (get_success && fhttp.state == RECEIVING)
  125. {
  126. canvas_draw_str(canvas, 0, 10, "Receiving and parsing data...");
  127. }
  128. else if (get_success && fhttp.state == IDLE)
  129. {
  130. canvas_draw_str(canvas, 0, 10, "Data saved to file.");
  131. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  132. }
  133. else
  134. {
  135. if (fhttp.state == ISSUE)
  136. {
  137. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  138. {
  139. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  140. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  141. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  142. }
  143. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  144. {
  145. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  146. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  147. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  148. }
  149. else if (strstr(fhttp.last_response, "[ERROR] GET request failed with error: connection refused") != NULL)
  150. {
  151. canvas_draw_str(canvas, 0, 10, "[ERROR] Connection refused.");
  152. canvas_draw_str(canvas, 0, 50, "Choose another URL.");
  153. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  154. }
  155. else
  156. {
  157. canvas_draw_str(canvas, 0, 10, "[ERROR] Failed to sync data.");
  158. canvas_draw_str(canvas, 0, 30, "If this is your third attempt,");
  159. canvas_draw_str(canvas, 0, 40, "it's likely your URL is not");
  160. canvas_draw_str(canvas, 0, 50, "compabilbe or correct.");
  161. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  162. }
  163. }
  164. else
  165. {
  166. canvas_draw_str(canvas, 0, 10, "HTTP request failed.");
  167. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  168. }
  169. get_success = false;
  170. }
  171. }
  172. }
  173. else
  174. {
  175. canvas_draw_str(canvas, 0, 10, "URL not set.");
  176. }
  177. }
  178. /**
  179. * @brief Navigation callback to handle exiting from other views to the submenu.
  180. * @param context The context - WebCrawlerApp object.
  181. * @return WebCrawlerViewSubmenu
  182. */
  183. static uint32_t web_crawler_back_to_configure_callback(void *context)
  184. {
  185. UNUSED(context);
  186. // free file read widget if it exists
  187. if (app_instance->widget_file_read)
  188. {
  189. widget_reset(app_instance->widget_file_read);
  190. }
  191. return WebCrawlerViewSubmenuConfig; // Return to the configure screen
  192. }
  193. /**
  194. * @brief Navigation callback to handle returning to the Wifi Settings screen.
  195. * @param context The context - WebCrawlerApp object.
  196. * @return WebCrawlerViewSubmenu
  197. */
  198. static uint32_t web_crawler_back_to_main_callback(void *context)
  199. {
  200. UNUSED(context);
  201. // reset GET request flags
  202. sent_http_request = false;
  203. get_success = false;
  204. already_success = false;
  205. // free file read widget if it exists
  206. if (app_instance->widget_file_read)
  207. {
  208. widget_reset(app_instance->widget_file_read);
  209. }
  210. return WebCrawlerViewSubmenuMain; // Return to the main submenu
  211. }
  212. static uint32_t web_crawler_back_to_file_callback(void *context)
  213. {
  214. UNUSED(context);
  215. return WebCrawlerViewVariableItemListFile; // Return to the file submenu
  216. }
  217. static uint32_t web_crawler_back_to_wifi_callback(void *context)
  218. {
  219. UNUSED(context);
  220. return WebCrawlerViewVariableItemListWifi; // Return to the wifi submenu
  221. }
  222. static uint32_t web_crawler_back_to_request_callback(void *context)
  223. {
  224. UNUSED(context);
  225. return WebCrawlerViewVariableItemListRequest; // Return to the request submenu
  226. }
  227. /**
  228. * @brief Navigation callback to handle exiting the app from the main submenu.
  229. * @param context The context - unused
  230. * @return VIEW_NONE to exit the app
  231. */
  232. static uint32_t web_crawler_exit_app_callback(void *context)
  233. {
  234. UNUSED(context);
  235. return VIEW_NONE;
  236. }
  237. /**
  238. * @brief Handle submenu item selection.
  239. * @param context The context - WebCrawlerApp object.
  240. * @param index The WebCrawlerSubmenuIndex item that was clicked.
  241. */
  242. static void web_crawler_submenu_callback(void *context, uint32_t index)
  243. {
  244. WebCrawlerApp *app = (WebCrawlerApp *)context;
  245. if (app->view_dispatcher)
  246. {
  247. switch (index)
  248. {
  249. case WebCrawlerSubmenuIndexRun:
  250. sent_http_request = false; // Reset the flag
  251. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewRun);
  252. break;
  253. case WebCrawlerSubmenuIndexAbout:
  254. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewAbout);
  255. break;
  256. case WebCrawlerSubmenuIndexConfig:
  257. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenuConfig);
  258. break;
  259. case WebCrawlerSubmenuIndexWifi:
  260. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  261. break;
  262. case WebCrawlerSubmenuIndexRequest:
  263. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  264. break;
  265. case WebCrawlerSubmenuIndexFile:
  266. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  267. break;
  268. default:
  269. FURI_LOG_E(TAG, "Unknown submenu index");
  270. break;
  271. }
  272. }
  273. }
  274. /**
  275. * @brief Configuration enter callback to handle different items.
  276. * @param context The context - WebCrawlerApp object.
  277. * @param index The index of the item that was clicked.
  278. */
  279. static void web_crawler_wifi_enter_callback(void *context, uint32_t index)
  280. {
  281. switch (index)
  282. {
  283. case 0: // SSID
  284. web_crawler_setting_item_ssid_clicked(context, index);
  285. break;
  286. case 1: // Password
  287. web_crawler_setting_item_password_clicked(context, index);
  288. break;
  289. default:
  290. FURI_LOG_E(TAG, "Unknown configuration item index");
  291. break;
  292. }
  293. }
  294. /**
  295. * @brief Configuration enter callback to handle different items.
  296. * @param context The context - WebCrawlerApp object.
  297. * @param index The index of the item that was clicked.
  298. */
  299. static void web_crawler_file_enter_callback(void *context, uint32_t index)
  300. {
  301. switch (index)
  302. {
  303. case 0: // File Read
  304. web_crawler_setting_item_file_read_clicked(context, index);
  305. break;
  306. case 1: // FIle Type
  307. web_crawler_setting_item_file_type_clicked(context, index);
  308. break;
  309. case 2: // File Rename
  310. web_crawler_setting_item_file_rename_clicked(context, index);
  311. break;
  312. case 3: // File Delete
  313. web_crawler_setting_item_file_delete_clicked(context, index);
  314. break;
  315. default:
  316. FURI_LOG_E(TAG, "Unknown configuration item index");
  317. break;
  318. }
  319. }
  320. /**
  321. * @brief Configuration enter callback to handle different items.
  322. * @param context The context - WebCrawlerApp object.
  323. * @param index The index of the item that was clicked.
  324. */
  325. static void web_crawler_request_enter_callback(void *context, uint32_t index)
  326. {
  327. switch (index)
  328. {
  329. case 0: // URL
  330. web_crawler_setting_item_path_clicked(context, index);
  331. break;
  332. case 1:
  333. // HTTP Method
  334. break;
  335. case 2:
  336. // Headers
  337. web_crawler_setting_item_headers_clicked(context, index);
  338. break;
  339. case 3:
  340. // Payload
  341. web_crawler_setting_item_payload_clicked(context, index);
  342. break;
  343. default:
  344. FURI_LOG_E(TAG, "Unknown configuration item index");
  345. break;
  346. }
  347. }
  348. /**
  349. * @brief Callback for when the user finishes entering the URL.
  350. * @param context The context - WebCrawlerApp object.
  351. */
  352. static void web_crawler_set_path_updated(void *context)
  353. {
  354. WebCrawlerApp *app = (WebCrawlerApp *)context;
  355. if (!app)
  356. {
  357. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  358. return;
  359. }
  360. if (!app->path || !app->temp_buffer_path || !app->temp_buffer_size_path || !app->path_item)
  361. {
  362. FURI_LOG_E(TAG, "Invalid path buffer");
  363. return;
  364. }
  365. // Store the entered URL from temp_buffer_path to path
  366. strncpy(app->path, app->temp_buffer_path, app->temp_buffer_size_path - 1);
  367. if (app->path_item)
  368. {
  369. variable_item_set_current_value_text(app->path_item, app->path);
  370. // Save the URL to the settings
  371. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  372. }
  373. // Return to the Configure view
  374. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  375. }
  376. /**
  377. * @brief Callback for when the user finishes entering the headers
  378. * @param context The context - WebCrawlerApp object.
  379. */
  380. static void web_crawler_set_headers_updated(void *context)
  381. {
  382. WebCrawlerApp *app = (WebCrawlerApp *)context;
  383. if (!app)
  384. {
  385. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  386. return;
  387. }
  388. if (!app->temp_buffer_headers || !app->temp_buffer_size_headers || !app->headers_item)
  389. {
  390. FURI_LOG_E(TAG, "Invalid headers buffer");
  391. return;
  392. }
  393. // Store the entered headers from temp_buffer_headers to headers
  394. strncpy(app->headers, app->temp_buffer_headers, app->temp_buffer_size_headers - 1);
  395. if (app->headers_item)
  396. {
  397. variable_item_set_current_value_text(app->headers_item, app->headers);
  398. // Save the headers to the settings
  399. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  400. }
  401. // Return to the Configure view
  402. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  403. }
  404. /**
  405. * @brief Callback for when the user finishes entering the payload.
  406. * @param context The context - WebCrawlerApp object.
  407. */
  408. static void web_crawler_set_payload_updated(void *context)
  409. {
  410. WebCrawlerApp *app = (WebCrawlerApp *)context;
  411. if (!app)
  412. {
  413. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  414. return;
  415. }
  416. if (!app->temp_buffer_payload || !app->temp_buffer_size_payload || !app->payload_item)
  417. {
  418. FURI_LOG_E(TAG, "Invalid payload buffer");
  419. return;
  420. }
  421. // Store the entered payload from temp_buffer_payload to payload
  422. strncpy(app->payload, app->temp_buffer_payload, app->temp_buffer_size_payload - 1);
  423. if (app->payload_item)
  424. {
  425. variable_item_set_current_value_text(app->payload_item, app->payload);
  426. // Save the payload to the settings
  427. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  428. }
  429. // Return to the Configure view
  430. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  431. }
  432. /**
  433. * @brief Callback for when the user finishes entering the SSID.
  434. * @param context The context - WebCrawlerApp object.
  435. */
  436. static void web_crawler_set_ssid_updated(void *context)
  437. {
  438. WebCrawlerApp *app = (WebCrawlerApp *)context;
  439. if (!app)
  440. {
  441. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  442. return;
  443. }
  444. if (!app->temp_buffer_ssid || !app->temp_buffer_size_ssid || !app->ssid || !app->ssid_item)
  445. {
  446. FURI_LOG_E(TAG, "Invalid SSID buffer");
  447. return;
  448. }
  449. // Store the entered SSID from temp_buffer_ssid to ssid
  450. strncpy(app->ssid, app->temp_buffer_ssid, app->temp_buffer_size_ssid - 1);
  451. if (app->ssid_item)
  452. {
  453. variable_item_set_current_value_text(app->ssid_item, app->ssid);
  454. // Save the SSID to the settings
  455. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  456. // send to UART
  457. if (!flipper_http_save_wifi(app->ssid, app->password))
  458. {
  459. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  460. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  461. }
  462. }
  463. // Return to the Configure view
  464. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  465. }
  466. /**
  467. * @brief Callback for when the user finishes entering the Password.
  468. * @param context The context - WebCrawlerApp object.
  469. */
  470. static void web_crawler_set_password_update(void *context)
  471. {
  472. WebCrawlerApp *app = (WebCrawlerApp *)context;
  473. if (!app)
  474. {
  475. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  476. return;
  477. }
  478. if (!app->temp_buffer_password || !app->temp_buffer_size_password || !app->password || !app->password_item)
  479. {
  480. FURI_LOG_E(TAG, "Invalid password buffer");
  481. return;
  482. }
  483. // Store the entered Password from temp_buffer_password to password
  484. strncpy(app->password, app->temp_buffer_password, app->temp_buffer_size_password - 1);
  485. if (app->password_item)
  486. {
  487. variable_item_set_current_value_text(app->password_item, app->password);
  488. // Save the Password to the settings
  489. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  490. // send to UART
  491. if (!flipper_http_save_wifi(app->ssid, app->password))
  492. {
  493. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  494. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  495. }
  496. }
  497. // Return to the Configure view
  498. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  499. }
  500. /**
  501. * @brief Callback for when the user finishes entering the File Type.
  502. * @param context The context - WebCrawlerApp object.
  503. */
  504. static void web_crawler_set_file_type_update(void *context)
  505. {
  506. WebCrawlerApp *app = (WebCrawlerApp *)context;
  507. if (!app)
  508. {
  509. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  510. return;
  511. }
  512. if (!app->temp_buffer_file_type || !app->temp_buffer_size_file_type || !app->file_type || !app->file_type_item)
  513. {
  514. FURI_LOG_E(TAG, "Invalid file type buffer");
  515. return;
  516. }
  517. // Temporary buffer to store the old name
  518. char old_file_type[256];
  519. strncpy(old_file_type, app->file_type, sizeof(old_file_type) - 1);
  520. old_file_type[sizeof(old_file_type) - 1] = '\0'; // Null-terminate
  521. strncpy(app->file_type, app->temp_buffer_file_type, app->temp_buffer_size_file_type - 1);
  522. if (app->file_type_item)
  523. {
  524. variable_item_set_current_value_text(app->file_type_item, app->file_type);
  525. // Save the File Type to the settings
  526. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  527. }
  528. rename_received_data(app->file_rename, app->file_rename, app->file_type, old_file_type);
  529. // set the file path for fhttp.file_path
  530. if (app->file_rename && app->file_type)
  531. {
  532. char file_path[256];
  533. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  534. file_path[sizeof(file_path) - 1] = '\0'; // Null-terminate
  535. strncpy(fhttp.file_path, file_path, sizeof(fhttp.file_path) - 1);
  536. fhttp.file_path[sizeof(fhttp.file_path) - 1] = '\0'; // Null-terminate
  537. }
  538. // Return to the Configure view
  539. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  540. }
  541. /**
  542. * @brief Callback for when the user finishes entering the File Rename.
  543. * @param context The context - WebCrawlerApp object.
  544. */
  545. static void web_crawler_set_file_rename_update(void *context)
  546. {
  547. WebCrawlerApp *app = (WebCrawlerApp *)context;
  548. if (!app)
  549. {
  550. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  551. return;
  552. }
  553. if (!app->temp_buffer_file_rename || !app->temp_buffer_size_file_rename || !app->file_rename || !app->file_rename_item)
  554. {
  555. FURI_LOG_E(TAG, "Invalid file rename buffer");
  556. return;
  557. }
  558. // Temporary buffer to store the old name
  559. char old_name[256];
  560. // Ensure that app->file_rename is null-terminated
  561. strncpy(old_name, app->file_rename, sizeof(old_name) - 1);
  562. old_name[sizeof(old_name) - 1] = '\0'; // Null-terminate
  563. // Store the entered File Rename from temp_buffer_file_rename to file_rename
  564. strncpy(app->file_rename, app->temp_buffer_file_rename, app->temp_buffer_size_file_rename - 1);
  565. if (app->file_rename_item)
  566. {
  567. variable_item_set_current_value_text(app->file_rename_item, app->file_rename);
  568. // Save the File Rename to the settings
  569. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  570. }
  571. rename_received_data(old_name, app->file_rename, app->file_type, app->file_type);
  572. // set the file path for fhttp.file_path
  573. if (app->file_rename && app->file_type)
  574. {
  575. char file_path[256];
  576. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  577. file_path[sizeof(file_path) - 1] = '\0'; // Null-terminate
  578. strncpy(fhttp.file_path, file_path, sizeof(fhttp.file_path) - 1);
  579. fhttp.file_path[sizeof(fhttp.file_path) - 1] = '\0'; // Null-terminate
  580. }
  581. // Return to the Configure view
  582. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  583. }
  584. /**
  585. * @brief Handler for Path configuration item click.
  586. * @param context The context - WebCrawlerApp object.
  587. * @param index The index of the item that was clicked.
  588. */
  589. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  590. {
  591. WebCrawlerApp *app = (WebCrawlerApp *)context;
  592. if (!app)
  593. {
  594. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  595. return;
  596. }
  597. if (!app->text_input_path)
  598. {
  599. FURI_LOG_E(TAG, "Text input is NULL");
  600. return;
  601. }
  602. UNUSED(index);
  603. // Initialize temp_buffer with existing path
  604. if (app->path && strlen(app->path) > 0)
  605. {
  606. strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  607. }
  608. else
  609. {
  610. strncpy(app->temp_buffer_path, "https://httpbin.org/get", app->temp_buffer_size_path - 1);
  611. }
  612. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  613. // Configure the text input
  614. bool clear_previous_text = false;
  615. uart_text_input_set_result_callback(
  616. app->text_input_path,
  617. web_crawler_set_path_updated,
  618. app,
  619. app->temp_buffer_path,
  620. app->temp_buffer_size_path,
  621. clear_previous_text);
  622. // Set the previous callback to return to Configure screen
  623. view_set_previous_callback(
  624. uart_text_input_get_view(app->text_input_path),
  625. web_crawler_back_to_request_callback);
  626. // Show text input dialog
  627. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInput);
  628. }
  629. /**
  630. * @brief Handler for headers configuration item click.
  631. * @param context The context - WebCrawlerApp object.
  632. * @param index The index of the item that was clicked.
  633. */
  634. static void web_crawler_setting_item_headers_clicked(void *context, uint32_t index)
  635. {
  636. WebCrawlerApp *app = (WebCrawlerApp *)context;
  637. if (!app)
  638. {
  639. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  640. return;
  641. }
  642. UNUSED(index);
  643. if (!app->text_input_headers)
  644. {
  645. FURI_LOG_E(TAG, "Text input is NULL");
  646. return;
  647. }
  648. if (!app->headers)
  649. {
  650. FURI_LOG_E(TAG, "Headers is NULL");
  651. return;
  652. }
  653. if (!app->temp_buffer_headers)
  654. {
  655. FURI_LOG_E(TAG, "Temp buffer headers is NULL");
  656. return;
  657. }
  658. // Initialize temp_buffer with existing headers
  659. if (app->headers && strlen(app->headers) > 0)
  660. {
  661. strncpy(app->temp_buffer_headers, app->headers, app->temp_buffer_size_headers - 1);
  662. }
  663. else
  664. {
  665. strncpy(app->temp_buffer_headers, "{\"Content-Type\":\"application/json\",\"key\":\"value\"}", app->temp_buffer_size_headers - 1);
  666. }
  667. app->temp_buffer_headers[app->temp_buffer_size_headers - 1] = '\0';
  668. // Configure the text input
  669. bool clear_previous_text = false;
  670. uart_text_input_set_result_callback(
  671. app->text_input_headers,
  672. web_crawler_set_headers_updated,
  673. app,
  674. app->temp_buffer_headers,
  675. app->temp_buffer_size_headers,
  676. clear_previous_text);
  677. // Set the previous callback to return to Configure screen
  678. view_set_previous_callback(
  679. uart_text_input_get_view(app->text_input_headers),
  680. web_crawler_back_to_request_callback);
  681. // Show text input dialog
  682. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputHeaders);
  683. }
  684. /**
  685. * @brief Handler for payload configuration item click.
  686. * @param context The context - WebCrawlerApp object.
  687. * @param index The index of the item that was clicked.
  688. */
  689. static void web_crawler_setting_item_payload_clicked(void *context, uint32_t index)
  690. {
  691. WebCrawlerApp *app = (WebCrawlerApp *)context;
  692. if (!app)
  693. {
  694. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  695. return;
  696. }
  697. UNUSED(index);
  698. if (!app->text_input_payload)
  699. {
  700. FURI_LOG_E(TAG, "Text input is NULL");
  701. return;
  702. }
  703. // Initialize temp_buffer with existing payload
  704. if (app->payload && strlen(app->payload) > 0)
  705. {
  706. strncpy(app->temp_buffer_payload, app->payload, app->temp_buffer_size_payload - 1);
  707. }
  708. else
  709. {
  710. strncpy(app->temp_buffer_payload, "{\"key\":\"value\"}", app->temp_buffer_size_payload - 1);
  711. }
  712. app->temp_buffer_payload[app->temp_buffer_size_payload - 1] = '\0';
  713. // Configure the text input
  714. bool clear_previous_text = false;
  715. uart_text_input_set_result_callback(
  716. app->text_input_payload,
  717. web_crawler_set_payload_updated,
  718. app,
  719. app->temp_buffer_payload,
  720. app->temp_buffer_size_payload,
  721. clear_previous_text);
  722. // Set the previous callback to return to Configure screen
  723. view_set_previous_callback(
  724. uart_text_input_get_view(app->text_input_payload),
  725. web_crawler_back_to_request_callback);
  726. // Show text input dialog
  727. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPayload);
  728. }
  729. /**
  730. * @brief Handler for SSID configuration item click.
  731. * @param context The context - WebCrawlerApp object.
  732. * @param index The index of the item that was clicked.
  733. */
  734. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  735. {
  736. WebCrawlerApp *app = (WebCrawlerApp *)context;
  737. if (!app)
  738. {
  739. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  740. return;
  741. }
  742. UNUSED(index);
  743. if (!app->text_input_ssid)
  744. {
  745. FURI_LOG_E(TAG, "Text input is NULL");
  746. return;
  747. }
  748. // Initialize temp_buffer with existing SSID
  749. if (app->ssid && strlen(app->ssid) > 0)
  750. {
  751. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  752. }
  753. else
  754. {
  755. strncpy(app->temp_buffer_ssid, "", app->temp_buffer_size_ssid - 1);
  756. }
  757. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  758. // Configure the text input
  759. bool clear_previous_text = false;
  760. uart_text_input_set_result_callback(
  761. app->text_input_ssid,
  762. web_crawler_set_ssid_updated,
  763. app,
  764. app->temp_buffer_ssid,
  765. app->temp_buffer_size_ssid,
  766. clear_previous_text);
  767. // Set the previous callback to return to Configure screen
  768. view_set_previous_callback(
  769. uart_text_input_get_view(app->text_input_ssid),
  770. web_crawler_back_to_wifi_callback);
  771. // Show text input dialog
  772. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputSSID);
  773. }
  774. /**
  775. * @brief Handler for Password configuration item click.
  776. * @param context The context - WebCrawlerApp object.
  777. * @param index The index of the item that was clicked.
  778. */
  779. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  780. {
  781. WebCrawlerApp *app = (WebCrawlerApp *)context;
  782. if (!app)
  783. {
  784. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  785. return;
  786. }
  787. UNUSED(index);
  788. if (!app->text_input_password)
  789. {
  790. FURI_LOG_E(TAG, "Text input is NULL");
  791. return;
  792. }
  793. // Initialize temp_buffer with existing password
  794. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  795. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  796. // Configure the text input
  797. bool clear_previous_text = false;
  798. uart_text_input_set_result_callback(
  799. app->text_input_password,
  800. web_crawler_set_password_update,
  801. app,
  802. app->temp_buffer_password,
  803. app->temp_buffer_size_password,
  804. clear_previous_text);
  805. // Set the previous callback to return to Configure screen
  806. view_set_previous_callback(
  807. uart_text_input_get_view(app->text_input_password),
  808. web_crawler_back_to_wifi_callback);
  809. // Show text input dialog
  810. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPassword);
  811. }
  812. /**
  813. * @brief Handler for File Type configuration item click.
  814. * @param context The context - WebCrawlerApp object.
  815. * @param index The index of the item that was clicked.
  816. */
  817. static void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index)
  818. {
  819. WebCrawlerApp *app = (WebCrawlerApp *)context;
  820. if (!app)
  821. {
  822. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  823. return;
  824. }
  825. UNUSED(index);
  826. if (!app->text_input_file_type)
  827. {
  828. FURI_LOG_E(TAG, "Text input is NULL");
  829. return;
  830. }
  831. // Initialize temp_buffer with existing file_type
  832. if (app->file_type && strlen(app->file_type) > 0)
  833. {
  834. strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  835. }
  836. else
  837. {
  838. strncpy(app->temp_buffer_file_type, ".txt", app->temp_buffer_size_file_type - 1);
  839. }
  840. app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  841. // Configure the text input
  842. bool clear_previous_text = false;
  843. uart_text_input_set_result_callback(
  844. app->text_input_file_type,
  845. web_crawler_set_file_type_update,
  846. app,
  847. app->temp_buffer_file_type,
  848. app->temp_buffer_size_file_type,
  849. clear_previous_text);
  850. // Set the previous callback to return to Configure screen
  851. view_set_previous_callback(
  852. uart_text_input_get_view(app->text_input_file_type),
  853. web_crawler_back_to_file_callback);
  854. // Show text input dialog
  855. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileType);
  856. }
  857. /**
  858. * @brief Handler for File Rename configuration item click.
  859. * @param context The context - WebCrawlerApp object.
  860. * @param index The index of the item that was clicked.
  861. */
  862. static void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index)
  863. {
  864. WebCrawlerApp *app = (WebCrawlerApp *)context;
  865. if (!app)
  866. {
  867. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  868. return;
  869. }
  870. UNUSED(index);
  871. if (!app->text_input_file_rename)
  872. {
  873. FURI_LOG_E(TAG, "Text input is NULL");
  874. return;
  875. }
  876. // Initialize temp_buffer with existing file_rename
  877. if (app->file_rename && strlen(app->file_rename) > 0)
  878. {
  879. strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  880. }
  881. else
  882. {
  883. strncpy(app->temp_buffer_file_rename, "received_data", app->temp_buffer_size_file_rename - 1);
  884. }
  885. app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  886. // Configure the text input
  887. bool clear_previous_text = false;
  888. uart_text_input_set_result_callback(
  889. app->text_input_file_rename,
  890. web_crawler_set_file_rename_update,
  891. app,
  892. app->temp_buffer_file_rename,
  893. app->temp_buffer_size_file_rename,
  894. clear_previous_text);
  895. // Set the previous callback to return to Configure screen
  896. view_set_previous_callback(
  897. uart_text_input_get_view(app->text_input_file_rename),
  898. web_crawler_back_to_file_callback);
  899. // Show text input dialog
  900. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileRename);
  901. }
  902. /**
  903. * @brief Handler for File Delete configuration item click.
  904. * @param context The context - WebCrawlerApp object.
  905. * @param index The index of the item that was clicked.
  906. */
  907. static void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index)
  908. {
  909. WebCrawlerApp *app = (WebCrawlerApp *)context;
  910. if (!app)
  911. {
  912. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  913. return;
  914. }
  915. UNUSED(index);
  916. if (!delete_received_data(app))
  917. {
  918. FURI_LOG_E(TAG, "Failed to delete file");
  919. }
  920. // Set the previous callback to return to Configure screen
  921. view_set_previous_callback(
  922. widget_get_view(app->widget_file_delete),
  923. web_crawler_back_to_file_callback);
  924. // Show text input dialog
  925. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileDelete);
  926. }
  927. static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index)
  928. {
  929. WebCrawlerApp *app = (WebCrawlerApp *)context;
  930. if (!app)
  931. {
  932. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  933. return;
  934. }
  935. UNUSED(index);
  936. if (!load_received_data(app))
  937. {
  938. if (app->widget_file_read)
  939. {
  940. widget_reset(app->widget_file_read);
  941. widget_add_text_scroll_element(
  942. app->widget_file_read,
  943. 0,
  944. 0,
  945. 128,
  946. 64, "File is empty.");
  947. }
  948. }
  949. // Set the previous callback to return to Configure screen
  950. view_set_previous_callback(
  951. widget_get_view(app->widget_file_read),
  952. web_crawler_back_to_file_callback);
  953. // Show text input dialog
  954. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
  955. }