ble_nvs.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Created by kindring on 2025/12/25.
  3. //
  4. #include "ble_nvs.h"
  5. #include "esp_err.h"
  6. #include "esp_log.h"
  7. #include "nvs_flash.h"
  8. static const char *TAG = "BLE_NVS";
  9. #define NVS_STORAGE_NAMESPACE "bt_device"
  10. #define NVS_KEY_BT_ADDR "last_addr"
  11. #define NVS_KEY_BT_NAME "last_name"
  12. #define NVS_KEY_IS_PAIRED "is_paired"
  13. #define NVS_KEY_CONNECT_COUNT "connect_count"
  14. #define NVS_KEY_LAST_CONNECT_TIME "last_connect_time"
  15. #define NVS_KEY_AUTO_RECONNECT "auto_reconnect"
  16. static bool s_auto_reconnect = true;
  17. static bool s_reconnect_active = false;
  18. esp_err_t ble_nvs_init()
  19. {
  20. esp_err_t ret = nvs_flash_init();
  21. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  22. ESP_LOGW(TAG, "NVS partition needs to be erased");
  23. ESP_ERROR_CHECK(nvs_flash_erase());
  24. ret = nvs_flash_init();
  25. }
  26. if (ret != ESP_OK) {
  27. ESP_LOGE(TAG, "Failed to initialize NVS: %s", esp_err_to_name(ret));
  28. return ret;
  29. }
  30. ESP_LOGI(TAG, "Bluetooth Device Manager initialized");
  31. return ESP_OK;
  32. }
  33. esp_err_t bt_device_save_info(const bt_device_info_t *device_info)
  34. {
  35. if (!device_info) {
  36. return ESP_ERR_INVALID_ARG;
  37. }
  38. nvs_handle_t handle;
  39. esp_err_t ret;
  40. ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READWRITE, &handle);
  41. if (ret != ESP_OK) {
  42. ESP_LOGE(TAG, "Error opening NVS handle: %s", esp_err_to_name(ret));
  43. return ret;
  44. }
  45. // 保存蓝牙设备地址
  46. ret = nvs_set_blob(handle, NVS_KEY_BT_ADDR, device_info->bda, 6);
  47. if (ret != ESP_OK)
  48. {
  49. ESP_LOGE(TAG, "Failed to save Bluetooth address: %s", esp_err_to_name(ret));
  50. nvs_close(handle);
  51. return ret;
  52. }
  53. // 保存设备名称
  54. ret = nvs_set_str(handle, NVS_KEY_BT_NAME, device_info->device_name);
  55. if (ret != ESP_OK) {
  56. ESP_LOGE(TAG, "Failed to save device name: %s", esp_err_to_name(ret));
  57. nvs_close(handle);
  58. return ret;
  59. }
  60. // 保存配对状态
  61. ret = nvs_set_u8(handle, NVS_KEY_IS_PAIRED, device_info->is_paired ? 1 : 0);
  62. if (ret != ESP_OK) {
  63. ESP_LOGE(TAG, "Failed to save pairing status: %s", esp_err_to_name(ret));
  64. nvs_close(handle);
  65. return ret;
  66. }
  67. // 保存连接次数
  68. ret = nvs_set_u32(handle, NVS_KEY_CONNECT_COUNT, device_info->connect_count);
  69. if (ret != ESP_OK) {
  70. ESP_LOGE(TAG, "Failed to save connect count: %s", esp_err_to_name(ret));
  71. nvs_close(handle);
  72. return ret;
  73. }
  74. // 保存最后连接时间
  75. ret = nvs_set_u64(handle, NVS_KEY_LAST_CONNECT_TIME, device_info->last_connect_time);
  76. if (ret != ESP_OK) {
  77. ESP_LOGE(TAG, "Failed to save last connect time: %s", esp_err_to_name(ret));
  78. nvs_close(handle);
  79. return ret;
  80. }
  81. // 提交更改
  82. ret = nvs_commit(handle);
  83. if (ret != ESP_OK) {
  84. ESP_LOGE(TAG, "Failed to commit NVS changes: %s", esp_err_to_name(ret));
  85. }
  86. nvs_close(handle);
  87. ESP_LOGI(TAG, "Bluetooth device info saved successfully");
  88. return ret;
  89. }
  90. esp_err_t bt_device_load_info(bt_device_info_t *device_info)
  91. {
  92. if (!device_info) {
  93. return ESP_ERR_INVALID_ARG;
  94. }
  95. nvs_handle_t handle;
  96. esp_err_t ret;
  97. ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READONLY, &handle);
  98. if (ret != ESP_OK) {
  99. ESP_LOGE(TAG, "Error opening NVS handle: %s", esp_err_to_name(ret));
  100. return ret;
  101. }
  102. // 读取蓝牙设备地址
  103. size_t addr_len = 6;
  104. ret = nvs_get_blob(handle, NVS_KEY_BT_ADDR, device_info->bda, &addr_len);
  105. if (ret != ESP_OK) {
  106. ESP_LOGE(TAG, "Failed to load Bluetooth address: %s", esp_err_to_name(ret));
  107. nvs_close(handle);
  108. return ret;
  109. }
  110. // 读取设备名称
  111. size_t name_len = sizeof(device_info->device_name);
  112. ret = nvs_get_str(handle, NVS_KEY_BT_NAME, device_info->device_name, &name_len);
  113. if (ret != ESP_OK) {
  114. ESP_LOGE(TAG, "Failed to load device name: %s", esp_err_to_name(ret));
  115. nvs_close(handle);
  116. return ret;
  117. }
  118. // 读取配对状态
  119. uint8_t is_paired;
  120. ret = nvs_get_u8(handle, NVS_KEY_IS_PAIRED, &is_paired);
  121. if (ret == ESP_OK) {
  122. device_info->is_paired = (is_paired != 0);
  123. } else {
  124. device_info->is_paired = false;
  125. }
  126. // 读取连接次数
  127. ret = nvs_get_u32(handle, NVS_KEY_CONNECT_COUNT, &device_info->connect_count);
  128. if (ret != ESP_OK) {
  129. device_info->connect_count = 0;
  130. }
  131. // 读取最后连接时间
  132. ret = nvs_get_u64(handle, NVS_KEY_LAST_CONNECT_TIME, &device_info->last_connect_time);
  133. if (ret != ESP_OK) {
  134. device_info->last_connect_time = 0;
  135. }
  136. nvs_close(handle);
  137. ESP_LOGI(TAG, "Bluetooth device info loaded successfully");
  138. return ESP_OK;
  139. }
  140. esp_err_t bt_device_clear_info(void)
  141. {
  142. nvs_handle_t handle;
  143. esp_err_t ret;
  144. ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READWRITE, &handle);
  145. if (ret != ESP_OK) {
  146. ESP_LOGE(TAG, "Error opening NVS handle: %s", esp_err_to_name(ret));
  147. return ret;
  148. }
  149. // 清除所有键值
  150. nvs_erase_all(handle);
  151. ret = nvs_commit(handle);
  152. nvs_close(handle);
  153. ESP_LOGI(TAG, "Bluetooth device info cleared");
  154. return ret;
  155. }
  156. void bt_device_set_auto_reconnect(bool enable)
  157. {
  158. s_auto_reconnect = enable;
  159. nvs_handle_t handle;
  160. if (nvs_open(NVS_STORAGE_NAMESPACE, NVS_READWRITE, &handle) == ESP_OK) {
  161. nvs_set_u8(handle, NVS_KEY_AUTO_RECONNECT, enable ? 1 : 0);
  162. nvs_commit(handle);
  163. nvs_close(handle);
  164. }
  165. }
  166. bool bt_device_get_auto_reconnect(void)
  167. {
  168. uint8_t auto_reconnect;
  169. nvs_handle_t handle;
  170. esp_err_t ret;
  171. ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READONLY, &handle);
  172. if (ret == ESP_OK) {
  173. ret = nvs_get_u8(handle, NVS_KEY_AUTO_RECONNECT, &auto_reconnect);
  174. nvs_close(handle);
  175. if (ret == ESP_OK) {
  176. s_auto_reconnect = (auto_reconnect != 0);
  177. }
  178. }
  179. return s_auto_reconnect;
  180. }