flip_store_callback.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. #include <callback/flip_store_callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  6. #define DEV_CRASH() furi_crash()
  7. #else
  8. #define FURI_LOG_DEV(tag, format, ...)
  9. #define DEV_CRASH()
  10. #endif
  11. bool flip_store_app_does_exist = false;
  12. uint32_t selected_firmware_index = 0;
  13. static uint32_t callback_to_app_category_list(void *context);
  14. static bool flip_store_dl_app_fetch(DataLoaderModel *model)
  15. {
  16. if (!model->fhttp)
  17. {
  18. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  19. return false;
  20. }
  21. return flip_store_install_app(model->fhttp, categories[flip_store_category_index]);
  22. }
  23. static char *flip_store_dl_app_parse(DataLoaderModel *model)
  24. {
  25. if (!model->fhttp || model->fhttp->state != IDLE)
  26. {
  27. FURI_LOG_E(TAG, "FlipperHTTP is NULL or not IDLE");
  28. return "Failed to install app.";
  29. }
  30. return "App installed successfully.";
  31. }
  32. static void flip_store_dl_app_switch_to_view(FlipStoreApp *app)
  33. {
  34. flip_store_generic_switch_to_view(app, flip_catalog[app_selected_index].app_name, flip_store_dl_app_fetch, flip_store_dl_app_parse, 1, callback_to_app_category_list, FlipStoreViewLoader);
  35. }
  36. //
  37. static bool flip_store_fetch_app_list(DataLoaderModel *model)
  38. {
  39. if (!model->fhttp)
  40. {
  41. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  42. return false;
  43. }
  44. model->fhttp->state = IDLE;
  45. flip_catalog_free();
  46. snprintf(
  47. model->fhttp->file_path,
  48. sizeof(model->fhttp->file_path),
  49. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", categories[flip_store_category_index]);
  50. model->fhttp->save_received_data = true;
  51. model->fhttp->is_bytes_request = false;
  52. char url[128];
  53. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/max/", categories[flip_store_category_index]);
  54. return flipper_http_get_request_with_headers(model->fhttp, url, "{\"Content-Type\":\"application/json\"}");
  55. }
  56. static bool set_appropriate_list(FlipperHTTP *fhttp, Submenu **submenu, FlipStoreApp *app)
  57. {
  58. if (!submenu || !fhttp || !app)
  59. {
  60. FURI_LOG_E(TAG, "Submenu, FlipperHTTP, or app is NULL");
  61. return false;
  62. }
  63. if (!easy_flipper_set_submenu(submenu, FlipStoreViewAppListCategory, categories[flip_store_category_index], callback_to_app_list, &app->view_dispatcher))
  64. {
  65. return false;
  66. }
  67. if (flip_store_process_app_list(fhttp) && submenu && flip_catalog)
  68. {
  69. submenu_reset(*submenu);
  70. // add each app name to submenu
  71. for (int i = 0; i < MAX_APP_COUNT; i++)
  72. {
  73. if (strlen(flip_catalog[i].app_name) > 0)
  74. {
  75. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
  76. }
  77. else
  78. {
  79. break;
  80. }
  81. }
  82. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  83. return true;
  84. }
  85. else
  86. {
  87. FURI_LOG_E(TAG, "Failed to process the app list");
  88. return false;
  89. }
  90. return false;
  91. }
  92. static char *flip_store_parse_app_list(DataLoaderModel *model)
  93. {
  94. if (!model->fhttp)
  95. {
  96. return "Failed to fetch app list.";
  97. }
  98. FlipStoreApp *app = (FlipStoreApp *)model->parser_context;
  99. return set_appropriate_list(model->fhttp, &app->submenu_app_list_category, app) ? "App list fetched successfully." : "Failed to fetch app list.";
  100. }
  101. static void flip_store_switch_to_app_list(FlipStoreApp *app)
  102. {
  103. flip_store_generic_switch_to_view(app, categories[flip_store_category_index], flip_store_fetch_app_list, flip_store_parse_app_list, 1, callback_to_submenu, FlipStoreViewLoader);
  104. }
  105. //
  106. static bool flip_store_fetch_firmware(DataLoaderModel *model)
  107. {
  108. if (!model->fhttp)
  109. {
  110. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  111. return false;
  112. }
  113. model->fhttp->state = IDLE;
  114. if (model->request_index == 0)
  115. {
  116. firmware_free();
  117. firmwares = firmware_alloc();
  118. if (!firmwares)
  119. {
  120. return false;
  121. }
  122. firmware_request_success = flip_store_get_firmware_file(
  123. model->fhttp,
  124. firmwares[selected_firmware_index].links[0],
  125. firmwares[selected_firmware_index].name,
  126. strrchr(firmwares[selected_firmware_index].links[0], '/') + 1);
  127. return firmware_request_success;
  128. }
  129. else if (model->request_index == 1)
  130. {
  131. firmware_request_success_2 = flip_store_get_firmware_file(
  132. model->fhttp,
  133. firmwares[selected_firmware_index].links[1],
  134. firmwares[selected_firmware_index].name,
  135. strrchr(firmwares[selected_firmware_index].links[1], '/') + 1);
  136. return firmware_request_success_2;
  137. }
  138. else if (model->request_index == 2)
  139. {
  140. firmware_request_success_3 = flip_store_get_firmware_file(
  141. model->fhttp,
  142. firmwares[selected_firmware_index].links[2],
  143. firmwares[selected_firmware_index].name,
  144. strrchr(firmwares[selected_firmware_index].links[2], '/') + 1);
  145. return firmware_request_success_3;
  146. }
  147. return false;
  148. }
  149. static char *flip_store_parse_firmware(DataLoaderModel *model)
  150. {
  151. if (model->request_index == 0)
  152. {
  153. if (firmware_request_success)
  154. {
  155. return "File 1 installed.";
  156. }
  157. }
  158. else if (model->request_index == 1)
  159. {
  160. if (firmware_request_success_2)
  161. {
  162. return "File 2 installed.";
  163. }
  164. }
  165. else if (model->request_index == 2)
  166. {
  167. if (firmware_request_success_3)
  168. {
  169. return "Firmware downloaded successfully";
  170. }
  171. }
  172. return NULL;
  173. }
  174. static void flip_store_switch_to_firmware_list(FlipStoreApp *app)
  175. {
  176. flip_store_generic_switch_to_view(app, firmwares[selected_firmware_index].name, flip_store_fetch_firmware, flip_store_parse_firmware, FIRMWARE_LINKS, callback_to_firmware_list, FlipStoreViewLoader);
  177. }
  178. // Function to draw the message on the canvas with word wrapping
  179. static void draw_description(Canvas *canvas, const char *description, int x, int y)
  180. {
  181. if (description == NULL || strlen(description) == 0)
  182. {
  183. FURI_LOG_E(TAG, "User message is NULL.");
  184. return;
  185. }
  186. if (!canvas)
  187. {
  188. FURI_LOG_E(TAG, "Canvas is NULL.");
  189. return;
  190. }
  191. size_t msg_length = strlen(description);
  192. size_t start = 0;
  193. int line_num = 0;
  194. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  195. while (start < msg_length && line_num < 4)
  196. {
  197. size_t remaining = msg_length - start;
  198. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  199. if (remaining > MAX_LINE_LENGTH)
  200. {
  201. // Find the last space within the first 'len' characters
  202. size_t last_space = len;
  203. while (last_space > 0 && description[start + last_space - 1] != ' ')
  204. {
  205. last_space--;
  206. }
  207. if (last_space > 0)
  208. {
  209. len = last_space; // Adjust len to the position of the last space
  210. }
  211. }
  212. // Copy the substring to 'line' and null-terminate it
  213. memcpy(line, description + start, len);
  214. line[len] = '\0'; // Ensure the string is null-terminated
  215. // Draw the string on the canvas
  216. // Adjust the y-coordinate based on the line number
  217. canvas_draw_str_aligned(canvas, x, y + line_num * 10, AlignLeft, AlignTop, line);
  218. // Update the start position for the next line
  219. start += len;
  220. // Skip any spaces to avoid leading spaces on the next line
  221. while (start < msg_length && description[start] == ' ')
  222. {
  223. start++;
  224. }
  225. // Increment the line number
  226. line_num++;
  227. }
  228. }
  229. static void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  230. {
  231. UNUSED(model);
  232. canvas_clear(canvas);
  233. canvas_set_font(canvas, FontPrimary);
  234. char title[64];
  235. snprintf(title, 64, "%s (v.%s)", flip_catalog[app_selected_index].app_name, flip_catalog[app_selected_index].app_version);
  236. canvas_draw_str(canvas, 0, 10, title);
  237. canvas_set_font(canvas, FontSecondary);
  238. draw_description(canvas, flip_catalog[app_selected_index].app_description, 0, 13);
  239. if (flip_store_app_does_exist)
  240. {
  241. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  242. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  243. canvas_draw_icon(canvas, 45, 53, &I_ButtonBACK_10x8);
  244. canvas_draw_str_aligned(canvas, 57, 54, AlignLeft, AlignTop, "Back");
  245. }
  246. else
  247. {
  248. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  249. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  250. }
  251. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  252. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  253. }
  254. static bool flip_store_input_callback(InputEvent *event, void *context)
  255. {
  256. FlipStoreApp *app = (FlipStoreApp *)context;
  257. if (!app)
  258. {
  259. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  260. return false;
  261. }
  262. if (event->type == InputTypeShort)
  263. {
  264. if (event->key == InputKeyLeft && flip_store_app_does_exist)
  265. {
  266. // Left button clicked, delete the app
  267. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppDelete);
  268. return true;
  269. }
  270. if (event->key == InputKeyRight)
  271. {
  272. // Right button clicked, download the app
  273. flip_store_dl_app_switch_to_view(app);
  274. return true;
  275. }
  276. }
  277. else if (event->type == InputTypePress)
  278. {
  279. if (event->key == InputKeyBack)
  280. {
  281. // Back button clicked, switch to the previous view.
  282. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. static void flip_store_text_updated_ssid(void *context)
  289. {
  290. FlipStoreApp *app = (FlipStoreApp *)context;
  291. if (!app)
  292. {
  293. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  294. return;
  295. }
  296. // store the entered text
  297. strncpy(app->uart_text_input_buffer, app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size);
  298. // Ensure null-termination
  299. app->uart_text_input_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  300. // save the setting
  301. save_char("WiFi-SSID", app->uart_text_input_buffer);
  302. // update the variable item text
  303. if (app->variable_item_ssid)
  304. {
  305. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer);
  306. // get value of password
  307. char pass[64];
  308. if (load_char("WiFi-Password", pass, sizeof(pass)))
  309. {
  310. if (strlen(pass) > 0 && strlen(app->uart_text_input_buffer) > 0)
  311. {
  312. // save the settings
  313. save_settings(app->uart_text_input_buffer, pass);
  314. // initialize the http
  315. FlipperHTTP *fhttp = flipper_http_alloc();
  316. if (fhttp)
  317. {
  318. // save the wifi if the device is connected
  319. if (!flipper_http_save_wifi(fhttp, app->uart_text_input_buffer, pass))
  320. {
  321. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  322. }
  323. // free the resources
  324. flipper_http_free(fhttp);
  325. }
  326. else
  327. {
  328. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  329. }
  330. }
  331. }
  332. }
  333. // switch to the settings view
  334. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  335. }
  336. static void flip_store_text_updated_pass(void *context)
  337. {
  338. FlipStoreApp *app = (FlipStoreApp *)context;
  339. if (!app)
  340. {
  341. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  342. return;
  343. }
  344. // store the entered text
  345. strncpy(app->uart_text_input_buffer, app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size);
  346. // Ensure null-termination
  347. app->uart_text_input_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  348. // save the setting
  349. save_char("WiFi-Password", app->uart_text_input_buffer);
  350. // update the variable item text
  351. if (app->variable_item_pass)
  352. {
  353. // variable_item_set_current_value_text(app->variable_item_pass, app->uart_text_input_buffer);
  354. }
  355. // get value of ssid
  356. char ssid[64];
  357. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  358. {
  359. if (strlen(ssid) > 0 && strlen(app->uart_text_input_buffer) > 0)
  360. {
  361. // save the settings
  362. save_settings(ssid, app->uart_text_input_buffer);
  363. // initialize the http
  364. FlipperHTTP *fhttp = flipper_http_alloc();
  365. if (fhttp)
  366. {
  367. // save the wifi if the device is connected
  368. if (!flipper_http_save_wifi(fhttp, ssid, app->uart_text_input_buffer))
  369. {
  370. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  371. }
  372. // free the resources
  373. flipper_http_free(fhttp);
  374. }
  375. else
  376. {
  377. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  378. }
  379. }
  380. }
  381. // switch to the settings view
  382. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  383. }
  384. uint32_t callback_to_submenu(void *context)
  385. {
  386. UNUSED(context);
  387. firmware_free();
  388. return FlipStoreViewSubmenu;
  389. }
  390. uint32_t callback_to_submenu_options(void *context)
  391. {
  392. UNUSED(context);
  393. firmware_free();
  394. return FlipStoreViewSubmenuOptions;
  395. }
  396. uint32_t callback_to_firmware_list(void *context)
  397. {
  398. UNUSED(context);
  399. sent_firmware_request = false;
  400. sent_firmware_request_2 = false;
  401. sent_firmware_request_3 = false;
  402. //
  403. firmware_request_success = false;
  404. firmware_request_success_2 = false;
  405. firmware_request_success_3 = false;
  406. //
  407. firmware_download_success = false;
  408. firmware_download_success_2 = false;
  409. firmware_download_success_3 = false;
  410. return FlipStoreViewFirmwares;
  411. }
  412. static uint32_t callback_to_app_category_list(void *context)
  413. {
  414. UNUSED(context);
  415. return FlipStoreViewAppListCategory;
  416. }
  417. uint32_t callback_to_app_list(void *context)
  418. {
  419. UNUSED(context);
  420. flip_store_sent_request = false;
  421. flip_store_success = false;
  422. flip_store_saved_data = false;
  423. flip_store_saved_success = false;
  424. flip_store_app_does_exist = false;
  425. sent_firmware_request = false;
  426. return FlipStoreViewAppList;
  427. }
  428. static uint32_t callback_to_wifi_settings(void *context)
  429. {
  430. UNUSED(context);
  431. return FlipStoreViewSettings;
  432. }
  433. static void dialog_firmware_callback(DialogExResult result, void *context)
  434. {
  435. FlipStoreApp *app = (FlipStoreApp *)context;
  436. if (!app)
  437. {
  438. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  439. return;
  440. }
  441. if (result == DialogExResultLeft) // No
  442. {
  443. // switch to the firmware list
  444. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  445. }
  446. else if (result == DialogExResultRight)
  447. {
  448. // download the firmware then return to the firmware list
  449. flip_store_switch_to_firmware_list(app);
  450. }
  451. }
  452. static bool alloc_about_view(FlipStoreApp *app)
  453. {
  454. if (!app)
  455. {
  456. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  457. return false;
  458. }
  459. if (!app->widget_about)
  460. {
  461. if (!easy_flipper_set_widget(
  462. &app->widget_about,
  463. FlipStoreViewAbout,
  464. "Welcome to the FlipStore!\n------\nDownload apps via WiFi and\nrun them on your Flipper!\n------\nwww.github.com/jblanked",
  465. callback_to_submenu,
  466. &app->view_dispatcher))
  467. {
  468. return false;
  469. }
  470. if (!app->widget_about)
  471. {
  472. return false;
  473. }
  474. }
  475. return true;
  476. }
  477. static void free_about_view(FlipStoreApp *app)
  478. {
  479. if (app && app->widget_about != NULL)
  480. {
  481. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAbout);
  482. widget_free(app->widget_about);
  483. app->widget_about = NULL;
  484. }
  485. }
  486. static bool alloc_text_input_view(void *context, char *title)
  487. {
  488. FlipStoreApp *app = (FlipStoreApp *)context;
  489. if (!app)
  490. {
  491. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  492. return false;
  493. }
  494. if (!title)
  495. {
  496. FURI_LOG_E(TAG, "Title is NULL");
  497. return false;
  498. }
  499. app->uart_text_input_buffer_size = 64;
  500. if (!app->uart_text_input_buffer)
  501. {
  502. if (!easy_flipper_set_buffer(&app->uart_text_input_buffer, app->uart_text_input_buffer_size))
  503. {
  504. return false;
  505. }
  506. }
  507. if (!app->uart_text_input_temp_buffer)
  508. {
  509. if (!easy_flipper_set_buffer(&app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size))
  510. {
  511. return false;
  512. }
  513. }
  514. if (!app->uart_text_input)
  515. {
  516. if (!easy_flipper_set_uart_text_input(
  517. &app->uart_text_input,
  518. FlipStoreViewTextInput,
  519. title,
  520. app->uart_text_input_temp_buffer,
  521. app->uart_text_input_buffer_size,
  522. strcmp(title, "SSID") == 0 ? flip_store_text_updated_ssid : flip_store_text_updated_pass,
  523. callback_to_wifi_settings,
  524. &app->view_dispatcher,
  525. app))
  526. {
  527. return false;
  528. }
  529. if (!app->uart_text_input)
  530. {
  531. return false;
  532. }
  533. char ssid[64];
  534. char pass[64];
  535. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  536. {
  537. if (strcmp(title, "SSID") == 0)
  538. {
  539. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size);
  540. }
  541. else
  542. {
  543. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size);
  544. }
  545. }
  546. }
  547. return true;
  548. }
  549. static void free_text_input_view(FlipStoreApp *app)
  550. {
  551. if (!app)
  552. {
  553. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  554. return;
  555. }
  556. if (app->uart_text_input)
  557. {
  558. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewTextInput);
  559. uart_text_input_free(app->uart_text_input);
  560. app->uart_text_input = NULL;
  561. }
  562. if (app->uart_text_input_buffer)
  563. {
  564. free(app->uart_text_input_buffer);
  565. app->uart_text_input_buffer = NULL;
  566. }
  567. if (app->uart_text_input_temp_buffer)
  568. {
  569. free(app->uart_text_input_temp_buffer);
  570. app->uart_text_input_temp_buffer = NULL;
  571. }
  572. }
  573. static void settings_item_selected(void *context, uint32_t index);
  574. static bool alloc_variable_item_list(void *context)
  575. {
  576. FlipStoreApp *app = (FlipStoreApp *)context;
  577. if (!app)
  578. {
  579. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  580. return false;
  581. }
  582. if (!app->variable_item_list)
  583. {
  584. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipStoreViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  585. return false;
  586. if (!app->variable_item_list)
  587. return false;
  588. if (!app->variable_item_ssid)
  589. {
  590. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  591. variable_item_set_current_value_text(app->variable_item_ssid, "");
  592. }
  593. if (!app->variable_item_pass)
  594. {
  595. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  596. variable_item_set_current_value_text(app->variable_item_pass, "");
  597. }
  598. char ssid[64];
  599. char pass[64];
  600. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  601. {
  602. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  603. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  604. save_char("WiFi-SSID", ssid);
  605. save_char("WiFi-Password", pass);
  606. }
  607. }
  608. return true;
  609. }
  610. static void free_variable_item_list(FlipStoreApp *app)
  611. {
  612. if (!app)
  613. {
  614. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  615. return;
  616. }
  617. if (app->variable_item_list)
  618. {
  619. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewSettings);
  620. variable_item_list_free(app->variable_item_list);
  621. app->variable_item_list = NULL;
  622. }
  623. if (app->variable_item_ssid)
  624. {
  625. free(app->variable_item_ssid);
  626. app->variable_item_ssid = NULL;
  627. }
  628. if (app->variable_item_pass)
  629. {
  630. free(app->variable_item_pass);
  631. app->variable_item_pass = NULL;
  632. }
  633. }
  634. static bool alloc_dialog_firmware(void *context)
  635. {
  636. FlipStoreApp *app = (FlipStoreApp *)context;
  637. if (!app->dialog_firmware)
  638. {
  639. if (!easy_flipper_set_dialog_ex(
  640. &app->dialog_firmware,
  641. FlipStoreViewFirmwareDialog,
  642. "Download Firmware",
  643. 0,
  644. 0,
  645. "Are you sure you want to\ndownload this firmware?",
  646. 0,
  647. 10,
  648. "No",
  649. "Yes",
  650. NULL,
  651. dialog_firmware_callback,
  652. callback_to_firmware_list,
  653. &app->view_dispatcher,
  654. app))
  655. {
  656. return false;
  657. }
  658. if (!app->dialog_firmware)
  659. {
  660. return false;
  661. }
  662. dialog_ex_set_header(app->dialog_firmware, firmwares[selected_firmware_index].name, 0, 0, AlignLeft, AlignTop);
  663. }
  664. return true;
  665. }
  666. static void free_dialog_firmware(FlipStoreApp *app)
  667. {
  668. if (!app)
  669. {
  670. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  671. return;
  672. }
  673. if (app->dialog_firmware)
  674. {
  675. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  676. dialog_ex_free(app->dialog_firmware);
  677. app->dialog_firmware = NULL;
  678. }
  679. }
  680. static bool alloc_app_info_view(FlipStoreApp *app)
  681. {
  682. if (!app)
  683. {
  684. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  685. return false;
  686. }
  687. if (!app->view_app_info)
  688. {
  689. if (!easy_flipper_set_view(
  690. &app->view_app_info,
  691. FlipStoreViewAppInfo,
  692. flip_store_view_draw_callback_app_list,
  693. flip_store_input_callback,
  694. callback_to_app_category_list,
  695. &app->view_dispatcher,
  696. app))
  697. {
  698. return false;
  699. }
  700. if (!app->view_app_info)
  701. {
  702. return false;
  703. }
  704. }
  705. return true;
  706. }
  707. static void free_app_info_view(FlipStoreApp *app)
  708. {
  709. if (!app)
  710. {
  711. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  712. return;
  713. }
  714. if (app->view_app_info)
  715. {
  716. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppInfo);
  717. view_free(app->view_app_info);
  718. app->view_app_info = NULL;
  719. }
  720. }
  721. static void free_all_views(FlipStoreApp *app, bool should_free_variable_item_list)
  722. {
  723. if (!app)
  724. {
  725. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  726. return;
  727. }
  728. free_about_view(app);
  729. flip_catalog_free();
  730. if (should_free_variable_item_list)
  731. {
  732. free_variable_item_list(app);
  733. }
  734. if (app->submenu_app_list_category)
  735. {
  736. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  737. submenu_free(app->submenu_app_list_category);
  738. app->submenu_app_list_category = NULL;
  739. }
  740. free_text_input_view(app);
  741. free_dialog_firmware(app);
  742. free_app_info_view(app);
  743. }
  744. uint32_t callback_exit_app(void *context)
  745. {
  746. UNUSED(context);
  747. return VIEW_NONE; // Return VIEW_NONE to exit the app
  748. }
  749. void callback_submenu_choices(void *context, uint32_t index)
  750. {
  751. FlipStoreApp *app = (FlipStoreApp *)context;
  752. if (!app)
  753. {
  754. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  755. return;
  756. }
  757. switch (index)
  758. {
  759. case FlipStoreSubmenuIndexAbout:
  760. free_all_views(app, true);
  761. if (!alloc_about_view(app))
  762. {
  763. FURI_LOG_E(TAG, "Failed to set about view");
  764. return;
  765. }
  766. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  767. break;
  768. case FlipStoreSubmenuIndexSettings:
  769. free_all_views(app, true);
  770. if (!alloc_variable_item_list(app))
  771. {
  772. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  773. return;
  774. }
  775. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  776. break;
  777. case FlipStoreSubmenuIndexOptions:
  778. free_all_views(app, true);
  779. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  780. break;
  781. case FlipStoreSubmenuIndexAppList:
  782. flip_store_category_index = 0;
  783. flip_store_app_does_exist = false;
  784. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  785. break;
  786. case FlipStoreSubmenuIndexFirmwares:
  787. if (!app->submenu_firmwares)
  788. {
  789. FURI_LOG_E(TAG, "Submenu firmwares is NULL");
  790. return;
  791. }
  792. firmwares = firmware_alloc();
  793. if (firmwares == NULL)
  794. {
  795. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  796. return;
  797. }
  798. submenu_reset(app->submenu_firmwares);
  799. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  800. for (int i = 0; i < FIRMWARE_COUNT; i++)
  801. {
  802. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  803. }
  804. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  805. break;
  806. case FlipStoreSubmenuIndexAppListBluetooth:
  807. free_all_views(app, true);
  808. flip_store_category_index = 0;
  809. flip_store_app_does_exist = false;
  810. flip_store_switch_to_app_list(app);
  811. break;
  812. case FlipStoreSubmenuIndexAppListGames:
  813. free_all_views(app, true);
  814. flip_store_category_index = 1;
  815. flip_store_app_does_exist = false;
  816. flip_store_switch_to_app_list(app);
  817. break;
  818. case FlipStoreSubmenuIndexAppListGPIO:
  819. free_all_views(app, true);
  820. flip_store_category_index = 2;
  821. flip_store_app_does_exist = false;
  822. flip_store_switch_to_app_list(app);
  823. break;
  824. case FlipStoreSubmenuIndexAppListInfrared:
  825. free_all_views(app, true);
  826. flip_store_category_index = 3;
  827. flip_store_app_does_exist = false;
  828. flip_store_switch_to_app_list(app);
  829. break;
  830. case FlipStoreSubmenuIndexAppListiButton:
  831. free_all_views(app, true);
  832. flip_store_category_index = 4;
  833. flip_store_app_does_exist = false;
  834. flip_store_switch_to_app_list(app);
  835. break;
  836. case FlipStoreSubmenuIndexAppListMedia:
  837. free_all_views(app, true);
  838. flip_store_category_index = 5;
  839. flip_store_app_does_exist = false;
  840. flip_store_switch_to_app_list(app);
  841. break;
  842. case FlipStoreSubmenuIndexAppListNFC:
  843. free_all_views(app, true);
  844. flip_store_category_index = 6;
  845. flip_store_app_does_exist = false;
  846. flip_store_switch_to_app_list(app);
  847. break;
  848. case FlipStoreSubmenuIndexAppListRFID:
  849. free_all_views(app, true);
  850. flip_store_category_index = 7;
  851. flip_store_app_does_exist = false;
  852. flip_store_switch_to_app_list(app);
  853. break;
  854. case FlipStoreSubmenuIndexAppListSubGHz:
  855. free_all_views(app, true);
  856. flip_store_category_index = 8;
  857. flip_store_app_does_exist = false;
  858. flip_store_switch_to_app_list(app);
  859. break;
  860. case FlipStoreSubmenuIndexAppListTools:
  861. free_all_views(app, true);
  862. flip_store_category_index = 9;
  863. flip_store_app_does_exist = false;
  864. flip_store_switch_to_app_list(app);
  865. break;
  866. case FlipStoreSubmenuIndexAppListUSB:
  867. free_all_views(app, true);
  868. flip_store_category_index = 10;
  869. flip_store_app_does_exist = false;
  870. flip_store_switch_to_app_list(app);
  871. break;
  872. default:
  873. // Check if the index is within the firmwares list range
  874. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  875. {
  876. // Get the firmware index
  877. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  878. // Check if the firmware index is valid
  879. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  880. {
  881. // Get the firmware name
  882. selected_firmware_index = firmware_index;
  883. // Switch to the firmware download view
  884. free_all_views(app, true);
  885. if (!alloc_dialog_firmware(app))
  886. {
  887. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  888. return;
  889. }
  890. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  891. }
  892. else
  893. {
  894. FURI_LOG_E(TAG, "Invalid firmware index");
  895. easy_flipper_dialog("Error", "Issue parsing firmware.");
  896. }
  897. }
  898. // Check if the index is within the app list range
  899. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  900. {
  901. // Get the app index
  902. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  903. // Check if the app index is valid
  904. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  905. {
  906. // Get the app name
  907. char *app_name = flip_catalog[app_index].app_name;
  908. // Check if the app name is valid
  909. if (app_name != NULL && strlen(app_name) > 0)
  910. {
  911. app_selected_index = app_index;
  912. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  913. free_app_info_view(app);
  914. if (!alloc_app_info_view(app))
  915. {
  916. FURI_LOG_E(TAG, "Failed to allocate app info view");
  917. return;
  918. }
  919. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  920. }
  921. else
  922. {
  923. FURI_LOG_E(TAG, "Invalid app name");
  924. }
  925. }
  926. else
  927. {
  928. FURI_LOG_E(TAG, "Invalid app index");
  929. }
  930. }
  931. else
  932. {
  933. FURI_LOG_E(TAG, "Unknown submenu index");
  934. }
  935. break;
  936. }
  937. }
  938. static void settings_item_selected(void *context, uint32_t index)
  939. {
  940. FlipStoreApp *app = (FlipStoreApp *)context;
  941. if (!app)
  942. {
  943. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  944. return;
  945. }
  946. char ssid[64];
  947. char pass[64];
  948. switch (index)
  949. {
  950. case 0: // Input SSID
  951. // Text Input
  952. free_all_views(app, false);
  953. if (!alloc_text_input_view(app, "SSID"))
  954. {
  955. FURI_LOG_E(TAG, "Failed to allocate text input view");
  956. return;
  957. }
  958. // load SSID
  959. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  960. {
  961. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  962. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  963. }
  964. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  965. break;
  966. case 1: // Input Password
  967. free_all_views(app, false);
  968. if (!alloc_text_input_view(app, "Password"))
  969. {
  970. FURI_LOG_E(TAG, "Failed to allocate text input view");
  971. return;
  972. }
  973. // load password
  974. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  975. {
  976. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  977. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  978. }
  979. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  980. break;
  981. default:
  982. FURI_LOG_E(TAG, "Unknown configuration item index");
  983. break;
  984. }
  985. }
  986. static void flip_store_widget_set_text(char *message, Widget **widget)
  987. {
  988. if (widget == NULL)
  989. {
  990. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  991. DEV_CRASH();
  992. return;
  993. }
  994. if (message == NULL)
  995. {
  996. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  997. DEV_CRASH();
  998. return;
  999. }
  1000. widget_reset(*widget);
  1001. uint32_t message_length = strlen(message); // Length of the message
  1002. uint32_t i = 0; // Index tracker
  1003. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1004. char *formatted_message; // Buffer to hold the final formatted message
  1005. // Allocate buffer with double the message length plus one for safety
  1006. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1007. {
  1008. return;
  1009. }
  1010. while (i < message_length)
  1011. {
  1012. uint32_t max_line_length = 31; // Maximum characters per line
  1013. uint32_t remaining_length = message_length - i; // Remaining characters
  1014. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1015. // Check for newline character within the current segment
  1016. uint32_t newline_pos = i;
  1017. bool found_newline = false;
  1018. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1019. {
  1020. if (message[newline_pos] == '\n')
  1021. {
  1022. found_newline = true;
  1023. break;
  1024. }
  1025. }
  1026. if (found_newline)
  1027. {
  1028. // If newline found, set line_length up to the newline
  1029. line_length = newline_pos - i;
  1030. }
  1031. // Temporary buffer to hold the current line
  1032. char line[32];
  1033. strncpy(line, message + i, line_length);
  1034. line[line_length] = '\0';
  1035. // If newline was found, skip it for the next iteration
  1036. if (found_newline)
  1037. {
  1038. i += line_length + 1; // +1 to skip the '\n' character
  1039. }
  1040. else
  1041. {
  1042. // Check if the line ends in the middle of a word and adjust accordingly
  1043. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1044. {
  1045. // Find the last space within the current line to avoid breaking a word
  1046. char *last_space = strrchr(line, ' ');
  1047. if (last_space != NULL)
  1048. {
  1049. // Adjust the line_length to avoid cutting the word
  1050. line_length = last_space - line;
  1051. line[line_length] = '\0'; // Null-terminate at the space
  1052. }
  1053. }
  1054. // Move the index forward by the determined line_length
  1055. i += line_length;
  1056. // Skip any spaces at the beginning of the next line
  1057. while (i < message_length && message[i] == ' ')
  1058. {
  1059. i++;
  1060. }
  1061. }
  1062. // Manually copy the fixed line into the formatted_message buffer
  1063. for (uint32_t j = 0; j < line_length; j++)
  1064. {
  1065. formatted_message[formatted_index++] = line[j];
  1066. }
  1067. // Add a newline character for line spacing
  1068. formatted_message[formatted_index++] = '\n';
  1069. }
  1070. // Null-terminate the formatted_message
  1071. formatted_message[formatted_index] = '\0';
  1072. // Add the formatted message to the widget
  1073. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1074. }
  1075. static void flip_store_request_error(Canvas *canvas, FlipperHTTP *fhttp)
  1076. {
  1077. if (!canvas)
  1078. {
  1079. FURI_LOG_E(TAG, "Canvas is NULL");
  1080. return;
  1081. }
  1082. if (!fhttp)
  1083. {
  1084. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1085. return;
  1086. }
  1087. if (fhttp->last_response != NULL)
  1088. {
  1089. if (strstr(fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  1090. {
  1091. canvas_clear(canvas);
  1092. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1093. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1094. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1095. }
  1096. else if (strstr(fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  1097. {
  1098. canvas_clear(canvas);
  1099. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1100. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1101. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1102. }
  1103. else
  1104. {
  1105. FURI_LOG_E(TAG, "Received an error: %s", fhttp->last_response);
  1106. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  1107. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1108. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1109. }
  1110. }
  1111. else
  1112. {
  1113. canvas_clear(canvas);
  1114. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  1115. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1116. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1117. }
  1118. }
  1119. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1120. {
  1121. if (!canvas || !model)
  1122. {
  1123. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1124. return;
  1125. }
  1126. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1127. SerialState http_state = data_loader_model->fhttp->state;
  1128. DataState data_state = data_loader_model->data_state;
  1129. char *title = data_loader_model->title;
  1130. canvas_set_font(canvas, FontSecondary);
  1131. if (http_state == INACTIVE)
  1132. {
  1133. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1134. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1135. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1136. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1137. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1138. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1139. return;
  1140. }
  1141. if (data_state == DataStateError || data_state == DataStateParseError)
  1142. {
  1143. flip_store_request_error(canvas, data_loader_model->fhttp);
  1144. return;
  1145. }
  1146. canvas_draw_str(canvas, 0, 7, title);
  1147. canvas_draw_str(canvas, 0, 17, "Loading...");
  1148. if (data_state == DataStateInitial)
  1149. {
  1150. return;
  1151. }
  1152. if (http_state == SENDING)
  1153. {
  1154. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1155. return;
  1156. }
  1157. if (http_state == RECEIVING || data_state == DataStateRequested)
  1158. {
  1159. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1160. return;
  1161. }
  1162. if (http_state == IDLE && data_state == DataStateReceived)
  1163. {
  1164. canvas_draw_str(canvas, 0, 27, "Processing...");
  1165. return;
  1166. }
  1167. if (http_state == IDLE && data_state == DataStateParsed)
  1168. {
  1169. canvas_draw_str(canvas, 0, 27, "Processed...");
  1170. return;
  1171. }
  1172. }
  1173. static void flip_store_loader_process_callback(void *context)
  1174. {
  1175. if (context == NULL)
  1176. {
  1177. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1178. DEV_CRASH();
  1179. return;
  1180. }
  1181. FlipStoreApp *app = (FlipStoreApp *)context;
  1182. View *view = app->view_loader;
  1183. DataState current_data_state;
  1184. DataLoaderModel *loader_model = NULL;
  1185. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1186. if (!loader_model || !loader_model->fhttp)
  1187. {
  1188. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1189. DEV_CRASH();
  1190. return;
  1191. }
  1192. if (current_data_state == DataStateInitial)
  1193. {
  1194. with_view_model(
  1195. view,
  1196. DataLoaderModel * model,
  1197. {
  1198. model->data_state = DataStateRequested;
  1199. DataLoaderFetch fetch = model->fetcher;
  1200. if (fetch == NULL)
  1201. {
  1202. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1203. model->data_state = DataStateError;
  1204. return;
  1205. }
  1206. // Clear any previous responses
  1207. strncpy(model->fhttp->last_response, "", 1);
  1208. bool request_status = fetch(model);
  1209. if (!request_status)
  1210. {
  1211. model->data_state = DataStateError;
  1212. }
  1213. },
  1214. true);
  1215. }
  1216. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1217. {
  1218. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1219. {
  1220. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1221. {
  1222. FURI_LOG_DEV(TAG, "PONG received.");
  1223. }
  1224. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1225. {
  1226. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1227. }
  1228. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1229. {
  1230. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1231. }
  1232. else if (strlen(loader_model->fhttp->last_response) == 0)
  1233. {
  1234. // Still waiting on response
  1235. }
  1236. else
  1237. {
  1238. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1239. }
  1240. }
  1241. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1242. {
  1243. // continue waiting
  1244. }
  1245. else if (loader_model->fhttp->state == INACTIVE)
  1246. {
  1247. // inactive. try again
  1248. }
  1249. else if (loader_model->fhttp->state == ISSUE)
  1250. {
  1251. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1252. }
  1253. else
  1254. {
  1255. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1256. DEV_CRASH();
  1257. }
  1258. }
  1259. else if (current_data_state == DataStateReceived)
  1260. {
  1261. with_view_model(
  1262. view,
  1263. DataLoaderModel * model,
  1264. {
  1265. char *data_text;
  1266. if (model->parser == NULL)
  1267. {
  1268. data_text = NULL;
  1269. FURI_LOG_DEV(TAG, "Parser is NULL");
  1270. DEV_CRASH();
  1271. }
  1272. else
  1273. {
  1274. data_text = model->parser(model);
  1275. }
  1276. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1277. model->data_text = data_text;
  1278. if (data_text == NULL)
  1279. {
  1280. model->data_state = DataStateParseError;
  1281. }
  1282. else
  1283. {
  1284. model->data_state = DataStateParsed;
  1285. }
  1286. },
  1287. true);
  1288. }
  1289. else if (current_data_state == DataStateParsed)
  1290. {
  1291. with_view_model(
  1292. view,
  1293. DataLoaderModel * model,
  1294. {
  1295. if (++model->request_index < model->request_count)
  1296. {
  1297. model->data_state = DataStateInitial;
  1298. }
  1299. else
  1300. {
  1301. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1302. if (model->data_text != NULL)
  1303. {
  1304. free(model->data_text);
  1305. model->data_text = NULL;
  1306. }
  1307. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1308. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1309. }
  1310. },
  1311. true);
  1312. }
  1313. }
  1314. static void flip_store_loader_timer_callback(void *context)
  1315. {
  1316. if (context == NULL)
  1317. {
  1318. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1319. DEV_CRASH();
  1320. return;
  1321. }
  1322. FlipStoreApp *app = (FlipStoreApp *)context;
  1323. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1324. }
  1325. static void flip_store_loader_on_enter(void *context)
  1326. {
  1327. if (context == NULL)
  1328. {
  1329. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1330. DEV_CRASH();
  1331. return;
  1332. }
  1333. FlipStoreApp *app = (FlipStoreApp *)context;
  1334. View *view = app->view_loader;
  1335. with_view_model(
  1336. view,
  1337. DataLoaderModel * model,
  1338. {
  1339. view_set_previous_callback(view, model->back_callback);
  1340. if (model->timer == NULL)
  1341. {
  1342. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1343. }
  1344. furi_timer_start(model->timer, 250);
  1345. },
  1346. true);
  1347. }
  1348. static void flip_store_loader_on_exit(void *context)
  1349. {
  1350. if (context == NULL)
  1351. {
  1352. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1353. DEV_CRASH();
  1354. return;
  1355. }
  1356. FlipStoreApp *app = (FlipStoreApp *)context;
  1357. View *view = app->view_loader;
  1358. with_view_model(
  1359. view,
  1360. DataLoaderModel * model,
  1361. {
  1362. if (model->timer)
  1363. {
  1364. furi_timer_stop(model->timer);
  1365. }
  1366. },
  1367. false);
  1368. }
  1369. void flip_store_loader_init(View *view)
  1370. {
  1371. if (view == NULL)
  1372. {
  1373. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1374. DEV_CRASH();
  1375. return;
  1376. }
  1377. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1378. view_set_enter_callback(view, flip_store_loader_on_enter);
  1379. view_set_exit_callback(view, flip_store_loader_on_exit);
  1380. }
  1381. void flip_store_loader_free_model(View *view)
  1382. {
  1383. if (view == NULL)
  1384. {
  1385. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1386. DEV_CRASH();
  1387. return;
  1388. }
  1389. with_view_model(
  1390. view,
  1391. DataLoaderModel * model,
  1392. {
  1393. if (model->timer)
  1394. {
  1395. furi_timer_free(model->timer);
  1396. model->timer = NULL;
  1397. }
  1398. if (model->parser_context)
  1399. {
  1400. // do not free the context here, it is the app context
  1401. // free(model->parser_context);
  1402. // model->parser_context = NULL;
  1403. }
  1404. if (model->fhttp)
  1405. {
  1406. flipper_http_free(model->fhttp);
  1407. model->fhttp = NULL;
  1408. }
  1409. },
  1410. false);
  1411. }
  1412. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1413. {
  1414. if (context == NULL)
  1415. {
  1416. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1417. DEV_CRASH();
  1418. return false;
  1419. }
  1420. switch (index)
  1421. {
  1422. case FlipStoreCustomEventProcess:
  1423. flip_store_loader_process_callback(context);
  1424. return true;
  1425. default:
  1426. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1427. return false;
  1428. }
  1429. }
  1430. void flip_store_generic_switch_to_view(FlipStoreApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1431. {
  1432. if (app == NULL)
  1433. {
  1434. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1435. DEV_CRASH();
  1436. return;
  1437. }
  1438. View *view = app->view_loader;
  1439. if (view == NULL)
  1440. {
  1441. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1442. DEV_CRASH();
  1443. return;
  1444. }
  1445. with_view_model(
  1446. view,
  1447. DataLoaderModel * model,
  1448. {
  1449. model->title = title;
  1450. model->fetcher = fetcher;
  1451. model->parser = parser;
  1452. model->request_index = 0;
  1453. model->request_count = request_count;
  1454. model->back_callback = back;
  1455. model->data_state = DataStateInitial;
  1456. model->data_text = NULL;
  1457. //
  1458. model->parser_context = app;
  1459. if (!model->fhttp)
  1460. {
  1461. model->fhttp = flipper_http_alloc();
  1462. }
  1463. },
  1464. true);
  1465. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1466. }