Skip to content

Commit 3d03b7f

Browse files
authored
Merge pull request #12878 from mikaleppanen/ns_kv_store
Added kv store adaptation to Nanostack file interface
2 parents 1e9f83f + 9270487 commit 3d03b7f

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

features/nanostack/nanostack-hal-mbed-cmsis-rtos/mbed_lib.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
"event-loop-use-mbed-events": {
1717
"help": "Use Mbed OS global event queue for Nanostack event loop, rather than our own thread.",
1818
"value": false
19+
},
20+
"use-kvstore": {
21+
"help": "Use Mbed OS KVStore API instead of filesystem. Default: false",
22+
"value": false
23+
},
24+
"kvstore-path": {
25+
"help": "Path of KVStore partition. String, Default \"/kv/\"",
26+
"value": "\"/kv/\""
1927
}
2028
}
2129
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
#ifndef FILE_SYSTEM_API_H_
19+
#define FILE_SYSTEM_API_H_
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
void file_system_api_init(void);
26+
27+
#ifdef __cplusplus
28+
}
29+
#endif
30+
31+
#endif

features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "arm_hal_interrupt_private.h"
2929
#include "ns_hal_init.h"
30+
#include "ns_file_system_api.h"
3031

3132
void ns_hal_init(void *heap, size_t h_size, void (*passed_fptr)(heap_fail_t), mem_stat_t *info_ptr)
3233
{
@@ -43,6 +44,10 @@ void ns_hal_init(void *heap, size_t h_size, void (*passed_fptr)(heap_fail_t), me
4344
}
4445
platform_critical_init();
4546
ns_dyn_mem_init(heap, h_size, passed_fptr, info_ptr);
47+
#if MBED_CONF_NANOSTACK_HAL_USE_KVSTORE == 1
48+
ns_file_system_api_init();
49+
#endif
50+
4651
#ifndef NS_EXCLUDE_HIGHRES_TIMER
4752
platform_timer_enable();
4853
#endif

0 commit comments

Comments
 (0)