flip_social_storage.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // Close the file and storage
  122. storage_file_close(file);
  123. storage_file_free(file);
  124. furi_record_close(RECORD_STORAGE);
  125. return true;
  126. }
  127. static void save_settings(
  128. const char *ssid,
  129. const char *password,
  130. const char *login_username_logged_out,
  131. const char *login_username_logged_in,
  132. const char *login_password_logged_out,
  133. const char *change_password_logged_in,
  134. const char *is_logged_in)
  135. {
  136. // Create the directory for saving settings
  137. char directory_path[256];
  138. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
  139. // Create the directory
  140. Storage *storage = furi_record_open(RECORD_STORAGE);
  141. storage_common_mkdir(storage, directory_path);
  142. // Open the settings file
  143. File *file = storage_file_alloc(storage);
  144. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  145. {
  146. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  147. storage_file_free(file);
  148. furi_record_close(RECORD_STORAGE);
  149. return;
  150. }
  151. // Save the ssid length and data
  152. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  153. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  154. storage_file_write(file, ssid, ssid_length) != ssid_length)
  155. {
  156. FURI_LOG_E(TAG, "Failed to write SSID");
  157. }
  158. // Save the password length and data
  159. size_t password_length = strlen(password) + 1; // Include null terminator
  160. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  161. storage_file_write(file, password, password_length) != password_length)
  162. {
  163. FURI_LOG_E(TAG, "Failed to write password");
  164. }
  165. // Save the login_username_logged_out length and data
  166. size_t username_out_length = strlen(login_username_logged_out) + 1; // Include null terminator
  167. if (storage_file_write(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) ||
  168. storage_file_write(file, login_username_logged_out, username_out_length) != username_out_length)
  169. {
  170. FURI_LOG_E(TAG, "Failed to write login_username_logged_out");
  171. }
  172. // Save the login_username_logged_in length and data
  173. size_t username_in_length = strlen(login_username_logged_in) + 1; // Include null terminator
  174. if (storage_file_write(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) ||
  175. storage_file_write(file, login_username_logged_in, username_in_length) != username_in_length)
  176. {
  177. FURI_LOG_E(TAG, "Failed to write login_username_logged_in");
  178. }
  179. // Save the login_password_logged_out length and data
  180. size_t password_out_length = strlen(login_password_logged_out) + 1; // Include null terminator
  181. if (storage_file_write(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) ||
  182. storage_file_write(file, login_password_logged_out, password_out_length) != password_out_length)
  183. {
  184. FURI_LOG_E(TAG, "Failed to write login_password_logged_out");
  185. }
  186. // Save the change_password_logged_in length and data
  187. size_t change_password_length = strlen(change_password_logged_in) + 1; // Include null terminator
  188. if (storage_file_write(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) ||
  189. storage_file_write(file, change_password_logged_in, change_password_length) != change_password_length)
  190. {
  191. FURI_LOG_E(TAG, "Failed to write change_password_logged_in");
  192. }
  193. // Save the is_logged_in length and data
  194. size_t is_logged_in_length = strlen(is_logged_in) + 1; // Include null terminator
  195. if (storage_file_write(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) ||
  196. storage_file_write(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  197. {
  198. FURI_LOG_E(TAG, "Failed to write is_logged_in");
  199. }
  200. storage_file_close(file);
  201. storage_file_free(file);
  202. furi_record_close(RECORD_STORAGE);
  203. }
  204. static 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 *is_logged_in,
  218. size_t is_logged_in_size)
  219. {
  220. Storage *storage = furi_record_open(RECORD_STORAGE);
  221. File *file = storage_file_alloc(storage);
  222. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  223. {
  224. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  225. storage_file_free(file);
  226. furi_record_close(RECORD_STORAGE);
  227. return false; // Return false if the file does not exist
  228. }
  229. // Load the ssid
  230. size_t ssid_length;
  231. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  232. storage_file_read(file, ssid, ssid_length) != ssid_length)
  233. {
  234. FURI_LOG_E(TAG, "Failed to read SSID");
  235. storage_file_close(file);
  236. storage_file_free(file);
  237. furi_record_close(RECORD_STORAGE);
  238. return false;
  239. }
  240. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  241. // Load the password
  242. size_t password_length;
  243. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  244. storage_file_read(file, password, password_length) != password_length)
  245. {
  246. FURI_LOG_E(TAG, "Failed to read password");
  247. storage_file_close(file);
  248. storage_file_free(file);
  249. furi_record_close(RECORD_STORAGE);
  250. return false;
  251. }
  252. password[password_length - 1] = '\0'; // Ensure null-termination
  253. // Load the login_username_logged_out
  254. size_t username_out_length;
  255. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  256. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  257. {
  258. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  259. storage_file_close(file);
  260. storage_file_free(file);
  261. furi_record_close(RECORD_STORAGE);
  262. return false;
  263. }
  264. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  265. // Load the login_username_logged_in
  266. size_t username_in_length;
  267. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  268. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  269. {
  270. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  271. storage_file_close(file);
  272. storage_file_free(file);
  273. furi_record_close(RECORD_STORAGE);
  274. return false;
  275. }
  276. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  277. // Load the login_password_logged_out
  278. size_t password_out_length;
  279. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  280. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  281. {
  282. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  283. storage_file_close(file);
  284. storage_file_free(file);
  285. furi_record_close(RECORD_STORAGE);
  286. return false;
  287. }
  288. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  289. // Load the change_password_logged_in
  290. size_t change_password_length;
  291. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  292. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  293. {
  294. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  295. storage_file_close(file);
  296. storage_file_free(file);
  297. furi_record_close(RECORD_STORAGE);
  298. return false;
  299. }
  300. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  301. // Load the is_logged_in
  302. size_t is_logged_in_length;
  303. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  304. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  305. {
  306. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  307. storage_file_close(file);
  308. storage_file_free(file);
  309. furi_record_close(RECORD_STORAGE);
  310. return false;
  311. }
  312. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  313. storage_file_close(file);
  314. storage_file_free(file);
  315. furi_record_close(RECORD_STORAGE);
  316. return true;
  317. }
  318. #endif // FLIP_SOCIAL_STORAGE_H