34
34
#include "nsconfig.h"
35
35
36
36
#include <string.h>
37
- #include <stdio.h>
37
+ #include "Service_Libs/utils/ns_file.h"
38
38
#include "Core/include/ns_address_internal.h"
39
39
#include "ns_file_system.h"
40
40
#include "thread_config.h"
44
44
45
45
#define TRACE_GROUP "tnvm"
46
46
const char * FAST_DATA_FILE = "f_d" ;
47
- #define FAST_DATA_VERSION 1
48
47
#define LINK_INFO_WRITE_DELAY 2
49
48
#define LINK_INFO_SHORT_ADDR_NOT_SET 0xffff
50
49
#define LINK_INFO_WRITE_DONE 0xffff
51
50
52
51
const char * LINK_INFO_FILE = "l_i" ;
53
- #define LINK_INFO_DATA_VERSION 1
54
52
55
53
const char * LEADER_INFO_FILE = "ld_i" ;
56
- #define LEADER_INFO_DATA_VERSION 1
57
54
58
55
typedef struct {
59
56
uint8_t mac [8 ];
@@ -67,18 +64,15 @@ typedef struct {
67
64
} thread_nvm_store_link_info_t ;
68
65
69
66
const char * THREAD_NVM_ACTIVE_CONF_FILE = "a_c" ;
70
- #define ACTIVE_CONF_DATA_VERSION 1
71
67
72
68
const char * DEVICE_CONF_FILE = "s_d" ;
73
- #define DEVICE_CONF_VERSION 1
74
69
75
70
const char * THREAD_NVM_PENDING_CONF_FILE = "p_c" ;
76
- #define PENDING_CONF_DATA_VERSION 1
77
71
78
72
static const char * thread_nvm_store_get_root_path (void );
79
73
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 );
82
76
static void thread_nvm_store_create_path (char * fast_data_path , const char * file_name );
83
77
static int thread_nvm_store_fast_data_save (thread_nvm_fast_data_t * fast_data_to_set );
84
78
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_
131
125
}
132
126
thread_nvm_store_create_path (lc_data_path , LEADER_INFO_FILE );
133
127
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 ));
135
129
}
136
130
137
131
int thread_nvm_store_mleid_rloc_map_read (thread_nvm_mleid_rloc_map * mleid_rloc_map )
138
132
{
139
133
char lc_data_path [LEADER_INFO_STRING_LEN ];
140
- uint32_t version ;
141
134
if (NULL == mleid_rloc_map ) {
142
135
return THREAD_NVM_FILE_PARAMETER_INVALID ;
143
136
}
@@ -146,20 +139,14 @@ int thread_nvm_store_mleid_rloc_map_read(thread_nvm_mleid_rloc_map *mleid_rloc_m
146
139
}
147
140
thread_nvm_store_create_path (lc_data_path , LEADER_INFO_FILE );
148
141
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 ));
150
143
151
144
if (THREAD_NVM_FILE_SUCCESS != ret ) {
152
145
tr_info ("Leader data map read failed" );
153
146
thread_nvm_store_mleid_rloc_map_remove ();
154
147
return ret ;
155
148
}
156
149
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
-
163
150
return ret ;
164
151
}
165
152
@@ -191,7 +178,7 @@ int thread_nvm_store_device_configuration_write(uint8_t *mac_ptr, uint8_t *mleid
191
178
memcpy (d_c .mle_id , mleid_ptr , sizeof (d_c .mle_id ));
192
179
char device_conf_path [DEVICE_CONF_STRING_LEN ];
193
180
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 ));
195
182
}
196
183
197
184
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_
202
189
}
203
190
char device_conf_path [DEVICE_CONF_STRING_LEN ];
204
191
thread_nvm_store_create_path (device_conf_path , DEVICE_CONF_FILE );
205
- uint32_t version ;
206
192
thread_nvm_device_conf_t d_c ;
207
193
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 ));
209
195
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 ));
217
198
}
218
199
return ret ;
219
200
}
@@ -228,13 +209,12 @@ int thread_nvm_store_pending_configuration_write(void *data, uint16_t size)
228
209
return THREAD_NVM_FILE_ROOT_PATH_INVALID ;
229
210
}
230
211
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 );
232
213
}
233
214
234
215
int thread_nvm_store_pending_configuration_read (void * data , uint16_t size )
235
216
{
236
217
char pc_data_path [PENDING_CONF_STRING_LEN ];
237
- uint32_t version ;
238
218
if (NULL == data ) {
239
219
return THREAD_NVM_FILE_PARAMETER_INVALID ;
240
220
}
@@ -243,11 +223,7 @@ int thread_nvm_store_pending_configuration_read(void *data, uint16_t size)
243
223
}
244
224
thread_nvm_store_create_path (pc_data_path , THREAD_NVM_PENDING_CONF_FILE );
245
225
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 );
251
227
return ret ;
252
228
}
253
229
@@ -262,13 +238,12 @@ int thread_nvm_store_active_configuration_write(void *data, uint16_t data_size)
262
238
}
263
239
264
240
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 );
266
242
}
267
243
268
244
int thread_nvm_store_active_configuration_read (void * data , uint16_t data_size )
269
245
{
270
246
char ac_data_path [ACTIVE_CONF_STRING_LEN ];
271
- uint32_t version ;
272
247
if (NULL == data ) {
273
248
return THREAD_NVM_FILE_PARAMETER_INVALID ;
274
249
}
@@ -277,11 +252,7 @@ int thread_nvm_store_active_configuration_read(void *data, uint16_t data_size)
277
252
}
278
253
thread_nvm_store_create_path (ac_data_path , THREAD_NVM_ACTIVE_CONF_FILE );
279
254
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 );
285
256
return ret ;
286
257
}
287
258
@@ -399,12 +370,7 @@ int thread_nvm_store_fast_data_read(thread_nvm_fast_data_t *fast_data)
399
370
if (root_path_valid ()) {
400
371
char fast_data_path [FAST_DATA_STRING_LEN ];
401
372
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 ));
408
374
} else {
409
375
fast_data -> mac_frame_counter = cached_fast_data .mac_frame_counter ;
410
376
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_
417
383
{
418
384
char fast_data_path [FAST_DATA_STRING_LEN ];
419
385
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 ));
421
387
}
422
388
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 )
424
390
{
425
- FILE * fp = fopen (file_name , "w" );
391
+ NS_FILE * fp = ns_fopen (file_name , "w" );
426
392
if (fp == NULL ) {
427
393
tr_error ("NVM open error: %s" , file_name );
428
394
return THREAD_NVM_FILE_CANNOT_OPEN ;
429
395
}
430
396
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 );
440
399
if (n_bytes != data_size ) {
441
400
tr_error ("NVM write error %s" , file_name );
442
401
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
446
405
}
447
406
448
407
// 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 )
450
409
{
451
- FILE * fp = fopen (file_name , "r" );
410
+ NS_FILE * fp = ns_fopen (file_name , "r" );
452
411
if (fp == NULL ) {
453
412
tr_warning ("File not found: %s" , file_name );
454
413
return THREAD_NVM_FILE_CANNOT_OPEN ;
455
414
}
456
415
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 );
466
418
if (n_bytes != data_size ) {
467
419
tr_error ("NVM read error %s" , file_name );
468
420
return THREAD_NVM_FILE_READ_ERROR ;
@@ -488,8 +440,7 @@ int thread_nvm_store_link_info_read(void)
488
440
strcpy (link_info_path , thread_nvm_store_get_root_path ());
489
441
strcat (link_info_path , LINK_INFO_FILE );
490
442
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 ));
493
444
494
445
if (status != THREAD_NVM_FILE_SUCCESS ) {
495
446
if (!memcmp (cached_link_info .nvm_link_info .mac , ADDR_UNSPECIFIED , 8 ) &&
@@ -499,9 +450,6 @@ int thread_nvm_store_link_info_read(void)
499
450
return THREAD_NVM_FILE_READ_ERROR ;
500
451
}
501
452
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 ;
505
453
}
506
454
memcpy (cached_link_info .nvm_link_info .mac , nvm_link_info_tmp .mac , 8 );
507
455
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)
597
545
strcpy (link_info_path , thread_nvm_store_get_root_path ());
598
546
strcat (link_info_path , LINK_INFO_FILE );
599
547
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 ));
601
549
}
602
550
603
551
void thread_nvm_store_seconds_timer (uint32_t seconds )
0 commit comments