flip_social_storage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #include "flip_social_storage.h"
  2. // Function to save the playlist
  3. void save_playlist(const PreSavedPlaylist *playlist)
  4. {
  5. if (!playlist)
  6. {
  7. FURI_LOG_E(TAG, "Playlist is NULL");
  8. return;
  9. }
  10. // Create the directory for saving settings
  11. char directory_path[128];
  12. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  13. // Create the directory
  14. Storage *storage = furi_record_open(RECORD_STORAGE);
  15. storage_common_mkdir(storage, directory_path);
  16. // Open the settings file
  17. File *file = storage_file_alloc(storage);
  18. if (!storage_file_open(file, PRE_SAVED_MESSAGES_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  19. {
  20. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", PRE_SAVED_MESSAGES_PATH);
  21. storage_file_free(file);
  22. furi_record_close(RECORD_STORAGE);
  23. return;
  24. }
  25. // Write each playlist message on a separate line
  26. for (size_t i = 0; i < playlist->count; ++i)
  27. {
  28. // Write the message
  29. if (storage_file_write(file, playlist->messages[i], strlen(playlist->messages[i])) != strlen(playlist->messages[i]))
  30. {
  31. FURI_LOG_E(TAG, "Failed to write playlist message %zu", i);
  32. }
  33. // Write a newline after each message
  34. if (storage_file_write(file, "\n", 1) != 1)
  35. {
  36. FURI_LOG_E(TAG, "Failed to write newline after message %zu", i);
  37. }
  38. }
  39. // Close the file and storage
  40. storage_file_close(file);
  41. storage_file_free(file);
  42. furi_record_close(RECORD_STORAGE);
  43. }
  44. bool load_playlist(PreSavedPlaylist *playlist)
  45. {
  46. if (!playlist)
  47. {
  48. FURI_LOG_E(TAG, "Playlist is NULL");
  49. return false;
  50. }
  51. // Clear existing data in the playlist
  52. memset(playlist->messages, 0, sizeof(playlist->messages));
  53. playlist->count = 0;
  54. // Open the storage
  55. Storage *storage = furi_record_open(RECORD_STORAGE);
  56. File *file = storage_file_alloc(storage);
  57. if (!storage_file_open(file, PRE_SAVED_MESSAGES_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  58. {
  59. FURI_LOG_E(TAG, "Failed to open pre-saved messages file for reading: %s", PRE_SAVED_MESSAGES_PATH);
  60. storage_file_free(file);
  61. furi_record_close(RECORD_STORAGE);
  62. return false;
  63. }
  64. // Read the file line by line
  65. char line[MAX_MESSAGE_LENGTH] = {0};
  66. size_t line_pos = 0;
  67. char ch;
  68. while (storage_file_read(file, &ch, 1) == 1)
  69. {
  70. if (ch == '\n' || line_pos >= (MAX_MESSAGE_LENGTH - 1)) // End of line or max length reached
  71. {
  72. line[line_pos] = '\0'; // Null-terminate the line
  73. strncpy(playlist->messages[playlist->count], line, MAX_MESSAGE_LENGTH);
  74. playlist->count++;
  75. line_pos = 0;
  76. // Ensure playlist count does not exceed maximum allowed
  77. if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
  78. {
  79. FURI_LOG_W(TAG, "Reached maximum playlist messages");
  80. break;
  81. }
  82. }
  83. else
  84. {
  85. line[line_pos++] = ch;
  86. }
  87. }
  88. // Handle the last line if it does not end with a newline
  89. if (line_pos > 0)
  90. {
  91. line[line_pos] = '\0'; // Null-terminate the last line
  92. strncpy(playlist->messages[playlist->count], line, MAX_MESSAGE_LENGTH);
  93. playlist->count++;
  94. }
  95. // Close the file and storage
  96. storage_file_close(file);
  97. storage_file_free(file);
  98. furi_record_close(RECORD_STORAGE);
  99. return true;
  100. }
  101. void save_settings(
  102. const char *ssid,
  103. const char *password,
  104. const char *login_username_logged_out,
  105. const char *login_username_logged_in,
  106. const char *login_password_logged_out,
  107. const char *change_password_logged_in,
  108. const char *change_bio_logged_in,
  109. const char *is_logged_in)
  110. {
  111. // Create the directory for saving settings
  112. char directory_path[128];
  113. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  114. // Create the directory
  115. Storage *storage = furi_record_open(RECORD_STORAGE);
  116. storage_common_mkdir(storage, directory_path);
  117. // Open the settings file
  118. File *file = storage_file_alloc(storage);
  119. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  120. {
  121. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  122. storage_file_free(file);
  123. furi_record_close(RECORD_STORAGE);
  124. return;
  125. }
  126. // Save the ssid length and data
  127. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  128. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  129. storage_file_write(file, ssid, ssid_length) != ssid_length)
  130. {
  131. FURI_LOG_E(TAG, "Failed to write SSID");
  132. }
  133. // Save the password length and data
  134. size_t password_length = strlen(password) + 1; // Include null terminator
  135. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  136. storage_file_write(file, password, password_length) != password_length)
  137. {
  138. FURI_LOG_E(TAG, "Failed to write password");
  139. }
  140. // Save the login_username_logged_out length and data
  141. size_t username_out_length = strlen(login_username_logged_out) + 1; // Include null terminator
  142. if (storage_file_write(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) ||
  143. storage_file_write(file, login_username_logged_out, username_out_length) != username_out_length)
  144. {
  145. FURI_LOG_E(TAG, "Failed to write login_username_logged_out");
  146. }
  147. // Save the login_username_logged_in length and data
  148. size_t username_in_length = strlen(login_username_logged_in) + 1; // Include null terminator
  149. if (storage_file_write(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) ||
  150. storage_file_write(file, login_username_logged_in, username_in_length) != username_in_length)
  151. {
  152. FURI_LOG_E(TAG, "Failed to write login_username_logged_in");
  153. }
  154. // Save the login_password_logged_out length and data
  155. size_t password_out_length = strlen(login_password_logged_out) + 1; // Include null terminator
  156. if (storage_file_write(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) ||
  157. storage_file_write(file, login_password_logged_out, password_out_length) != password_out_length)
  158. {
  159. FURI_LOG_E(TAG, "Failed to write login_password_logged_out");
  160. }
  161. // Save the change_password_logged_in length and data
  162. size_t change_password_length = strlen(change_password_logged_in) + 1; // Include null terminator
  163. if (storage_file_write(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) ||
  164. storage_file_write(file, change_password_logged_in, change_password_length) != change_password_length)
  165. {
  166. FURI_LOG_E(TAG, "Failed to write change_password_logged_in");
  167. }
  168. // Save the is_logged_in length and data
  169. size_t is_logged_in_length = strlen(is_logged_in) + 1; // Include null terminator
  170. if (storage_file_write(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) ||
  171. storage_file_write(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  172. {
  173. FURI_LOG_E(TAG, "Failed to write is_logged_in");
  174. }
  175. // Save the change_bio_logged_in length and data
  176. size_t change_bio_length = strlen(change_bio_logged_in) + 1; // Include null terminator
  177. if (storage_file_write(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) ||
  178. storage_file_write(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  179. {
  180. FURI_LOG_E(TAG, "Failed to write change_bio_logged_in");
  181. }
  182. storage_file_close(file);
  183. storage_file_free(file);
  184. furi_record_close(RECORD_STORAGE);
  185. }
  186. bool load_settings(
  187. char *ssid,
  188. size_t ssid_size,
  189. char *password,
  190. size_t password_size,
  191. char *login_username_logged_out,
  192. size_t username_out_size,
  193. char *login_username_logged_in,
  194. size_t username_in_size,
  195. char *login_password_logged_out,
  196. size_t password_out_size,
  197. char *change_password_logged_in,
  198. size_t change_password_size,
  199. char *change_bio_logged_in,
  200. size_t change_bio_size,
  201. char *is_logged_in,
  202. size_t is_logged_in_size)
  203. {
  204. Storage *storage = furi_record_open(RECORD_STORAGE);
  205. File *file = storage_file_alloc(storage);
  206. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  207. {
  208. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  209. storage_file_free(file);
  210. furi_record_close(RECORD_STORAGE);
  211. return false; // Return false if the file does not exist
  212. }
  213. // Load the ssid
  214. size_t ssid_length;
  215. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  216. storage_file_read(file, ssid, ssid_length) != ssid_length)
  217. {
  218. FURI_LOG_E(TAG, "Failed to read SSID");
  219. storage_file_close(file);
  220. storage_file_free(file);
  221. furi_record_close(RECORD_STORAGE);
  222. return false;
  223. }
  224. else
  225. {
  226. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  227. }
  228. // Load the password
  229. size_t password_length;
  230. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  231. storage_file_read(file, password, password_length) != password_length)
  232. {
  233. FURI_LOG_E(TAG, "Failed to read password");
  234. storage_file_close(file);
  235. storage_file_free(file);
  236. furi_record_close(RECORD_STORAGE);
  237. return false;
  238. }
  239. else
  240. {
  241. password[password_length - 1] = '\0'; // Ensure null-termination
  242. }
  243. // Load the login_username_logged_out
  244. size_t username_out_length;
  245. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  246. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  247. {
  248. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  249. // storage_file_close(file);
  250. // storage_file_free(file);
  251. // furi_record_close(RECORD_STORAGE);
  252. // return false;
  253. }
  254. else
  255. {
  256. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  257. }
  258. // Load the login_username_logged_in
  259. size_t username_in_length;
  260. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  261. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  262. {
  263. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  264. // storage_file_close(file);
  265. // storage_file_free(file);
  266. // furi_record_close(RECORD_STORAGE);
  267. // return false;
  268. }
  269. else
  270. {
  271. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  272. }
  273. // Load the login_password_logged_out
  274. size_t password_out_length;
  275. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  276. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  277. {
  278. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  279. // storage_file_close(file);
  280. // storage_file_free(file);
  281. // furi_record_close(RECORD_STORAGE);
  282. // return false;
  283. }
  284. else
  285. {
  286. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  287. }
  288. // Load the change_password_logged_in
  289. size_t change_password_length;
  290. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  291. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  292. {
  293. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  294. // storage_file_close(file);
  295. // storage_file_free(file);
  296. // furi_record_close(RECORD_STORAGE);
  297. // return false;
  298. }
  299. else
  300. {
  301. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  302. }
  303. // Load the is_logged_in
  304. size_t is_logged_in_length;
  305. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  306. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  307. {
  308. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  309. // storage_file_close(file);
  310. // storage_file_free(file);
  311. // furi_record_close(RECORD_STORAGE);
  312. // return false;
  313. }
  314. else
  315. {
  316. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  317. }
  318. // Load the change_bio_logged_in
  319. size_t change_bio_length;
  320. if (storage_file_read(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) || change_bio_length > change_bio_size ||
  321. storage_file_read(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  322. {
  323. FURI_LOG_E(TAG, "Failed to read change_bio_logged_in");
  324. // storage_file_close(file);
  325. // storage_file_free(file);
  326. // furi_record_close(RECORD_STORAGE);
  327. // return false;
  328. }
  329. else
  330. {
  331. change_bio_logged_in[change_bio_length - 1] = '\0'; // Ensure null-termination
  332. }
  333. storage_file_close(file);
  334. storage_file_free(file);
  335. furi_record_close(RECORD_STORAGE);
  336. return true;
  337. }
  338. bool flip_social_save_post(const char *post_id, const char *json_feed_data)
  339. {
  340. if (!post_id || !json_feed_data)
  341. {
  342. FURI_LOG_E(TAG, "Post ID or JSON feed data is NULL");
  343. return false;
  344. }
  345. Storage *storage = furi_record_open(RECORD_STORAGE);
  346. File *file = storage_file_alloc(storage);
  347. // Create the directory for saving the feed
  348. char directory_path[128];
  349. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  350. storage_common_mkdir(storage, directory_path);
  351. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed");
  352. storage_common_mkdir(storage, directory_path);
  353. char file_path[128];
  354. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed/feed_post_%s.json", post_id);
  355. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  356. {
  357. FURI_LOG_E(TAG, "Failed to open file for writing: %s", file_path);
  358. storage_file_free(file);
  359. furi_record_close(RECORD_STORAGE);
  360. return false;
  361. }
  362. if (storage_file_write(file, json_feed_data, strlen(json_feed_data)) != strlen(json_feed_data))
  363. {
  364. FURI_LOG_E(TAG, "Failed to write feed post data");
  365. storage_file_close(file);
  366. storage_file_free(file);
  367. furi_record_close(RECORD_STORAGE);
  368. return false;
  369. }
  370. storage_file_close(file);
  371. storage_file_free(file);
  372. furi_record_close(RECORD_STORAGE);
  373. return true;
  374. }
  375. //
  376. bool save_char(
  377. const char *path_name, const char *value)
  378. {
  379. if (!value)
  380. {
  381. return false;
  382. }
  383. // Create the directory for saving settings
  384. char directory_path[256];
  385. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  386. // Create the directory
  387. Storage *storage = furi_record_open(RECORD_STORAGE);
  388. storage_common_mkdir(storage, directory_path);
  389. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data");
  390. storage_common_mkdir(storage, directory_path);
  391. // Open the settings file
  392. File *file = storage_file_alloc(storage);
  393. char file_path[256];
  394. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data/%s.txt", path_name);
  395. // Open the file in write mode
  396. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  397. {
  398. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  399. storage_file_free(file);
  400. furi_record_close(RECORD_STORAGE);
  401. return false;
  402. }
  403. // Write the data to the file
  404. size_t data_size = strlen(value) + 1; // Include null terminator
  405. if (storage_file_write(file, value, data_size) != data_size)
  406. {
  407. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  408. storage_file_close(file);
  409. storage_file_free(file);
  410. furi_record_close(RECORD_STORAGE);
  411. return false;
  412. }
  413. storage_file_close(file);
  414. storage_file_free(file);
  415. furi_record_close(RECORD_STORAGE);
  416. return true;
  417. }
  418. bool load_char(
  419. const char *path_name,
  420. char *value,
  421. size_t value_size)
  422. {
  423. if (!value)
  424. {
  425. return false;
  426. }
  427. Storage *storage = furi_record_open(RECORD_STORAGE);
  428. File *file = storage_file_alloc(storage);
  429. char file_path[256];
  430. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/data/%s.txt", path_name);
  431. // Open the file for reading
  432. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  433. {
  434. storage_file_free(file);
  435. furi_record_close(RECORD_STORAGE);
  436. return false;
  437. }
  438. // Read data into the buffer
  439. size_t read_count = storage_file_read(file, value, value_size);
  440. if (storage_file_get_error(file) != FSE_OK)
  441. {
  442. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  443. storage_file_close(file);
  444. storage_file_free(file);
  445. furi_record_close(RECORD_STORAGE);
  446. return false;
  447. }
  448. // Ensure null-termination
  449. value[read_count - 1] = '\0';
  450. storage_file_close(file);
  451. storage_file_free(file);
  452. furi_record_close(RECORD_STORAGE);
  453. return strlen(value) > 0;
  454. }