storage.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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");
  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/%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/%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 true;
  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. 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. FURI_LOG_E(TAG, "Invalid key");
  499. furi_string_free(result);
  500. return NULL;
  501. }
  502. bool is_logged_in_to_flip_social()
  503. {
  504. // load flip social settings and check if logged in
  505. FuriString *is_logged_in = flip_social_info("is_logged_in");
  506. if (!is_logged_in)
  507. {
  508. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  509. return false;
  510. }
  511. if (furi_string_cmp(is_logged_in, "true") == 0)
  512. {
  513. // copy the logged_in FlipSocaial settings to FlipWorld
  514. FuriString *username = flip_social_info("login_username_logged_in");
  515. FuriString *password = flip_social_info("login_password_logged_out");
  516. FuriString *wifi_password = flip_social_info("password");
  517. FuriString *wifi_ssid = flip_social_info("ssid");
  518. if (!username || !password || !wifi_password || !wifi_ssid)
  519. {
  520. furi_string_free(username);
  521. furi_string_free(password);
  522. furi_string_free(wifi_password);
  523. furi_string_free(wifi_ssid);
  524. return false;
  525. }
  526. save_settings(furi_string_get_cstr(wifi_ssid), furi_string_get_cstr(wifi_password), furi_string_get_cstr(username), furi_string_get_cstr(password));
  527. furi_string_free(username);
  528. furi_string_free(password);
  529. furi_string_free(wifi_password);
  530. furi_string_free(wifi_ssid);
  531. furi_string_free(is_logged_in);
  532. return true;
  533. }
  534. furi_string_free(is_logged_in);
  535. return false;
  536. }
  537. bool is_logged_in()
  538. {
  539. char is_logged_in[64];
  540. if (load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  541. {
  542. return strcmp(is_logged_in, "true") == 0;
  543. }
  544. return false;
  545. }
  546. static bool load_flip_social_settings(
  547. char *ssid,
  548. size_t ssid_size,
  549. char *password,
  550. size_t password_size,
  551. char *login_username_logged_out,
  552. size_t username_out_size,
  553. char *login_username_logged_in,
  554. size_t username_in_size,
  555. char *login_password_logged_out,
  556. size_t password_out_size,
  557. char *change_password_logged_in,
  558. size_t change_password_size,
  559. char *change_bio_logged_in,
  560. size_t change_bio_size,
  561. char *is_logged_in,
  562. size_t is_logged_in_size)
  563. {
  564. Storage *storage = furi_record_open(RECORD_STORAGE);
  565. File *file = storage_file_alloc(storage);
  566. // file path from flipsocial
  567. char file_path[128];
  568. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/settings.bin");
  569. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  570. {
  571. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", file_path);
  572. storage_file_free(file);
  573. furi_record_close(RECORD_STORAGE);
  574. return false; // Return false if the file does not exist
  575. }
  576. // Load the ssid
  577. size_t ssid_length;
  578. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  579. storage_file_read(file, ssid, ssid_length) != ssid_length)
  580. {
  581. FURI_LOG_E(TAG, "Failed to read SSID");
  582. storage_file_close(file);
  583. storage_file_free(file);
  584. furi_record_close(RECORD_STORAGE);
  585. return false;
  586. }
  587. else
  588. {
  589. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  590. }
  591. // Load the password
  592. size_t password_length;
  593. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  594. storage_file_read(file, password, password_length) != password_length)
  595. {
  596. FURI_LOG_E(TAG, "Failed to read password");
  597. storage_file_close(file);
  598. storage_file_free(file);
  599. furi_record_close(RECORD_STORAGE);
  600. return false;
  601. }
  602. else
  603. {
  604. password[password_length - 1] = '\0'; // Ensure null-termination
  605. }
  606. // Load the login_username_logged_out
  607. size_t username_out_length;
  608. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  609. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  610. {
  611. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  612. // storage_file_close(file);
  613. // storage_file_free(file);
  614. // furi_record_close(RECORD_STORAGE);
  615. // return false;
  616. }
  617. else
  618. {
  619. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  620. }
  621. // Load the login_username_logged_in
  622. size_t username_in_length;
  623. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  624. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  625. {
  626. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  627. // storage_file_close(file);
  628. // storage_file_free(file);
  629. // furi_record_close(RECORD_STORAGE);
  630. // return false;
  631. }
  632. else
  633. {
  634. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  635. }
  636. // Load the login_password_logged_out
  637. size_t password_out_length;
  638. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  639. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  640. {
  641. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  642. // storage_file_close(file);
  643. // storage_file_free(file);
  644. // furi_record_close(RECORD_STORAGE);
  645. // return false;
  646. }
  647. else
  648. {
  649. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  650. }
  651. // Load the change_password_logged_in
  652. size_t change_password_length;
  653. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  654. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  655. {
  656. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  657. // storage_file_close(file);
  658. // storage_file_free(file);
  659. // furi_record_close(RECORD_STORAGE);
  660. // return false;
  661. }
  662. else
  663. {
  664. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  665. }
  666. // Load the is_logged_in
  667. size_t is_logged_in_length;
  668. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  669. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  670. {
  671. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  672. // storage_file_close(file);
  673. // storage_file_free(file);
  674. // furi_record_close(RECORD_STORAGE);
  675. // return false;
  676. }
  677. else
  678. {
  679. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  680. }
  681. // Load the change_bio_logged_in
  682. size_t change_bio_length;
  683. if (storage_file_read(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) || change_bio_length > change_bio_size ||
  684. storage_file_read(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  685. {
  686. FURI_LOG_E(TAG, "Failed to read change_bio_logged_in");
  687. // storage_file_close(file);
  688. // storage_file_free(file);
  689. // furi_record_close(RECORD_STORAGE);
  690. // return false;
  691. }
  692. else
  693. {
  694. change_bio_logged_in[change_bio_length - 1] = '\0'; // Ensure null-termination
  695. }
  696. storage_file_close(file);
  697. storage_file_free(file);
  698. furi_record_close(RECORD_STORAGE);
  699. return true;
  700. }