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

Unverified Commit 7e174137 authored by Haynes Mathew George's avatar Haynes Mathew George Committed by Michael Bestas
Browse files

WiredAccessoryManager: Support for multiple extconn devices

Add support for multiple extconn connections.
Support multiple connections with multiple controllers.

CRs-Fixed: 2262706
Change-Id: I42aca85a6834f6c4ab6c5659ef2836134892518d
parent ce19f0ba
Loading
Loading
Loading
Loading
+93 −40
Original line number Diff line number Diff line
@@ -71,6 +71,11 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
    private static final String NAME_USB_AUDIO = "usb_audio";
    private static final String NAME_HDMI_AUDIO = "hdmi_audio";
    private static final String NAME_DP_AUDIO = "soc:qcom,msm-ext-disp";
    // within a device, a single stream supports DP
    private static final String[] DP_AUDIO_CONNS = {
            NAME_DP_AUDIO + "/1/0",
            NAME_DP_AUDIO + "/0/0"
    };
    private static final String NAME_HDMI = "hdmi";

    private static final int MSG_NEW_DEVICE_STATE = 1;
@@ -183,7 +188,7 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
            }

            updateLocked(
                    NAME_H2W,
                    NAME_H2W, "",
                    (mHeadsetState & ~(BIT_HEADSET | BIT_HEADSET_NO_MIC | BIT_LINEOUT)) | headset,
                    isSynchronous);
        }
@@ -209,7 +214,8 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
     * @param newState 0 or one of the BIT_xxx variables defined above.
     * @param isSynchronous boolean to determine whether should happen sync or async
     */
    private void updateLocked(String newName, int newState, boolean isSynchronous) {
    private void updateLocked(String newName, String address, int newState,
            boolean isSynchronous) {
        // Retain only relevant bits
        int headsetState = newState & SUPPORTED_HEADSETS;
        int usb_headset_anlg = headsetState & BIT_USB_HEADSET_ANLG;
@@ -252,8 +258,9 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
        } else {
            mWakeLock.acquire();
            Log.i(TAG, "MSG_NEW_DEVICE_STATE");
            // Send a combined name, address string separated by |
            Message msg = mHandler.obtainMessage(MSG_NEW_DEVICE_STATE, headsetState,
                    mHeadsetState, "");
                    mHeadsetState, newName + "/" + address);
            mHandler.sendMessage(msg);
        }

@@ -277,12 +284,13 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
    };

    private void setDevicesState(
            int headsetState, int prevHeadsetState, String headsetName) {
            int headsetState, int prevHeadsetState, String headsetNameAddr) {
        synchronized (mLock) {
            int allHeadsets = SUPPORTED_HEADSETS;
            for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) {
                if ((curHeadset & allHeadsets) != 0) {
                    setDeviceStateLocked(curHeadset, headsetState, prevHeadsetState, headsetName);
                    setDeviceStateLocked(curHeadset, headsetState, prevHeadsetState,
                            headsetNameAddr);
                    allHeadsets &= ~curHeadset;
                }
            }
@@ -290,7 +298,7 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
    }

    private void setDeviceStateLocked(int headset,
            int headsetState, int prevHeadsetState, String headsetName) {
            int headsetState, int prevHeadsetState, String headsetNameAddr) {
        if ((headsetState & headset) != (prevHeadsetState & headset)) {
            int outDevice = 0;
            int inDevice = 0;
@@ -321,15 +329,18 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
            }

            if (LOG) {
                Slog.v(TAG, "headsetName: " + headsetName +
                Slog.v(TAG, "headsetNameAddr: " + headsetNameAddr +
                        (state == 1 ? " connected" : " disconnected"));
            }

            String[] hs = headsetNameAddr.split("/");
            if (outDevice != 0) {
                mAudioManager.setWiredDeviceConnectionState(outDevice, state, "", headsetName);
                mAudioManager.setWiredDeviceConnectionState(outDevice, state,
                        (hs.length > 1 ? hs[1] : ""), hs[0]);
            }
            if (inDevice != 0) {
                mAudioManager.setWiredDeviceConnectionState(inDevice, state, "", headsetName);
                mAudioManager.setWiredDeviceConnectionState(inDevice, state,
                        (hs.length > 1 ? hs[1] : ""), hs[0]);
            }
        }
    }
@@ -432,12 +443,17 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
                }
            }

            for (String conn : DP_AUDIO_CONNS) {
                // Monitor DisplayPort
            uei = new UEventInfo(NAME_DP_AUDIO, BIT_HDMI_AUDIO, 0, 0);
                if (LOG) {
                    Slog.v(TAG, "Monitor DP conn " + conn);
                }
                uei = new UEventInfo(conn, BIT_HDMI_AUDIO, 0, 0);
                if (uei.checkSwitchExists()) {
                    retVal.add(uei);
                } else {
                Slog.w(TAG, "This kernel does not have DP audio support");
                    Slog.w(TAG, "Conn " + conn + " does not have DP audio support");
                }
            }

            return retVal;
@@ -454,7 +470,7 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
            }

            try {
                if (name.equals(NAME_DP_AUDIO)) {
                if (name.startsWith(NAME_DP_AUDIO)) {
                    String stateStr = event.get("STATE");
                    int offset = 0;
                    int length = stateStr.length();
@@ -520,7 +536,7 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
                UEventInfo uei = mUEventInfo.get(i);
                if (devPath.equals(uei.getDevPath())) {
                    updateLocked(
                            name,
                            name, uei.getDevAddress(),
                            uei.computeNewHeadsetState(mHeadsetState, state),
                            false /*isSynchronous*/);
                    return;
@@ -530,6 +546,7 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {

        private final class UEventInfo {
            private final String mDevName;
            private String mDevAddress;
            private final int mState1Bits;
            private final int mState2Bits;
            private final int mStateNbits;
@@ -538,71 +555,103 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {

            public UEventInfo(String devName, int state1Bits, int state2Bits, int stateNbits) {
                mDevName = devName;
                mDevAddress = "controller=0;stream=0";
                mState1Bits = state1Bits;
                mState2Bits = state2Bits;
                mStateNbits = stateNbits;

                if (mDevName.equals(NAME_DP_AUDIO)) {
                    getDevIndex();
                    getCableIndex();
                mDevIndex = -1;
                mCableIndex = -1;

                if (mDevName.startsWith(NAME_DP_AUDIO)) {
                    int idx = mDevName.indexOf("/");
                    if (idx != -1) {
                        int idx2 = mDevName.indexOf("/", idx + 1);
                        assert(idx2 != -1);
                        int dev = Integer.parseInt(mDevName.substring(idx + 1, idx2));
                        int cable = Integer.parseInt(mDevName.substring(idx2 + 1));
                        mDevAddress = "controller=" + dev + ";stream=" + cable;
                        if (LOG) {
                            Slog.v(TAG, "UEvent dev address " + mDevAddress);
                        }
                        checkDevIndex(dev);
                        checkCableIndex(cable);
                    }
                }
            }

            private void getDevIndex() {
            private void checkDevIndex(int devIndex) {
                int index = 0;
                char[] buffer = new char[1024];

                while (true) {
                    String devPath = String.format(Locale.US,
                            "/sys/devices/platform/soc/%s/extcon/extcon%d/name",
                            mDevName, index);
                            NAME_DP_AUDIO, index);
                    if (LOG) {
                        Slog.v(TAG, "checkDevIndex " + devPath);
                    }

                    File f = new File(devPath);
                    if (!f.exists()) {
                        Slog.e(TAG, "file " + devPath + " not found");
                        break;
                    }

                    try {
                        FileReader file = new FileReader(devPath);
                        FileReader file = new FileReader(f);
                        int len = file.read(buffer, 0, 1024);
                        file.close();

                        String devName = (new String(buffer, 0, len)).trim();
                        if (devName.equals(mDevName)) {
                            mDevIndex = index;
                        if (devName.startsWith(NAME_DP_AUDIO) && index == devIndex) {
                            mDevIndex = devIndex;
                            break;
                        } else {
                            index++;
                        }
                    } catch (FileNotFoundException e) {
                        break;
                    } catch (Exception e) {
                        Slog.e(TAG, "getDevIndex exception", e);
                        Slog.e(TAG, "checkDevIndex exception", e);
                        break;
                    }
                }
            }

            private void getCableIndex() {
            private void checkCableIndex(int cableIndex) {
                if (mDevIndex == -1) {
                    return;
                }
                int index = 0;
                char[] buffer = new char[1024];

                while (true) {
                    String cablePath = String.format(Locale.US,
                            "/sys/devices/platform/soc/%s/extcon/extcon%d/cable.%d/name",
                            mDevName, mDevIndex, index);
                            NAME_DP_AUDIO, mDevIndex, index);
                    if (LOG) {
                        Slog.v(TAG, "checkCableIndex " + cablePath);
                    }

                    File f = new File(cablePath);
                    if (!f.exists()) {
                        Slog.e(TAG, "file " + cablePath + " not found");
                        break;
                    }

                    try {
                        FileReader file = new FileReader(cablePath);
                        FileReader file = new FileReader(f);
                        int len = file.read(buffer, 0, 1024);
                        file.close();

                        String cableName = (new String(buffer, 0, len)).trim();
                        if (cableName.equals("DP")) {
                        if (cableName.equals("DP") && index == cableIndex) {
                            mCableIndex = index;
                            break;
                        } else {
                            index++;
                        }
                    } catch (FileNotFoundException e) {
                        break;
                    } catch (Exception e) {
                        Slog.e(TAG, "getCableIndex exception", e);
                        Slog.e(TAG, "checkCableIndex exception", e);
                        break;
                    }
                }
@@ -612,20 +661,24 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
                return mDevName;
            }

            public String getDevAddress() {
                return mDevAddress;
            }

            public String getDevPath() {
                if (mDevName.equals(NAME_DP_AUDIO)) {
                if (mDevName.startsWith(NAME_DP_AUDIO)) {
                    return String.format(Locale.US,
                            "/devices/platform/soc/%s/extcon/extcon%d",
                            mDevName, mDevIndex);
                            NAME_DP_AUDIO, mDevIndex);
                }
                return String.format(Locale.US, "/devices/virtual/switch/%s", mDevName);
            }

            public String getSwitchStatePath() {
                if (mDevName.equals(NAME_DP_AUDIO)) {
                if (mDevName.startsWith(NAME_DP_AUDIO)) {
                    return String.format(Locale.US,
                            "/sys/devices/platform/soc/%s/extcon/extcon%d/cable.%d/state",
                            mDevName, mDevIndex, mCableIndex);
                            NAME_DP_AUDIO, mDevIndex, mCableIndex);
                }
                return String.format(Locale.US, "/sys/class/switch/%s/state", mDevName);
            }
@@ -711,7 +764,7 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
                int mask = maskAndState.first;
                int state = maskAndState.second;
                updateLocked(
                        name,
                        name, "",
                        mHeadsetState & ~(mask & ~state) | (mask & state),
                        false /*isSynchronous*/);
                return;