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.
18 package org.altusmetrum.altoslib_1;
21 public abstract class AltosTelemetryRecord {
24 abstract public AltosRecord update_state(AltosRecord previous);
26 static boolean cksum(int[] bytes) {
28 for (int i = 1; i < bytes.length - 1; i++)
31 return sum == bytes[bytes.length - 1];
34 final static int PKT_APPEND_STATUS_1_CRC_OK = (1 << 7);
35 final static int PKT_APPEND_STATUS_1_LQI_MASK = (0x7f);
36 final static int PKT_APPEND_STATUS_1_LQI_SHIFT = 0;
38 final static int packet_type_TM_sensor = 0x01;
39 final static int packet_type_Tm_sensor = 0x02;
40 final static int packet_type_Tn_sensor = 0x03;
41 final static int packet_type_configuration = 0x04;
42 final static int packet_type_location = 0x05;
43 final static int packet_type_satellite = 0x06;
44 final static int packet_type_companion = 0x07;
45 final static int packet_type_MM_sensor = 0x08;
46 final static int packet_type_MM_data = 0x09;
47 final static int packet_type_Mini = 0x10;
49 static AltosTelemetryRecord parse_hex(String hex) throws ParseException, AltosCRCException {
50 AltosTelemetryRecord r;
54 bytes = AltosLib.hexbytes(hex);
55 } catch (NumberFormatException ne) {
56 throw new ParseException(ne.getMessage(), 0);
59 /* one for length, one for checksum */
60 if (bytes[0] != bytes.length - 2)
61 throw new ParseException(String.format("invalid length %d != %d\n",
63 bytes.length - 2), 0);
65 throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
67 int rssi = AltosLib.int8(bytes, bytes.length - 3) / 2 - 74;
68 int status = AltosLib.uint8(bytes, bytes.length - 2);
70 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
71 throw new AltosCRCException(rssi);
73 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
74 switch (bytes.length) {
75 case AltosLib.ao_telemetry_standard_len + 4:
76 int type = AltosLib.uint8(bytes, 4 + 1);
78 case packet_type_TM_sensor:
79 case packet_type_Tm_sensor:
80 case packet_type_Tn_sensor:
81 r = new AltosTelemetryRecordSensor(bytes, rssi);
83 case packet_type_configuration:
84 r = new AltosTelemetryRecordConfiguration(bytes, rssi);
86 case packet_type_location:
87 r = new AltosTelemetryRecordLocation(bytes, rssi);
89 case packet_type_satellite:
90 r = new AltosTelemetryRecordSatellite(bytes, rssi);
92 case packet_type_companion:
93 r = new AltosTelemetryRecordCompanion(bytes, rssi);
95 case packet_type_MM_sensor:
96 r = new AltosTelemetryRecordMegaSensor(bytes, rssi);
98 case packet_type_MM_data:
99 r = new AltosTelemetryRecordMegaData(bytes, rssi);
102 r = new AltosTelemetryRecordRaw(bytes, rssi);
106 case AltosLib.ao_telemetry_0_9_len + 4:
107 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
109 case AltosLib.ao_telemetry_0_8_len + 4:
110 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
113 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
115 r.received_time = System.currentTimeMillis();
119 public static AltosTelemetryRecord parse(String line) throws ParseException, AltosCRCException {
120 AltosTelemetryRecord r;
122 String[] word = line.split("\\s+");
124 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
126 AltosParse.word(word[i++], "RSSI");
127 throw new AltosCRCException(AltosParse.parse_int(word[i++]));
130 if (word[i].equals("TELEM"))
131 r = parse_hex(word[i+1]);
133 r = new AltosTelemetryRecordLegacy(line);