web_crawler_callback.c 73 KB

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