nvs_platform.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #ifdef LINUX_TARGET
  15. namespace nvs
  16. {
  17. class Lock
  18. {
  19. public:
  20. Lock() { }
  21. ~Lock() { }
  22. static esp_err_t init()
  23. {
  24. return ESP_OK;
  25. }
  26. static void uninit() {}
  27. };
  28. } // namespace nvs
  29. #else // LINUX_TARGET
  30. #include "freertos/FreeRTOS.h"
  31. #include "freertos/semphr.h"
  32. namespace nvs
  33. {
  34. class Lock
  35. {
  36. public:
  37. Lock()
  38. {
  39. if (mSemaphore) {
  40. xSemaphoreTake(mSemaphore, portMAX_DELAY);
  41. }
  42. }
  43. ~Lock()
  44. {
  45. if (mSemaphore) {
  46. xSemaphoreGive(mSemaphore);
  47. }
  48. }
  49. static esp_err_t init()
  50. {
  51. if (mSemaphore) {
  52. return ESP_OK;
  53. }
  54. mSemaphore = xSemaphoreCreateMutex();
  55. if (!mSemaphore) {
  56. return ESP_ERR_NO_MEM;
  57. }
  58. return ESP_OK;
  59. }
  60. static void uninit()
  61. {
  62. if (mSemaphore) {
  63. vSemaphoreDelete(mSemaphore);
  64. }
  65. mSemaphore = nullptr;
  66. }
  67. static SemaphoreHandle_t mSemaphore;
  68. };
  69. } // namespace nvs
  70. #endif // LINUX_TARGET