dolphin_state.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "dolphin_state.h"
  2. #include <storage/storage.h>
  3. #include <furi.h>
  4. #include <math.h>
  5. #define DOLPHIN_STORE_KEY "/int/dolphin.state"
  6. #define DOLPHIN_STORE_HEADER_MAGIC 0xD0
  7. #define DOLPHIN_STORE_HEADER_VERSION 0x01
  8. #define DOLPHIN_LVL_THRESHOLD 20.0f
  9. typedef struct {
  10. uint8_t magic;
  11. uint8_t version;
  12. uint8_t checksum;
  13. uint8_t flags;
  14. uint32_t timestamp;
  15. } DolphinStoreHeader;
  16. typedef struct {
  17. uint32_t limit_ibutton;
  18. uint32_t limit_nfc;
  19. uint32_t limit_ir;
  20. uint32_t limit_rfid;
  21. uint32_t flags;
  22. uint32_t icounter;
  23. uint32_t butthurt;
  24. uint64_t timestamp;
  25. } DolphinStoreData;
  26. typedef struct {
  27. DolphinStoreHeader header;
  28. DolphinStoreData data;
  29. } DolphinStore;
  30. struct DolphinState {
  31. Storage* fs_api;
  32. DolphinStoreData data;
  33. bool dirty;
  34. };
  35. DolphinState* dolphin_state_alloc() {
  36. DolphinState* dolphin_state = furi_alloc(sizeof(DolphinState));
  37. dolphin_state->fs_api = furi_record_open("storage");
  38. return dolphin_state;
  39. }
  40. void dolphin_state_free(DolphinState* dolphin_state) {
  41. furi_record_close("storage");
  42. free(dolphin_state);
  43. }
  44. bool dolphin_state_save(DolphinState* dolphin_state) {
  45. if(!dolphin_state->dirty) {
  46. return true;
  47. }
  48. FURI_LOG_I("dolphin-state", "State is dirty, saving to \"%s\"", DOLPHIN_STORE_KEY);
  49. DolphinStore store;
  50. // Calculate checksum
  51. uint8_t* source = (uint8_t*)&dolphin_state->data;
  52. uint8_t checksum = 0;
  53. for(size_t i = 0; i < sizeof(DolphinStoreData); i++) {
  54. checksum += source[i];
  55. }
  56. // Set header
  57. store.header.magic = DOLPHIN_STORE_HEADER_MAGIC;
  58. store.header.version = DOLPHIN_STORE_HEADER_VERSION;
  59. store.header.checksum = checksum;
  60. store.header.flags = 0;
  61. store.header.timestamp = 0;
  62. // Set data
  63. store.data = dolphin_state->data;
  64. // Store
  65. File* file = storage_file_alloc(dolphin_state->fs_api);
  66. bool save_result = storage_file_open(file, DOLPHIN_STORE_KEY, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  67. if(save_result) {
  68. uint16_t bytes_count = storage_file_write(file, &store, sizeof(DolphinStore));
  69. if(bytes_count != sizeof(DolphinStore)) {
  70. save_result = false;
  71. }
  72. }
  73. if(!save_result) {
  74. FURI_LOG_E(
  75. "dolphin-state",
  76. "Save failed. Storage returned: %s",
  77. storage_file_get_error_desc(file));
  78. }
  79. storage_file_close(file);
  80. storage_file_free(file);
  81. dolphin_state->dirty = !save_result;
  82. FURI_LOG_I("dolphin-state", "Saved");
  83. return save_result;
  84. }
  85. bool dolphin_state_load(DolphinState* dolphin_state) {
  86. DolphinStore store;
  87. // Read Dolphin State Store
  88. FURI_LOG_I("dolphin-state", "Loading state from \"%s\"", DOLPHIN_STORE_KEY);
  89. File* file = storage_file_alloc(dolphin_state->fs_api);
  90. bool load_result = storage_file_open(file, DOLPHIN_STORE_KEY, FSAM_READ, FSOM_OPEN_EXISTING);
  91. if(!load_result) {
  92. FURI_LOG_E(
  93. "dolphin-state",
  94. "Load failed. Storage returned: %s",
  95. storage_file_get_error_desc(file));
  96. } else {
  97. uint16_t bytes_count = storage_file_read(file, &store, sizeof(DolphinStore));
  98. if(bytes_count != sizeof(DolphinStore)) {
  99. load_result = false;
  100. }
  101. }
  102. if(!load_result) {
  103. FURI_LOG_E("dolphin-state", "DolphinStore size mismatch");
  104. } else {
  105. if(store.header.magic == DOLPHIN_STORE_HEADER_MAGIC &&
  106. store.header.version == DOLPHIN_STORE_HEADER_VERSION) {
  107. FURI_LOG_I(
  108. "dolphin-state",
  109. "Magic(%d) and Version(%d) match",
  110. store.header.magic,
  111. store.header.version);
  112. uint8_t checksum = 0;
  113. const uint8_t* source = (const uint8_t*)&store.data;
  114. for(size_t i = 0; i < sizeof(DolphinStoreData); i++) {
  115. checksum += source[i];
  116. }
  117. if(store.header.checksum == checksum) {
  118. FURI_LOG_I("dolphin-state", "Checksum(%d) match", store.header.checksum);
  119. dolphin_state->data = store.data;
  120. } else {
  121. FURI_LOG_E(
  122. "dolphin-state",
  123. "Checksum(%d != %d) mismatch",
  124. store.header.checksum,
  125. checksum);
  126. load_result = false;
  127. }
  128. } else {
  129. FURI_LOG_E(
  130. "dolphin-state",
  131. "Magic(%d != %d) or Version(%d != %d) mismatch",
  132. store.header.magic,
  133. DOLPHIN_STORE_HEADER_MAGIC,
  134. store.header.version,
  135. DOLPHIN_STORE_HEADER_VERSION);
  136. load_result = false;
  137. }
  138. }
  139. storage_file_close(file);
  140. storage_file_free(file);
  141. dolphin_state->dirty = !load_result;
  142. return load_result;
  143. }
  144. void dolphin_state_clear(DolphinState* dolphin_state) {
  145. memset(&dolphin_state->data, 0, sizeof(DolphinStoreData));
  146. }
  147. uint64_t dolphin_state_timestamp() {
  148. RTC_TimeTypeDef time;
  149. RTC_DateTypeDef date;
  150. struct tm current;
  151. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  152. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  153. current.tm_year = date.Year + 100;
  154. current.tm_mday = date.Date;
  155. current.tm_mon = date.Month - 1;
  156. current.tm_hour = time.Hours;
  157. current.tm_min = time.Minutes;
  158. current.tm_sec = time.Seconds;
  159. return mktime(&current);
  160. }
  161. void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
  162. const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
  163. int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
  164. int32_t butthurt = dolphin_state->data.butthurt;
  165. if(icounter >= 0) {
  166. dolphin_state->data.icounter = icounter;
  167. dolphin_state->data.butthurt = MAX(butthurt - deed_weight->icounter, 0);
  168. dolphin_state->data.timestamp = dolphin_state_timestamp();
  169. }
  170. dolphin_state->dirty = true;
  171. }
  172. void dolphin_state_butthurted(DolphinState* dolphin_state) {
  173. dolphin_state->data.butthurt++;
  174. dolphin_state->data.timestamp = dolphin_state_timestamp();
  175. dolphin_state->dirty = true;
  176. }
  177. uint32_t dolphin_state_get_icounter(DolphinState* dolphin_state) {
  178. return dolphin_state->data.icounter;
  179. }
  180. uint32_t dolphin_state_get_butthurt(DolphinState* dolphin_state) {
  181. return dolphin_state->data.butthurt;
  182. }
  183. uint64_t dolphin_state_get_timestamp(DolphinState* dolphin_state) {
  184. return dolphin_state->data.timestamp;
  185. }
  186. uint32_t dolphin_state_get_level(uint32_t icounter) {
  187. return 0.5f + sqrtf(1.0f + 8.0f * ((float)icounter / DOLPHIN_LVL_THRESHOLD)) / 2.0f;
  188. }
  189. uint32_t dolphin_state_xp_to_levelup(uint32_t icounter, uint32_t level, bool remaining) {
  190. return (DOLPHIN_LVL_THRESHOLD * level * (level + 1) / 2) - (remaining ? icounter : 0);
  191. }