Merge branch 'master' into new-state
[fw/altos] / altoslib / AltosTelemetry.java
1 /*
2  * Copyright © 2010 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_1;
19
20 import java.text.*;
21
22 /*
23  * Telemetry data contents
24  */
25
26 public abstract class AltosTelemetry implements AltosStateUpdate {
27
28         /* All telemetry packets have these fields */
29         public int      tick;
30         public int      serial;
31         public int      rssi;
32         public int      status;
33
34         /* Mark when we received the packet */
35         long            received_time;
36
37         static boolean cksum(int[] bytes) {
38                 int     sum = 0x5a;
39                 for (int i = 1; i < bytes.length - 1; i++)
40                         sum += bytes[i];
41                 sum &= 0xff;
42                 return sum == bytes[bytes.length - 1];
43         }
44
45         public void update_state(AltosState state) {
46         }
47
48         final static int PKT_APPEND_STATUS_1_CRC_OK             = (1 << 7);
49         final static int PKT_APPEND_STATUS_1_LQI_MASK           = (0x7f);
50         final static int PKT_APPEND_STATUS_1_LQI_SHIFT          = 0;
51
52         final static int packet_type_TM_sensor = 0x01;
53         final static int packet_type_Tm_sensor = 0x02;
54         final static int packet_type_Tn_sensor = 0x03;
55         final static int packet_type_configuration = 0x04;
56         final static int packet_type_location = 0x05;
57         final static int packet_type_satellite = 0x06;
58         final static int packet_type_companion = 0x07;
59         final static int packet_type_MM_sensor = 0x08;
60         final static int packet_type_MM_data = 0x09;
61         final static int packet_type_Mini = 0x10;
62         
63         static AltosTelemetry parse_hex(String hex)  throws ParseException, AltosCRCException {
64                 AltosTelemetry  telem = null;
65
66                 int[] bytes;
67                 try {
68                         bytes = AltosLib.hexbytes(hex);
69                 } catch (NumberFormatException ne) {
70                         throw new ParseException(ne.getMessage(), 0);
71                 }
72
73                 /* one for length, one for checksum */
74                 if (bytes[0] != bytes.length - 2)
75                         throw new ParseException(String.format("invalid length %d != %d\n",
76                                                                bytes[0],
77                                                                bytes.length - 2), 0);
78                 if (!cksum(bytes))
79                         throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
80
81                 int     rssi = AltosLib.int8(bytes, bytes.length - 3) / 2 - 74;
82                 int     status = AltosLib.uint8(bytes, bytes.length - 2);
83
84                 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
85                         throw new AltosCRCException(rssi);
86
87                 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
88                 switch (bytes.length) {
89                 case AltosLib.ao_telemetry_standard_len + 4:
90                         int     type = AltosLib.uint8(bytes, 4 + 1);
91 /*
92                         switch (type) {
93                         case packet_type_TM_sensor:
94                         case packet_type_Tm_sensor:
95                         case packet_type_Tn_sensor:
96                                 telem = new AltosTelemetrySensor(bytes);
97                                 break;
98                         case packet_type_configuration:
99                                 telem = new AltosTelemetryConfiguration(bytes);
100                                 break;
101                         case packet_type_location:
102                                 telem = new AltosTelemetryLocation(bytes);
103                                 break;
104                         case packet_type_satellite:
105                                 telem = new AltosTelemetrySatellite(bytes);
106                                 break;
107                         case packet_type_companion:
108                                 telem = new AltosTelemetryCompanion(bytes);
109                                 break;
110                         case packet_type_MM_sensor:
111                                 telem = new AltosTelemetryMegaSensor(bytes);
112                                 break;
113                         case packet_type_MM_data:
114                                 telem = new AltosTelemetryMegaData(bytes);
115                                 break;
116                         default:
117                                 telem = new AltosTelemetryRaw(bytes);
118                                 break;
119                         }
120 */
121                         break;
122                 case AltosLib.ao_telemetry_0_9_len + 4:
123                         telem = new AltosTelemetryLegacy(bytes);
124                         break;
125                 case AltosLib.ao_telemetry_0_8_len + 4:
126                         telem = new AltosTelemetryLegacy(bytes);
127                         break;
128                 default:
129                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
130                 }
131                 if (telem != null) {
132                         telem.received_time = System.currentTimeMillis();
133                         telem.rssi = rssi;
134                         telem.status = status;
135                 }
136                 return telem;
137         }
138
139         public static AltosTelemetry parse(String line) throws ParseException, AltosCRCException {
140                 String[] word = line.split("\\s+");
141                 int i =0;
142
143                 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
144                         i += 2;
145                         AltosParse.word(word[i++], "RSSI");
146                         throw new AltosCRCException(AltosParse.parse_int(word[i++]));
147                 }
148
149                 AltosTelemetry telem;
150
151                 if (word[i].equals("TELEM")) {
152                         telem = parse_hex(word[i+1]);
153                 } else {
154                         telem = new AltosTelemetryLegacy(line);
155                 }
156                 return telem;
157         }
158 }