web_crawler_callback.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. #include <callback/web_crawler_callback.h>
  2. #include <storage/storage.h>
  3. #include <html/html_furi.h>
  4. // Below added by Derek Jamison
  5. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  6. #ifdef DEVELOPMENT
  7. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  8. #define DEV_CRASH() furi_crash()
  9. #else
  10. #define FURI_LOG_DEV(tag, format, ...)
  11. #define DEV_CRASH()
  12. #endif
  13. static uint32_t web_crawler_back_to_file_callback(void *context);
  14. static bool alloc_widget(WebCrawlerApp *app, uint32_t view)
  15. {
  16. furi_check(app, "alloc_widget: app is NULL");
  17. if (!app->widget)
  18. {
  19. switch (view)
  20. {
  21. case WebCrawlerViewAbout:
  22. return easy_flipper_set_widget(&app->widget, WebCrawlerViewWidget, "Web Crawler App\n---\nBrowse the web, fetch API data, and more..\n---\nVisit github.com/jblanked for more details.\n---\nPress BACK to return.", web_crawler_back_to_main_callback, &app->view_dispatcher);
  23. case WebCrawlerViewFileRead:
  24. return easy_flipper_set_widget(&app->widget, WebCrawlerViewWidget, "Data will be displayed here.", web_crawler_back_to_file_callback, &app->view_dispatcher);
  25. case WebCrawlerViewFileDelete:
  26. return easy_flipper_set_widget(&app->widget, WebCrawlerViewWidget, "File deleted.", web_crawler_back_to_file_callback, &app->view_dispatcher);
  27. }
  28. }
  29. return false;
  30. }
  31. static void free_widget(WebCrawlerApp *app)
  32. {
  33. if (app->widget)
  34. {
  35. view_dispatcher_remove_view(app->view_dispatcher, WebCrawlerViewWidget);
  36. free(app->widget);
  37. app->widget = NULL;
  38. }
  39. }
  40. static bool alloc_submenu_config(WebCrawlerApp *app)
  41. {
  42. furi_check(app, "alloc_submenu_config: WebCrawlerApp is NULL");
  43. if (app->submenu_config)
  44. {
  45. FURI_LOG_E(TAG, "alloc_submenu_config: Submenu already allocated");
  46. return false;
  47. }
  48. if (easy_flipper_set_submenu(&app->submenu_config, WebCrawlerViewSubmenuConfig, "Settings", web_crawler_back_to_main_callback, &app->view_dispatcher))
  49. {
  50. submenu_add_item(app->submenu_config, "WiFi", WebCrawlerSubmenuIndexWifi, web_crawler_submenu_callback, app);
  51. submenu_add_item(app->submenu_config, "File", WebCrawlerSubmenuIndexFile, web_crawler_submenu_callback, app);
  52. submenu_add_item(app->submenu_config, "Request", WebCrawlerSubmenuIndexRequest, web_crawler_submenu_callback, app);
  53. return true;
  54. }
  55. return false;
  56. }
  57. static void free_submenu_config(WebCrawlerApp *app)
  58. {
  59. furi_check(app, "free_submenu_config: WebCrawlerApp is NULL");
  60. if (app->submenu_config)
  61. {
  62. view_dispatcher_remove_view(app->view_dispatcher, WebCrawlerViewSubmenuConfig);
  63. submenu_free(app->submenu_config);
  64. app->submenu_config = NULL;
  65. }
  66. }
  67. static bool alloc_variable_item_list(WebCrawlerApp *app, uint32_t view)
  68. {
  69. furi_check(app, "alloc_variable_item_list: WebCrawlerApp is NULL");
  70. if (app->variable_item_list)
  71. {
  72. FURI_LOG_E(TAG, "Variable Item List already allocated");
  73. return false;
  74. }
  75. bool settings_loaded = true;
  76. // load settings
  77. char path[128];
  78. char ssid[64];
  79. char password[64];
  80. char file_rename[128];
  81. char file_type[16];
  82. char http_method[16];
  83. char headers[256];
  84. char payload[256];
  85. if (!load_settings(path, 128, ssid, 64, password, 64, file_rename, 128, file_type, 16, http_method, 16, headers, 256, payload, 256, app))
  86. {
  87. FURI_LOG_E(TAG, "Failed to load settings");
  88. settings_loaded = false;
  89. }
  90. switch (view)
  91. {
  92. case WebCrawlerViewVariableItemListWifi:
  93. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, WebCrawlerViewVariableItemList, web_crawler_wifi_enter_callback, web_crawler_back_to_configure_callback, &app->view_dispatcher, app))
  94. {
  95. return false;
  96. }
  97. if (!app->ssid_item)
  98. {
  99. app->ssid_item = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL); // index 0
  100. variable_item_set_current_value_text(app->ssid_item, ""); // Initialize
  101. }
  102. if (!app->password_item)
  103. {
  104. app->password_item = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL); // index 1
  105. variable_item_set_current_value_text(app->password_item, ""); // Initialize
  106. }
  107. if (settings_loaded)
  108. {
  109. variable_item_set_current_value_text(app->ssid_item, ssid);
  110. // variable_item_set_current_value_text(app->password_item, password);
  111. }
  112. else
  113. {
  114. variable_item_set_current_value_text(app->ssid_item, ""); // Initialize
  115. // variable_item_set_current_value_text(app->password_item, "Password"); // Initialize
  116. }
  117. // save for updating temp buffers later
  118. save_char("ssid", ssid);
  119. save_char("password", password);
  120. // strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  121. // app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  122. // strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  123. // app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  124. break;
  125. case WebCrawlerViewVariableItemListFile:
  126. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, WebCrawlerViewVariableItemList, web_crawler_file_enter_callback, web_crawler_back_to_configure_callback, &app->view_dispatcher, app))
  127. {
  128. return false;
  129. }
  130. if (!app->file_read_item)
  131. {
  132. app->file_read_item = variable_item_list_add(app->variable_item_list, "Read File", 0, NULL, NULL); // index 0
  133. variable_item_set_current_value_text(app->file_read_item, ""); // Initialize
  134. }
  135. if (!app->file_type_item)
  136. {
  137. app->file_type_item = variable_item_list_add(app->variable_item_list, "Set File Type", 0, NULL, NULL); // index 1
  138. variable_item_set_current_value_text(app->file_type_item, ""); // Initialize
  139. }
  140. if (!app->file_rename_item)
  141. {
  142. app->file_rename_item = variable_item_list_add(app->variable_item_list, "Rename File", 0, NULL, NULL); // index 2
  143. variable_item_set_current_value_text(app->file_rename_item, ""); // Initialize
  144. }
  145. if (!app->file_delete_item)
  146. {
  147. app->file_delete_item = variable_item_list_add(app->variable_item_list, "Delete File", 0, NULL, NULL); // index 3
  148. variable_item_set_current_value_text(app->file_delete_item, ""); // Initialize
  149. }
  150. if (settings_loaded)
  151. {
  152. variable_item_set_current_value_text(app->file_type_item, file_type);
  153. variable_item_set_current_value_text(app->file_rename_item, file_rename);
  154. }
  155. else
  156. {
  157. variable_item_set_current_value_text(app->file_type_item, ".txt"); // Initialize
  158. variable_item_set_current_value_text(app->file_rename_item, "received_data"); // Initialize
  159. }
  160. // save for updating temp buffers later
  161. save_char("file_type", file_type);
  162. save_char("file_rename", file_rename);
  163. // strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  164. // app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  165. // strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  166. // app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  167. break;
  168. case WebCrawlerViewVariableItemListRequest:
  169. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, WebCrawlerViewVariableItemList, web_crawler_request_enter_callback, web_crawler_back_to_configure_callback, &app->view_dispatcher, app))
  170. {
  171. return false;
  172. }
  173. if (!app->path_item)
  174. {
  175. app->path_item = variable_item_list_add(app->variable_item_list, "Path", 0, NULL, NULL);
  176. variable_item_set_current_value_text(app->path_item, ""); // Initialize
  177. }
  178. if (!app->http_method_item)
  179. {
  180. app->http_method_item = variable_item_list_add(app->variable_item_list, "HTTP Method", 6, web_crawler_http_method_change, app);
  181. variable_item_set_current_value_text(app->http_method_item, ""); // Initialize
  182. variable_item_set_current_value_index(app->http_method_item, 0); // Initialize
  183. }
  184. if (!app->headers_item)
  185. {
  186. app->headers_item = variable_item_list_add(app->variable_item_list, "Headers", 0, NULL, NULL);
  187. variable_item_set_current_value_text(app->headers_item, ""); // Initialize
  188. }
  189. if (!app->payload_item)
  190. {
  191. app->payload_item = variable_item_list_add(app->variable_item_list, "Payload", 0, NULL, NULL);
  192. variable_item_set_current_value_text(app->payload_item, ""); // Initialize
  193. }
  194. //
  195. //
  196. if (settings_loaded)
  197. {
  198. variable_item_set_current_value_text(app->path_item, path);
  199. variable_item_set_current_value_text(app->http_method_item, http_method);
  200. variable_item_set_current_value_text(app->headers_item, headers);
  201. variable_item_set_current_value_text(app->payload_item, payload);
  202. //
  203. variable_item_set_current_value_index(
  204. app->http_method_item,
  205. strstr(http_method, "GET") != NULL ? 0 : strstr(http_method, "POST") != NULL ? 1
  206. : strstr(http_method, "PUT") != NULL ? 2
  207. : strstr(http_method, "DELETE") != NULL ? 3
  208. : strstr(http_method, "DOWNLOAD") != NULL ? 4
  209. : strstr(http_method, "BROWSE") != NULL ? 5
  210. : 0);
  211. }
  212. else
  213. {
  214. variable_item_set_current_value_text(app->path_item, "https://httpbin.org/get"); // Initialize
  215. variable_item_set_current_value_text(app->http_method_item, "GET"); // Initialize
  216. variable_item_set_current_value_text(app->headers_item, "{\n\t\"Content-Type\": \"application/json\"\n}"); // Initialize
  217. variable_item_set_current_value_text(app->payload_item, "{\n\t\"key\": \"value\"\n}"); // Initialize
  218. }
  219. // save for updating temp buffers later
  220. save_char("path", path);
  221. save_char("http_method", http_method);
  222. save_char("headers", headers);
  223. save_char("payload", payload);
  224. // strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  225. // app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  226. // strncpy(app->temp_buffer_http_method, app->http_method, app->temp_buffer_size_http_method - 1);
  227. // app->temp_buffer_http_method[app->temp_buffer_size_http_method - 1] = '\0';
  228. // strncpy(app->temp_buffer_headers, app->headers, app->temp_buffer_size_headers - 1);
  229. // app->temp_buffer_headers[app->temp_buffer_size_headers - 1] = '\0';
  230. // strncpy(app->temp_buffer_payload, app->payload, app->temp_buffer_size_payload - 1);
  231. // app->temp_buffer_payload[app->temp_buffer_size_payload - 1] = '\0';
  232. break;
  233. default:
  234. FURI_LOG_E(TAG, "Invalid view");
  235. return false;
  236. }
  237. return true;
  238. }
  239. static void free_variable_item_list(WebCrawlerApp *app)
  240. {
  241. furi_check(app, "free_variable_item_list: WebCrawlerApp is NULL");
  242. if (app->variable_item_list)
  243. {
  244. view_dispatcher_remove_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  245. variable_item_list_free(app->variable_item_list);
  246. app->variable_item_list = NULL;
  247. }
  248. // check and free variable items
  249. if (app->ssid_item)
  250. {
  251. free(app->ssid_item);
  252. app->ssid_item = NULL;
  253. }
  254. if (app->password_item)
  255. {
  256. free(app->password_item);
  257. app->password_item = NULL;
  258. }
  259. if (app->file_type_item)
  260. {
  261. free(app->file_type_item);
  262. app->file_type_item = NULL;
  263. }
  264. if (app->file_rename_item)
  265. {
  266. free(app->file_rename_item);
  267. app->file_rename_item = NULL;
  268. }
  269. if (app->file_read_item)
  270. {
  271. free(app->file_read_item);
  272. app->file_read_item = NULL;
  273. }
  274. if (app->file_delete_item)
  275. {
  276. free(app->file_delete_item);
  277. app->file_delete_item = NULL;
  278. }
  279. if (app->path_item)
  280. {
  281. free(app->path_item);
  282. app->path_item = NULL;
  283. }
  284. if (app->http_method_item)
  285. {
  286. free(app->http_method_item);
  287. app->http_method_item = NULL;
  288. }
  289. if (app->headers_item)
  290. {
  291. free(app->headers_item);
  292. app->headers_item = NULL;
  293. }
  294. if (app->payload_item)
  295. {
  296. free(app->payload_item);
  297. app->payload_item = NULL;
  298. }
  299. }
  300. static bool alloc_text_input(WebCrawlerApp *app, uint32_t view)
  301. {
  302. furi_check(app, "alloc_text_input: WebCrawlerApp is NULL");
  303. if (app->uart_text_input)
  304. {
  305. FURI_LOG_E(TAG, "Text Input already allocated");
  306. return false;
  307. }
  308. switch (view)
  309. {
  310. case WebCrawlerViewTextInput:
  311. app->temp_buffer_size_path = 128;
  312. if (!easy_flipper_set_buffer(&app->temp_buffer_path, app->temp_buffer_size_path) || !easy_flipper_set_buffer(&app->path, app->temp_buffer_size_path))
  313. {
  314. return false;
  315. }
  316. if (!easy_flipper_set_uart_text_input(&app->uart_text_input, WebCrawlerViewInput, "Enter URL", app->temp_buffer_path, app->temp_buffer_size_path, NULL, web_crawler_back_to_request_callback, &app->view_dispatcher, app))
  317. {
  318. return false;
  319. }
  320. if (load_char("path", app->path, app->temp_buffer_size_path))
  321. {
  322. snprintf(app->temp_buffer_path, app->temp_buffer_size_path, "%s", app->path);
  323. }
  324. break;
  325. case WebCrawlerViewTextInputSSID:
  326. app->temp_buffer_size_ssid = 64;
  327. if (!easy_flipper_set_buffer(&app->temp_buffer_ssid, app->temp_buffer_size_ssid) || !easy_flipper_set_buffer(&app->ssid, app->temp_buffer_size_ssid))
  328. {
  329. return false;
  330. }
  331. if (!easy_flipper_set_uart_text_input(&app->uart_text_input, WebCrawlerViewInput, "Enter SSID", app->temp_buffer_ssid, app->temp_buffer_size_ssid, NULL, web_crawler_back_to_wifi_callback, &app->view_dispatcher, app))
  332. {
  333. return false;
  334. }
  335. if (load_char("ssid", app->ssid, app->temp_buffer_size_ssid))
  336. {
  337. snprintf(app->temp_buffer_ssid, app->temp_buffer_size_ssid, "%s", app->ssid);
  338. }
  339. break;
  340. case WebCrawlerViewTextInputPassword:
  341. app->temp_buffer_size_password = 64;
  342. if (!easy_flipper_set_buffer(&app->temp_buffer_password, app->temp_buffer_size_password) || !easy_flipper_set_buffer(&app->password, app->temp_buffer_size_password))
  343. {
  344. return false;
  345. }
  346. if (!easy_flipper_set_uart_text_input(&app->uart_text_input, WebCrawlerViewInput, "Enter Password", app->temp_buffer_password, app->temp_buffer_size_password, NULL, web_crawler_back_to_wifi_callback, &app->view_dispatcher, app))
  347. {
  348. return false;
  349. }
  350. if (load_char("password", app->password, app->temp_buffer_size_password))
  351. {
  352. snprintf(app->temp_buffer_password, app->temp_buffer_size_password, "%s", app->password);
  353. }
  354. break;
  355. case WebCrawlerViewTextInputFileType:
  356. app->temp_buffer_size_file_type = 16;
  357. if (!easy_flipper_set_buffer(&app->temp_buffer_file_type, app->temp_buffer_size_file_type) || !easy_flipper_set_buffer(&app->file_type, app->temp_buffer_size_file_type))
  358. {
  359. return false;
  360. }
  361. if (!easy_flipper_set_uart_text_input(&app->uart_text_input, WebCrawlerViewInput, "Enter File Type", app->temp_buffer_file_type, app->temp_buffer_size_file_type, NULL, web_crawler_back_to_file_callback, &app->view_dispatcher, app))
  362. {
  363. return false;
  364. }
  365. if (load_char("file_type", app->file_type, app->temp_buffer_size_file_type))
  366. {
  367. snprintf(app->temp_buffer_file_type, app->temp_buffer_size_file_type, "%s", app->file_type);
  368. }
  369. break;
  370. case WebCrawlerViewTextInputFileRename:
  371. app->temp_buffer_size_file_rename = 128;
  372. if (!easy_flipper_set_buffer(&app->temp_buffer_file_rename, app->temp_buffer_size_file_rename) || !easy_flipper_set_buffer(&app->file_rename, app->temp_buffer_size_file_rename))
  373. {
  374. return false;
  375. }
  376. if (!easy_flipper_set_uart_text_input(&app->uart_text_input, WebCrawlerViewInput, "Enter File Rename", app->temp_buffer_file_rename, app->temp_buffer_size_file_rename, NULL, web_crawler_back_to_file_callback, &app->view_dispatcher, app))
  377. {
  378. return false;
  379. }
  380. if (load_char("file_rename", app->file_rename, app->temp_buffer_size_file_rename))
  381. {
  382. snprintf(app->temp_buffer_file_rename, app->temp_buffer_size_file_rename, "%s", app->file_rename);
  383. }
  384. break;
  385. case WebCrawlerViewTextInputHeaders:
  386. app->temp_buffer_size_headers = 256;
  387. if (!easy_flipper_set_buffer(&app->temp_buffer_headers, app->temp_buffer_size_headers) || !easy_flipper_set_buffer(&app->headers, app->temp_buffer_size_headers))
  388. {
  389. return false;
  390. }
  391. if (!easy_flipper_set_uart_text_input(&app->uart_text_input, WebCrawlerViewInput, "Enter Headers", app->temp_buffer_headers, app->temp_buffer_size_headers, NULL, web_crawler_back_to_request_callback, &app->view_dispatcher, app))
  392. {
  393. return false;
  394. }
  395. if (load_char("headers", app->headers, app->temp_buffer_size_headers))
  396. {
  397. snprintf(app->temp_buffer_headers, app->temp_buffer_size_headers, "%s", app->headers);
  398. }
  399. break;
  400. case WebCrawlerViewTextInputPayload:
  401. app->temp_buffer_size_payload = 256;
  402. if (!easy_flipper_set_buffer(&app->temp_buffer_payload, app->temp_buffer_size_payload) || !easy_flipper_set_buffer(&app->payload, app->temp_buffer_size_payload))
  403. {
  404. return false;
  405. }
  406. if (!easy_flipper_set_uart_text_input(&app->uart_text_input, WebCrawlerViewInput, "Enter Payload", app->temp_buffer_payload, app->temp_buffer_size_payload, NULL, web_crawler_back_to_request_callback, &app->view_dispatcher, app))
  407. {
  408. return false;
  409. }
  410. if (load_char("payload", app->payload, app->temp_buffer_size_payload))
  411. {
  412. snprintf(app->temp_buffer_payload, app->temp_buffer_size_payload, "%s", app->payload);
  413. }
  414. break;
  415. default:
  416. FURI_LOG_E(TAG, "Invalid view");
  417. return false;
  418. }
  419. return true;
  420. }
  421. static void free_text_input(WebCrawlerApp *app)
  422. {
  423. furi_check(app, "free_text_input: WebCrawlerApp is NULL");
  424. if (app->uart_text_input)
  425. {
  426. view_dispatcher_remove_view(app->view_dispatcher, WebCrawlerViewInput);
  427. uart_text_input_free(app->uart_text_input);
  428. app->uart_text_input = NULL;
  429. }
  430. // check and free path
  431. if (app->temp_buffer_path)
  432. {
  433. free(app->temp_buffer_path);
  434. app->temp_buffer_path = NULL;
  435. }
  436. if (app->path)
  437. {
  438. free(app->path);
  439. app->path = NULL;
  440. }
  441. // check and free ssid
  442. if (app->temp_buffer_ssid)
  443. {
  444. free(app->temp_buffer_ssid);
  445. app->temp_buffer_ssid = NULL;
  446. }
  447. if (app->ssid)
  448. {
  449. free(app->ssid);
  450. app->ssid = NULL;
  451. }
  452. // check and free password
  453. if (app->temp_buffer_password)
  454. {
  455. free(app->temp_buffer_password);
  456. app->temp_buffer_password = NULL;
  457. }
  458. if (app->password)
  459. {
  460. free(app->password);
  461. app->password = NULL;
  462. }
  463. // check and free file type
  464. if (app->temp_buffer_file_type)
  465. {
  466. free(app->temp_buffer_file_type);
  467. app->temp_buffer_file_type = NULL;
  468. }
  469. if (app->file_type)
  470. {
  471. free(app->file_type);
  472. app->file_type = NULL;
  473. }
  474. // check and free file rename
  475. if (app->temp_buffer_file_rename)
  476. {
  477. free(app->temp_buffer_file_rename);
  478. app->temp_buffer_file_rename = NULL;
  479. }
  480. if (app->file_rename)
  481. {
  482. free(app->file_rename);
  483. app->file_rename = NULL;
  484. }
  485. // check and free headers
  486. if (app->temp_buffer_headers)
  487. {
  488. free(app->temp_buffer_headers);
  489. app->temp_buffer_headers = NULL;
  490. }
  491. if (app->headers)
  492. {
  493. free(app->headers);
  494. app->headers = NULL;
  495. }
  496. // check and free payload
  497. if (app->temp_buffer_payload)
  498. {
  499. free(app->temp_buffer_payload);
  500. app->temp_buffer_payload = NULL;
  501. }
  502. if (app->payload)
  503. {
  504. free(app->payload);
  505. app->payload = NULL;
  506. }
  507. }
  508. void free_all(WebCrawlerApp *app)
  509. {
  510. furi_check(app, "free_all: app is NULL");
  511. free_widget(app);
  512. free_submenu_config(app);
  513. free_variable_item_list(app);
  514. free_text_input(app);
  515. }
  516. static void web_crawler_draw_error(Canvas *canvas, DataLoaderModel *model)
  517. {
  518. furi_check(model, "web_crawler_draw_error: DataLoaderModel is NULL");
  519. furi_check(model->fhttp, "web_crawler_draw_error: FlipperHTTP is NULL");
  520. furi_check(canvas, "Canvas is NULL");
  521. canvas_clear(canvas);
  522. canvas_set_font(canvas, FontSecondary);
  523. if (model->fhttp->state == INACTIVE)
  524. {
  525. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  526. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  527. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  528. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  529. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  530. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  531. return;
  532. }
  533. if (model->fhttp->last_response)
  534. {
  535. if (strstr(model->fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  536. {
  537. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  538. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  539. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  540. return;
  541. }
  542. if (strstr(model->fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  543. {
  544. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  545. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  546. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  547. return;
  548. }
  549. if (strstr(model->fhttp->last_response, "[ERROR] GET request failed with error: connection refused") != NULL)
  550. {
  551. canvas_draw_str(canvas, 0, 10, "[ERROR] Connection refused.");
  552. canvas_draw_str(canvas, 0, 50, "Choose another URL.");
  553. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  554. return;
  555. }
  556. if (strstr(model->fhttp->last_response, "[PONG]") != NULL)
  557. {
  558. canvas_clear(canvas);
  559. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  560. return;
  561. }
  562. canvas_draw_str(canvas, 0, 10, "[ERROR] Failed to sync data.");
  563. canvas_draw_str(canvas, 0, 30, "If this is your third attempt,");
  564. canvas_draw_str(canvas, 0, 40, "it's likely your URL is not");
  565. canvas_draw_str(canvas, 0, 50, "compabilbe or correct.");
  566. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  567. return;
  568. }
  569. canvas_draw_str(canvas, 0, 10, "HTTP request failed.");
  570. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  571. }
  572. static void save_simply()
  573. {
  574. char path[128];
  575. char ssid[64];
  576. char password[64];
  577. char file_rename[128];
  578. char file_type[16];
  579. char http_method[16];
  580. char headers[256];
  581. char payload[256];
  582. if (load_char("path", path, 128) &&
  583. load_char("ssid", ssid, 64) &&
  584. load_char("password", password, 64) &&
  585. load_char("file_rename", file_rename, 128) &&
  586. load_char("file_type", file_type, 16) &&
  587. load_char("http_method", http_method, 16) &&
  588. load_char("headers", headers, 256) &&
  589. load_char("payload", payload, 256))
  590. {
  591. save_settings(path, ssid, password, file_rename, file_type, http_method, headers, payload);
  592. }
  593. }
  594. void web_crawler_http_method_change(VariableItem *item)
  595. {
  596. WebCrawlerApp *app = (WebCrawlerApp *)variable_item_get_context(item);
  597. furi_check(app, "web_crawler_http_method_change: WebCrawlerApp is NULL");
  598. uint8_t index = variable_item_get_current_value_index(item);
  599. variable_item_set_current_value_text(item, http_method_names[index]);
  600. variable_item_set_current_value_index(item, index);
  601. // save the http method
  602. save_char("http_method", http_method_names[index]);
  603. save_simply();
  604. }
  605. static bool web_crawler_fetch(DataLoaderModel *model)
  606. {
  607. WebCrawlerApp *app = (WebCrawlerApp *)model->parser_context;
  608. furi_check(app, "web_crawler_fetch: WebCrawlerApp is NULL");
  609. furi_check(model->fhttp, "web_crawler_fetch: FlipperHTTP is NULL");
  610. char url[128];
  611. if (!load_char("path", url, 128))
  612. {
  613. return false;
  614. }
  615. char file_type[16];
  616. if (!load_char("file_type", file_type, 16))
  617. {
  618. return false;
  619. }
  620. char file_rename[128];
  621. if (!load_char("file_rename", file_rename, 128))
  622. {
  623. return false;
  624. }
  625. char http_method[16];
  626. if (!load_char("http_method", http_method, 16))
  627. {
  628. return false;
  629. }
  630. char headers[256];
  631. if (!load_char("headers", headers, 256))
  632. {
  633. return false;
  634. }
  635. char payload[256];
  636. if (!load_char("payload", payload, 256))
  637. {
  638. return false;
  639. }
  640. if (strlen(file_rename) > 0 && strlen(file_type) > 0)
  641. {
  642. snprintf(
  643. model->fhttp->file_path,
  644. sizeof(model->fhttp->file_path),
  645. STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/%s%s",
  646. file_rename,
  647. file_type);
  648. }
  649. else
  650. {
  651. snprintf(
  652. model->fhttp->file_path,
  653. sizeof(model->fhttp->file_path),
  654. STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/received_data.txt");
  655. }
  656. if (strstr(http_method, "GET") != NULL)
  657. {
  658. model->fhttp->save_received_data = true;
  659. model->fhttp->is_bytes_request = false;
  660. // Perform GET request and handle the response
  661. if (strlen(headers) == 0)
  662. {
  663. return flipper_http_get_request(model->fhttp, url);
  664. }
  665. else
  666. {
  667. return flipper_http_get_request_with_headers(model->fhttp, url, headers);
  668. }
  669. }
  670. else if (strstr(http_method, "POST") != NULL)
  671. {
  672. model->fhttp->save_received_data = true;
  673. model->fhttp->is_bytes_request = false;
  674. // Perform POST request and handle the response
  675. return flipper_http_post_request_with_headers(model->fhttp, url, headers, payload);
  676. }
  677. else if (strstr(http_method, "PUT") != NULL)
  678. {
  679. model->fhttp->save_received_data = true;
  680. model->fhttp->is_bytes_request = false;
  681. // Perform PUT request and handle the response
  682. return flipper_http_put_request_with_headers(model->fhttp, url, headers, payload);
  683. }
  684. else if (strstr(http_method, "DELETE") != NULL)
  685. {
  686. model->fhttp->save_received_data = true;
  687. model->fhttp->is_bytes_request = false;
  688. // Perform DELETE request and handle the response
  689. return flipper_http_delete_request_with_headers(model->fhttp, url, headers, payload);
  690. }
  691. else if (strstr(http_method, "DOWNLOAD") != NULL)
  692. {
  693. model->fhttp->save_received_data = false;
  694. model->fhttp->is_bytes_request = true;
  695. // Perform GET request and handle the response
  696. return flipper_http_get_request_bytes(model->fhttp, url, "{\"Content-Type\": \"application/octet-stream\"}");
  697. }
  698. else // BROWSE
  699. {
  700. model->fhttp->save_received_data = false;
  701. model->fhttp->is_bytes_request = true;
  702. // download HTML response since the html could be large
  703. return flipper_http_get_request_bytes(model->fhttp, url, "{\"Content-Type\": \"application/octet-stream\"}");
  704. }
  705. return false;
  706. }
  707. static char *web_crawler_parse(DataLoaderModel *model)
  708. {
  709. UNUSED(model);
  710. // parse HTML response if BROWSE request
  711. char http_method[16];
  712. if (!load_char("http_method", http_method, 16))
  713. {
  714. FURI_LOG_E(TAG, "Failed to load http method");
  715. }
  716. else
  717. {
  718. if (strstr(http_method, "BROWSE") != NULL)
  719. {
  720. // parse HTML then return response
  721. FuriString *returned_data = flipper_http_load_from_file(model->fhttp->file_path);
  722. if (returned_data == NULL || furi_string_size(returned_data) == 0)
  723. {
  724. return "Failed to load HTML response.\nPress BACK to return.";
  725. }
  726. // head is mandatory,
  727. bool head_exists = html_furi_tag_exists("<head>", returned_data, 0);
  728. if (!head_exists)
  729. {
  730. return "Invalid HTML response.\nPress BACK to return.";
  731. }
  732. // optional tags but we'll append them the response in order (title -> h1 -> h2 -> h3 -> p)
  733. bool title_exists = html_furi_tag_exists("<title>", returned_data, 0);
  734. bool h1_exists = html_furi_tag_exists("<h1>", returned_data, 0);
  735. bool h2_exists = html_furi_tag_exists("<h2>", returned_data, 0);
  736. bool h3_exists = html_furi_tag_exists("<h3>", returned_data, 0);
  737. bool p_exists = html_furi_tag_exists("<p>", returned_data, 0);
  738. FuriString *response = furi_string_alloc();
  739. if (title_exists)
  740. {
  741. FuriString *title = html_furi_find_tag("<title>", returned_data, 0);
  742. furi_string_cat_str(response, "Title: ");
  743. furi_string_cat(response, title);
  744. furi_string_cat_str(response, "\n\n");
  745. furi_string_free(title);
  746. }
  747. if (h1_exists)
  748. {
  749. FuriString *h1 = html_furi_find_tag("<h1>", returned_data, 0);
  750. furi_string_cat(response, h1);
  751. furi_string_cat_str(response, "\n\n");
  752. furi_string_free(h1);
  753. }
  754. if (h2_exists)
  755. {
  756. FuriString *h2 = html_furi_find_tag("<h2>", returned_data, 0);
  757. furi_string_cat(response, h2);
  758. furi_string_cat_str(response, "\n");
  759. furi_string_free(h2);
  760. }
  761. if (h3_exists)
  762. {
  763. FuriString *h3 = html_furi_find_tag("<h3>", returned_data, 0);
  764. furi_string_cat(response, h3);
  765. furi_string_cat_str(response, "\n");
  766. furi_string_free(h3);
  767. }
  768. if (p_exists)
  769. {
  770. FuriString *p = html_furi_find_tags("<p>", returned_data);
  771. furi_string_cat(response, p);
  772. furi_string_free(p);
  773. }
  774. furi_string_free(returned_data);
  775. return (char *)furi_string_get_cstr(response);
  776. }
  777. }
  778. return "Data saved to file.\nPress BACK to return.";
  779. }
  780. static void web_crawler_data_switch_to_view(WebCrawlerApp *app)
  781. {
  782. furi_check(app, "web_crawler_data_switch_to_view: WebCrawlerApp is NULL");
  783. // Allocate title on the heap.
  784. char *title = malloc(32);
  785. if (title == NULL)
  786. {
  787. FURI_LOG_E(TAG, "Failed to allocate memory for title");
  788. return; // or handle the error as needed
  789. }
  790. char http_method[16];
  791. if (!load_char("http_method", http_method, sizeof(http_method)))
  792. {
  793. FURI_LOG_E(TAG, "Failed to load http method");
  794. snprintf(title, 32, "Request");
  795. }
  796. else
  797. {
  798. if (strstr(http_method, "GET") != NULL)
  799. {
  800. snprintf(title, 32, "GET Request");
  801. }
  802. else if (strstr(http_method, "POST") != NULL)
  803. {
  804. snprintf(title, 32, "POST Request");
  805. }
  806. else if (strstr(http_method, "PUT") != NULL)
  807. {
  808. snprintf(title, 32, "PUT Request");
  809. }
  810. else if (strstr(http_method, "DELETE") != NULL)
  811. {
  812. snprintf(title, 32, "DELETE Request");
  813. }
  814. else if (strstr(http_method, "DOWNLOAD") != NULL)
  815. {
  816. snprintf(title, 32, "File Download");
  817. }
  818. else if (strstr(http_method, "BROWSE") != NULL)
  819. {
  820. snprintf(title, 32, "Browse URL");
  821. }
  822. else
  823. {
  824. // Provide a default title if no known http method is found.
  825. snprintf(title, 32, "Request");
  826. }
  827. }
  828. web_crawler_generic_switch_to_view(app, title, web_crawler_fetch, web_crawler_parse, 1, web_crawler_back_to_main_callback, WebCrawlerViewLoader);
  829. }
  830. /**
  831. * @brief Navigation callback to handle exiting from other views to the submenu.
  832. * @param context The context - WebCrawlerApp object.
  833. * @return WebCrawlerViewSubmenu
  834. */
  835. uint32_t web_crawler_back_to_configure_callback(void *context)
  836. {
  837. WebCrawlerApp *app = (WebCrawlerApp *)context;
  838. furi_check(app, "web_crawler_back_to_configure_callback: WebCrawlerApp is NULL");
  839. return WebCrawlerViewSubmenuConfig; // Return to the configure screen
  840. }
  841. /**
  842. * @brief Navigation callback to handle returning to the Wifi Settings screen.
  843. * @param context The context - WebCrawlerApp object.
  844. * @return WebCrawlerViewSubmenu
  845. */
  846. uint32_t web_crawler_back_to_main_callback(void *context)
  847. {
  848. WebCrawlerApp *app = (WebCrawlerApp *)context;
  849. furi_check(app, "web_crawler_back_to_main_callback: WebCrawlerApp is NULL");
  850. return WebCrawlerViewSubmenuMain; // Return to the main submenu
  851. }
  852. static uint32_t web_crawler_back_to_file_callback(void *context)
  853. {
  854. UNUSED(context);
  855. return WebCrawlerViewVariableItemList; // Return to the file submenu
  856. }
  857. uint32_t web_crawler_back_to_wifi_callback(void *context)
  858. {
  859. UNUSED(context);
  860. return WebCrawlerViewVariableItemList; // Return to the wifi submenu
  861. }
  862. uint32_t web_crawler_back_to_request_callback(void *context)
  863. {
  864. UNUSED(context);
  865. return WebCrawlerViewVariableItemList; // Return to the request submenu
  866. }
  867. /**
  868. * @brief Navigation callback to handle exiting the app from the main submenu.
  869. * @param context The context - unused
  870. * @return VIEW_NONE to exit the app
  871. */
  872. uint32_t web_crawler_exit_app_callback(void *context)
  873. {
  874. UNUSED(context);
  875. return VIEW_NONE;
  876. }
  877. /**
  878. * @brief Handle submenu item selection.
  879. * @param context The context - WebCrawlerApp object.
  880. * @param index The WebCrawlerSubmenuIndex item that was clicked.
  881. */
  882. void web_crawler_submenu_callback(void *context, uint32_t index)
  883. {
  884. WebCrawlerApp *app = (WebCrawlerApp *)context;
  885. furi_check(app, "WebCrawlerApp is NULL");
  886. if (app->view_dispatcher)
  887. {
  888. switch (index)
  889. {
  890. case WebCrawlerSubmenuIndexRun:
  891. web_crawler_data_switch_to_view(app);
  892. break;
  893. case WebCrawlerSubmenuIndexAbout:
  894. free_all(app);
  895. if (!alloc_widget(app, WebCrawlerViewAbout))
  896. {
  897. FURI_LOG_E(TAG, "Failed to allocate widget");
  898. return;
  899. }
  900. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  901. break;
  902. case WebCrawlerSubmenuIndexConfig:
  903. free_all(app);
  904. if (!alloc_submenu_config(app))
  905. {
  906. FURI_LOG_E(TAG, "Failed to allocate submenu");
  907. return;
  908. }
  909. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenuConfig);
  910. break;
  911. case WebCrawlerSubmenuIndexWifi:
  912. free_variable_item_list(app);
  913. if (!alloc_variable_item_list(app, WebCrawlerViewVariableItemListWifi))
  914. {
  915. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  916. return;
  917. }
  918. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  919. break;
  920. case WebCrawlerSubmenuIndexRequest:
  921. free_variable_item_list(app);
  922. if (!alloc_variable_item_list(app, WebCrawlerViewVariableItemListRequest))
  923. {
  924. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  925. return;
  926. }
  927. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  928. break;
  929. case WebCrawlerSubmenuIndexFile:
  930. free_variable_item_list(app);
  931. if (!alloc_variable_item_list(app, WebCrawlerViewVariableItemListFile))
  932. {
  933. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  934. return;
  935. }
  936. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  937. break;
  938. default:
  939. FURI_LOG_E(TAG, "Unknown submenu index");
  940. break;
  941. }
  942. }
  943. }
  944. /**
  945. * @brief Configuration enter callback to handle different items.
  946. * @param context The context - WebCrawlerApp object.
  947. * @param index The index of the item that was clicked.
  948. */
  949. void web_crawler_wifi_enter_callback(void *context, uint32_t index)
  950. {
  951. WebCrawlerApp *app = (WebCrawlerApp *)context;
  952. furi_check(app, "web_crawler_wifi_enter_callback: WebCrawlerApp is NULL");
  953. switch (index)
  954. {
  955. case 0: // SSID
  956. free_text_input(app);
  957. if (!alloc_text_input(app, WebCrawlerViewTextInputSSID))
  958. {
  959. FURI_LOG_E(TAG, "Failed to allocate text input");
  960. return;
  961. }
  962. web_crawler_setting_item_ssid_clicked(context, index);
  963. break;
  964. case 1: // Password
  965. free_text_input(app);
  966. if (!alloc_text_input(app, WebCrawlerViewTextInputPassword))
  967. {
  968. FURI_LOG_E(TAG, "Failed to allocate text input");
  969. return;
  970. }
  971. web_crawler_setting_item_password_clicked(context, index);
  972. break;
  973. default:
  974. FURI_LOG_E(TAG, "Unknown configuration item index");
  975. break;
  976. }
  977. }
  978. /**
  979. * @brief Configuration enter callback to handle different items.
  980. * @param context The context - WebCrawlerApp object.
  981. * @param index The index of the item that was clicked.
  982. */
  983. void web_crawler_file_enter_callback(void *context, uint32_t index)
  984. {
  985. WebCrawlerApp *app = (WebCrawlerApp *)context;
  986. furi_check(app, "web_crawler_file_enter_callback: WebCrawlerApp is NULL");
  987. switch (index)
  988. {
  989. case 0: // File Read
  990. web_crawler_setting_item_file_read_clicked(context, index);
  991. break;
  992. case 1: // FIle Type
  993. free_text_input(app);
  994. if (!alloc_text_input(app, WebCrawlerViewTextInputFileType))
  995. {
  996. FURI_LOG_E(TAG, "Failed to allocate text input");
  997. return;
  998. }
  999. web_crawler_setting_item_file_type_clicked(context, index);
  1000. break;
  1001. case 2: // File Rename
  1002. free_text_input(app);
  1003. if (!alloc_text_input(app, WebCrawlerViewTextInputFileRename))
  1004. {
  1005. FURI_LOG_E(TAG, "Failed to allocate text input");
  1006. return;
  1007. }
  1008. web_crawler_setting_item_file_rename_clicked(context, index);
  1009. break;
  1010. case 3: // File Delete
  1011. web_crawler_setting_item_file_delete_clicked(context, index);
  1012. break;
  1013. default:
  1014. FURI_LOG_E(TAG, "Unknown configuration item index");
  1015. break;
  1016. }
  1017. }
  1018. /**
  1019. * @brief Configuration enter callback to handle different items.
  1020. * @param context The context - WebCrawlerApp object.
  1021. * @param index The index of the item that was clicked.
  1022. */
  1023. void web_crawler_request_enter_callback(void *context, uint32_t index)
  1024. {
  1025. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1026. furi_check(app, "web_crawler_request_enter_callback: WebCrawlerApp is NULL");
  1027. switch (index)
  1028. {
  1029. case 0: // URL
  1030. free_text_input(app);
  1031. if (!alloc_text_input(app, WebCrawlerViewTextInput))
  1032. {
  1033. FURI_LOG_E(TAG, "Failed to allocate text input");
  1034. return;
  1035. }
  1036. web_crawler_setting_item_path_clicked(context, index);
  1037. break;
  1038. case 1:
  1039. // HTTP Method
  1040. break;
  1041. case 2:
  1042. // Headers
  1043. free_text_input(app);
  1044. if (!alloc_text_input(app, WebCrawlerViewTextInputHeaders))
  1045. {
  1046. FURI_LOG_E(TAG, "Failed to allocate text input");
  1047. return;
  1048. }
  1049. web_crawler_setting_item_headers_clicked(context, index);
  1050. break;
  1051. case 3:
  1052. // Payload
  1053. free_text_input(app);
  1054. if (!alloc_text_input(app, WebCrawlerViewTextInputPayload))
  1055. {
  1056. FURI_LOG_E(TAG, "Failed to allocate text input");
  1057. return;
  1058. }
  1059. web_crawler_setting_item_payload_clicked(context, index);
  1060. break;
  1061. default:
  1062. FURI_LOG_E(TAG, "Unknown configuration item index");
  1063. break;
  1064. }
  1065. }
  1066. /**
  1067. * @brief Callback for when the user finishes entering the URL.
  1068. * @param context The context - WebCrawlerApp object.
  1069. */
  1070. void web_crawler_set_path_updated(void *context)
  1071. {
  1072. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1073. furi_check(app, "WebCrawlerApp is NULL");
  1074. snprintf(app->path, app->temp_buffer_size_path, "%s", app->temp_buffer_path);
  1075. if (app->path_item)
  1076. {
  1077. variable_item_set_current_value_text(app->path_item, app->path);
  1078. }
  1079. save_char("path", app->temp_buffer_path);
  1080. save_simply();
  1081. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1082. }
  1083. /**
  1084. * @brief Callback for when the user finishes entering the headers
  1085. * @param context The context - WebCrawlerApp object.
  1086. */
  1087. void web_crawler_set_headers_updated(void *context)
  1088. {
  1089. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1090. furi_check(app, "WebCrawlerApp is NULL");
  1091. snprintf(app->headers, app->temp_buffer_size_headers, "%s", app->temp_buffer_headers);
  1092. if (app->headers_item)
  1093. {
  1094. variable_item_set_current_value_text(app->headers_item, app->headers);
  1095. }
  1096. save_char("headers", app->temp_buffer_headers);
  1097. save_simply();
  1098. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1099. }
  1100. /**
  1101. * @brief Callback for when the user finishes entering the payload.
  1102. * @param context The context - WebCrawlerApp object.
  1103. */
  1104. void web_crawler_set_payload_updated(void *context)
  1105. {
  1106. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1107. furi_check(app, "WebCrawlerApp is NULL");
  1108. snprintf(app->payload, app->temp_buffer_size_payload, "%s", app->temp_buffer_payload);
  1109. if (app->payload_item)
  1110. {
  1111. variable_item_set_current_value_text(app->payload_item, app->payload);
  1112. }
  1113. save_char("payload", app->temp_buffer_payload);
  1114. save_simply();
  1115. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1116. }
  1117. /**
  1118. * @brief Callback for when the user finishes entering the SSID.
  1119. * @param context The context - WebCrawlerApp object.
  1120. */
  1121. void web_crawler_set_ssid_updated(void *context)
  1122. {
  1123. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1124. furi_check(app, "WebCrawlerApp is NULL");
  1125. FlipperHTTP *fhttp = flipper_http_alloc();
  1126. furi_check(fhttp, "FlipperHTTP is NULL");
  1127. snprintf(app->ssid, app->temp_buffer_size_ssid, "%s", app->temp_buffer_ssid);
  1128. if (app->ssid_item)
  1129. {
  1130. variable_item_set_current_value_text(app->ssid_item, app->ssid);
  1131. // send to UART
  1132. if (!flipper_http_save_wifi(fhttp, app->ssid, app->password))
  1133. {
  1134. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1135. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1136. }
  1137. }
  1138. save_char("ssid", app->temp_buffer_ssid);
  1139. save_simply();
  1140. flipper_http_free(fhttp);
  1141. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1142. }
  1143. /**
  1144. * @brief Callback for when the user finishes entering the Password.
  1145. * @param context The context - WebCrawlerApp object.
  1146. */
  1147. void web_crawler_set_password_update(void *context)
  1148. {
  1149. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1150. furi_check(app, "WebCrawlerApp is NULL");
  1151. FlipperHTTP *fhttp = flipper_http_alloc();
  1152. furi_check(fhttp, "FlipperHTTP is NULL");
  1153. snprintf(app->password, app->temp_buffer_size_password, "%s", app->temp_buffer_password);
  1154. if (app->password_item)
  1155. {
  1156. variable_item_set_current_value_text(app->password_item, app->password);
  1157. // send to UART
  1158. if (!flipper_http_save_wifi(fhttp, app->ssid, app->password))
  1159. {
  1160. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1161. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1162. }
  1163. }
  1164. save_char("password", app->temp_buffer_password);
  1165. save_simply();
  1166. flipper_http_free(fhttp);
  1167. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1168. }
  1169. /**
  1170. * @brief Callback for when the user finishes entering the File Type.
  1171. * @param context The context - WebCrawlerApp object.
  1172. */
  1173. void web_crawler_set_file_type_update(void *context)
  1174. {
  1175. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1176. furi_check(app, "WebCrawlerApp is NULL");
  1177. char old_file_type[16];
  1178. snprintf(old_file_type, sizeof(old_file_type), "%s", app->file_type);
  1179. snprintf(app->file_type, app->temp_buffer_size_file_type, "%s", app->temp_buffer_file_type);
  1180. if (app->file_type_item)
  1181. {
  1182. variable_item_set_current_value_text(app->file_type_item, app->file_type);
  1183. }
  1184. char file_rename[128];
  1185. if (load_char("file_rename", file_rename, 128))
  1186. {
  1187. rename_received_data(file_rename, file_rename, app->file_type, old_file_type);
  1188. }
  1189. save_char("file_type", app->file_type);
  1190. save_simply();
  1191. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1192. }
  1193. /**
  1194. * @brief Callback for when the user finishes entering the File Rename.
  1195. * @param context The context - WebCrawlerApp object.
  1196. */
  1197. void web_crawler_set_file_rename_update(void *context)
  1198. {
  1199. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1200. furi_check(app, "WebCrawlerApp is NULL");
  1201. char old_name[256];
  1202. snprintf(old_name, sizeof(old_name), "%s", app->file_rename);
  1203. snprintf(app->file_rename, app->temp_buffer_size_file_rename, "%s", app->temp_buffer_file_rename);
  1204. if (app->file_rename_item)
  1205. {
  1206. variable_item_set_current_value_text(app->file_rename_item, app->file_rename);
  1207. }
  1208. rename_received_data(old_name, app->file_rename, app->file_type, app->file_type);
  1209. save_char("file_rename", app->file_rename);
  1210. save_simply();
  1211. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1212. }
  1213. /**
  1214. * @brief Handler for Path configuration item click.
  1215. * @param context The context - WebCrawlerApp object.
  1216. * @param index The index of the item that was clicked.
  1217. */
  1218. void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  1219. {
  1220. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1221. furi_check(app, "WebCrawlerApp is NULL");
  1222. if (!app->uart_text_input)
  1223. {
  1224. FURI_LOG_E(TAG, "Text input is NULL");
  1225. return;
  1226. }
  1227. UNUSED(index);
  1228. // Initialize temp_buffer with existing path
  1229. if (app->path && strlen(app->path) > 0)
  1230. {
  1231. strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  1232. }
  1233. else
  1234. {
  1235. strncpy(app->temp_buffer_path, "https://httpbin.org/get", app->temp_buffer_size_path - 1);
  1236. }
  1237. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  1238. // Configure the text input
  1239. bool clear_previous_text = false;
  1240. uart_text_input_set_result_callback(
  1241. app->uart_text_input,
  1242. web_crawler_set_path_updated,
  1243. app,
  1244. app->temp_buffer_path,
  1245. app->temp_buffer_size_path,
  1246. clear_previous_text);
  1247. // Set the previous callback to return to Configure screen
  1248. view_set_previous_callback(
  1249. uart_text_input_get_view(app->uart_text_input),
  1250. web_crawler_back_to_request_callback);
  1251. // Show text input dialog
  1252. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1253. }
  1254. /**
  1255. * @brief Handler for headers configuration item click.
  1256. * @param context The context - WebCrawlerApp object.
  1257. * @param index The index of the item that was clicked.
  1258. */
  1259. void web_crawler_setting_item_headers_clicked(void *context, uint32_t index)
  1260. {
  1261. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1262. furi_check(app, "WebCrawlerApp is NULL");
  1263. UNUSED(index);
  1264. if (!app->uart_text_input)
  1265. {
  1266. FURI_LOG_E(TAG, "Text input is NULL");
  1267. return;
  1268. }
  1269. if (!app->headers)
  1270. {
  1271. FURI_LOG_E(TAG, "Headers is NULL");
  1272. return;
  1273. }
  1274. if (!app->temp_buffer_headers)
  1275. {
  1276. FURI_LOG_E(TAG, "Temp buffer headers is NULL");
  1277. return;
  1278. }
  1279. // Initialize temp_buffer with existing headers
  1280. if (app->headers && strlen(app->headers) > 0)
  1281. {
  1282. strncpy(app->temp_buffer_headers, app->headers, app->temp_buffer_size_headers - 1);
  1283. }
  1284. else
  1285. {
  1286. strncpy(app->temp_buffer_headers, "{\"Content-Type\":\"application/json\",\"key\":\"value\"}", app->temp_buffer_size_headers - 1);
  1287. }
  1288. app->temp_buffer_headers[app->temp_buffer_size_headers - 1] = '\0';
  1289. // Configure the text input
  1290. bool clear_previous_text = false;
  1291. uart_text_input_set_result_callback(
  1292. app->uart_text_input,
  1293. web_crawler_set_headers_updated,
  1294. app,
  1295. app->temp_buffer_headers,
  1296. app->temp_buffer_size_headers,
  1297. clear_previous_text);
  1298. // Set the previous callback to return to Configure screen
  1299. view_set_previous_callback(
  1300. uart_text_input_get_view(app->uart_text_input),
  1301. web_crawler_back_to_request_callback);
  1302. // Show text input dialog
  1303. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1304. }
  1305. /**
  1306. * @brief Handler for payload configuration item click.
  1307. * @param context The context - WebCrawlerApp object.
  1308. * @param index The index of the item that was clicked.
  1309. */
  1310. void web_crawler_setting_item_payload_clicked(void *context, uint32_t index)
  1311. {
  1312. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1313. furi_check(app, "WebCrawlerApp is NULL");
  1314. UNUSED(index);
  1315. if (!app->uart_text_input)
  1316. {
  1317. FURI_LOG_E(TAG, "Text input is NULL");
  1318. return;
  1319. }
  1320. // Initialize temp_buffer with existing payload
  1321. if (app->payload && strlen(app->payload) > 0)
  1322. {
  1323. strncpy(app->temp_buffer_payload, app->payload, app->temp_buffer_size_payload - 1);
  1324. }
  1325. else
  1326. {
  1327. strncpy(app->temp_buffer_payload, "{\"key\":\"value\"}", app->temp_buffer_size_payload - 1);
  1328. }
  1329. app->temp_buffer_payload[app->temp_buffer_size_payload - 1] = '\0';
  1330. // Configure the text input
  1331. bool clear_previous_text = false;
  1332. uart_text_input_set_result_callback(
  1333. app->uart_text_input,
  1334. web_crawler_set_payload_updated,
  1335. app,
  1336. app->temp_buffer_payload,
  1337. app->temp_buffer_size_payload,
  1338. clear_previous_text);
  1339. // Set the previous callback to return to Configure screen
  1340. view_set_previous_callback(
  1341. uart_text_input_get_view(app->uart_text_input),
  1342. web_crawler_back_to_request_callback);
  1343. // Show text input dialog
  1344. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1345. }
  1346. /**
  1347. * @brief Handler for SSID configuration item click.
  1348. * @param context The context - WebCrawlerApp object.
  1349. * @param index The index of the item that was clicked.
  1350. */
  1351. void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  1352. {
  1353. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1354. furi_check(app, "WebCrawlerApp is NULL");
  1355. UNUSED(index);
  1356. if (!app->uart_text_input)
  1357. {
  1358. FURI_LOG_E(TAG, "Text input is NULL");
  1359. return;
  1360. }
  1361. // Initialize temp_buffer with existing SSID
  1362. if (app->ssid && strlen(app->ssid) > 0)
  1363. {
  1364. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  1365. }
  1366. else
  1367. {
  1368. strncpy(app->temp_buffer_ssid, "", app->temp_buffer_size_ssid - 1);
  1369. }
  1370. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  1371. // Configure the text input
  1372. bool clear_previous_text = false;
  1373. uart_text_input_set_result_callback(
  1374. app->uart_text_input,
  1375. web_crawler_set_ssid_updated,
  1376. app,
  1377. app->temp_buffer_ssid,
  1378. app->temp_buffer_size_ssid,
  1379. clear_previous_text);
  1380. // Set the previous callback to return to Configure screen
  1381. view_set_previous_callback(
  1382. uart_text_input_get_view(app->uart_text_input),
  1383. web_crawler_back_to_wifi_callback);
  1384. // Show text input dialog
  1385. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1386. }
  1387. /**
  1388. * @brief Handler for Password configuration item click.
  1389. * @param context The context - WebCrawlerApp object.
  1390. * @param index The index of the item that was clicked.
  1391. */
  1392. void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  1393. {
  1394. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1395. furi_check(app, "WebCrawlerApp is NULL");
  1396. UNUSED(index);
  1397. if (!app->uart_text_input)
  1398. {
  1399. FURI_LOG_E(TAG, "Text input is NULL");
  1400. return;
  1401. }
  1402. // Initialize temp_buffer with existing password
  1403. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  1404. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  1405. // Configure the text input
  1406. bool clear_previous_text = false;
  1407. uart_text_input_set_result_callback(
  1408. app->uart_text_input,
  1409. web_crawler_set_password_update,
  1410. app,
  1411. app->temp_buffer_password,
  1412. app->temp_buffer_size_password,
  1413. clear_previous_text);
  1414. // Set the previous callback to return to Configure screen
  1415. view_set_previous_callback(
  1416. uart_text_input_get_view(app->uart_text_input),
  1417. web_crawler_back_to_wifi_callback);
  1418. // Show text input dialog
  1419. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1420. }
  1421. /**
  1422. * @brief Handler for File Type configuration item click.
  1423. * @param context The context - WebCrawlerApp object.
  1424. * @param index The index of the item that was clicked.
  1425. */
  1426. void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index)
  1427. {
  1428. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1429. furi_check(app, "WebCrawlerApp is NULL");
  1430. UNUSED(index);
  1431. if (!app->uart_text_input)
  1432. {
  1433. FURI_LOG_E(TAG, "Text input is NULL");
  1434. return;
  1435. }
  1436. // Initialize temp_buffer with existing file_type
  1437. if (app->file_type && strlen(app->file_type) > 0)
  1438. {
  1439. strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  1440. }
  1441. else
  1442. {
  1443. strncpy(app->temp_buffer_file_type, ".txt", app->temp_buffer_size_file_type - 1);
  1444. }
  1445. app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  1446. // Configure the text input
  1447. bool clear_previous_text = false;
  1448. uart_text_input_set_result_callback(
  1449. app->uart_text_input,
  1450. web_crawler_set_file_type_update,
  1451. app,
  1452. app->temp_buffer_file_type,
  1453. app->temp_buffer_size_file_type,
  1454. clear_previous_text);
  1455. // Set the previous callback to return to Configure screen
  1456. view_set_previous_callback(
  1457. uart_text_input_get_view(app->uart_text_input),
  1458. web_crawler_back_to_file_callback);
  1459. // Show text input dialog
  1460. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1461. }
  1462. /**
  1463. * @brief Handler for File Rename configuration item click.
  1464. * @param context The context - WebCrawlerApp object.
  1465. * @param index The index of the item that was clicked.
  1466. */
  1467. void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index)
  1468. {
  1469. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1470. furi_check(app, "WebCrawlerApp is NULL");
  1471. UNUSED(index);
  1472. if (!app->uart_text_input)
  1473. {
  1474. FURI_LOG_E(TAG, "Text input is NULL");
  1475. return;
  1476. }
  1477. // Initialize temp_buffer with existing file_rename
  1478. if (app->file_rename && strlen(app->file_rename) > 0)
  1479. {
  1480. strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  1481. }
  1482. else
  1483. {
  1484. strncpy(app->temp_buffer_file_rename, "received_data", app->temp_buffer_size_file_rename - 1);
  1485. }
  1486. app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  1487. // Configure the text input
  1488. bool clear_previous_text = false;
  1489. uart_text_input_set_result_callback(
  1490. app->uart_text_input,
  1491. web_crawler_set_file_rename_update,
  1492. app,
  1493. app->temp_buffer_file_rename,
  1494. app->temp_buffer_size_file_rename,
  1495. clear_previous_text);
  1496. // Set the previous callback to return to Configure screen
  1497. view_set_previous_callback(
  1498. uart_text_input_get_view(app->uart_text_input),
  1499. web_crawler_back_to_file_callback);
  1500. // Show text input dialog
  1501. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1502. }
  1503. /**
  1504. * @brief Handler for File Delete configuration item click.
  1505. * @param context The context - WebCrawlerApp object.
  1506. * @param index The index of the item that was clicked.
  1507. */
  1508. void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index)
  1509. {
  1510. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1511. furi_check(app, "WebCrawlerApp is NULL");
  1512. UNUSED(index);
  1513. free_widget(app);
  1514. if (!alloc_widget(app, WebCrawlerViewFileDelete))
  1515. {
  1516. FURI_LOG_E(TAG, "web_crawler_setting_item_file_delete_clicked: Failed to allocate widget");
  1517. return;
  1518. }
  1519. if (!delete_received_data())
  1520. {
  1521. FURI_LOG_E(TAG, "Failed to delete file");
  1522. }
  1523. // Set the previous callback to return to Configure screen
  1524. view_set_previous_callback(
  1525. widget_get_view(app->widget),
  1526. web_crawler_back_to_file_callback);
  1527. // Show text input dialog
  1528. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  1529. }
  1530. void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index)
  1531. {
  1532. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1533. furi_check(app, "WebCrawlerApp is NULL");
  1534. UNUSED(index);
  1535. free_widget(app);
  1536. if (!alloc_widget(app, WebCrawlerViewFileRead))
  1537. {
  1538. FURI_LOG_E(TAG, "web_crawler_setting_item_file_read_clicked: Failed to allocate widget");
  1539. return;
  1540. }
  1541. widget_reset(app->widget);
  1542. char file_path[256];
  1543. char file_rename[128];
  1544. char file_type[16];
  1545. if (load_char("file_rename", file_rename, 128) && load_char("file_type", file_type, 16))
  1546. {
  1547. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, file_rename, file_type);
  1548. }
  1549. else
  1550. {
  1551. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, "received_data", ".txt");
  1552. }
  1553. // load the received data from the saved file
  1554. FuriString *received_data = flipper_http_load_from_file(file_path);
  1555. if (received_data == NULL)
  1556. {
  1557. FURI_LOG_E(TAG, "Failed to load received data from file.");
  1558. if (app->widget)
  1559. {
  1560. widget_add_text_scroll_element(
  1561. app->widget,
  1562. 0,
  1563. 0,
  1564. 128,
  1565. 64, "File is empty.");
  1566. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  1567. }
  1568. return;
  1569. }
  1570. widget_add_text_scroll_element(app->widget, 0, 0, 128, 64, furi_string_get_cstr(received_data));
  1571. furi_string_free(received_data);
  1572. // Set the previous callback to return to Configure screen
  1573. view_set_previous_callback(
  1574. widget_get_view(app->widget),
  1575. web_crawler_back_to_file_callback);
  1576. // Show text input dialog
  1577. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  1578. }
  1579. static void web_crawler_widget_set_text(char *message, Widget **widget)
  1580. {
  1581. if (widget == NULL)
  1582. {
  1583. FURI_LOG_E(TAG, "flip_weather_set_widget_text - widget is NULL");
  1584. DEV_CRASH();
  1585. return;
  1586. }
  1587. if (message == NULL)
  1588. {
  1589. FURI_LOG_E(TAG, "flip_weather_set_widget_text - message is NULL");
  1590. DEV_CRASH();
  1591. return;
  1592. }
  1593. widget_reset(*widget);
  1594. uint32_t message_length = strlen(message); // Length of the message
  1595. uint32_t i = 0; // Index tracker
  1596. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1597. char *formatted_message; // Buffer to hold the final formatted message
  1598. // Allocate buffer with double the message length plus one for safety
  1599. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1600. {
  1601. return;
  1602. }
  1603. while (i < message_length)
  1604. {
  1605. uint32_t max_line_length = 31; // Maximum characters per line
  1606. uint32_t remaining_length = message_length - i; // Remaining characters
  1607. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1608. // Check for newline character within the current segment
  1609. uint32_t newline_pos = i;
  1610. bool found_newline = false;
  1611. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1612. {
  1613. if (message[newline_pos] == '\n')
  1614. {
  1615. found_newline = true;
  1616. break;
  1617. }
  1618. }
  1619. if (found_newline)
  1620. {
  1621. // If newline found, set line_length up to the newline
  1622. line_length = newline_pos - i;
  1623. }
  1624. // Temporary buffer to hold the current line
  1625. char line[32];
  1626. strncpy(line, message + i, line_length);
  1627. line[line_length] = '\0';
  1628. // If newline was found, skip it for the next iteration
  1629. if (found_newline)
  1630. {
  1631. i += line_length + 1; // +1 to skip the '\n' character
  1632. }
  1633. else
  1634. {
  1635. // Check if the line ends in the middle of a word and adjust accordingly
  1636. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1637. {
  1638. // Find the last space within the current line to avoid breaking a word
  1639. char *last_space = strrchr(line, ' ');
  1640. if (last_space != NULL)
  1641. {
  1642. // Adjust the line_length to avoid cutting the word
  1643. line_length = last_space - line;
  1644. line[line_length] = '\0'; // Null-terminate at the space
  1645. }
  1646. }
  1647. // Move the index forward by the determined line_length
  1648. i += line_length;
  1649. // Skip any spaces at the beginning of the next line
  1650. while (i < message_length && message[i] == ' ')
  1651. {
  1652. i++;
  1653. }
  1654. }
  1655. // Manually copy the fixed line into the formatted_message buffer
  1656. for (uint32_t j = 0; j < line_length; j++)
  1657. {
  1658. formatted_message[formatted_index++] = line[j];
  1659. }
  1660. // Add a newline character for line spacing
  1661. formatted_message[formatted_index++] = '\n';
  1662. }
  1663. // Null-terminate the formatted_message
  1664. formatted_message[formatted_index] = '\0';
  1665. // Add the formatted message to the widget
  1666. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1667. }
  1668. void web_crawler_loader_draw_callback(Canvas *canvas, void *model)
  1669. {
  1670. if (!canvas || !model)
  1671. {
  1672. FURI_LOG_E(TAG, "web_crawler_loader_draw_callback - canvas or model is NULL");
  1673. return;
  1674. }
  1675. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1676. SerialState http_state = data_loader_model->fhttp->state;
  1677. DataState data_state = data_loader_model->data_state;
  1678. char *title = data_loader_model->title;
  1679. canvas_set_font(canvas, FontSecondary);
  1680. if (http_state == INACTIVE)
  1681. {
  1682. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1683. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1684. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1685. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1686. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1687. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1688. return;
  1689. }
  1690. if (data_state == DataStateError || data_state == DataStateParseError)
  1691. {
  1692. web_crawler_draw_error(canvas, data_loader_model);
  1693. return;
  1694. }
  1695. canvas_draw_str(canvas, 0, 7, title);
  1696. canvas_draw_str(canvas, 0, 17, "Loading...");
  1697. if (data_state == DataStateInitial)
  1698. {
  1699. return;
  1700. }
  1701. if (http_state == SENDING)
  1702. {
  1703. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1704. return;
  1705. }
  1706. if (http_state == RECEIVING || data_state == DataStateRequested)
  1707. {
  1708. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1709. return;
  1710. }
  1711. if (http_state == IDLE && data_state == DataStateReceived)
  1712. {
  1713. canvas_draw_str(canvas, 0, 27, "Processing...");
  1714. return;
  1715. }
  1716. if (http_state == IDLE && data_state == DataStateParsed)
  1717. {
  1718. canvas_draw_str(canvas, 0, 27, "Processed...");
  1719. return;
  1720. }
  1721. }
  1722. static void web_crawler_loader_process_callback(void *context)
  1723. {
  1724. if (context == NULL)
  1725. {
  1726. FURI_LOG_E(TAG, "web_crawler_loader_process_callback - context is NULL");
  1727. DEV_CRASH();
  1728. return;
  1729. }
  1730. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1731. View *view = app->view_loader;
  1732. DataState current_data_state;
  1733. DataLoaderModel *loader_model = NULL;
  1734. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1735. if (!loader_model || !loader_model->fhttp)
  1736. {
  1737. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1738. DEV_CRASH();
  1739. return;
  1740. }
  1741. if (current_data_state == DataStateInitial)
  1742. {
  1743. with_view_model(
  1744. view,
  1745. DataLoaderModel * model,
  1746. {
  1747. model->data_state = DataStateRequested;
  1748. DataLoaderFetch fetch = model->fetcher;
  1749. if (fetch == NULL)
  1750. {
  1751. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1752. model->data_state = DataStateError;
  1753. return;
  1754. }
  1755. // Clear any previous responses
  1756. strncpy(model->fhttp->last_response, "", 1);
  1757. bool request_status = fetch(model);
  1758. if (!request_status)
  1759. {
  1760. model->data_state = DataStateError;
  1761. }
  1762. },
  1763. true);
  1764. }
  1765. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1766. {
  1767. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1768. {
  1769. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1770. {
  1771. FURI_LOG_DEV(TAG, "PONG received.");
  1772. }
  1773. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1774. {
  1775. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1776. }
  1777. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1778. {
  1779. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1780. }
  1781. else if (strlen(loader_model->fhttp->last_response) == 0)
  1782. {
  1783. // Still waiting on response
  1784. }
  1785. else
  1786. {
  1787. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1788. }
  1789. }
  1790. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1791. {
  1792. // continue waiting
  1793. }
  1794. else if (loader_model->fhttp->state == INACTIVE)
  1795. {
  1796. // inactive. try again
  1797. }
  1798. else if (loader_model->fhttp->state == ISSUE)
  1799. {
  1800. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1801. }
  1802. else
  1803. {
  1804. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1805. DEV_CRASH();
  1806. }
  1807. }
  1808. else if (current_data_state == DataStateReceived)
  1809. {
  1810. with_view_model(
  1811. view,
  1812. DataLoaderModel * model,
  1813. {
  1814. char *data_text;
  1815. if (model->parser == NULL)
  1816. {
  1817. data_text = NULL;
  1818. FURI_LOG_DEV(TAG, "Parser is NULL");
  1819. DEV_CRASH();
  1820. }
  1821. else
  1822. {
  1823. data_text = model->parser(model);
  1824. }
  1825. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1826. model->data_text = data_text;
  1827. if (data_text == NULL)
  1828. {
  1829. model->data_state = DataStateParseError;
  1830. }
  1831. else
  1832. {
  1833. model->data_state = DataStateParsed;
  1834. }
  1835. },
  1836. true);
  1837. }
  1838. else if (current_data_state == DataStateParsed)
  1839. {
  1840. with_view_model(
  1841. view,
  1842. DataLoaderModel * model,
  1843. {
  1844. if (++model->request_index < model->request_count)
  1845. {
  1846. model->data_state = DataStateInitial;
  1847. }
  1848. else
  1849. {
  1850. web_crawler_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1851. if (model->data_text != NULL)
  1852. {
  1853. free(model->data_text);
  1854. model->data_text = NULL;
  1855. }
  1856. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1857. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidgetResult);
  1858. }
  1859. },
  1860. true);
  1861. }
  1862. }
  1863. static void web_crawler_loader_timer_callback(void *context)
  1864. {
  1865. if (context == NULL)
  1866. {
  1867. FURI_LOG_E(TAG, "web_crawler_loader_timer_callback - context is NULL");
  1868. DEV_CRASH();
  1869. return;
  1870. }
  1871. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1872. view_dispatcher_send_custom_event(app->view_dispatcher, WebCrawlerCustomEventProcess);
  1873. }
  1874. static void web_crawler_loader_on_enter(void *context)
  1875. {
  1876. if (context == NULL)
  1877. {
  1878. FURI_LOG_E(TAG, "web_crawler_loader_on_enter - context is NULL");
  1879. DEV_CRASH();
  1880. return;
  1881. }
  1882. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1883. View *view = app->view_loader;
  1884. with_view_model(
  1885. view,
  1886. DataLoaderModel * model,
  1887. {
  1888. view_set_previous_callback(view, model->back_callback);
  1889. if (model->timer == NULL)
  1890. {
  1891. model->timer = furi_timer_alloc(web_crawler_loader_timer_callback, FuriTimerTypePeriodic, app);
  1892. }
  1893. furi_timer_start(model->timer, 250);
  1894. },
  1895. true);
  1896. }
  1897. static void web_crawler_loader_on_exit(void *context)
  1898. {
  1899. if (context == NULL)
  1900. {
  1901. FURI_LOG_E(TAG, "web_crawler_loader_on_exit - context is NULL");
  1902. DEV_CRASH();
  1903. return;
  1904. }
  1905. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1906. View *view = app->view_loader;
  1907. with_view_model(
  1908. view,
  1909. DataLoaderModel * model,
  1910. {
  1911. if (model->timer)
  1912. {
  1913. furi_timer_stop(model->timer);
  1914. }
  1915. },
  1916. false);
  1917. }
  1918. void web_crawler_loader_init(View *view)
  1919. {
  1920. if (view == NULL)
  1921. {
  1922. FURI_LOG_E(TAG, "web_crawler_loader_init - view is NULL");
  1923. DEV_CRASH();
  1924. return;
  1925. }
  1926. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1927. view_set_enter_callback(view, web_crawler_loader_on_enter);
  1928. view_set_exit_callback(view, web_crawler_loader_on_exit);
  1929. }
  1930. void web_crawler_loader_free_model(View *view)
  1931. {
  1932. if (view == NULL)
  1933. {
  1934. FURI_LOG_E(TAG, "web_crawler_loader_free_model - view is NULL");
  1935. DEV_CRASH();
  1936. return;
  1937. }
  1938. with_view_model(
  1939. view,
  1940. DataLoaderModel * model,
  1941. {
  1942. if (model->timer)
  1943. {
  1944. furi_timer_free(model->timer);
  1945. model->timer = NULL;
  1946. }
  1947. if (model->parser_context)
  1948. {
  1949. // do not free the context here, it is the app context
  1950. // free(model->parser_context);
  1951. // model->parser_context = NULL;
  1952. }
  1953. if (model->fhttp)
  1954. {
  1955. flipper_http_free(model->fhttp);
  1956. model->fhttp = NULL;
  1957. }
  1958. },
  1959. false);
  1960. }
  1961. bool web_crawler_custom_event_callback(void *context, uint32_t index)
  1962. {
  1963. if (context == NULL)
  1964. {
  1965. FURI_LOG_E(TAG, "web_crawler_custom_event_callback - context is NULL");
  1966. DEV_CRASH();
  1967. return false;
  1968. }
  1969. switch (index)
  1970. {
  1971. case WebCrawlerCustomEventProcess:
  1972. web_crawler_loader_process_callback(context);
  1973. return true;
  1974. default:
  1975. FURI_LOG_DEV(TAG, "web_crawler_custom_event_callback. Unknown index: %ld", index);
  1976. return false;
  1977. }
  1978. }
  1979. void web_crawler_generic_switch_to_view(WebCrawlerApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1980. {
  1981. if (app == NULL)
  1982. {
  1983. FURI_LOG_E(TAG, "web_crawler_generic_switch_to_view - app is NULL");
  1984. DEV_CRASH();
  1985. return;
  1986. }
  1987. View *view = app->view_loader;
  1988. if (view == NULL)
  1989. {
  1990. FURI_LOG_E(TAG, "web_crawler_generic_switch_to_view - view is NULL");
  1991. DEV_CRASH();
  1992. return;
  1993. }
  1994. with_view_model(
  1995. view,
  1996. DataLoaderModel * model,
  1997. {
  1998. model->title = title;
  1999. model->fetcher = fetcher;
  2000. model->parser = parser;
  2001. model->request_index = 0;
  2002. model->request_count = request_count;
  2003. model->back_callback = back;
  2004. model->data_state = DataStateInitial;
  2005. model->data_text = NULL;
  2006. //
  2007. model->parser_context = app;
  2008. if (!model->fhttp)
  2009. {
  2010. model->fhttp = flipper_http_alloc();
  2011. }
  2012. },
  2013. true);
  2014. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2015. }