flip_store_callback.c 58 KB

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