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 import java.util.HashMap;
24 public class AltosTelemetryRecordRaw implements AltosTelemetryRecord {
30 final static int packet_type_TM_sensor = 0x01;
31 final static int packet_type_Tm_sensor = 0x02;
32 final static int packet_type_Tn_sensor = 0x03;
33 final static int packet_type_configuration = 0x04;
34 final static int packet_type_location = 0x05;
35 final static int packet_type_satellite = 0x06;
36 final static int packet_type_companion = 0x07;
38 final static int PKT_APPEND_STATUS_1_CRC_OK = (1 << 7);
39 final static int PKT_APPEND_STATUS_1_LQI_MASK = (0x7f);
40 final static int PKT_APPEND_STATUS_1_LQI_SHIFT = 0;
42 static boolean cksum(int[] bytes) {
44 for (int i = 1; i < bytes.length - 1; i++)
47 return sum == bytes[bytes.length - 1];
50 public static AltosTelemetryRecord parse (String hex) throws ParseException, AltosCRCException {
51 AltosTelemetryRecord r;
55 bytes = Altos.hexbytes(hex);
56 } catch (NumberFormatException ne) {
57 throw new ParseException(ne.getMessage(), 0);
60 /* one for length, one for checksum */
61 if (bytes[0] != bytes.length - 2)
62 throw new ParseException(String.format("invalid length %d != %d\n",
64 bytes.length - 2), 0);
66 throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
68 int rssi = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
69 int status = Altos.uint8(bytes, bytes.length - 2);
71 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
72 throw new AltosCRCException(rssi);
74 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
75 switch (bytes.length) {
76 case Altos.ao_telemetry_standard_len + 4:
77 int type = Altos.uint8(bytes, 4 + 1);
79 case packet_type_TM_sensor:
80 case packet_type_Tm_sensor:
81 case packet_type_Tn_sensor:
82 r = new AltosTelemetryRecordSensor(bytes, rssi);
84 case packet_type_configuration:
85 r = new AltosTelemetryRecordConfiguration(bytes);
87 case packet_type_location:
88 r = new AltosTelemetryRecordLocation(bytes);
90 case packet_type_satellite:
91 r = new AltosTelemetryRecordSatellite(bytes);
93 case packet_type_companion:
94 r = new AltosTelemetryRecordCompanion(bytes);
97 r = new AltosTelemetryRecordRaw(bytes);
101 case Altos.ao_telemetry_0_9_len + 4:
102 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
104 case Altos.ao_telemetry_0_8_len + 4:
105 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
108 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
113 public int int8(int off) {
114 return Altos.int8(bytes, off + 1);
117 public int uint8(int off) {
118 return Altos.uint8(bytes, off + 1);
121 public int int16(int off) {
122 return Altos.int16(bytes, off + 1);
125 public int uint16(int off) {
126 return Altos.uint16(bytes, off + 1);
129 public int uint32(int off) {
130 return Altos.uint32(bytes, off + 1);
133 public String string(int off, int l) {
134 return Altos.string(bytes, off + 1, l);
137 public AltosTelemetryRecordRaw(int[] in_bytes) {
144 public AltosRecord update_state(AltosRecord previous) {
146 if (previous != null) {
147 next = new AltosRecord(previous);
148 while (tick < previous.tick)
151 next = new AltosRecord();
152 next.serial = serial;