altosui: Parse remaining standard telemetry packets
[fw/altos] / altosui / AltosTelemetryRecordRaw.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.lang.*;
21 import java.text.*;
22 import java.util.HashMap;
23
24 public class AltosTelemetryRecordRaw implements AltosTelemetryRecord {
25         int[]   bytes;
26         int     serial;
27         int     tick;
28         int     type;
29
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         
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;
40
41         static boolean cksum(int[] bytes) {
42                 int     sum = 0x5a;
43                 for (int i = 1; i < bytes.length - 1; i++)
44                         sum += bytes[i];
45                 sum &= 0xff;
46                 return sum == bytes[bytes.length - 1];
47         }
48
49         public static AltosTelemetryRecord parse (String hex) throws ParseException, AltosCRCException {
50                 AltosTelemetryRecord    r;
51
52                 int[] bytes;
53                 try {
54                         bytes = Altos.hexbytes(hex);
55                 } catch (NumberFormatException ne) {
56                         throw new ParseException(ne.getMessage(), 0);
57                 }
58
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",
62                                                                bytes[0],
63                                                                bytes.length - 2), 0);
64                 if (!cksum(bytes))
65                         throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
66
67                 int     rssi = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
68                 int     status = Altos.uint8(bytes, bytes.length - 2);
69
70                 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
71                         throw new AltosCRCException(rssi);
72
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);
77                         switch (type) {
78                         case packet_type_TM_sensor:
79                         case packet_type_Tm_sensor:
80                         case packet_type_Tn_sensor:
81                                 r = new AltosTelemetryRecordSensor(bytes, rssi);
82                                 break;
83                         case packet_type_configuration:
84                                 r = new AltosTelemetryRecordConfiguration(bytes);
85                                 break;
86                         case packet_type_location:
87                                 r = new AltosTelemetryRecordLocation(bytes);
88                                 break;
89                         case packet_type_satellite:
90                                 r = new AltosTelemetryRecordSatellite(bytes);
91                                 break;
92                         default:
93                                 r = new AltosTelemetryRecordRaw(bytes);
94                                 break;
95                         }
96                         break;
97                 case Altos.ao_telemetry_legacy_len + 4:
98                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
99                         break;
100                 default:
101                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
102                 }
103                 return r;
104         }
105
106         public int int8(int off) {
107                 return Altos.int8(bytes, off + 1);
108         }
109
110         public int uint8(int off) {
111                 return Altos.uint8(bytes, off + 1);
112         }
113
114         public int int16(int off) {
115                 return Altos.int16(bytes, off + 1);
116         }
117
118         public int uint16(int off) {
119                 return Altos.uint16(bytes, off + 1);
120         }
121
122         public int uint32(int off) {
123                 return Altos.uint32(bytes, off + 1);
124         }
125
126         public String string(int off, int l) {
127                 return Altos.string(bytes, off + 1, l);
128         }
129
130         public AltosTelemetryRecordRaw(int[] in_bytes) {
131                 bytes = in_bytes;
132                 serial = uint16(0);
133                 tick   = uint16(2);
134                 type   = uint8(4);
135         }
136
137         public AltosRecord update_state(AltosRecord previous) {
138                 AltosRecord     next;
139                 if (previous != null)
140                         next = new AltosRecord(previous);
141                 else
142                         next = new AltosRecord();
143                 next.serial = serial;
144                 next.tick = tick;
145                 return next;
146         }
147 }