altosui: Support raw telemetry from TeleDongle
[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_config = 0x04;
34         final static int packet_type_GPS_location = 0x05;
35         final static int packet_type_GPS_satellites = 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                 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];
49         }
50
51         public static AltosTelemetryRecord parse (String hex) throws ParseException, AltosCRCException {
52                 AltosTelemetryRecord    r;
53
54                 int[] bytes;
55                 try {
56                         bytes = Altos.hexbytes(hex);
57                 } catch (NumberFormatException ne) {
58                         throw new ParseException(ne.getMessage(), 0);
59                 }
60
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",
64                                                                bytes[0],
65                                                                bytes.length - 2), 0);
66                 if (!cksum(bytes))
67                         throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
68
69                 int     rssi = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
70                 int     status = Altos.uint8(bytes, bytes.length - 2);
71
72                 System.out.printf ("rssi 0x%x = %d status 0x%x\n",
73                                    Altos.uint8(bytes, bytes.length - 3),
74                                    rssi, status);
75
76                 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
77                         throw new AltosCRCException(rssi);
78
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);
83                         switch (type) {
84                         case packet_type_TM_sensor:
85                         case packet_type_Tm_sensor:
86                         case packet_type_Tn_sensor:
87                                 r = new AltosTelemetryRecordSensor(bytes);
88                                 break;
89                         default:
90                                 r = new AltosTelemetryRecordRaw(bytes);
91                                 break;
92                         }
93                 case Altos.ao_telemetry_legacy_len + 4:
94                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
95                         break;
96                 default:
97                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
98                 }
99                 return r;
100         }
101
102         public int int8(int off) {
103                 return Altos.int8(bytes, off + 1);
104         }
105
106         public int uint8(int off) {
107                 return Altos.uint8(bytes, off + 1);
108         }
109
110         public int int16(int off) {
111                 return Altos.int16(bytes, off + 1);
112         }
113
114         public int uint16(int off) {
115                 return Altos.uint16(bytes, off + 1);
116         }
117
118         public int uint32(int off) {
119                 return Altos.uint32(bytes, off + 1);
120         }
121
122         public AltosTelemetryRecordRaw(int[] in_bytes) {
123                 bytes = in_bytes;
124                 serial = uint16(0);
125                 tick   = uint16(2);
126                 type   = uint8(4);
127         }
128
129         public AltosRecord update_state(AltosRecord previous) {
130                 if (previous != null)
131                         return new AltosRecord(previous);
132                 else
133                         return new AltosRecord();
134         }
135 }