Add battfs test dir.
[bertos.git] / app / battfs / battfs_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id: demo.c 18242 2007-10-08 17:35:23Z marco $
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * \brief BattFs Test.
38  */
39
40 #include <cfg/debug.h>
41 #include <fs/battfs.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 FILE *fp;
48 const char test_filename[]="battfs_disk.bin";
49 const char *filename;
50
51 #define PAGE_SIZE 128
52 static bool disk_open(struct BattFsSuper *d)
53 {
54         fp = fopen(filename, "r+b");
55         ASSERT(fp);
56         fseek(fp, 0, SEEK_END);
57         d->page_size = PAGE_SIZE;
58         d->page_count = ftell(fp) / d->page_size;
59         d->disk_size = d->page_size * d->page_count;
60         d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
61         TRACEMSG("page_size:%d, page_count:%d, disk_size:%d\n", d->page_size, d->page_count, d->disk_size);
62         return (fp && d->page_array);
63 }
64
65 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
66 {
67         TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
68         fseek(fp, page * d->page_size + addr, SEEK_SET);
69         return fread(buf, 1, size, fp);
70 }
71
72 static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
73 {
74         TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
75         fseek(fp, page * d->page_size + addr, SEEK_SET);
76         return fwrite(buf, 1, size, fp);
77 }
78
79 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
80 {
81         TRACEMSG("page:%d\n", page);
82         fseek(fp, page * d->page_size, SEEK_SET);
83
84         for (int i = 0; i < d->page_size; i++)
85                 if (fputc(0xff, fp) == EOF)
86                         return false;
87         return true;
88 }
89
90 static bool disk_close(struct BattFsSuper *d)
91 {
92         TRACE;
93         free(d->page_array);
94         return (fclose(fp) != EOF);
95 }
96
97 int main(int argc, char *argv[])
98 {
99         if (argc < 2)
100         {
101                 FILE *fpt = fopen(test_filename, "w+");
102                 for (int i = 0; i < 32768; i++)
103                         fputc(0xff, fpt);
104                 fclose(fpt);
105                 filename = test_filename;
106         }
107         else
108                 filename = argv[1];
109
110         BattFsSuper disk;
111         disk.open = disk_open;
112         disk.read = disk_page_read;
113         disk.write = disk_page_write;
114         disk.erase = disk_page_erase;
115         disk.close = disk_close;
116
117         if (battfs_init(&disk))
118         {
119                 kprintf("page_array:\n");
120                 for (pgcnt_t i = 0; i < disk.page_count; i++)
121                 {
122                         if (!(i % 16))
123                                 kputchar('\n');
124                         kprintf("%04d ", disk.page_array[i]);
125                 }
126                 kputchar('\n');
127                 return 0;
128         }
129         else
130                 return 1;
131 }