flip_store_callback.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  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(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 FlipDownloader!\n------\nDownload apps and assets via WiFi\nand run 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. uart_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. fhttp->state = IDLE;
  924. flip_catalog_free();
  925. snprintf(
  926. fhttp->file_path,
  927. sizeof(fhttp->file_path),
  928. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/data/%s.json", categories[flip_store_category_index]);
  929. storage_simply_remove_recursive(storage, fhttp->file_path);
  930. furi_record_close(RECORD_STORAGE);
  931. fhttp->save_received_data = true;
  932. fhttp->is_bytes_request = false;
  933. char url[256];
  934. // load 8 at a time
  935. snprintf(url, sizeof(url), "https://catalog.flipperzero.one/api/v0/0/application?limit=%d&is_latest_release_version=true&offset=%d&sort_by=updated_at&sort_order=-1&category_id=%s", MAX_RECEIVED_APPS, iteration, category_ids[flip_store_category_index]);
  936. // return flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\":\"application/json\"}");
  937. return flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  938. }
  939. bool parse_app_list()
  940. {
  941. return set_appropriate_list(fhttp, app);
  942. }
  943. flipper_http_loading_task(fhttp, fetch_app_list, parse_app_list, FlipStoreViewAppListCategory, FlipStoreViewSubmenuOptions, &app->view_dispatcher);
  944. flipper_http_free(fhttp);
  945. }
  946. void callback_submenu_choices(void *context, uint32_t index)
  947. {
  948. FlipStoreApp *app = (FlipStoreApp *)context;
  949. if (!app)
  950. {
  951. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  952. return;
  953. }
  954. switch (index)
  955. {
  956. case FlipStoreSubmenuIndexAbout:
  957. free_all_views(app, true);
  958. if (!alloc_about_view(app))
  959. {
  960. FURI_LOG_E(TAG, "Failed to set about view");
  961. return;
  962. }
  963. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  964. break;
  965. case FlipStoreSubmenuIndexSettings:
  966. free_all_views(app, true);
  967. if (!alloc_variable_item_list(app))
  968. {
  969. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  970. return;
  971. }
  972. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  973. break;
  974. case FlipStoreSubmenuIndexOptions:
  975. free_all_views(app, true);
  976. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  977. break;
  978. case FlipStoreSubmenuIndexAppList:
  979. flip_store_category_index = 0;
  980. flip_store_app_does_exist = false;
  981. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  982. break;
  983. case FlipStoreSubmenuIndexFirmwares: // esp32 firmwares
  984. firmwares = firmware_alloc();
  985. if (firmwares == NULL)
  986. {
  987. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  988. return;
  989. }
  990. submenu_reset(app->submenu_firmwares);
  991. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  992. for (int i = 0; i < FIRMWARE_COUNT; i++)
  993. {
  994. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  995. }
  996. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  997. break;
  998. case FlipStoreSubmenuIndexVGMFirmwares: // vgm firmwares
  999. vgm_firmwares = vgm_firmware_alloc();
  1000. if (vgm_firmwares == NULL)
  1001. {
  1002. FURI_LOG_E(TAG, "Failed to allocate memory for vgm firmwares");
  1003. return;
  1004. }
  1005. submenu_reset(app->submenu_vgm_firmwares);
  1006. submenu_set_header(app->submenu_vgm_firmwares, "VGM Firmwares");
  1007. for (int i = 0; i < VGM_FIRMWARE_COUNT; i++)
  1008. {
  1009. submenu_add_item(app->submenu_vgm_firmwares, vgm_firmwares[i].name, FlipStoreSubmenuIndexStartVGMFirmwares + i, callback_submenu_choices, app);
  1010. }
  1011. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewVGMFirmwares);
  1012. break;
  1013. case FlipStoreSubmenuIndexGitHub: // github
  1014. free_all_views(app, true);
  1015. if (!alloc_text_input_view(app, "Author"))
  1016. {
  1017. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1018. return;
  1019. }
  1020. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  1021. break;
  1022. case FlipStoreSubmenuIndexAppListBluetooth:
  1023. free_all_views(app, true);
  1024. flip_store_category_index = 0;
  1025. flip_store_app_does_exist = false;
  1026. fetch_appropiate_app_list(app, 0);
  1027. break;
  1028. case FlipStoreSubmenuIndexAppListGames:
  1029. free_all_views(app, true);
  1030. flip_store_category_index = 1;
  1031. flip_store_app_does_exist = false;
  1032. fetch_appropiate_app_list(app, 0);
  1033. break;
  1034. case FlipStoreSubmenuIndexAppListGPIO:
  1035. free_all_views(app, true);
  1036. flip_store_category_index = 2;
  1037. flip_store_app_does_exist = false;
  1038. fetch_appropiate_app_list(app, 0);
  1039. break;
  1040. case FlipStoreSubmenuIndexAppListInfrared:
  1041. free_all_views(app, true);
  1042. flip_store_category_index = 3;
  1043. flip_store_app_does_exist = false;
  1044. fetch_appropiate_app_list(app, 0);
  1045. break;
  1046. case FlipStoreSubmenuIndexAppListiButton:
  1047. free_all_views(app, true);
  1048. flip_store_category_index = 4;
  1049. flip_store_app_does_exist = false;
  1050. fetch_appropiate_app_list(app, 0);
  1051. break;
  1052. case FlipStoreSubmenuIndexAppListMedia:
  1053. free_all_views(app, true);
  1054. flip_store_category_index = 5;
  1055. flip_store_app_does_exist = false;
  1056. fetch_appropiate_app_list(app, 0);
  1057. break;
  1058. case FlipStoreSubmenuIndexAppListNFC:
  1059. free_all_views(app, true);
  1060. flip_store_category_index = 6;
  1061. flip_store_app_does_exist = false;
  1062. fetch_appropiate_app_list(app, 0);
  1063. break;
  1064. case FlipStoreSubmenuIndexAppListRFID:
  1065. free_all_views(app, true);
  1066. flip_store_category_index = 7;
  1067. flip_store_app_does_exist = false;
  1068. fetch_appropiate_app_list(app, 0);
  1069. break;
  1070. case FlipStoreSubmenuIndexAppListSubGHz:
  1071. free_all_views(app, true);
  1072. flip_store_category_index = 8;
  1073. flip_store_app_does_exist = false;
  1074. fetch_appropiate_app_list(app, 0);
  1075. break;
  1076. case FlipStoreSubmenuIndexAppListTools:
  1077. free_all_views(app, true);
  1078. flip_store_category_index = 9;
  1079. flip_store_app_does_exist = false;
  1080. fetch_appropiate_app_list(app, 0);
  1081. break;
  1082. case FlipStoreSubmenuIndexAppListUSB:
  1083. free_all_views(app, true);
  1084. flip_store_category_index = 10;
  1085. flip_store_app_does_exist = false;
  1086. fetch_appropiate_app_list(app, 0);
  1087. break;
  1088. default:
  1089. // Check if the index is within the ESP32 firmwares list range
  1090. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + FIRMWARE_COUNT)
  1091. {
  1092. // Get the firmware index
  1093. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  1094. // Check if the firmware index is valid
  1095. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  1096. {
  1097. // Get the firmware name
  1098. selected_firmware_index = firmware_index;
  1099. is_esp32_firmware = true;
  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 VGM firmwares list range
  1116. else if (index >= FlipStoreSubmenuIndexStartVGMFirmwares && index < FlipStoreSubmenuIndexStartVGMFirmwares + VGM_FIRMWARE_COUNT)
  1117. {
  1118. // Get the firmware index
  1119. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartVGMFirmwares;
  1120. // Check if the firmware index is valid
  1121. if ((int)firmware_index >= 0 && firmware_index < VGM_FIRMWARE_COUNT)
  1122. {
  1123. // Get the firmware name
  1124. selected_firmware_index = firmware_index;
  1125. is_esp32_firmware = false;
  1126. // Switch to the firmware download view
  1127. free_dialog_firmware(app);
  1128. if (!alloc_dialog_firmware(app))
  1129. {
  1130. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  1131. return;
  1132. }
  1133. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  1134. }
  1135. else
  1136. {
  1137. FURI_LOG_E(TAG, "Invalid firmware index");
  1138. easy_flipper_dialog("Error", "Issue parsing firmware.");
  1139. }
  1140. }
  1141. // Check if the index is within the app list range
  1142. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + flip_catalog->count)
  1143. {
  1144. // Get the app index
  1145. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  1146. // Check if the app index is valid
  1147. if ((int)app_index >= 0 && app_index < flip_catalog->count)
  1148. {
  1149. // Get the app name
  1150. char *app_name = flip_catalog[app_index].app_name;
  1151. // Check if the app name is valid
  1152. if (app_name != NULL && strlen(app_name) > 0)
  1153. {
  1154. app_selected_index = app_index;
  1155. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  1156. free_app_info_view(app);
  1157. if (!alloc_app_info_view(app))
  1158. {
  1159. FURI_LOG_E(TAG, "Failed to allocate app info view");
  1160. return;
  1161. }
  1162. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  1163. }
  1164. else
  1165. {
  1166. FURI_LOG_E(TAG, "Invalid app name");
  1167. }
  1168. }
  1169. else
  1170. {
  1171. FURI_LOG_E(TAG, "Invalid app index");
  1172. }
  1173. }
  1174. // Check if the index is for loading the next set of apps
  1175. else if (index == FlipStoreSubmenuIndexStartAppList + flip_catalog->count)
  1176. {
  1177. catalog_iteration = flip_catalog->iteration + 8;
  1178. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1179. free_category_submenu(app);
  1180. flip_catalog_free();
  1181. fetch_appropiate_app_list(app, catalog_iteration);
  1182. }
  1183. else
  1184. {
  1185. FURI_LOG_E(TAG, "Unknown submenu index");
  1186. }
  1187. break;
  1188. }
  1189. }
  1190. static void settings_item_selected(void *context, uint32_t index)
  1191. {
  1192. FlipStoreApp *app = (FlipStoreApp *)context;
  1193. if (!app)
  1194. {
  1195. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  1196. return;
  1197. }
  1198. char ssid[64];
  1199. char pass[64];
  1200. switch (index)
  1201. {
  1202. case 0: // Input SSID
  1203. // Text Input
  1204. free_all_views(app, false);
  1205. if (!alloc_text_input_view(app, "SSID"))
  1206. {
  1207. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1208. return;
  1209. }
  1210. // load SSID
  1211. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  1212. {
  1213. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  1214. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  1215. }
  1216. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  1217. break;
  1218. case 1: // Input Password
  1219. free_all_views(app, false);
  1220. if (!alloc_text_input_view(app, "Password"))
  1221. {
  1222. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1223. return;
  1224. }
  1225. // load password
  1226. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  1227. {
  1228. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  1229. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  1230. }
  1231. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  1232. break;
  1233. default:
  1234. FURI_LOG_E(TAG, "Unknown configuration item index");
  1235. break;
  1236. }
  1237. }
  1238. static void flip_store_widget_set_text(char *message, Widget **widget)
  1239. {
  1240. if (widget == NULL)
  1241. {
  1242. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  1243. DEV_CRASH();
  1244. return;
  1245. }
  1246. if (message == NULL)
  1247. {
  1248. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  1249. DEV_CRASH();
  1250. return;
  1251. }
  1252. widget_reset(*widget);
  1253. uint32_t message_length = strlen(message); // Length of the message
  1254. uint32_t i = 0; // Index tracker
  1255. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1256. char *formatted_message; // Buffer to hold the final formatted message
  1257. // Allocate buffer with double the message length plus one for safety
  1258. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1259. {
  1260. return;
  1261. }
  1262. while (i < message_length)
  1263. {
  1264. uint32_t max_line_length = 31; // Maximum characters per line
  1265. uint32_t remaining_length = message_length - i; // Remaining characters
  1266. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1267. // Check for newline character within the current segment
  1268. uint32_t newline_pos = i;
  1269. bool found_newline = false;
  1270. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1271. {
  1272. if (message[newline_pos] == '\n')
  1273. {
  1274. found_newline = true;
  1275. break;
  1276. }
  1277. }
  1278. if (found_newline)
  1279. {
  1280. // If newline found, set line_length up to the newline
  1281. line_length = newline_pos - i;
  1282. }
  1283. // Temporary buffer to hold the current line
  1284. char line[32];
  1285. strncpy(line, message + i, line_length);
  1286. line[line_length] = '\0';
  1287. // If newline was found, skip it for the next iteration
  1288. if (found_newline)
  1289. {
  1290. i += line_length + 1; // +1 to skip the '\n' character
  1291. }
  1292. else
  1293. {
  1294. // Check if the line ends in the middle of a word and adjust accordingly
  1295. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1296. {
  1297. // Find the last space within the current line to avoid breaking a word
  1298. char *last_space = strrchr(line, ' ');
  1299. if (last_space != NULL)
  1300. {
  1301. // Adjust the line_length to avoid cutting the word
  1302. line_length = last_space - line;
  1303. line[line_length] = '\0'; // Null-terminate at the space
  1304. }
  1305. }
  1306. // Move the index forward by the determined line_length
  1307. i += line_length;
  1308. // Skip any spaces at the beginning of the next line
  1309. while (i < message_length && message[i] == ' ')
  1310. {
  1311. i++;
  1312. }
  1313. }
  1314. // Manually copy the fixed line into the formatted_message buffer
  1315. for (uint32_t j = 0; j < line_length; j++)
  1316. {
  1317. formatted_message[formatted_index++] = line[j];
  1318. }
  1319. // Add a newline character for line spacing
  1320. formatted_message[formatted_index++] = '\n';
  1321. }
  1322. // Null-terminate the formatted_message
  1323. formatted_message[formatted_index] = '\0';
  1324. // Add the formatted message to the widget
  1325. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1326. }
  1327. static void flip_store_request_error(Canvas *canvas, FlipperHTTP *fhttp)
  1328. {
  1329. if (!canvas)
  1330. {
  1331. FURI_LOG_E(TAG, "Canvas is NULL");
  1332. return;
  1333. }
  1334. if (!fhttp)
  1335. {
  1336. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1337. return;
  1338. }
  1339. if (fhttp->last_response != NULL)
  1340. {
  1341. if (strstr(fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  1342. {
  1343. canvas_clear(canvas);
  1344. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1345. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1346. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1347. }
  1348. else if (strstr(fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  1349. {
  1350. canvas_clear(canvas);
  1351. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1352. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1353. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1354. }
  1355. else
  1356. {
  1357. FURI_LOG_E(TAG, "Received an error: %s", fhttp->last_response);
  1358. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  1359. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1360. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1361. }
  1362. }
  1363. else
  1364. {
  1365. canvas_clear(canvas);
  1366. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  1367. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1368. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1369. }
  1370. }
  1371. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1372. {
  1373. if (!canvas || !model)
  1374. {
  1375. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1376. return;
  1377. }
  1378. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1379. HTTPState http_state = data_loader_model->fhttp->state;
  1380. DataState data_state = data_loader_model->data_state;
  1381. char *title = data_loader_model->title;
  1382. canvas_set_font(canvas, FontSecondary);
  1383. if (http_state == INACTIVE)
  1384. {
  1385. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1386. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1387. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1388. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1389. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1390. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1391. return;
  1392. }
  1393. if (data_state == DataStateError || data_state == DataStateParseError)
  1394. {
  1395. flip_store_request_error(canvas, data_loader_model->fhttp);
  1396. return;
  1397. }
  1398. canvas_draw_str(canvas, 0, 7, title);
  1399. canvas_draw_str(canvas, 0, 17, "Loading...");
  1400. if (data_state == DataStateInitial)
  1401. {
  1402. return;
  1403. }
  1404. if (http_state == SENDING)
  1405. {
  1406. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1407. return;
  1408. }
  1409. if (http_state == RECEIVING || data_state == DataStateRequested)
  1410. {
  1411. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1412. canvas_draw_str(canvas, 0, 37, "This may take two minutes...");
  1413. canvas_draw_str(canvas, 0, 47, "Please wait...");
  1414. return;
  1415. }
  1416. if (http_state == IDLE && data_state == DataStateReceived)
  1417. {
  1418. canvas_draw_str(canvas, 0, 27, "Processing...");
  1419. return;
  1420. }
  1421. if (http_state == IDLE && data_state == DataStateParsed)
  1422. {
  1423. canvas_draw_str(canvas, 0, 27, "Processed...");
  1424. return;
  1425. }
  1426. }
  1427. static void flip_store_loader_process_callback(void *context)
  1428. {
  1429. if (context == NULL)
  1430. {
  1431. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1432. DEV_CRASH();
  1433. return;
  1434. }
  1435. FlipStoreApp *app = (FlipStoreApp *)context;
  1436. View *view = app->view_loader;
  1437. DataState current_data_state;
  1438. DataLoaderModel *loader_model = NULL;
  1439. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1440. if (!loader_model || !loader_model->fhttp)
  1441. {
  1442. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1443. DEV_CRASH();
  1444. return;
  1445. }
  1446. if (current_data_state == DataStateInitial)
  1447. {
  1448. with_view_model(
  1449. view,
  1450. DataLoaderModel * model,
  1451. {
  1452. model->data_state = DataStateRequested;
  1453. DataLoaderFetch fetch = model->fetcher;
  1454. if (fetch == NULL)
  1455. {
  1456. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1457. model->data_state = DataStateError;
  1458. return;
  1459. }
  1460. // Clear any previous responses
  1461. strncpy(model->fhttp->last_response, "", 1);
  1462. bool request_status = fetch(model);
  1463. if (!request_status)
  1464. {
  1465. model->data_state = DataStateError;
  1466. }
  1467. },
  1468. true);
  1469. }
  1470. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1471. {
  1472. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1473. {
  1474. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1475. {
  1476. FURI_LOG_DEV(TAG, "PONG received.");
  1477. }
  1478. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1479. {
  1480. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1481. }
  1482. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1483. {
  1484. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1485. }
  1486. else if (strlen(loader_model->fhttp->last_response) == 0)
  1487. {
  1488. // Still waiting on response
  1489. }
  1490. else
  1491. {
  1492. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1493. }
  1494. }
  1495. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1496. {
  1497. // continue waiting
  1498. }
  1499. else if (loader_model->fhttp->state == INACTIVE)
  1500. {
  1501. // inactive. try again
  1502. }
  1503. else if (loader_model->fhttp->state == ISSUE)
  1504. {
  1505. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1506. }
  1507. else
  1508. {
  1509. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1510. DEV_CRASH();
  1511. }
  1512. }
  1513. else if (current_data_state == DataStateReceived)
  1514. {
  1515. with_view_model(
  1516. view,
  1517. DataLoaderModel * model,
  1518. {
  1519. char *data_text;
  1520. if (model->parser == NULL)
  1521. {
  1522. data_text = NULL;
  1523. FURI_LOG_DEV(TAG, "Parser is NULL");
  1524. DEV_CRASH();
  1525. }
  1526. else
  1527. {
  1528. data_text = model->parser(model);
  1529. }
  1530. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1531. model->data_text = data_text;
  1532. if (data_text == NULL)
  1533. {
  1534. model->data_state = DataStateParseError;
  1535. }
  1536. else
  1537. {
  1538. model->data_state = DataStateParsed;
  1539. }
  1540. },
  1541. true);
  1542. }
  1543. else if (current_data_state == DataStateParsed)
  1544. {
  1545. with_view_model(
  1546. view,
  1547. DataLoaderModel * model,
  1548. {
  1549. if (++model->request_index < model->request_count)
  1550. {
  1551. model->data_state = DataStateInitial;
  1552. }
  1553. else
  1554. {
  1555. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1556. if (model->data_text != NULL)
  1557. {
  1558. free(model->data_text);
  1559. model->data_text = NULL;
  1560. }
  1561. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1562. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1563. }
  1564. },
  1565. true);
  1566. }
  1567. }
  1568. static void flip_store_loader_timer_callback(void *context)
  1569. {
  1570. if (context == NULL)
  1571. {
  1572. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1573. DEV_CRASH();
  1574. return;
  1575. }
  1576. FlipStoreApp *app = (FlipStoreApp *)context;
  1577. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1578. }
  1579. static void flip_store_loader_on_enter(void *context)
  1580. {
  1581. if (context == NULL)
  1582. {
  1583. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1584. DEV_CRASH();
  1585. return;
  1586. }
  1587. FlipStoreApp *app = (FlipStoreApp *)context;
  1588. View *view = app->view_loader;
  1589. with_view_model(
  1590. view,
  1591. DataLoaderModel * model,
  1592. {
  1593. view_set_previous_callback(view, model->back_callback);
  1594. if (model->timer == NULL)
  1595. {
  1596. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1597. }
  1598. furi_timer_start(model->timer, 250);
  1599. },
  1600. true);
  1601. }
  1602. static void flip_store_loader_on_exit(void *context)
  1603. {
  1604. if (context == NULL)
  1605. {
  1606. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1607. DEV_CRASH();
  1608. return;
  1609. }
  1610. FlipStoreApp *app = (FlipStoreApp *)context;
  1611. View *view = app->view_loader;
  1612. with_view_model(
  1613. view,
  1614. DataLoaderModel * model,
  1615. {
  1616. if (model->timer)
  1617. {
  1618. furi_timer_stop(model->timer);
  1619. }
  1620. },
  1621. false);
  1622. }
  1623. void flip_store_loader_init(View *view)
  1624. {
  1625. if (view == NULL)
  1626. {
  1627. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1628. DEV_CRASH();
  1629. return;
  1630. }
  1631. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1632. view_set_enter_callback(view, flip_store_loader_on_enter);
  1633. view_set_exit_callback(view, flip_store_loader_on_exit);
  1634. }
  1635. void flip_store_loader_free_model(View *view)
  1636. {
  1637. if (view == NULL)
  1638. {
  1639. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1640. DEV_CRASH();
  1641. return;
  1642. }
  1643. with_view_model(
  1644. view,
  1645. DataLoaderModel * model,
  1646. {
  1647. if (model->timer)
  1648. {
  1649. furi_timer_free(model->timer);
  1650. model->timer = NULL;
  1651. }
  1652. if (model->parser_context)
  1653. {
  1654. // do not free the context here, it is the app context
  1655. // free(model->parser_context);
  1656. // model->parser_context = NULL;
  1657. }
  1658. if (model->fhttp)
  1659. {
  1660. flipper_http_free(model->fhttp);
  1661. model->fhttp = NULL;
  1662. }
  1663. },
  1664. false);
  1665. }
  1666. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1667. {
  1668. if (context == NULL)
  1669. {
  1670. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1671. DEV_CRASH();
  1672. return false;
  1673. }
  1674. switch (index)
  1675. {
  1676. case FlipStoreCustomEventProcess:
  1677. flip_store_loader_process_callback(context);
  1678. return true;
  1679. default:
  1680. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1681. return false;
  1682. }
  1683. }
  1684. 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)
  1685. {
  1686. if (app == NULL)
  1687. {
  1688. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1689. DEV_CRASH();
  1690. return;
  1691. }
  1692. View *view = app->view_loader;
  1693. if (view == NULL)
  1694. {
  1695. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1696. DEV_CRASH();
  1697. return;
  1698. }
  1699. with_view_model(
  1700. view,
  1701. DataLoaderModel * model,
  1702. {
  1703. model->title = title;
  1704. model->fetcher = fetcher;
  1705. model->parser = parser;
  1706. model->request_index = 0;
  1707. model->request_count = request_count;
  1708. model->back_callback = back;
  1709. model->data_state = DataStateInitial;
  1710. model->data_text = NULL;
  1711. //
  1712. model->parser_context = app;
  1713. if (!model->fhttp)
  1714. {
  1715. model->fhttp = flipper_http_alloc();
  1716. }
  1717. },
  1718. true);
  1719. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1720. }