Switch from GPLv2 to GPLv2+
[fw/altos] / altoslib / AltosTelemetry.java
1 /*
2  * Copyright © 2010 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 package org.altusmetrum.altoslib_11;
20
21 import java.text.*;
22
23 /*
24  * Telemetry data contents
25  */
26
27 public abstract class AltosTelemetry implements AltosStateUpdate {
28
29         /* All telemetry packets have these fields */
30         public int      tick;
31         public int      serial;
32         public int      rssi;
33         public int      status;
34
35         /* Mark when we received the packet */
36         long            received_time;
37
38         static boolean cksum(int[] bytes) {
39                 int     sum = 0x5a;
40                 for (int i = 1; i < bytes.length - 1; i++)
41                         sum += bytes[i];
42                 sum &= 0xff;
43                 return sum == bytes[bytes.length - 1];
44         }
45
46         public void update_state(AltosState state) {
47                 state.set_serial(serial);
48                 if (state.state() == AltosLib.ao_flight_invalid)
49                         state.set_state(AltosLib.ao_flight_startup);
50                 state.set_tick(tick);
51                 state.set_rssi(rssi, status);
52                 state.set_received_time(received_time);
53         }
54
55         final static int PKT_APPEND_STATUS_1_CRC_OK             = (1 << 7);
56         final static int PKT_APPEND_STATUS_1_LQI_MASK           = (0x7f);
57         final static int PKT_APPEND_STATUS_1_LQI_SHIFT          = 0;
58
59         final static int packet_type_TM_sensor = 0x01;
60         final static int packet_type_Tm_sensor = 0x02;
61         final static int packet_type_Tn_sensor = 0x03;
62         final static int packet_type_configuration = 0x04;
63         final static int packet_type_location = 0x05;
64         final static int packet_type_satellite = 0x06;
65         final static int packet_type_companion = 0x07;
66         final static int packet_type_mega_sensor = 0x08;
67         final static int packet_type_mega_data = 0x09;
68         final static int packet_type_metrum_sensor = 0x0a;
69         final static int packet_type_metrum_data = 0x0b;
70         final static int packet_type_mini = 0x10;
71
72         static AltosTelemetry parse_hex(String hex)  throws ParseException, AltosCRCException {
73                 AltosTelemetry  telem = null;
74
75                 int[] bytes;
76                 try {
77                         bytes = AltosLib.hexbytes(hex);
78                 } catch (NumberFormatException ne) {
79                         throw new ParseException(ne.getMessage(), 0);
80                 }
81
82                 /* one for length, one for checksum */
83                 if (bytes[0] != bytes.length - 2)
84                         throw new ParseException(String.format("invalid length %d != %d\n",
85                                                                bytes[0],
86                                                                bytes.length - 2), 0);
87                 if (!cksum(bytes))
88                         throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
89
90                 int     rssi = AltosLib.int8(bytes, bytes.length - 3) / 2 - 74;
91                 int     status = AltosLib.uint8(bytes, bytes.length - 2);
92
93                 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
94                         throw new AltosCRCException(rssi);
95
96                 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
97                 switch (bytes.length) {
98                 case AltosLib.ao_telemetry_standard_len + 4:
99                         telem = AltosTelemetryStandard.parse_hex(bytes);
100                         break;
101                 case AltosLib.ao_telemetry_0_9_len + 4:
102                         telem = new AltosTelemetryLegacy(bytes);
103                         break;
104                 case AltosLib.ao_telemetry_0_8_len + 4:
105                         telem = new AltosTelemetryLegacy(bytes);
106                         break;
107                 default:
108                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
109                 }
110                 if (telem != null) {
111                         telem.received_time = System.currentTimeMillis();
112                         telem.rssi = rssi;
113                         telem.status = status;
114                 }
115                 return telem;
116         }
117
118         public static int extend_height(AltosState state, int height_16) {
119                 double  compare_height;
120                 int     height = height_16;
121
122                 if (state.gps != null && state.gps.alt != AltosLib.MISSING) {
123                         compare_height = state.gps_height();
124                 } else {
125                         compare_height = state.height();
126                 }
127
128                 if (compare_height != AltosLib.MISSING) {
129                         int     high_bits = (int) Math.floor (compare_height / 65536.0);
130
131                         height = (high_bits << 16) | (height_16 & 0xffff);
132
133                         if (Math.abs(height + 65536 - compare_height) < Math.abs(height - compare_height))
134                                 height += 65536;
135                         else if (Math.abs(height - 65536 - compare_height) < Math.abs(height - compare_height))
136                                 height -= 65536;
137                 }
138                 return height;
139         }
140
141         public static AltosTelemetry parse(String line) throws ParseException, AltosCRCException {
142                 String[] word = line.split("\\s+");
143                 int i =0;
144
145                 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
146                         i += 2;
147                         AltosParse.word(word[i++], "RSSI");
148                         throw new AltosCRCException(AltosParse.parse_int(word[i++]));
149                 }
150
151                 AltosTelemetry telem;
152
153                 if (word[i].equals("TELEM")) {
154                         telem = parse_hex(word[i+1]);
155                 } else {
156                         telem = new AltosTelemetryLegacy(line);
157                 }
158                 return telem;
159         }
160 }