altos/altosui: Add pad orientation configure option
[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_standard_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_0_9_len + 4:
98                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
99                         break;
100                 case Altos.ao_telemetry_0_8_len + 4:
101                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
102                         break;
103                 default:
104                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
105                 }
106                 return r;
107         }
108
109         public int int8(int off) {
110                 return Altos.int8(bytes, off + 1);
111         }
112
113         public int uint8(int off) {
114                 return Altos.uint8(bytes, off + 1);
115         }
116
117         public int int16(int off) {
118                 return Altos.int16(bytes, off + 1);
119         }
120
121         public int uint16(int off) {
122                 return Altos.uint16(bytes, off + 1);
123         }
124
125         public int uint32(int off) {
126                 return Altos.uint32(bytes, off + 1);
127         }
128
129         public String string(int off, int l) {
130                 return Altos.string(bytes, off + 1, l);
131         }
132
133         public AltosTelemetryRecordRaw(int[] in_bytes) {
134                 bytes = in_bytes;
135                 serial = uint16(0);
136                 tick   = uint16(2);
137                 type   = uint8(4);
138         }
139
140         public AltosRecord update_state(AltosRecord previous) {
141                 AltosRecord     next;
142                 if (previous != null)
143                         next = new AltosRecord(previous);
144                 else
145                         next = new AltosRecord();
146                 next.serial = serial;
147                 next.tick = tick;
148                 return next;
149         }
150 }