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