2 * Copyright © 2011 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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 public abstract class AltosTelemetryRecord {
25 abstract public AltosRecord update_state(AltosRecord previous);
27 static boolean cksum(int[] bytes) {
29 for (int i = 1; i < bytes.length - 1; i++)
32 return sum == bytes[bytes.length - 1];
35 final static int PKT_APPEND_STATUS_1_CRC_OK = (1 << 7);
36 final static int PKT_APPEND_STATUS_1_LQI_MASK = (0x7f);
37 final static int PKT_APPEND_STATUS_1_LQI_SHIFT = 0;
39 final static int packet_type_TM_sensor = 0x01;
40 final static int packet_type_Tm_sensor = 0x02;
41 final static int packet_type_Tn_sensor = 0x03;
42 final static int packet_type_configuration = 0x04;
43 final static int packet_type_location = 0x05;
44 final static int packet_type_satellite = 0x06;
45 final static int packet_type_companion = 0x07;
47 static AltosTelemetryRecord parse_hex(String hex) throws ParseException, AltosCRCException {
48 AltosTelemetryRecord r;
52 bytes = Altos.hexbytes(hex);
53 } catch (NumberFormatException ne) {
54 throw new ParseException(ne.getMessage(), 0);
57 /* one for length, one for checksum */
58 if (bytes[0] != bytes.length - 2)
59 throw new ParseException(String.format("invalid length %d != %d\n",
61 bytes.length - 2), 0);
63 throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
65 int rssi = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
66 int status = Altos.uint8(bytes, bytes.length - 2);
68 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
69 throw new AltosCRCException(rssi);
71 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
72 switch (bytes.length) {
73 case Altos.ao_telemetry_standard_len + 4:
74 int type = Altos.uint8(bytes, 4 + 1);
76 case packet_type_TM_sensor:
77 case packet_type_Tm_sensor:
78 case packet_type_Tn_sensor:
79 r = new AltosTelemetryRecordSensor(bytes, rssi);
81 case packet_type_configuration:
82 r = new AltosTelemetryRecordConfiguration(bytes);
84 case packet_type_location:
85 r = new AltosTelemetryRecordLocation(bytes);
87 case packet_type_satellite:
88 r = new AltosTelemetryRecordSatellite(bytes);
90 case packet_type_companion:
91 r = new AltosTelemetryRecordCompanion(bytes);
94 r = new AltosTelemetryRecordRaw(bytes);
98 case Altos.ao_telemetry_0_9_len + 4:
99 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
101 case Altos.ao_telemetry_0_8_len + 4:
102 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
105 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
107 r.received_time = System.currentTimeMillis();
111 public static AltosTelemetryRecord parse(String line) throws ParseException, AltosCRCException {
112 AltosTelemetryRecord r;
114 String[] word = line.split("\\s+");
116 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
118 AltosParse.word(word[i++], "RSSI");
119 throw new AltosCRCException(AltosParse.parse_int(word[i++]));
122 if (word[i].equals("TELEM"))
123 r = parse_hex(word[i+1]);
125 r = new AltosTelemetryRecordLegacy(line);