Remove ant build files
[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; 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.*;
26 import java.util.concurrent.*;
27 import android.os.Handler;
28
29 import org.altusmetrum.altoslib_13.*;
30
31
32 public class TelemetryReader extends Thread {
33
34         int         crc_errors;
35
36         Handler     handler;
37
38         AltosLink   link;
39
40         LinkedBlockingQueue<AltosLine> telemQueue;
41
42         public AltosTelemetry read() throws ParseException, AltosCRCException, InterruptedException, IOException {
43                 AltosLine l = telemQueue.take();
44                 if (l.line == null)
45                         throw new IOException("IO error");
46                 AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
47                 return telem;
48         }
49
50         public void close() {
51                 link.remove_monitor(telemQueue);
52                 link = null;
53                 telemQueue.clear();
54                 telemQueue = null;
55         }
56
57         public void run() {
58                 try {
59                         AltosDebug.debug("starting loop");
60                         while (telemQueue != null) {
61                                 try {
62                                         AltosTelemetry  telem = read();
63                                         telem.set_frequency(link.frequency);
64                                         handler.obtainMessage(TelemetryService.MSG_TELEMETRY, telem).sendToTarget();
65                                 } catch (ParseException pp) {
66                                         AltosDebug.error("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage());
67                                 } catch (AltosCRCException ce) {
68                                         ++crc_errors;
69                                         handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
70                                 }
71                         }
72                 } catch (InterruptedException ee) {
73                 } catch (IOException ie) {
74                         AltosDebug.error("IO exception in telemetry reader");
75                         handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, link).sendToTarget();
76                 } finally {
77                         close();
78                 }
79         }
80
81         public TelemetryReader (AltosLink in_link, Handler in_handler) {
82                 AltosDebug.debug("connected TelemetryReader create started");
83                 link    = in_link;
84                 handler = in_handler;
85
86                 telemQueue = new LinkedBlockingQueue<AltosLine>();
87                 link.add_monitor(telemQueue);
88                 link.set_telemetry(AltosLib.ao_telemetry_standard);
89
90                 AltosDebug.debug("connected TelemetryReader created");
91         }
92 }