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;
37 final static int PKT_APPEND_STATUS_1_CRC_OK = (1 << 7);
38 final static int PKT_APPEND_STATUS_1_LQI_MASK = (0x7f);
39 final static int PKT_APPEND_STATUS_1_LQI_SHIFT = 0;
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 static AltosTelemetryRecord parse (String hex) throws ParseException, AltosCRCException {
50 AltosTelemetryRecord r;
54 bytes = Altos.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 = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
68 int status = Altos.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 Altos.ao_telemetry_split_len + 4:
76 int type = Altos.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);
86 case packet_type_location:
87 r = new AltosTelemetryRecordLocation(bytes);
89 case packet_type_satellite:
90 r = new AltosTelemetryRecordSatellite(bytes);
93 r = new AltosTelemetryRecordRaw(bytes);
97 case Altos.ao_telemetry_legacy_len + 4:
98 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
101 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
106 public int int8(int off) {
107 return Altos.int8(bytes, off + 1);
110 public int uint8(int off) {
111 return Altos.uint8(bytes, off + 1);
114 public int int16(int off) {
115 return Altos.int16(bytes, off + 1);
118 public int uint16(int off) {
119 return Altos.uint16(bytes, off + 1);
122 public int uint32(int off) {
123 return Altos.uint32(bytes, off + 1);
126 public String string(int off, int l) {
127 return Altos.string(bytes, off + 1, l);
130 public AltosTelemetryRecordRaw(int[] in_bytes) {
137 public AltosRecord update_state(AltosRecord previous) {
139 if (previous != null)
140 next = new AltosRecord(previous);
142 next = new AltosRecord();
143 next.serial = serial;