|
| 1 | +/* Copyright (c) 2018 Arm Limited |
| 2 | +* |
| 3 | +* SPDX-License-Identifier: Apache-2.0 |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include "mbed.h" |
| 19 | +#include <stdio.h> |
| 20 | +#include <string.h> |
| 21 | +#include "KVStore.h" |
| 22 | +#include "kvstore_global_api.h" |
| 23 | + |
| 24 | +using namespace mbed; |
| 25 | + |
| 26 | +#define EXAMPLE_KV_VALUE_LENGTH 64 |
| 27 | +#define EXAMPLE_KV_KEY_LENGTH 32 |
| 28 | +#define err_code(res) MBED_GET_ERROR_CODE(res) |
| 29 | + |
| 30 | +void kv_store_global_api_example(); |
| 31 | + |
| 32 | +int main() |
| 33 | +{ |
| 34 | + /* KV Store Static API Example */ |
| 35 | + kv_store_global_api_example(); |
| 36 | +} |
| 37 | + |
| 38 | +void kv_store_global_api_example() |
| 39 | +{ |
| 40 | + char kv_value_in[EXAMPLE_KV_VALUE_LENGTH] = {"kvstore_dummy_value_hello_world"}; |
| 41 | + char kv_key_in[EXAMPLE_KV_KEY_LENGTH] = {"/kv/dummy_key1"}; |
| 42 | + char kv_key_out[EXAMPLE_KV_KEY_LENGTH] = {0}; |
| 43 | + size_t actual_size = 0; |
| 44 | + |
| 45 | + /* key information container */ |
| 46 | + kv_info_t info; |
| 47 | + |
| 48 | + /* kv store iterator */ |
| 49 | + kv_iterator_t kvstore_it; |
| 50 | + |
| 51 | + int i_ind = 0; |
| 52 | + |
| 53 | + printf("--- Mbed OS KVStore static API example ---\n"); |
| 54 | + |
| 55 | + int res = MBED_ERROR_NOT_READY; |
| 56 | + |
| 57 | + /* Start By Resetting the KV Storage */ |
| 58 | + printf("kv_reset\n"); |
| 59 | + res = kv_reset("/kv/"); |
| 60 | + printf("kv_reset -> %d\n", err_code(res)); |
| 61 | + |
| 62 | + /* Set First 'Dummy' Key/Value pair with unprotected clear value data */ |
| 63 | + printf("kv_set first dummy key\n"); |
| 64 | + res = kv_set(kv_key_in, kv_value_in, strlen(kv_value_in), 0); |
| 65 | + printf("kv_set -> %d\n", err_code(res)); |
| 66 | + |
| 67 | + /* Read the KV Pair you've just set */ |
| 68 | + /* Start by getting key's information */ |
| 69 | + printf("kv_get_info of first key\n"); |
| 70 | + res = kv_get_info(kv_key_in, &info); |
| 71 | + printf("kv_get_info -> %d\n", err_code(res)); |
| 72 | + printf("kv_get_info key: %s\n", kv_key_in); |
| 73 | + printf("kv_get_info info - size: %u, flags: %lu\n", info.size, info.flags); |
| 74 | + |
| 75 | + /* Now that you know the data value size of this key, |
| 76 | + * allocate a buffer with matching size and get the value data */ |
| 77 | + printf("kv_get first key\n"); |
| 78 | + char *kv_first_value_out = new char[info.size+1]; |
| 79 | + memset(kv_first_value_out, 0, info.size+1); |
| 80 | + res = kv_get(kv_key_in, kv_first_value_out, info.size, &actual_size); |
| 81 | + printf("kv_get -> %d\n", err_code(res)); |
| 82 | + printf("kv_get key: %s\n", kv_key_in); |
| 83 | + printf("kv_get value: %s\n", kv_first_value_out); |
| 84 | + delete[] kv_first_value_out; |
| 85 | + |
| 86 | + /* Lets set some more 'Dummy' and 'Real' KV pairs */ |
| 87 | + /* Set 'Dummy' Key2 */ |
| 88 | + printf("kv_set second dummy key \n"); |
| 89 | + res = kv_set("/kv/dummy_key2", "dummy_value2", strlen("dummy_value2"), 0); |
| 90 | + printf("kv_set -> %d\n", err_code(res)); |
| 91 | + |
| 92 | + /* Set an authenticated-encrypted 'Dummy' key with Replay protection */ |
| 93 | + printf("kv_set third key with Confidentiality, Integrity and Replay Protection flags\n"); |
| 94 | + res = kv_set("/kv/dummy_auth_enc_key", "auth_enc_value", strlen("auth_enc_value"), |
| 95 | + KV_REQUIRE_CONFIDENTIALITY_FLAG|KV_REQUIRE_INTEGRITY_FLAG|KV_REQUIRE_REPLAY_PROTECTION_FLAG); |
| 96 | + printf("kv_set -> %d\n", err_code(res)); |
| 97 | + |
| 98 | + /* Set 2 non-dummy 'Real' KV pairs */ |
| 99 | + /* Set 'Real' Key 1 */ |
| 100 | + printf("kv_set Set 'Real' Key 1\n"); |
| 101 | + res = kv_set("/kv/real_key1", "real_value1", strlen("real_value1"), 0); |
| 102 | + printf("kv_set -> %d\n", err_code(res)); |
| 103 | + /* Set 'Real' Write-Once Key2 for a key that you do not want to be removed */ |
| 104 | + printf("kv_set Set 'Real' Key 2 with flag write-once\n"); |
| 105 | + res = kv_set("/kv/real_wo_key", "real_wo_value", strlen("real_wo_value"), KV_WRITE_ONCE_FLAG); |
| 106 | + printf("kv_set -> %d\n", err_code(res)); |
| 107 | + |
| 108 | + /* Now lets remove all of the 'Dummy' Keys and remain with the 'Real' ones */ |
| 109 | + printf("Removing 'Dummy' Keys\n"); |
| 110 | + /* Iterate and remove Keys that start with prefix 'dummy' */ |
| 111 | + res = kv_iterator_open(&kvstore_it, "dummy"); |
| 112 | + memset(kv_key_out, 0, EXAMPLE_KV_KEY_LENGTH); |
| 113 | + while (kv_iterator_next(kvstore_it, kv_key_out, EXAMPLE_KV_KEY_LENGTH) != MBED_ERROR_ITEM_NOT_FOUND) { |
| 114 | + i_ind++; |
| 115 | + printf("%d) Removing %s\n", i_ind, kv_key_out); |
| 116 | + kv_remove(kv_key_out); |
| 117 | + memset(kv_key_out, 0, EXAMPLE_KV_KEY_LENGTH); |
| 118 | + } |
| 119 | + res = kv_iterator_close(kvstore_it); |
| 120 | + printf("Remaining with 'Real' Keys:\n"); |
| 121 | + /* Iterate on all remaining Keys */ |
| 122 | + res = kv_iterator_open(&kvstore_it, NULL); |
| 123 | + memset(kv_key_out, 0, EXAMPLE_KV_KEY_LENGTH); |
| 124 | + i_ind = 0; |
| 125 | + while (kv_iterator_next(kvstore_it, kv_key_out, EXAMPLE_KV_KEY_LENGTH) != MBED_ERROR_ITEM_NOT_FOUND) { |
| 126 | + i_ind++; |
| 127 | + printf("%d) %s\n", i_ind, kv_key_out); |
| 128 | + memset(kv_key_out, 0, EXAMPLE_KV_KEY_LENGTH); |
| 129 | + } |
| 130 | + res = kv_iterator_close(kvstore_it); |
| 131 | + |
| 132 | + /* Try to remove write-once Key - should fail */ |
| 133 | + printf("kv_remove write-once file - should fail!\n"); |
| 134 | + res = kv_remove("/kv/real_wo_key"); |
| 135 | + printf("kv_remove -> %d\n", err_code(res)); |
| 136 | + |
| 137 | + /* Finally, reset will format kvstore and remove All Keys (including write-once keys) */ |
| 138 | + printf("kv_reset format kvstore (including write-once)\n"); |
| 139 | + res = kv_reset("/kv/"); |
| 140 | + printf("kv_reset -> %d\n", err_code(res)); |
| 141 | + |
| 142 | + return; |
| 143 | +} |
0 commit comments