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