web_crawler_callback.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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, headers);
  697. }
  698. else // BROWSE
  699. {
  700. model->fhttp->save_received_data = true;
  701. model->fhttp->is_bytes_request = false;
  702. // Perform GET request and handle the response
  703. if (strlen(headers) == 0)
  704. {
  705. return flipper_http_get_request(model->fhttp, url);
  706. }
  707. else
  708. {
  709. return flipper_http_get_request_with_headers(model->fhttp, url, headers);
  710. }
  711. }
  712. return false;
  713. }
  714. static char *web_crawler_parse(DataLoaderModel *model)
  715. {
  716. UNUSED(model);
  717. // parse HTML response if BROWSE request
  718. char http_method[16];
  719. if (!load_char("http_method", http_method, 16))
  720. {
  721. FURI_LOG_E(TAG, "Failed to load http method");
  722. }
  723. else
  724. {
  725. if (strstr(http_method, "BROWSE") != NULL)
  726. {
  727. // parse HTML then return response
  728. FuriString *returned_data = flipper_http_load_from_file(model->fhttp->file_path);
  729. if (returned_data == NULL)
  730. {
  731. return "Failed to load HTML response.\nPress BACK to return.";
  732. }
  733. // parse HTML response
  734. FuriString *h1_tag = html_furi_find_tag("<h1>", returned_data, 0);
  735. FuriString *p_tag = html_furi_find_tag("<p>", returned_data, 0);
  736. furi_string_free(returned_data);
  737. if (p_tag == NULL && h1_tag == NULL)
  738. {
  739. return "Failed to find <h1> or <p> tag.\nPress BACK to return.";
  740. }
  741. else if (p_tag && h1_tag)
  742. {
  743. FuriString *combined = furi_string_alloc_printf("%s\n%s", furi_string_get_cstr(h1_tag), furi_string_get_cstr(p_tag));
  744. if (combined)
  745. {
  746. furi_string_free(h1_tag);
  747. furi_string_free(p_tag);
  748. return (char *)furi_string_get_cstr(combined);
  749. }
  750. else
  751. {
  752. furi_string_free(h1_tag);
  753. furi_string_free(p_tag);
  754. return "Failed to combine <h1> and <p> tags.\nPress BACK to return.";
  755. }
  756. }
  757. else if (h1_tag != NULL)
  758. {
  759. furi_string_free(p_tag);
  760. return (char *)furi_string_get_cstr(h1_tag);
  761. }
  762. else if (p_tag != NULL)
  763. {
  764. furi_string_free(h1_tag);
  765. return (char *)furi_string_get_cstr(p_tag);
  766. }
  767. }
  768. }
  769. return "Data saved to file.\nPress BACK to return.";
  770. }
  771. static void web_crawler_data_switch_to_view(WebCrawlerApp *app)
  772. {
  773. furi_check(app, "web_crawler_data_switch_to_view: WebCrawlerApp is NULL");
  774. // Allocate title on the heap.
  775. char *title = malloc(32);
  776. if (title == NULL)
  777. {
  778. FURI_LOG_E(TAG, "Failed to allocate memory for title");
  779. return; // or handle the error as needed
  780. }
  781. char http_method[16];
  782. if (!load_char("http_method", http_method, sizeof(http_method)))
  783. {
  784. FURI_LOG_E(TAG, "Failed to load http method");
  785. snprintf(title, 32, "Request");
  786. }
  787. else
  788. {
  789. if (strstr(http_method, "GET") != NULL)
  790. {
  791. snprintf(title, 32, "GET Request");
  792. }
  793. else if (strstr(http_method, "POST") != NULL)
  794. {
  795. snprintf(title, 32, "POST Request");
  796. }
  797. else if (strstr(http_method, "PUT") != NULL)
  798. {
  799. snprintf(title, 32, "PUT Request");
  800. }
  801. else if (strstr(http_method, "DELETE") != NULL)
  802. {
  803. snprintf(title, 32, "DELETE Request");
  804. }
  805. else if (strstr(http_method, "DOWNLOAD") != NULL)
  806. {
  807. snprintf(title, 32, "File Download");
  808. }
  809. else if (strstr(http_method, "BROWSE") != NULL)
  810. {
  811. snprintf(title, 32, "Browse URL");
  812. }
  813. else
  814. {
  815. // Provide a default title if no known http method is found.
  816. snprintf(title, 32, "Request");
  817. }
  818. }
  819. web_crawler_generic_switch_to_view(app, title, web_crawler_fetch, web_crawler_parse, 1, web_crawler_back_to_main_callback, WebCrawlerViewLoader);
  820. }
  821. /**
  822. * @brief Navigation callback to handle exiting from other views to the submenu.
  823. * @param context The context - WebCrawlerApp object.
  824. * @return WebCrawlerViewSubmenu
  825. */
  826. uint32_t web_crawler_back_to_configure_callback(void *context)
  827. {
  828. WebCrawlerApp *app = (WebCrawlerApp *)context;
  829. furi_check(app, "web_crawler_back_to_configure_callback: WebCrawlerApp is NULL");
  830. return WebCrawlerViewSubmenuConfig; // Return to the configure screen
  831. }
  832. /**
  833. * @brief Navigation callback to handle returning to the Wifi Settings screen.
  834. * @param context The context - WebCrawlerApp object.
  835. * @return WebCrawlerViewSubmenu
  836. */
  837. uint32_t web_crawler_back_to_main_callback(void *context)
  838. {
  839. WebCrawlerApp *app = (WebCrawlerApp *)context;
  840. furi_check(app, "web_crawler_back_to_main_callback: WebCrawlerApp is NULL");
  841. return WebCrawlerViewSubmenuMain; // Return to the main submenu
  842. }
  843. static uint32_t web_crawler_back_to_file_callback(void *context)
  844. {
  845. UNUSED(context);
  846. return WebCrawlerViewVariableItemList; // Return to the file submenu
  847. }
  848. uint32_t web_crawler_back_to_wifi_callback(void *context)
  849. {
  850. UNUSED(context);
  851. return WebCrawlerViewVariableItemList; // Return to the wifi submenu
  852. }
  853. uint32_t web_crawler_back_to_request_callback(void *context)
  854. {
  855. UNUSED(context);
  856. return WebCrawlerViewVariableItemList; // Return to the request submenu
  857. }
  858. /**
  859. * @brief Navigation callback to handle exiting the app from the main submenu.
  860. * @param context The context - unused
  861. * @return VIEW_NONE to exit the app
  862. */
  863. uint32_t web_crawler_exit_app_callback(void *context)
  864. {
  865. UNUSED(context);
  866. return VIEW_NONE;
  867. }
  868. /**
  869. * @brief Handle submenu item selection.
  870. * @param context The context - WebCrawlerApp object.
  871. * @param index The WebCrawlerSubmenuIndex item that was clicked.
  872. */
  873. void web_crawler_submenu_callback(void *context, uint32_t index)
  874. {
  875. WebCrawlerApp *app = (WebCrawlerApp *)context;
  876. furi_check(app, "WebCrawlerApp is NULL");
  877. if (app->view_dispatcher)
  878. {
  879. switch (index)
  880. {
  881. case WebCrawlerSubmenuIndexRun:
  882. web_crawler_data_switch_to_view(app);
  883. break;
  884. case WebCrawlerSubmenuIndexAbout:
  885. free_all(app);
  886. if (!alloc_widget(app, WebCrawlerViewAbout))
  887. {
  888. FURI_LOG_E(TAG, "Failed to allocate widget");
  889. return;
  890. }
  891. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  892. break;
  893. case WebCrawlerSubmenuIndexConfig:
  894. free_all(app);
  895. if (!alloc_submenu_config(app))
  896. {
  897. FURI_LOG_E(TAG, "Failed to allocate submenu");
  898. return;
  899. }
  900. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenuConfig);
  901. break;
  902. case WebCrawlerSubmenuIndexWifi:
  903. free_variable_item_list(app);
  904. if (!alloc_variable_item_list(app, WebCrawlerViewVariableItemListWifi))
  905. {
  906. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  907. return;
  908. }
  909. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  910. break;
  911. case WebCrawlerSubmenuIndexRequest:
  912. free_variable_item_list(app);
  913. if (!alloc_variable_item_list(app, WebCrawlerViewVariableItemListRequest))
  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 WebCrawlerSubmenuIndexFile:
  921. free_variable_item_list(app);
  922. if (!alloc_variable_item_list(app, WebCrawlerViewVariableItemListFile))
  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. default:
  930. FURI_LOG_E(TAG, "Unknown submenu index");
  931. break;
  932. }
  933. }
  934. }
  935. /**
  936. * @brief Configuration enter callback to handle different items.
  937. * @param context The context - WebCrawlerApp object.
  938. * @param index The index of the item that was clicked.
  939. */
  940. void web_crawler_wifi_enter_callback(void *context, uint32_t index)
  941. {
  942. WebCrawlerApp *app = (WebCrawlerApp *)context;
  943. furi_check(app, "web_crawler_wifi_enter_callback: WebCrawlerApp is NULL");
  944. switch (index)
  945. {
  946. case 0: // SSID
  947. free_text_input(app);
  948. if (!alloc_text_input(app, WebCrawlerViewTextInputSSID))
  949. {
  950. FURI_LOG_E(TAG, "Failed to allocate text input");
  951. return;
  952. }
  953. web_crawler_setting_item_ssid_clicked(context, index);
  954. break;
  955. case 1: // Password
  956. free_text_input(app);
  957. if (!alloc_text_input(app, WebCrawlerViewTextInputPassword))
  958. {
  959. FURI_LOG_E(TAG, "Failed to allocate text input");
  960. return;
  961. }
  962. web_crawler_setting_item_password_clicked(context, index);
  963. break;
  964. default:
  965. FURI_LOG_E(TAG, "Unknown configuration item index");
  966. break;
  967. }
  968. }
  969. /**
  970. * @brief Configuration enter callback to handle different items.
  971. * @param context The context - WebCrawlerApp object.
  972. * @param index The index of the item that was clicked.
  973. */
  974. void web_crawler_file_enter_callback(void *context, uint32_t index)
  975. {
  976. WebCrawlerApp *app = (WebCrawlerApp *)context;
  977. furi_check(app, "web_crawler_file_enter_callback: WebCrawlerApp is NULL");
  978. switch (index)
  979. {
  980. case 0: // File Read
  981. web_crawler_setting_item_file_read_clicked(context, index);
  982. break;
  983. case 1: // FIle Type
  984. free_text_input(app);
  985. if (!alloc_text_input(app, WebCrawlerViewTextInputFileType))
  986. {
  987. FURI_LOG_E(TAG, "Failed to allocate text input");
  988. return;
  989. }
  990. web_crawler_setting_item_file_type_clicked(context, index);
  991. break;
  992. case 2: // File Rename
  993. free_text_input(app);
  994. if (!alloc_text_input(app, WebCrawlerViewTextInputFileRename))
  995. {
  996. FURI_LOG_E(TAG, "Failed to allocate text input");
  997. return;
  998. }
  999. web_crawler_setting_item_file_rename_clicked(context, index);
  1000. break;
  1001. case 3: // File Delete
  1002. web_crawler_setting_item_file_delete_clicked(context, index);
  1003. break;
  1004. default:
  1005. FURI_LOG_E(TAG, "Unknown configuration item index");
  1006. break;
  1007. }
  1008. }
  1009. /**
  1010. * @brief Configuration enter callback to handle different items.
  1011. * @param context The context - WebCrawlerApp object.
  1012. * @param index The index of the item that was clicked.
  1013. */
  1014. void web_crawler_request_enter_callback(void *context, uint32_t index)
  1015. {
  1016. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1017. furi_check(app, "web_crawler_request_enter_callback: WebCrawlerApp is NULL");
  1018. switch (index)
  1019. {
  1020. case 0: // URL
  1021. free_text_input(app);
  1022. if (!alloc_text_input(app, WebCrawlerViewTextInput))
  1023. {
  1024. FURI_LOG_E(TAG, "Failed to allocate text input");
  1025. return;
  1026. }
  1027. web_crawler_setting_item_path_clicked(context, index);
  1028. break;
  1029. case 1:
  1030. // HTTP Method
  1031. break;
  1032. case 2:
  1033. // Headers
  1034. free_text_input(app);
  1035. if (!alloc_text_input(app, WebCrawlerViewTextInputHeaders))
  1036. {
  1037. FURI_LOG_E(TAG, "Failed to allocate text input");
  1038. return;
  1039. }
  1040. web_crawler_setting_item_headers_clicked(context, index);
  1041. break;
  1042. case 3:
  1043. // Payload
  1044. free_text_input(app);
  1045. if (!alloc_text_input(app, WebCrawlerViewTextInputPayload))
  1046. {
  1047. FURI_LOG_E(TAG, "Failed to allocate text input");
  1048. return;
  1049. }
  1050. web_crawler_setting_item_payload_clicked(context, index);
  1051. break;
  1052. default:
  1053. FURI_LOG_E(TAG, "Unknown configuration item index");
  1054. break;
  1055. }
  1056. }
  1057. /**
  1058. * @brief Callback for when the user finishes entering the URL.
  1059. * @param context The context - WebCrawlerApp object.
  1060. */
  1061. void web_crawler_set_path_updated(void *context)
  1062. {
  1063. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1064. furi_check(app, "WebCrawlerApp is NULL");
  1065. snprintf(app->path, app->temp_buffer_size_path, "%s", app->temp_buffer_path);
  1066. if (app->path_item)
  1067. {
  1068. variable_item_set_current_value_text(app->path_item, app->path);
  1069. }
  1070. save_char("path", app->temp_buffer_path);
  1071. save_simply();
  1072. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1073. }
  1074. /**
  1075. * @brief Callback for when the user finishes entering the headers
  1076. * @param context The context - WebCrawlerApp object.
  1077. */
  1078. void web_crawler_set_headers_updated(void *context)
  1079. {
  1080. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1081. furi_check(app, "WebCrawlerApp is NULL");
  1082. snprintf(app->headers, app->temp_buffer_size_headers, "%s", app->temp_buffer_headers);
  1083. if (app->headers_item)
  1084. {
  1085. variable_item_set_current_value_text(app->headers_item, app->headers);
  1086. }
  1087. save_char("headers", app->temp_buffer_headers);
  1088. save_simply();
  1089. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1090. }
  1091. /**
  1092. * @brief Callback for when the user finishes entering the payload.
  1093. * @param context The context - WebCrawlerApp object.
  1094. */
  1095. void web_crawler_set_payload_updated(void *context)
  1096. {
  1097. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1098. furi_check(app, "WebCrawlerApp is NULL");
  1099. snprintf(app->payload, app->temp_buffer_size_payload, "%s", app->temp_buffer_payload);
  1100. if (app->payload_item)
  1101. {
  1102. variable_item_set_current_value_text(app->payload_item, app->payload);
  1103. }
  1104. save_char("payload", app->temp_buffer_payload);
  1105. save_simply();
  1106. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1107. }
  1108. /**
  1109. * @brief Callback for when the user finishes entering the SSID.
  1110. * @param context The context - WebCrawlerApp object.
  1111. */
  1112. void web_crawler_set_ssid_updated(void *context)
  1113. {
  1114. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1115. furi_check(app, "WebCrawlerApp is NULL");
  1116. FlipperHTTP *fhttp = flipper_http_alloc();
  1117. furi_check(fhttp, "FlipperHTTP is NULL");
  1118. snprintf(app->ssid, app->temp_buffer_size_ssid, "%s", app->temp_buffer_ssid);
  1119. if (app->ssid_item)
  1120. {
  1121. variable_item_set_current_value_text(app->ssid_item, app->ssid);
  1122. // send to UART
  1123. if (!flipper_http_save_wifi(fhttp, app->ssid, app->password))
  1124. {
  1125. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1126. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1127. }
  1128. }
  1129. save_char("ssid", app->temp_buffer_ssid);
  1130. save_simply();
  1131. flipper_http_free(fhttp);
  1132. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1133. }
  1134. /**
  1135. * @brief Callback for when the user finishes entering the Password.
  1136. * @param context The context - WebCrawlerApp object.
  1137. */
  1138. void web_crawler_set_password_update(void *context)
  1139. {
  1140. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1141. furi_check(app, "WebCrawlerApp is NULL");
  1142. FlipperHTTP *fhttp = flipper_http_alloc();
  1143. furi_check(fhttp, "FlipperHTTP is NULL");
  1144. snprintf(app->password, app->temp_buffer_size_password, "%s", app->temp_buffer_password);
  1145. if (app->password_item)
  1146. {
  1147. variable_item_set_current_value_text(app->password_item, app->password);
  1148. // send to UART
  1149. if (!flipper_http_save_wifi(fhttp, app->ssid, app->password))
  1150. {
  1151. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  1152. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  1153. }
  1154. }
  1155. save_char("password", app->temp_buffer_password);
  1156. save_simply();
  1157. flipper_http_free(fhttp);
  1158. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1159. }
  1160. /**
  1161. * @brief Callback for when the user finishes entering the File Type.
  1162. * @param context The context - WebCrawlerApp object.
  1163. */
  1164. void web_crawler_set_file_type_update(void *context)
  1165. {
  1166. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1167. furi_check(app, "WebCrawlerApp is NULL");
  1168. char old_file_type[16];
  1169. snprintf(old_file_type, sizeof(old_file_type), "%s", app->file_type);
  1170. snprintf(app->file_type, app->temp_buffer_size_file_type, "%s", app->temp_buffer_file_type);
  1171. if (app->file_type_item)
  1172. {
  1173. variable_item_set_current_value_text(app->file_type_item, app->file_type);
  1174. }
  1175. char file_rename[128];
  1176. if (load_char("file_rename", file_rename, 128))
  1177. {
  1178. rename_received_data(file_rename, file_rename, app->file_type, old_file_type);
  1179. }
  1180. save_char("file_type", app->file_type);
  1181. save_simply();
  1182. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1183. }
  1184. /**
  1185. * @brief Callback for when the user finishes entering the File Rename.
  1186. * @param context The context - WebCrawlerApp object.
  1187. */
  1188. void web_crawler_set_file_rename_update(void *context)
  1189. {
  1190. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1191. furi_check(app, "WebCrawlerApp is NULL");
  1192. char old_name[256];
  1193. snprintf(old_name, sizeof(old_name), "%s", app->file_rename);
  1194. snprintf(app->file_rename, app->temp_buffer_size_file_rename, "%s", app->temp_buffer_file_rename);
  1195. if (app->file_rename_item)
  1196. {
  1197. variable_item_set_current_value_text(app->file_rename_item, app->file_rename);
  1198. }
  1199. rename_received_data(old_name, app->file_rename, app->file_type, app->file_type);
  1200. save_char("file_rename", app->file_rename);
  1201. save_simply();
  1202. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemList);
  1203. }
  1204. /**
  1205. * @brief Handler for Path configuration item click.
  1206. * @param context The context - WebCrawlerApp object.
  1207. * @param index The index of the item that was clicked.
  1208. */
  1209. void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  1210. {
  1211. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1212. furi_check(app, "WebCrawlerApp is NULL");
  1213. if (!app->uart_text_input)
  1214. {
  1215. FURI_LOG_E(TAG, "Text input is NULL");
  1216. return;
  1217. }
  1218. UNUSED(index);
  1219. // Initialize temp_buffer with existing path
  1220. if (app->path && strlen(app->path) > 0)
  1221. {
  1222. strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  1223. }
  1224. else
  1225. {
  1226. strncpy(app->temp_buffer_path, "https://httpbin.org/get", app->temp_buffer_size_path - 1);
  1227. }
  1228. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  1229. // Configure the text input
  1230. bool clear_previous_text = false;
  1231. uart_text_input_set_result_callback(
  1232. app->uart_text_input,
  1233. web_crawler_set_path_updated,
  1234. app,
  1235. app->temp_buffer_path,
  1236. app->temp_buffer_size_path,
  1237. clear_previous_text);
  1238. // Set the previous callback to return to Configure screen
  1239. view_set_previous_callback(
  1240. uart_text_input_get_view(app->uart_text_input),
  1241. web_crawler_back_to_request_callback);
  1242. // Show text input dialog
  1243. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1244. }
  1245. /**
  1246. * @brief Handler for headers configuration item click.
  1247. * @param context The context - WebCrawlerApp object.
  1248. * @param index The index of the item that was clicked.
  1249. */
  1250. void web_crawler_setting_item_headers_clicked(void *context, uint32_t index)
  1251. {
  1252. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1253. furi_check(app, "WebCrawlerApp is NULL");
  1254. UNUSED(index);
  1255. if (!app->uart_text_input)
  1256. {
  1257. FURI_LOG_E(TAG, "Text input is NULL");
  1258. return;
  1259. }
  1260. if (!app->headers)
  1261. {
  1262. FURI_LOG_E(TAG, "Headers is NULL");
  1263. return;
  1264. }
  1265. if (!app->temp_buffer_headers)
  1266. {
  1267. FURI_LOG_E(TAG, "Temp buffer headers is NULL");
  1268. return;
  1269. }
  1270. // Initialize temp_buffer with existing headers
  1271. if (app->headers && strlen(app->headers) > 0)
  1272. {
  1273. strncpy(app->temp_buffer_headers, app->headers, app->temp_buffer_size_headers - 1);
  1274. }
  1275. else
  1276. {
  1277. strncpy(app->temp_buffer_headers, "{\"Content-Type\":\"application/json\",\"key\":\"value\"}", app->temp_buffer_size_headers - 1);
  1278. }
  1279. app->temp_buffer_headers[app->temp_buffer_size_headers - 1] = '\0';
  1280. // Configure the text input
  1281. bool clear_previous_text = false;
  1282. uart_text_input_set_result_callback(
  1283. app->uart_text_input,
  1284. web_crawler_set_headers_updated,
  1285. app,
  1286. app->temp_buffer_headers,
  1287. app->temp_buffer_size_headers,
  1288. clear_previous_text);
  1289. // Set the previous callback to return to Configure screen
  1290. view_set_previous_callback(
  1291. uart_text_input_get_view(app->uart_text_input),
  1292. web_crawler_back_to_request_callback);
  1293. // Show text input dialog
  1294. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1295. }
  1296. /**
  1297. * @brief Handler for payload configuration item click.
  1298. * @param context The context - WebCrawlerApp object.
  1299. * @param index The index of the item that was clicked.
  1300. */
  1301. void web_crawler_setting_item_payload_clicked(void *context, uint32_t index)
  1302. {
  1303. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1304. furi_check(app, "WebCrawlerApp is NULL");
  1305. UNUSED(index);
  1306. if (!app->uart_text_input)
  1307. {
  1308. FURI_LOG_E(TAG, "Text input is NULL");
  1309. return;
  1310. }
  1311. // Initialize temp_buffer with existing payload
  1312. if (app->payload && strlen(app->payload) > 0)
  1313. {
  1314. strncpy(app->temp_buffer_payload, app->payload, app->temp_buffer_size_payload - 1);
  1315. }
  1316. else
  1317. {
  1318. strncpy(app->temp_buffer_payload, "{\"key\":\"value\"}", app->temp_buffer_size_payload - 1);
  1319. }
  1320. app->temp_buffer_payload[app->temp_buffer_size_payload - 1] = '\0';
  1321. // Configure the text input
  1322. bool clear_previous_text = false;
  1323. uart_text_input_set_result_callback(
  1324. app->uart_text_input,
  1325. web_crawler_set_payload_updated,
  1326. app,
  1327. app->temp_buffer_payload,
  1328. app->temp_buffer_size_payload,
  1329. clear_previous_text);
  1330. // Set the previous callback to return to Configure screen
  1331. view_set_previous_callback(
  1332. uart_text_input_get_view(app->uart_text_input),
  1333. web_crawler_back_to_request_callback);
  1334. // Show text input dialog
  1335. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1336. }
  1337. /**
  1338. * @brief Handler for SSID configuration item click.
  1339. * @param context The context - WebCrawlerApp object.
  1340. * @param index The index of the item that was clicked.
  1341. */
  1342. void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  1343. {
  1344. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1345. furi_check(app, "WebCrawlerApp is NULL");
  1346. UNUSED(index);
  1347. if (!app->uart_text_input)
  1348. {
  1349. FURI_LOG_E(TAG, "Text input is NULL");
  1350. return;
  1351. }
  1352. // Initialize temp_buffer with existing SSID
  1353. if (app->ssid && strlen(app->ssid) > 0)
  1354. {
  1355. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  1356. }
  1357. else
  1358. {
  1359. strncpy(app->temp_buffer_ssid, "", app->temp_buffer_size_ssid - 1);
  1360. }
  1361. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  1362. // Configure the text input
  1363. bool clear_previous_text = false;
  1364. uart_text_input_set_result_callback(
  1365. app->uart_text_input,
  1366. web_crawler_set_ssid_updated,
  1367. app,
  1368. app->temp_buffer_ssid,
  1369. app->temp_buffer_size_ssid,
  1370. clear_previous_text);
  1371. // Set the previous callback to return to Configure screen
  1372. view_set_previous_callback(
  1373. uart_text_input_get_view(app->uart_text_input),
  1374. web_crawler_back_to_wifi_callback);
  1375. // Show text input dialog
  1376. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1377. }
  1378. /**
  1379. * @brief Handler for Password configuration item click.
  1380. * @param context The context - WebCrawlerApp object.
  1381. * @param index The index of the item that was clicked.
  1382. */
  1383. void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  1384. {
  1385. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1386. furi_check(app, "WebCrawlerApp is NULL");
  1387. UNUSED(index);
  1388. if (!app->uart_text_input)
  1389. {
  1390. FURI_LOG_E(TAG, "Text input is NULL");
  1391. return;
  1392. }
  1393. // Initialize temp_buffer with existing password
  1394. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  1395. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  1396. // Configure the text input
  1397. bool clear_previous_text = false;
  1398. uart_text_input_set_result_callback(
  1399. app->uart_text_input,
  1400. web_crawler_set_password_update,
  1401. app,
  1402. app->temp_buffer_password,
  1403. app->temp_buffer_size_password,
  1404. clear_previous_text);
  1405. // Set the previous callback to return to Configure screen
  1406. view_set_previous_callback(
  1407. uart_text_input_get_view(app->uart_text_input),
  1408. web_crawler_back_to_wifi_callback);
  1409. // Show text input dialog
  1410. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1411. }
  1412. /**
  1413. * @brief Handler for File Type configuration item click.
  1414. * @param context The context - WebCrawlerApp object.
  1415. * @param index The index of the item that was clicked.
  1416. */
  1417. void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index)
  1418. {
  1419. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1420. furi_check(app, "WebCrawlerApp is NULL");
  1421. UNUSED(index);
  1422. if (!app->uart_text_input)
  1423. {
  1424. FURI_LOG_E(TAG, "Text input is NULL");
  1425. return;
  1426. }
  1427. // Initialize temp_buffer with existing file_type
  1428. if (app->file_type && strlen(app->file_type) > 0)
  1429. {
  1430. strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  1431. }
  1432. else
  1433. {
  1434. strncpy(app->temp_buffer_file_type, ".txt", app->temp_buffer_size_file_type - 1);
  1435. }
  1436. app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  1437. // Configure the text input
  1438. bool clear_previous_text = false;
  1439. uart_text_input_set_result_callback(
  1440. app->uart_text_input,
  1441. web_crawler_set_file_type_update,
  1442. app,
  1443. app->temp_buffer_file_type,
  1444. app->temp_buffer_size_file_type,
  1445. clear_previous_text);
  1446. // Set the previous callback to return to Configure screen
  1447. view_set_previous_callback(
  1448. uart_text_input_get_view(app->uart_text_input),
  1449. web_crawler_back_to_file_callback);
  1450. // Show text input dialog
  1451. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1452. }
  1453. /**
  1454. * @brief Handler for File Rename configuration item click.
  1455. * @param context The context - WebCrawlerApp object.
  1456. * @param index The index of the item that was clicked.
  1457. */
  1458. void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index)
  1459. {
  1460. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1461. furi_check(app, "WebCrawlerApp is NULL");
  1462. UNUSED(index);
  1463. if (!app->uart_text_input)
  1464. {
  1465. FURI_LOG_E(TAG, "Text input is NULL");
  1466. return;
  1467. }
  1468. // Initialize temp_buffer with existing file_rename
  1469. if (app->file_rename && strlen(app->file_rename) > 0)
  1470. {
  1471. strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  1472. }
  1473. else
  1474. {
  1475. strncpy(app->temp_buffer_file_rename, "received_data", app->temp_buffer_size_file_rename - 1);
  1476. }
  1477. app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  1478. // Configure the text input
  1479. bool clear_previous_text = false;
  1480. uart_text_input_set_result_callback(
  1481. app->uart_text_input,
  1482. web_crawler_set_file_rename_update,
  1483. app,
  1484. app->temp_buffer_file_rename,
  1485. app->temp_buffer_size_file_rename,
  1486. clear_previous_text);
  1487. // Set the previous callback to return to Configure screen
  1488. view_set_previous_callback(
  1489. uart_text_input_get_view(app->uart_text_input),
  1490. web_crawler_back_to_file_callback);
  1491. // Show text input dialog
  1492. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewInput);
  1493. }
  1494. /**
  1495. * @brief Handler for File Delete configuration item click.
  1496. * @param context The context - WebCrawlerApp object.
  1497. * @param index The index of the item that was clicked.
  1498. */
  1499. void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index)
  1500. {
  1501. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1502. furi_check(app, "WebCrawlerApp is NULL");
  1503. UNUSED(index);
  1504. free_widget(app);
  1505. if (!alloc_widget(app, WebCrawlerViewFileDelete))
  1506. {
  1507. FURI_LOG_E(TAG, "web_crawler_setting_item_file_delete_clicked: Failed to allocate widget");
  1508. return;
  1509. }
  1510. if (!delete_received_data())
  1511. {
  1512. FURI_LOG_E(TAG, "Failed to delete file");
  1513. }
  1514. // Set the previous callback to return to Configure screen
  1515. view_set_previous_callback(
  1516. widget_get_view(app->widget),
  1517. web_crawler_back_to_file_callback);
  1518. // Show text input dialog
  1519. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  1520. }
  1521. void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index)
  1522. {
  1523. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1524. furi_check(app, "WebCrawlerApp is NULL");
  1525. UNUSED(index);
  1526. free_widget(app);
  1527. if (!alloc_widget(app, WebCrawlerViewFileRead))
  1528. {
  1529. FURI_LOG_E(TAG, "web_crawler_setting_item_file_read_clicked: Failed to allocate widget");
  1530. return;
  1531. }
  1532. widget_reset(app->widget);
  1533. char file_path[256];
  1534. char file_rename[128];
  1535. char file_type[16];
  1536. if (load_char("file_rename", file_rename, 128) && load_char("file_type", file_type, 16))
  1537. {
  1538. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, file_rename, file_type);
  1539. }
  1540. else
  1541. {
  1542. snprintf(file_path, sizeof(file_path), "%s%s%s", RECEIVED_DATA_PATH, "received_data", ".txt");
  1543. }
  1544. // load the received data from the saved file
  1545. FuriString *received_data = flipper_http_load_from_file(file_path);
  1546. if (received_data == NULL)
  1547. {
  1548. FURI_LOG_E(TAG, "Failed to load received data from file.");
  1549. if (app->widget)
  1550. {
  1551. widget_add_text_scroll_element(
  1552. app->widget,
  1553. 0,
  1554. 0,
  1555. 128,
  1556. 64, "File is empty.");
  1557. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  1558. }
  1559. return;
  1560. }
  1561. widget_add_text_scroll_element(app->widget, 0, 0, 128, 64, furi_string_get_cstr(received_data));
  1562. furi_string_free(received_data);
  1563. // Set the previous callback to return to Configure screen
  1564. view_set_previous_callback(
  1565. widget_get_view(app->widget),
  1566. web_crawler_back_to_file_callback);
  1567. // Show text input dialog
  1568. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidget);
  1569. }
  1570. static void web_crawler_widget_set_text(char *message, Widget **widget)
  1571. {
  1572. if (widget == NULL)
  1573. {
  1574. FURI_LOG_E(TAG, "flip_weather_set_widget_text - widget is NULL");
  1575. DEV_CRASH();
  1576. return;
  1577. }
  1578. if (message == NULL)
  1579. {
  1580. FURI_LOG_E(TAG, "flip_weather_set_widget_text - message is NULL");
  1581. DEV_CRASH();
  1582. return;
  1583. }
  1584. widget_reset(*widget);
  1585. uint32_t message_length = strlen(message); // Length of the message
  1586. uint32_t i = 0; // Index tracker
  1587. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1588. char *formatted_message; // Buffer to hold the final formatted message
  1589. // Allocate buffer with double the message length plus one for safety
  1590. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1591. {
  1592. return;
  1593. }
  1594. while (i < message_length)
  1595. {
  1596. uint32_t max_line_length = 31; // Maximum characters per line
  1597. uint32_t remaining_length = message_length - i; // Remaining characters
  1598. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1599. // Check for newline character within the current segment
  1600. uint32_t newline_pos = i;
  1601. bool found_newline = false;
  1602. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1603. {
  1604. if (message[newline_pos] == '\n')
  1605. {
  1606. found_newline = true;
  1607. break;
  1608. }
  1609. }
  1610. if (found_newline)
  1611. {
  1612. // If newline found, set line_length up to the newline
  1613. line_length = newline_pos - i;
  1614. }
  1615. // Temporary buffer to hold the current line
  1616. char line[32];
  1617. strncpy(line, message + i, line_length);
  1618. line[line_length] = '\0';
  1619. // If newline was found, skip it for the next iteration
  1620. if (found_newline)
  1621. {
  1622. i += line_length + 1; // +1 to skip the '\n' character
  1623. }
  1624. else
  1625. {
  1626. // Check if the line ends in the middle of a word and adjust accordingly
  1627. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1628. {
  1629. // Find the last space within the current line to avoid breaking a word
  1630. char *last_space = strrchr(line, ' ');
  1631. if (last_space != NULL)
  1632. {
  1633. // Adjust the line_length to avoid cutting the word
  1634. line_length = last_space - line;
  1635. line[line_length] = '\0'; // Null-terminate at the space
  1636. }
  1637. }
  1638. // Move the index forward by the determined line_length
  1639. i += line_length;
  1640. // Skip any spaces at the beginning of the next line
  1641. while (i < message_length && message[i] == ' ')
  1642. {
  1643. i++;
  1644. }
  1645. }
  1646. // Manually copy the fixed line into the formatted_message buffer
  1647. for (uint32_t j = 0; j < line_length; j++)
  1648. {
  1649. formatted_message[formatted_index++] = line[j];
  1650. }
  1651. // Add a newline character for line spacing
  1652. formatted_message[formatted_index++] = '\n';
  1653. }
  1654. // Null-terminate the formatted_message
  1655. formatted_message[formatted_index] = '\0';
  1656. // Add the formatted message to the widget
  1657. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1658. }
  1659. void web_crawler_loader_draw_callback(Canvas *canvas, void *model)
  1660. {
  1661. if (!canvas || !model)
  1662. {
  1663. FURI_LOG_E(TAG, "web_crawler_loader_draw_callback - canvas or model is NULL");
  1664. return;
  1665. }
  1666. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1667. SerialState http_state = data_loader_model->fhttp->state;
  1668. DataState data_state = data_loader_model->data_state;
  1669. char *title = data_loader_model->title;
  1670. canvas_set_font(canvas, FontSecondary);
  1671. if (http_state == INACTIVE)
  1672. {
  1673. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1674. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1675. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1676. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1677. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1678. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1679. return;
  1680. }
  1681. if (data_state == DataStateError || data_state == DataStateParseError)
  1682. {
  1683. web_crawler_draw_error(canvas, data_loader_model);
  1684. return;
  1685. }
  1686. canvas_draw_str(canvas, 0, 7, title);
  1687. canvas_draw_str(canvas, 0, 17, "Loading...");
  1688. if (data_state == DataStateInitial)
  1689. {
  1690. return;
  1691. }
  1692. if (http_state == SENDING)
  1693. {
  1694. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1695. return;
  1696. }
  1697. if (http_state == RECEIVING || data_state == DataStateRequested)
  1698. {
  1699. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1700. return;
  1701. }
  1702. if (http_state == IDLE && data_state == DataStateReceived)
  1703. {
  1704. canvas_draw_str(canvas, 0, 27, "Processing...");
  1705. return;
  1706. }
  1707. if (http_state == IDLE && data_state == DataStateParsed)
  1708. {
  1709. canvas_draw_str(canvas, 0, 27, "Processed...");
  1710. return;
  1711. }
  1712. }
  1713. static void web_crawler_loader_process_callback(void *context)
  1714. {
  1715. if (context == NULL)
  1716. {
  1717. FURI_LOG_E(TAG, "web_crawler_loader_process_callback - context is NULL");
  1718. DEV_CRASH();
  1719. return;
  1720. }
  1721. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1722. View *view = app->view_loader;
  1723. DataState current_data_state;
  1724. DataLoaderModel *loader_model = NULL;
  1725. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1726. if (!loader_model || !loader_model->fhttp)
  1727. {
  1728. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1729. DEV_CRASH();
  1730. return;
  1731. }
  1732. if (current_data_state == DataStateInitial)
  1733. {
  1734. with_view_model(
  1735. view,
  1736. DataLoaderModel * model,
  1737. {
  1738. model->data_state = DataStateRequested;
  1739. DataLoaderFetch fetch = model->fetcher;
  1740. if (fetch == NULL)
  1741. {
  1742. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1743. model->data_state = DataStateError;
  1744. return;
  1745. }
  1746. // Clear any previous responses
  1747. strncpy(model->fhttp->last_response, "", 1);
  1748. bool request_status = fetch(model);
  1749. if (!request_status)
  1750. {
  1751. model->data_state = DataStateError;
  1752. }
  1753. },
  1754. true);
  1755. }
  1756. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1757. {
  1758. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1759. {
  1760. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1761. {
  1762. FURI_LOG_DEV(TAG, "PONG received.");
  1763. }
  1764. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1765. {
  1766. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1767. }
  1768. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1769. {
  1770. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1771. }
  1772. else if (strlen(loader_model->fhttp->last_response) == 0)
  1773. {
  1774. // Still waiting on response
  1775. }
  1776. else
  1777. {
  1778. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1779. }
  1780. }
  1781. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1782. {
  1783. // continue waiting
  1784. }
  1785. else if (loader_model->fhttp->state == INACTIVE)
  1786. {
  1787. // inactive. try again
  1788. }
  1789. else if (loader_model->fhttp->state == ISSUE)
  1790. {
  1791. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1792. }
  1793. else
  1794. {
  1795. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1796. DEV_CRASH();
  1797. }
  1798. }
  1799. else if (current_data_state == DataStateReceived)
  1800. {
  1801. with_view_model(
  1802. view,
  1803. DataLoaderModel * model,
  1804. {
  1805. char *data_text;
  1806. if (model->parser == NULL)
  1807. {
  1808. data_text = NULL;
  1809. FURI_LOG_DEV(TAG, "Parser is NULL");
  1810. DEV_CRASH();
  1811. }
  1812. else
  1813. {
  1814. data_text = model->parser(model);
  1815. }
  1816. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1817. model->data_text = data_text;
  1818. if (data_text == NULL)
  1819. {
  1820. model->data_state = DataStateParseError;
  1821. }
  1822. else
  1823. {
  1824. model->data_state = DataStateParsed;
  1825. }
  1826. },
  1827. true);
  1828. }
  1829. else if (current_data_state == DataStateParsed)
  1830. {
  1831. with_view_model(
  1832. view,
  1833. DataLoaderModel * model,
  1834. {
  1835. if (++model->request_index < model->request_count)
  1836. {
  1837. model->data_state = DataStateInitial;
  1838. }
  1839. else
  1840. {
  1841. web_crawler_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1842. if (model->data_text != NULL)
  1843. {
  1844. free(model->data_text);
  1845. model->data_text = NULL;
  1846. }
  1847. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1848. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewWidgetResult);
  1849. }
  1850. },
  1851. true);
  1852. }
  1853. }
  1854. static void web_crawler_loader_timer_callback(void *context)
  1855. {
  1856. if (context == NULL)
  1857. {
  1858. FURI_LOG_E(TAG, "web_crawler_loader_timer_callback - context is NULL");
  1859. DEV_CRASH();
  1860. return;
  1861. }
  1862. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1863. view_dispatcher_send_custom_event(app->view_dispatcher, WebCrawlerCustomEventProcess);
  1864. }
  1865. static void web_crawler_loader_on_enter(void *context)
  1866. {
  1867. if (context == NULL)
  1868. {
  1869. FURI_LOG_E(TAG, "web_crawler_loader_on_enter - context is NULL");
  1870. DEV_CRASH();
  1871. return;
  1872. }
  1873. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1874. View *view = app->view_loader;
  1875. with_view_model(
  1876. view,
  1877. DataLoaderModel * model,
  1878. {
  1879. view_set_previous_callback(view, model->back_callback);
  1880. if (model->timer == NULL)
  1881. {
  1882. model->timer = furi_timer_alloc(web_crawler_loader_timer_callback, FuriTimerTypePeriodic, app);
  1883. }
  1884. furi_timer_start(model->timer, 250);
  1885. },
  1886. true);
  1887. }
  1888. static void web_crawler_loader_on_exit(void *context)
  1889. {
  1890. if (context == NULL)
  1891. {
  1892. FURI_LOG_E(TAG, "web_crawler_loader_on_exit - context is NULL");
  1893. DEV_CRASH();
  1894. return;
  1895. }
  1896. WebCrawlerApp *app = (WebCrawlerApp *)context;
  1897. View *view = app->view_loader;
  1898. with_view_model(
  1899. view,
  1900. DataLoaderModel * model,
  1901. {
  1902. if (model->timer)
  1903. {
  1904. furi_timer_stop(model->timer);
  1905. }
  1906. },
  1907. false);
  1908. }
  1909. void web_crawler_loader_init(View *view)
  1910. {
  1911. if (view == NULL)
  1912. {
  1913. FURI_LOG_E(TAG, "web_crawler_loader_init - view is NULL");
  1914. DEV_CRASH();
  1915. return;
  1916. }
  1917. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1918. view_set_enter_callback(view, web_crawler_loader_on_enter);
  1919. view_set_exit_callback(view, web_crawler_loader_on_exit);
  1920. }
  1921. void web_crawler_loader_free_model(View *view)
  1922. {
  1923. if (view == NULL)
  1924. {
  1925. FURI_LOG_E(TAG, "web_crawler_loader_free_model - view is NULL");
  1926. DEV_CRASH();
  1927. return;
  1928. }
  1929. with_view_model(
  1930. view,
  1931. DataLoaderModel * model,
  1932. {
  1933. if (model->timer)
  1934. {
  1935. furi_timer_free(model->timer);
  1936. model->timer = NULL;
  1937. }
  1938. if (model->parser_context)
  1939. {
  1940. // do not free the context here, it is the app context
  1941. // free(model->parser_context);
  1942. // model->parser_context = NULL;
  1943. }
  1944. if (model->fhttp)
  1945. {
  1946. flipper_http_free(model->fhttp);
  1947. model->fhttp = NULL;
  1948. }
  1949. },
  1950. false);
  1951. }
  1952. bool web_crawler_custom_event_callback(void *context, uint32_t index)
  1953. {
  1954. if (context == NULL)
  1955. {
  1956. FURI_LOG_E(TAG, "web_crawler_custom_event_callback - context is NULL");
  1957. DEV_CRASH();
  1958. return false;
  1959. }
  1960. switch (index)
  1961. {
  1962. case WebCrawlerCustomEventProcess:
  1963. web_crawler_loader_process_callback(context);
  1964. return true;
  1965. default:
  1966. FURI_LOG_DEV(TAG, "web_crawler_custom_event_callback. Unknown index: %ld", index);
  1967. return false;
  1968. }
  1969. }
  1970. 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)
  1971. {
  1972. if (app == NULL)
  1973. {
  1974. FURI_LOG_E(TAG, "web_crawler_generic_switch_to_view - app is NULL");
  1975. DEV_CRASH();
  1976. return;
  1977. }
  1978. View *view = app->view_loader;
  1979. if (view == NULL)
  1980. {
  1981. FURI_LOG_E(TAG, "web_crawler_generic_switch_to_view - view is NULL");
  1982. DEV_CRASH();
  1983. return;
  1984. }
  1985. with_view_model(
  1986. view,
  1987. DataLoaderModel * model,
  1988. {
  1989. model->title = title;
  1990. model->fetcher = fetcher;
  1991. model->parser = parser;
  1992. model->request_index = 0;
  1993. model->request_count = request_count;
  1994. model->back_callback = back;
  1995. model->data_state = DataStateInitial;
  1996. model->data_text = NULL;
  1997. //
  1998. model->parser_context = app;
  1999. if (!model->fhttp)
  2000. {
  2001. model->fhttp = flipper_http_alloc();
  2002. }
  2003. },
  2004. true);
  2005. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2006. }