altos: Define ao_log_mutex in ao_log.c rather than every log product
[fw/altos] / src / kernel / ao_log_gps.c
1 /*
2  * Copyright © 2014 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_log_gps.h>
21 #include <ao_data.h>
22 #include <ao_flight.h>
23 #include <ao_distance.h>
24 #include <ao_tracker.h>
25
26 static __xdata struct ao_log_gps log;
27
28 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEGPS;
29
30 static uint8_t
31 ao_log_csum(__xdata uint8_t *b) __reentrant
32 {
33         uint8_t sum = 0x5a;
34         uint8_t i;
35
36         for (i = 0; i < sizeof (struct ao_log_gps); i++)
37                 sum += *b++;
38         return -sum;
39 }
40
41 uint8_t
42 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant
43 {
44         uint8_t wrote = 0;
45         /* set checksum */
46         log->csum = 0;
47         log->csum = ao_log_csum((__xdata uint8_t *) log);
48         ao_mutex_get(&ao_log_mutex); {
49                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
50                         ao_log_stop();
51                 if (ao_log_running) {
52                         wrote = 1;
53                         ao_storage_write(ao_log_current_pos,
54                                          log,
55                                          sizeof (struct ao_log_gps));
56                         ao_log_current_pos += sizeof (struct ao_log_gps);
57                 }
58         } ao_mutex_put(&ao_log_mutex);
59         return wrote;
60 }
61
62 void
63 ao_log_gps_flight(void)
64 {
65         log.type = AO_LOG_FLIGHT;
66         log.tick = ao_time();
67         log.u.flight.flight = ao_flight_number;
68         ao_log_gps(&log);
69 }
70
71 void
72 ao_log_gps_data(uint16_t tick, struct ao_telemetry_location *gps_data)
73 {
74         log.tick = tick;
75         log.type = AO_LOG_GPS_TIME;
76         log.u.gps.latitude = gps_data->latitude;
77         log.u.gps.longitude = gps_data->longitude;
78         log.u.gps.altitude = gps_data->altitude;
79
80         log.u.gps.hour = gps_data->hour;
81         log.u.gps.minute = gps_data->minute;
82         log.u.gps.second = gps_data->second;
83         log.u.gps.flags = gps_data->flags;
84         log.u.gps.year = gps_data->year;
85         log.u.gps.month = gps_data->month;
86         log.u.gps.day = gps_data->day;
87         log.u.gps.course = gps_data->course;
88         log.u.gps.ground_speed = gps_data->ground_speed;
89         log.u.gps.climb_rate = gps_data->climb_rate;
90         log.u.gps.pdop = gps_data->pdop;
91         log.u.gps.hdop = gps_data->hdop;
92         log.u.gps.vdop = gps_data->vdop;
93         log.u.gps.mode = gps_data->mode;
94         ao_log_gps(&log);
95 }
96
97 void
98 ao_log_gps_tracking(uint16_t tick, struct ao_telemetry_satellite *gps_tracking_data)
99 {
100         uint8_t c, n, i;
101
102         log.tick = tick;
103         log.type = AO_LOG_GPS_SAT;
104         i = 0;
105         n = gps_tracking_data->channels;
106         for (c = 0; c < n; c++)
107                 if ((log.u.gps_sat.sats[i].svid = gps_tracking_data->sats[c].svid))
108                 {
109                         log.u.gps_sat.sats[i].c_n = gps_tracking_data->sats[c].c_n_1;
110                         i++;
111                         if (i >= 12)
112                                 break;
113                 }
114         log.u.gps_sat.channels = i;
115         ao_log_gps(&log);
116 }
117
118 static uint8_t
119 ao_log_dump_check_data(void)
120 {
121         if (ao_log_csum((uint8_t *) &log) != 0)
122                 return 0;
123         return 1;
124 }
125
126 uint16_t
127 ao_log_flight(uint8_t slot)
128 {
129         if (!ao_storage_read(ao_log_pos(slot),
130                              &log,
131                              sizeof (struct ao_log_gps)))
132                 return 0;
133
134         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
135                 return log.u.flight.flight;
136         return 0;
137 }