altos: Support staging by going back to boost as needed
[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         final static int packet_type_companion = 0x07;
37         
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;
41
42         static boolean cksum(int[] bytes) {
43                 int     sum = 0x5a;
44                 for (int i = 1; i < bytes.length - 1; i++)
45                         sum += bytes[i];
46                 sum &= 0xff;
47                 return sum == bytes[bytes.length - 1];
48         }
49
50         public static AltosTelemetryRecord parse (String hex) throws ParseException, AltosCRCException {
51                 AltosTelemetryRecord    r;
52
53                 int[] bytes;
54                 try {
55                         bytes = Altos.hexbytes(hex);
56                 } catch (NumberFormatException ne) {
57                         throw new ParseException(ne.getMessage(), 0);
58                 }
59
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",
63                                                                bytes[0],
64                                                                bytes.length - 2), 0);
65                 if (!cksum(bytes))
66                         throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
67
68                 int     rssi = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
69                 int     status = Altos.uint8(bytes, bytes.length - 2);
70
71                 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
72                         throw new AltosCRCException(rssi);
73
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);
78                         switch (type) {
79                         case packet_type_TM_sensor:
80                         case packet_type_Tm_sensor:
81                         case packet_type_Tn_sensor:
82                                 r = new AltosTelemetryRecordSensor(bytes, rssi);
83                                 break;
84                         case packet_type_configuration:
85                                 r = new AltosTelemetryRecordConfiguration(bytes);
86                                 break;
87                         case packet_type_location:
88                                 r = new AltosTelemetryRecordLocation(bytes);
89                                 break;
90                         case packet_type_satellite:
91                                 r = new AltosTelemetryRecordSatellite(bytes);
92                                 break;
93                         case packet_type_companion:
94                                 r = new AltosTelemetryRecordCompanion(bytes);
95                                 break;
96                         default:
97                                 r = new AltosTelemetryRecordRaw(bytes);
98                                 break;
99                         }
100                         break;
101                 case Altos.ao_telemetry_0_9_len + 4:
102                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
103                         break;
104                 case Altos.ao_telemetry_0_8_len + 4:
105                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
106                         break;
107                 default:
108                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
109                 }
110                 return r;
111         }
112
113         public int int8(int off) {
114                 return Altos.int8(bytes, off + 1);
115         }
116
117         public int uint8(int off) {
118                 return Altos.uint8(bytes, off + 1);
119         }
120
121         public int int16(int off) {
122                 return Altos.int16(bytes, off + 1);
123         }
124
125         public int uint16(int off) {
126                 return Altos.uint16(bytes, off + 1);
127         }
128
129         public int uint32(int off) {
130                 return Altos.uint32(bytes, off + 1);
131         }
132
133         public String string(int off, int l) {
134                 return Altos.string(bytes, off + 1, l);
135         }
136
137         public AltosTelemetryRecordRaw(int[] in_bytes) {
138                 bytes = in_bytes;
139                 serial = uint16(0);
140                 tick   = uint16(2);
141                 type   = uint8(4);
142         }
143
144         public AltosRecord update_state(AltosRecord previous) {
145                 AltosRecord     next;
146                 if (previous != null) {
147                         next = new AltosRecord(previous);
148                         while (tick < previous.tick)
149                                 tick += 65536;
150                 } else
151                         next = new AltosRecord();
152                 next.serial = serial;
153                 next.tick = tick;
154                 return next;
155         }
156 }