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