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_11;
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 static boolean cksum(int[] bytes) {
43 for (int i = 1; i < bytes.length - 1; i++)
46 return sum == bytes[bytes.length - 1];
49 public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
50 cal_data.set_serial(serial());
51 if (listener.state == AltosLib.ao_flight_invalid)
52 listener.set_state(AltosLib.ao_flight_startup);
53 cal_data.set_tick(tick());
54 listener.set_time(cal_data.time());
55 listener.set_rssi(rssi(), status());
56 listener.set_received_time(received_time);
59 final static int PKT_APPEND_STATUS_1_CRC_OK = (1 << 7);
60 final static int PKT_APPEND_STATUS_1_LQI_MASK = (0x7f);
61 final static int PKT_APPEND_STATUS_1_LQI_SHIFT = 0;
63 final static int packet_type_TM_sensor = 0x01;
64 final static int packet_type_Tm_sensor = 0x02;
65 final static int packet_type_Tn_sensor = 0x03;
66 final static int packet_type_configuration = 0x04;
67 final static int packet_type_location = 0x05;
68 final static int packet_type_satellite = 0x06;
69 final static int packet_type_companion = 0x07;
70 final static int packet_type_mega_sensor = 0x08;
71 final static int packet_type_mega_data = 0x09;
72 final static int packet_type_metrum_sensor = 0x0a;
73 final static int packet_type_metrum_data = 0x0b;
74 final static int packet_type_mini2 = 0x10;
75 final static int packet_type_mini3 = 0x11;
77 static AltosTelemetry parse_hex(String hex) throws ParseException, AltosCRCException {
78 AltosTelemetry telem = null;
82 bytes = AltosLib.hexbytes(hex);
83 } catch (NumberFormatException ne) {
84 throw new ParseException(ne.getMessage(), 0);
87 /* one for length, one for checksum */
88 if (bytes[0] != bytes.length - 2)
89 throw new ParseException(String.format("invalid length %d != %d\n",
91 bytes.length - 2), 0);
93 throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
95 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
96 switch (bytes.length) {
97 case AltosLib.ao_telemetry_standard_len + 4:
98 telem = AltosTelemetryStandard.parse_hex(bytes);
100 case AltosLib.ao_telemetry_0_9_len + 4:
101 telem = new AltosTelemetryLegacy(bytes);
103 case AltosLib.ao_telemetry_0_8_len + 4:
104 telem = new AltosTelemetryLegacy(bytes);
107 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
112 public AltosTelemetry() {
113 this.received_time = System.currentTimeMillis();
116 public AltosTelemetry(int[] bytes) throws AltosCRCException {
119 if ((status() & PKT_APPEND_STATUS_1_CRC_OK) == 0)
120 throw new AltosCRCException(rssi());
123 public static AltosTelemetry parse(String line) throws ParseException, AltosCRCException {
124 String[] word = line.split("\\s+");
127 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
129 AltosParse.word(word[i++], "RSSI");
130 throw new AltosCRCException(AltosParse.parse_int(word[i++]));
133 AltosTelemetry telem = null;
135 if (word[i].equals("TELEM")) {
136 telem = parse_hex(word[i+1]);
138 telem = new AltosTelemetryLegacy(line);