settings-esp32.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * esp32 / esp-idf NV settings shim
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <private-lib-core.h>
  25. #include <nvs_flash.h>
  26. int
  27. lws_settings_plat_get(lws_settings_instance_t *si, const char *name,
  28. uint8_t *dest, size_t *max_actual)
  29. {
  30. int n;
  31. n = nvs_flash_init_partition((const char *)si->opaque_plat);
  32. lwsl_notice("%s: init partition %d\n", __func__, n);
  33. if (n == ESP_ERR_NOT_FOUND)
  34. return 1;
  35. if (nvs_open_from_partition((const char *)si->opaque_plat,
  36. "_lws_settings", NVS_READONLY,
  37. (nvs_handle_t *)&si->handle_plat))
  38. return 1;
  39. n = nvs_get_blob((nvs_handle_t)si->handle_plat,
  40. name, dest, max_actual);
  41. nvs_close((nvs_handle_t)si->handle_plat);
  42. return !!n;
  43. }
  44. int
  45. lws_settings_plat_set(lws_settings_instance_t *si, const char *name,
  46. const uint8_t *src, size_t len)
  47. {
  48. int n = nvs_flash_init_partition((const char *)si->opaque_plat);
  49. lwsl_notice("%s: init partition %d\n", __func__, n);
  50. if (n == ESP_ERR_NOT_FOUND)
  51. return 1;
  52. if (nvs_open_from_partition((const char *)si->opaque_plat,
  53. "_lws_settings", NVS_READWRITE,
  54. (nvs_handle_t *)&si->handle_plat))
  55. return 1;
  56. n = nvs_set_blob((nvs_handle_t)si->handle_plat, name, src, len);
  57. nvs_commit((nvs_handle_t)si->handle_plat);
  58. nvs_close((nvs_handle_t)si->handle_plat);
  59. return 0;
  60. }