web_crawler_callback.c 68 KB

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