HalDeviceManager: Use name + type for interface cache

IWifiNanIface & IWifiStaIface uses the same interface name. So, using
just the interface name to key into the interface cache leads to the
wrong interface being deleted from the internal cache.

Bug: 70811488
Test: Modified Unit tests to reflect the interfae naming scheme used
on Pixel devices (i.e sta0 = wlan0, nan0 = wlan0, ap0 = wlan0, sta1 =
wlan1).
Change-Id: Idb6a46146a710f2ea79109c213c47c4b13b1e03b
diff --git a/service/java/com/android/server/wifi/HalDeviceManager.java b/service/java/com/android/server/wifi/HalDeviceManager.java
index 51ff312..3cc5504 100644
--- a/service/java/com/android/server/wifi/HalDeviceManager.java
+++ b/service/java/com/android/server/wifi/HalDeviceManager.java
@@ -41,6 +41,7 @@
 import android.util.LongSparseArray;
 import android.util.MutableBoolean;
 import android.util.MutableInt;
+import android.util.Pair;
 import android.util.SparseArray;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -265,10 +266,11 @@
      */
     public IWifiChip getChip(IWifiIface iface) {
         String name = getName(iface);
+        int type = getType(iface);
         if (VDBG) Log.d(TAG, "getChip: iface(name)=" + name);
 
         synchronized (mLock) {
-            InterfaceCacheEntry cacheEntry = mInterfaceInfoCache.get(name);
+            InterfaceCacheEntry cacheEntry = mInterfaceInfoCache.get(Pair.create(name, type));
             if (cacheEntry == null) {
                 Log.e(TAG, "getChip: no entry for iface(name)=" + name);
                 return null;
@@ -294,10 +296,11 @@
             @NonNull InterfaceDestroyedListener destroyedListener,
             @Nullable Handler handler) {
         String name = getName(iface);
+        int type = getType(iface);
         if (VDBG) Log.d(TAG, "registerDestroyedListener: iface(name)=" + name);
 
         synchronized (mLock) {
-            InterfaceCacheEntry cacheEntry = mInterfaceInfoCache.get(name);
+            InterfaceCacheEntry cacheEntry = mInterfaceInfoCache.get(Pair.create(name, type));
             if (cacheEntry == null) {
                 Log.e(TAG, "registerDestroyedListener: no entry for iface(name)=" + name);
                 return false;
@@ -494,7 +497,8 @@
      * we need to keep a list of registered destroyed listeners. Will be validated regularly
      * in getAllChipInfoAndValidateCache().
      */
-    private final Map<String, InterfaceCacheEntry> mInterfaceInfoCache = new HashMap<>();
+    private final Map<Pair<String, Integer>, InterfaceCacheEntry> mInterfaceInfoCache =
+            new HashMap<>();
 
     private class InterfaceCacheEntry {
         public IWifiChip chip;
@@ -1376,7 +1380,8 @@
                     cacheEntry.creationTime = mClock.getUptimeSinceBootMillis();
 
                     if (mDbg) Log.d(TAG, "createIfaceIfPossible: added cacheEntry=" + cacheEntry);
-                    mInterfaceInfoCache.put(cacheEntry.name, cacheEntry);
+                    mInterfaceInfoCache.put(
+                            Pair.create(cacheEntry.name, cacheEntry.type), cacheEntry);
                     return iface;
                 }
             }
@@ -1668,7 +1673,8 @@
         boolean lookupError = false;
         LongSparseArray<WifiIfaceInfo> orderedList = new LongSparseArray(interfaces.length);
         for (WifiIfaceInfo info : interfaces) {
-            InterfaceCacheEntry cacheEntry = mInterfaceInfoCache.get(info.name);
+            InterfaceCacheEntry cacheEntry = mInterfaceInfoCache.get(
+                    Pair.create(info.name, getType(info.iface)));
             if (cacheEntry == null) {
                 Log.e(TAG,
                         "selectInterfacesToDelete: can't find cache entry with name=" + info.name);
@@ -1836,7 +1842,7 @@
             }
 
             // dispatch listeners no matter what status
-            dispatchDestroyedListeners(name);
+            dispatchDestroyedListeners(name, type);
 
             if (status != null && status.code == WifiStatusCode.SUCCESS) {
                 return true;
@@ -1896,11 +1902,11 @@
 
     // dispatch all destroyed listeners registered for the specified interface AND remove the
     // cache entry
-    private void dispatchDestroyedListeners(String name) {
+    private void dispatchDestroyedListeners(String name, int type) {
         if (VDBG) Log.d(TAG, "dispatchDestroyedListeners: iface(name)=" + name);
 
         synchronized (mLock) {
-            InterfaceCacheEntry entry = mInterfaceInfoCache.get(name);
+            InterfaceCacheEntry entry = mInterfaceInfoCache.get(Pair.create(name, type));
             if (entry == null) {
                 Log.e(TAG, "dispatchDestroyedListeners: no cache entry for iface(name)=" + name);
                 return;
@@ -1910,7 +1916,7 @@
                 listener.trigger();
             }
             entry.destroyedListeners.clear(); // for insurance (though cache entry is removed)
-            mInterfaceInfoCache.remove(name);
+            mInterfaceInfoCache.remove(Pair.create(name, type));
         }
     }
 
@@ -1919,7 +1925,7 @@
         if (VDBG) Log.d(TAG, "dispatchAllDestroyedListeners");
 
         synchronized (mLock) {
-            Iterator<Map.Entry<String, InterfaceCacheEntry>> it =
+            Iterator<Map.Entry<Pair<String, Integer>, InterfaceCacheEntry>> it =
                     mInterfaceInfoCache.entrySet().iterator();
             while (it.hasNext()) {
                 InterfaceCacheEntry entry = it.next().getValue();
diff --git a/tests/wifitests/src/com/android/server/wifi/HalDeviceManagerTest.java b/tests/wifitests/src/com/android/server/wifi/HalDeviceManagerTest.java
index 4290ada..f9c3c3f 100644
--- a/tests/wifitests/src/com/android/server/wifi/HalDeviceManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/HalDeviceManagerTest.java
@@ -318,7 +318,7 @@
                 false, // chipModeValid
                 -1000, // chipModeId (only used if chipModeValid is true)
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.STA_CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener, // destroyedListener
@@ -330,7 +330,7 @@
                 true, // chipModeValid
                 TestChipV1.STA_CHIP_MODE_ID, // chipModeId
                 IfaceType.NAN, // ifaceTypeToCreate
-                "nan0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.STA_CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 nanDestroyedListener, // destroyedListener
@@ -338,7 +338,7 @@
         );
 
         // fiddle with the "chip" by removing the STA
-        chipMock.interfaceNames.get(IfaceType.STA).remove("sta0");
+        chipMock.interfaceNames.get(IfaceType.STA).remove("wlan0");
 
         // now try to request another NAN
         IWifiIface nanIface2 = mDut.createNanIface(nanDestroyedListener, mHandler);
@@ -384,7 +384,7 @@
                 false, // chipModeValid
                 -1000, // chipModeId (only used if chipModeValid is true)
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.STA_CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 null, // destroyedListener
@@ -515,7 +515,7 @@
      */
     @Test
     public void testCreateStaInterfaceNoInitModeTestChipV1() throws Exception {
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV1(), IfaceType.STA, "sta0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV1(), IfaceType.STA, "wlan0",
                 TestChipV1.STA_CHIP_MODE_ID, 1);
     }
 
@@ -524,7 +524,7 @@
      */
     @Test
     public void testCreateApInterfaceNoInitModeTestChipV1() throws Exception {
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV1(), IfaceType.AP, "ap0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV1(), IfaceType.AP, "wlan0",
                 TestChipV1.AP_CHIP_MODE_ID, 1);
     }
 
@@ -542,7 +542,7 @@
      */
     @Test
     public void testCreateNanInterfaceNoInitModeTestChipV1() throws Exception {
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV1(), IfaceType.NAN, "nan0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV1(), IfaceType.NAN, "wlan0",
                 TestChipV1.STA_CHIP_MODE_ID, 1);
     }
 
@@ -557,7 +557,7 @@
         // we get callback 1 after creating the first STA (since we can create another STA),
         // and we get callback 2 after destroying the first STA (since we can create another STA -
         // as expected).
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV2(), IfaceType.STA, "sta0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV2(), IfaceType.STA, "wlan0",
                 TestChipV2.CHIP_MODE_ID, 2);
     }
 
@@ -566,7 +566,7 @@
      */
     @Test
     public void testCreateApInterfaceNoInitModeTestChipV2() throws Exception {
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV2(), IfaceType.AP, "ap0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV2(), IfaceType.AP, "wlan0",
                 TestChipV2.CHIP_MODE_ID, 1);
     }
 
@@ -584,7 +584,7 @@
      */
     @Test
     public void testCreateNanInterfaceNoInitModeTestChipV2() throws Exception {
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV2(), IfaceType.NAN, "nan0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV2(), IfaceType.NAN, "wlan0",
                 TestChipV2.CHIP_MODE_ID, 1);
     }
 
@@ -599,7 +599,7 @@
         // we get callback 1 after creating the first STA (since we can create another STA),
         // and we get callback 2 after destroying the first STA (since we can create another STA -
         // as expected).
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV3(), IfaceType.STA, "sta0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV3(), IfaceType.STA, "wlan0",
                 TestChipV3.CHIP_MODE_ID, 2);
     }
 
@@ -608,7 +608,7 @@
      */
     @Test
     public void testCreateApInterfaceNoInitModeTestChipV3() throws Exception {
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV3(), IfaceType.AP, "ap0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV3(), IfaceType.AP, "wlan0",
                 TestChipV3.CHIP_MODE_ID, 1);
     }
 
@@ -626,7 +626,7 @@
      */
     @Test
     public void testCreateNanInterfaceNoInitModeTestChipV3() throws Exception {
-        runCreateSingleXxxInterfaceNoInitMode(new TestChipV3(), IfaceType.NAN, "nan0",
+        runCreateSingleXxxInterfaceNoInitMode(new TestChipV3(), IfaceType.NAN, "wlan0",
                 TestChipV3.CHIP_MODE_ID, 1);
     }
 
@@ -640,7 +640,7 @@
      */
     @Test
     public void testCreateApWithStaModeUpTestChipV1() throws Exception {
-        final String name = "ap0";
+        final String name = "wlan0";
 
         TestChipV1 chipMock = new TestChipV1();
         chipMock.initialize();
@@ -754,7 +754,7 @@
      */
     @Test
     public void testCreateApWithApModeUpTestChipV1() throws Exception {
-        final String name = "ap0";
+        final String name = "wlan0";
 
         TestChipV1 chipMock = new TestChipV1();
         chipMock.initialize();
@@ -869,7 +869,7 @@
                 false, // chipModeValid
                 -1000, // chipModeId (only used if chipModeValid is true)
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.STA_CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener, // destroyedListener
@@ -910,7 +910,7 @@
                 true, // chipModeValid
                 TestChipV1.STA_CHIP_MODE_ID, // chipModeId
                 IfaceType.AP, // ifaceTypeToCreate
-                "ap0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.AP_CHIP_MODE_ID, // finalChipMode
                 new IWifiIface[]{staIface, p2pIface}, // tearDownList
                 apDestroyedListener, // destroyedListener
@@ -940,7 +940,7 @@
                 true, // chipModeValid
                 TestChipV1.AP_CHIP_MODE_ID, // chipModeId
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.STA_CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener, // destroyedListener
@@ -993,7 +993,7 @@
                 true, // chipModeValid
                 TestChipV1.STA_CHIP_MODE_ID, // chipModeId
                 IfaceType.NAN, // ifaceTypeToCreate
-                "nan0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.STA_CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 nanDestroyedListener, // destroyedListener
@@ -1054,7 +1054,7 @@
                 false, // chipModeValid
                 -1000, // chipModeId (only used if chipModeValid is true)
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV1.STA_CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener1, // destroyedListener
@@ -1196,7 +1196,7 @@
                 false, // chipModeValid
                 -1000, // chipModeId (only used if chipModeValid is true)
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV2.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener, // destroyedListener
@@ -1234,7 +1234,7 @@
                 true, // chipModeValid
                 TestChipV2.CHIP_MODE_ID, // chipModeId
                 IfaceType.AP, // ifaceTypeToCreate
-                "ap0", // ifaceName
+                "wlan1", // ifaceName
                 TestChipV2.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 apDestroyedListener, // destroyedListener
@@ -1256,7 +1256,7 @@
 
         inOrderStaAvail.verify(staAvailListener).onAvailableForRequest();
         inOrderApAvail.verify(apAvailListener).onAvailableForRequest();
-        verify(chipMock.chip).removeApIface("ap0");
+        verify(chipMock.chip).removeApIface("wlan1");
         verify(apDestroyedListener).onDestroyed(getName(apIface));
 
         // create STA2: using a later clock
@@ -1265,7 +1265,7 @@
                 true, // chipModeValid
                 TestChipV2.CHIP_MODE_ID, // chipModeId
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta1", // ifaceName
+                "wlan1", // ifaceName
                 TestChipV2.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener2, // destroyedListener
@@ -1284,7 +1284,7 @@
                 true, // chipModeValid
                 TestChipV2.CHIP_MODE_ID, // chipModeId
                 IfaceType.AP, // ifaceTypeToCreate
-                "ap0", // ifaceName
+                "wlan1", // ifaceName
                 TestChipV2.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 apDestroyedListener, // destroyedListener
@@ -1309,7 +1309,7 @@
                 true, // chipModeValid
                 TestChipV2.CHIP_MODE_ID, // chipModeId
                 IfaceType.NAN, // ifaceTypeToCreate
-                "nan0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV2.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 nanDestroyedListener, // destroyedListener
@@ -1471,7 +1471,7 @@
                 false, // chipModeValid
                 -1000, // chipModeId (only used if chipModeValid is true)
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV3.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener, // destroyedListener
@@ -1508,7 +1508,7 @@
                 true, // chipModeValid
                 TestChipV3.CHIP_MODE_ID, // chipModeId
                 IfaceType.AP, // ifaceTypeToCreate
-                "ap0", // ifaceName
+                "wlan1", // ifaceName
                 TestChipV3.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 apDestroyedListener, // destroyedListener
@@ -1538,7 +1538,7 @@
         inOrderApAvail.verify(apAvailListener).onAvailableForRequest();
         inOrderP2pAvail.verify(p2pAvailListener).onAvailableForRequest();
         inOrderNanAvail.verify(nanAvailListener).onAvailableForRequest();
-        verify(chipMock.chip).removeApIface("ap0");
+        verify(chipMock.chip).removeApIface("wlan1");
         verify(apDestroyedListener).onDestroyed(getName(apIface));
 
         // create STA2: using a later clock
@@ -1547,7 +1547,7 @@
                 true, // chipModeValid
                 TestChipV3.CHIP_MODE_ID, // chipModeId
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta1", // ifaceName
+                "wlan1", // ifaceName
                 TestChipV3.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener2, // destroyedListener
@@ -1568,7 +1568,7 @@
                 true, // chipModeValid
                 TestChipV3.CHIP_MODE_ID, // chipModeId
                 IfaceType.NAN, // ifaceTypeToCreate
-                "nan0", // ifaceName
+                "wlan0", // ifaceName
                 TestChipV3.CHIP_MODE_ID, // finalChipMode
                 null, // tearDownList
                 nanDestroyedListener, // destroyedListener
@@ -1580,7 +1580,7 @@
 
         inOrderApAvail.verify(apAvailListener).onAvailableForRequest();
         inOrderP2pAvail.verify(p2pAvailListener).onAvailableForRequest();
-        verify(chipMock.chip).removeStaIface("sta1");
+        verify(chipMock.chip).removeStaIface("wlan1");
         verify(staDestroyedListener2).onDestroyed(getName(staIface2));
 
         // request STA2: should fail
@@ -1817,7 +1817,7 @@
                 false, // chipModeValid
                 -1000, // chipModeId (only used if chipModeValid is true)
                 IfaceType.STA, // ifaceTypeToCreate
-                "sta0", // ifaceName
+                "wlan0", // ifaceName
                 onlyChipMode, // finalChipMode
                 null, // tearDownList
                 staDestroyedListener, // destroyedListener
@@ -1829,7 +1829,7 @@
                 true, // chipModeValid
                 onlyChipMode, // chipModeId
                 IfaceType.NAN, // ifaceTypeToCreate
-                "nan0", // ifaceName
+                "wlan0", // ifaceName
                 onlyChipMode, // finalChipMode
                 null, // tearDownList
                 nanDestroyedListener, // destroyedListener
@@ -1871,7 +1871,7 @@
                 true, // chipModeValid
                 onlyChipMode, // chipModeId
                 IfaceType.NAN, // ifaceTypeToCreate
-                "nan0", // ifaceName
+                "wlan0", // ifaceName
                 onlyChipMode, // finalChipMode
                 null, // tearDownList
                 nanDestroyedListener, // destroyedListener