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