altos: Replace ao_xmem functions with direct mem calls
[fw/altos] / src / kernel / ao_gps_report.c
1 /*
2  * Copyright © 2009 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
21 void
22 ao_gps_report(void)
23 {
24         static struct ao_log_record             gps_log;
25         static struct ao_telemetry_location     gps_data;
26         static struct ao_telemetry_satellite    gps_tracking_data;
27         uint8_t date_reported = 0;
28         uint8_t new;
29
30         for (;;) {
31                 while ((new = ao_gps_new) == 0)
32                         ao_sleep(&ao_gps_new);
33                 ao_mutex_get(&ao_gps_mutex);
34                 if (new & AO_GPS_NEW_DATA)
35                         memcpy(&gps_data, &ao_gps_data, sizeof (ao_gps_data));
36                 if (new & AO_GPS_NEW_TRACKING)
37                         memcpy(&gps_tracking_data, &ao_gps_tracking_data, sizeof (ao_gps_tracking_data));
38                 ao_gps_new = 0;
39                 ao_mutex_put(&ao_gps_mutex);
40
41                 if ((new & AO_GPS_NEW_DATA) && (gps_data.flags & AO_GPS_VALID)) {
42                         gps_log.tick = ao_gps_tick;
43                         gps_log.type = AO_LOG_GPS_TIME;
44                         gps_log.u.gps_time.hour = gps_data.hour;
45                         gps_log.u.gps_time.minute = gps_data.minute;
46                         gps_log.u.gps_time.second = gps_data.second;
47                         gps_log.u.gps_time.flags = gps_data.flags;
48                         ao_log_write(&gps_log);
49                         gps_log.type = AO_LOG_GPS_LAT;
50                         gps_log.u.gps_latitude = gps_data.latitude;
51                         ao_log_write(&gps_log);
52                         gps_log.type = AO_LOG_GPS_LON;
53                         gps_log.u.gps_longitude = gps_data.longitude;
54                         ao_log_write(&gps_log);
55                         gps_log.type = AO_LOG_GPS_ALT;
56                         gps_log.u.gps_altitude.altitude_low = gps_data.altitude_low;
57 #if HAS_WIDE_GPS
58                         gps_log.u.gps_altitude.altitude_high = gps_data.altitude_high;
59 #else
60                         gps_log.u.gps_altitude.altitude_high = 0xffff;
61 #endif
62                         ao_log_write(&gps_log);
63                         if (!date_reported && (gps_data.flags & AO_GPS_DATE_VALID)) {
64                                 gps_log.type = AO_LOG_GPS_DATE;
65                                 gps_log.u.gps_date.year = gps_data.year;
66                                 gps_log.u.gps_date.month = gps_data.month;
67                                 gps_log.u.gps_date.day = gps_data.day;
68                                 gps_log.u.gps_date.extra = 0;
69                                 date_reported = ao_log_write(&gps_log);
70                         }
71                 }
72                 if (new & AO_GPS_NEW_TRACKING) {
73                         uint8_t c, n;
74
75                         if ((n = gps_tracking_data.channels) != 0) {
76                                 gps_log.type = AO_LOG_GPS_SAT;
77                                 for (c = 0; c < n; c++)
78                                         if ((gps_log.u.gps_sat.svid = gps_tracking_data.sats[c].svid))
79                                         {
80                                                 gps_log.u.gps_sat.c_n = gps_tracking_data.sats[c].c_n_1;
81                                                 ao_log_write(&gps_log);
82                                         }
83                         }
84                 }
85         }
86 }
87
88 struct ao_task ao_gps_report_task;
89
90 void
91 ao_gps_report_init(void)
92 {
93         ao_add_task(&ao_gps_report_task, ao_gps_report, "gps_report");
94 }