altosdroid: Move pause before reopening bluetooth into connec thread
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryService.java
index 5f1389725e8aa240e23b77d86f63790fd60d966c..ed7b75a1ef1ab470ad4ecc39ed5cdeb25aa635f9 100644 (file)
@@ -29,6 +29,7 @@ import android.app.PendingIntent;
 import android.app.Service;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothAdapter;
+import android.hardware.usb.*;
 import android.content.Intent;
 import android.content.Context;
 import android.os.Bundle;
@@ -45,7 +46,7 @@ import android.location.LocationManager;
 import android.location.LocationListener;
 import android.location.Criteria;
 
-import org.altusmetrum.altoslib_6.*;
+import org.altusmetrum.altoslib_7.*;
 
 
 public class TelemetryService extends Service implements LocationListener {
@@ -56,14 +57,15 @@ public class TelemetryService extends Service implements LocationListener {
        static final int MSG_REGISTER_CLIENT   = 1;
        static final int MSG_UNREGISTER_CLIENT = 2;
        static final int MSG_CONNECT           = 3;
-       static final int MSG_CONNECTED         = 4;
-       static final int MSG_CONNECT_FAILED    = 5;
-       static final int MSG_DISCONNECTED      = 6;
-       static final int MSG_TELEMETRY         = 7;
-       static final int MSG_SETFREQUENCY      = 8;
-       static final int MSG_CRC_ERROR         = 9;
-       static final int MSG_SETBAUD           = 10;
-       static final int MSG_DISCONNECT        = 11;
+       static final int MSG_OPEN_USB          = 4;
+       static final int MSG_CONNECTED         = 5;
+       static final int MSG_CONNECT_FAILED    = 6;
+       static final int MSG_DISCONNECTED      = 7;
+       static final int MSG_TELEMETRY         = 8;
+       static final int MSG_SETFREQUENCY      = 9;
+       static final int MSG_CRC_ERROR         = 10;
+       static final int MSG_SETBAUD           = 11;
+       static final int MSG_DISCONNECT        = 12;
 
        // Unique Identification Number for the Notification.
        // We use it on Notification start, and to cancel it.
@@ -76,7 +78,7 @@ public class TelemetryService extends Service implements LocationListener {
 
        // Name of the connected device
        DeviceAddress address;
-       private AltosBluetooth  altos_bluetooth  = null;
+       private AltosDroidLink  altos_link  = null;
        private TelemetryReader telemetry_reader = null;
        private TelemetryLogger telemetry_logger = null;
 
@@ -94,6 +96,7 @@ public class TelemetryService extends Service implements LocationListener {
                @Override
                public void handleMessage(Message msg) {
                        TelemetryService s = service.get();
+                       AltosDroidLink bt = null;
                        if (s == null)
                                return;
                        switch (msg.what) {
@@ -109,20 +112,25 @@ public class TelemetryService extends Service implements LocationListener {
                                if (D) Log.d(TAG, "Connect command received");
                                DeviceAddress address = (DeviceAddress) msg.obj;
                                AltosDroidPreferences.set_active_device(address);
-                               s.start_altos_bluetooth(address);
+                               s.start_altos_bluetooth(address, false);
+                               break;
+                       case MSG_OPEN_USB:
+                               if (D) Log.d(TAG, "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");
                                s.address = null;
-                               s.stop_altos_bluetooth(true);
+                               s.disconnect(true);
                                break;
                        case MSG_SETFREQUENCY:
                                if (D) Log.d(TAG, "MSG_SETFREQUENCY");
                                s.telemetry_state.frequency = (Double) msg.obj;
                                if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
                                        try {
-                                               s.altos_bluetooth.set_radio_frequency(s.telemetry_state.frequency);
-                                               s.altos_bluetooth.save_frequency();
+                                               s.altos_link.set_radio_frequency(s.telemetry_state.frequency);
+                                               s.altos_link.save_frequency();
                                        } catch (InterruptedException e) {
                                        } catch (TimeoutException e) {
                                        }
@@ -133,8 +141,8 @@ public class TelemetryService extends Service implements LocationListener {
                                if (D) Log.d(TAG, "MSG_SETBAUD");
                                s.telemetry_state.telemetry_rate = (Integer) msg.obj;
                                if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
-                                       s.altos_bluetooth.set_telemetry_rate(s.telemetry_state.telemetry_rate);
-                                       s.altos_bluetooth.save_telemetry_rate();
+                                       s.altos_link.set_telemetry_rate(s.telemetry_state.telemetry_rate);
+                                       s.altos_link.save_telemetry_rate();
                                }
                                s.send_to_clients();
                                break;
@@ -143,6 +151,13 @@ public class TelemetryService extends Service implements LocationListener {
                                 *Messages from AltosBluetooth
                                 */
                        case MSG_CONNECTED:
+                               Log.d(TAG, "MSG_CONNECTED");
+                               bt = (AltosDroidLink) msg.obj;
+
+                               if (bt != s.altos_link) {
+                                       if (D) Log.d(TAG, "Stale message");
+                                       break;
+                               }
                                if (D) Log.d(TAG, "Connected to device");
                                try {
                                        s.connected();
@@ -150,20 +165,35 @@ public class TelemetryService extends Service implements LocationListener {
                                }
                                break;
                        case MSG_CONNECT_FAILED:
+                               Log.d(TAG, "MSG_CONNECT_FAILED");
+                               bt = (AltosDroidLink) msg.obj;
+
+                               if (bt != s.altos_link) {
+                                       if (D) Log.d(TAG, "Stale message");
+                                       break;
+                               }
                                if (s.address != null) {
                                        if (D) Log.d(TAG, "Connection failed... retrying");
-                                       s.start_altos_bluetooth(s.address);
+                                       s.start_altos_bluetooth(s.address, true);
                                } else {
-                                       s.stop_altos_bluetooth(true);
+                                       s.disconnect(true);
                                }
                                break;
                        case MSG_DISCONNECTED:
+
+                               /* This can be sent by either AltosDroidLink or TelemetryReader */
                                Log.d(TAG, "MSG_DISCONNECTED");
+                               bt = (AltosDroidLink) msg.obj;
+
+                               if (bt != s.altos_link) {
+                                       if (D) Log.d(TAG, "Stale message");
+                                       break;
+                               }
                                if (s.address != null) {
                                        if (D) Log.d(TAG, "Connection lost... retrying");
-                                       s.start_altos_bluetooth(s.address);
+                                       s.start_altos_bluetooth(s.address, true);
                                } else {
-                                       s.stop_altos_bluetooth(true);
+                                       s.disconnect(true);
                                }
                                break;
 
@@ -217,7 +247,7 @@ public class TelemetryService extends Service implements LocationListener {
                 */
                if (address != null && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
                        if (D) Log.d(TAG, "Reconnecting now...");
-                       start_altos_bluetooth(address);
+                       start_altos_bluetooth(address, false);
                }
        }
 
@@ -254,16 +284,17 @@ public class TelemetryService extends Service implements LocationListener {
                        send_to_client(client, m);
        }
 
-       private void stop_altos_bluetooth(boolean notify) {
-               if (D) Log.d(TAG, "stop_altos_bluetooth(): begin");
+       private void disconnect(boolean notify) {
+               if (D) Log.d(TAG, "disconnect(): begin");
+
                telemetry_state.connect = TelemetryState.CONNECT_DISCONNECTED;
                telemetry_state.address = null;
 
-               if (altos_bluetooth != null)
-                       altos_bluetooth.closing();
+               if (altos_link != null)
+                       altos_link.closing();
 
                if (telemetry_reader != null) {
-                       if (D) Log.d(TAG, "stop_altos_bluetooth(): stopping TelemetryReader");
+                       if (D) Log.d(TAG, "disconnect(): stopping TelemetryReader");
                        telemetry_reader.interrupt();
                        try {
                                telemetry_reader.join();
@@ -272,34 +303,47 @@ public class TelemetryService extends Service implements LocationListener {
                        telemetry_reader = null;
                }
                if (telemetry_logger != null) {
-                       if (D) Log.d(TAG, "stop_altos_bluetooth(): stopping TelemetryLogger");
+                       if (D) Log.d(TAG, "disconnect(): stopping TelemetryLogger");
                        telemetry_logger.stop();
                        telemetry_logger = null;
                }
-               if (altos_bluetooth != null) {
-                       if (D) Log.d(TAG, "stop_altos_bluetooth(): stopping AltosBluetooth");
-                       altos_bluetooth.close();
-                       altos_bluetooth = null;
+               if (altos_link != null) {
+                       if (D) Log.d(TAG, "disconnect(): stopping AltosDroidLink");
+                       altos_link.close();
+                       altos_link = null;
                }
                telemetry_state.config = null;
                if (notify) {
-                       if (D) Log.d(TAG, "stop_altos_bluetooth(): send message to clients");
+                       if (D) Log.d(TAG, "disconnect(): send message to clients");
                        send_to_clients();
                        if (clients.isEmpty()) {
-                               if (D) Log.d(TAG, "stop_altos_bluetooth(): no clients, terminating");
+                               if (D) Log.d(TAG, "disconnect(): no clients, terminating");
                                stopSelf();
                        }
                }
        }
 
-       private void start_altos_bluetooth(DeviceAddress address) {
+       private void start_usb(UsbDevice device) {
+               AltosUsb        d = new AltosUsb(this, device, handler);
+
+               if (d != null) {
+                       disconnect(false);
+                       altos_link = d;
+                       try {
+                               connected();
+                       } catch (InterruptedException ie) {
+                       }
+               }
+       }
+
+       private void start_altos_bluetooth(DeviceAddress address, boolean pause) {
                // Get the BLuetoothDevice object
                BluetoothDevice device = bluetooth_adapter.getRemoteDevice(address.address);
 
-               stop_altos_bluetooth(false);
+               disconnect(false);
                this.address = address;
                if (D) Log.d(TAG, String.format("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress()));
-               altos_bluetooth = new AltosBluetooth(device, handler);
+               altos_link = new AltosBluetooth(device, handler);
                telemetry_state.connect = TelemetryState.CONNECT_CONNECTING;
                telemetry_state.address = address;
                send_to_clients();
@@ -308,15 +352,22 @@ public class TelemetryService extends Service implements LocationListener {
        private void connected() throws InterruptedException {
                if (D) Log.d(TAG, "connected top");
                try {
-                       if (altos_bluetooth == null)
+                       if (altos_link == null)
                                throw new InterruptedException("no bluetooth");
-                       telemetry_state.config = altos_bluetooth.config_data();
-                       altos_bluetooth.set_radio_frequency(telemetry_state.frequency);
-                       altos_bluetooth.set_telemetry_rate(telemetry_state.telemetry_rate);
+                       telemetry_state.config = altos_link.config_data();
+                       altos_link.set_radio_frequency(telemetry_state.frequency);
+                       altos_link.set_telemetry_rate(telemetry_state.telemetry_rate);
                } catch (TimeoutException e) {
                        // If this timed out, then we really want to retry it, but
                        // probably safer to just retry the connection from scratch.
-                       handler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget();
+                       if (D) Log.d(TAG, "connected timeout");
+                       if (address != null) {
+                               if (D) Log.d(TAG, "connected timeout, retrying");
+                               start_altos_bluetooth(address, true);
+                       } else {
+                               handler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget();
+                               disconnect(true);
+                       }
                        return;
                }
 
@@ -324,12 +375,12 @@ public class TelemetryService extends Service implements LocationListener {
                telemetry_state.connect = TelemetryState.CONNECT_CONNECTED;
                telemetry_state.address = address;
 
-               telemetry_reader = new TelemetryReader(altos_bluetooth, handler, telemetry_state.state);
+               telemetry_reader = new TelemetryReader(altos_link, handler, telemetry_state.state);
                telemetry_reader.start();
 
                if (D) Log.d(TAG, "connected TelemetryReader started");
 
-               telemetry_logger = new TelemetryLogger(this, altos_bluetooth);
+               telemetry_logger = new TelemetryLogger(this, altos_link);
 
                if (D) Log.d(TAG, "Notify UI of connection");
 
@@ -369,10 +420,6 @@ public class TelemetryService extends Service implements LocationListener {
                LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
-
-               DeviceAddress address = AltosDroidPreferences.active_device();
-               if (address != null)
-                       start_altos_bluetooth(address);
        }
 
        @Override
@@ -397,6 +444,16 @@ public class TelemetryService extends Service implements LocationListener {
                // Move us into the foreground.
                startForeground(NOTIFICATION, notification);
 
+               if (intent != null) {
+                       String  action = intent.getAction();
+
+                       if (action.equals(AltosDroid.ACTION_BLUETOOTH)) {
+                               DeviceAddress address = AltosDroidPreferences.active_device();
+                               if (address != null && !address.address.startsWith("USB"))
+                                       start_altos_bluetooth(address, false);
+                       }
+               }
+
                // We want this service to continue running until it is explicitly
                // stopped, so return sticky.
                return START_STICKY;
@@ -409,7 +466,7 @@ public class TelemetryService extends Service implements LocationListener {
                ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
 
                // Stop the bluetooth Comms threads
-               stop_altos_bluetooth(true);
+               disconnect(true);
 
                // Demote us from the foreground, and cancel the persistent notification.
                stopForeground(true);