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 java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.UUID;
26 import android.os.Handler;
28 import org.altusmetrum.altoslib_13.*;
30 public abstract class AltosDroidLink extends AltosLink {
34 Thread input_thread = null;
36 public double frequency() {
40 public int telemetry_rate() {
41 return telemetry_rate;
44 public void save_frequency() {
45 AltosPreferences.set_frequency(0, frequency);
48 public void save_telemetry_rate() {
49 AltosPreferences.set_telemetry_rate(0, telemetry_rate);
52 Object closed_lock = new Object();
53 boolean closing = false;
54 boolean closed = false;
56 public boolean closed() {
57 synchronized(closed_lock) {
62 void connected() throws InterruptedException {
63 input_thread = new Thread(this);
66 // Configure the newly connected device for telemetry
69 AltosDebug.debug("ConnectThread: connected");
71 /* Let TelemetryService know we're connected
73 handler.obtainMessage(TelemetryService.MSG_CONNECTED, this).sendToTarget();
75 /* Notify other waiting threads that we're connected now
80 public void closing() {
81 synchronized(closed_lock) {
82 AltosDebug.debug("Marked closing true");
87 private boolean actually_closed() {
88 synchronized(closed_lock) {
93 abstract void close_device();
96 AltosDebug.debug("close(): begin");
102 synchronized (closed_lock) {
103 AltosDebug.debug("Marked closed true");
111 if (input_thread != null) {
112 AltosDebug.debug("close(): stopping input_thread");
114 AltosDebug.debug("close(): input_thread.interrupt().....");
115 input_thread.interrupt();
116 AltosDebug.debug("close(): input_thread.join().....");
118 } catch (Exception e) {}
125 abstract int write(byte[] buffer, int len);
127 abstract int read(byte[] buffer, int len);
129 private static final int buffer_size = 64;
131 private byte[] in_buffer = new byte[buffer_size];
132 private byte[] out_buffer = new byte[buffer_size];
133 private int buffer_len = 0;
134 private int buffer_off = 0;
135 private int out_buffer_off = 0;
137 private byte[] debug_chars = new byte[buffer_size];
138 private int debug_off;
140 private void debug_input(byte b) {
142 AltosDebug.debug(" " + new String(debug_chars, 0, debug_off));
145 if (debug_off < buffer_size)
146 debug_chars[debug_off++] = b;
150 private void disconnected() {
152 AltosDebug.debug("disconnected after closed");
156 AltosDebug.debug("Sending disconnected message");
157 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
160 public int getchar() {
162 if (actually_closed())
165 while (buffer_off == buffer_len) {
166 buffer_len = read(in_buffer, buffer_size);
167 if (buffer_len < 0) {
168 AltosDebug.debug("ERROR returned from getchar()");
175 // debug_input(in_buffer[buffer_off]);
176 return in_buffer[buffer_off++];
179 public void flush_output() {
180 super.flush_output();
182 if (actually_closed()) {
187 while (out_buffer_off != 0) {
188 int sent = write(out_buffer, out_buffer_off);
191 AltosDebug.debug("flush_output() failed");
196 if (sent < out_buffer_off)
197 System.arraycopy(out_buffer, 0, out_buffer, sent, out_buffer_off - sent);
199 out_buffer_off -= sent;
203 public void putchar(byte c) {
204 out_buffer[out_buffer_off++] = c;
205 if (out_buffer_off == buffer_size)
209 public void print(String data) {
210 byte[] bytes = data.getBytes();
211 // AltosDebug.debug(data.replace('\n', '\\'));
216 public AltosDroidLink(Handler handler) {
217 this.handler = handler;