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