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