flip_social_storage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. // Function to load the playlist
  45. bool load_playlist(PreSavedPlaylist *playlist)
  46. {
  47. // Ensure playlist is not NULL
  48. if (!playlist)
  49. {
  50. FURI_LOG_E(TAG, "Playlist is NULL");
  51. return false;
  52. }
  53. // Ensure playlist->messages is not NULL and allocate memory for each message
  54. for (size_t i = 0; i < MAX_PRE_SAVED_MESSAGES; ++i)
  55. {
  56. if (!playlist->messages[i]) // Check if memory is already allocated
  57. {
  58. playlist->messages[i] = (char *)malloc(MAX_MESSAGE_LENGTH * sizeof(char));
  59. if (!playlist->messages[i])
  60. {
  61. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  62. return false; // Return false on memory allocation failure
  63. }
  64. }
  65. }
  66. // Open the storage
  67. Storage *storage = furi_record_open(RECORD_STORAGE);
  68. File *file = storage_file_alloc(storage);
  69. if (!storage_file_open(file, PRE_SAVED_MESSAGES_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  70. {
  71. FURI_LOG_E(TAG, "Failed to open pre-saved messages file for reading: %s", PRE_SAVED_MESSAGES_PATH);
  72. storage_file_free(file);
  73. furi_record_close(RECORD_STORAGE);
  74. return false; // Return false if the file does not exist
  75. }
  76. // Initialize the playlist count
  77. playlist->count = 0;
  78. // Read the file byte by byte to simulate reading lines
  79. char ch;
  80. size_t message_pos = 0;
  81. bool message_started = false;
  82. while (storage_file_read(file, &ch, 1) == 1) // Read one character at a time
  83. {
  84. message_started = true;
  85. if (ch == '\n' || message_pos >= (MAX_MESSAGE_LENGTH - 1)) // End of line or message is too long
  86. {
  87. playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the message
  88. playlist->count++; // Move to the next message
  89. message_pos = 0; // Reset for the next message
  90. message_started = false;
  91. // Ensure the playlist count does not exceed the maximum
  92. if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
  93. {
  94. FURI_LOG_W(TAG, "Reached maximum playlist messages");
  95. break;
  96. }
  97. }
  98. else
  99. {
  100. playlist->messages[playlist->count][message_pos++] = ch; // Add character to current message
  101. }
  102. }
  103. // Handle the case where the last message does not end with a newline
  104. if (message_started && message_pos > 0)
  105. {
  106. playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the last message
  107. playlist->count++; // Increment the count for the last message
  108. // Ensure the playlist count does not exceed the maximum
  109. if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
  110. {
  111. FURI_LOG_W(TAG, "Reached maximum playlist messages");
  112. }
  113. }
  114. // Close the file and storage
  115. storage_file_close(file);
  116. storage_file_free(file);
  117. furi_record_close(RECORD_STORAGE);
  118. return true;
  119. }
  120. void save_settings(
  121. const char *ssid,
  122. const char *password,
  123. const char *login_username_logged_out,
  124. const char *login_username_logged_in,
  125. const char *login_password_logged_out,
  126. const char *change_password_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. storage_file_close(file);
  194. storage_file_free(file);
  195. furi_record_close(RECORD_STORAGE);
  196. }
  197. bool load_settings(
  198. char *ssid,
  199. size_t ssid_size,
  200. char *password,
  201. size_t password_size,
  202. char *login_username_logged_out,
  203. size_t username_out_size,
  204. char *login_username_logged_in,
  205. size_t username_in_size,
  206. char *login_password_logged_out,
  207. size_t password_out_size,
  208. char *change_password_logged_in,
  209. size_t change_password_size,
  210. char *is_logged_in,
  211. size_t is_logged_in_size)
  212. {
  213. Storage *storage = furi_record_open(RECORD_STORAGE);
  214. File *file = storage_file_alloc(storage);
  215. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  216. {
  217. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  218. storage_file_free(file);
  219. furi_record_close(RECORD_STORAGE);
  220. return false; // Return false if the file does not exist
  221. }
  222. // Load the ssid
  223. size_t ssid_length;
  224. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  225. storage_file_read(file, ssid, ssid_length) != ssid_length)
  226. {
  227. FURI_LOG_E(TAG, "Failed to read SSID");
  228. storage_file_close(file);
  229. storage_file_free(file);
  230. furi_record_close(RECORD_STORAGE);
  231. return false;
  232. }
  233. else
  234. {
  235. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  236. }
  237. // Load the password
  238. size_t password_length;
  239. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  240. storage_file_read(file, password, password_length) != password_length)
  241. {
  242. FURI_LOG_E(TAG, "Failed to read password");
  243. storage_file_close(file);
  244. storage_file_free(file);
  245. furi_record_close(RECORD_STORAGE);
  246. return false;
  247. }
  248. else
  249. {
  250. password[password_length - 1] = '\0'; // Ensure null-termination
  251. }
  252. // Load the login_username_logged_out
  253. size_t username_out_length;
  254. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  255. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  256. {
  257. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  258. // storage_file_close(file);
  259. // storage_file_free(file);
  260. // furi_record_close(RECORD_STORAGE);
  261. // return false;
  262. }
  263. else
  264. {
  265. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  266. }
  267. // Load the login_username_logged_in
  268. size_t username_in_length;
  269. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  270. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  271. {
  272. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  273. // storage_file_close(file);
  274. // storage_file_free(file);
  275. // furi_record_close(RECORD_STORAGE);
  276. // return false;
  277. }
  278. else
  279. {
  280. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  281. }
  282. // Load the login_password_logged_out
  283. size_t password_out_length;
  284. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  285. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  286. {
  287. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  288. // storage_file_close(file);
  289. // storage_file_free(file);
  290. // furi_record_close(RECORD_STORAGE);
  291. // return false;
  292. }
  293. else
  294. {
  295. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  296. }
  297. // Load the change_password_logged_in
  298. size_t change_password_length;
  299. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  300. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  301. {
  302. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  303. // storage_file_close(file);
  304. // storage_file_free(file);
  305. // furi_record_close(RECORD_STORAGE);
  306. // return false;
  307. }
  308. else
  309. {
  310. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  311. }
  312. // Load the is_logged_in
  313. size_t is_logged_in_length;
  314. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  315. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  316. {
  317. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  318. // storage_file_close(file);
  319. // storage_file_free(file);
  320. // furi_record_close(RECORD_STORAGE);
  321. // return false;
  322. }
  323. else
  324. {
  325. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  326. }
  327. storage_file_close(file);
  328. storage_file_free(file);
  329. furi_record_close(RECORD_STORAGE);
  330. return true;
  331. }
  332. bool flip_social_save_post(char *post_id, char *json_feed_data)
  333. {
  334. Storage *storage = furi_record_open(RECORD_STORAGE);
  335. File *file = storage_file_alloc(storage);
  336. char file_path[128];
  337. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed_post_%s.json", post_id);
  338. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  339. {
  340. FURI_LOG_E(TAG, "Failed to open file for writing: %s", file_path);
  341. storage_file_free(file);
  342. furi_record_close(RECORD_STORAGE);
  343. return false;
  344. }
  345. if (storage_file_write(file, json_feed_data, strlen(json_feed_data)) != strlen(json_feed_data))
  346. {
  347. FURI_LOG_E(TAG, "Failed to write feed post data");
  348. storage_file_close(file);
  349. storage_file_free(file);
  350. furi_record_close(RECORD_STORAGE);
  351. return false;
  352. }
  353. storage_file_close(file);
  354. storage_file_free(file);
  355. furi_record_close(RECORD_STORAGE);
  356. return true;
  357. }