Create AltosDebug to hold the debug code, use it everywhere.
Signed-off-by: Keith Packard <keithp@keithp.com>
//import android.os.Bundle;
import android.os.Handler;
//import android.os.Message;
-import android.util.Log;
import org.altusmetrum.altoslib_7.*;
public class AltosBluetooth extends AltosDroidLink {
- // Debugging
- private static final String TAG = "AltosBluetooth";
- private static final boolean D = true;
-
private ConnectThread connect_thread = null;
private BluetoothDevice device;
void connected() {
if (closed()) {
- if (D) Log.d(TAG, "connected after closed");
+ AltosDebug.debug("connected after closed");
return;
}
private void connect_failed() {
if (closed()) {
- if (D) Log.d(TAG, "connect_failed after closed");
+ AltosDebug.debug("connect_failed after closed");
return;
}
input = null;
output = null;
handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED, this).sendToTarget();
- if (D) Log.e(TAG, "ConnectThread: Failed to establish connection");
+ AltosDebug.error("ConnectThread: Failed to establish connection");
}
void close_device() {
try {
tmp_socket.close();
} catch (IOException e) {
- if (D) Log.e(TAG, "close_socket failed");
+ AltosDebug.error("close_socket failed");
}
}
}
BluetoothSocket tmp_socket = null;
+ AltosDebug.check_ui("create_socket\n");
try {
tmp_socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
} catch (IOException e) {
e.printStackTrace();
}
if (socket != null) {
- if (D) Log.d(TAG, String.format("Socket already allocated %s", socket.toString()));
+ AltosDebug.debug("Socket already allocated %s", socket.toString());
close_device();
}
synchronized (this) {
private class ConnectThread extends Thread {
public void run() {
- if (D) Log.d(TAG, "ConnectThread: BEGIN");
+ AltosDebug.debug("ConnectThread: BEGIN (pause %b)", pause);
setName("ConnectThread");
if (pause) {
try {
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
} catch (Exception e) {
- if (D) Log.d(TAG, String.format("cancelDiscovery exception %s", e.toString()));
+ AltosDebug.debug("cancelDiscovery exception %s", e.toString());
}
BluetoothSocket local_socket = null;
// This is a blocking call and will only return on a
// successful connection or an exception
local_socket.connect();
- } catch (IOException e) {
- if (D) Log.d(TAG, String.format("Connect exception %s", e.toString()));
+ } catch (Exception e) {
+ AltosDebug.debug("Connect exception %s", e.toString());
+ try {
+ local_socket.close();
+ } catch (Exception ce) {
+ AltosDebug.debug("Close exception %s", ce.toString());
+ }
local_socket = null;
}
}
connect_failed();
}
- if (D) Log.d(TAG, "ConnectThread: completed");
+ AltosDebug.debug("ConnectThread: completed");
}
}
private synchronized void wait_connected() throws InterruptedException, IOException {
+ AltosDebug.check_ui("wait_connected\n");
if (input == null && socket != null) {
- if (D) Log.d(TAG, "wait_connected...");
+ AltosDebug.debug("wait_connected...");
wait();
- if (D) Log.d(TAG, "wait_connected done");
+ AltosDebug.debug("wait_connected done");
}
if (socket == null)
throw new IOException();
--- /dev/null
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * 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.util.Arrays;
+import java.io.*;
+import java.lang.*;
+
+import org.altusmetrum.altoslib_7.*;
+
+import android.app.Activity;
+import android.graphics.*;
+import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentTransaction;
+import android.view.*;
+import android.widget.*;
+import android.location.Location;
+import android.content.*;
+import android.util.Log;
+import android.os.Looper;
+
+public class AltosDebug {
+ // Debugging
+ static final String TAG = "AltosDroid";
+ public static final boolean D = true;
+
+ static void info(String format, Object ... arguments) {
+ Log.i(TAG, String.format(format, arguments));
+ }
+
+ static void debug(String format, Object ... arguments) {
+ if (D)
+ Log.d(TAG, String.format(format, arguments));
+ }
+
+ static void error(String format, Object ... arguments) {
+ Log.e(TAG, String.format(format, arguments));
+ }
+
+ static void check_ui(String format, Object ... arguments) {
+ if (Looper.myLooper() == Looper.getMainLooper()) {
+ Log.e(TAG, String.format("ON UI THREAD " + format, arguments));
+ for (StackTraceElement el : Thread.currentThread().getStackTrace())
+ Log.e(TAG, "\t" + el.toString() + "\n");
+ }
+ }
+}
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
-import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import org.altusmetrum.altoslib_7.*;
public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
- // Debugging
- static final String TAG = "AltosDroid";
- static final boolean D = true;
// Actions sent to the telemetry server at startup time
switch (msg.what) {
case MSG_STATE:
- if(D) Log.d(TAG, "MSG_STATE");
+ AltosDebug.debug("MSG_STATE");
TelemetryState telemetry_state = (TelemetryState) msg.obj;
if (telemetry_state == null) {
- Log.d(TAG, "telemetry_state null!");
+ AltosDebug.debug("telemetry_state null!");
return;
}
ad.update_state(telemetry_state);
break;
case MSG_UPDATE_AGE:
- if(D) Log.d(TAG, "MSG_UPDATE_AGE");
+ AltosDebug.debug("MSG_UPDATE_AGE");
ad.update_age();
break;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- if(D) Log.e(TAG, "+++ ON CREATE +++");
+ AltosDebug.debug("+++ ON CREATE +++");
fm = getSupportFragmentManager();
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true);
- if (D) Log.e(TAG, "intent " + intent + " device " + device + " granted " + granted);
+ AltosDebug.debug("intent %s device %s granted %s", intent, device, granted);
if (!granted)
device = null;
if (device != null) {
- if (D) Log.d(TAG, "intent has usb device " + device.toString());
+ AltosDebug.debug("intent has usb device " + device.toString());
connectUsb(device);
} else {
* don't want to loop forever...
*/
if (granted) {
- if (D) Log.d(TAG, "check for a USB device at startup");
+ AltosDebug.debug("check for a USB device at startup");
if (check_usb())
return;
}
- if (D) Log.d(TAG, "Starting by looking for bluetooth devices");
+ AltosDebug.debug("Starting by looking for bluetooth devices");
if (ensureBluetooth())
return;
finish();
@Override
public void onStart() {
super.onStart();
- if(D) Log.e(TAG, "++ ON START ++");
+ AltosDebug.debug("++ ON START ++");
noticeIntent(getIntent());
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
- if(D) Log.d(TAG, "onNewIntent");
+ AltosDebug.debug("onNewIntent");
noticeIntent(intent);
}
@Override
public void onResume() {
super.onResume();
- if(D) Log.e(TAG, "+ ON RESUME +");
+ AltosDebug.debug("+ ON RESUME +");
}
@Override
public void onPause() {
super.onPause();
- if(D) Log.e(TAG, "- ON PAUSE -");
+ AltosDebug.debug("- ON PAUSE -");
}
@Override
public void onStop() {
super.onStop();
- if(D) Log.e(TAG, "-- ON STOP --");
+ AltosDebug.debug("-- ON STOP --");
doUnbindService();
if (mAltosVoice != null) {
@Override
public void onDestroy() {
super.onDestroy();
- if(D) Log.e(TAG, "--- ON DESTROY ---");
+ AltosDebug.debug("--- ON DESTROY ---");
if (mAltosVoice != null) mAltosVoice.stop();
stop_timer();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(D) Log.d(TAG, "onActivityResult " + resultCode);
+ AltosDebug.debug("onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect to
//setupChat();
} else {
// User did not enable Bluetooth or an error occured
- Log.e(TAG, "BT not enabled");
+ AltosDebug.error("BT not enabled");
stopService(new Intent(AltosDroid.this, TelemetryService.class));
Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
finish();
// Attempt to connect to the device
try {
mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, device));
- if (D) Log.d(TAG, "Sent OPEN_USB message");
+ AltosDebug.debug("Sent OPEN_USB message");
} catch (RemoteException e) {
- if (D) Log.e(TAG, "connect device message failed");
+ AltosDebug.debug("connect device message failed");
}
}
}
String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
- if (D) Log.d(TAG, "Connecting to " + address + " " + name);
+ AltosDebug.debug("Connecting to " + address + " " + name);
DeviceAddress a = new DeviceAddress(address, name);
mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
- if (D) Log.d(TAG, "Sent connecting message");
+ AltosDebug.debug("Sent connecting message");
} catch (RemoteException e) {
- if (D) Log.e(TAG, "connect device message failed");
+ AltosDebug.debug("connect device message failed");
}
}
disconnectDevice();
return true;
case R.id.quit:
- Log.d(TAG, "R.id.quit");
+ AltosDebug.debug("R.id.quit");
disconnectDevice();
finish();
return true;
import java.util.UUID;
import android.os.Handler;
-import android.util.Log;
import org.altusmetrum.altoslib_7.*;
public abstract class AltosDroidLink extends AltosLink {
- // Debugging
- private static final String TAG = "AltosDroidLink";
- private static final boolean D = true;
-
Handler handler;
Thread input_thread = null;
// Configure the newly connected device for telemetry
print("~\nE 0\n");
set_monitor(false);
- if (D) Log.d(TAG, "ConnectThread: connected");
+ AltosDebug.debug("ConnectThread: connected");
/* Let TelemetryService know we're connected
*/
public void closing() {
synchronized(closed_lock) {
- if (D) Log.d(TAG, "Marked closing true");
+ AltosDebug.debug("Marked closing true");
closing = true;
}
}
abstract void close_device();
public void close() {
- if (D) Log.d(TAG, "close(): begin");
+ AltosDebug.debug("close(): begin");
closing();
flush_output();
synchronized (closed_lock) {
- if (D) Log.d(TAG, "Marked closed true");
+ AltosDebug.debug("Marked closed true");
closed = true;
}
synchronized(this) {
if (input_thread != null) {
- if (D) Log.d(TAG, "close(): stopping input_thread");
+ AltosDebug.debug("close(): stopping input_thread");
try {
- if (D) Log.d(TAG, "close(): input_thread.interrupt().....");
+ AltosDebug.debug("close(): input_thread.interrupt().....");
input_thread.interrupt();
- if (D) Log.d(TAG, "close(): input_thread.join().....");
+ AltosDebug.debug("close(): input_thread.join().....");
input_thread.join();
} catch (Exception e) {}
input_thread = null;
private void debug_input(byte b) {
if (b == '\n') {
- Log.d(TAG, " " + new String(debug_chars, 0, debug_off));
+ AltosDebug.debug(" " + new String(debug_chars, 0, debug_off));
debug_off = 0;
} else {
if (debug_off < buffer_size)
private void disconnected() {
if (closed()) {
- if (D) Log.d(TAG, "disconnected after closed");
+ AltosDebug.debug("disconnected after closed");
return;
}
- if (D) Log.d(TAG, "Sending disconnected message");
+ AltosDebug.debug("Sending disconnected message");
handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
}
while (buffer_off == buffer_len) {
buffer_len = read(in_buffer, buffer_size);
if (buffer_len < 0) {
- Log.d(TAG, "ERROR returned from getchar()");
+ AltosDebug.debug("ERROR returned from getchar()");
disconnected();
return ERROR;
}
buffer_off = 0;
}
- if (D)
+ if (AltosDebug.D)
debug_input(in_buffer[buffer_off]);
return in_buffer[buffer_off++];
}
int sent = write(out_buffer, out_buffer_off);
if (sent <= 0) {
- Log.d(TAG, "flush_output() failed");
+ AltosDebug.debug("flush_output() failed");
out_buffer_off = 0;
break;
}
public void print(String data) {
byte[] bytes = data.getBytes();
- if (D) Log.d(TAG, "print(): begin");
+ AltosDebug.debug("print(): begin");
for (byte b : bytes)
putchar(b);
- if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
+ AltosDebug.debug("print(): Wrote bytes: '" + data.replace('\n', '\\') + "'");
}
public AltosDroidLink(Handler handler) {
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.location.Location;
-import android.util.Log;
import android.widget.TextView;
public abstract class AltosDroidTab extends Fragment implements AltosUnitsListener {
import android.hardware.usb.*;
import android.app.*;
import android.os.Handler;
-import android.util.Log;
import org.altusmetrum.altoslib_7.*;
public class AltosUsb extends AltosDroidLink {
- // Debugging
- private static final String TAG = "AltosUsb";
- private static final boolean D = true;
-
private Thread input_thread = null;
private Handler handler;
}
if (in != null && out != null) {
- Log.d(TAG, String.format("\tin %s out %s\n", in.toString(), out.toString()));
+ AltosDebug.debug("\tin %s out %s\n", in.toString(), out.toString());
manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
if (manager == null) {
- Log.d(TAG, "USB_SERVICE failed");
+ AltosDebug.debug("USB_SERVICE failed");
return;
}
connection = manager.openDevice(device);
if (connection == null) {
- Log.d(TAG, "openDevice failed");
+ AltosDebug.debug("openDevice failed");
return;
}
// if (manager.hasPermission(device))
// return true;
- Log.d(TAG, "request permission for USB device " + device.toString());
+ AltosDebug.debug("request permission for USB device " + device.toString());
manager.requestPermission(device, pi);
return false;
int product = device.getProductId();
if (matchProduct(match_product, device)) {
- Log.d(TAG, "found USB device " + device.toString());
+ AltosDebug.debug("found USB device " + device.toString());
return device;
}
}
private void disconnected() {
if (closed()) {
- if (D) Log.d(TAG, "disconnected after closed");
+ AltosDebug.debug("disconnected after closed");
return;
}
- if (D) Log.d(TAG, "Sending disconnected message");
+ AltosDebug.debug("Sending disconnected message");
handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
}
}
if (tmp_connection != null) {
- if (D) Log.d(TAG, "Closing USB device");
+ AltosDebug.debug("Closing USB device");
tmp_connection.close();
}
}
int read(byte[] buffer, int len) {
int ret = connection.bulkTransfer(in, buffer, len, -1);
- if (D) Log.d(TAG, String.format("read(%d) = %d\n", len, ret));
+ AltosDebug.debug("read(%d) = %d\n", len, ret);
return ret;
}
int write(byte[] buffer, int len) {
int ret = connection.bulkTransfer(out, buffer, len, -1);
- if (D) Log.d(TAG, String.format("write(%d) = %d\n", len, ret));
+ AltosDebug.debug("write(%d) = %d\n", len, ret);
return ret;
}
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
-import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
* Activity in the result Intent.
*/
public class DeviceListActivity extends Activity {
- // Debugging
- private static final String TAG = "DeviceListActivity";
- private static final boolean D = true;
// Return Intent extra
public static final String EXTRA_DEVICE_ADDRESS = "device_address";
* Start device discover with the BluetoothAdapter
*/
private void doDiscovery() {
- if (D) Log.d(TAG, "doDiscovery()");
+ AltosDebug.debug("doDiscovery()");
// Indicate scanning in the title
setProgressBarIndeterminateVisibility(true);
else
name = info;
- if (D) Log.d(TAG, String.format("******* selected item '%s'", info));
+ AltosDebug.debug("******* selected item '%s'", info);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
import android.widget.*;
import android.location.Location;
import android.content.*;
-import android.util.Log;
public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
- // Debugging
- static final String TAG = "AltosDroid";
- static final boolean D = true;
AltosDroid mAltosDroid;
public boolean onScale(ScaleGestureDetector detector) {
float f = detector.getScaleFactor();
- if (D) Log.d(TAG, String.format("onScale %f\n", f));
+ AltosDebug.debug("onScale %f\n", f);
if (f <= 0.8) {
map.set_zoom(map.get_zoom() - 1);
return true;
}
public boolean onScaleBegin(ScaleGestureDetector detector) {
- if (D) Log.d(TAG, String.format("onScaleBegin %f\n", detector.getScaleFactor()));
+ AltosDebug.debug("onScaleBegin %f\n", detector.getScaleFactor());
return true;
}
public void onScaleEnd(ScaleGestureDetector detector) {
- if (D) Log.d(TAG, String.format("onScaleEnd %f\n", detector.getScaleFactor()));
+ AltosDebug.debug("onScaleEnd %f\n", detector.getScaleFactor());
}
@Override
}
if (scaling) {
- if(D) Log.d(TAG, "scale in progress\n");
+ if(AltosDebug.D) AltosDebug.debug("scale in progress\n");
if (event.getAction() == MotionEvent.ACTION_UP) {
- if (D) Log.d(TAG, "scale finished\n");
+ AltosDebug.debug("scale finished\n");
scaling = false;
}
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
- if(D) Log.d(TAG, String.format("down event %g %g\n", event.getX(), event.getY()));
+ AltosDebug.debug("down event %g %g\n", event.getX(), event.getY());
map.touch_start((int) event.getX(), (int) event.getY(), true);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
- if(D) Log.d(TAG, String.format("continue event %g %g\n", event.getX(), event.getY()));
+ AltosDebug.debug("continue event %g %g\n", event.getX(), event.getY());
map.touch_continue((int) event.getX(), (int) event.getY(), true);
}
return true;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;
-import android.util.Log;
/**
* This is a helper class that implements the management of tabs and all
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
- Log.d(AltosDroid.TAG, String.format("TabsAdapter.getItem(%d)", position));
+ AltosDebug.debug("TabsAdapter.getItem(%d)", position);
info.fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args);
return info.fragment;
}
if (cur_frag != null) {
cur_frag.set_visible(true);
}
- Log.d(AltosDroid.TAG, String.format("TabsAdapter.onTabChanged(%s) = %d", tabId, position));
+ AltosDebug.debug("TabsAdapter.onTabChanged(%s) = %d", tabId, position);
mViewPager.setCurrentItem(position);
}
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
-import android.util.Log;
public class TelemetryLogger {
- private static final String TAG = "TelemetryLogger";
- private static final boolean D = true;
-
private Context context = null;
private AltosLink link = null;
private AltosLog logger = null;
private void close() {
if (logger != null) {
- if (D) Log.d(TAG, "Shutting down Telemetry Logging");
+ AltosDebug.debug("Shutting down Telemetry Logging");
logger.close();
logger = null;
}
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (logger == null) {
- if (D) Log.d(TAG, "Starting up Telemetry Logging");
+ AltosDebug.debug("Starting up Telemetry Logging");
logger = new AltosLog(link);
}
} else {
- if (D) Log.d(TAG, "External Storage not present - stopping");
+ AltosDebug.debug("External Storage not present - stopping");
close();
}
}
import java.text.*;
import java.io.*;
import java.util.concurrent.*;
-import android.util.Log;
import android.os.Handler;
import org.altusmetrum.altoslib_7.*;
public class TelemetryReader extends Thread {
- private static final String TAG = "TelemetryReader";
- private static final boolean D = true;
-
int crc_errors;
Handler handler;
AltosState state = null;
try {
- if (D) Log.d(TAG, "starting loop");
+ AltosDebug.debug("starting loop");
while (telemQueue != null) {
try {
state = read();
handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
} catch (ParseException pp) {
- Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));
+ 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) {
- Log.e(TAG, "IO exception in telemetry reader");
+ 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) {
- if (D) Log.d(TAG, "connected TelemetryReader create started");
+ AltosDebug.debug("connected TelemetryReader create started");
link = in_link;
handler = in_handler;
link.add_monitor(telemQueue);
link.set_telemetry(AltosLib.ao_telemetry_standard);
- if (D) Log.d(TAG, "connected TelemetryReader created");
+ AltosDebug.debug("connected TelemetryReader created");
}
}
import android.os.Messenger;
import android.os.RemoteException;
import android.os.Looper;
-import android.util.Log;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationManager;
public class TelemetryService extends Service implements LocationListener {
- private static final String TAG = "TelemetryService";
- private static final boolean D = true;
-
static final int MSG_REGISTER_CLIENT = 1;
static final int MSG_UNREGISTER_CLIENT = 2;
static final int MSG_CONNECT = 3;
s.remove_client(msg.replyTo);
break;
case MSG_CONNECT:
- if (D) Log.d(TAG, "Connect command received");
+ AltosDebug.debug("Connect command received");
DeviceAddress address = (DeviceAddress) msg.obj;
AltosDroidPreferences.set_active_device(address);
s.start_altos_bluetooth(address, false);
break;
case MSG_OPEN_USB:
- if (D) Log.d(TAG, "Open USB command received");
+ AltosDebug.debug("Open USB command received");
UsbDevice device = (UsbDevice) msg.obj;
s.start_usb(device);
break;
case MSG_DISCONNECT:
- if (D) Log.d(TAG, "Disconnect command received");
+ AltosDebug.debug("Disconnect command received");
s.address = null;
s.disconnect(true);
break;
case MSG_SETFREQUENCY:
- if (D) Log.d(TAG, "MSG_SETFREQUENCY");
+ AltosDebug.debug("MSG_SETFREQUENCY");
s.telemetry_state.frequency = (Double) msg.obj;
if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
try {
s.send_to_clients();
break;
case MSG_SETBAUD:
- if (D) Log.d(TAG, "MSG_SETBAUD");
+ AltosDebug.debug("MSG_SETBAUD");
s.telemetry_state.telemetry_rate = (Integer) msg.obj;
if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
s.altos_link.set_telemetry_rate(s.telemetry_state.telemetry_rate);
*Messages from AltosBluetooth
*/
case MSG_CONNECTED:
- Log.d(TAG, "MSG_CONNECTED");
+ AltosDebug.debug("MSG_CONNECTED");
bt = (AltosDroidLink) msg.obj;
if (bt != s.altos_link) {
- if (D) Log.d(TAG, "Stale message");
+ AltosDebug.debug("Stale message");
break;
}
- if (D) Log.d(TAG, "Connected to device");
+ AltosDebug.debug("Connected to device");
try {
s.connected();
} catch (InterruptedException ie) {
}
break;
case MSG_CONNECT_FAILED:
- Log.d(TAG, "MSG_CONNECT_FAILED");
+ AltosDebug.debug("MSG_CONNECT_FAILED");
bt = (AltosDroidLink) msg.obj;
if (bt != s.altos_link) {
- if (D) Log.d(TAG, "Stale message");
+ AltosDebug.debug("Stale message");
break;
}
if (s.address != null) {
- if (D) Log.d(TAG, "Connection failed... retrying");
+ AltosDebug.debug("Connection failed... retrying");
s.start_altos_bluetooth(s.address, true);
} else {
s.disconnect(true);
case MSG_DISCONNECTED:
/* This can be sent by either AltosDroidLink or TelemetryReader */
- Log.d(TAG, "MSG_DISCONNECTED");
+ AltosDebug.debug("MSG_DISCONNECTED");
bt = (AltosDroidLink) msg.obj;
if (bt != s.altos_link) {
- if (D) Log.d(TAG, "Stale message");
+ AltosDebug.debug("Stale message");
break;
}
if (s.address != null) {
- if (D) Log.d(TAG, "Connection lost... retrying");
+ AltosDebug.debug("Connection lost... retrying");
s.start_altos_bluetooth(s.address, true);
} else {
s.disconnect(true);
case MSG_TELEMETRY:
s.telemetry_state.state = (AltosState) msg.obj;
if (s.telemetry_state.state != null) {
- if (D) Log.d(TAG, "Save state");
+ AltosDebug.debug("Save state");
AltosPreferences.set_state(0, s.telemetry_state.state, null);
}
- if (D) Log.d(TAG, "MSG_TELEMETRY");
+ AltosDebug.debug("MSG_TELEMETRY");
s.send_to_clients();
break;
case MSG_CRC_ERROR:
// forward crc error messages
s.telemetry_state.crc_errors = (Integer) msg.obj;
- if (D) Log.d(TAG, "MSG_CRC_ERROR");
+ AltosDebug.debug("MSG_CRC_ERROR");
s.send_to_clients();
break;
default:
*/
private Message message() {
if (telemetry_state == null)
- Log.d(TAG, "telemetry_state null!");
+ AltosDebug.debug("telemetry_state null!");
if (telemetry_state.state == null)
- Log.d(TAG, "telemetry_state.state null!");
+ AltosDebug.debug("telemetry_state.state null!");
return Message.obtain(null, AltosDroid.MSG_STATE, telemetry_state);
}
private void add_client(Messenger client) {
clients.add(client);
- if (D) Log.d(TAG, "Client bound to service");
+ AltosDebug.debug("Client bound to service");
/* On connect, send the current state to the new client
*/
* go ahead and try to reconnect to the device
*/
if (address != null && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
- if (D) Log.d(TAG, "Reconnecting now...");
+ AltosDebug.debug("Reconnecting now...");
start_altos_bluetooth(address, false);
}
}
*/
private void remove_client(Messenger client) {
clients.remove(client);
- if (D) Log.d(TAG, "Client unbound from service");
+ AltosDebug.debug("Client unbound from service");
/* When the list of clients is empty, stop the service if
* we have no current telemetry source
*/
if (clients.isEmpty() && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
- if (!D) Log.d(TAG, "No clients, no connection. Stopping\n");
+ AltosDebug.debug("No clients, no connection. Stopping\n");
stopSelf();
}
}
private void send_to_client(Messenger client, Message m) {
try {
- if (D) Log.d(TAG, String.format("Send message to client %s", client.toString()));
+ AltosDebug.debug("Send message to client %s", client.toString());
client.send(m);
} catch (RemoteException e) {
- if (D) Log.e(TAG, String.format("Client %s disappeared", client.toString()));
+ AltosDebug.error("Client %s disappeared", client.toString());
remove_client(client);
}
}
private void send_to_clients() {
Message m = message();
- if (D) Log.d(TAG, String.format("Send message to %d clients", clients.size()));
+ AltosDebug.debug("Send message to %d clients", clients.size());
for (Messenger client : clients)
send_to_client(client, m);
}
private void disconnect(boolean notify) {
- if (D) Log.d(TAG, "disconnect(): begin");
+ AltosDebug.debug("disconnect(): begin");
telemetry_state.connect = TelemetryState.CONNECT_DISCONNECTED;
telemetry_state.address = null;
altos_link.closing();
if (telemetry_reader != null) {
- if (D) Log.d(TAG, "disconnect(): stopping TelemetryReader");
+ AltosDebug.debug("disconnect(): stopping TelemetryReader");
telemetry_reader.interrupt();
try {
telemetry_reader.join();
telemetry_reader = null;
}
if (telemetry_logger != null) {
- if (D) Log.d(TAG, "disconnect(): stopping TelemetryLogger");
+ AltosDebug.debug("disconnect(): stopping TelemetryLogger");
telemetry_logger.stop();
telemetry_logger = null;
}
if (altos_link != null) {
- if (D) Log.d(TAG, "disconnect(): stopping AltosDroidLink");
+ AltosDebug.debug("disconnect(): stopping AltosDroidLink");
altos_link.close();
altos_link = null;
}
telemetry_state.config = null;
if (notify) {
- if (D) Log.d(TAG, "disconnect(): send message to clients");
+ AltosDebug.debug("disconnect(): send message to clients");
send_to_clients();
if (clients.isEmpty()) {
- if (D) Log.d(TAG, "disconnect(): no clients, terminating");
+ AltosDebug.debug("disconnect(): no clients, terminating");
stopSelf();
}
}
private void start_altos_bluetooth(DeviceAddress address, boolean pause) {
// Get the BLuetoothDevice object
+ AltosDebug.check_ui("start_altos_bluetooth\n");
BluetoothDevice device = bluetooth_adapter.getRemoteDevice(address.address);
disconnect(false);
this.address = address;
- if (D) Log.d(TAG, String.format("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress()));
- altos_link = new AltosBluetooth(device, handler);
+ AltosDebug.debug("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress());
+ altos_link = new AltosBluetooth(device, handler, pause);
telemetry_state.connect = TelemetryState.CONNECT_CONNECTING;
telemetry_state.address = address;
send_to_clients();
}
private void connected() throws InterruptedException {
- if (D) Log.d(TAG, "connected top");
+ AltosDebug.debug("connected top");
+ AltosDebug.check_ui("connected\n");
try {
if (altos_link == null)
throw new InterruptedException("no bluetooth");
} catch (TimeoutException e) {
// If this timed out, then we really want to retry it, but
// probably safer to just retry the connection from scratch.
- if (D) Log.d(TAG, "connected timeout");
+ AltosDebug.debug("connected timeout");
if (address != null) {
- if (D) Log.d(TAG, "connected timeout, retrying");
+ AltosDebug.debug("connected timeout, retrying");
start_altos_bluetooth(address, true);
} else {
handler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget();
return;
}
- if (D) Log.d(TAG, "connected bluetooth configured");
+ AltosDebug.debug("connected bluetooth configured");
telemetry_state.connect = TelemetryState.CONNECT_CONNECTED;
telemetry_state.address = address;
telemetry_reader = new TelemetryReader(altos_link, handler, telemetry_state.state);
telemetry_reader.start();
- if (D) Log.d(TAG, "connected TelemetryReader started");
+ AltosDebug.debug("connected TelemetryReader started");
telemetry_logger = new TelemetryLogger(this, altos_link);
- if (D) Log.d(TAG, "Notify UI of connection");
+ AltosDebug.debug("Notify UI of connection");
send_to_clients();
}
AltosSavedState saved_state = AltosPreferences.state(0);
if (saved_state != null) {
- if (D) Log.d(TAG, String.format("recovered old state flight %d\n", saved_state.state.flight));
+ AltosDebug.debug("recovered old state flight %d\n", saved_state.state.flight);
telemetry_state.state = saved_state.state;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
- Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
+ AltosDebug.debug("Received start id %d: %s", startId, intent);
CharSequence text = getText(R.string.telemetry_service_started);
public void onLocationChanged(Location location) {
telemetry_state.location = location;
- if (D) Log.d(TAG, "location changed");
+ AltosDebug.debug("location changed");
send_to_clients();
}