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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.UUID;
25 import android.os.Handler;
26 import android.util.Log;
28 import org.altusmetrum.altoslib_7.*;
30 public abstract class AltosDroidLink extends AltosLink {
33 private static final String TAG = "AltosDroidLink";
34 private static final boolean D = true;
38 Thread input_thread = null;
40 public double frequency() {
44 public int telemetry_rate() {
45 return telemetry_rate;
48 public void save_frequency() {
49 AltosPreferences.set_frequency(0, frequency);
52 public void save_telemetry_rate() {
53 AltosPreferences.set_telemetry_rate(0, telemetry_rate);
56 Object closed_lock = new Object();
57 boolean closing = false;
58 boolean closed = false;
60 public boolean closed() {
61 synchronized(closed_lock) {
66 void connected() throws InterruptedException {
67 input_thread = new Thread(this);
70 // Configure the newly connected device for telemetry
73 if (D) Log.d(TAG, "ConnectThread: connected");
75 /* Let TelemetryService know we're connected
77 handler.obtainMessage(TelemetryService.MSG_CONNECTED, this).sendToTarget();
79 /* Notify other waiting threads that we're connected now
84 public void closing() {
85 synchronized(closed_lock) {
86 if (D) Log.d(TAG, "Marked closing true");
91 private boolean actually_closed() {
92 synchronized(closed_lock) {
97 abstract void close_device();
100 if (D) Log.d(TAG, "close(): begin");
106 synchronized (closed_lock) {
107 if (D) Log.d(TAG, "Marked closed true");
115 if (input_thread != null) {
116 if (D) Log.d(TAG, "close(): stopping input_thread");
118 if (D) Log.d(TAG, "close(): input_thread.interrupt().....");
119 input_thread.interrupt();
120 if (D) Log.d(TAG, "close(): input_thread.join().....");
122 } catch (Exception e) {}
129 abstract int write(byte[] buffer, int len);
131 abstract int read(byte[] buffer, int len);
133 private static final int buffer_size = 64;
135 private byte[] in_buffer = new byte[buffer_size];
136 private byte[] out_buffer = new byte[buffer_size];
137 private int buffer_len = 0;
138 private int buffer_off = 0;
139 private int out_buffer_off = 0;
141 private byte[] debug_chars = new byte[buffer_size];
142 private int debug_off;
144 private void debug_input(byte b) {
146 Log.d(TAG, " " + new String(debug_chars, 0, debug_off));
149 if (debug_off < buffer_size)
150 debug_chars[debug_off++] = b;
154 private void disconnected() {
156 if (D) Log.d(TAG, "disconnected after closed");
160 if (D) Log.d(TAG, "Sending disconnected message");
161 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
164 public int getchar() {
166 if (actually_closed())
169 while (buffer_off == buffer_len) {
170 buffer_len = read(in_buffer, buffer_size);
171 if (buffer_len < 0) {
172 Log.d(TAG, "ERROR returned from getchar()");
179 debug_input(in_buffer[buffer_off]);
180 return in_buffer[buffer_off++];
183 public void flush_output() {
184 super.flush_output();
186 if (actually_closed()) {
191 while (out_buffer_off != 0) {
192 int sent = write(out_buffer, out_buffer_off);
195 Log.d(TAG, "flush_output() failed");
200 if (sent < out_buffer_off)
201 System.arraycopy(out_buffer, 0, out_buffer, sent, out_buffer_off - sent);
203 out_buffer_off -= sent;
207 public void putchar(byte c) {
208 out_buffer[out_buffer_off++] = c;
209 if (out_buffer_off == buffer_size)
213 public void print(String data) {
214 byte[] bytes = data.getBytes();
215 if (D) Log.d(TAG, "print(): begin");
218 if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
221 public AltosDroidLink(Handler handler) {
222 this.handler = handler;