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_config = 0x04;
34 final static int packet_type_GPS_location = 0x05;
35 final static int packet_type_GPS_satellites = 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 System.out.printf("%d bytes sum 0x%x last byte 0x%x\n",
47 bytes.length, sum, bytes[bytes.length - 1]);
48 return sum == bytes[bytes.length - 1];
51 public static AltosTelemetryRecord parse (String hex) throws ParseException, AltosCRCException {
52 AltosTelemetryRecord r;
56 bytes = Altos.hexbytes(hex);
57 } catch (NumberFormatException ne) {
58 throw new ParseException(ne.getMessage(), 0);
61 /* one for length, one for checksum */
62 if (bytes[0] != bytes.length - 2)
63 throw new ParseException(String.format("invalid length %d != %d\n",
65 bytes.length - 2), 0);
67 throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
69 int rssi = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
70 int status = Altos.uint8(bytes, bytes.length - 2);
72 System.out.printf ("rssi 0x%x = %d status 0x%x\n",
73 Altos.uint8(bytes, bytes.length - 3),
76 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
77 throw new AltosCRCException(rssi);
79 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
80 switch (bytes.length) {
81 case Altos.ao_telemetry_split_len + 4:
82 int type = Altos.uint8(bytes, 4 + 1);
84 case packet_type_TM_sensor:
85 case packet_type_Tm_sensor:
86 case packet_type_Tn_sensor:
87 r = new AltosTelemetryRecordSensor(bytes);
90 r = new AltosTelemetryRecordRaw(bytes);
93 case Altos.ao_telemetry_legacy_len + 4:
94 r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
97 throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
102 public int int8(int off) {
103 return Altos.int8(bytes, off + 1);
106 public int uint8(int off) {
107 return Altos.uint8(bytes, off + 1);
110 public int int16(int off) {
111 return Altos.int16(bytes, off + 1);
114 public int uint16(int off) {
115 return Altos.uint16(bytes, off + 1);
118 public int uint32(int off) {
119 return Altos.uint32(bytes, off + 1);
122 public AltosTelemetryRecordRaw(int[] in_bytes) {
129 public AltosRecord update_state(AltosRecord previous) {
130 if (previous != null)
131 return new AltosRecord(previous);
133 return new AltosRecord();