flip_store_callback.c 59 KB

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