2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_12;
24 * Telemetry data contents
27 public abstract class AltosTelemetry implements AltosDataProvider {
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); }
34 /* All telemetry packets report these fields in some form */
35 public abstract int serial();
36 public abstract int tick();
38 /* Mark when we received the packet */
41 /* Mark frequency packet was received on */
42 public double frequency = AltosLib.MISSING;
44 static boolean cksum(int[] bytes) {
46 for (int i = 1; i < bytes.length - 1; i++)
49 return sum == bytes[bytes.length - 1];
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);
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;
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;
81 static AltosTelemetry parse_hex(String hex) throws ParseException, AltosCRCException {
82 AltosTelemetry telem = null;
86 bytes = AltosLib.hexbytes(hex);
87 } catch (NumberFormatException ne) {
88 throw new ParseException(ne.getMessage(), 0);
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",
95 bytes.length - 2), 0);
97 throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
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);
104 case AltosLib.ao_telemetry_0_9_len + 4:
105 telem = new AltosTelemetryLegacy(bytes);
107 case AltosLib.ao_telemetry_0_8_len + 4:
108 telem = new AltosTelemetryLegacy(bytes);
111 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
116 public void set_frequency(double frequency) {
117 this.frequency = frequency;
120 public AltosTelemetry() {
121 this.received_time = System.currentTimeMillis();
124 public AltosTelemetry(int[] bytes) throws AltosCRCException {
127 if ((status() & PKT_APPEND_STATUS_1_CRC_OK) == 0)
128 throw new AltosCRCException(rssi());
131 public static AltosTelemetry parse(String line) throws ParseException, AltosCRCException {
132 String[] word = line.split("\\s+");
135 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
137 AltosParse.word(word[i++], "RSSI");
138 throw new AltosCRCException(AltosParse.parse_int(word[i++]));
141 AltosTelemetry telem = null;
143 if (word[i].equals("TELEM")) {
144 telem = parse_hex(word[i+1]);
146 telem = new AltosTelemetryLegacy(line);