altoslib: Support binary reading/writing in AltosLink
[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_2.*;
29
30
31 public class TelemetryReader extends Thread {
32
33         private static final String TAG = "TelemetryReader";
34
35         int         crc_errors;
36
37         Handler     handler;
38
39         AltosLink   link;
40         AltosState  state = null;
41
42         LinkedBlockingQueue<AltosLine> telemQueue;
43
44         public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
45                 AltosLine l = telemQueue.take();
46                 if (l.line == null)
47                         throw new IOException("IO error");
48                 AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
49                 if (state == null)
50                         state = new AltosState();
51                 else
52                         state = state.clone();
53                 telem.update_state(state);
54                 return state;
55         }
56
57         public void close() {
58                 state = null;
59                 link.remove_monitor(telemQueue);
60                 link = null;
61                 telemQueue.clear();
62                 telemQueue = null;
63         }
64
65         public void run() {
66                 AltosState  state = null;
67
68                 try {
69                         for (;;) {
70                                 try {
71                                         state = read();
72                                         handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
73                                 } catch (ParseException pp) {
74                                         Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));
75                                 } catch (AltosCRCException ce) {
76                                         ++crc_errors;
77                                         handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
78                                 }
79                         }
80                 } catch (InterruptedException ee) {
81                 } catch (IOException ie) {
82                 } finally {
83                         close();
84                 }
85         }
86
87         public TelemetryReader (AltosLink in_link, Handler in_handler) {
88                 link    = in_link;
89                 handler = in_handler;
90
91                 state = null;
92                 telemQueue = new LinkedBlockingQueue<AltosLine>();
93                 link.add_monitor(telemQueue);
94         }
95 }