altos: Add logging and telem to telegps
[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; 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 "ao.h"
19 #include "ao_log.h"
20 #include "ao_fat.h"
21
22 static uint8_t  log_year, log_month, log_day;
23 static uint8_t  log_running;
24 static uint8_t  log_mutex;
25
26 static void
27 ao_log_open(void)
28 {
29         char    name[12];
30
31         sprintf(name,"%04d%02d%02dLOG", 2000 + log_year, log_month, log_day);
32         if (ao_fat_open(name, AO_FAT_OPEN_WRITE) == AO_FAT_SUCCESS)
33                 log_running = 1;
34 }
35
36 static void
37 ao_log_close(void)
38 {
39         log_running = 0;
40         ao_fat_close();
41 }
42
43 uint8_t
44 ao_log_full(void)
45 {
46         return ao_fat_full();
47 }
48
49 uint8_t
50 ao_log_mega(struct ao_log_mega *log)
51 {
52         uint8_t wrote = 0;
53         ao_mutex_get(&log_mutex);
54         if (log->type == AO_LOG_GPS_TIME) {
55                 if (log_running &&
56                     (log_year != log->u.gps.year ||
57                      log_month != log->u.gps.month ||
58                      log_day != log->u.gps.day)) {
59                         ao_log_close();
60                 }
61                 if (!log_running) {
62                         log_year = log->u.gps.year;
63                         log_month = log->u.gps.month;
64                         log_day = log->u.gps.day;
65                         ao_log_open();
66                 }
67         }
68         if (log_running) {
69                 wrote = ao_fat_write(log, sizeof (*log)) == AO_FAT_SUCCESS;
70                 ao_fat_sync();
71         }
72         ao_mutex_put(&log_mutex);
73         return wrote;
74 }