web_crawler_callback.c 37 KB

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