|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Arm Limited and affiliates. |
| 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 <new> |
| 19 | +#include <string.h> |
| 20 | +#include "mbed_error.h" |
| 21 | +#include "kvstore_global_api.h" |
| 22 | +#include "kv_config.h" |
| 23 | +#if MBED_CONF_NANOSTACK_HAL_USE_KVSTORE == 1 |
| 24 | +#include "ns_file_system.h" |
| 25 | +#endif |
| 26 | + |
| 27 | +#define TRACE_GROUP "kvs" |
| 28 | + |
| 29 | +#if MBED_CONF_NANOSTACK_HAL_USE_KVSTORE == 1 |
| 30 | + |
| 31 | +static NS_FILE ns_file_system_api_open(const char *filename, const char *mode); |
| 32 | +static int ns_file_system_api_close(NS_FILE *handle); |
| 33 | +static int ns_file_system_api_remove(const char *filename); |
| 34 | +static size_t ns_file_system_api_write(NS_FILE *handle, const void *buffer, size_t size); |
| 35 | +static size_t ns_file_system_api_read(NS_FILE *handle, void *buffer, size_t size); |
| 36 | +static int ns_file_system_api_size(NS_FILE *handle, size_t *size); |
| 37 | + |
| 38 | +extern "C" { |
| 39 | + void ns_file_system_api_init(void) |
| 40 | + { |
| 41 | + ns_file_system_set_root_path(MBED_CONF_NANOSTACK_HAL_KVSTORE_PATH); |
| 42 | + ns_file_system_callbacks_set(ns_file_system_api_open, ns_file_system_api_close, ns_file_system_api_remove, ns_file_system_api_write, ns_file_system_api_read, ns_file_system_api_size); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +static NS_FILE ns_file_system_api_open(const char *filename, const char *mode) |
| 47 | +{ |
| 48 | + if (!filename || (*mode != 'r' && *mode != 'w')) { |
| 49 | + return NULL; |
| 50 | + } |
| 51 | + |
| 52 | + uint16_t file_name_len = strlen(filename); |
| 53 | + if (file_name_len == 0) { |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + |
| 57 | + char *full_name_key = new (std::nothrow) char[file_name_len + sizeof(char)]; |
| 58 | + if (!full_name_key) { |
| 59 | + return NULL; |
| 60 | + } |
| 61 | + |
| 62 | + strcpy(full_name_key, filename); |
| 63 | + |
| 64 | + return (NS_FILE) full_name_key; |
| 65 | +} |
| 66 | + |
| 67 | +static int ns_file_system_api_close(NS_FILE *handle) |
| 68 | +{ |
| 69 | + if (!handle) { |
| 70 | + return -1; |
| 71 | + } |
| 72 | + char *full_name_key = (char *) handle; |
| 73 | + delete full_name_key; |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static int ns_file_system_api_remove(const char *filename) |
| 78 | +{ |
| 79 | + if (!filename) { |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + int ret = kv_remove(filename); |
| 84 | + |
| 85 | + if (ret == MBED_SUCCESS) { |
| 86 | + return 0; |
| 87 | + } |
| 88 | + return -1; |
| 89 | +} |
| 90 | + |
| 91 | +static size_t ns_file_system_api_write(NS_FILE *handle, const void *buffer, size_t size) |
| 92 | +{ |
| 93 | + if (!handle || !buffer || size == 0) { |
| 94 | + return -1; |
| 95 | + } |
| 96 | + |
| 97 | + char *full_name_key = (char *) handle; |
| 98 | + int ret = kv_set(full_name_key, buffer, size, 0); |
| 99 | + |
| 100 | + if (ret == MBED_SUCCESS) { |
| 101 | + return size; |
| 102 | + } |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +static size_t ns_file_system_api_read(NS_FILE *handle, void *buffer, size_t size) |
| 107 | +{ |
| 108 | + if (!handle || !buffer || size == 0) { |
| 109 | + return -1; |
| 110 | + } |
| 111 | + |
| 112 | + char *full_name_key = (char *) handle; |
| 113 | + size_t read_size; |
| 114 | + int ret = kv_get(full_name_key, buffer, size, &read_size); |
| 115 | + |
| 116 | + if (ret == MBED_SUCCESS) { |
| 117 | + return read_size; |
| 118 | + } |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +static int ns_file_system_api_size(NS_FILE *handle, size_t *size) |
| 123 | +{ |
| 124 | + if (!handle || !size) { |
| 125 | + return -1; |
| 126 | + } |
| 127 | + char *full_name_key = (char *) handle; |
| 128 | + |
| 129 | + kv_info_t info; |
| 130 | + int ret = kv_get_info(full_name_key, &info); |
| 131 | + |
| 132 | + if (ret == MBED_SUCCESS) { |
| 133 | + *size = info.size; |
| 134 | + return 0; |
| 135 | + } |
| 136 | + return -1; |
| 137 | +} |
| 138 | + |
| 139 | +#endif |
0 commit comments