flip_social_storage.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // flip_social_storage.h
  2. #ifndef FLIP_SOCIAL_STORAGE_H
  3. #define FLIP_SOCIAL_STORAGE_H
  4. #include <furi.h>
  5. #include <storage/storage.h>
  6. #define SETTINGS_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/settings.bin"
  7. #define PRE_SAVED_MESSAGES_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/pre_saved_messages.txt"
  8. // Function to save the playlist
  9. void save_playlist(const PreSavedPlaylist *playlist)
  10. {
  11. if (!playlist)
  12. {
  13. FURI_LOG_E(TAG, "Playlist is NULL");
  14. return;
  15. }
  16. // Create the directory for saving settings
  17. char directory_path[256];
  18. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  19. // Create the directory
  20. Storage *storage = furi_record_open(RECORD_STORAGE);
  21. storage_common_mkdir(storage, directory_path);
  22. // Open the settings file
  23. File *file = storage_file_alloc(storage);
  24. if (!storage_file_open(file, PRE_SAVED_MESSAGES_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  25. {
  26. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", PRE_SAVED_MESSAGES_PATH);
  27. storage_file_free(file);
  28. furi_record_close(RECORD_STORAGE);
  29. return;
  30. }
  31. // Write each playlist message on a separate line
  32. for (size_t i = 0; i < playlist->count; ++i)
  33. {
  34. // Add a newline character after each message
  35. if (storage_file_write(file, playlist->messages[i], strlen(playlist->messages[i])) != strlen(playlist->messages[i]))
  36. {
  37. FURI_LOG_E(TAG, "Failed to write playlist message %zu", i);
  38. }
  39. // Write a newline after each message
  40. if (storage_file_write(file, "\n", 1) != 1)
  41. {
  42. FURI_LOG_E(TAG, "Failed to write newline after message %zu", i);
  43. }
  44. }
  45. FURI_LOG_I(TAG, "Playlist saved: playlist_count=%zu", playlist->count);
  46. storage_file_close(file);
  47. storage_file_free(file);
  48. furi_record_close(RECORD_STORAGE);
  49. }
  50. // Function to load the playlist
  51. // Function to load the playlist
  52. bool load_playlist(PreSavedPlaylist *playlist)
  53. {
  54. // Ensure playlist is not NULL
  55. if (!playlist)
  56. {
  57. FURI_LOG_E(TAG, "Playlist is NULL");
  58. return false;
  59. }
  60. // Ensure playlist->messages is not NULL and allocate memory for each message
  61. for (size_t i = 0; i < MAX_PRE_SAVED_MESSAGES; ++i)
  62. {
  63. if (!playlist->messages[i]) // Check if memory is already allocated
  64. {
  65. playlist->messages[i] = (char *)malloc(MAX_MESSAGE_LENGTH * sizeof(char));
  66. if (!playlist->messages[i])
  67. {
  68. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  69. return false; // Return false on memory allocation failure
  70. }
  71. }
  72. }
  73. // Open the storage
  74. Storage *storage = furi_record_open(RECORD_STORAGE);
  75. File *file = storage_file_alloc(storage);
  76. if (!storage_file_open(file, PRE_SAVED_MESSAGES_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  77. {
  78. FURI_LOG_E(TAG, "Failed to open pre-saved messages file for reading: %s", PRE_SAVED_MESSAGES_PATH);
  79. storage_file_free(file);
  80. furi_record_close(RECORD_STORAGE);
  81. return false; // Return false if the file does not exist
  82. }
  83. // Initialize the playlist count
  84. playlist->count = 0;
  85. // Read the file byte by byte to simulate reading lines
  86. char ch;
  87. size_t message_pos = 0;
  88. bool message_started = false;
  89. while (storage_file_read(file, &ch, 1) == 1) // Read one character at a time
  90. {
  91. message_started = true;
  92. if (ch == '\n' || message_pos >= (MAX_MESSAGE_LENGTH - 1)) // End of line or message is too long
  93. {
  94. playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the message
  95. playlist->count++; // Move to the next message
  96. message_pos = 0; // Reset for the next message
  97. message_started = false;
  98. // Ensure the playlist count does not exceed the maximum
  99. if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
  100. {
  101. FURI_LOG_W(TAG, "Reached maximum playlist messages");
  102. break;
  103. }
  104. }
  105. else
  106. {
  107. playlist->messages[playlist->count][message_pos++] = ch; // Add character to current message
  108. }
  109. }
  110. // Handle the case where the last message does not end with a newline
  111. if (message_started && message_pos > 0)
  112. {
  113. playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the last message
  114. playlist->count++; // Increment the count for the last message
  115. // Ensure the playlist count does not exceed the maximum
  116. if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
  117. {
  118. FURI_LOG_W(TAG, "Reached maximum playlist messages");
  119. }
  120. }
  121. FURI_LOG_I(TAG, "Playlist loaded: playlist_count=%zu", playlist->count);
  122. // Close the file and storage
  123. storage_file_close(file);
  124. storage_file_free(file);
  125. furi_record_close(RECORD_STORAGE);
  126. return true;
  127. }
  128. static void save_settings(
  129. const char *ssid,
  130. const char *password,
  131. const char *login_username_logged_out,
  132. const char *login_username_logged_in,
  133. const char *login_password_logged_out,
  134. const char *change_password_logged_in,
  135. const char *is_logged_in)
  136. {
  137. // Create the directory for saving settings
  138. char directory_path[256];
  139. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  140. // Create the directory
  141. Storage *storage = furi_record_open(RECORD_STORAGE);
  142. storage_common_mkdir(storage, directory_path);
  143. // Open the settings file
  144. File *file = storage_file_alloc(storage);
  145. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  146. {
  147. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  148. storage_file_free(file);
  149. furi_record_close(RECORD_STORAGE);
  150. return;
  151. }
  152. // Save the ssid length and data
  153. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  154. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  155. storage_file_write(file, ssid, ssid_length) != ssid_length)
  156. {
  157. FURI_LOG_E(TAG, "Failed to write SSID");
  158. }
  159. // Save the password length and data
  160. size_t password_length = strlen(password) + 1; // Include null terminator
  161. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  162. storage_file_write(file, password, password_length) != password_length)
  163. {
  164. FURI_LOG_E(TAG, "Failed to write password");
  165. }
  166. // Save the login_username_logged_out length and data
  167. size_t username_out_length = strlen(login_username_logged_out) + 1; // Include null terminator
  168. if (storage_file_write(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) ||
  169. storage_file_write(file, login_username_logged_out, username_out_length) != username_out_length)
  170. {
  171. FURI_LOG_E(TAG, "Failed to write login_username_logged_out");
  172. }
  173. // Save the login_username_logged_in length and data
  174. size_t username_in_length = strlen(login_username_logged_in) + 1; // Include null terminator
  175. if (storage_file_write(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) ||
  176. storage_file_write(file, login_username_logged_in, username_in_length) != username_in_length)
  177. {
  178. FURI_LOG_E(TAG, "Failed to write login_username_logged_in");
  179. }
  180. // Save the login_password_logged_out length and data
  181. size_t password_out_length = strlen(login_password_logged_out) + 1; // Include null terminator
  182. if (storage_file_write(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) ||
  183. storage_file_write(file, login_password_logged_out, password_out_length) != password_out_length)
  184. {
  185. FURI_LOG_E(TAG, "Failed to write login_password_logged_out");
  186. }
  187. // Save the change_password_logged_in length and data
  188. size_t change_password_length = strlen(change_password_logged_in) + 1; // Include null terminator
  189. if (storage_file_write(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) ||
  190. storage_file_write(file, change_password_logged_in, change_password_length) != change_password_length)
  191. {
  192. FURI_LOG_E(TAG, "Failed to write change_password_logged_in");
  193. }
  194. // Save the is_logged_in length and data
  195. size_t is_logged_in_length = strlen(is_logged_in) + 1; // Include null terminator
  196. if (storage_file_write(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) ||
  197. storage_file_write(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  198. {
  199. FURI_LOG_E(TAG, "Failed to write is_logged_in");
  200. }
  201. storage_file_close(file);
  202. storage_file_free(file);
  203. furi_record_close(RECORD_STORAGE);
  204. }
  205. static bool load_settings(
  206. char *ssid,
  207. size_t ssid_size,
  208. char *password,
  209. size_t password_size,
  210. char *login_username_logged_out,
  211. size_t username_out_size,
  212. char *login_username_logged_in,
  213. size_t username_in_size,
  214. char *login_password_logged_out,
  215. size_t password_out_size,
  216. char *change_password_logged_in,
  217. size_t change_password_size,
  218. char *is_logged_in,
  219. size_t is_logged_in_size)
  220. {
  221. Storage *storage = furi_record_open(RECORD_STORAGE);
  222. File *file = storage_file_alloc(storage);
  223. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  224. {
  225. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  226. storage_file_free(file);
  227. furi_record_close(RECORD_STORAGE);
  228. return false; // Return false if the file does not exist
  229. }
  230. // Load the ssid
  231. size_t ssid_length;
  232. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  233. storage_file_read(file, ssid, ssid_length) != ssid_length)
  234. {
  235. FURI_LOG_E(TAG, "Failed to read SSID");
  236. storage_file_close(file);
  237. storage_file_free(file);
  238. furi_record_close(RECORD_STORAGE);
  239. return false;
  240. }
  241. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  242. // Load the password
  243. size_t password_length;
  244. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  245. storage_file_read(file, password, password_length) != password_length)
  246. {
  247. FURI_LOG_E(TAG, "Failed to read password");
  248. storage_file_close(file);
  249. storage_file_free(file);
  250. furi_record_close(RECORD_STORAGE);
  251. return false;
  252. }
  253. password[password_length - 1] = '\0'; // Ensure null-termination
  254. // Load the login_username_logged_out
  255. size_t username_out_length;
  256. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  257. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  258. {
  259. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  260. storage_file_close(file);
  261. storage_file_free(file);
  262. furi_record_close(RECORD_STORAGE);
  263. return false;
  264. }
  265. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  266. // Load the login_username_logged_in
  267. size_t username_in_length;
  268. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  269. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  270. {
  271. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  272. storage_file_close(file);
  273. storage_file_free(file);
  274. furi_record_close(RECORD_STORAGE);
  275. return false;
  276. }
  277. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  278. // Load the login_password_logged_out
  279. size_t password_out_length;
  280. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  281. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  282. {
  283. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  284. storage_file_close(file);
  285. storage_file_free(file);
  286. furi_record_close(RECORD_STORAGE);
  287. return false;
  288. }
  289. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  290. // Load the change_password_logged_in
  291. size_t change_password_length;
  292. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  293. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  294. {
  295. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  296. storage_file_close(file);
  297. storage_file_free(file);
  298. furi_record_close(RECORD_STORAGE);
  299. return false;
  300. }
  301. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  302. // Load the is_logged_in
  303. size_t is_logged_in_length;
  304. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  305. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  306. {
  307. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  308. storage_file_close(file);
  309. storage_file_free(file);
  310. furi_record_close(RECORD_STORAGE);
  311. return false;
  312. }
  313. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  314. storage_file_close(file);
  315. storage_file_free(file);
  316. furi_record_close(RECORD_STORAGE);
  317. return true;
  318. }
  319. #endif // FLIP_SOCIAL_STORAGE_H