altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / drivers / ao_log_fat.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include "ao_log.h"
21 #include "ao_fat.h"
22
23 static uint8_t  log_year, log_month, log_day;
24 static uint8_t  log_open;
25 static int8_t   log_fd;
26 static uint8_t  log_mutex;
27
28 static void
29 ao_log_open(void)
30 {
31         static char     name[12];
32         int8_t  status;
33
34         sprintf(name,"%04d%02d%02dLOG", 2000 + log_year, log_month, log_day);
35         status = ao_fat_open(name, AO_FAT_OPEN_WRITE);
36         if (status >= 0) {
37                 log_fd = status;
38                 ao_fat_seek(log_fd, 0, AO_FAT_SEEK_END);
39                 log_open = 1;
40         } else if (status == -AO_FAT_ENOENT) {
41                 status = ao_fat_creat(name);
42                 if (status >= 0) {
43                         log_fd = status;
44                         log_open = 1;
45                 }
46         } 
47 }
48
49 static void
50 ao_log_close(void)
51 {
52         if (log_open) {
53                 log_open = 0;
54                 ao_fat_close(log_fd);
55                 log_fd = -1;
56         }
57 }
58
59 uint8_t
60 ao_log_full(void)
61 {
62         return ao_fat_full();
63 }
64
65 uint8_t
66 ao_log_mega(struct ao_log_mega *log)
67 {
68         uint8_t wrote = 0;
69         ao_mutex_get(&log_mutex);
70         if (log->type == AO_LOG_GPS_TIME) {
71                 if (log_open &&
72                     (log_year != log->u.gps.year ||
73                      log_month != log->u.gps.month ||
74                      log_day != log->u.gps.day)) {
75                         ao_log_close();
76                 }
77                 if (!log_open) {
78                         log_year = log->u.gps.year;
79                         log_month = log->u.gps.month;
80                         log_day = log->u.gps.day;
81                         ao_log_open();
82                 }
83         }
84         if (log_open) {
85                 wrote = ao_fat_write(log_fd, log, sizeof (*log)) == AO_FAT_SUCCESS;
86                 ao_fat_sync();
87         }
88         ao_mutex_put(&log_mutex);
89         return wrote;
90 }
91
92 void
93 ao_log_flush(void)
94 {
95         ao_fat_sync();
96 }