flip_social_storage.h 14 KB

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