web_crawler_callback.c 72 KB

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