web_crawler_callback.c 72 KB

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