flip_store_callback.c 50 KB

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