altosdroid: Add USB support for TeleDongle/TeleBT
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryReader.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19
20 package org.altusmetrum.AltosDroid;
21
22 import java.text.*;
23 import java.io.*;
24 import java.util.concurrent.*;
25 import android.util.Log;
26 import android.os.Handler;
27
28 import org.altusmetrum.altoslib_6.*;
29
30
31 public class TelemetryReader extends Thread {
32
33         private static final String TAG = "TelemetryReader";
34         private static final boolean D = true;
35
36         int         crc_errors;
37
38         Handler     handler;
39
40         AltosLink   link;
41         AltosState  state = null;
42
43         LinkedBlockingQueue<AltosLine> telemQueue;
44
45         public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
46                 AltosLine l = telemQueue.take();
47                 if (l.line == null)
48                         throw new IOException("IO error");
49                 AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
50                 if (state == null)
51                         state = new AltosState();
52                 else
53                         state = state.clone();
54                 telem.update_state(state);
55                 return state;
56         }
57
58         public void close() {
59                 state = null;
60                 link.remove_monitor(telemQueue);
61                 link = null;
62                 telemQueue.clear();
63                 telemQueue = null;
64         }
65
66         public void run() {
67                 AltosState  state = null;
68
69                 try {
70                         if (D) Log.d(TAG, "starting loop");
71                         while (telemQueue != null) {
72                                 try {
73                                         state = read();
74                                         handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
75                                 } catch (ParseException pp) {
76                                         Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));
77                                 } catch (AltosCRCException ce) {
78                                         ++crc_errors;
79                                         handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
80                                 }
81                         }
82                 } catch (InterruptedException ee) {
83                 } catch (IOException ie) {
84                         Log.e(TAG, "IO exception in telemetry reader");
85                         handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, link).sendToTarget();
86                 } finally {
87                         close();
88                 }
89         }
90
91         public TelemetryReader (AltosLink in_link, Handler in_handler, AltosState in_state) {
92                 if (D) Log.d(TAG, "connected TelemetryReader create started");
93                 link    = in_link;
94                 handler = in_handler;
95
96                 state = in_state;
97                 telemQueue = new LinkedBlockingQueue<AltosLine>();
98                 link.add_monitor(telemQueue);
99                 link.set_telemetry(AltosLib.ao_telemetry_standard);
100
101                 if (D) Log.d(TAG, "connected TelemetryReader created");
102         }
103 }