-
Notifications
You must be signed in to change notification settings - Fork 721
Expand file tree
/
Copy pathconfigdata_main.c
More file actions
792 lines (644 loc) · 21.2 KB
/
configdata_main.c
File metadata and controls
792 lines (644 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/****************************************************************************
* apps/examples/configdata/configdata_main.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <nuttx/debug.h>
#include <nuttx/crc32.h>
#include <nuttx/mtd/mtd.h>
#include <nuttx/mtd/configdata.h>
#include <nuttx/fs/ioctl.h>
#include <sys/ioctl.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* The default is to use the RAM MTD device at drivers/mtd/rammtd.c. But
* an architecture-specific MTD driver can be used instead by defining
* CONFIG_EXAMPLES_CONFIGDATA_ARCHINIT. In this case, the initialization
* logic will call configdata_archinitialize() to obtain the MTD driver
* instance.
*/
#ifndef CONFIG_EXAMPLES_CONFIGDATA_ARCHINIT
/* This must exactly match the default configuration in
* drivers/mtd/rammtd.c
*/
# ifndef CONFIG_RAMMTD_ERASESIZE
# define CONFIG_RAMMTD_ERASESIZE 4096
# endif
# ifndef CONFIG_EXAMPLES_CONFIGDATA_NEBLOCKS
# define CONFIG_EXAMPLES_CONFIGDATA_NEBLOCKS (256)
# endif
# define EXAMPLES_CONFIGDATA_BUFSIZE \
(CONFIG_RAMMTD_ERASESIZE * CONFIG_EXAMPLES_CONFIGDATA_NEBLOCKS)
#endif
#ifndef CONFIG_EXAMPLES_CONFIGDATA_MAXSIZE
# define CONFIG_EXAMPLES_CONFIGDATA_MAXSIZE 512
#endif
#ifndef CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES
# define CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES 3000
#endif
#ifndef CONFIG_EXAMPLES_CONFIGDATA_NLOOPS
# define CONFIG_EXAMPLES_CONFIGDATA_NLOOPS 100
#endif
#define EXAMPLES_CONFIGDATA_REPORT (CONFIG_EXAMPLES_CONFIGDATA_NLOOPS / 20)
#if EXAMPLES_CONFIGDATA_REPORT == 0
# undef EXAMPLES_CONFIGDATA_REPORT
# define EXAMPLES_CONFIGDATA_REPORT 1
#endif
#ifndef CONFIG_EXAMPLES_CONFIGDATA_VERBOSE
# define CONFIG_EXAMPLES_CONFIGDATA_VERBOSE 0
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct configdata_entrydesc_s
{
uint16_t id;
uint8_t instance;
uint16_t len;
uint32_t crc;
uint8_t deleted;
uint8_t changed;
};
/****************************************************************************
* Private Data
****************************************************************************/
/* Pre-allocated simulated flash */
#ifndef CONFIG_EXAMPLES_CONFIGDATA_ARCHINIT
static uint8_t g_simflash[EXAMPLES_CONFIGDATA_BUFSIZE << 1];
#endif
static uint8_t g_entryimage[CONFIG_EXAMPLES_CONFIGDATA_MAXSIZE];
static struct configdata_entrydesc_s
g_entries[CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES];
static int g_nentries;
static int g_ndeleted;
static int g_fd;
static int g_ntests;
static int g_nverified;
static int g_ntotalalloc;
static int g_ntotaldelete;
static struct mallinfo g_mmbefore;
static struct mallinfo g_mmafter;
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
static struct mallinfo g_mmprevious;
#endif
/****************************************************************************
* External Functions
****************************************************************************/
#ifdef CONFIG_EXAMPLES_CONFIGDATA_ARCHINIT
extern FAR struct mtd_dev_s *configdata_archinitialize(void);
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: configdata_memusage
****************************************************************************/
static void configdata_showmemusage(struct mallinfo *mmbefore,
struct mallinfo *mmafter)
{
printf("VARIABLE BEFORE AFTER\n");
printf("======== ======== ========\n");
printf("arena %8x %8x\n", mmbefore->arena, mmafter->arena);
printf("ordblks %8d %8d\n", mmbefore->ordblks, mmafter->ordblks);
printf("mxordblk %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk);
printf("uordblks %8x %8x\n", mmbefore->uordblks, mmafter->uordblks);
printf("fordblks %8x %8x\n", mmbefore->fordblks, mmafter->fordblks);
}
/****************************************************************************
* Name: configdata_loopmemusage
****************************************************************************/
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
static void configdata_loopmemusage(void)
{
/* Get the current memory usage */
g_mmafter = mallinfo();
/* Show the change from the previous loop */
printf("\nEnd of loop memory usage:\n");
configdata_showmemusage(&g_mmprevious, &g_mmafter);
/* Set up for the next test */
g_mmprevious = g_mmafter;
}
#endif
/****************************************************************************
* Name: configdata_endmemusage
****************************************************************************/
static void configdata_endmemusage(void)
{
g_mmafter = mallinfo();
printf("\nFinal memory usage:\n");
configdata_showmemusage(&g_mmbefore, &g_mmafter);
printf("\nTotal adds: %d Total deletes : %d\n",
g_ntotalalloc, g_ntotaldelete);
printf("Total tests: %d Number passed: %d Failed: %d\n",
g_ntests, g_nverified, g_ntests - g_nverified);
}
/****************************************************************************
* Name: configdata_randid
****************************************************************************/
static inline uint16_t configdata_randid(void)
{
int x;
int value;
retry:
value = rand() & 0x7fff;
if (value == 0)
{
value = 100;
}
/* Ensure we don't have a duplicate id */
for (x = 0; x < g_nentries; x++)
{
if (value == g_entries[x].id)
{
goto retry;
}
}
return value;
}
/****************************************************************************
* Name: configdata_randlen
****************************************************************************/
static inline uint16_t configdata_randlen(void)
{
int value = rand() % CONFIG_EXAMPLES_CONFIGDATA_MAXSIZE;
if (value == 0)
{
return 1;
}
return value;
}
/****************************************************************************
* Name: configdata_randinstance
****************************************************************************/
static inline uint8_t configdata_randinstance(void)
{
return rand() & 0xff;
}
/****************************************************************************
* Name: configdata_freefile
****************************************************************************/
#if 0 /* Not used */
static void configdata_freeentry(FAR struct configdata_entrydesc_s *entry)
{
memset(entry, 0, sizeof(struct configdata_entrydesc_s));
}
#endif
/****************************************************************************
* Name: configdata_wrentry
****************************************************************************/
static inline int
configdata_wrentry(FAR struct configdata_entrydesc_s *entry)
{
size_t x;
int ret;
struct config_data_s config;
/* Create a random entry */
entry->id = configdata_randid();
entry->instance = configdata_randinstance();
entry->len = configdata_randlen();
/* Write some random data to the entry */
for (x = 0; x < entry->len; x++)
{
g_entryimage[x] = rand() & 0xff;
}
/* Calculate the crc32 for the data */
entry->crc = crc32(g_entryimage, entry->len);
/* Write the entry to the /dev/config device */
config.id = entry->id;
config.instance = entry->instance;
config.len = entry->len;
config.configdata = g_entryimage;
ret = ioctl(g_fd, CFGDIOC_SETCONFIG, (unsigned long)&config);
if (ret < 0)
{
entry->id = 0;
entry->len = 0;
return ERROR;
}
return OK;
}
/****************************************************************************
* Name: configdata_fillconfig
****************************************************************************/
static int configdata_fillconfig(void)
{
FAR struct configdata_entrydesc_s *entry;
int ret;
int i;
/* Create a file for each unused file structure */
for (i = 0; i < CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES; i++)
{
entry = &g_entries[i];
if (entry->id == 0)
{
ret = configdata_wrentry(entry);
if (ret < 0)
{
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf(" /dev/config full\n");
#endif
return ERROR;
}
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf(" Created entry %04X, %d Len=%d\n",
entry->id, entry->instance, entry->len);
#endif
g_nentries++;
g_ntotalalloc++;
}
}
return OK;
}
/****************************************************************************
* Name: configdata_rdentry
****************************************************************************/
static inline int
configdata_rdentry(FAR struct configdata_entrydesc_s *entry)
{
struct config_data_s config;
uint32_t crc;
int ret;
/* Read the config entry from /dev/config */
config.id = entry->id;
config.instance = entry->instance;
config.len = entry->len;
config.configdata = g_entryimage;
ret = ioctl(g_fd, CFGDIOC_GETCONFIG, (unsigned long)&config);
if (ret < 0)
{
return ERROR;
}
/* Verify the file image CRC */
crc = crc32(g_entryimage, entry->len);
if (crc != entry->crc)
{
printf("ERROR: Bad CRC: %" PRIu32 " vs %" PRIu32 "\n",
crc, entry->crc);
printf(" Entry id: %04X\n", entry->id);
printf(" Entry size: %d\n", entry->len);
return ERROR;
}
return OK;
}
/****************************************************************************
* Name: configdata_verifyconfig
****************************************************************************/
static int configdata_verifyconfig(void)
{
FAR struct configdata_entrydesc_s *entry;
int ret;
int errcode = OK;
int i;
/* Create a file for each unused file structure */
for (i = 0; i < CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES; i++)
{
entry = &g_entries[i];
if (entry->id != 0)
{
g_ntests++;
ret = configdata_rdentry(entry);
if (ret < 0)
{
/* Check if this entry has been deleted */
if (entry->deleted)
{
/* Good, it wasn't found (as it shouldn't be) */
g_nverified++;
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf(" Verified delete %04X, %d\n", entry->id,
entry->instance);
#endif
}
else
{
printf("ERROR: Failed to read an entry: %d\n", i);
printf(" Entry id: %04X\n", entry->id);
printf(" Entry size: %d\n", entry->len);
errcode = ERROR;
}
}
else
{
/* Check if this entry has been deleted and should report an
* error.
*/
if (entry->deleted)
{
printf("ERROR: Succesffully read a deleted entry\n");
printf(" Entry id: %04X\n", entry->id);
printf(" Entry size: %d\n", entry->len);
errcode = ERROR;
}
else
{
g_nverified++;
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf(" Verified entry %04X, %d\n",
entry->id, entry->instance);
#endif
}
}
}
}
return errcode;
}
/****************************************************************************
* Name: configdata_delentries
****************************************************************************/
static int configdata_delentries(void)
{
FAR struct configdata_entrydesc_s *entry;
struct config_data_s hdr;
int ndel;
int ret;
int i;
int j;
/* Are there any files to be deleted? */
int nentries = g_nentries - g_ndeleted;
if (nentries < 1)
{
return 0;
}
/* Yes... How many files should we delete? */
ndel = (rand() % nentries) + 1;
/* Now pick which files to delete */
for (i = 0; i < ndel; i++)
{
/* Guess a file index */
int ndx = (rand() % (g_nentries - g_ndeleted));
/* And delete the next undeleted file after that random index */
for (j = ndx + 1; j != ndx; )
{
entry = &g_entries[j];
if (entry->id && !entry->deleted)
{
hdr.id = entry->id;
hdr.instance = entry->instance;
hdr.len = 0;
ret = ioctl(g_fd, CFGDIOC_SETCONFIG, (unsigned long) &hdr);
if (ret < 0)
{
printf("ERROR: Delete %d failed: %d\n", i + 1, errno);
printf(" Entry id: %04X\n", entry->id);
printf(" Entry size: %d\n", entry->len);
printf(" Entry index: %d\n", j);
}
else
{
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf(" Deleted entry %04X\n", entry->id);
#endif
entry->deleted = true;
g_ndeleted++;
g_ntotaldelete++;
break;
}
}
/* Increment the index and test for wrap-around */
if (++j >= CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES)
{
j = 0;
}
}
}
return OK;
}
/****************************************************************************
* Name: configdata_getnextdeleted
****************************************************************************/
static int configdata_getnextdeleted(void)
{
int x;
int nextdeleted = -1;
/* Find next deleted entry */
for (x = 0; x < CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES; x++)
{
if (g_entries[x].deleted)
{
nextdeleted = x;
break;
}
}
return nextdeleted;
}
/****************************************************************************
* Name: configdata_cleardeleted
****************************************************************************/
static void configdata_cleardeleted(void)
{
int nextdeleted;
int x;
while ((nextdeleted = configdata_getnextdeleted()) != -1)
{
/* Find next non-deleted entry after the deleted one */
for (x = nextdeleted + 1;
x < CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES;
x++)
{
if (g_entries[x].id && !g_entries[x].deleted)
{
break;
}
}
/* Test if an non-deleted entry found */
if (x < CONFIG_EXAMPLES_CONFIGDATA_MAXENTRIES)
{
/* Move this entry to the deleted entry location */
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf(" Overwrite entry %d, OLD=%04X NEW=%04X\n",
nextdeleted, g_entries[nextdeleted].id, g_entries[x].id);
#endif
g_entries[nextdeleted] = g_entries[x];
g_entries[x].id = 0;
}
else
{
/* Just remove the entry */
g_entries[nextdeleted].id = 0;
g_entries[nextdeleted].deleted = FALSE;
}
}
g_nentries -= g_ndeleted;
g_ndeleted = 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: configdata_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
unsigned int i;
int ret;
FAR struct mtd_dev_s *mtd;
/* Seed the random number generated */
srand(0x93846);
/* Create and initialize a RAM MTD device instance */
#ifdef CONFIG_EXAMPLES_CONFIGDATA_ARCHINIT
mtd = configdata_archinitialize();
#else
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf("Creating %d byte RAM drive\n", EXAMPLES_CONFIGDATA_BUFSIZE);
#endif
mtd = rammtd_initialize(g_simflash, EXAMPLES_CONFIGDATA_BUFSIZE);
#endif
if (!mtd)
{
printf("ERROR: Failed to create RAM MTD instance\n");
fflush(stdout);
exit(1);
}
/* Initialize to provide CONFIGDATA on an MTD interface */
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf("Registering /dev/config device\n");
#endif
MTD_IOCTL(mtd, MTDIOC_BULKERASE, 0);
ret = mtdconfig_register(mtd);
if (ret < 0)
{
printf("ERROR: /dev/config registration failed: %d\n", -ret);
fflush(stdout);
exit(2);
}
/* Zero out our entry array */
memset(g_entries, 0, sizeof(g_entries));
/* Open the /dev/config device */
g_fd = open("/dev/config", O_RDOK);
if (g_fd == -1)
{
printf("ERROR: Failed to open /dev/config %d\n", -errno);
fflush(stdout);
exit(2);
}
/* Initialize the before memory values */
g_mmbefore = mallinfo();
/* Loop seveal times ... create some config data items, delete them
* randomly, verify them randomly, add new config items.
*/
g_ntests = g_nverified = 0;
g_ntotaldelete = g_ntotalalloc = 0;
#if CONFIG_EXAMPLES_CONFIGDATA_NLOOPS == 0
for (i = 0; ; i++)
#else
for (i = 1; i <= CONFIG_EXAMPLES_CONFIGDATA_NLOOPS; i++)
#endif
{
/* Write config data to the /dev/config device until either (1) all of
* the open file structures are utilized or until (2) CONFIGDATA
* reports an error (hopefully that the /dev/config device is full)
*/
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
printf("\n=== FILLING %u =============================\n", i);
#endif
configdata_fillconfig();
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
printf("Filled /dev/config\n");
printf(" Number of entries: %d\n", g_nentries);
#endif
/* Verify all files entries to FLASH */
ret = configdata_verifyconfig();
if (ret < 0)
{
printf("ERROR: Failed to verify partition\n");
printf(" Number of entries: %d\n", g_nentries);
}
else
{
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf("Verified!\n");
printf(" Number of entries: %d\n", g_nentries);
#endif
#endif
}
/* Delete some entries */
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
printf("\n=== DELETING %u ============================\n", i);
#endif
ret = configdata_delentries();
if (ret < 0)
{
printf("ERROR: Failed to delete enries\n");
printf(" Number of entries: %d\n", g_nentries);
printf(" Number deleted: %d\n", g_ndeleted);
}
else
{
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
printf("Deleted some enries\n");
printf(" Number of entries: %d\n", g_nentries);
printf(" Number deleted: %d\n", g_ndeleted);
#endif
}
/* Verify all files written to FLASH */
ret = configdata_verifyconfig();
if (ret < 0)
{
printf("ERROR: Failed to verify partition\n");
printf(" Number of entries: %d\n", g_nentries);
printf(" Number deleted: %d\n", g_ndeleted);
}
else
{
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
#if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0
printf("Verified!\n");
printf(" Number of entries: %d\n", g_nentries);
printf(" Number deleted: %d\n", g_ndeleted);
#endif
#endif
}
/* Clear deleted entries */
configdata_cleardeleted();
/* Show memory usage */
#ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT
configdata_loopmemusage();
fflush(stdout);
#else
if ((i % EXAMPLES_CONFIGDATA_REPORT) == 0)
{
printf("%u\n", i);
fflush(stdout);
}
#endif
}
#if 0
/* Delete all files then show memory usage again */
configdata_delallfiles();
#endif
configdata_endmemusage();
fflush(stdout);
return 0;
}