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