Bump java lib versions in preparation for 1.9.2
[fw/altos] / altosdroid / app / src / main / java / 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; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18  */
19
20
21 package org.altusmetrum.AltosDroid;
22
23 import java.text.*;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import android.os.Handler;
27
28 import org.altusmetrum.altoslib_14.*;
29
30
31 public class TelemetryReader extends Thread {
32
33         int         crc_errors;
34
35         Handler     handler;
36
37         AltosLink   link;
38
39         LinkedBlockingQueue<AltosLine> telemQueue;
40
41         public AltosTelemetry read() throws ParseException, AltosCRCException, InterruptedException, IOException {
42                 AltosLine l = telemQueue.take();
43                 if (l.line == null)
44                         throw new IOException("IO error");
45                 AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
46                 return telem;
47         }
48
49         public void close() {
50                 link.remove_monitor(telemQueue);
51                 link = null;
52                 telemQueue.clear();
53                 telemQueue = null;
54         }
55
56         public void run() {
57                 try {
58                         AltosDebug.debug("starting loop");
59                         while (telemQueue != null) {
60                                 try {
61                                         AltosTelemetry  telem = read();
62                                         telem.set_frequency(link.frequency);
63                                         handler.obtainMessage(TelemetryService.MSG_TELEMETRY, telem).sendToTarget();
64                                 } catch (ParseException pp) {
65                                         AltosDebug.error("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage());
66                                 } catch (AltosCRCException ce) {
67                                         ++crc_errors;
68                                         handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
69                                 }
70                         }
71                 } catch (InterruptedException ee) {
72                 } catch (IOException ie) {
73                         AltosDebug.error("IO exception in telemetry reader");
74                         handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, link).sendToTarget();
75                 } finally {
76                         close();
77                 }
78         }
79
80         public TelemetryReader (AltosLink in_link, Handler in_handler) {
81                 AltosDebug.debug("connected TelemetryReader create started");
82                 link    = in_link;
83                 handler = in_handler;
84
85                 telemQueue = new LinkedBlockingQueue<AltosLine>();
86                 link.add_monitor(telemQueue);
87                 link.set_telemetry(AltosLib.ao_telemetry_standard);
88
89                 AltosDebug.debug("connected TelemetryReader created");
90         }
91 }