altos: Add SDCARD and FAT16 filesystem support
[fw/altos] / src / test / ao_fat_test.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <getopt.h>
23 #include <math.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26
27 #define AO_FAT_TEST
28
29 void
30 ao_mutex_get(uint8_t *mutex)
31 {
32 }
33
34 void
35 ao_mutex_put(uint8_t *mutex)
36 {
37 }
38
39 void
40 ao_panic(uint8_t panic)
41 {
42         printf ("panic %d\n", panic);
43         exit(1);
44 }
45
46 #define AO_PANIC_BUFIO  15
47
48 #define ao_cmd_success  0
49
50 uint8_t ao_cmd_status;
51 uint32_t ao_cmd_lex_u32;
52
53 void
54 ao_cmd_decimal()
55 {
56 }
57
58 #define ao_cmd_register(x)
59
60 struct ao_cmds {
61         void            (*func)(void);
62         const char      *help;
63 };
64
65 int fs_fd;
66
67 uint8_t
68 ao_sdcard_read_block(uint32_t block, uint8_t *data)
69 {
70         lseek(fs_fd, block * 512, 0);
71         return read(fs_fd, data, 512) == 512;
72 }
73
74 uint8_t
75 ao_sdcard_write_block(uint32_t block, uint8_t *data)
76 {
77         lseek(fs_fd, block * 512, 0);
78         return write(fs_fd, data, 512) == 512;
79 }
80
81 void
82 ao_sdcard_init(void)
83 {
84         fs_fd = open("fat.fs", 2);
85 }
86
87 #include "ao_bufio.c"
88 #include "ao_fat.c"
89
90 int
91 main(int argc, char **argv)
92 {
93         uint8_t data[15];
94         int     len;
95         ao_fat_init();
96         ao_fat_test();
97         if (ao_fat_open("DATALOG TXT")) {
98                 printf ("DATALOG.TXT\n");
99                 while ((len = ao_fat_read(data, sizeof (data))) > 0) {
100                         write(1, data, len);
101                 }
102                 ao_fat_close();
103 //              ao_fat_unlink("DATALOG TXT");
104         }
105         if (ao_fat_open("NEWFILE TXT")) {
106                 printf ("NEWFILE.TXT\n");
107                 while ((len = ao_fat_read(data, sizeof (data))) > 0) {
108                         write(1, data, len);
109                 }
110                 ao_fat_close();
111         }
112         if (ao_fat_creat ("NEWFILE TXT")) {
113                 for (len = 0; len < 4095; len++)
114                         ao_fat_write((uint8_t *) "hello, world!\n", 14);
115                 ao_fat_close();
116         }
117         return 0;
118 }