2 * Copyright © 2015 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
19 package org.altusmetrum.AltosDroid;
21 import android.os.Handler;
23 import org.altusmetrum.altoslib_14.*;
25 public abstract class AltosDroidLink extends AltosLink {
29 Thread input_thread = null;
31 public double frequency() {
35 public int telemetry_rate() {
36 return telemetry_rate;
39 public void save_frequency() {
40 AltosPreferences.set_frequency(0, frequency);
43 public void save_telemetry_rate() {
44 AltosPreferences.set_telemetry_rate(0, telemetry_rate);
47 Object closed_lock = new Object();
48 boolean closing = false;
49 boolean closed = false;
51 public boolean closed() {
52 synchronized(closed_lock) {
57 void connected() throws InterruptedException {
58 input_thread = new Thread(this);
61 // Configure the newly connected device for telemetry
64 AltosDebug.debug("ConnectThread: connected");
66 /* Let TelemetryService know we're connected
68 handler.obtainMessage(TelemetryService.MSG_CONNECTED, this).sendToTarget();
70 /* Notify other waiting threads that we're connected now
75 public void closing() {
76 synchronized(closed_lock) {
77 AltosDebug.debug("Marked closing true");
82 private boolean actually_closed() {
83 synchronized(closed_lock) {
88 abstract void close_device();
91 AltosDebug.debug("close(): begin");
97 synchronized (closed_lock) {
98 AltosDebug.debug("Marked closed true");
106 if (input_thread != null) {
107 AltosDebug.debug("close(): stopping input_thread");
109 AltosDebug.debug("close(): input_thread.interrupt().....");
110 input_thread.interrupt();
111 AltosDebug.debug("close(): input_thread.join().....");
113 } catch (Exception e) {}
120 abstract int write(byte[] buffer, int len);
122 abstract int read(byte[] buffer, int len);
124 private static final int buffer_size = 64;
126 private byte[] in_buffer = new byte[buffer_size];
127 private byte[] out_buffer = new byte[buffer_size];
128 private int buffer_len = 0;
129 private int buffer_off = 0;
130 private int out_buffer_off = 0;
132 private byte[] debug_chars = new byte[buffer_size];
133 private int debug_off;
135 private void debug_input(byte b) {
137 AltosDebug.debug(" " + new String(debug_chars, 0, debug_off));
140 if (debug_off < buffer_size)
141 debug_chars[debug_off++] = b;
145 private void disconnected() {
147 AltosDebug.debug("disconnected after closed");
151 AltosDebug.debug("Sending disconnected message");
152 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
155 public int getchar() {
157 if (actually_closed())
160 while (buffer_off == buffer_len) {
161 buffer_len = read(in_buffer, buffer_size);
162 if (buffer_len < 0) {
163 AltosDebug.debug("ERROR returned from getchar()");
170 // debug_input(in_buffer[buffer_off]);
171 return in_buffer[buffer_off++];
174 public void flush_output() {
175 super.flush_output();
177 if (actually_closed()) {
182 while (out_buffer_off != 0) {
183 int sent = write(out_buffer, out_buffer_off);
186 AltosDebug.debug("flush_output() failed");
191 if (sent < out_buffer_off)
192 System.arraycopy(out_buffer, 0, out_buffer, sent, out_buffer_off - sent);
194 out_buffer_off -= sent;
198 public void putchar(byte c) {
199 out_buffer[out_buffer_off++] = c;
200 if (out_buffer_off == buffer_size)
204 public void print(String data) {
205 byte[] bytes = data.getBytes();
206 // AltosDebug.debug(data.replace('\n', '\\'));
211 public AltosDroidLink(Handler handler) {
212 this.handler = handler;