flip_social_storage.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. // Add a newline character after each 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. storage_file_close(file);
  40. storage_file_free(file);
  41. furi_record_close(RECORD_STORAGE);
  42. }
  43. // Function to load the playlist
  44. bool load_playlist(PreSavedPlaylist *playlist)
  45. {
  46. // Ensure playlist is not NULL
  47. if (!playlist)
  48. {
  49. FURI_LOG_E(TAG, "Playlist is NULL");
  50. return false;
  51. }
  52. // Ensure playlist->messages is not NULL and allocate memory for each message
  53. for (size_t i = 0; i < MAX_PRE_SAVED_MESSAGES; ++i)
  54. {
  55. if (!playlist->messages[i]) // Check if memory is already allocated
  56. {
  57. playlist->messages[i] = (char *)malloc(MAX_MESSAGE_LENGTH * sizeof(char));
  58. if (!playlist->messages[i])
  59. {
  60. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  61. return false; // Return false on memory allocation failure
  62. }
  63. }
  64. }
  65. // Open the storage
  66. Storage *storage = furi_record_open(RECORD_STORAGE);
  67. File *file = storage_file_alloc(storage);
  68. if (!storage_file_open(file, PRE_SAVED_MESSAGES_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  69. {
  70. FURI_LOG_E(TAG, "Failed to open pre-saved messages file for reading: %s", PRE_SAVED_MESSAGES_PATH);
  71. storage_file_free(file);
  72. furi_record_close(RECORD_STORAGE);
  73. return false; // Return false if the file does not exist
  74. }
  75. // Initialize the playlist count
  76. playlist->count = 0;
  77. // Read the file byte by byte to simulate reading lines
  78. char ch;
  79. size_t message_pos = 0;
  80. bool message_started = false;
  81. while (storage_file_read(file, &ch, 1) == 1) // Read one character at a time
  82. {
  83. message_started = true;
  84. if (ch == '\n' || message_pos >= (MAX_MESSAGE_LENGTH - 1)) // End of line or message is too long
  85. {
  86. playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the message
  87. playlist->count++; // Move to the next message
  88. message_pos = 0; // Reset for the next message
  89. message_started = false;
  90. // Ensure the playlist count does not exceed the maximum
  91. if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
  92. {
  93. FURI_LOG_W(TAG, "Reached maximum playlist messages");
  94. break;
  95. }
  96. }
  97. else
  98. {
  99. playlist->messages[playlist->count][message_pos++] = ch; // Add character to current message
  100. }
  101. }
  102. // Handle the case where the last message does not end with a newline
  103. if (message_started && message_pos > 0)
  104. {
  105. playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the last message
  106. playlist->count++; // Increment the count for the last message
  107. // Ensure the playlist count does not exceed the maximum
  108. if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
  109. {
  110. FURI_LOG_W(TAG, "Reached maximum playlist messages");
  111. }
  112. }
  113. // Close the file and storage
  114. storage_file_close(file);
  115. storage_file_free(file);
  116. furi_record_close(RECORD_STORAGE);
  117. return true;
  118. }
  119. void save_settings(
  120. const char *ssid,
  121. const char *password,
  122. const char *login_username_logged_out,
  123. const char *login_username_logged_in,
  124. const char *login_password_logged_out,
  125. const char *change_password_logged_in,
  126. const char *change_bio_logged_in,
  127. const char *is_logged_in)
  128. {
  129. // Create the directory for saving settings
  130. char directory_path[128];
  131. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  132. // Create the directory
  133. Storage *storage = furi_record_open(RECORD_STORAGE);
  134. storage_common_mkdir(storage, directory_path);
  135. // Open the settings file
  136. File *file = storage_file_alloc(storage);
  137. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  138. {
  139. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  140. storage_file_free(file);
  141. furi_record_close(RECORD_STORAGE);
  142. return;
  143. }
  144. // Save the ssid length and data
  145. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  146. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  147. storage_file_write(file, ssid, ssid_length) != ssid_length)
  148. {
  149. FURI_LOG_E(TAG, "Failed to write SSID");
  150. }
  151. // Save the password length and data
  152. size_t password_length = strlen(password) + 1; // Include null terminator
  153. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  154. storage_file_write(file, password, password_length) != password_length)
  155. {
  156. FURI_LOG_E(TAG, "Failed to write password");
  157. }
  158. // Save the login_username_logged_out length and data
  159. size_t username_out_length = strlen(login_username_logged_out) + 1; // Include null terminator
  160. if (storage_file_write(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) ||
  161. storage_file_write(file, login_username_logged_out, username_out_length) != username_out_length)
  162. {
  163. FURI_LOG_E(TAG, "Failed to write login_username_logged_out");
  164. }
  165. // Save the login_username_logged_in length and data
  166. size_t username_in_length = strlen(login_username_logged_in) + 1; // Include null terminator
  167. if (storage_file_write(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) ||
  168. storage_file_write(file, login_username_logged_in, username_in_length) != username_in_length)
  169. {
  170. FURI_LOG_E(TAG, "Failed to write login_username_logged_in");
  171. }
  172. // Save the login_password_logged_out length and data
  173. size_t password_out_length = strlen(login_password_logged_out) + 1; // Include null terminator
  174. if (storage_file_write(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) ||
  175. storage_file_write(file, login_password_logged_out, password_out_length) != password_out_length)
  176. {
  177. FURI_LOG_E(TAG, "Failed to write login_password_logged_out");
  178. }
  179. // Save the change_password_logged_in length and data
  180. size_t change_password_length = strlen(change_password_logged_in) + 1; // Include null terminator
  181. if (storage_file_write(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) ||
  182. storage_file_write(file, change_password_logged_in, change_password_length) != change_password_length)
  183. {
  184. FURI_LOG_E(TAG, "Failed to write change_password_logged_in");
  185. }
  186. // Save the is_logged_in length and data
  187. size_t is_logged_in_length = strlen(is_logged_in) + 1; // Include null terminator
  188. if (storage_file_write(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) ||
  189. storage_file_write(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  190. {
  191. FURI_LOG_E(TAG, "Failed to write is_logged_in");
  192. }
  193. // Save the change_bio_logged_in length and data
  194. size_t change_bio_length = strlen(change_bio_logged_in) + 1; // Include null terminator
  195. if (storage_file_write(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) ||
  196. storage_file_write(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  197. {
  198. FURI_LOG_E(TAG, "Failed to write change_bio_logged_in");
  199. }
  200. storage_file_close(file);
  201. storage_file_free(file);
  202. furi_record_close(RECORD_STORAGE);
  203. }
  204. bool load_settings(
  205. char *ssid,
  206. size_t ssid_size,
  207. char *password,
  208. size_t password_size,
  209. char *login_username_logged_out,
  210. size_t username_out_size,
  211. char *login_username_logged_in,
  212. size_t username_in_size,
  213. char *login_password_logged_out,
  214. size_t password_out_size,
  215. char *change_password_logged_in,
  216. size_t change_password_size,
  217. char *change_bio_logged_in,
  218. size_t change_bio_size,
  219. char *is_logged_in,
  220. size_t is_logged_in_size)
  221. {
  222. Storage *storage = furi_record_open(RECORD_STORAGE);
  223. File *file = storage_file_alloc(storage);
  224. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  225. {
  226. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  227. storage_file_free(file);
  228. furi_record_close(RECORD_STORAGE);
  229. return false; // Return false if the file does not exist
  230. }
  231. // Load the ssid
  232. size_t ssid_length;
  233. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  234. storage_file_read(file, ssid, ssid_length) != ssid_length)
  235. {
  236. FURI_LOG_E(TAG, "Failed to read SSID");
  237. storage_file_close(file);
  238. storage_file_free(file);
  239. furi_record_close(RECORD_STORAGE);
  240. return false;
  241. }
  242. else
  243. {
  244. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  245. }
  246. // Load the password
  247. size_t password_length;
  248. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  249. storage_file_read(file, password, password_length) != password_length)
  250. {
  251. FURI_LOG_E(TAG, "Failed to read password");
  252. storage_file_close(file);
  253. storage_file_free(file);
  254. furi_record_close(RECORD_STORAGE);
  255. return false;
  256. }
  257. else
  258. {
  259. password[password_length - 1] = '\0'; // Ensure null-termination
  260. }
  261. // Load the login_username_logged_out
  262. size_t username_out_length;
  263. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  264. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  265. {
  266. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  267. // storage_file_close(file);
  268. // storage_file_free(file);
  269. // furi_record_close(RECORD_STORAGE);
  270. // return false;
  271. }
  272. else
  273. {
  274. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  275. }
  276. // Load the login_username_logged_in
  277. size_t username_in_length;
  278. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  279. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  280. {
  281. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  282. // storage_file_close(file);
  283. // storage_file_free(file);
  284. // furi_record_close(RECORD_STORAGE);
  285. // return false;
  286. }
  287. else
  288. {
  289. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  290. }
  291. // Load the login_password_logged_out
  292. size_t password_out_length;
  293. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  294. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  295. {
  296. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  297. // storage_file_close(file);
  298. // storage_file_free(file);
  299. // furi_record_close(RECORD_STORAGE);
  300. // return false;
  301. }
  302. else
  303. {
  304. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  305. }
  306. // Load the change_password_logged_in
  307. size_t change_password_length;
  308. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  309. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  310. {
  311. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  312. // storage_file_close(file);
  313. // storage_file_free(file);
  314. // furi_record_close(RECORD_STORAGE);
  315. // return false;
  316. }
  317. else
  318. {
  319. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  320. }
  321. // Load the is_logged_in
  322. size_t is_logged_in_length;
  323. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  324. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  325. {
  326. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  327. // storage_file_close(file);
  328. // storage_file_free(file);
  329. // furi_record_close(RECORD_STORAGE);
  330. // return false;
  331. }
  332. else
  333. {
  334. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  335. }
  336. // Load the change_bio_logged_in
  337. size_t change_bio_length;
  338. if (storage_file_read(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) || change_bio_length > change_bio_size ||
  339. storage_file_read(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  340. {
  341. FURI_LOG_E(TAG, "Failed to read change_bio_logged_in");
  342. // storage_file_close(file);
  343. // storage_file_free(file);
  344. // furi_record_close(RECORD_STORAGE);
  345. // return false;
  346. }
  347. else
  348. {
  349. change_bio_logged_in[change_bio_length - 1] = '\0'; // Ensure null-termination
  350. }
  351. storage_file_close(file);
  352. storage_file_free(file);
  353. furi_record_close(RECORD_STORAGE);
  354. return true;
  355. }
  356. bool flip_social_save_post(char *post_id, char *json_feed_data)
  357. {
  358. Storage *storage = furi_record_open(RECORD_STORAGE);
  359. File *file = storage_file_alloc(storage);
  360. char file_path[128];
  361. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed_post_%s.json", post_id);
  362. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  363. {
  364. FURI_LOG_E(TAG, "Failed to open file for writing: %s", file_path);
  365. storage_file_free(file);
  366. furi_record_close(RECORD_STORAGE);
  367. return false;
  368. }
  369. if (storage_file_write(file, json_feed_data, strlen(json_feed_data)) != strlen(json_feed_data))
  370. {
  371. FURI_LOG_E(TAG, "Failed to write feed post data");
  372. storage_file_close(file);
  373. storage_file_free(file);
  374. furi_record_close(RECORD_STORAGE);
  375. return false;
  376. }
  377. storage_file_close(file);
  378. storage_file_free(file);
  379. furi_record_close(RECORD_STORAGE);
  380. return true;
  381. }