web_crawler_callback.c 71 KB

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