altosuilib: Don't match product_altusmetrum for product_basestation or product_altimeter
[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_2;
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                 if (state.state == AltosLib.ao_flight_invalid)
47                         state.set_state(AltosLib.ao_flight_startup);
48                 state.set_serial(serial);
49                 state.set_tick(tick);
50                 state.set_rssi(rssi, status);
51                 state.set_received_time(received_time);
52         }
53
54         final static int PKT_APPEND_STATUS_1_CRC_OK             = (1 << 7);
55         final static int PKT_APPEND_STATUS_1_LQI_MASK           = (0x7f);
56         final static int PKT_APPEND_STATUS_1_LQI_SHIFT          = 0;
57
58         final static int packet_type_TM_sensor = 0x01;
59         final static int packet_type_Tm_sensor = 0x02;
60         final static int packet_type_Tn_sensor = 0x03;
61         final static int packet_type_configuration = 0x04;
62         final static int packet_type_location = 0x05;
63         final static int packet_type_satellite = 0x06;
64         final static int packet_type_companion = 0x07;
65         final static int packet_type_mega_sensor = 0x08;
66         final static int packet_type_mega_data = 0x09;
67         final static int packet_type_metrum_sensor = 0x0a;
68         final static int packet_type_metrum_data = 0x0b;
69         final static int packet_type_mini = 0x10;
70         
71         static AltosTelemetry parse_hex(String hex)  throws ParseException, AltosCRCException {
72                 AltosTelemetry  telem = null;
73
74                 int[] bytes;
75                 try {
76                         bytes = AltosLib.hexbytes(hex);
77                 } catch (NumberFormatException ne) {
78                         throw new ParseException(ne.getMessage(), 0);
79                 }
80
81                 /* one for length, one for checksum */
82                 if (bytes[0] != bytes.length - 2)
83                         throw new ParseException(String.format("invalid length %d != %d\n",
84                                                                bytes[0],
85                                                                bytes.length - 2), 0);
86                 if (!cksum(bytes))
87                         throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
88
89                 int     rssi = AltosLib.int8(bytes, bytes.length - 3) / 2 - 74;
90                 int     status = AltosLib.uint8(bytes, bytes.length - 2);
91
92                 if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0)
93                         throw new AltosCRCException(rssi);
94
95                 /* length, data ..., rssi, status, checksum -- 4 bytes extra */
96                 switch (bytes.length) {
97                 case AltosLib.ao_telemetry_standard_len + 4:
98                         telem = AltosTelemetryStandard.parse_hex(bytes);
99                         break;
100                 case AltosLib.ao_telemetry_0_9_len + 4:
101                         telem = new AltosTelemetryLegacy(bytes);
102                         break;
103                 case AltosLib.ao_telemetry_0_8_len + 4:
104                         telem = new AltosTelemetryLegacy(bytes);
105                         break;
106                 default:
107                         throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0);
108                 }
109                 if (telem != null) {
110                         telem.received_time = System.currentTimeMillis();
111                         telem.rssi = rssi;
112                         telem.status = status;
113                 }
114                 return telem;
115         }
116
117         public static AltosTelemetry parse(String line) throws ParseException, AltosCRCException {
118                 String[] word = line.split("\\s+");
119                 int i =0;
120
121                 if (word[i].equals("CRC") && word[i+1].equals("INVALID")) {
122                         i += 2;
123                         AltosParse.word(word[i++], "RSSI");
124                         throw new AltosCRCException(AltosParse.parse_int(word[i++]));
125                 }
126
127                 AltosTelemetry telem;
128
129                 if (word[i].equals("TELEM")) {
130                         telem = parse_hex(word[i+1]);
131                 } else {
132                         telem = new AltosTelemetryLegacy(line);
133                 }
134                 return telem;
135         }
136 }