flip_library_callback.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. #include <callback/flip_library_callback.h>
  2. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  3. #ifdef DEVELOPMENT
  4. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  5. #define DEV_CRASH() furi_crash()
  6. #else
  7. #define FURI_LOG_DEV(tag, format, ...)
  8. #define DEV_CRASH()
  9. #endif
  10. static bool flip_library_wiki_fetch(FactLoaderModel *model)
  11. {
  12. UNUSED(model);
  13. snprintf(
  14. fhttp.file_path,
  15. sizeof(fhttp.file_path),
  16. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_library/wiki.json");
  17. // encode spaces for url
  18. for (size_t i = 0; i < strlen(app_instance->uart_text_input_buffer_query); i++)
  19. {
  20. if (app_instance->uart_text_input_buffer_query[i] == ' ')
  21. {
  22. app_instance->uart_text_input_buffer_query[i] = '_';
  23. }
  24. }
  25. char url[260];
  26. snprintf(url, sizeof(url), "https://api.wikimedia.org/core/v1/wikipedia/en/search/title?q=%s&limit=1", app_instance->uart_text_input_buffer_query);
  27. Storage *storage = furi_record_open(RECORD_STORAGE);
  28. storage_simply_remove_recursive(storage, fhttp.file_path);
  29. fhttp.save_received_data = true;
  30. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  31. }
  32. static char *flip_library_wiki_parse(FactLoaderModel *model)
  33. {
  34. UNUSED(model);
  35. FuriString *data = flipper_http_load_from_file(fhttp.file_path);
  36. if (data == NULL)
  37. {
  38. FURI_LOG_E(TAG, "Failed to load received data from file.");
  39. return "Failed to load received data from file.";
  40. }
  41. char *data_cstr = (char *)furi_string_get_cstr(data);
  42. if (data_cstr == NULL)
  43. {
  44. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  45. furi_string_free(data);
  46. return "Failed to get C-string from FuriString.";
  47. }
  48. char *pages = get_json_array_value("pages", 0, data_cstr, MAX_TOKENS);
  49. if (pages == NULL)
  50. {
  51. furi_string_free(data);
  52. return data_cstr;
  53. }
  54. char *description = get_json_value("description", pages, 64);
  55. if (description == NULL)
  56. {
  57. furi_string_free(data);
  58. return data_cstr;
  59. }
  60. return description;
  61. }
  62. static void flip_library_wiki_switch_to_view(FlipLibraryApp *app)
  63. {
  64. text_input_set_header_text(app->uart_text_input_query, "Search Wikipedia");
  65. flip_library_generic_switch_to_view(app, "Searching..", flip_library_wiki_fetch, flip_library_wiki_parse, 1, callback_to_submenu_library, FlipLibraryViewTextInputQuery);
  66. }
  67. //
  68. static bool flip_library_random_fact_fetch(FactLoaderModel *model)
  69. {
  70. UNUSED(model);
  71. return flipper_http_get_request("https://uselessfacts.jsph.pl/api/v2/facts/random");
  72. }
  73. static char *flip_library_random_fact_parse(FactLoaderModel *model)
  74. {
  75. UNUSED(model);
  76. return get_json_value("text", fhttp.last_response, 128);
  77. }
  78. static void flip_library_random_fact_switch_to_view(FlipLibraryApp *app)
  79. {
  80. flip_library_generic_switch_to_view(app, "Random Fact", flip_library_random_fact_fetch, flip_library_random_fact_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  81. }
  82. //
  83. static bool flip_library_cat_fact_fetch(FactLoaderModel *model)
  84. {
  85. UNUSED(model);
  86. return flipper_http_get_request_with_headers("https://catfact.ninja/fact", "{\"Content-Type\":\"application/json\"}");
  87. }
  88. static char *flip_library_cat_fact_parse(FactLoaderModel *model)
  89. {
  90. UNUSED(model);
  91. return get_json_value("fact", fhttp.last_response, 128);
  92. }
  93. static void flip_library_cat_fact_switch_to_view(FlipLibraryApp *app)
  94. {
  95. flip_library_generic_switch_to_view(app, "Random Cat Fact", flip_library_cat_fact_fetch, flip_library_cat_fact_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  96. }
  97. //
  98. static bool flip_library_dog_fact_fetch(FactLoaderModel *model)
  99. {
  100. UNUSED(model);
  101. return flipper_http_get_request_with_headers("https://dog-api.kinduff.com/api/facts", "{\"Content-Type\":\"application/json\"}");
  102. }
  103. static char *flip_library_dog_fact_parse(FactLoaderModel *model)
  104. {
  105. UNUSED(model);
  106. return get_json_array_value("facts", 0, fhttp.last_response, 256);
  107. }
  108. static void flip_library_dog_fact_switch_to_view(FlipLibraryApp *app)
  109. {
  110. flip_library_generic_switch_to_view(app, "Random Dog Fact", flip_library_dog_fact_fetch, flip_library_dog_fact_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  111. }
  112. //
  113. static bool flip_library_quote_fetch(FactLoaderModel *model)
  114. {
  115. UNUSED(model);
  116. return flipper_http_get_request("https://zenquotes.io/api/random");
  117. }
  118. static char *flip_library_quote_parse(FactLoaderModel *model)
  119. {
  120. UNUSED(model);
  121. // remove [ and ] from the start and end of the string
  122. char *response = fhttp.last_response;
  123. if (response[0] == '[')
  124. {
  125. response++;
  126. }
  127. if (response[strlen(response) - 1] == ']')
  128. {
  129. response[strlen(response) - 1] = '\0';
  130. }
  131. // remove white space from both sides
  132. while (response[0] == ' ')
  133. {
  134. response++;
  135. }
  136. while (response[strlen(response) - 1] == ' ')
  137. {
  138. response[strlen(response) - 1] = '\0';
  139. }
  140. return get_json_value("q", response, 128);
  141. }
  142. static void flip_library_quote_switch_to_view(FlipLibraryApp *app)
  143. {
  144. flip_library_generic_switch_to_view(app, "Random Quote", flip_library_quote_fetch, flip_library_quote_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  145. }
  146. //
  147. static bool flip_library_dictionary_fetch(FactLoaderModel *model)
  148. {
  149. UNUSED(model);
  150. char payload[128];
  151. snprintf(payload, sizeof(payload), "{\"word\":\"%s\"}", app_instance->uart_text_input_buffer_query);
  152. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/define/", "{\"Content-Type\":\"application/json\"}", payload);
  153. }
  154. static char *flip_library_dictionary_parse(FactLoaderModel *model)
  155. {
  156. UNUSED(model);
  157. char *defn = get_json_value("definition", fhttp.last_response, 16);
  158. if (defn == NULL)
  159. {
  160. defn = get_json_value("[ERROR]", fhttp.last_response, 16);
  161. }
  162. return defn;
  163. }
  164. static void flip_library_dictionary_switch_to_view(FlipLibraryApp *app)
  165. {
  166. text_input_set_header_text(app->uart_text_input_query, "Enter a word");
  167. flip_library_generic_switch_to_view(app, "Defining", flip_library_dictionary_fetch, flip_library_dictionary_parse, 1, callback_to_submenu_library, FlipLibraryViewTextInputQuery);
  168. }
  169. //
  170. static bool flip_library_gps_fetch(FactLoaderModel *model)
  171. {
  172. UNUSED(model);
  173. if (fhttp.state == INACTIVE)
  174. {
  175. FURI_LOG_E(TAG, "Board is INACTIVE");
  176. flipper_http_ping(); // ping the device
  177. fhttp.state = ISSUE;
  178. return false;
  179. }
  180. if (!flipper_http_get_request_with_headers("https://ipwhois.app/json/", "{\"Content-Type\": \"application/json\"}"))
  181. {
  182. FURI_LOG_E(TAG, "Failed to send GET request");
  183. fhttp.state = ISSUE;
  184. return false;
  185. }
  186. fhttp.state = RECEIVING;
  187. return true;
  188. }
  189. static char *flip_library_gps_parse(FactLoaderModel *model)
  190. {
  191. UNUSED(model);
  192. char *total_data = NULL;
  193. if (fhttp.last_response != NULL)
  194. {
  195. char *city = get_json_value("city", fhttp.last_response, MAX_TOKENS);
  196. char *region = get_json_value("region", fhttp.last_response, MAX_TOKENS);
  197. char *country = get_json_value("country", fhttp.last_response, MAX_TOKENS);
  198. char *latitude = get_json_value("latitude", fhttp.last_response, MAX_TOKENS);
  199. char *longitude = get_json_value("longitude", fhttp.last_response, MAX_TOKENS);
  200. if (city == NULL || region == NULL || country == NULL || latitude == NULL || longitude == NULL)
  201. {
  202. FURI_LOG_E(TAG, "Failed to get geo location data");
  203. fhttp.state = ISSUE;
  204. return NULL;
  205. }
  206. if (!total_data)
  207. {
  208. total_data = (char *)malloc(512);
  209. if (!total_data)
  210. {
  211. FURI_LOG_E(TAG, "Failed to allocate memory for total_data");
  212. fhttp.state = ISSUE;
  213. return NULL;
  214. }
  215. }
  216. snprintf(total_data, 512, "You are in %s, %s, %s. \nLatitude: %s, Longitude: %s", city, region, country, latitude, longitude);
  217. fhttp.state = IDLE;
  218. free(city);
  219. free(region);
  220. free(country);
  221. free(latitude);
  222. free(longitude);
  223. }
  224. return total_data;
  225. }
  226. static void flip_library_gps_switch_to_view(FlipLibraryApp *app)
  227. {
  228. flip_library_generic_switch_to_view(app, "Fetching GPS data..", flip_library_gps_fetch, flip_library_gps_parse, 1, callback_to_submenu_library, FlipLibraryViewLoader);
  229. }
  230. //
  231. static bool flip_library_weather_fetch(FactLoaderModel *model)
  232. {
  233. if (model->request_index == 0)
  234. {
  235. fhttp.save_received_data = true;
  236. snprintf(
  237. fhttp.file_path,
  238. sizeof(fhttp.file_path),
  239. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_library/gps.json");
  240. return flip_library_gps_fetch(model); // fetch the Lat and Long
  241. }
  242. else if (model->request_index == 1)
  243. {
  244. FuriString *data = flipper_http_load_from_file(fhttp.file_path);
  245. if (data == NULL)
  246. {
  247. FURI_LOG_E(TAG, "Failed to load received data from file.");
  248. return false;
  249. }
  250. char *data_cstr = (char *)furi_string_get_cstr(data);
  251. if (data_cstr == NULL)
  252. {
  253. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  254. furi_string_free(data);
  255. return false;
  256. }
  257. char url[512];
  258. char *latitude = get_json_value("latitude", data_cstr, MAX_TOKENS);
  259. char *longitude = get_json_value("longitude", data_cstr, MAX_TOKENS);
  260. if (latitude == NULL || longitude == NULL)
  261. {
  262. FURI_LOG_E(TAG, "Failed to get geo location data");
  263. fhttp.state = ISSUE;
  264. return false;
  265. }
  266. snprintf(url, 512, "https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s&current=temperature_2m,precipitation,rain,showers,snowfall&temperature_unit=celsius&wind_speed_unit=mph&precipitation_unit=inch&forecast_days=1", latitude, longitude);
  267. if (!flipper_http_get_request_with_headers(url, "{\"Content-Type\": \"application/json\"}"))
  268. {
  269. FURI_LOG_E(TAG, "Failed to send GET request");
  270. fhttp.state = ISSUE;
  271. return false;
  272. }
  273. fhttp.state = RECEIVING;
  274. return true;
  275. }
  276. return false;
  277. }
  278. static char *flip_library_weather_parse(FactLoaderModel *model)
  279. {
  280. if (model->request_index == 0)
  281. {
  282. return flip_library_gps_parse(model); // show the location
  283. }
  284. else if (model->request_index == 1)
  285. {
  286. UNUSED(model);
  287. char *weather_data = NULL;
  288. if (fhttp.last_response != NULL)
  289. {
  290. char *current_data = get_json_value("current", fhttp.last_response, MAX_TOKENS);
  291. char *temperature = get_json_value("temperature_2m", current_data, MAX_TOKENS);
  292. char *precipitation = get_json_value("precipitation", current_data, MAX_TOKENS);
  293. char *rain = get_json_value("rain", current_data, MAX_TOKENS);
  294. char *showers = get_json_value("showers", current_data, MAX_TOKENS);
  295. char *snowfall = get_json_value("snowfall", current_data, MAX_TOKENS);
  296. char *time = get_json_value("time", current_data, MAX_TOKENS);
  297. if (current_data == NULL || temperature == NULL || precipitation == NULL || rain == NULL || showers == NULL || snowfall == NULL || time == NULL)
  298. {
  299. FURI_LOG_E(TAG, "Failed to get weather data");
  300. fhttp.state = ISSUE;
  301. return NULL;
  302. }
  303. // replace the "T" in time with a space
  304. char *ptr = strstr(time, "T");
  305. if (ptr != NULL)
  306. {
  307. *ptr = ' ';
  308. }
  309. if (!weather_data)
  310. {
  311. weather_data = (char *)malloc(512);
  312. if (!weather_data)
  313. {
  314. FURI_LOG_E(TAG, "Failed to allocate memory for weather_data");
  315. fhttp.state = ISSUE;
  316. return NULL;
  317. }
  318. }
  319. snprintf(weather_data, 512, "Temperature: %s C\nPrecipitation: %s\nRain: %s\nShowers: %s\nSnowfall: %s\nTime: %s", temperature, precipitation, rain, showers, snowfall, time);
  320. fhttp.state = IDLE;
  321. free(current_data);
  322. free(temperature);
  323. free(precipitation);
  324. free(rain);
  325. free(showers);
  326. free(snowfall);
  327. free(time);
  328. }
  329. return weather_data;
  330. }
  331. return NULL;
  332. }
  333. static void flip_library_weather_switch_to_view(FlipLibraryApp *app)
  334. {
  335. flip_library_generic_switch_to_view(app, "Fetching Weather data..", flip_library_weather_fetch, flip_library_weather_parse, 2, callback_to_submenu_library, FlipLibraryViewLoader);
  336. }
  337. //
  338. static bool flip_library_elevation_fetch(FactLoaderModel *model)
  339. {
  340. if (model->request_index == 0)
  341. {
  342. fhttp.save_received_data = true;
  343. snprintf(
  344. fhttp.file_path,
  345. sizeof(fhttp.file_path),
  346. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_library/gps.json");
  347. return flip_library_gps_fetch(model); // fetch the Lat and Long
  348. }
  349. else if (model->request_index == 1)
  350. {
  351. FuriString *data = flipper_http_load_from_file(fhttp.file_path);
  352. if (data == NULL)
  353. {
  354. FURI_LOG_E(TAG, "Failed to load received data from file.");
  355. return false;
  356. }
  357. char *data_cstr = (char *)furi_string_get_cstr(data);
  358. if (data_cstr == NULL)
  359. {
  360. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  361. furi_string_free(data);
  362. return false;
  363. }
  364. char url[256];
  365. char *latitude = get_json_value("latitude", data_cstr, MAX_TOKENS);
  366. char *longitude = get_json_value("longitude", data_cstr, MAX_TOKENS);
  367. if (latitude == NULL || longitude == NULL)
  368. {
  369. FURI_LOG_E(TAG, "Failed to get geo location data");
  370. fhttp.state = ISSUE;
  371. return false;
  372. }
  373. fhttp.save_received_data = true;
  374. snprintf(
  375. fhttp.file_path,
  376. sizeof(fhttp.file_path),
  377. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_library/elevation.json");
  378. snprintf(url, sizeof(url), "https://api.opentopodata.org/v1/aster30m?locations=%s,%s", latitude, longitude);
  379. if (!flipper_http_get_request_with_headers(url, "{\"Content-Type\": \"application/json\"}"))
  380. {
  381. FURI_LOG_E(TAG, "Failed to send GET request");
  382. fhttp.state = ISSUE;
  383. return false;
  384. }
  385. fhttp.state = RECEIVING;
  386. return true;
  387. }
  388. return false;
  389. }
  390. static char *flip_library_elevation_parse(FactLoaderModel *model)
  391. {
  392. if (model->request_index == 0)
  393. {
  394. return flip_library_gps_parse(model); // show the location
  395. }
  396. else if (model->request_index == 1)
  397. {
  398. UNUSED(model);
  399. FuriString *data = flipper_http_load_from_file(fhttp.file_path);
  400. if (data == NULL)
  401. {
  402. FURI_LOG_E(TAG, "Failed to load received data from file.");
  403. return NULL;
  404. }
  405. char *elevation_data = NULL;
  406. char *results = get_json_array_value("results", 0, (char *)furi_string_get_cstr(data), 64);
  407. char *elevation = get_json_value("elevation", results, 16);
  408. if (elevation == NULL)
  409. {
  410. FURI_LOG_E(TAG, "Failed to get elevation data");
  411. fhttp.state = ISSUE;
  412. return NULL;
  413. }
  414. elevation_data = (char *)malloc(64);
  415. if (!elevation_data)
  416. {
  417. FURI_LOG_E(TAG, "Failed to allocate memory for elevation_data");
  418. fhttp.state = ISSUE;
  419. return NULL;
  420. }
  421. snprintf(elevation_data, 64, "Elevation: %s meters", elevation);
  422. fhttp.state = IDLE;
  423. free(elevation);
  424. return elevation_data;
  425. }
  426. return NULL;
  427. }
  428. static void flip_library_elevation_switch_to_view(FlipLibraryApp *app)
  429. {
  430. flip_library_generic_switch_to_view(app, "Fetching Elevation data..", flip_library_elevation_fetch, flip_library_elevation_parse, 2, callback_to_submenu_library, FlipLibraryViewLoader);
  431. }
  432. //
  433. static bool flip_library_asset_price_fetch(FactLoaderModel *model)
  434. {
  435. UNUSED(model);
  436. if (!app_instance->uart_text_input_buffer_query)
  437. {
  438. FURI_LOG_E(TAG, "Query is NULL");
  439. fhttp.state = ISSUE;
  440. return false;
  441. }
  442. char url[128];
  443. snprintf(
  444. fhttp.file_path,
  445. sizeof(fhttp.file_path),
  446. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_library/price.txt");
  447. // capitalize the query
  448. for (size_t i = 0; i < strlen(app_instance->uart_text_input_buffer_query); i++)
  449. {
  450. app_instance->uart_text_input_buffer_query[i] = toupper(app_instance->uart_text_input_buffer_query[i]);
  451. }
  452. fhttp.save_received_data = true;
  453. snprintf(url, 128, "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=%s&apikey=2X90WLEFMP43OJKE", app_instance->uart_text_input_buffer_query);
  454. if (!flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}"))
  455. {
  456. FURI_LOG_E(TAG, "Failed to send GET request");
  457. fhttp.state = ISSUE;
  458. return false;
  459. }
  460. fhttp.state = RECEIVING;
  461. return true;
  462. }
  463. static char *flip_library_asset_price_parse(FactLoaderModel *model)
  464. {
  465. UNUSED(model);
  466. // load the received data from the saved file
  467. FuriString *price_data = flipper_http_load_from_file(fhttp.file_path);
  468. if (price_data == NULL)
  469. {
  470. FURI_LOG_E(TAG, "Failed to load received data from file.");
  471. fhttp.state = ISSUE;
  472. return NULL;
  473. }
  474. char *data_cstr = (char *)furi_string_get_cstr(price_data);
  475. if (data_cstr == NULL)
  476. {
  477. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  478. furi_string_free(price_data);
  479. fhttp.state = ISSUE;
  480. return NULL;
  481. }
  482. char *global_quote = get_json_value("Global Quote", data_cstr, MAX_TOKENS);
  483. if (global_quote == NULL)
  484. {
  485. FURI_LOG_E(TAG, "Failed to get Global Quote");
  486. fhttp.state = ISSUE;
  487. furi_string_free(price_data);
  488. free(global_quote);
  489. free(data_cstr);
  490. return NULL;
  491. }
  492. char *price = get_json_value("05. price", global_quote, MAX_TOKENS);
  493. if (price == NULL)
  494. {
  495. FURI_LOG_E(TAG, "Failed to get price");
  496. fhttp.state = ISSUE;
  497. furi_string_free(price_data);
  498. free(global_quote);
  499. free(price);
  500. free(data_cstr);
  501. return NULL;
  502. }
  503. char *asset_price = (char *)malloc(64);
  504. if (!asset_price)
  505. {
  506. FURI_LOG_E(TAG, "Failed to allocate memory for asset_price");
  507. fhttp.state = ISSUE;
  508. furi_string_free(price_data);
  509. free(global_quote);
  510. free(price);
  511. free(data_cstr);
  512. return NULL;
  513. }
  514. snprintf(asset_price, 64, "%s: $%s", app_instance->uart_text_input_buffer_query, price);
  515. fhttp.state = IDLE;
  516. furi_string_free(price_data);
  517. free(global_quote);
  518. free(price);
  519. free(data_cstr);
  520. return asset_price;
  521. }
  522. static void flip_library_asset_price_switch_to_view(FlipLibraryApp *app)
  523. {
  524. text_input_set_header_text(app->uart_text_input_query, "Enter an asset");
  525. flip_library_generic_switch_to_view(app, "Fetching Asset Price..", flip_library_asset_price_fetch, flip_library_asset_price_parse, 1, callback_to_submenu_library, FlipLibraryViewTextInputQuery);
  526. }
  527. //
  528. static bool flip_library_next_holiday_fetch(FactLoaderModel *model)
  529. {
  530. UNUSED(model);
  531. char url[128];
  532. snprintf(
  533. fhttp.file_path,
  534. sizeof(fhttp.file_path),
  535. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_library/holiday.json");
  536. fhttp.save_received_data = true;
  537. snprintf(url, sizeof(url), "https://date.nager.at/api/v3/NextPublicHolidays/%s", app_instance->uart_text_input_buffer_query);
  538. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  539. }
  540. static char *flip_library_next_holiday_parse(FactLoaderModel *model)
  541. {
  542. UNUSED(model);
  543. FuriString *holiday_data = flipper_http_load_from_file(fhttp.file_path);
  544. if (holiday_data == NULL)
  545. {
  546. FURI_LOG_E(TAG, "Failed to load received data from file.");
  547. return "Failed to load received data from file.";
  548. }
  549. furi_string_replace_at(holiday_data, 0, 0, "{\"holidays\": ");
  550. size_t term = furi_string_search_char(holiday_data, 0x0d, 0);
  551. if (term == FURI_STRING_FAILURE)
  552. {
  553. term = furi_string_size(holiday_data);
  554. }
  555. furi_string_replace_at(holiday_data, term, 0, "}");
  556. char *holiday = get_json_array_value("holidays", 0, (char *)furi_string_get_cstr(holiday_data), 512);
  557. if (holiday == NULL)
  558. {
  559. furi_string_free(holiday_data);
  560. return NULL;
  561. }
  562. char *name = get_json_value("name", holiday, 32);
  563. char *date = get_json_value("date", holiday, 32);
  564. char *result = (char *)malloc(strlen(name) + strlen(date) + 3);
  565. if (result == NULL)
  566. {
  567. furi_string_free(holiday_data);
  568. free(name);
  569. free(date);
  570. return NULL;
  571. }
  572. snprintf(result, strlen(name) + strlen(date) + 3, "%s: %s", date, name);
  573. furi_string_free(holiday_data);
  574. free(name);
  575. free(date);
  576. return result;
  577. }
  578. static void flip_library_next_holiday_switch_to_view(FlipLibraryApp *app)
  579. {
  580. text_input_set_header_text(app->uart_text_input_query, "Enter a country code");
  581. flip_library_generic_switch_to_view(app, "Fetching Next Holiday..", flip_library_next_holiday_fetch, flip_library_next_holiday_parse, 1, callback_to_submenu_library, FlipLibraryViewTextInputQuery);
  582. }
  583. //
  584. static bool flip_library_predict_age_fetch(FactLoaderModel *model)
  585. {
  586. UNUSED(model);
  587. char url[128];
  588. snprintf(url, sizeof(url), "https://api.agify.io/?name=%s", app_instance->uart_text_input_buffer_query);
  589. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  590. }
  591. static char *flip_library_predict_age_parse(FactLoaderModel *model)
  592. {
  593. UNUSED(model);
  594. if (fhttp.last_response == NULL)
  595. {
  596. return "Failed to get response";
  597. }
  598. char *age = get_json_value("age", fhttp.last_response, 16);
  599. if (age == NULL)
  600. {
  601. age = get_json_value("[ERROR]", fhttp.last_response, 16);
  602. }
  603. return age;
  604. }
  605. static void flip_library_predict_age_switch_to_view(FlipLibraryApp *app)
  606. {
  607. text_input_set_header_text(app->uart_text_input_query, "Enter a name");
  608. flip_library_generic_switch_to_view(app, "Predicting Age..", flip_library_predict_age_fetch, flip_library_predict_age_parse, 1, callback_to_submenu_predict, FlipLibraryViewTextInputQuery);
  609. }
  610. //
  611. static bool flip_library_predict_gender_fetch(FactLoaderModel *model)
  612. {
  613. UNUSED(model);
  614. char url[128];
  615. snprintf(url, sizeof(url), "https://api.genderize.io/?name=%s", app_instance->uart_text_input_buffer_query);
  616. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  617. }
  618. static char *flip_library_predict_gender_parse(FactLoaderModel *model)
  619. {
  620. UNUSED(model);
  621. if (fhttp.last_response == NULL)
  622. {
  623. return "Failed to get response";
  624. }
  625. char *gender = get_json_value("gender", fhttp.last_response, 16);
  626. if (gender == NULL)
  627. {
  628. gender = get_json_value("[ERROR]", fhttp.last_response, 16);
  629. }
  630. return gender;
  631. }
  632. static void flip_library_predict_gender_switch_to_view(FlipLibraryApp *app)
  633. {
  634. text_input_set_header_text(app->uart_text_input_query, "Enter a name");
  635. flip_library_generic_switch_to_view(app, "Predicing Gender..", flip_library_predict_gender_fetch, flip_library_predict_gender_parse, 1, callback_to_submenu_predict, FlipLibraryViewTextInputQuery);
  636. }
  637. //
  638. static bool flip_library_predict_ethnicity_fetch(FactLoaderModel *model)
  639. {
  640. UNUSED(model);
  641. char url[128];
  642. snprintf(url, sizeof(url), "https://api.nationalize.io/?name=%s", app_instance->uart_text_input_buffer_query);
  643. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  644. }
  645. static char *flip_library_predict_ethnicity_parse(FactLoaderModel *model)
  646. {
  647. UNUSED(model);
  648. if (fhttp.last_response == NULL)
  649. {
  650. return "Failed to get response";
  651. }
  652. char *country = get_json_array_value("country", 0, fhttp.last_response, 64);
  653. if (country == NULL)
  654. {
  655. return get_json_value("[ERROR]", fhttp.last_response, 16);
  656. }
  657. return get_json_value("country_id", country, 16);
  658. }
  659. static void flip_library_predict_ethnicity_switch_to_view(FlipLibraryApp *app)
  660. {
  661. text_input_set_header_text(app->uart_text_input_query, "Enter a name");
  662. flip_library_generic_switch_to_view(app, "Predicting Ethnicity..", flip_library_predict_ethnicity_fetch, flip_library_predict_ethnicity_parse, 1, callback_to_submenu_predict, FlipLibraryViewTextInputQuery);
  663. }
  664. //
  665. static bool flip_library_random_advice_fetch(FactLoaderModel *model)
  666. {
  667. UNUSED(model);
  668. return flipper_http_get_request("https://api.adviceslip.com/advice");
  669. }
  670. static char *flip_library_random_advice_parse(FactLoaderModel *model)
  671. {
  672. UNUSED(model);
  673. char *slip = get_json_value("slip", fhttp.last_response, 32);
  674. if (!slip)
  675. {
  676. return "Failed to get response";
  677. }
  678. return get_json_value("advice", slip, 32);
  679. }
  680. static void flip_library_random_advice_switch_to_view(FlipLibraryApp *app)
  681. {
  682. flip_library_generic_switch_to_view(app, "Random Advice", flip_library_random_advice_fetch, flip_library_random_advice_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  683. }
  684. //
  685. static bool flip_library_random_trivia_fetch(FactLoaderModel *model)
  686. {
  687. UNUSED(model);
  688. return flipper_http_get_request_with_headers("https://opentdb.com/api.php?amount=1", "{\"Content-Type\":\"application/json\"}");
  689. }
  690. static char *flip_library_random_trivia_parse(FactLoaderModel *model)
  691. {
  692. UNUSED(model);
  693. if (!fhttp.last_response)
  694. {
  695. return "Failed to get response";
  696. }
  697. // replace the &quot; with "
  698. char *ptr = fhttp.last_response;
  699. while ((ptr = strstr(ptr, "&quot;")) != NULL)
  700. {
  701. ptr[0] = '"';
  702. ptr[1] = '"';
  703. ptr[2] = '"';
  704. ptr[3] = '"';
  705. }
  706. // replace the &#039; with '
  707. ptr = fhttp.last_response;
  708. while ((ptr = strstr(ptr, "&#039;")) != NULL)
  709. {
  710. ptr[0] = '\'';
  711. ptr[1] = '\0';
  712. }
  713. // replace the &amp; with &
  714. ptr = fhttp.last_response;
  715. while ((ptr = strstr(ptr, "&amp;")) != NULL)
  716. {
  717. ptr[0] = '&';
  718. ptr[1] = '\0';
  719. }
  720. char *results = get_json_array_value("results", 0, fhttp.last_response, 256);
  721. if (!results)
  722. {
  723. FURI_LOG_E(TAG, "Failed to get response");
  724. FURI_LOG_E(TAG, fhttp.last_response);
  725. return "Failed to get response";
  726. }
  727. char *question = get_json_value("question", results, 32);
  728. char *correct_answer = get_json_value("correct_answer", results, 32);
  729. if (!question || !correct_answer)
  730. {
  731. return "Failed to get response";
  732. }
  733. char *result = (char *)malloc(strlen(question) + strlen(correct_answer) + 128);
  734. if (!result)
  735. {
  736. return "Failed to allocate memory";
  737. }
  738. snprintf(result, strlen(question) + strlen(correct_answer) + 128, "Q: %s\n\n(Scroll down for the answer)\n\nA: %s", question, correct_answer);
  739. return result;
  740. }
  741. static void flip_library_random_trivia_switch_to_view(FlipLibraryApp *app)
  742. {
  743. flip_library_generic_switch_to_view(app, "Random Trivia", flip_library_random_trivia_fetch, flip_library_random_trivia_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  744. }
  745. //
  746. static bool flip_library_random_tech_phrase_fetch(FactLoaderModel *model)
  747. {
  748. UNUSED(model);
  749. return flipper_http_get_request("https://techy-api.vercel.app/api/json");
  750. }
  751. static char *flip_library_random_tech_phrase_parse(FactLoaderModel *model)
  752. {
  753. UNUSED(model);
  754. if (!fhttp.last_response)
  755. {
  756. return "Failed to get response";
  757. }
  758. char *message = get_json_value("message", fhttp.last_response, 32);
  759. if (!message)
  760. {
  761. return "Failed to get response";
  762. }
  763. return message;
  764. }
  765. static void flip_library_random_tech_phrase_switch_to_view(FlipLibraryApp *app)
  766. {
  767. flip_library_generic_switch_to_view(app, "Random Tech Phrase", flip_library_random_tech_phrase_fetch, flip_library_random_tech_phrase_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  768. }
  769. //
  770. static bool flip_library_random_uuid_fetch(FactLoaderModel *model)
  771. {
  772. UNUSED(model);
  773. return flipper_http_get_request("https://www.uuidtools.com/api/generate/v4");
  774. }
  775. static char *flip_library_random_uuid_parse(FactLoaderModel *model)
  776. {
  777. UNUSED(model);
  778. if (!fhttp.last_response)
  779. {
  780. return "Failed to get response";
  781. }
  782. // remove [ and ] from the start and end of the string and " from the start and end of the uuid
  783. char *response = fhttp.last_response;
  784. if (response[0] == '[')
  785. {
  786. response++;
  787. }
  788. if (response[strlen(response) - 1] == ']')
  789. {
  790. response[strlen(response) - 1] = '\0';
  791. }
  792. if (response[0] == '"')
  793. {
  794. response++;
  795. }
  796. if (response[strlen(response) - 1] == '"')
  797. {
  798. response[strlen(response) - 1] = '\0';
  799. }
  800. return response;
  801. }
  802. static void flip_library_random_uuid_switch_to_view(FlipLibraryApp *app)
  803. {
  804. flip_library_generic_switch_to_view(app, "Random UUID", flip_library_random_uuid_fetch, flip_library_random_uuid_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  805. }
  806. //
  807. static bool flip_library_random_address_fetch(FactLoaderModel *model)
  808. {
  809. UNUSED(model);
  810. return flipper_http_get_request("https://fakerapi.it/api/v2/addresses?_quantity=1");
  811. }
  812. static char *flip_library_random_address_parse(FactLoaderModel *model)
  813. {
  814. UNUSED(model);
  815. if (fhttp.last_response == NULL)
  816. {
  817. return "Failed to get response";
  818. }
  819. char *data = get_json_array_value("data", 0, fhttp.last_response, 128);
  820. if (!data)
  821. {
  822. return "Failed to get response";
  823. }
  824. char *street = get_json_value("street", data, 32);
  825. char *city = get_json_value("city", data, 32);
  826. char *country = get_json_value("country", data, 32);
  827. char *zip = get_json_value("zipcode", data, 32);
  828. if (!street || !city || !country || !zip)
  829. {
  830. return "Failed to get response";
  831. }
  832. char *result = (char *)malloc(strlen(street) + strlen(city) + strlen(country) + strlen(zip) + 128);
  833. if (!result)
  834. {
  835. return "Failed to allocate memory";
  836. }
  837. snprintf(result, strlen(street) + strlen(city) + strlen(country) + strlen(zip) + 128, "Street: %s\nCity: %s\nCountry: %s\nZip: %s", street, city, country, zip);
  838. return result;
  839. }
  840. static void flip_library_random_address_switch_to_view(FlipLibraryApp *app)
  841. {
  842. flip_library_generic_switch_to_view(app, "Random Address", flip_library_random_address_fetch, flip_library_random_address_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  843. }
  844. //
  845. static bool flip_library_random_credit_card_fetch(FactLoaderModel *model)
  846. {
  847. UNUSED(model);
  848. return flipper_http_get_request("https://fakerapi.it/api/v2/creditCards?_quantity=1");
  849. }
  850. static char *flip_library_random_credit_card_parse(FactLoaderModel *model)
  851. {
  852. UNUSED(model);
  853. if (!fhttp.last_response)
  854. {
  855. return "Failed to get response";
  856. }
  857. char *data = get_json_array_value("data", 0, fhttp.last_response, 128);
  858. if (!data)
  859. {
  860. return "Failed to get response";
  861. }
  862. char *card_number = get_json_value("number", data, 32);
  863. char *card_name = get_json_value("owner", data, 32);
  864. char *card_expiration = get_json_value("expiration", data, 32);
  865. char *card_type = get_json_value("type", data, 32);
  866. if (!card_number || !card_name || !card_expiration || !card_type)
  867. {
  868. return "Failed to get response";
  869. }
  870. char *result = (char *)malloc(strlen(card_number) + strlen(card_name) + strlen(card_expiration) + strlen(card_type) + 128);
  871. if (!result)
  872. {
  873. return "Failed to allocate memory";
  874. }
  875. snprintf(result, strlen(card_number) + strlen(card_name) + strlen(card_expiration) + strlen(card_type) + 128, "Card Number: %s\nName: %s\nExpiration: %s\nType: %s", card_number, card_name, card_expiration, card_type);
  876. return result;
  877. }
  878. static void flip_library_random_credit_card_switch_to_view(FlipLibraryApp *app)
  879. {
  880. flip_library_generic_switch_to_view(app, "Random Credit Card", flip_library_random_credit_card_fetch, flip_library_random_credit_card_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  881. }
  882. //
  883. static bool flip_library_random_user_info_fetch(FactLoaderModel *model)
  884. {
  885. UNUSED(model);
  886. return flipper_http_get_request("https://fakerapi.it/api/v2/users?_quantity=1&_gender=male");
  887. }
  888. static char *flip_library_random_user_info_parse(FactLoaderModel *model)
  889. {
  890. UNUSED(model);
  891. if (!fhttp.last_response)
  892. {
  893. return "Failed to get response";
  894. }
  895. char *data = get_json_array_value("data", 0, fhttp.last_response, 128);
  896. if (!data)
  897. {
  898. return "Failed to get response";
  899. }
  900. char *firstname = get_json_value("firstname", data, 32);
  901. char *lastname = get_json_value("lastname", data, 32);
  902. char *email = get_json_value("email", data, 32);
  903. char *username = get_json_value("username", data, 32);
  904. char *password = get_json_value("password", data, 32);
  905. if (!firstname || !lastname || !email || !username || !password)
  906. {
  907. return "Failed to get response";
  908. }
  909. char *result = (char *)malloc(strlen(firstname) + strlen(lastname) + strlen(email) + strlen(username) + strlen(password) + 128);
  910. if (!result)
  911. {
  912. return "Failed to allocate memory";
  913. }
  914. snprintf(result, strlen(firstname) + strlen(lastname) + strlen(email) + strlen(username) + strlen(password) + 128, "First Name: %s\nLast Name: %s\nEmail: %s\nUsername: %s\nPassword: %s", firstname, lastname, email, username, password);
  915. return result;
  916. }
  917. static void flip_library_random_user_info_switch_to_view(FlipLibraryApp *app)
  918. {
  919. flip_library_generic_switch_to_view(app, "Random User Info", flip_library_random_user_info_fetch, flip_library_random_user_info_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  920. }
  921. //
  922. static void flip_library_request_error_draw(Canvas *canvas)
  923. {
  924. if (canvas == NULL)
  925. {
  926. FURI_LOG_E(TAG, "flip_library_request_error_draw - canvas is NULL");
  927. DEV_CRASH();
  928. return;
  929. }
  930. if (fhttp.last_response != NULL)
  931. {
  932. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  933. {
  934. canvas_clear(canvas);
  935. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  936. canvas_draw_str(canvas, 0, 22, "Failed to reconnect.");
  937. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  938. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  939. }
  940. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  941. {
  942. canvas_clear(canvas);
  943. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  944. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  945. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  946. }
  947. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  948. {
  949. canvas_clear(canvas);
  950. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  951. }
  952. else
  953. {
  954. canvas_clear(canvas);
  955. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  956. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  957. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  958. }
  959. }
  960. else
  961. {
  962. canvas_clear(canvas);
  963. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  964. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  965. }
  966. }
  967. static void flip_library_widget_set_text(char *message, Widget **widget)
  968. {
  969. if (widget == NULL)
  970. {
  971. FURI_LOG_E(TAG, "flip_library_set_widget_text - widget is NULL");
  972. DEV_CRASH();
  973. return;
  974. }
  975. if (message == NULL)
  976. {
  977. FURI_LOG_E(TAG, "flip_library_set_widget_text - message is NULL");
  978. DEV_CRASH();
  979. return;
  980. }
  981. widget_reset(*widget);
  982. uint32_t message_length = strlen(message); // Length of the message
  983. uint32_t i = 0; // Index tracker
  984. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  985. char *formatted_message; // Buffer to hold the final formatted message
  986. // Allocate buffer with double the message length plus one for safety
  987. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  988. {
  989. return;
  990. }
  991. while (i < message_length)
  992. {
  993. uint32_t max_line_length = 31; // Maximum characters per line
  994. uint32_t remaining_length = message_length - i; // Remaining characters
  995. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  996. // Check for newline character within the current segment
  997. uint32_t newline_pos = i;
  998. bool found_newline = false;
  999. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1000. {
  1001. if (message[newline_pos] == '\n')
  1002. {
  1003. found_newline = true;
  1004. break;
  1005. }
  1006. }
  1007. if (found_newline)
  1008. {
  1009. // If newline found, set line_length up to the newline
  1010. line_length = newline_pos - i;
  1011. }
  1012. // Temporary buffer to hold the current line
  1013. char line[32];
  1014. strncpy(line, message + i, line_length);
  1015. line[line_length] = '\0';
  1016. // If newline was found, skip it for the next iteration
  1017. if (found_newline)
  1018. {
  1019. i += line_length + 1; // +1 to skip the '\n' character
  1020. }
  1021. else
  1022. {
  1023. // Check if the line ends in the middle of a word and adjust accordingly
  1024. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1025. {
  1026. // Find the last space within the current line to avoid breaking a word
  1027. char *last_space = strrchr(line, ' ');
  1028. if (last_space != NULL)
  1029. {
  1030. // Adjust the line_length to avoid cutting the word
  1031. line_length = last_space - line;
  1032. line[line_length] = '\0'; // Null-terminate at the space
  1033. }
  1034. }
  1035. // Move the index forward by the determined line_length
  1036. i += line_length;
  1037. // Skip any spaces at the beginning of the next line
  1038. while (i < message_length && message[i] == ' ')
  1039. {
  1040. i++;
  1041. }
  1042. }
  1043. // Manually copy the fixed line into the formatted_message buffer
  1044. for (uint32_t j = 0; j < line_length; j++)
  1045. {
  1046. formatted_message[formatted_index++] = line[j];
  1047. }
  1048. // Add a newline character for line spacing
  1049. formatted_message[formatted_index++] = '\n';
  1050. }
  1051. // Null-terminate the formatted_message
  1052. formatted_message[formatted_index] = '\0';
  1053. // Add the formatted message to the widget
  1054. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1055. }
  1056. void flip_library_loader_draw_callback(Canvas *canvas, void *model)
  1057. {
  1058. if (!canvas || !model)
  1059. {
  1060. FURI_LOG_E(TAG, "flip_library_loader_draw_callback - canvas or model is NULL");
  1061. return;
  1062. }
  1063. SerialState http_state = fhttp.state;
  1064. FactLoaderModel *fact_loader_model = (FactLoaderModel *)model;
  1065. FactState fact_state = fact_loader_model->fact_state;
  1066. char *title = fact_loader_model->title;
  1067. canvas_set_font(canvas, FontSecondary);
  1068. if (http_state == INACTIVE)
  1069. {
  1070. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1071. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1072. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1073. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1074. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1075. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1076. return;
  1077. }
  1078. if (fact_state == FactStateError || fact_state == FactStateParseError)
  1079. {
  1080. flip_library_request_error_draw(canvas);
  1081. return;
  1082. }
  1083. canvas_draw_str(canvas, 0, 7, title);
  1084. canvas_draw_str(canvas, 0, 15, "Loading...");
  1085. if (fact_state == FactStateInitial)
  1086. {
  1087. return;
  1088. }
  1089. if (http_state == SENDING)
  1090. {
  1091. canvas_draw_str(canvas, 0, 22, "Sending...");
  1092. return;
  1093. }
  1094. if (http_state == RECEIVING || fact_state == FactStateRequested)
  1095. {
  1096. canvas_draw_str(canvas, 0, 22, "Receiving...");
  1097. return;
  1098. }
  1099. if (http_state == IDLE && fact_state == FactStateReceived)
  1100. {
  1101. canvas_draw_str(canvas, 0, 22, "Processing...");
  1102. return;
  1103. }
  1104. if (http_state == IDLE && fact_state == FactStateParsed)
  1105. {
  1106. canvas_draw_str(canvas, 0, 22, "Processed...");
  1107. return;
  1108. }
  1109. }
  1110. static void flip_library_loader_process_callback(void *context)
  1111. {
  1112. if (context == NULL)
  1113. {
  1114. FURI_LOG_E(TAG, "flip_library_loader_process_callback - context is NULL");
  1115. DEV_CRASH();
  1116. return;
  1117. }
  1118. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1119. View *view = app->view_loader;
  1120. FactState current_fact_state;
  1121. with_view_model(view, FactLoaderModel * model, { current_fact_state = model->fact_state; }, false);
  1122. if (current_fact_state == FactStateInitial)
  1123. {
  1124. with_view_model(
  1125. view,
  1126. FactLoaderModel * model,
  1127. {
  1128. model->fact_state = FactStateRequested;
  1129. FactLoaderFetch fetch = model->fetcher;
  1130. if (fetch == NULL)
  1131. {
  1132. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1133. model->fact_state = FactStateError;
  1134. return;
  1135. }
  1136. // Clear any previous responses
  1137. strncpy(fhttp.last_response, "", 1);
  1138. bool request_status = fetch(model);
  1139. if (!request_status)
  1140. {
  1141. model->fact_state = FactStateError;
  1142. }
  1143. },
  1144. true);
  1145. }
  1146. else if (current_fact_state == FactStateRequested || current_fact_state == FactStateError)
  1147. {
  1148. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1149. {
  1150. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1151. {
  1152. FURI_LOG_DEV(TAG, "PONG received.");
  1153. }
  1154. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1155. {
  1156. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1157. }
  1158. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1159. {
  1160. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1161. }
  1162. else if (strlen(fhttp.last_response) == 0)
  1163. {
  1164. // Still waiting on response
  1165. }
  1166. else
  1167. {
  1168. with_view_model(view, FactLoaderModel * model, { model->fact_state = FactStateReceived; }, true);
  1169. }
  1170. }
  1171. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1172. {
  1173. // continue waiting
  1174. }
  1175. else if (fhttp.state == INACTIVE)
  1176. {
  1177. // inactive. try again
  1178. }
  1179. else if (fhttp.state == ISSUE)
  1180. {
  1181. with_view_model(view, FactLoaderModel * model, { model->fact_state = FactStateError; }, true);
  1182. }
  1183. else
  1184. {
  1185. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1186. DEV_CRASH();
  1187. }
  1188. }
  1189. else if (current_fact_state == FactStateReceived)
  1190. {
  1191. with_view_model(
  1192. view,
  1193. FactLoaderModel * model,
  1194. {
  1195. char *fact_text;
  1196. if (model->parser == NULL)
  1197. {
  1198. fact_text = NULL;
  1199. FURI_LOG_DEV(TAG, "Parser is NULL");
  1200. DEV_CRASH();
  1201. }
  1202. else
  1203. {
  1204. fact_text = model->parser(model);
  1205. }
  1206. FURI_LOG_DEV(TAG, "Parsed fact: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", fact_text ? fact_text : "NULL");
  1207. model->fact_text = fact_text;
  1208. if (fact_text == NULL)
  1209. {
  1210. model->fact_state = FactStateParseError;
  1211. }
  1212. else
  1213. {
  1214. model->fact_state = FactStateParsed;
  1215. }
  1216. },
  1217. true);
  1218. }
  1219. else if (current_fact_state == FactStateParsed)
  1220. {
  1221. with_view_model(
  1222. view,
  1223. FactLoaderModel * model,
  1224. {
  1225. if (++model->request_index < model->request_count)
  1226. {
  1227. model->fact_state = FactStateInitial;
  1228. }
  1229. else
  1230. {
  1231. flip_library_widget_set_text(model->fact_text != NULL ? model->fact_text : "Empty result", &app_instance->widget_result);
  1232. if (model->fact_text != NULL)
  1233. {
  1234. free(model->fact_text);
  1235. model->fact_text = NULL;
  1236. }
  1237. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  1238. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewWidgetResult);
  1239. }
  1240. },
  1241. true);
  1242. }
  1243. }
  1244. static void flip_library_loader_timer_callback(void *context)
  1245. {
  1246. if (context == NULL)
  1247. {
  1248. FURI_LOG_E(TAG, "flip_library_loader_timer_callback - context is NULL");
  1249. DEV_CRASH();
  1250. return;
  1251. }
  1252. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1253. view_dispatcher_send_custom_event(app->view_dispatcher, FlipLibraryCustomEventProcess);
  1254. }
  1255. static void flip_library_loader_on_enter(void *context)
  1256. {
  1257. if (context == NULL)
  1258. {
  1259. FURI_LOG_E(TAG, "flip_library_loader_on_enter - context is NULL");
  1260. DEV_CRASH();
  1261. return;
  1262. }
  1263. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1264. View *view = app->view_loader;
  1265. with_view_model(
  1266. view,
  1267. FactLoaderModel * model,
  1268. {
  1269. view_set_previous_callback(view, model->back_callback);
  1270. if (model->timer == NULL)
  1271. {
  1272. model->timer = furi_timer_alloc(flip_library_loader_timer_callback, FuriTimerTypePeriodic, app);
  1273. }
  1274. furi_timer_start(model->timer, 250);
  1275. },
  1276. true);
  1277. }
  1278. static void flip_library_loader_on_exit(void *context)
  1279. {
  1280. if (context == NULL)
  1281. {
  1282. FURI_LOG_E(TAG, "flip_library_loader_on_exit - context is NULL");
  1283. DEV_CRASH();
  1284. return;
  1285. }
  1286. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1287. View *view = app->view_loader;
  1288. with_view_model(
  1289. view,
  1290. FactLoaderModel * model,
  1291. {
  1292. if (model->timer)
  1293. {
  1294. furi_timer_stop(model->timer);
  1295. }
  1296. },
  1297. false);
  1298. }
  1299. void flip_library_loader_init(View *view)
  1300. {
  1301. if (view == NULL)
  1302. {
  1303. FURI_LOG_E(TAG, "flip_library_loader_init - view is NULL");
  1304. DEV_CRASH();
  1305. return;
  1306. }
  1307. view_allocate_model(view, ViewModelTypeLocking, sizeof(FactLoaderModel));
  1308. view_set_enter_callback(view, flip_library_loader_on_enter);
  1309. view_set_exit_callback(view, flip_library_loader_on_exit);
  1310. }
  1311. void flip_library_loader_free_model(View *view)
  1312. {
  1313. if (view == NULL)
  1314. {
  1315. FURI_LOG_E(TAG, "flip_library_loader_free_model - view is NULL");
  1316. DEV_CRASH();
  1317. return;
  1318. }
  1319. with_view_model(
  1320. view,
  1321. FactLoaderModel * model,
  1322. {
  1323. if (model->timer)
  1324. {
  1325. furi_timer_free(model->timer);
  1326. model->timer = NULL;
  1327. }
  1328. if (model->parser_context)
  1329. {
  1330. free(model->parser_context);
  1331. model->parser_context = NULL;
  1332. }
  1333. },
  1334. false);
  1335. }
  1336. bool flip_library_custom_event_callback(void *context, uint32_t index)
  1337. {
  1338. if (context == NULL)
  1339. {
  1340. FURI_LOG_E(TAG, "flip_library_custom_event_callback - context is NULL");
  1341. DEV_CRASH();
  1342. return false;
  1343. }
  1344. switch (index)
  1345. {
  1346. case FlipLibraryCustomEventProcess:
  1347. flip_library_loader_process_callback(context);
  1348. return true;
  1349. default:
  1350. FURI_LOG_DEV(TAG, "flip_library_custom_event_callback. Unknown index: %ld", index);
  1351. return false;
  1352. }
  1353. }
  1354. void callback_submenu_choices(void *context, uint32_t index)
  1355. {
  1356. if (context == NULL)
  1357. {
  1358. FURI_LOG_E(TAG, "callback_submenu_choices - context is NULL");
  1359. DEV_CRASH();
  1360. return;
  1361. }
  1362. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1363. switch (index)
  1364. {
  1365. case FlipLibrarySubmenuIndexRandomFacts:
  1366. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFacts);
  1367. break;
  1368. case FlipLibrarySubmenuIndexAbout:
  1369. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewAbout);
  1370. break;
  1371. case FlipLibrarySubmenuIndexSettings:
  1372. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  1373. break;
  1374. case FlipLibrarySubmenuIndexDictionary:
  1375. flip_library_dictionary_switch_to_view(app);
  1376. break;
  1377. case FlipLibrarySubmenuIndexRandomFactsCats:
  1378. flip_library_cat_fact_switch_to_view(app);
  1379. break;
  1380. case FlipLibrarySubmenuIndexRandomFactsDogs:
  1381. flip_library_dog_fact_switch_to_view(app);
  1382. break;
  1383. case FlipLibrarySubmenuIndexRandomFactsQuotes:
  1384. flip_library_quote_switch_to_view(app);
  1385. break;
  1386. case FlipLibrarySubmenuIndexRandomFactsAll:
  1387. flip_library_random_fact_switch_to_view(app);
  1388. break;
  1389. case FlipLibrarySubmenuIndexRandomAdvice:
  1390. flip_library_random_advice_switch_to_view(app);
  1391. break;
  1392. case FlipLibrarySubmenuIndexRandomTrivia:
  1393. flip_library_random_trivia_switch_to_view(app);
  1394. break;
  1395. case FlipLibrarySubmenuIndexRandomTechPhrase:
  1396. flip_library_random_tech_phrase_switch_to_view(app);
  1397. break;
  1398. case FlipLibrarySubmenuIndexRandomUUID:
  1399. flip_library_random_uuid_switch_to_view(app);
  1400. break;
  1401. case FlipLibrarySubmenuIndexRandomAddress:
  1402. flip_library_random_address_switch_to_view(app);
  1403. break;
  1404. case FlipLibrarySubmenuIndexRandomCreditCard:
  1405. flip_library_random_credit_card_switch_to_view(app);
  1406. break;
  1407. case FlipLibrarySubmenuIndexRandomUserInfo:
  1408. flip_library_random_user_info_switch_to_view(app);
  1409. break;
  1410. case FlipLibrarySubmenuIndexWiki:
  1411. flip_library_wiki_switch_to_view(app);
  1412. break;
  1413. case FlipLibrarySubmenuIndexGPS:
  1414. flip_library_gps_switch_to_view(app);
  1415. break;
  1416. case FlipLibrarySubmenuIndexWeather:
  1417. flip_library_weather_switch_to_view(app);
  1418. break;
  1419. case FlipLibrarySubmenuIndexElevation:
  1420. flip_library_elevation_switch_to_view(app);
  1421. break;
  1422. case FlipLibrarySubmenuIndexAssetPrice:
  1423. flip_library_asset_price_switch_to_view(app);
  1424. break;
  1425. case FlipLibrarySubmenuIndexNextHoliday:
  1426. flip_library_next_holiday_switch_to_view(app);
  1427. break;
  1428. case FlipLibrarySubmenuIndexPredict: // predict submenu
  1429. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewPredict);
  1430. break;
  1431. case FlipLibrarySubmenuIndexPredictGender:
  1432. flip_library_predict_gender_switch_to_view(app);
  1433. break;
  1434. case FlipLibrarySubmenuIndexPredictAge:
  1435. flip_library_predict_age_switch_to_view(app);
  1436. break;
  1437. case FlipLibrarySubmenuIndexPredictEthnicity:
  1438. flip_library_predict_ethnicity_switch_to_view(app);
  1439. break;
  1440. case FlipLibrarySubmenuIndexLibrary:
  1441. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSubmenuLibrary);
  1442. break;
  1443. default:
  1444. break;
  1445. }
  1446. }
  1447. void text_updated_ssid(void *context)
  1448. {
  1449. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1450. if (!app)
  1451. {
  1452. FURI_LOG_E(TAG, "text_updated_ssid - FlipLibraryApp is NULL");
  1453. DEV_CRASH();
  1454. return;
  1455. }
  1456. // store the entered text
  1457. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  1458. // Ensure null-termination
  1459. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  1460. // update the variable item text
  1461. if (app->variable_item_ssid)
  1462. {
  1463. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  1464. }
  1465. // save settings
  1466. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  1467. // save wifi settings to devboard
  1468. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  1469. {
  1470. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  1471. {
  1472. FURI_LOG_E(TAG, "Failed to save wifi settings.");
  1473. }
  1474. }
  1475. // switch to the settings view
  1476. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  1477. }
  1478. void text_updated_password(void *context)
  1479. {
  1480. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1481. if (!app)
  1482. {
  1483. FURI_LOG_E(TAG, "text_updated_password - FlipLibraryApp is NULL");
  1484. DEV_CRASH();
  1485. return;
  1486. }
  1487. // store the entered text
  1488. strncpy(app->uart_text_input_buffer_password, app->uart_text_input_temp_buffer_password, app->uart_text_input_buffer_size_password);
  1489. // Ensure null-termination
  1490. app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
  1491. // update the variable item text
  1492. if (app->variable_item_password)
  1493. {
  1494. variable_item_set_current_value_text(app->variable_item_password, app->uart_text_input_buffer_password);
  1495. }
  1496. // save settings
  1497. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  1498. // save wifi settings to devboard
  1499. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  1500. {
  1501. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  1502. {
  1503. FURI_LOG_E(TAG, "Failed to save wifi settings");
  1504. }
  1505. }
  1506. // switch to the settings view
  1507. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  1508. }
  1509. void text_updated_query(void *context)
  1510. {
  1511. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1512. if (!app)
  1513. {
  1514. FURI_LOG_E(TAG, "text_updated_query - FlipLibraryApp is NULL");
  1515. DEV_CRASH();
  1516. return;
  1517. }
  1518. // store the entered text
  1519. strncpy(app->uart_text_input_buffer_query, app->uart_text_input_temp_buffer_query, app->uart_text_input_buffer_size_query);
  1520. // Ensure null-termination
  1521. app->uart_text_input_buffer_query[app->uart_text_input_buffer_size_query - 1] = '\0';
  1522. // switch to the loader view
  1523. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewLoader);
  1524. }
  1525. uint32_t callback_to_submenu(void *context)
  1526. {
  1527. UNUSED(context);
  1528. return FlipLibraryViewSubmenuMain;
  1529. }
  1530. uint32_t callback_to_wifi_settings(void *context)
  1531. {
  1532. UNUSED(context);
  1533. return FlipLibraryViewSettings;
  1534. }
  1535. uint32_t callback_to_random_facts(void *context)
  1536. {
  1537. UNUSED(context);
  1538. return FlipLibraryViewRandomFacts;
  1539. }
  1540. uint32_t callback_to_submenu_library(void *context)
  1541. {
  1542. UNUSED(context);
  1543. return FlipLibraryViewSubmenuLibrary;
  1544. }
  1545. void settings_item_selected(void *context, uint32_t index)
  1546. {
  1547. FlipLibraryApp *app = (FlipLibraryApp *)context;
  1548. if (!app)
  1549. {
  1550. FURI_LOG_E(TAG, "settings_item_selected - FlipLibraryApp is NULL");
  1551. DEV_CRASH();
  1552. return;
  1553. }
  1554. switch (index)
  1555. {
  1556. case 0: // Input SSID
  1557. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputSSID);
  1558. break;
  1559. case 1: // Input Password
  1560. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputPassword);
  1561. break;
  1562. default:
  1563. FURI_LOG_E(TAG, "Unknown configuration item index");
  1564. break;
  1565. }
  1566. }
  1567. /**
  1568. * @brief Navigation callback for exiting the application
  1569. * @param context The context - unused
  1570. * @return next view id (VIEW_NONE to exit the app)
  1571. */
  1572. uint32_t callback_exit_app(void *context)
  1573. {
  1574. // Exit the application
  1575. if (!context)
  1576. {
  1577. FURI_LOG_E(TAG, "callback_exit_app - Context is NULL");
  1578. return VIEW_NONE;
  1579. }
  1580. UNUSED(context);
  1581. return VIEW_NONE; // Return VIEW_NONE to exit the app
  1582. }
  1583. uint32_t callback_to_submenu_predict(void *context)
  1584. {
  1585. UNUSED(context);
  1586. return FlipLibraryViewPredict;
  1587. }
  1588. void flip_library_generic_switch_to_view(FlipLibraryApp *app, char *title, FactLoaderFetch fetcher, FactLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1589. {
  1590. if (app == NULL)
  1591. {
  1592. FURI_LOG_E(TAG, "flip_library_generic_switch_to_view - app is NULL");
  1593. DEV_CRASH();
  1594. return;
  1595. }
  1596. View *view = app->view_loader;
  1597. if (view == NULL)
  1598. {
  1599. FURI_LOG_E(TAG, "flip_library_generic_switch_to_view - view is NULL");
  1600. DEV_CRASH();
  1601. return;
  1602. }
  1603. with_view_model(
  1604. view,
  1605. FactLoaderModel * model,
  1606. {
  1607. model->title = title;
  1608. model->fetcher = fetcher;
  1609. model->parser = parser;
  1610. model->request_index = 0;
  1611. model->request_count = request_count;
  1612. model->back_callback = back;
  1613. model->fact_state = FactStateInitial;
  1614. model->fact_text = NULL;
  1615. },
  1616. true);
  1617. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1618. }