flip_social_storage.h 14 KB

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