altosui: Output recorded clock tick in CSV files
[fw/altos] / altosui / AltosTelemetryRecord.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 import java.lang.*;
20 import java.text.*;
21
22 public abstract class AltosTelemetryRecord {
23
24         long    received_time;
25         abstract public AltosRecord update_state(AltosRecord previous);
26
27         static boolean cksum(int[] bytes) {
28                 int     sum = 0x5a;
29                 for (int i = 1; i < bytes.length - 1; i++)
30                         sum += bytes[i];
31                 sum &= 0xff;
32                 return sum == bytes[bytes.length - 1];
33         }
34
35         final static int PKT_APPEND_STATUS_1_CRC_OK             = (1 << 7);
36         final static int PKT_APPEND_STATUS_1_LQI_MASK           = (0x7f);
37         final static int PKT_APPEND_STATUS_1_LQI_SHIFT          = 0;
38
39         final static int packet_type_TM_sensor = 0x01;
40         final static int packet_type_Tm_sensor = 0x02;
41         final static int packet_type_Tn_sensor = 0x03;
42         final static int packet_type_configuration = 0x04;
43         final static int packet_type_location = 0x05;
44         final static int packet_type_satellite = 0x06;
45         final static int packet_type_companion = 0x07;
46         
47         static AltosTelemetryRecord parse_hex(String hex)  throws ParseException, AltosCRCException {
48                 AltosTelemetryRecord    r;
49
50                 int[] bytes;
51                 try {
52                         bytes = Altos.hexbytes(hex);
53                 } catch (NumberFormatException ne) {
54                         throw new ParseException(ne.getMessage(), 0);
55                 }
56
57                 /* one for length, one for checksum */
58                 if (bytes[0] != bytes.length - 2)
59                         throw new ParseException(String.format("invalid length %d != %d\n",
60                                                                bytes[0],
61                                                                bytes.length - 2), 0);
62                 if (!cksum(bytes))
63                         throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
64
65                 int     rssi = Altos.int8(bytes, bytes.length - 3) / 2 - 74;
66                 int     status = Altos.uint8(bytes, bytes.length - 2);
67
68                 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
69                         throw new AltosCRCException(rssi);
70
71                 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
72                 switch (bytes.length) {
73                 case Altos.ao_telemetry_standard_len + 4:
74                         int     type = Altos.uint8(bytes, 4 + 1);
75                         switch (type) {
76                         case packet_type_TM_sensor:
77                         case packet_type_Tm_sensor:
78                         case packet_type_Tn_sensor:
79                                 r = new AltosTelemetryRecordSensor(bytes, rssi);
80                                 break;
81                         case packet_type_configuration:
82                                 r = new AltosTelemetryRecordConfiguration(bytes);
83                                 break;
84                         case packet_type_location:
85                                 r = new AltosTelemetryRecordLocation(bytes);
86                                 break;
87                         case packet_type_satellite:
88                                 r = new AltosTelemetryRecordSatellite(bytes);
89                                 break;
90                         case packet_type_companion:
91                                 r = new AltosTelemetryRecordCompanion(bytes);
92                                 break;
93                         default:
94                                 r = new AltosTelemetryRecordRaw(bytes);
95                                 break;
96                         }
97                         break;
98                 case Altos.ao_telemetry_0_9_len + 4:
99                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
100                         break;
101                 case Altos.ao_telemetry_0_8_len + 4:
102                         r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
103                         break;
104                 default:
105                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
106                 }
107                 r.received_time = System.currentTimeMillis();
108                 return r;
109         }
110
111         public static AltosTelemetryRecord parse(String line) throws ParseException, AltosCRCException {
112                 AltosTelemetryRecord    r;
113
114                 String[] word = line.split("\\s+");
115                 int i =0;
116                 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
117                         i += 2;
118                         AltosParse.word(word[i++], "RSSI");
119                         throw new AltosCRCException(AltosParse.parse_int(word[i++]));
120                 }
121
122                 if (word[i].equals("TELEM"))
123                         r = parse_hex(word[i+1]);
124                 else
125                         r = new AltosTelemetryRecordLegacy(line);
126                 return r;
127         }
128 }