flip_social_storage.c 13 KB

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