flip_store_callback.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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 bool flip_store_dl_app_fetch(DataLoaderModel *model)
  14. {
  15. UNUSED(model);
  16. return flip_store_install_app(categories[flip_store_category_index]);
  17. }
  18. static char *flip_store_dl_app_parse(DataLoaderModel *model)
  19. {
  20. UNUSED(model);
  21. if (fhttp.state != IDLE)
  22. {
  23. return NULL;
  24. }
  25. // free the resources
  26. flipper_http_deinit();
  27. return "App installed successfully.";
  28. }
  29. static void flip_store_dl_app_switch_to_view(FlipStoreApp *app)
  30. {
  31. 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_list, FlipStoreViewLoader);
  32. }
  33. //
  34. static bool flip_store_fetch_app_list(DataLoaderModel *model)
  35. {
  36. UNUSED(model);
  37. if (!app_instance)
  38. {
  39. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  40. return false;
  41. }
  42. // initialize the http
  43. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  44. {
  45. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP.");
  46. return false;
  47. }
  48. fhttp.state = IDLE;
  49. flip_catalog_free();
  50. snprintf(
  51. fhttp.file_path,
  52. sizeof(fhttp.file_path),
  53. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", categories[flip_store_category_index]);
  54. fhttp.save_received_data = true;
  55. fhttp.is_bytes_request = false;
  56. char url[128];
  57. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/max/", categories[flip_store_category_index]);
  58. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  59. }
  60. static bool set_appropriate_list(Submenu **submenu)
  61. {
  62. if (!submenu)
  63. {
  64. FURI_LOG_E(TAG, "Submenu is NULL");
  65. return false;
  66. }
  67. if (!easy_flipper_set_submenu(submenu, FlipStoreViewAppListCategory, categories[flip_store_category_index], callback_to_app_list, &app_instance->view_dispatcher))
  68. {
  69. return false;
  70. }
  71. if (flip_store_process_app_list() && submenu && flip_catalog)
  72. {
  73. submenu_reset(*submenu);
  74. // add each app name to submenu
  75. for (int i = 0; i < MAX_APP_COUNT; i++)
  76. {
  77. if (strlen(flip_catalog[i].app_name) > 0)
  78. {
  79. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app_instance);
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. }
  86. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipStoreViewAppListCategory);
  87. return true;
  88. }
  89. else
  90. {
  91. FURI_LOG_E(TAG, "Failed to process the app list");
  92. return false;
  93. }
  94. return false;
  95. }
  96. static char *flip_store_parse_app_list(DataLoaderModel *model)
  97. {
  98. UNUSED(model);
  99. if (!app_instance)
  100. {
  101. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  102. return "Failed to fetch app list.";
  103. }
  104. return set_appropriate_list(&app_instance->submenu_app_list_category) ? "App list fetched successfully." : "Failed to fetch app list.";
  105. }
  106. static void flip_store_switch_to_app_list(FlipStoreApp *app)
  107. {
  108. 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);
  109. }
  110. //
  111. static bool flip_store_fetch_firmware(DataLoaderModel *model)
  112. {
  113. if (!app_instance)
  114. {
  115. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  116. return false;
  117. }
  118. // initialize the http
  119. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  120. {
  121. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP.");
  122. return false;
  123. }
  124. fhttp.state = IDLE;
  125. if (model->request_index == 0)
  126. {
  127. firmware_free();
  128. firmwares = firmware_alloc();
  129. if (!firmwares)
  130. {
  131. return false;
  132. }
  133. firmware_request_success = flip_store_get_firmware_file(
  134. firmwares[selected_firmware_index].links[0],
  135. firmwares[selected_firmware_index].name,
  136. strrchr(firmwares[selected_firmware_index].links[0], '/') + 1);
  137. return firmware_request_success;
  138. }
  139. else if (model->request_index == 1)
  140. {
  141. firmware_request_success_2 = flip_store_get_firmware_file(
  142. firmwares[selected_firmware_index].links[1],
  143. firmwares[selected_firmware_index].name,
  144. strrchr(firmwares[selected_firmware_index].links[1], '/') + 1);
  145. return firmware_request_success_2;
  146. }
  147. else if (model->request_index == 2)
  148. {
  149. firmware_request_success_3 = flip_store_get_firmware_file(
  150. firmwares[selected_firmware_index].links[2],
  151. firmwares[selected_firmware_index].name,
  152. strrchr(firmwares[selected_firmware_index].links[2], '/') + 1);
  153. return firmware_request_success_3;
  154. }
  155. return false;
  156. }
  157. static char *flip_store_parse_firmware(DataLoaderModel *model)
  158. {
  159. // free the resources
  160. flipper_http_deinit();
  161. if (model->request_index == 0)
  162. {
  163. if (firmware_request_success)
  164. {
  165. return "File 1 installed.";
  166. }
  167. }
  168. else if (model->request_index == 1)
  169. {
  170. if (firmware_request_success_2)
  171. {
  172. return "File 2 installed.";
  173. }
  174. }
  175. else if (model->request_index == 2)
  176. {
  177. if (firmware_request_success_3)
  178. {
  179. return "Firmware downloaded successfully";
  180. }
  181. }
  182. return NULL;
  183. }
  184. static void flip_store_switch_to_firmware_list(FlipStoreApp *app)
  185. {
  186. flip_store_generic_switch_to_view(app, firmwares[selected_firmware_index].name, flip_store_fetch_firmware, flip_store_parse_firmware, FIRMWARE_LINKS, callback_to_submenu, FlipStoreViewLoader);
  187. }
  188. // Function to draw the message on the canvas with word wrapping
  189. void draw_description(Canvas *canvas, const char *description, int x, int y)
  190. {
  191. if (description == NULL || strlen(description) == 0)
  192. {
  193. FURI_LOG_E(TAG, "User message is NULL.");
  194. return;
  195. }
  196. if (!canvas)
  197. {
  198. FURI_LOG_E(TAG, "Canvas is NULL.");
  199. return;
  200. }
  201. size_t msg_length = strlen(description);
  202. size_t start = 0;
  203. int line_num = 0;
  204. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  205. while (start < msg_length && line_num < 4)
  206. {
  207. size_t remaining = msg_length - start;
  208. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  209. if (remaining > MAX_LINE_LENGTH)
  210. {
  211. // Find the last space within the first 'len' characters
  212. size_t last_space = len;
  213. while (last_space > 0 && description[start + last_space - 1] != ' ')
  214. {
  215. last_space--;
  216. }
  217. if (last_space > 0)
  218. {
  219. len = last_space; // Adjust len to the position of the last space
  220. }
  221. }
  222. // Copy the substring to 'line' and null-terminate it
  223. memcpy(line, description + start, len);
  224. line[len] = '\0'; // Ensure the string is null-terminated
  225. // Draw the string on the canvas
  226. // Adjust the y-coordinate based on the line number
  227. canvas_draw_str_aligned(canvas, x, y + line_num * 10, AlignLeft, AlignTop, line);
  228. // Update the start position for the next line
  229. start += len;
  230. // Skip any spaces to avoid leading spaces on the next line
  231. while (start < msg_length && description[start] == ' ')
  232. {
  233. start++;
  234. }
  235. // Increment the line number
  236. line_num++;
  237. }
  238. }
  239. void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  240. {
  241. UNUSED(model);
  242. canvas_clear(canvas);
  243. canvas_set_font(canvas, FontPrimary);
  244. char title[30];
  245. snprintf(title, 30, "%s (v.%s)", flip_catalog[app_selected_index].app_name, flip_catalog[app_selected_index].app_version);
  246. canvas_draw_str(canvas, 0, 10, title);
  247. canvas_set_font(canvas, FontSecondary);
  248. draw_description(canvas, flip_catalog[app_selected_index].app_description, 0, 13);
  249. if (flip_store_app_does_exist)
  250. {
  251. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  252. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  253. canvas_draw_icon(canvas, 45, 53, &I_ButtonBACK_10x8);
  254. canvas_draw_str_aligned(canvas, 57, 54, AlignLeft, AlignTop, "Back");
  255. }
  256. else
  257. {
  258. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  259. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  260. }
  261. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  262. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  263. }
  264. bool flip_store_input_callback(InputEvent *event, void *context)
  265. {
  266. FlipStoreApp *app = (FlipStoreApp *)context;
  267. if (!app)
  268. {
  269. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  270. return false;
  271. }
  272. if (event->type == InputTypeShort)
  273. {
  274. if (event->key == InputKeyLeft && flip_store_app_does_exist)
  275. {
  276. // Left button clicked, delete the app
  277. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppDelete);
  278. return true;
  279. }
  280. if (event->key == InputKeyRight)
  281. {
  282. // Right button clicked, download the app
  283. flip_store_dl_app_switch_to_view(app);
  284. return true;
  285. }
  286. }
  287. else if (event->type == InputTypePress)
  288. {
  289. if (event->key == InputKeyBack)
  290. {
  291. // Back button clicked, switch to the previous view.
  292. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. void flip_store_text_updated_ssid(void *context)
  299. {
  300. FlipStoreApp *app = (FlipStoreApp *)context;
  301. if (!app)
  302. {
  303. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  304. return;
  305. }
  306. // store the entered text
  307. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  308. // Ensure null-termination
  309. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  310. // update the variable item text
  311. if (app->variable_item_ssid)
  312. {
  313. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  314. }
  315. // save the settings
  316. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  317. // if SSID and PASS are not empty, connect to the WiFi
  318. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  319. {
  320. // save wifi settings
  321. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  322. {
  323. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  324. }
  325. }
  326. // switch to the settings view
  327. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  328. }
  329. void flip_store_text_updated_pass(void *context)
  330. {
  331. FlipStoreApp *app = (FlipStoreApp *)context;
  332. if (!app)
  333. {
  334. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  335. return;
  336. }
  337. // store the entered text
  338. strncpy(app->uart_text_input_buffer_pass, app->uart_text_input_temp_buffer_pass, app->uart_text_input_buffer_size_pass);
  339. // Ensure null-termination
  340. app->uart_text_input_buffer_pass[app->uart_text_input_buffer_size_pass - 1] = '\0';
  341. // update the variable item text
  342. if (app->variable_item_pass)
  343. {
  344. variable_item_set_current_value_text(app->variable_item_pass, app->uart_text_input_buffer_pass);
  345. }
  346. // save the settings
  347. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  348. // if SSID and PASS are not empty, connect to the WiFi
  349. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  350. {
  351. // save wifi settings
  352. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  353. {
  354. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  355. }
  356. }
  357. // switch to the settings view
  358. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  359. }
  360. uint32_t callback_to_submenu(void *context)
  361. {
  362. if (!context)
  363. {
  364. FURI_LOG_E(TAG, "Context is NULL");
  365. return VIEW_NONE;
  366. }
  367. UNUSED(context);
  368. firmware_free();
  369. return FlipStoreViewSubmenu;
  370. }
  371. uint32_t callback_to_submenu_options(void *context)
  372. {
  373. if (!context)
  374. {
  375. FURI_LOG_E(TAG, "Context is NULL");
  376. return VIEW_NONE;
  377. }
  378. UNUSED(context);
  379. firmware_free();
  380. return FlipStoreViewSubmenuOptions;
  381. }
  382. uint32_t callback_to_firmware_list(void *context)
  383. {
  384. if (!context)
  385. {
  386. FURI_LOG_E(TAG, "Context is NULL");
  387. return VIEW_NONE;
  388. }
  389. UNUSED(context);
  390. sent_firmware_request = false;
  391. sent_firmware_request_2 = false;
  392. sent_firmware_request_3 = false;
  393. //
  394. firmware_request_success = false;
  395. firmware_request_success_2 = false;
  396. firmware_request_success_3 = false;
  397. //
  398. firmware_download_success = false;
  399. firmware_download_success_2 = false;
  400. firmware_download_success_3 = false;
  401. return FlipStoreViewFirmwares;
  402. }
  403. uint32_t callback_to_app_list(void *context)
  404. {
  405. if (!context)
  406. {
  407. FURI_LOG_E(TAG, "Context is NULL");
  408. return VIEW_NONE;
  409. }
  410. UNUSED(context);
  411. flip_store_sent_request = false;
  412. flip_store_success = false;
  413. flip_store_saved_data = false;
  414. flip_store_saved_success = false;
  415. flip_store_app_does_exist = false;
  416. sent_firmware_request = false;
  417. return FlipStoreViewAppList;
  418. }
  419. void settings_item_selected(void *context, uint32_t index)
  420. {
  421. FlipStoreApp *app = (FlipStoreApp *)context;
  422. if (!app)
  423. {
  424. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  425. return;
  426. }
  427. switch (index)
  428. {
  429. case 0: // Input SSID
  430. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputSSID);
  431. break;
  432. case 1: // Input Password
  433. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputPass);
  434. break;
  435. default:
  436. FURI_LOG_E(TAG, "Unknown configuration item index");
  437. break;
  438. }
  439. }
  440. void dialog_delete_callback(DialogExResult result, void *context)
  441. {
  442. furi_assert(context);
  443. FlipStoreApp *app = (FlipStoreApp *)context;
  444. if (result == DialogExResultLeft) // No
  445. {
  446. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  447. }
  448. else if (result == DialogExResultRight)
  449. {
  450. // delete the app then return to the app list
  451. if (!delete_app(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]))
  452. {
  453. // pop up a message
  454. popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
  455. popup_set_text(app->popup, "Issue deleting app.", 0, 50, AlignLeft, AlignTop);
  456. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  457. furi_delay_ms(2000); // delay for 2 seconds
  458. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  459. }
  460. else
  461. {
  462. // pop up a message
  463. popup_set_header(app->popup, "[SUCCESS]", 0, 0, AlignLeft, AlignTop);
  464. popup_set_text(app->popup, "App deleted successfully.", 0, 50, AlignLeft, AlignTop);
  465. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  466. furi_delay_ms(2000); // delay for 2 seconds
  467. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  468. }
  469. }
  470. }
  471. void dialog_firmware_callback(DialogExResult result, void *context)
  472. {
  473. furi_assert(context);
  474. FlipStoreApp *app = (FlipStoreApp *)context;
  475. if (result == DialogExResultLeft) // No
  476. {
  477. // switch to the firmware list
  478. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  479. }
  480. else if (result == DialogExResultRight)
  481. {
  482. // download the firmware then return to the firmware list
  483. flip_store_switch_to_firmware_list(app);
  484. }
  485. }
  486. void popup_callback(void *context)
  487. {
  488. FlipStoreApp *app = (FlipStoreApp *)context;
  489. if (!app)
  490. {
  491. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  492. return;
  493. }
  494. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenu);
  495. }
  496. static bool alloc_about_view(FlipStoreApp *app)
  497. {
  498. if (!app)
  499. {
  500. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  501. return false;
  502. }
  503. if (!app->widget)
  504. {
  505. if (!easy_flipper_set_widget(
  506. &app->widget,
  507. FlipStoreViewAbout,
  508. "Welcome to the FlipStore!\n------\nDownload apps via WiFi and\nrun them on your Flipper!\n------\nwww.github.com/jblanked",
  509. callback_to_submenu,
  510. &app->view_dispatcher))
  511. {
  512. return false;
  513. }
  514. if (!app->widget)
  515. {
  516. return false;
  517. }
  518. }
  519. return true;
  520. }
  521. static void free_about_view()
  522. {
  523. if (!app_instance)
  524. {
  525. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  526. return;
  527. }
  528. if (app_instance->widget != NULL)
  529. {
  530. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipStoreViewAbout);
  531. widget_free(app_instance->widget);
  532. app_instance->widget = NULL;
  533. }
  534. }
  535. static void free_all_views()
  536. {
  537. free_about_view();
  538. // clear app list views
  539. if (app_instance->submenu_app_list_category)
  540. {
  541. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipStoreViewAppListCategory);
  542. submenu_free(app_instance->submenu_app_list_category);
  543. app_instance->submenu_app_list_category = NULL;
  544. }
  545. }
  546. uint32_t callback_exit_app(void *context)
  547. {
  548. UNUSED(context);
  549. free_all_views();
  550. return VIEW_NONE; // Return VIEW_NONE to exit the app
  551. }
  552. void callback_submenu_choices(void *context, uint32_t index)
  553. {
  554. FlipStoreApp *app = (FlipStoreApp *)context;
  555. if (!app)
  556. {
  557. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  558. return;
  559. }
  560. switch (index)
  561. {
  562. case FlipStoreSubmenuIndexAbout:
  563. free_all_views();
  564. if (!alloc_about_view(app))
  565. {
  566. FURI_LOG_E(TAG, "Failed to set about view");
  567. return;
  568. }
  569. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  570. break;
  571. case FlipStoreSubmenuIndexSettings:
  572. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  573. break;
  574. case FlipStoreSubmenuIndexOptions:
  575. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  576. break;
  577. case FlipStoreSubmenuIndexAppList:
  578. flip_store_category_index = 0;
  579. flip_store_app_does_exist = false;
  580. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  581. break;
  582. case FlipStoreSubmenuIndexFirmwares:
  583. if (!app->submenu_firmwares)
  584. {
  585. FURI_LOG_E(TAG, "Submenu firmwares is NULL");
  586. return;
  587. }
  588. firmwares = firmware_alloc();
  589. if (firmwares == NULL)
  590. {
  591. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  592. return;
  593. }
  594. submenu_reset(app->submenu_firmwares);
  595. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  596. for (int i = 0; i < FIRMWARE_COUNT; i++)
  597. {
  598. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  599. }
  600. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  601. break;
  602. case FlipStoreSubmenuIndexAppListBluetooth:
  603. free_all_views();
  604. flip_store_category_index = 0;
  605. flip_store_app_does_exist = false;
  606. flip_store_switch_to_app_list(app);
  607. break;
  608. case FlipStoreSubmenuIndexAppListGames:
  609. free_all_views();
  610. flip_store_category_index = 1;
  611. flip_store_app_does_exist = false;
  612. flip_store_switch_to_app_list(app);
  613. break;
  614. case FlipStoreSubmenuIndexAppListGPIO:
  615. free_all_views();
  616. flip_store_category_index = 2;
  617. flip_store_app_does_exist = false;
  618. flip_store_switch_to_app_list(app);
  619. break;
  620. case FlipStoreSubmenuIndexAppListInfrared:
  621. free_all_views();
  622. flip_store_category_index = 3;
  623. flip_store_app_does_exist = false;
  624. flip_store_switch_to_app_list(app);
  625. break;
  626. case FlipStoreSubmenuIndexAppListiButton:
  627. free_all_views();
  628. flip_store_category_index = 4;
  629. flip_store_app_does_exist = false;
  630. flip_store_switch_to_app_list(app);
  631. break;
  632. case FlipStoreSubmenuIndexAppListMedia:
  633. free_all_views();
  634. flip_store_category_index = 5;
  635. flip_store_app_does_exist = false;
  636. flip_store_switch_to_app_list(app);
  637. break;
  638. case FlipStoreSubmenuIndexAppListNFC:
  639. free_all_views();
  640. flip_store_category_index = 6;
  641. flip_store_app_does_exist = false;
  642. flip_store_switch_to_app_list(app);
  643. break;
  644. case FlipStoreSubmenuIndexAppListRFID:
  645. free_all_views();
  646. flip_store_category_index = 7;
  647. flip_store_app_does_exist = false;
  648. flip_store_switch_to_app_list(app);
  649. break;
  650. case FlipStoreSubmenuIndexAppListSubGHz:
  651. free_all_views();
  652. flip_store_category_index = 8;
  653. flip_store_app_does_exist = false;
  654. flip_store_switch_to_app_list(app);
  655. break;
  656. case FlipStoreSubmenuIndexAppListTools:
  657. free_all_views();
  658. flip_store_category_index = 9;
  659. flip_store_app_does_exist = false;
  660. flip_store_switch_to_app_list(app);
  661. break;
  662. case FlipStoreSubmenuIndexAppListUSB:
  663. free_all_views();
  664. flip_store_category_index = 10;
  665. flip_store_app_does_exist = false;
  666. flip_store_switch_to_app_list(app);
  667. break;
  668. default:
  669. // Check if the index is within the firmwares list range
  670. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  671. {
  672. // Get the firmware index
  673. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  674. // Check if the firmware index is valid
  675. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  676. {
  677. // Get the firmware name
  678. selected_firmware_index = firmware_index;
  679. // Switch to the firmware download view
  680. dialog_ex_set_header(app->dialog_firmware, firmwares[firmware_index].name, 0, 0, AlignLeft, AlignTop);
  681. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  682. }
  683. else
  684. {
  685. FURI_LOG_E(TAG, "Invalid firmware index");
  686. popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
  687. popup_set_text(app->popup, "Issue parsing firmwarex", 0, 50, AlignLeft, AlignTop);
  688. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  689. }
  690. }
  691. // Check if the index is within the app list range
  692. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  693. {
  694. // Get the app index
  695. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  696. // Check if the app index is valid
  697. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  698. {
  699. // Get the app name
  700. char *app_name = flip_catalog[app_index].app_name;
  701. // Check if the app name is valid
  702. if (app_name != NULL && strlen(app_name) > 0)
  703. {
  704. app_selected_index = app_index;
  705. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  706. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  707. }
  708. else
  709. {
  710. FURI_LOG_E(TAG, "Invalid app name");
  711. }
  712. }
  713. else
  714. {
  715. FURI_LOG_E(TAG, "Invalid app index");
  716. }
  717. }
  718. else
  719. {
  720. FURI_LOG_E(TAG, "Unknown submenu index");
  721. }
  722. break;
  723. }
  724. }
  725. static void flip_store_widget_set_text(char *message, Widget **widget)
  726. {
  727. if (widget == NULL)
  728. {
  729. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  730. DEV_CRASH();
  731. return;
  732. }
  733. if (message == NULL)
  734. {
  735. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  736. DEV_CRASH();
  737. return;
  738. }
  739. widget_reset(*widget);
  740. uint32_t message_length = strlen(message); // Length of the message
  741. uint32_t i = 0; // Index tracker
  742. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  743. char *formatted_message; // Buffer to hold the final formatted message
  744. // Allocate buffer with double the message length plus one for safety
  745. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  746. {
  747. return;
  748. }
  749. while (i < message_length)
  750. {
  751. uint32_t max_line_length = 31; // Maximum characters per line
  752. uint32_t remaining_length = message_length - i; // Remaining characters
  753. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  754. // Check for newline character within the current segment
  755. uint32_t newline_pos = i;
  756. bool found_newline = false;
  757. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  758. {
  759. if (message[newline_pos] == '\n')
  760. {
  761. found_newline = true;
  762. break;
  763. }
  764. }
  765. if (found_newline)
  766. {
  767. // If newline found, set line_length up to the newline
  768. line_length = newline_pos - i;
  769. }
  770. // Temporary buffer to hold the current line
  771. char line[32];
  772. strncpy(line, message + i, line_length);
  773. line[line_length] = '\0';
  774. // If newline was found, skip it for the next iteration
  775. if (found_newline)
  776. {
  777. i += line_length + 1; // +1 to skip the '\n' character
  778. }
  779. else
  780. {
  781. // Check if the line ends in the middle of a word and adjust accordingly
  782. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  783. {
  784. // Find the last space within the current line to avoid breaking a word
  785. char *last_space = strrchr(line, ' ');
  786. if (last_space != NULL)
  787. {
  788. // Adjust the line_length to avoid cutting the word
  789. line_length = last_space - line;
  790. line[line_length] = '\0'; // Null-terminate at the space
  791. }
  792. }
  793. // Move the index forward by the determined line_length
  794. i += line_length;
  795. // Skip any spaces at the beginning of the next line
  796. while (i < message_length && message[i] == ' ')
  797. {
  798. i++;
  799. }
  800. }
  801. // Manually copy the fixed line into the formatted_message buffer
  802. for (uint32_t j = 0; j < line_length; j++)
  803. {
  804. formatted_message[formatted_index++] = line[j];
  805. }
  806. // Add a newline character for line spacing
  807. formatted_message[formatted_index++] = '\n';
  808. }
  809. // Null-terminate the formatted_message
  810. formatted_message[formatted_index] = '\0';
  811. // Add the formatted message to the widget
  812. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  813. }
  814. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  815. {
  816. if (!canvas || !model)
  817. {
  818. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  819. return;
  820. }
  821. SerialState http_state = fhttp.state;
  822. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  823. DataState data_state = data_loader_model->data_state;
  824. char *title = data_loader_model->title;
  825. canvas_set_font(canvas, FontSecondary);
  826. if (http_state == INACTIVE)
  827. {
  828. canvas_draw_str(canvas, 0, 7, "WiFi Dev Board disconnected.");
  829. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  830. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  831. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  832. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  833. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  834. return;
  835. }
  836. if (data_state == DataStateError || data_state == DataStateParseError)
  837. {
  838. flip_store_request_error(canvas);
  839. return;
  840. }
  841. canvas_draw_str(canvas, 0, 7, title);
  842. canvas_draw_str(canvas, 0, 17, "Loading...");
  843. if (data_state == DataStateInitial)
  844. {
  845. return;
  846. }
  847. if (http_state == SENDING)
  848. {
  849. canvas_draw_str(canvas, 0, 27, "Fetching...");
  850. return;
  851. }
  852. if (http_state == RECEIVING || data_state == DataStateRequested)
  853. {
  854. canvas_draw_str(canvas, 0, 27, "Receiving...");
  855. return;
  856. }
  857. if (http_state == IDLE && data_state == DataStateReceived)
  858. {
  859. canvas_draw_str(canvas, 0, 27, "Processing...");
  860. return;
  861. }
  862. if (http_state == IDLE && data_state == DataStateParsed)
  863. {
  864. canvas_draw_str(canvas, 0, 27, "Processed...");
  865. return;
  866. }
  867. }
  868. static void flip_store_loader_process_callback(void *context)
  869. {
  870. if (context == NULL)
  871. {
  872. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  873. DEV_CRASH();
  874. return;
  875. }
  876. FlipStoreApp *app = (FlipStoreApp *)context;
  877. View *view = app->view_loader;
  878. DataState current_data_state;
  879. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  880. if (current_data_state == DataStateInitial)
  881. {
  882. with_view_model(
  883. view,
  884. DataLoaderModel * model,
  885. {
  886. model->data_state = DataStateRequested;
  887. DataLoaderFetch fetch = model->fetcher;
  888. if (fetch == NULL)
  889. {
  890. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  891. model->data_state = DataStateError;
  892. return;
  893. }
  894. // Clear any previous responses
  895. if (fhttp.last_response != NULL)
  896. {
  897. free(fhttp.last_response);
  898. fhttp.last_response = NULL;
  899. }
  900. // strncpy(fhttp.last_response, "", 1);
  901. bool request_status = fetch(model);
  902. if (!request_status)
  903. {
  904. model->data_state = DataStateError;
  905. }
  906. },
  907. true);
  908. }
  909. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  910. {
  911. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  912. {
  913. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  914. {
  915. FURI_LOG_DEV(TAG, "PONG received.");
  916. }
  917. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  918. {
  919. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  920. }
  921. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  922. {
  923. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  924. }
  925. else if (strlen(fhttp.last_response) == 0)
  926. {
  927. // Still waiting on response
  928. }
  929. else
  930. {
  931. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  932. }
  933. }
  934. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  935. {
  936. // continue waiting
  937. }
  938. else if (fhttp.state == INACTIVE)
  939. {
  940. // inactive. try again
  941. }
  942. else if (fhttp.state == ISSUE)
  943. {
  944. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  945. }
  946. else
  947. {
  948. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  949. DEV_CRASH();
  950. }
  951. }
  952. else if (current_data_state == DataStateReceived)
  953. {
  954. with_view_model(
  955. view,
  956. DataLoaderModel * model,
  957. {
  958. char *data_text;
  959. if (model->parser == NULL)
  960. {
  961. data_text = NULL;
  962. FURI_LOG_DEV(TAG, "Parser is NULL");
  963. DEV_CRASH();
  964. }
  965. else
  966. {
  967. data_text = model->parser(model);
  968. }
  969. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  970. model->data_text = data_text;
  971. if (data_text == NULL)
  972. {
  973. model->data_state = DataStateParseError;
  974. }
  975. else
  976. {
  977. model->data_state = DataStateParsed;
  978. }
  979. },
  980. true);
  981. }
  982. else if (current_data_state == DataStateParsed)
  983. {
  984. with_view_model(
  985. view,
  986. DataLoaderModel * model,
  987. {
  988. if (++model->request_index < model->request_count)
  989. {
  990. model->data_state = DataStateInitial;
  991. }
  992. else
  993. {
  994. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app->widget_result);
  995. if (model->data_text != NULL)
  996. {
  997. free(model->data_text);
  998. model->data_text = NULL;
  999. }
  1000. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1001. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1002. }
  1003. },
  1004. true);
  1005. }
  1006. }
  1007. static void flip_store_loader_timer_callback(void *context)
  1008. {
  1009. if (context == NULL)
  1010. {
  1011. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1012. DEV_CRASH();
  1013. return;
  1014. }
  1015. FlipStoreApp *app = (FlipStoreApp *)context;
  1016. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1017. }
  1018. static void flip_store_loader_on_enter(void *context)
  1019. {
  1020. if (context == NULL)
  1021. {
  1022. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1023. DEV_CRASH();
  1024. return;
  1025. }
  1026. FlipStoreApp *app = (FlipStoreApp *)context;
  1027. View *view = app->view_loader;
  1028. with_view_model(
  1029. view,
  1030. DataLoaderModel * model,
  1031. {
  1032. view_set_previous_callback(view, model->back_callback);
  1033. if (model->timer == NULL)
  1034. {
  1035. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1036. }
  1037. furi_timer_start(model->timer, 250);
  1038. },
  1039. true);
  1040. }
  1041. static void flip_store_loader_on_exit(void *context)
  1042. {
  1043. if (context == NULL)
  1044. {
  1045. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1046. DEV_CRASH();
  1047. return;
  1048. }
  1049. FlipStoreApp *app = (FlipStoreApp *)context;
  1050. View *view = app->view_loader;
  1051. with_view_model(
  1052. view,
  1053. DataLoaderModel * model,
  1054. {
  1055. if (model->timer)
  1056. {
  1057. furi_timer_stop(model->timer);
  1058. }
  1059. },
  1060. false);
  1061. }
  1062. void flip_store_loader_init(View *view)
  1063. {
  1064. if (view == NULL)
  1065. {
  1066. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1067. DEV_CRASH();
  1068. return;
  1069. }
  1070. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1071. view_set_enter_callback(view, flip_store_loader_on_enter);
  1072. view_set_exit_callback(view, flip_store_loader_on_exit);
  1073. }
  1074. void flip_store_loader_free_model(View *view)
  1075. {
  1076. if (view == NULL)
  1077. {
  1078. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1079. DEV_CRASH();
  1080. return;
  1081. }
  1082. with_view_model(
  1083. view,
  1084. DataLoaderModel * model,
  1085. {
  1086. if (model->timer)
  1087. {
  1088. furi_timer_free(model->timer);
  1089. model->timer = NULL;
  1090. }
  1091. if (model->parser_context)
  1092. {
  1093. free(model->parser_context);
  1094. model->parser_context = NULL;
  1095. }
  1096. },
  1097. false);
  1098. }
  1099. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1100. {
  1101. if (context == NULL)
  1102. {
  1103. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1104. DEV_CRASH();
  1105. return false;
  1106. }
  1107. switch (index)
  1108. {
  1109. case FlipStoreCustomEventProcess:
  1110. flip_store_loader_process_callback(context);
  1111. return true;
  1112. default:
  1113. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1114. return false;
  1115. }
  1116. }
  1117. 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)
  1118. {
  1119. if (app == NULL)
  1120. {
  1121. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1122. DEV_CRASH();
  1123. return;
  1124. }
  1125. View *view = app->view_loader;
  1126. if (view == NULL)
  1127. {
  1128. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1129. DEV_CRASH();
  1130. return;
  1131. }
  1132. with_view_model(
  1133. view,
  1134. DataLoaderModel * model,
  1135. {
  1136. model->title = title;
  1137. model->fetcher = fetcher;
  1138. model->parser = parser;
  1139. model->request_index = 0;
  1140. model->request_count = request_count;
  1141. model->back_callback = back;
  1142. model->data_state = DataStateInitial;
  1143. model->data_text = NULL;
  1144. },
  1145. true);
  1146. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1147. }