flip_store_callback.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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. uart_text_input_reset(app->uart_text_input);
  357. uart_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. uart_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(DataLoaderModel *model)
  367. {
  368. if (!model || !model->fhttp)
  369. {
  370. FURI_LOG_E(TAG, "Model/FlipperHTTP is NULL");
  371. return false;
  372. }
  373. if (model->request_index == 0)
  374. {
  375. char author[64];
  376. char repo[64];
  377. if (!load_char("Github-Author", author, sizeof(author)) || !load_char("Github-Repo", repo, sizeof(repo)))
  378. {
  379. FURI_LOG_E(TAG, "Failed to load Github author or repo");
  380. return false;
  381. }
  382. return flip_store_get_github_contents(model->fhttp, author, repo);
  383. }
  384. return false; // return false for now
  385. }
  386. static char *flip_store_parse_github(DataLoaderModel *model)
  387. {
  388. if (model->request_index == 0)
  389. {
  390. char author[64];
  391. char repo[64];
  392. if (!load_char("Github-Author", author, sizeof(author)) || !load_char("Github-Repo", repo, sizeof(repo)))
  393. {
  394. FURI_LOG_E(TAG, "Failed to load Github author or repo");
  395. return "Failed to load Github author or repo";
  396. }
  397. if (!flip_store_parse_github_contents(model->fhttp->file_path, author, repo))
  398. {
  399. return "Failed to parse Github contents";
  400. }
  401. if (!flip_store_install_all_github_files(model->fhttp, author, repo))
  402. {
  403. return "Failed to install all Github files";
  404. }
  405. return "Repository downloaded successfully";
  406. }
  407. return "Failed to download repository.";
  408. }
  409. static void flip_store_github_switch_to_view(FlipStoreApp *app)
  410. {
  411. flip_store_generic_switch_to_view(app, "Downloading Repository..", flip_store_fetch_github, flip_store_parse_github, 2, callback_to_submenu_options, FlipStoreViewLoader);
  412. }
  413. static void flip_store_text_updated_repo(void *context)
  414. {
  415. FlipStoreApp *app = (FlipStoreApp *)context;
  416. if (!app)
  417. {
  418. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  419. return;
  420. }
  421. // store the entered text
  422. strncpy(app->uart_text_input_buffer, app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size);
  423. // Ensure null-termination
  424. app->uart_text_input_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  425. // save the setting
  426. save_char("Github-Repo", app->uart_text_input_buffer);
  427. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  428. flip_store_github_switch_to_view(app);
  429. }
  430. static void free_category_submenu(FlipStoreApp *app)
  431. {
  432. if (!app)
  433. {
  434. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  435. return;
  436. }
  437. if (app->submenu_app_list_category)
  438. {
  439. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  440. submenu_free(app->submenu_app_list_category);
  441. app->submenu_app_list_category = NULL;
  442. }
  443. }
  444. static void free_variable_item_list(FlipStoreApp *app);
  445. uint32_t callback_to_submenu(void *context)
  446. {
  447. UNUSED(context);
  448. firmware_free();
  449. return FlipStoreViewSubmenu;
  450. }
  451. uint32_t callback_to_firmware_list(void *context)
  452. {
  453. UNUSED(context);
  454. return FlipStoreViewFirmwares;
  455. }
  456. uint32_t callback_to_vgm_firmware_list(void *context)
  457. {
  458. UNUSED(context);
  459. return FlipStoreViewVGMFirmwares;
  460. }
  461. static uint32_t callback_to_app_category_list(void *context)
  462. {
  463. UNUSED(context);
  464. return FlipStoreViewAppListCategory;
  465. }
  466. uint32_t callback_to_app_list(void *context)
  467. {
  468. UNUSED(context);
  469. return FlipStoreViewAppList;
  470. }
  471. static uint32_t callback_to_wifi_settings(void *context)
  472. {
  473. UNUSED(context);
  474. return FlipStoreViewSettings;
  475. }
  476. static void dialog_firmware_callback(DialogExResult result, void *context)
  477. {
  478. FlipStoreApp *app = (FlipStoreApp *)context;
  479. if (!app)
  480. {
  481. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  482. return;
  483. }
  484. if (result == DialogExResultLeft) // No
  485. {
  486. // switch to the firmware list
  487. if (is_esp32_firmware)
  488. {
  489. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  490. }
  491. else
  492. {
  493. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewVGMFirmwares);
  494. }
  495. }
  496. else if (result == DialogExResultRight)
  497. {
  498. // download the firmware then return to the firmware list
  499. if (is_esp32_firmware)
  500. {
  501. flip_store_switch_to_firmware_list(app);
  502. }
  503. else
  504. {
  505. flip_store_switch_to_vgm_firmware_list(app);
  506. }
  507. }
  508. }
  509. static bool alloc_about_view(FlipStoreApp *app)
  510. {
  511. if (!app)
  512. {
  513. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  514. return false;
  515. }
  516. if (!app->widget_about)
  517. {
  518. if (!easy_flipper_set_widget(
  519. &app->widget_about,
  520. FlipStoreViewAbout,
  521. "Welcome to the FlipStore!\n------\nDownload apps via WiFi and\nrun them on your Flipper!\n------\nwww.github.com/jblanked",
  522. callback_to_submenu,
  523. &app->view_dispatcher))
  524. {
  525. return false;
  526. }
  527. if (!app->widget_about)
  528. {
  529. return false;
  530. }
  531. }
  532. return true;
  533. }
  534. static void free_about_view(FlipStoreApp *app)
  535. {
  536. if (app && app->widget_about != NULL)
  537. {
  538. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAbout);
  539. widget_free(app->widget_about);
  540. app->widget_about = NULL;
  541. }
  542. }
  543. static bool alloc_text_input_view(void *context, char *title)
  544. {
  545. FlipStoreApp *app = (FlipStoreApp *)context;
  546. if (!app)
  547. {
  548. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  549. return false;
  550. }
  551. if (!title)
  552. {
  553. FURI_LOG_E(TAG, "Title is NULL");
  554. return false;
  555. }
  556. app->uart_text_input_buffer_size = 64;
  557. if (!app->uart_text_input_buffer)
  558. {
  559. if (!easy_flipper_set_buffer(&app->uart_text_input_buffer, app->uart_text_input_buffer_size))
  560. {
  561. return false;
  562. }
  563. }
  564. if (!app->uart_text_input_temp_buffer)
  565. {
  566. if (!easy_flipper_set_buffer(&app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size))
  567. {
  568. return false;
  569. }
  570. }
  571. if (!app->uart_text_input)
  572. {
  573. if (strcmp(title, "SSID") != 0 && strcmp(title, "Password") != 0)
  574. {
  575. // Github repository download
  576. if (!easy_flipper_set_uart_text_input(
  577. &app->uart_text_input,
  578. FlipStoreViewTextInput,
  579. title,
  580. app->uart_text_input_temp_buffer,
  581. app->uart_text_input_buffer_size,
  582. strcmp(title, "Author") == 0 ? flip_store_text_updated_author : flip_store_text_updated_repo,
  583. callback_to_submenu_options,
  584. &app->view_dispatcher,
  585. app))
  586. {
  587. return false;
  588. }
  589. if (!app->uart_text_input)
  590. {
  591. return false;
  592. }
  593. }
  594. else
  595. {
  596. if (!easy_flipper_set_uart_text_input(
  597. &app->uart_text_input,
  598. FlipStoreViewTextInput,
  599. title,
  600. app->uart_text_input_temp_buffer,
  601. app->uart_text_input_buffer_size,
  602. strcmp(title, "SSID") == 0 ? flip_store_text_updated_ssid : flip_store_text_updated_pass,
  603. callback_to_wifi_settings,
  604. &app->view_dispatcher,
  605. app))
  606. {
  607. return false;
  608. }
  609. if (!app->uart_text_input)
  610. {
  611. return false;
  612. }
  613. char ssid[64];
  614. char pass[64];
  615. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  616. {
  617. if (strcmp(title, "SSID") == 0)
  618. {
  619. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size);
  620. }
  621. else
  622. {
  623. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size);
  624. }
  625. }
  626. }
  627. }
  628. return true;
  629. }
  630. static void free_text_input_view(FlipStoreApp *app)
  631. {
  632. if (!app)
  633. {
  634. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  635. return;
  636. }
  637. if (app->uart_text_input)
  638. {
  639. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewTextInput);
  640. uart_text_input_free(app->uart_text_input);
  641. app->uart_text_input = NULL;
  642. }
  643. if (app->uart_text_input_buffer)
  644. {
  645. free(app->uart_text_input_buffer);
  646. app->uart_text_input_buffer = NULL;
  647. }
  648. if (app->uart_text_input_temp_buffer)
  649. {
  650. free(app->uart_text_input_temp_buffer);
  651. app->uart_text_input_temp_buffer = NULL;
  652. }
  653. }
  654. static void settings_item_selected(void *context, uint32_t index);
  655. static bool alloc_variable_item_list(void *context)
  656. {
  657. FlipStoreApp *app = (FlipStoreApp *)context;
  658. if (!app)
  659. {
  660. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  661. return false;
  662. }
  663. if (!app->variable_item_list)
  664. {
  665. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipStoreViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  666. return false;
  667. if (!app->variable_item_list)
  668. return false;
  669. if (!app->variable_item_ssid)
  670. {
  671. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  672. variable_item_set_current_value_text(app->variable_item_ssid, "");
  673. }
  674. if (!app->variable_item_pass)
  675. {
  676. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  677. variable_item_set_current_value_text(app->variable_item_pass, "");
  678. }
  679. char ssid[64];
  680. char pass[64];
  681. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  682. {
  683. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  684. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  685. save_char("WiFi-SSID", ssid);
  686. save_char("WiFi-Password", pass);
  687. }
  688. }
  689. return true;
  690. }
  691. static void free_variable_item_list(FlipStoreApp *app)
  692. {
  693. if (!app)
  694. {
  695. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  696. return;
  697. }
  698. if (app->variable_item_list)
  699. {
  700. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewSettings);
  701. variable_item_list_free(app->variable_item_list);
  702. app->variable_item_list = NULL;
  703. }
  704. if (app->variable_item_ssid)
  705. {
  706. free(app->variable_item_ssid);
  707. app->variable_item_ssid = NULL;
  708. }
  709. if (app->variable_item_pass)
  710. {
  711. free(app->variable_item_pass);
  712. app->variable_item_pass = NULL;
  713. }
  714. }
  715. static bool alloc_dialog_firmware(void *context)
  716. {
  717. FlipStoreApp *app = (FlipStoreApp *)context;
  718. if (!app)
  719. {
  720. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  721. return false;
  722. }
  723. if (!app->dialog_firmware)
  724. {
  725. if (!easy_flipper_set_dialog_ex(
  726. &app->dialog_firmware,
  727. FlipStoreViewFirmwareDialog,
  728. is_esp32_firmware ? "Download ESP32 Firmware" : "Download VGM Firmware",
  729. 0,
  730. 0,
  731. "Are you sure you want to\ndownload this firmware?",
  732. 0,
  733. 10,
  734. "No",
  735. "Yes",
  736. NULL,
  737. dialog_firmware_callback,
  738. callback_to_firmware_list,
  739. &app->view_dispatcher,
  740. app))
  741. {
  742. return false;
  743. }
  744. if (!app->dialog_firmware)
  745. {
  746. return false;
  747. }
  748. if (is_esp32_firmware)
  749. {
  750. dialog_ex_set_header(app->dialog_firmware, firmwares[selected_firmware_index].name, 0, 0, AlignLeft, AlignTop);
  751. }
  752. else
  753. {
  754. dialog_ex_set_header(app->dialog_firmware, vgm_firmwares[selected_firmware_index].name, 0, 0, AlignLeft, AlignTop);
  755. }
  756. }
  757. return true;
  758. }
  759. static void free_dialog_firmware(FlipStoreApp *app)
  760. {
  761. if (app && app->dialog_firmware)
  762. {
  763. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  764. dialog_ex_free(app->dialog_firmware);
  765. app->dialog_firmware = NULL;
  766. }
  767. }
  768. static bool alloc_app_info_view(FlipStoreApp *app)
  769. {
  770. if (!app)
  771. {
  772. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  773. return false;
  774. }
  775. if (!app->view_app_info)
  776. {
  777. if (!easy_flipper_set_view(
  778. &app->view_app_info,
  779. FlipStoreViewAppInfo,
  780. flip_store_view_draw_callback_app_list,
  781. flip_store_input_callback,
  782. callback_to_app_category_list,
  783. &app->view_dispatcher,
  784. app))
  785. {
  786. return false;
  787. }
  788. if (!app->view_app_info)
  789. {
  790. return false;
  791. }
  792. }
  793. return true;
  794. }
  795. static void free_app_info_view(FlipStoreApp *app)
  796. {
  797. if (!app)
  798. {
  799. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  800. return;
  801. }
  802. if (app->view_app_info)
  803. {
  804. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppInfo);
  805. view_free(app->view_app_info);
  806. app->view_app_info = NULL;
  807. }
  808. }
  809. uint32_t callback_to_submenu_options(void *context)
  810. {
  811. FlipStoreApp *app = (FlipStoreApp *)context;
  812. if (!app)
  813. {
  814. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  815. return FlipStoreViewSubmenuOptions;
  816. }
  817. firmware_free();
  818. vgm_firmware_free();
  819. flip_catalog_free();
  820. free_category_submenu(app);
  821. return FlipStoreViewSubmenuOptions;
  822. }
  823. void free_all_views(FlipStoreApp *app, bool should_free_variable_item_list)
  824. {
  825. if (!app)
  826. {
  827. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  828. return;
  829. }
  830. free_about_view(app);
  831. flip_catalog_free();
  832. if (should_free_variable_item_list)
  833. {
  834. free_variable_item_list(app);
  835. }
  836. free_category_submenu(app);
  837. free_text_input_view(app);
  838. free_dialog_firmware(app);
  839. free_app_info_view(app);
  840. firmware_free();
  841. vgm_firmware_free();
  842. }
  843. uint32_t callback_exit_app(void *context)
  844. {
  845. UNUSED(context);
  846. return VIEW_NONE; // Return VIEW_NONE to exit the app
  847. }
  848. static bool set_appropriate_list(FlipperHTTP *fhttp, FlipStoreApp *app)
  849. {
  850. if (!fhttp || !app)
  851. {
  852. FURI_LOG_E(TAG, "FlipperHTTP oor app is NULL");
  853. return false;
  854. }
  855. if (!easy_flipper_set_submenu(&app->submenu_app_list_category, FlipStoreViewAppListCategory, categories[flip_store_category_index], callback_to_app_list, &app->view_dispatcher))
  856. {
  857. FURI_LOG_E(TAG, "Failed to set submenu");
  858. return false;
  859. }
  860. if (flip_store_process_app_list(fhttp) && app->submenu_app_list_category && flip_catalog)
  861. {
  862. submenu_reset(app->submenu_app_list_category);
  863. submenu_set_header(app->submenu_app_list_category, categories[flip_store_category_index]);
  864. // add each app name to submenu
  865. for (size_t i = 0; i < flip_catalog->count; i++)
  866. {
  867. if (strlen(flip_catalog[i].app_name) > 0)
  868. {
  869. submenu_add_item(app->submenu_app_list_category, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
  870. }
  871. else
  872. {
  873. break;
  874. }
  875. }
  876. // add [LOAD NEXT] to submenu
  877. submenu_add_item(app->submenu_app_list_category, "[LOAD NEXT]", FlipStoreSubmenuIndexStartAppList + flip_catalog->count, callback_submenu_choices, app);
  878. return true;
  879. }
  880. FURI_LOG_E(TAG, "Failed to process the app list");
  881. return false;
  882. }
  883. // we'll have to loop 8 at a time due to memory constraints
  884. static void fetch_appropiate_app_list(FlipStoreApp *app, int iteration)
  885. {
  886. FlipperHTTP *fhttp = flipper_http_alloc();
  887. if (!fhttp)
  888. {
  889. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  890. return;
  891. }
  892. bool fetch_app_list()
  893. {
  894. // ensure /apps_data/flip_store/data exists
  895. Storage *storage = furi_record_open(RECORD_STORAGE);
  896. char dir[256];
  897. snprintf(dir, sizeof(dir), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data");
  898. storage_common_mkdir(storage, dir);
  899. furi_record_close(RECORD_STORAGE);
  900. fhttp->state = IDLE;
  901. flip_catalog_free();
  902. snprintf(
  903. fhttp->file_path,
  904. sizeof(fhttp->file_path),
  905. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s.json", categories[flip_store_category_index]);
  906. fhttp->save_received_data = true;
  907. fhttp->is_bytes_request = false;
  908. char url[256];
  909. // load 8 at a time
  910. 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]);
  911. return flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\":\"application/json\"}");
  912. }
  913. bool parse_app_list()
  914. {
  915. return set_appropriate_list(fhttp, app);
  916. }
  917. flipper_http_loading_task(fhttp, fetch_app_list, parse_app_list, FlipStoreViewAppListCategory, FlipStoreViewSubmenuOptions, &app->view_dispatcher);
  918. flipper_http_free(fhttp);
  919. }
  920. void callback_submenu_choices(void *context, uint32_t index)
  921. {
  922. FlipStoreApp *app = (FlipStoreApp *)context;
  923. if (!app)
  924. {
  925. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  926. return;
  927. }
  928. switch (index)
  929. {
  930. case FlipStoreSubmenuIndexAbout:
  931. free_all_views(app, true);
  932. if (!alloc_about_view(app))
  933. {
  934. FURI_LOG_E(TAG, "Failed to set about view");
  935. return;
  936. }
  937. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  938. break;
  939. case FlipStoreSubmenuIndexSettings:
  940. free_all_views(app, true);
  941. if (!alloc_variable_item_list(app))
  942. {
  943. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  944. return;
  945. }
  946. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  947. break;
  948. case FlipStoreSubmenuIndexOptions:
  949. free_all_views(app, true);
  950. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  951. break;
  952. case FlipStoreSubmenuIndexAppList:
  953. flip_store_category_index = 0;
  954. flip_store_app_does_exist = false;
  955. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  956. break;
  957. case FlipStoreSubmenuIndexFirmwares: // esp32 firmwares
  958. firmwares = firmware_alloc();
  959. if (firmwares == NULL)
  960. {
  961. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  962. return;
  963. }
  964. submenu_reset(app->submenu_firmwares);
  965. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  966. for (int i = 0; i < FIRMWARE_COUNT; i++)
  967. {
  968. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  969. }
  970. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  971. break;
  972. case FlipStoreSubmenuIndexVGMFirmwares: // vgm firmwares
  973. vgm_firmwares = vgm_firmware_alloc();
  974. if (vgm_firmwares == NULL)
  975. {
  976. FURI_LOG_E(TAG, "Failed to allocate memory for vgm firmwares");
  977. return;
  978. }
  979. submenu_reset(app->submenu_vgm_firmwares);
  980. submenu_set_header(app->submenu_vgm_firmwares, "VGM Firmwares");
  981. for (int i = 0; i < VGM_FIRMWARE_COUNT; i++)
  982. {
  983. submenu_add_item(app->submenu_vgm_firmwares, vgm_firmwares[i].name, FlipStoreSubmenuIndexStartVGMFirmwares + i, callback_submenu_choices, app);
  984. }
  985. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewVGMFirmwares);
  986. break;
  987. case FlipStoreSubmenuIndexGitHub: // github
  988. free_all_views(app, true);
  989. if (!alloc_text_input_view(app, "Author"))
  990. {
  991. FURI_LOG_E(TAG, "Failed to allocate text input view");
  992. return;
  993. }
  994. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  995. break;
  996. case FlipStoreSubmenuIndexAppListBluetooth:
  997. free_all_views(app, true);
  998. flip_store_category_index = 0;
  999. flip_store_app_does_exist = false;
  1000. fetch_appropiate_app_list(app, 0);
  1001. break;
  1002. case FlipStoreSubmenuIndexAppListGames:
  1003. free_all_views(app, true);
  1004. flip_store_category_index = 1;
  1005. flip_store_app_does_exist = false;
  1006. fetch_appropiate_app_list(app, 0);
  1007. break;
  1008. case FlipStoreSubmenuIndexAppListGPIO:
  1009. free_all_views(app, true);
  1010. flip_store_category_index = 2;
  1011. flip_store_app_does_exist = false;
  1012. fetch_appropiate_app_list(app, 0);
  1013. break;
  1014. case FlipStoreSubmenuIndexAppListInfrared:
  1015. free_all_views(app, true);
  1016. flip_store_category_index = 3;
  1017. flip_store_app_does_exist = false;
  1018. fetch_appropiate_app_list(app, 0);
  1019. break;
  1020. case FlipStoreSubmenuIndexAppListiButton:
  1021. free_all_views(app, true);
  1022. flip_store_category_index = 4;
  1023. flip_store_app_does_exist = false;
  1024. fetch_appropiate_app_list(app, 0);
  1025. break;
  1026. case FlipStoreSubmenuIndexAppListMedia:
  1027. free_all_views(app, true);
  1028. flip_store_category_index = 5;
  1029. flip_store_app_does_exist = false;
  1030. fetch_appropiate_app_list(app, 0);
  1031. break;
  1032. case FlipStoreSubmenuIndexAppListNFC:
  1033. free_all_views(app, true);
  1034. flip_store_category_index = 6;
  1035. flip_store_app_does_exist = false;
  1036. fetch_appropiate_app_list(app, 0);
  1037. break;
  1038. case FlipStoreSubmenuIndexAppListRFID:
  1039. free_all_views(app, true);
  1040. flip_store_category_index = 7;
  1041. flip_store_app_does_exist = false;
  1042. fetch_appropiate_app_list(app, 0);
  1043. break;
  1044. case FlipStoreSubmenuIndexAppListSubGHz:
  1045. free_all_views(app, true);
  1046. flip_store_category_index = 8;
  1047. flip_store_app_does_exist = false;
  1048. fetch_appropiate_app_list(app, 0);
  1049. break;
  1050. case FlipStoreSubmenuIndexAppListTools:
  1051. free_all_views(app, true);
  1052. flip_store_category_index = 9;
  1053. flip_store_app_does_exist = false;
  1054. fetch_appropiate_app_list(app, 0);
  1055. break;
  1056. case FlipStoreSubmenuIndexAppListUSB:
  1057. free_all_views(app, true);
  1058. flip_store_category_index = 10;
  1059. flip_store_app_does_exist = false;
  1060. fetch_appropiate_app_list(app, 0);
  1061. break;
  1062. default:
  1063. // Check if the index is within the ESP32 firmwares list range
  1064. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + FIRMWARE_COUNT)
  1065. {
  1066. // Get the firmware index
  1067. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  1068. // Check if the firmware index is valid
  1069. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  1070. {
  1071. // Get the firmware name
  1072. selected_firmware_index = firmware_index;
  1073. is_esp32_firmware = true;
  1074. // Switch to the firmware download view
  1075. free_dialog_firmware(app);
  1076. if (!alloc_dialog_firmware(app))
  1077. {
  1078. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  1079. return;
  1080. }
  1081. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  1082. }
  1083. else
  1084. {
  1085. FURI_LOG_E(TAG, "Invalid firmware index");
  1086. easy_flipper_dialog("Error", "Issue parsing firmware.");
  1087. }
  1088. }
  1089. // Check if the index is within the VGM firmwares list range
  1090. else if (index >= FlipStoreSubmenuIndexStartVGMFirmwares && index < FlipStoreSubmenuIndexStartVGMFirmwares + VGM_FIRMWARE_COUNT)
  1091. {
  1092. // Get the firmware index
  1093. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartVGMFirmwares;
  1094. // Check if the firmware index is valid
  1095. if ((int)firmware_index >= 0 && firmware_index < VGM_FIRMWARE_COUNT)
  1096. {
  1097. // Get the firmware name
  1098. selected_firmware_index = firmware_index;
  1099. is_esp32_firmware = false;
  1100. // Switch to the firmware download view
  1101. free_dialog_firmware(app);
  1102. if (!alloc_dialog_firmware(app))
  1103. {
  1104. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  1105. return;
  1106. }
  1107. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  1108. }
  1109. else
  1110. {
  1111. FURI_LOG_E(TAG, "Invalid firmware index");
  1112. easy_flipper_dialog("Error", "Issue parsing firmware.");
  1113. }
  1114. }
  1115. // Check if the index is within the app list range
  1116. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + flip_catalog->count)
  1117. {
  1118. // Get the app index
  1119. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  1120. // Check if the app index is valid
  1121. if ((int)app_index >= 0 && app_index < flip_catalog->count)
  1122. {
  1123. // Get the app name
  1124. char *app_name = flip_catalog[app_index].app_name;
  1125. // Check if the app name is valid
  1126. if (app_name != NULL && strlen(app_name) > 0)
  1127. {
  1128. app_selected_index = app_index;
  1129. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  1130. free_app_info_view(app);
  1131. if (!alloc_app_info_view(app))
  1132. {
  1133. FURI_LOG_E(TAG, "Failed to allocate app info view");
  1134. return;
  1135. }
  1136. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  1137. }
  1138. else
  1139. {
  1140. FURI_LOG_E(TAG, "Invalid app name");
  1141. }
  1142. }
  1143. else
  1144. {
  1145. FURI_LOG_E(TAG, "Invalid app index");
  1146. }
  1147. }
  1148. // Check if the index is for loading the next set of apps
  1149. else if (index == FlipStoreSubmenuIndexStartAppList + flip_catalog->count)
  1150. {
  1151. catalog_iteration = flip_catalog->iteration + 8;
  1152. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1153. free_category_submenu(app);
  1154. flip_catalog_free();
  1155. fetch_appropiate_app_list(app, catalog_iteration);
  1156. }
  1157. else
  1158. {
  1159. FURI_LOG_E(TAG, "Unknown submenu index");
  1160. }
  1161. break;
  1162. }
  1163. }
  1164. static void settings_item_selected(void *context, uint32_t index)
  1165. {
  1166. FlipStoreApp *app = (FlipStoreApp *)context;
  1167. if (!app)
  1168. {
  1169. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  1170. return;
  1171. }
  1172. char ssid[64];
  1173. char pass[64];
  1174. switch (index)
  1175. {
  1176. case 0: // Input SSID
  1177. // Text Input
  1178. free_all_views(app, false);
  1179. if (!alloc_text_input_view(app, "SSID"))
  1180. {
  1181. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1182. return;
  1183. }
  1184. // load SSID
  1185. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  1186. {
  1187. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  1188. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  1189. }
  1190. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  1191. break;
  1192. case 1: // Input Password
  1193. free_all_views(app, false);
  1194. if (!alloc_text_input_view(app, "Password"))
  1195. {
  1196. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1197. return;
  1198. }
  1199. // load password
  1200. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  1201. {
  1202. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  1203. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  1204. }
  1205. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  1206. break;
  1207. default:
  1208. FURI_LOG_E(TAG, "Unknown configuration item index");
  1209. break;
  1210. }
  1211. }
  1212. static void flip_store_widget_set_text(char *message, Widget **widget)
  1213. {
  1214. if (widget == NULL)
  1215. {
  1216. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  1217. DEV_CRASH();
  1218. return;
  1219. }
  1220. if (message == NULL)
  1221. {
  1222. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  1223. DEV_CRASH();
  1224. return;
  1225. }
  1226. widget_reset(*widget);
  1227. uint32_t message_length = strlen(message); // Length of the message
  1228. uint32_t i = 0; // Index tracker
  1229. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1230. char *formatted_message; // Buffer to hold the final formatted message
  1231. // Allocate buffer with double the message length plus one for safety
  1232. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1233. {
  1234. return;
  1235. }
  1236. while (i < message_length)
  1237. {
  1238. uint32_t max_line_length = 31; // Maximum characters per line
  1239. uint32_t remaining_length = message_length - i; // Remaining characters
  1240. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1241. // Check for newline character within the current segment
  1242. uint32_t newline_pos = i;
  1243. bool found_newline = false;
  1244. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1245. {
  1246. if (message[newline_pos] == '\n')
  1247. {
  1248. found_newline = true;
  1249. break;
  1250. }
  1251. }
  1252. if (found_newline)
  1253. {
  1254. // If newline found, set line_length up to the newline
  1255. line_length = newline_pos - i;
  1256. }
  1257. // Temporary buffer to hold the current line
  1258. char line[32];
  1259. strncpy(line, message + i, line_length);
  1260. line[line_length] = '\0';
  1261. // If newline was found, skip it for the next iteration
  1262. if (found_newline)
  1263. {
  1264. i += line_length + 1; // +1 to skip the '\n' character
  1265. }
  1266. else
  1267. {
  1268. // Check if the line ends in the middle of a word and adjust accordingly
  1269. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1270. {
  1271. // Find the last space within the current line to avoid breaking a word
  1272. char *last_space = strrchr(line, ' ');
  1273. if (last_space != NULL)
  1274. {
  1275. // Adjust the line_length to avoid cutting the word
  1276. line_length = last_space - line;
  1277. line[line_length] = '\0'; // Null-terminate at the space
  1278. }
  1279. }
  1280. // Move the index forward by the determined line_length
  1281. i += line_length;
  1282. // Skip any spaces at the beginning of the next line
  1283. while (i < message_length && message[i] == ' ')
  1284. {
  1285. i++;
  1286. }
  1287. }
  1288. // Manually copy the fixed line into the formatted_message buffer
  1289. for (uint32_t j = 0; j < line_length; j++)
  1290. {
  1291. formatted_message[formatted_index++] = line[j];
  1292. }
  1293. // Add a newline character for line spacing
  1294. formatted_message[formatted_index++] = '\n';
  1295. }
  1296. // Null-terminate the formatted_message
  1297. formatted_message[formatted_index] = '\0';
  1298. // Add the formatted message to the widget
  1299. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1300. }
  1301. static void flip_store_request_error(Canvas *canvas, FlipperHTTP *fhttp)
  1302. {
  1303. if (!canvas)
  1304. {
  1305. FURI_LOG_E(TAG, "Canvas is NULL");
  1306. return;
  1307. }
  1308. if (!fhttp)
  1309. {
  1310. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1311. return;
  1312. }
  1313. if (fhttp->last_response != NULL)
  1314. {
  1315. if (strstr(fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  1316. {
  1317. canvas_clear(canvas);
  1318. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1319. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1320. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1321. }
  1322. else if (strstr(fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  1323. {
  1324. canvas_clear(canvas);
  1325. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1326. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1327. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1328. }
  1329. else
  1330. {
  1331. FURI_LOG_E(TAG, "Received an error: %s", fhttp->last_response);
  1332. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  1333. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1334. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1335. }
  1336. }
  1337. else
  1338. {
  1339. canvas_clear(canvas);
  1340. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  1341. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1342. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1343. }
  1344. }
  1345. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1346. {
  1347. if (!canvas || !model)
  1348. {
  1349. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1350. return;
  1351. }
  1352. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1353. SerialState http_state = data_loader_model->fhttp->state;
  1354. DataState data_state = data_loader_model->data_state;
  1355. char *title = data_loader_model->title;
  1356. canvas_set_font(canvas, FontSecondary);
  1357. if (http_state == INACTIVE)
  1358. {
  1359. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1360. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1361. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1362. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1363. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1364. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1365. return;
  1366. }
  1367. if (data_state == DataStateError || data_state == DataStateParseError)
  1368. {
  1369. flip_store_request_error(canvas, data_loader_model->fhttp);
  1370. return;
  1371. }
  1372. canvas_draw_str(canvas, 0, 7, title);
  1373. canvas_draw_str(canvas, 0, 17, "Loading...");
  1374. if (data_state == DataStateInitial)
  1375. {
  1376. return;
  1377. }
  1378. if (http_state == SENDING)
  1379. {
  1380. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1381. return;
  1382. }
  1383. if (http_state == RECEIVING || data_state == DataStateRequested)
  1384. {
  1385. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1386. return;
  1387. }
  1388. if (http_state == IDLE && data_state == DataStateReceived)
  1389. {
  1390. canvas_draw_str(canvas, 0, 27, "Processing...");
  1391. return;
  1392. }
  1393. if (http_state == IDLE && data_state == DataStateParsed)
  1394. {
  1395. canvas_draw_str(canvas, 0, 27, "Processed...");
  1396. return;
  1397. }
  1398. }
  1399. static void flip_store_loader_process_callback(void *context)
  1400. {
  1401. if (context == NULL)
  1402. {
  1403. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1404. DEV_CRASH();
  1405. return;
  1406. }
  1407. FlipStoreApp *app = (FlipStoreApp *)context;
  1408. View *view = app->view_loader;
  1409. DataState current_data_state;
  1410. DataLoaderModel *loader_model = NULL;
  1411. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1412. if (!loader_model || !loader_model->fhttp)
  1413. {
  1414. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1415. DEV_CRASH();
  1416. return;
  1417. }
  1418. if (current_data_state == DataStateInitial)
  1419. {
  1420. with_view_model(
  1421. view,
  1422. DataLoaderModel * model,
  1423. {
  1424. model->data_state = DataStateRequested;
  1425. DataLoaderFetch fetch = model->fetcher;
  1426. if (fetch == NULL)
  1427. {
  1428. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1429. model->data_state = DataStateError;
  1430. return;
  1431. }
  1432. // Clear any previous responses
  1433. strncpy(model->fhttp->last_response, "", 1);
  1434. bool request_status = fetch(model);
  1435. if (!request_status)
  1436. {
  1437. model->data_state = DataStateError;
  1438. }
  1439. },
  1440. true);
  1441. }
  1442. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1443. {
  1444. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1445. {
  1446. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1447. {
  1448. FURI_LOG_DEV(TAG, "PONG received.");
  1449. }
  1450. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1451. {
  1452. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1453. }
  1454. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1455. {
  1456. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1457. }
  1458. else if (strlen(loader_model->fhttp->last_response) == 0)
  1459. {
  1460. // Still waiting on response
  1461. }
  1462. else
  1463. {
  1464. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1465. }
  1466. }
  1467. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1468. {
  1469. // continue waiting
  1470. }
  1471. else if (loader_model->fhttp->state == INACTIVE)
  1472. {
  1473. // inactive. try again
  1474. }
  1475. else if (loader_model->fhttp->state == ISSUE)
  1476. {
  1477. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1478. }
  1479. else
  1480. {
  1481. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1482. DEV_CRASH();
  1483. }
  1484. }
  1485. else if (current_data_state == DataStateReceived)
  1486. {
  1487. with_view_model(
  1488. view,
  1489. DataLoaderModel * model,
  1490. {
  1491. char *data_text;
  1492. if (model->parser == NULL)
  1493. {
  1494. data_text = NULL;
  1495. FURI_LOG_DEV(TAG, "Parser is NULL");
  1496. DEV_CRASH();
  1497. }
  1498. else
  1499. {
  1500. data_text = model->parser(model);
  1501. }
  1502. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1503. model->data_text = data_text;
  1504. if (data_text == NULL)
  1505. {
  1506. model->data_state = DataStateParseError;
  1507. }
  1508. else
  1509. {
  1510. model->data_state = DataStateParsed;
  1511. }
  1512. },
  1513. true);
  1514. }
  1515. else if (current_data_state == DataStateParsed)
  1516. {
  1517. with_view_model(
  1518. view,
  1519. DataLoaderModel * model,
  1520. {
  1521. if (++model->request_index < model->request_count)
  1522. {
  1523. model->data_state = DataStateInitial;
  1524. }
  1525. else
  1526. {
  1527. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1528. if (model->data_text != NULL)
  1529. {
  1530. free(model->data_text);
  1531. model->data_text = NULL;
  1532. }
  1533. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1534. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1535. }
  1536. },
  1537. true);
  1538. }
  1539. }
  1540. static void flip_store_loader_timer_callback(void *context)
  1541. {
  1542. if (context == NULL)
  1543. {
  1544. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1545. DEV_CRASH();
  1546. return;
  1547. }
  1548. FlipStoreApp *app = (FlipStoreApp *)context;
  1549. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1550. }
  1551. static void flip_store_loader_on_enter(void *context)
  1552. {
  1553. if (context == NULL)
  1554. {
  1555. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1556. DEV_CRASH();
  1557. return;
  1558. }
  1559. FlipStoreApp *app = (FlipStoreApp *)context;
  1560. View *view = app->view_loader;
  1561. with_view_model(
  1562. view,
  1563. DataLoaderModel * model,
  1564. {
  1565. view_set_previous_callback(view, model->back_callback);
  1566. if (model->timer == NULL)
  1567. {
  1568. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1569. }
  1570. furi_timer_start(model->timer, 250);
  1571. },
  1572. true);
  1573. }
  1574. static void flip_store_loader_on_exit(void *context)
  1575. {
  1576. if (context == NULL)
  1577. {
  1578. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1579. DEV_CRASH();
  1580. return;
  1581. }
  1582. FlipStoreApp *app = (FlipStoreApp *)context;
  1583. View *view = app->view_loader;
  1584. with_view_model(
  1585. view,
  1586. DataLoaderModel * model,
  1587. {
  1588. if (model->timer)
  1589. {
  1590. furi_timer_stop(model->timer);
  1591. }
  1592. },
  1593. false);
  1594. }
  1595. void flip_store_loader_init(View *view)
  1596. {
  1597. if (view == NULL)
  1598. {
  1599. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1600. DEV_CRASH();
  1601. return;
  1602. }
  1603. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1604. view_set_enter_callback(view, flip_store_loader_on_enter);
  1605. view_set_exit_callback(view, flip_store_loader_on_exit);
  1606. }
  1607. void flip_store_loader_free_model(View *view)
  1608. {
  1609. if (view == NULL)
  1610. {
  1611. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1612. DEV_CRASH();
  1613. return;
  1614. }
  1615. with_view_model(
  1616. view,
  1617. DataLoaderModel * model,
  1618. {
  1619. if (model->timer)
  1620. {
  1621. furi_timer_free(model->timer);
  1622. model->timer = NULL;
  1623. }
  1624. if (model->parser_context)
  1625. {
  1626. // do not free the context here, it is the app context
  1627. // free(model->parser_context);
  1628. // model->parser_context = NULL;
  1629. }
  1630. if (model->fhttp)
  1631. {
  1632. flipper_http_free(model->fhttp);
  1633. model->fhttp = NULL;
  1634. }
  1635. },
  1636. false);
  1637. }
  1638. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1639. {
  1640. if (context == NULL)
  1641. {
  1642. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1643. DEV_CRASH();
  1644. return false;
  1645. }
  1646. switch (index)
  1647. {
  1648. case FlipStoreCustomEventProcess:
  1649. flip_store_loader_process_callback(context);
  1650. return true;
  1651. default:
  1652. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1653. return false;
  1654. }
  1655. }
  1656. 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)
  1657. {
  1658. if (app == NULL)
  1659. {
  1660. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1661. DEV_CRASH();
  1662. return;
  1663. }
  1664. View *view = app->view_loader;
  1665. if (view == NULL)
  1666. {
  1667. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1668. DEV_CRASH();
  1669. return;
  1670. }
  1671. with_view_model(
  1672. view,
  1673. DataLoaderModel * model,
  1674. {
  1675. model->title = title;
  1676. model->fetcher = fetcher;
  1677. model->parser = parser;
  1678. model->request_index = 0;
  1679. model->request_count = request_count;
  1680. model->back_callback = back;
  1681. model->data_state = DataStateInitial;
  1682. model->data_text = NULL;
  1683. //
  1684. model->parser_context = app;
  1685. if (!model->fhttp)
  1686. {
  1687. model->fhttp = flipper_http_alloc();
  1688. }
  1689. },
  1690. true);
  1691. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1692. }