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