-/*\r
- * Copyright © 2011 Keith Packard <keithp@keithp.com>\r
- * Copyright © 2012 Mike Beattie <mike@ethernal.org>\r
- *\r
- * This program is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; version 2 of the License.\r
- *\r
- * This program is distributed in the hope that it will be useful, but\r
- * WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- * General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License along\r
- * with this program; if not, write to the Free Software Foundation, Inc.,\r
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
- */\r
-\r
-\r
-package org.altusmetrum.AltosDroid;\r
-\r
-import java.text.*;\r
-import java.io.*;\r
-import java.util.concurrent.*;\r
-import android.util.Log;\r
-import android.os.Handler;\r
-\r
-import org.altusmetrum.AltosLib.*;\r
-\r
-\r
-public class TelemetryReader extends Thread {\r
-\r
- private static final String TAG = "TelemetryReader";\r
-\r
- int crc_errors;\r
-\r
- Handler handler;\r
-\r
- AltosLink link;\r
- AltosRecord previous;\r
-\r
- LinkedBlockingQueue<AltosLine> telem;\r
-\r
- public AltosRecord read() throws ParseException, AltosCRCException, InterruptedException, IOException {\r
- AltosLine l = telem.take();\r
- if (l.line == null)\r
- throw new IOException("IO error");\r
- AltosRecord next = AltosTelemetry.parse(l.line, previous);\r
- previous = next;\r
- return next;\r
- }\r
-\r
- public void close() {\r
- previous = null;\r
- link.remove_monitor(telem);\r
- link = null;\r
- telem.clear();\r
- telem = null;\r
- }\r
-\r
- public void run() {\r
- AltosState state = null;\r
-\r
- try {\r
- for (;;) {\r
- try {\r
- AltosRecord record = read();\r
- if (record == null)\r
- break;\r
- state = new AltosState(record, state);\r
-\r
- handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();\r
- } catch (ParseException pp) {\r
- Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));\r
- } catch (AltosCRCException ce) {\r
- ++crc_errors;\r
- }\r
- }\r
- } catch (InterruptedException ee) {\r
- } catch (IOException ie) {\r
- } finally {\r
- close();\r
- }\r
- }\r
-\r
- public TelemetryReader (AltosLink in_link, Handler in_handler) {\r
- link = in_link;\r
- handler = in_handler;\r
-\r
- previous = null;\r
- telem = new LinkedBlockingQueue<AltosLine>();\r
- link.add_monitor(telem);\r
- }\r
-}\r
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ * Copyright © 2012 Mike Beattie <mike@ethernal.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+
+package org.altusmetrum.AltosDroid;
+
+import java.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+import android.os.Handler;
+
+import org.altusmetrum.altoslib_7.*;
+
+
+public class TelemetryReader extends Thread {
+
+ int crc_errors;
+
+ Handler handler;
+
+ AltosLink link;
+ AltosState state = null;
+
+ LinkedBlockingQueue<AltosLine> telemQueue;
+
+ public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
+ AltosLine l = telemQueue.take();
+ if (l.line == null)
+ throw new IOException("IO error");
+ AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
+ if (state == null)
+ state = new AltosState();
+ else
+ state = state.clone();
+ telem.update_state(state);
+ return state;
+ }
+
+ public void close() {
+ state = null;
+ link.remove_monitor(telemQueue);
+ link = null;
+ telemQueue.clear();
+ telemQueue = null;
+ }
+
+ public void run() {
+ AltosState state = null;
+
+ try {
+ AltosDebug.debug("starting loop");
+ while (telemQueue != null) {
+ try {
+ state = read();
+ handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
+ } catch (ParseException pp) {
+ AltosDebug.error("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage());
+ } catch (AltosCRCException ce) {
+ ++crc_errors;
+ handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
+ }
+ }
+ } catch (InterruptedException ee) {
+ } catch (IOException ie) {
+ AltosDebug.error("IO exception in telemetry reader");
+ handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, link).sendToTarget();
+ } finally {
+ close();
+ }
+ }
+
+ public TelemetryReader (AltosLink in_link, Handler in_handler, AltosState in_state) {
+ AltosDebug.debug("connected TelemetryReader create started");
+ link = in_link;
+ handler = in_handler;
+
+ state = in_state;
+ telemQueue = new LinkedBlockingQueue<AltosLine>();
+ link.add_monitor(telemQueue);
+ link.set_telemetry(AltosLib.ao_telemetry_standard);
+
+ AltosDebug.debug("connected TelemetryReader created");
+ }
+}