fix gps message when position is not correct
[protos/imu.git] / sd_log.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <aversive/pgmspace.h>
5
6 #include "fat.h"
7 #include "fat_config.h"
8 #include "partition.h"
9 #include "sd_raw.h"
10 #include "sd_raw_config.h"
11 #include "sd_log.h"
12
13 static struct fat_file_struct *log_fd = NULL;
14
15 static uint8_t find_file_in_dir(struct fat_fs_struct* fs,
16         struct fat_dir_struct* dd, const char* name,
17         struct fat_dir_entry_struct* dir_entry)
18 {
19         (void)fs;
20
21         while(fat_read_dir(dd, dir_entry)) {
22                 if(strcmp(dir_entry->long_name, name) == 0) {
23                         fat_reset_dir(dd);
24                         return 1;
25                 }
26         }
27
28         return 0;
29 }
30
31 static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
32         struct fat_dir_struct* dd, const char* name)
33 {
34         struct fat_dir_entry_struct file_entry;
35
36         if(!find_file_in_dir(fs, dd, name, &file_entry))
37                 return 0;
38
39         return fat_open_file(fs, &file_entry);
40 }
41
42 /* open the log file on the SD card */
43 int8_t sd_log_open(void)
44 {
45         struct fat_file_struct *fd;
46         struct fat_fs_struct *fs;
47         struct partition_struct *partition ;
48         struct fat_dir_struct *dd;
49         struct fat_dir_entry_struct directory;
50         struct fat_dir_entry_struct file_entry;
51         int16_t i = 0;
52         char name[16];
53
54         /* setup sd card slot */
55         if (!sd_raw_init()) {
56 #if SD_DEBUG
57                 printf_P(PSTR("MMC/SD initialization failed\n"));
58 #endif
59                 return -1;
60         }
61
62         /* open first partition */
63         partition = partition_open(sd_raw_read,
64                 sd_raw_read_interval,
65 #if SD_RAW_WRITE_SUPPORT
66                 sd_raw_write, sd_raw_write_interval,
67 #else
68                 0, 0,
69 #endif
70                 0);
71
72         if (!partition) {
73                 /* If the partition did not open, assume the storage device
74                  * is a "superfloppy", i.e. has no MBR.
75                  */
76                 partition = partition_open(sd_raw_read,
77                         sd_raw_read_interval,
78 #if SD_RAW_WRITE_SUPPORT
79                         sd_raw_write,
80                         sd_raw_write_interval,
81 #else
82                         0,
83                         0,
84 #endif
85                         -1);
86                 if (!partition) {
87 #if SD_DEBUG
88                         printf_P(PSTR("opening partition failed\n"));
89 #endif
90                         return -1;
91                 }
92         }
93
94         /* open file system */
95         fs = fat_open(partition);
96         if (!fs) {
97 #if SD_DEBUG
98                 printf_P(PSTR("opening filesystem failed\n"));
99 #endif
100                 return -1;
101         }
102
103         /* open root directory */
104         fat_get_dir_entry_of_path(fs, "/", &directory);
105         dd = fat_open_dir(fs, &directory);
106         if (!dd) {
107 #if SD_DEBUG
108                 printf_P(PSTR("opening root directory failed\n"));
109 #endif
110                 return -1;
111         }
112
113         printf("choose log file name\n");
114         while (1) {
115                 snprintf(name, sizeof(name), "log%.4d", i++);
116                 if (!find_file_in_dir(fs, dd, name, &file_entry))
117                         break;
118         }
119
120         printf("create log file %s\n", name);
121         if (!fat_create_file(dd, name, &file_entry)) {
122                 printf_P(PSTR("error creating file: "));
123         }
124
125         fd = open_file_in_dir(fs, dd, name);
126         if (!fd) {
127                 printf_P(PSTR("error opening "));
128                 return -1;
129         }
130
131         return 0;
132 }
133
134 /* log output */
135 intptr_t sd_log_write(const void *buffer, uintptr_t buffer_len)
136 {
137         if (log_fd == NULL)
138                 return -1;
139
140         return fat_write_file(log_fd, buffer, buffer_len);
141 }
142
143 uint8_t sd_log_enabled(void)
144 {
145         return log_fd != NULL;
146 }