Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7aa1130a authored by Amit Mahajan's avatar Amit Mahajan Committed by Gerrit Code Review
Browse files

Merge "Exception and death notification handling for ISap."

parents cb7b0b0a 29f9ae3d
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import org.android.btsap.SapApi;
import org.android.btsap.SapApi.*;
import com.google.protobuf.micro.*;

import android.os.RemoteException;
import android.util.Log;

/**
@@ -720,7 +721,7 @@ public class SapMessage {
    /**
     * Send the message by calling corresponding ISap api.
     */
    public void send(ISap sapProxy) throws Exception {
    public void send(ISap sapProxy) throws RemoteException, RuntimeException {
        int rilSerial = sNextSerial.getAndIncrement();

        Log.e(TAG, "callISapReq: called for mMsgType " + mMsgType + " rilSerial " + rilSerial);
+34 −3
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.android.btsap.SapApi.MsgHeader;

@@ -16,7 +17,9 @@ import android.hardware.radio.V1_0.ISapCallback;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Handler;
import android.os.HwBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;

public class SapRilReceiver implements Runnable {
@@ -34,7 +37,9 @@ public class SapRilReceiver implements Runnable {
    InputStream mRilBtInStream = null;

    SapCallback mSapCallback;
    ISap mSapProxy;
    volatile ISap mSapProxy = null;
    final AtomicLong mSapProxyCookie = new AtomicLong(0);
    final SapProxyDeathRecipient mSapProxyDeathRecipient;

    private Handler mSapServerMsgHandler = null;
    private Handler mSapServiceHandler = null;
@@ -42,6 +47,19 @@ public class SapRilReceiver implements Runnable {
    public static final int RIL_MAX_COMMAND_BYTES = (8 * 1024);
    byte[] buffer = new byte[RIL_MAX_COMMAND_BYTES];

    final class SapProxyDeathRecipient implements HwBinder.DeathRecipient {
        @Override
        public void serviceDied(long cookie) {
            // Deal with service going away
            Log.d(TAG, "serviceDied");
            // todo: temp hack to send delayed message so that rild is back up by then
            // mSapHandler.sendMessage(mSapHandler.obtainMessage(EVENT_SAP_PROXY_DEAD, cookie));
            mSapServerMsgHandler.sendMessageDelayed(
                    mSapServerMsgHandler.obtainMessage(SapServer.SAP_PROXY_DEAD, cookie),
                    SapServer.ISAP_GET_SERVICE_DELAY_MILLIS);
        }
    }

    private void sendSapMessage(SapMessage sapMessage) {
        if (sapMessage.getMsgType() < SapMessage.ID_RIL_BASE) {
            sendClientMessage(sapMessage);
@@ -185,21 +203,34 @@ public class SapRilReceiver implements Runnable {
            mSapProxy = ISap.getService(SOCKET_NAME_RIL_BT);
            if (mSapProxy != null) {
                Log.d(TAG, "getSapProxy: mSapProxy != null; calling setCallback()");
                // todo(b/31632518): need to do this again if remote process crashes/restarts
                mSapProxy.linkToDeath(mSapProxyDeathRecipient, mSapProxyCookie.incrementAndGet());
                mSapProxy.setCallback(mSapCallback);
            } else {
                Log.e(TAG, "getSapProxy: mSapProxy == null");
            }
        } catch (Exception e) {
        } catch (RemoteException | RuntimeException e) {
            mSapProxy = null;

            // if service is not up, treat it like death notification to try to get service again
            mSapServerMsgHandler.sendMessageDelayed(
                    mSapServerMsgHandler.obtainMessage(
                            SapServer.SAP_PROXY_DEAD, mSapProxyCookie.get()),
                    SapServer.ISAP_GET_SERVICE_DELAY_MILLIS);

            Log.e(TAG, "getSapProxy: exception", e);
        }
        return mSapProxy;
    }

    public void resetSapProxy() {
        mSapProxy = null;
    }

    public SapRilReceiver(Handler SapServerMsgHandler, Handler sapServiceHandler) {
        mSapServerMsgHandler = SapServerMsgHandler;
        mSapServiceHandler = sapServiceHandler;
        mSapCallback = new SapCallback();
        mSapProxyDeathRecipient = new SapProxyDeathRecipient();
        mSapProxy = getSapProxy();
    }

+18 −7
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.telephony.TelephonyManager;
@@ -77,12 +78,14 @@ public class SapServer extends Thread implements Callback {
    public static final int SAP_MSG_RIL_REQ =     0x02;
    public static final int SAP_MSG_RIL_IND =     0x03;
    public static final int SAP_RIL_SOCK_CLOSED = 0x04;
    public static final int SAP_PROXY_DEAD = 0x05;

    public static final String SAP_DISCONNECT_ACTION =
            "com.android.bluetooth.sap.action.DISCONNECT_ACTION";
    public static final String SAP_DISCONNECT_TYPE_EXTRA =
            "com.android.bluetooth.sap.extra.DISCONNECT_TYPE";
    public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
    public static final int ISAP_GET_SERVICE_DELAY_MILLIS = 3 * 1000;
    private static final int DISCONNECT_TIMEOUT_IMMEDIATE = 5000; /* ms */
    private static final int DISCONNECT_TIMEOUT_RFCOMM = 2000; /* ms */
    private PendingIntent pDiscIntent = null; // Holds a reference to disconnect timeout intents
@@ -635,6 +638,13 @@ public class SapServer extends Thread implements Callback {
            sendDisconnectInd(SapMessage.DISC_IMMEDIATE);
            startDisconnectTimer(SapMessage.DISC_RFCOMM, DISCONNECT_TIMEOUT_RFCOMM);
            break;
        case SAP_PROXY_DEAD:
            if ((long) msg.obj == mRilBtReceiver.mSapProxyCookie.get()) {
                mRilBtReceiver.resetSapProxy();

                // todo: rild should be back up since message was sent with a delay. this is a hack.
                mRilBtReceiver.getSapProxy();
            }
        default:
            /* Message not handled */
            return false;
@@ -849,21 +859,22 @@ public class SapServer extends Thread implements Callback {
        ISap sapProxy = mRilBtReceiver.getSapProxy();
        if (sapProxy == null) {
            Log.e(TAG_HANDLER, "sendRilMessage: Unable to send message to RIL; sapProxy is null");
            SapMessage errorReply = new SapMessage(SapMessage.ID_ERROR_RESP);
            sendClientMessage(errorReply);
            sendClientMessage(new SapMessage(SapMessage.ID_ERROR_RESP));
            return;
        }

        try {
            sapMsg.send(sapProxy);
            if (VERBOSE) {
                Log.d(TAG_HANDLER, "sendRilMessage: sapMsg.callISapReq called "
                                + "successfully");
                Log.d(TAG_HANDLER, "sendRilMessage: sapMsg.callISapReq called successfully");
            }
        } catch (Exception e) {
        } catch (IllegalArgumentException e) {
            Log.e(TAG_HANDLER, "sendRilMessage: IllegalArgumentException", e);
            sendClientMessage(new SapMessage(SapMessage.ID_ERROR_RESP));
        } catch (RemoteException | RuntimeException e) {
            Log.e(TAG_HANDLER, "sendRilMessage: Unable to send message to RIL", e);
            SapMessage errorReply = new SapMessage(SapMessage.ID_ERROR_RESP);
            sendClientMessage(errorReply);
            sendClientMessage(new SapMessage(SapMessage.ID_ERROR_RESP));
            mRilBtReceiver.resetSapProxy();
        }
    }