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; 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;
23 * Telemetry data contents
26 public abstract class AltosTelemetry implements AltosStateUpdate {
28 /* All telemetry packets have these fields */
34 /* Mark when we received the packet */
37 static boolean cksum(int[] bytes) {
39 for (int i = 1; i < bytes.length - 1; i++)
42 return sum == bytes[bytes.length - 1];
45 public void update_state(AltosState state) {
46 state.set_serial(serial);
48 state.set_rssi(rssi, status);
49 state.set_received_time(received_time);
52 final static int PKT_APPEND_STATUS_1_CRC_OK = (1 << 7);
53 final static int PKT_APPEND_STATUS_1_LQI_MASK = (0x7f);
54 final static int PKT_APPEND_STATUS_1_LQI_SHIFT = 0;
56 final static int packet_type_TM_sensor = 0x01;
57 final static int packet_type_Tm_sensor = 0x02;
58 final static int packet_type_Tn_sensor = 0x03;
59 final static int packet_type_configuration = 0x04;
60 final static int packet_type_location = 0x05;
61 final static int packet_type_satellite = 0x06;
62 final static int packet_type_companion = 0x07;
63 final static int packet_type_mega_sensor = 0x08;
64 final static int packet_type_mega_data = 0x09;
65 final static int packet_type_metrum_sensor = 0x0a;
66 final static int packet_type_metrum_data = 0x0b;
67 final static int packet_type_mini = 0x10;
69 static AltosTelemetry parse_hex(String hex) throws ParseException, AltosCRCException {
70 AltosTelemetry telem = null;
74 bytes = AltosLib.hexbytes(hex);
75 } catch (NumberFormatException ne) {
76 throw new ParseException(ne.getMessage(), 0);
79 /* one for length, one for checksum */
80 if (bytes[0] != bytes.length - 2)
81 throw new ParseException(String.format("invalid length %d != %d\n",
83 bytes.length - 2), 0);
85 throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
87 int rssi = AltosLib.int8(bytes, bytes.length - 3) / 2 - 74;
88 int status = AltosLib.uint8(bytes, bytes.length - 2);
90 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
91 throw new AltosCRCException(rssi);
93 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
94 switch (bytes.length) {
95 case AltosLib.ao_telemetry_standard_len + 4:
96 telem = AltosTelemetryStandard.parse_hex(bytes);
98 case AltosLib.ao_telemetry_0_9_len + 4:
99 telem = new AltosTelemetryLegacy(bytes);
101 case AltosLib.ao_telemetry_0_8_len + 4:
102 telem = new AltosTelemetryLegacy(bytes);
105 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
108 telem.received_time = System.currentTimeMillis();
110 telem.status = status;
115 public static AltosTelemetry parse(String line) throws ParseException, AltosCRCException {
116 String[] word = line.split("\\s+");
119 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
121 AltosParse.word(word[i++], "RSSI");
122 throw new AltosCRCException(AltosParse.parse_int(word[i++]));
125 AltosTelemetry telem;
127 if (word[i].equals("TELEM")) {
128 telem = parse_hex(word[i+1]);
130 telem = new AltosTelemetryLegacy(line);