Skip to content

Commit 69e2d19

Browse files
author
Mika Leppänen
committed
Added NS filesystem and interface to application
Added interface for application to define file system interface. Interface follows loosely the standard file system interface but with constrains on what is possible on each operation. If application callbacks are not set, functionality defaults to use standard filesystem. Converted Wi-SUN and Thread to use the new interface. Removed NVM TLV lists from Wi-SUN and removed file version from Thread to unify and simplify file system interface.
1 parent c5b6993 commit 69e2d19

File tree

13 files changed

+540
-368
lines changed

13 files changed

+540
-368
lines changed

nanostack/ns_file_system.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,109 @@ int ns_file_system_set_root_path(const char *root_path);
5050
*/
5151
char *ns_file_system_get_root_path(void);
5252

53+
/**
54+
* \brief NS file handle
55+
*
56+
*/
57+
typedef void *NS_FILE;
58+
59+
/**
60+
* File open callback
61+
*
62+
* Depending on underlying file system file open for read for non-existing
63+
* files can return success. In that case file read will fail.
64+
*
65+
* \param filename filename
66+
* \param mode can be either "r" or "w"
67+
*
68+
* \return file handle
69+
* \return NULL on error
70+
*
71+
*/
72+
typedef NS_FILE(*ns_file_open)(const char *filename, const char *mode);
73+
74+
/**
75+
* File close callback
76+
*
77+
* \param handle file handle
78+
*
79+
* \return 0 on success
80+
* \return < 0 in case of errors
81+
*
82+
*/
83+
typedef int (*ns_file_close)(NS_FILE *handle);
84+
85+
/**
86+
* File remove callback
87+
*
88+
* \param filename filename
89+
*
90+
* \return 0 on success
91+
* \return < 0 in case of errors
92+
*
93+
*/
94+
typedef int (*ns_file_remove)(const char *filename);
95+
96+
/**
97+
* File write callback
98+
*
99+
* Write is not stream write. The whole file is written from start to end
100+
* and if function is called again, previous file content is replaced with
101+
* new content.
102+
*
103+
* \param handle file handle
104+
* \param buffer buffer
105+
* \param buffer buffer size
106+
*
107+
* \return bytes written
108+
*
109+
*/
110+
typedef size_t (*ns_file_write)(NS_FILE *handle, const void *buffer, size_t size);
111+
112+
/**
113+
* File read callback
114+
*
115+
* Read is not stream read. The whole file is read from start to end
116+
* and if function is called again, read is started from start again.
117+
*
118+
* \param handle file handle
119+
* \param buffer buffer
120+
* \param size buffer size
121+
*
122+
* \return bytes written
123+
*
124+
*/
125+
typedef size_t (*ns_file_read)(NS_FILE *handle, void *buffer, size_t size);
126+
127+
/**
128+
* File size callback
129+
*
130+
* Reads file size.
131+
*
132+
* \param handle file handle
133+
* \param size file size
134+
*
135+
* \return 0 on success
136+
* \return < 0 in case of reading file size is not supported
137+
*
138+
*/
139+
typedef int (*ns_file_size)(NS_FILE *handle, size_t *size);
140+
141+
/**
142+
* File callbacks set
143+
*
144+
* Sets file handling callbacks to nanostack.
145+
*
146+
* \param open file open callback
147+
* \param close file close callback
148+
* \param remove file remove callback
149+
* \param write file write callback
150+
* \param read file read callback
151+
* \param size file size callback
152+
*
153+
*/
154+
void ns_file_system_callbacks_set(ns_file_open open, ns_file_close close, ns_file_remove remove, ns_file_write write, ns_file_read read, ns_file_size size);
155+
53156
#ifdef __cplusplus
54157
}
55158
#endif

source/6LoWPAN/Thread/thread_nvm_store.c

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "nsconfig.h"
3535

3636
#include <string.h>
37-
#include <stdio.h>
37+
#include "Service_Libs/utils/ns_file.h"
3838
#include "Core/include/ns_address_internal.h"
3939
#include "ns_file_system.h"
4040
#include "thread_config.h"
@@ -44,16 +44,13 @@
4444

4545
#define TRACE_GROUP "tnvm"
4646
const char *FAST_DATA_FILE = "f_d";
47-
#define FAST_DATA_VERSION 1
4847
#define LINK_INFO_WRITE_DELAY 2
4948
#define LINK_INFO_SHORT_ADDR_NOT_SET 0xffff
5049
#define LINK_INFO_WRITE_DONE 0xffff
5150

5251
const char *LINK_INFO_FILE = "l_i";
53-
#define LINK_INFO_DATA_VERSION 1
5452

5553
const char *LEADER_INFO_FILE = "ld_i";
56-
#define LEADER_INFO_DATA_VERSION 1
5754

5855
typedef struct {
5956
uint8_t mac[8];
@@ -67,18 +64,15 @@ typedef struct {
6764
} thread_nvm_store_link_info_t;
6865

6966
const char *THREAD_NVM_ACTIVE_CONF_FILE = "a_c";
70-
#define ACTIVE_CONF_DATA_VERSION 1
7167

7268
const char *DEVICE_CONF_FILE = "s_d";
73-
#define DEVICE_CONF_VERSION 1
7469

7570
const char *THREAD_NVM_PENDING_CONF_FILE = "p_c";
76-
#define PENDING_CONF_DATA_VERSION 1
7771

7872
static const char *thread_nvm_store_get_root_path(void);
7973
static int root_path_valid(void);
80-
static int thread_nvm_store_read(const char *file_name, void *data, uint32_t data_size, uint32_t *version);
81-
static int thread_nvm_store_write(const char *file_name, void *data, uint32_t data_size, uint32_t version);
74+
static int thread_nvm_store_read(const char *file_name, void *data, uint32_t data_size);
75+
static int thread_nvm_store_write(const char *file_name, void *data, uint32_t data_size);
8276
static void thread_nvm_store_create_path(char *fast_data_path, const char *file_name);
8377
static int thread_nvm_store_fast_data_save(thread_nvm_fast_data_t *fast_data_to_set);
8478
static int thread_nvm_store_all_counters_store(uint32_t mac_frame_counter, uint32_t mle_frame_counter, uint32_t seq_counter);
@@ -131,13 +125,12 @@ int thread_nvm_store_mleid_rloc_map_write(thread_nvm_mleid_rloc_map *mleid_rloc_
131125
}
132126
thread_nvm_store_create_path(lc_data_path, LEADER_INFO_FILE);
133127
tr_debug("writing to store rloc mapping info");
134-
return thread_nvm_store_write(lc_data_path, mleid_rloc_map, sizeof(thread_nvm_mleid_rloc_map), LEADER_INFO_DATA_VERSION);
128+
return thread_nvm_store_write(lc_data_path, mleid_rloc_map, sizeof(thread_nvm_mleid_rloc_map));
135129
}
136130

137131
int thread_nvm_store_mleid_rloc_map_read(thread_nvm_mleid_rloc_map *mleid_rloc_map)
138132
{
139133
char lc_data_path[LEADER_INFO_STRING_LEN];
140-
uint32_t version;
141134
if (NULL == mleid_rloc_map) {
142135
return THREAD_NVM_FILE_PARAMETER_INVALID;
143136
}
@@ -146,20 +139,14 @@ int thread_nvm_store_mleid_rloc_map_read(thread_nvm_mleid_rloc_map *mleid_rloc_m
146139
}
147140
thread_nvm_store_create_path(lc_data_path, LEADER_INFO_FILE);
148141

149-
int ret = thread_nvm_store_read(lc_data_path, mleid_rloc_map, sizeof(thread_nvm_mleid_rloc_map), &version);
142+
int ret = thread_nvm_store_read(lc_data_path, mleid_rloc_map, sizeof(thread_nvm_mleid_rloc_map));
150143

151144
if (THREAD_NVM_FILE_SUCCESS != ret) {
152145
tr_info("Leader data map read failed");
153146
thread_nvm_store_mleid_rloc_map_remove();
154147
return ret;
155148
}
156149

157-
if (LEADER_INFO_DATA_VERSION != version) {
158-
tr_info("Leader data map version mismatch %"PRIu32, version);
159-
thread_nvm_store_mleid_rloc_map_remove();
160-
return THREAD_NVM_FILE_VERSION_WRONG;
161-
}
162-
163150
return ret;
164151
}
165152

@@ -191,7 +178,7 @@ int thread_nvm_store_device_configuration_write(uint8_t *mac_ptr, uint8_t *mleid
191178
memcpy(d_c.mle_id, mleid_ptr, sizeof(d_c.mle_id));
192179
char device_conf_path[DEVICE_CONF_STRING_LEN];
193180
thread_nvm_store_create_path(device_conf_path, DEVICE_CONF_FILE);
194-
return thread_nvm_store_write(device_conf_path, &d_c, sizeof(thread_nvm_device_conf_t), DEVICE_CONF_VERSION);
181+
return thread_nvm_store_write(device_conf_path, &d_c, sizeof(thread_nvm_device_conf_t));
195182
}
196183

197184
int thread_nvm_store_device_configuration_read(uint8_t *mac_ptr, uint8_t *mleid_ptr)
@@ -202,18 +189,12 @@ int thread_nvm_store_device_configuration_read(uint8_t *mac_ptr, uint8_t *mleid_
202189
}
203190
char device_conf_path[DEVICE_CONF_STRING_LEN];
204191
thread_nvm_store_create_path(device_conf_path, DEVICE_CONF_FILE);
205-
uint32_t version;
206192
thread_nvm_device_conf_t d_c;
207193

208-
ret = thread_nvm_store_read(device_conf_path, &d_c, sizeof(thread_nvm_device_conf_t), &version);
194+
ret = thread_nvm_store_read(device_conf_path, &d_c, sizeof(thread_nvm_device_conf_t));
209195
if (THREAD_NVM_FILE_SUCCESS == ret) {
210-
if (THREAD_NVM_FILE_SUCCESS == ret && DEVICE_CONF_VERSION != version) {
211-
tr_info("fast data version mismatch %"PRIu32, version);
212-
ret = THREAD_NVM_FILE_VERSION_WRONG;
213-
} else {
214-
memcpy(mac_ptr, d_c.mac, sizeof(d_c.mac));
215-
memcpy(mleid_ptr, d_c.mle_id, sizeof(d_c.mle_id));
216-
}
196+
memcpy(mac_ptr, d_c.mac, sizeof(d_c.mac));
197+
memcpy(mleid_ptr, d_c.mle_id, sizeof(d_c.mle_id));
217198
}
218199
return ret;
219200
}
@@ -228,13 +209,12 @@ int thread_nvm_store_pending_configuration_write(void *data, uint16_t size)
228209
return THREAD_NVM_FILE_ROOT_PATH_INVALID;
229210
}
230211
thread_nvm_store_create_path(pc_data_path, THREAD_NVM_PENDING_CONF_FILE);
231-
return thread_nvm_store_write(pc_data_path, data, size, PENDING_CONF_DATA_VERSION);
212+
return thread_nvm_store_write(pc_data_path, data, size);
232213
}
233214

234215
int thread_nvm_store_pending_configuration_read(void *data, uint16_t size)
235216
{
236217
char pc_data_path[PENDING_CONF_STRING_LEN];
237-
uint32_t version;
238218
if (NULL == data) {
239219
return THREAD_NVM_FILE_PARAMETER_INVALID;
240220
}
@@ -243,11 +223,7 @@ int thread_nvm_store_pending_configuration_read(void *data, uint16_t size)
243223
}
244224
thread_nvm_store_create_path(pc_data_path, THREAD_NVM_PENDING_CONF_FILE);
245225

246-
int ret = thread_nvm_store_read(pc_data_path, data, size, &version);
247-
if (THREAD_NVM_FILE_SUCCESS == ret && PENDING_CONF_DATA_VERSION != version) {
248-
tr_info("Pending configuration version mismatch %"PRIu32, version);
249-
return THREAD_NVM_FILE_VERSION_WRONG;
250-
}
226+
int ret = thread_nvm_store_read(pc_data_path, data, size);
251227
return ret;
252228
}
253229

@@ -262,13 +238,12 @@ int thread_nvm_store_active_configuration_write(void *data, uint16_t data_size)
262238
}
263239

264240
thread_nvm_store_create_path(ac_data_path, THREAD_NVM_ACTIVE_CONF_FILE);
265-
return thread_nvm_store_write(ac_data_path, data, data_size, ACTIVE_CONF_DATA_VERSION);
241+
return thread_nvm_store_write(ac_data_path, data, data_size);
266242
}
267243

268244
int thread_nvm_store_active_configuration_read(void *data, uint16_t data_size)
269245
{
270246
char ac_data_path[ACTIVE_CONF_STRING_LEN];
271-
uint32_t version;
272247
if (NULL == data) {
273248
return THREAD_NVM_FILE_PARAMETER_INVALID;
274249
}
@@ -277,11 +252,7 @@ int thread_nvm_store_active_configuration_read(void *data, uint16_t data_size)
277252
}
278253
thread_nvm_store_create_path(ac_data_path, THREAD_NVM_ACTIVE_CONF_FILE);
279254

280-
int ret = thread_nvm_store_read(ac_data_path, data, data_size, &version);
281-
if (THREAD_NVM_FILE_SUCCESS == ret && ACTIVE_CONF_DATA_VERSION != version) {
282-
tr_info("active configuration version mismatch %"PRIu32, version);
283-
return THREAD_NVM_FILE_VERSION_WRONG;
284-
}
255+
int ret = thread_nvm_store_read(ac_data_path, data, data_size);
285256
return ret;
286257
}
287258

@@ -399,12 +370,7 @@ int thread_nvm_store_fast_data_read(thread_nvm_fast_data_t *fast_data)
399370
if (root_path_valid()) {
400371
char fast_data_path[FAST_DATA_STRING_LEN];
401372
thread_nvm_store_create_path(fast_data_path, FAST_DATA_FILE);
402-
uint32_t version;
403-
ret = thread_nvm_store_read(fast_data_path, fast_data, sizeof(thread_nvm_fast_data_t), &version);
404-
if (THREAD_NVM_FILE_SUCCESS == ret && FAST_DATA_VERSION != version) {
405-
tr_info("fast data version mismatch %"PRIu32, version);
406-
return THREAD_NVM_FILE_VERSION_WRONG;
407-
}
373+
ret = thread_nvm_store_read(fast_data_path, fast_data, sizeof(thread_nvm_fast_data_t));
408374
} else {
409375
fast_data->mac_frame_counter = cached_fast_data.mac_frame_counter;
410376
fast_data->mle_frame_counter = cached_fast_data.mle_frame_counter;
@@ -417,26 +383,19 @@ static int thread_nvm_store_fast_data_save(thread_nvm_fast_data_t *fast_data_to_
417383
{
418384
char fast_data_path[FAST_DATA_STRING_LEN];
419385
thread_nvm_store_create_path(fast_data_path, FAST_DATA_FILE);
420-
return thread_nvm_store_write(fast_data_path, fast_data_to_set, sizeof(thread_nvm_fast_data_t), FAST_DATA_VERSION);
386+
return thread_nvm_store_write(fast_data_path, fast_data_to_set, sizeof(thread_nvm_fast_data_t));
421387
}
422388

423-
static int thread_nvm_store_write(const char *file_name, void *data, uint32_t data_size, uint32_t version)
389+
static int thread_nvm_store_write(const char *file_name, void *data, uint32_t data_size)
424390
{
425-
FILE *fp = fopen(file_name, "w");
391+
NS_FILE *fp = ns_fopen(file_name, "w");
426392
if (fp == NULL) {
427393
tr_error("NVM open error: %s", file_name);
428394
return THREAD_NVM_FILE_CANNOT_OPEN;
429395
}
430396

431-
size_t n_bytes = fwrite(&version, 1, sizeof(uint32_t), fp);
432-
if (n_bytes != sizeof(uint32_t)) {
433-
tr_warning("NVM version write error");
434-
fclose(fp);
435-
return THREAD_NVM_FILE_WRITE_ERROR;
436-
}
437-
438-
n_bytes = fwrite(data, 1, data_size, fp);
439-
fclose(fp);
397+
size_t n_bytes = ns_fwrite(fp, data, data_size);
398+
ns_fclose(fp);
440399
if (n_bytes != data_size) {
441400
tr_error("NVM write error %s", file_name);
442401
return THREAD_NVM_FILE_WRITE_ERROR;
@@ -446,23 +405,16 @@ static int thread_nvm_store_write(const char *file_name, void *data, uint32_t da
446405
}
447406

448407
// returns 0 when ok
449-
static int thread_nvm_store_read(const char *file_name, void *data, uint32_t data_size, uint32_t *version)
408+
static int thread_nvm_store_read(const char *file_name, void *data, uint32_t data_size)
450409
{
451-
FILE *fp = fopen(file_name, "r");
410+
NS_FILE *fp = ns_fopen(file_name, "r");
452411
if (fp == NULL) {
453412
tr_warning("File not found: %s", file_name);
454413
return THREAD_NVM_FILE_CANNOT_OPEN;
455414
}
456415

457-
size_t n_bytes = fread(version, 1, sizeof(uint32_t), fp);
458-
if (n_bytes != sizeof(uint32_t)) {
459-
tr_warning("NVM version read error %s", file_name);
460-
fclose(fp);
461-
return THREAD_NVM_FILE_READ_ERROR;
462-
}
463-
464-
n_bytes = fread(data, 1, data_size, fp);
465-
fclose(fp);
416+
size_t n_bytes = ns_fread(fp, data, data_size);
417+
ns_fclose(fp);
466418
if (n_bytes != data_size) {
467419
tr_error("NVM read error %s", file_name);
468420
return THREAD_NVM_FILE_READ_ERROR;
@@ -488,8 +440,7 @@ int thread_nvm_store_link_info_read(void)
488440
strcpy(link_info_path, thread_nvm_store_get_root_path());
489441
strcat(link_info_path, LINK_INFO_FILE);
490442

491-
uint32_t version = 0;
492-
status = thread_nvm_store_read(link_info_path, &nvm_link_info_tmp, sizeof(nvm_link_info_t), &version);
443+
status = thread_nvm_store_read(link_info_path, &nvm_link_info_tmp, sizeof(nvm_link_info_t));
493444

494445
if (status != THREAD_NVM_FILE_SUCCESS) {
495446
if (!memcmp(cached_link_info.nvm_link_info.mac, ADDR_UNSPECIFIED, 8) &&
@@ -499,9 +450,6 @@ int thread_nvm_store_link_info_read(void)
499450
return THREAD_NVM_FILE_READ_ERROR;
500451
}
501452
return status;
502-
} else if (ACTIVE_CONF_DATA_VERSION != version) {
503-
tr_info("link info version mismatch %"PRIu32, version);
504-
return THREAD_NVM_FILE_VERSION_WRONG;
505453
}
506454
memcpy(cached_link_info.nvm_link_info.mac, nvm_link_info_tmp.mac, 8);
507455
cached_link_info.nvm_link_info.short_addr = nvm_link_info_tmp.short_addr;
@@ -597,7 +545,7 @@ static void thread_nvm_store_link_info_delayed_write(uint32_t seconds)
597545
strcpy(link_info_path, thread_nvm_store_get_root_path());
598546
strcat(link_info_path, LINK_INFO_FILE);
599547
tr_info("link info write parent mac: %s parent short addr: %"PRIu16, trace_array(cached_link_info.nvm_link_info.mac, 8), cached_link_info.nvm_link_info.short_addr);
600-
thread_nvm_store_write(link_info_path, &cached_link_info.nvm_link_info, sizeof(nvm_link_info_t), LINK_INFO_DATA_VERSION);
548+
thread_nvm_store_write(link_info_path, &cached_link_info.nvm_link_info, sizeof(nvm_link_info_t));
601549
}
602550

603551
void thread_nvm_store_seconds_timer(uint32_t seconds)

source/6LoWPAN/ws/ws_pae_auth.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "eventOS_scheduler.h"
2929
#include "eventOS_event_timer.h"
3030
#include "ns_address.h"
31+
#include "Service_Libs/utils/ns_file.h"
3132
#include "NWK_INTERFACE/Include/protocol.h"
3233
#include "6LoWPAN/ws/ws_config.h"
3334
#include "Security/protocols/sec_prot_cfg.h"

0 commit comments

Comments
 (0)