Update the p2p device interface name

In some implementations P2P device interface is created under
primary interface(wlan0 by default). In those implementations
p2p device name is predefined in system property wifi.direct.interface.
And the interface is created by supplicant with primary interface as
the parent interface. The naming of p2p device interface is
p2p-dev-<parent interface> ("p2p-dev-wlan0").

With STA+STA feature, wlan0 interface gets deleted in certain scenarios
and wlan1 becomes the active interface. In such scenarios P2P fails to
create the interface as parent interface wlan0 is deleted.

To fix the issue update the p2p device interface from system property
based on the current active wlan interface. ie First get the p2p parent
interface name from p2p device interface name set in property.
Check if the parent interface derived from p2p device interface name is
the current active interface. If not, get the current active interface
and update the name as p2p-dev-<ifname> ("p2p-dev-wlan1").
This helps HIDL/supplicant to get an active wlan interface from p2p
device interface name and succeed in creating the p2p interface under the
active interface.

Bug: 203434193

Test: Manual - Tested STA+STA which ended up deleting wlan0 interface.
      Then ran p2p tests and confirmed that p2p scan and connection
      works.
Change-Id: I522cec02a662c057e21d434f3ed98c7e7a4ca8f0
Merged-In: I522cec02a662c057e21d434f3ed98c7e7a4ca8f0
(cherry picked from commit 7f2822aff735e3c4f727de0759f867791db129ed)
diff --git a/wifi/1.5/default/wifi_chip.cpp b/wifi/1.5/default/wifi_chip.cpp
index 82d794c..6bdff42 100644
--- a/wifi/1.5/default/wifi_chip.cpp
+++ b/wifi/1.5/default/wifi_chip.cpp
@@ -28,6 +28,8 @@
 #include "wifi_chip.h"
 #include "wifi_status_util.h"
 
+#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
+
 namespace {
 using android::sp;
 using android::base::unique_fd;
@@ -126,8 +128,37 @@
 }
 
 std::string getPredefinedP2pIfaceName() {
+    std::array<char, PROPERTY_VALUE_MAX> primaryIfaceName;
+    char p2pParentIfname[100];
+    std::string p2pDevIfName = "";
     std::array<char, PROPERTY_VALUE_MAX> buffer;
     property_get("wifi.direct.interface", buffer.data(), "p2p0");
+    if (strncmp(buffer.data(), P2P_MGMT_DEVICE_PREFIX,
+                strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
+        /* Get the p2p parent interface name from p2p device interface name set
+         * in property */
+        strncpy(p2pParentIfname, buffer.data() + strlen(P2P_MGMT_DEVICE_PREFIX),
+                strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX));
+        if (property_get(kActiveWlanIfaceNameProperty, primaryIfaceName.data(),
+                         nullptr) == 0) {
+            return buffer.data();
+        }
+        /* Check if the parent interface derived from p2p device interface name
+         * is active */
+        if (strncmp(p2pParentIfname, primaryIfaceName.data(),
+                    strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX)) !=
+            0) {
+            /*
+             * Update the predefined p2p device interface parent interface name
+             * with current active wlan interface
+             */
+            p2pDevIfName += P2P_MGMT_DEVICE_PREFIX;
+            p2pDevIfName += primaryIfaceName.data();
+            LOG(INFO) << "update the p2p device interface name to "
+                      << p2pDevIfName.c_str();
+            return p2pDevIfName;
+        }
+    }
     return buffer.data();
 }