storage.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. #include "flip_storage/storage.h"
  2. // Forward declaration for use in other functions
  3. static bool load_flip_social_settings(
  4. char *ssid,
  5. size_t ssid_size,
  6. char *password,
  7. size_t password_size,
  8. char *login_username_logged_out,
  9. size_t username_out_size,
  10. char *login_username_logged_in,
  11. size_t username_in_size,
  12. char *login_password_logged_out,
  13. size_t password_out_size,
  14. char *change_password_logged_in,
  15. size_t change_password_size,
  16. char *change_bio_logged_in,
  17. size_t change_bio_size,
  18. char *is_logged_in,
  19. size_t is_logged_in_size);
  20. void save_settings(
  21. const char *wifi_ssid,
  22. const char *wifi_password,
  23. const char *username,
  24. const char *password)
  25. {
  26. // Create the directory for saving settings
  27. char directory_path[256];
  28. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  29. // Create the directory
  30. Storage *storage = furi_record_open(RECORD_STORAGE);
  31. storage_common_mkdir(storage, directory_path);
  32. // Open the settings file
  33. File *file = storage_file_alloc(storage);
  34. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  35. {
  36. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  37. storage_file_free(file);
  38. furi_record_close(RECORD_STORAGE);
  39. return;
  40. }
  41. // Save the wifi_ssid length and data
  42. size_t wifi_ssid_length = strlen(wifi_ssid) + 1; // Include null terminator
  43. if (storage_file_write(file, &wifi_ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  44. storage_file_write(file, wifi_ssid, wifi_ssid_length) != wifi_ssid_length)
  45. {
  46. FURI_LOG_E(TAG, "Failed to write wifi_SSID");
  47. }
  48. // Save the wifi_password length and data
  49. size_t wifi_password_length = strlen(wifi_password) + 1; // Include null terminator
  50. if (storage_file_write(file, &wifi_password_length, sizeof(size_t)) != sizeof(size_t) ||
  51. storage_file_write(file, wifi_password, wifi_password_length) != wifi_password_length)
  52. {
  53. FURI_LOG_E(TAG, "Failed to write wifi_password");
  54. }
  55. // Save the username length and data
  56. size_t username_length = strlen(username) + 1; // Include null terminator
  57. if (storage_file_write(file, &username_length, sizeof(size_t)) != sizeof(size_t) ||
  58. storage_file_write(file, username, username_length) != username_length)
  59. {
  60. FURI_LOG_E(TAG, "Failed to write username");
  61. }
  62. // Save the password length and data
  63. size_t password_length = strlen(password) + 1; // Include null terminator
  64. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  65. storage_file_write(file, password, password_length) != password_length)
  66. {
  67. FURI_LOG_E(TAG, "Failed to write password");
  68. }
  69. storage_file_close(file);
  70. storage_file_free(file);
  71. furi_record_close(RECORD_STORAGE);
  72. }
  73. bool load_settings(
  74. char *wifi_ssid,
  75. size_t wifi_ssid_size,
  76. char *wifi_password,
  77. size_t wifi_password_size,
  78. char *username,
  79. size_t username_size,
  80. char *password,
  81. size_t password_size)
  82. {
  83. Storage *storage = furi_record_open(RECORD_STORAGE);
  84. File *file = storage_file_alloc(storage);
  85. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  86. {
  87. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  88. storage_file_free(file);
  89. furi_record_close(RECORD_STORAGE);
  90. return false; // Return false if the file does not exist
  91. }
  92. // Load the wifi_ssid
  93. size_t wifi_ssid_length;
  94. if (storage_file_read(file, &wifi_ssid_length, sizeof(size_t)) != sizeof(size_t) || wifi_ssid_length > wifi_ssid_size ||
  95. storage_file_read(file, wifi_ssid, wifi_ssid_length) != wifi_ssid_length)
  96. {
  97. FURI_LOG_E(TAG, "Failed to read wifi_SSID");
  98. storage_file_close(file);
  99. storage_file_free(file);
  100. furi_record_close(RECORD_STORAGE);
  101. return false;
  102. }
  103. wifi_ssid[wifi_ssid_length - 1] = '\0'; // Ensure null-termination
  104. // Load the wifi_password
  105. size_t wifi_password_length;
  106. if (storage_file_read(file, &wifi_password_length, sizeof(size_t)) != sizeof(size_t) || wifi_password_length > wifi_password_size ||
  107. storage_file_read(file, wifi_password, wifi_password_length) != wifi_password_length)
  108. {
  109. FURI_LOG_E(TAG, "Failed to read wifi_password");
  110. storage_file_close(file);
  111. storage_file_free(file);
  112. furi_record_close(RECORD_STORAGE);
  113. return false;
  114. }
  115. wifi_password[wifi_password_length - 1] = '\0'; // Ensure null-termination
  116. // Load the username
  117. size_t username_length;
  118. if (storage_file_read(file, &username_length, sizeof(size_t)) != sizeof(size_t) || username_length > username_size ||
  119. storage_file_read(file, username, username_length) != username_length)
  120. {
  121. FURI_LOG_E(TAG, "Failed to read username");
  122. storage_file_close(file);
  123. storage_file_free(file);
  124. furi_record_close(RECORD_STORAGE);
  125. return false;
  126. }
  127. username[username_length - 1] = '\0'; // Ensure null-termination
  128. // Load the password
  129. size_t password_length;
  130. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  131. storage_file_read(file, password, password_length) != password_length)
  132. {
  133. FURI_LOG_E(TAG, "Failed to read password");
  134. storage_file_close(file);
  135. storage_file_free(file);
  136. furi_record_close(RECORD_STORAGE);
  137. return false;
  138. }
  139. password[password_length - 1] = '\0'; // Ensure null-termination
  140. storage_file_close(file);
  141. storage_file_free(file);
  142. furi_record_close(RECORD_STORAGE);
  143. return true;
  144. }
  145. bool save_char(
  146. const char *path_name, const char *value)
  147. {
  148. if (!value)
  149. {
  150. return false;
  151. }
  152. // Create the directory for saving settings
  153. char directory_path[256];
  154. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  155. // Create the directory
  156. Storage *storage = furi_record_open(RECORD_STORAGE);
  157. storage_common_mkdir(storage, directory_path);
  158. // Open the settings file
  159. File *file = storage_file_alloc(storage);
  160. char file_path[256];
  161. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/%s.txt", path_name);
  162. // Open the file in write mode
  163. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  164. {
  165. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  166. storage_file_free(file);
  167. furi_record_close(RECORD_STORAGE);
  168. return false;
  169. }
  170. // Write the data to the file
  171. size_t data_size = strlen(value) + 1; // Include null terminator
  172. if (storage_file_write(file, value, data_size) != data_size)
  173. {
  174. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  175. storage_file_close(file);
  176. storage_file_free(file);
  177. furi_record_close(RECORD_STORAGE);
  178. return false;
  179. }
  180. storage_file_close(file);
  181. storage_file_free(file);
  182. furi_record_close(RECORD_STORAGE);
  183. return true;
  184. }
  185. bool load_char(
  186. const char *path_name,
  187. char *value,
  188. size_t value_size)
  189. {
  190. if (!value)
  191. {
  192. return false;
  193. }
  194. Storage *storage = furi_record_open(RECORD_STORAGE);
  195. File *file = storage_file_alloc(storage);
  196. char file_path[256];
  197. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/%s.txt", path_name);
  198. // Open the file for reading
  199. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  200. {
  201. storage_file_free(file);
  202. furi_record_close(RECORD_STORAGE);
  203. return NULL; // Return false if the file does not exist
  204. }
  205. // Read data into the buffer
  206. size_t read_count = storage_file_read(file, value, value_size);
  207. if (storage_file_get_error(file) != FSE_OK)
  208. {
  209. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  210. storage_file_close(file);
  211. storage_file_free(file);
  212. furi_record_close(RECORD_STORAGE);
  213. return false;
  214. }
  215. // Ensure null-termination
  216. value[read_count - 1] = '\0';
  217. storage_file_close(file);
  218. storage_file_free(file);
  219. furi_record_close(RECORD_STORAGE);
  220. return strlen(value) > 0;
  221. }
  222. bool save_world(
  223. const char *name,
  224. const char *world_data)
  225. {
  226. // Create the directory for saving settings
  227. char directory_path[256];
  228. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  229. // Create the directory
  230. Storage *storage = furi_record_open(RECORD_STORAGE);
  231. storage_common_mkdir(storage, directory_path);
  232. // Open the settings file
  233. File *file = storage_file_alloc(storage);
  234. char file_path[256];
  235. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  236. // Open the file in write mode
  237. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  238. {
  239. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  240. storage_file_free(file);
  241. furi_record_close(RECORD_STORAGE);
  242. return false;
  243. }
  244. // Write the data to the file
  245. size_t data_size = strlen(world_data) + 1; // Include null terminator
  246. if (storage_file_write(file, world_data, data_size) != data_size)
  247. {
  248. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  249. storage_file_close(file);
  250. storage_file_free(file);
  251. furi_record_close(RECORD_STORAGE);
  252. return false;
  253. }
  254. storage_file_close(file);
  255. storage_file_free(file);
  256. furi_record_close(RECORD_STORAGE);
  257. return true;
  258. }
  259. bool save_world_furi(FuriString *name, FuriString *world_data)
  260. {
  261. // Create the directory for saving settings
  262. char directory_path[256];
  263. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  264. // Create the directory
  265. Storage *storage = furi_record_open(RECORD_STORAGE);
  266. storage_common_mkdir(storage, directory_path);
  267. // Open the settings file
  268. File *file = storage_file_alloc(storage);
  269. char file_path[256];
  270. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(name));
  271. // Open the file in write mode
  272. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  273. {
  274. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  275. storage_file_free(file);
  276. furi_record_close(RECORD_STORAGE);
  277. return false;
  278. }
  279. // Write the data to the file
  280. size_t data_size = furi_string_size(world_data) + 1; // Include null terminator
  281. if (storage_file_write(file, furi_string_get_cstr(world_data), data_size) != data_size)
  282. {
  283. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  284. storage_file_close(file);
  285. storage_file_free(file);
  286. furi_record_close(RECORD_STORAGE);
  287. return false;
  288. }
  289. storage_file_close(file);
  290. storage_file_free(file);
  291. furi_record_close(RECORD_STORAGE);
  292. return true;
  293. }
  294. bool load_world(
  295. const char *name,
  296. char *json_data,
  297. size_t json_data_size)
  298. {
  299. Storage *storage = furi_record_open(RECORD_STORAGE);
  300. File *file = storage_file_alloc(storage);
  301. char file_path[256];
  302. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  303. // Open the file for reading
  304. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  305. {
  306. storage_file_free(file);
  307. furi_record_close(RECORD_STORAGE);
  308. return NULL; // Return false if the file does not exist
  309. }
  310. // Read data into the buffer
  311. size_t read_count = storage_file_read(file, json_data, json_data_size);
  312. if (storage_file_get_error(file) != FSE_OK)
  313. {
  314. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  315. storage_file_close(file);
  316. storage_file_free(file);
  317. furi_record_close(RECORD_STORAGE);
  318. return false;
  319. }
  320. // Ensure null-termination
  321. json_data[read_count - 1] = '\0';
  322. storage_file_close(file);
  323. storage_file_free(file);
  324. furi_record_close(RECORD_STORAGE);
  325. return true;
  326. }
  327. FuriString *load_furi_world(
  328. const char *name)
  329. {
  330. Storage *storage = furi_record_open(RECORD_STORAGE);
  331. File *file = storage_file_alloc(storage);
  332. char file_path[128];
  333. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  334. // Open the file for reading
  335. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  336. {
  337. storage_file_free(file);
  338. furi_record_close(RECORD_STORAGE);
  339. return NULL; // Return false if the file does not exist
  340. }
  341. // Allocate a FuriString to hold the received data
  342. FuriString *str_result = furi_string_alloc();
  343. if (!str_result)
  344. {
  345. FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
  346. storage_file_close(file);
  347. storage_file_free(file);
  348. furi_record_close(RECORD_STORAGE);
  349. return NULL;
  350. }
  351. // Reset the FuriString to ensure it's empty before reading
  352. furi_string_reset(str_result);
  353. // Define a buffer to hold the read data
  354. uint8_t *buffer = (uint8_t *)malloc(MAX_FILE_SHOW);
  355. if (!buffer)
  356. {
  357. FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
  358. furi_string_free(str_result);
  359. storage_file_close(file);
  360. storage_file_free(file);
  361. furi_record_close(RECORD_STORAGE);
  362. return NULL;
  363. }
  364. // Read data into the buffer
  365. size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW);
  366. if (storage_file_get_error(file) != FSE_OK)
  367. {
  368. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  369. furi_string_free(str_result);
  370. storage_file_close(file);
  371. storage_file_free(file);
  372. furi_record_close(RECORD_STORAGE);
  373. return NULL;
  374. }
  375. // Append each byte to the FuriString
  376. for (size_t i = 0; i < read_count; i++)
  377. {
  378. furi_string_push_back(str_result, buffer[i]);
  379. }
  380. // Check if there is more data beyond the maximum size
  381. char extra_byte;
  382. storage_file_read(file, &extra_byte, 1);
  383. // Clean up
  384. storage_file_close(file);
  385. storage_file_free(file);
  386. furi_record_close(RECORD_STORAGE);
  387. free(buffer);
  388. return str_result;
  389. }
  390. bool world_exists(const char *name)
  391. {
  392. if (!name)
  393. {
  394. FURI_LOG_E(TAG, "Invalid name");
  395. return false;
  396. }
  397. Storage *storage = furi_record_open(RECORD_STORAGE);
  398. if (!storage)
  399. {
  400. FURI_LOG_E(TAG, "Failed to open storage");
  401. return false;
  402. }
  403. char file_path[256];
  404. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  405. bool does_exist = storage_file_exists(storage, file_path);
  406. // Clean up
  407. furi_record_close(RECORD_STORAGE);
  408. return does_exist;
  409. }
  410. bool save_world_names(const FuriString *json)
  411. {
  412. // Create the directory for saving settings
  413. char directory_path[256];
  414. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  415. // Create the directory
  416. Storage *storage = furi_record_open(RECORD_STORAGE);
  417. storage_common_mkdir(storage, directory_path);
  418. // Open the settings file
  419. File *file = storage_file_alloc(storage);
  420. char file_path[128];
  421. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  422. // Open the file in write mode
  423. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  424. {
  425. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  426. storage_file_free(file);
  427. furi_record_close(RECORD_STORAGE);
  428. return false;
  429. }
  430. // Write the data to the file
  431. size_t data_size = furi_string_size(json) + 1; // Include null terminator
  432. if (storage_file_write(file, furi_string_get_cstr(json), data_size) != data_size)
  433. {
  434. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  435. storage_file_close(file);
  436. storage_file_free(file);
  437. furi_record_close(RECORD_STORAGE);
  438. return false;
  439. }
  440. storage_file_close(file);
  441. storage_file_free(file);
  442. furi_record_close(RECORD_STORAGE);
  443. return true;
  444. }
  445. static FuriString *flip_social_info(char *key)
  446. {
  447. char ssid[64];
  448. char password[64];
  449. char login_username_logged_out[64];
  450. char login_username_logged_in[64];
  451. char login_password_logged_out[64];
  452. char change_password_logged_in[64];
  453. char change_bio_logged_in[64];
  454. char is_logged_in[64];
  455. FuriString *result = furi_string_alloc();
  456. if (!result)
  457. {
  458. FURI_LOG_E(TAG, "Failed to allocate FuriString");
  459. return NULL;
  460. }
  461. if (!load_flip_social_settings(ssid, sizeof(ssid), password, sizeof(password), login_username_logged_out, sizeof(login_username_logged_out), login_username_logged_in, sizeof(login_username_logged_in), login_password_logged_out, sizeof(login_password_logged_out), change_password_logged_in, sizeof(change_password_logged_in), change_bio_logged_in, sizeof(change_bio_logged_in), is_logged_in, sizeof(is_logged_in)))
  462. {
  463. FURI_LOG_E(TAG, "Failed to load flip social settings");
  464. return NULL;
  465. }
  466. if (strcmp(key, "ssid") == 0)
  467. {
  468. furi_string_set_str(result, ssid);
  469. }
  470. else if (strcmp(key, "password") == 0)
  471. {
  472. furi_string_set_str(result, password);
  473. }
  474. else if (strcmp(key, "login_username_logged_out") == 0)
  475. {
  476. furi_string_set_str(result, login_username_logged_out);
  477. }
  478. else if (strcmp(key, "login_username_logged_in") == 0)
  479. {
  480. furi_string_set_str(result, login_username_logged_in);
  481. }
  482. else if (strcmp(key, "login_password_logged_out") == 0)
  483. {
  484. furi_string_set_str(result, login_password_logged_out);
  485. }
  486. else if (strcmp(key, "change_password_logged_in") == 0)
  487. {
  488. furi_string_set_str(result, change_password_logged_in);
  489. }
  490. else if (strcmp(key, "change_bio_logged_in") == 0)
  491. {
  492. furi_string_set_str(result, change_bio_logged_in);
  493. }
  494. else if (strcmp(key, "is_logged_in") == 0)
  495. {
  496. furi_string_set_str(result, is_logged_in);
  497. }
  498. else
  499. {
  500. FURI_LOG_E(TAG, "Invalid key");
  501. furi_string_free(result);
  502. return NULL;
  503. }
  504. return result;
  505. }
  506. bool is_logged_in_to_flip_social()
  507. {
  508. // load flip social settings and check if logged in
  509. FuriString *is_logged_in = flip_social_info("is_logged_in");
  510. if (!is_logged_in)
  511. {
  512. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  513. return false;
  514. }
  515. if (furi_string_cmp(is_logged_in, "true") == 0)
  516. {
  517. // copy the logged_in FlipSocaial settings to FlipWorld
  518. FuriString *username = flip_social_info("login_username_logged_in");
  519. FuriString *password = flip_social_info("change_password_logged_in");
  520. FuriString *wifi_password = flip_social_info("password");
  521. FuriString *wifi_ssid = flip_social_info("ssid");
  522. if (!username || !password || !wifi_password || !wifi_ssid)
  523. {
  524. furi_string_free(username);
  525. furi_string_free(password);
  526. furi_string_free(wifi_password);
  527. furi_string_free(wifi_ssid);
  528. return false;
  529. }
  530. save_settings(furi_string_get_cstr(wifi_ssid), furi_string_get_cstr(wifi_password), furi_string_get_cstr(username), furi_string_get_cstr(password));
  531. furi_string_free(username);
  532. furi_string_free(password);
  533. furi_string_free(wifi_password);
  534. furi_string_free(wifi_ssid);
  535. furi_string_free(is_logged_in);
  536. return true;
  537. }
  538. furi_string_free(is_logged_in);
  539. return false;
  540. }
  541. bool is_logged_in()
  542. {
  543. char is_logged_in[64];
  544. if (load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  545. {
  546. return strcmp(is_logged_in, "true") == 0;
  547. }
  548. return false;
  549. }
  550. static bool load_flip_social_settings(
  551. char *ssid,
  552. size_t ssid_size,
  553. char *password,
  554. size_t password_size,
  555. char *login_username_logged_out,
  556. size_t username_out_size,
  557. char *login_username_logged_in,
  558. size_t username_in_size,
  559. char *login_password_logged_out,
  560. size_t password_out_size,
  561. char *change_password_logged_in,
  562. size_t change_password_size,
  563. char *change_bio_logged_in,
  564. size_t change_bio_size,
  565. char *is_logged_in,
  566. size_t is_logged_in_size)
  567. {
  568. Storage *storage = furi_record_open(RECORD_STORAGE);
  569. File *file = storage_file_alloc(storage);
  570. // file path from flipsocial
  571. char file_path[128];
  572. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/settings.bin");
  573. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  574. {
  575. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", file_path);
  576. storage_file_free(file);
  577. furi_record_close(RECORD_STORAGE);
  578. return false; // Return false if the file does not exist
  579. }
  580. // Load the ssid
  581. size_t ssid_length;
  582. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  583. storage_file_read(file, ssid, ssid_length) != ssid_length)
  584. {
  585. FURI_LOG_E(TAG, "Failed to read SSID");
  586. storage_file_close(file);
  587. storage_file_free(file);
  588. furi_record_close(RECORD_STORAGE);
  589. return false;
  590. }
  591. else
  592. {
  593. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  594. }
  595. // Load the password
  596. size_t password_length;
  597. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  598. storage_file_read(file, password, password_length) != password_length)
  599. {
  600. FURI_LOG_E(TAG, "Failed to read password");
  601. storage_file_close(file);
  602. storage_file_free(file);
  603. furi_record_close(RECORD_STORAGE);
  604. return false;
  605. }
  606. else
  607. {
  608. password[password_length - 1] = '\0'; // Ensure null-termination
  609. }
  610. // Load the login_username_logged_out
  611. size_t username_out_length;
  612. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  613. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  614. {
  615. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  616. // storage_file_close(file);
  617. // storage_file_free(file);
  618. // furi_record_close(RECORD_STORAGE);
  619. // return false;
  620. }
  621. else
  622. {
  623. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  624. }
  625. // Load the login_username_logged_in
  626. size_t username_in_length;
  627. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  628. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  629. {
  630. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  631. // storage_file_close(file);
  632. // storage_file_free(file);
  633. // furi_record_close(RECORD_STORAGE);
  634. // return false;
  635. }
  636. else
  637. {
  638. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  639. }
  640. // Load the login_password_logged_out
  641. size_t password_out_length;
  642. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  643. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  644. {
  645. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  646. // storage_file_close(file);
  647. // storage_file_free(file);
  648. // furi_record_close(RECORD_STORAGE);
  649. // return false;
  650. }
  651. else
  652. {
  653. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  654. }
  655. // Load the change_password_logged_in
  656. size_t change_password_length;
  657. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  658. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  659. {
  660. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  661. // storage_file_close(file);
  662. // storage_file_free(file);
  663. // furi_record_close(RECORD_STORAGE);
  664. // return false;
  665. }
  666. else
  667. {
  668. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  669. }
  670. // Load the is_logged_in
  671. size_t is_logged_in_length;
  672. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  673. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  674. {
  675. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  676. // storage_file_close(file);
  677. // storage_file_free(file);
  678. // furi_record_close(RECORD_STORAGE);
  679. // return false;
  680. }
  681. else
  682. {
  683. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  684. }
  685. // Load the change_bio_logged_in
  686. size_t change_bio_length;
  687. if (storage_file_read(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) || change_bio_length > change_bio_size ||
  688. storage_file_read(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  689. {
  690. FURI_LOG_E(TAG, "Failed to read change_bio_logged_in");
  691. // storage_file_close(file);
  692. // storage_file_free(file);
  693. // furi_record_close(RECORD_STORAGE);
  694. // return false;
  695. }
  696. else
  697. {
  698. change_bio_logged_in[change_bio_length - 1] = '\0'; // Ensure null-termination
  699. }
  700. storage_file_close(file);
  701. storage_file_free(file);
  702. furi_record_close(RECORD_STORAGE);
  703. return true;
  704. }