bt_keys_storage.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "bt_keys_storage.h"
  2. #include <furi.h>
  3. #include <furi_hal_bt.h>
  4. #include <lib/toolbox/saved_struct.h>
  5. #include <storage/storage.h>
  6. #define BT_KEYS_STORAGE_VERSION (0)
  7. #define BT_KEYS_STORAGE_MAGIC (0x18)
  8. #define TAG "BtKeyStorage"
  9. struct BtKeysStorage {
  10. uint8_t* nvm_sram_buff;
  11. uint16_t nvm_sram_buff_size;
  12. FuriString* file_path;
  13. };
  14. bool bt_keys_storage_delete(BtKeysStorage* instance) {
  15. furi_assert(instance);
  16. bool delete_succeed = false;
  17. bool bt_is_active = furi_hal_bt_is_active();
  18. furi_hal_bt_stop_advertising();
  19. delete_succeed = furi_hal_bt_clear_white_list();
  20. if(bt_is_active) {
  21. furi_hal_bt_start_advertising();
  22. }
  23. return delete_succeed;
  24. }
  25. BtKeysStorage* bt_keys_storage_alloc(const char* keys_storage_path) {
  26. furi_assert(keys_storage_path);
  27. BtKeysStorage* instance = malloc(sizeof(BtKeysStorage));
  28. // Set default nvm ram parameters
  29. furi_hal_bt_get_key_storage_buff(&instance->nvm_sram_buff, &instance->nvm_sram_buff_size);
  30. // Set key storage file
  31. instance->file_path = furi_string_alloc();
  32. furi_string_set_str(instance->file_path, keys_storage_path);
  33. return instance;
  34. }
  35. void bt_keys_storage_free(BtKeysStorage* instance) {
  36. furi_assert(instance);
  37. furi_string_free(instance->file_path);
  38. free(instance);
  39. }
  40. void bt_keys_storage_set_file_path(BtKeysStorage* instance, const char* path) {
  41. furi_assert(instance);
  42. furi_assert(path);
  43. furi_string_set_str(instance->file_path, path);
  44. }
  45. void bt_keys_storage_set_ram_params(BtKeysStorage* instance, uint8_t* buff, uint16_t size) {
  46. furi_assert(instance);
  47. furi_assert(buff);
  48. instance->nvm_sram_buff = buff;
  49. instance->nvm_sram_buff_size = size;
  50. }
  51. bool bt_keys_storage_load(BtKeysStorage* instance) {
  52. furi_assert(instance);
  53. bool loaded = false;
  54. do {
  55. // Get payload size
  56. size_t payload_size = 0;
  57. if(!saved_struct_get_payload_size(
  58. furi_string_get_cstr(instance->file_path),
  59. BT_KEYS_STORAGE_MAGIC,
  60. BT_KEYS_STORAGE_VERSION,
  61. &payload_size)) {
  62. FURI_LOG_E(TAG, "Failed to read payload size");
  63. break;
  64. }
  65. if(payload_size > instance->nvm_sram_buff_size) {
  66. FURI_LOG_E(TAG, "Saved data doesn't fit ram buffer");
  67. break;
  68. }
  69. // Load saved data to ram
  70. furi_hal_bt_nvm_sram_sem_acquire();
  71. bool data_loaded = saved_struct_load(
  72. furi_string_get_cstr(instance->file_path),
  73. instance->nvm_sram_buff,
  74. payload_size,
  75. BT_KEYS_STORAGE_MAGIC,
  76. BT_KEYS_STORAGE_VERSION);
  77. furi_hal_bt_nvm_sram_sem_release();
  78. if(!data_loaded) {
  79. FURI_LOG_E(TAG, "Failed to load struct");
  80. break;
  81. }
  82. loaded = true;
  83. } while(false);
  84. return loaded;
  85. }
  86. bool bt_keys_storage_update(BtKeysStorage* instance, uint8_t* start_addr, uint32_t size) {
  87. furi_assert(instance);
  88. furi_assert(start_addr);
  89. bool updated = false;
  90. FURI_LOG_I(
  91. TAG,
  92. "Base address: %p. Start update address: %p. Size changed: %ld",
  93. (void*)instance->nvm_sram_buff,
  94. start_addr,
  95. size);
  96. do {
  97. size_t new_size = start_addr - instance->nvm_sram_buff + size;
  98. if(new_size > instance->nvm_sram_buff_size) {
  99. FURI_LOG_E(TAG, "NVM RAM buffer overflow");
  100. break;
  101. }
  102. furi_hal_bt_nvm_sram_sem_acquire();
  103. bool data_updated = saved_struct_save(
  104. furi_string_get_cstr(instance->file_path),
  105. instance->nvm_sram_buff,
  106. new_size,
  107. BT_KEYS_STORAGE_MAGIC,
  108. BT_KEYS_STORAGE_VERSION);
  109. furi_hal_bt_nvm_sram_sem_release();
  110. if(!data_updated) {
  111. FURI_LOG_E(TAG, "Failed to update key storage");
  112. break;
  113. }
  114. updated = true;
  115. } while(false);
  116. return updated;
  117. }