Add firmware reload support for P2P

Refactor firmware reload call to support various modes - AP, STA & P2P

Bug: 5002384
Change-Id: I10d20fe5a7842437cf80861bc8a834990db87db5
diff --git a/services/java/com/android/server/NetworkManagementService.java b/services/java/com/android/server/NetworkManagementService.java
index cb5e968..4b7256a 100644
--- a/services/java/com/android/server/NetworkManagementService.java
+++ b/services/java/com/android/server/NetworkManagementService.java
@@ -854,7 +854,7 @@
         mContext.enforceCallingOrSelfPermission(
                 android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService");
         try {
-            mConnector.doCommand(String.format("softap fwreload " + wlanIface + " AP"));
+            wifiFirmwareReload(wlanIface, "AP");
             mConnector.doCommand(String.format("softap start " + wlanIface));
             if (wifiConfig == null) {
                 mConnector.doCommand(String.format("softap set " + wlanIface + " " + softapIface));
@@ -901,6 +901,20 @@
         }
     }
 
+    /* @param mode can be "AP", "STA" or "P2P" */
+    public void wifiFirmwareReload(String wlanIface, String mode) throws IllegalStateException {
+        mContext.enforceCallingOrSelfPermission(
+                android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
+        mContext.enforceCallingOrSelfPermission(
+                android.Manifest.permission.CHANGE_WIFI_STATE, "NetworkManagementService");
+
+        try {
+            mConnector.doCommand(String.format("softap fwreload " + wlanIface + " " + mode));
+        } catch (NativeDaemonConnectorException e) {
+            throw new IllegalStateException("Error communicating to native daemon ", e);
+        }
+    }
+
     public void stopAccessPoint(String wlanIface) throws IllegalStateException {
         mContext.enforceCallingOrSelfPermission(
                 android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
@@ -909,7 +923,7 @@
         try {
             mConnector.doCommand("softap stopap");
             mConnector.doCommand("softap stop " + wlanIface);
-            mConnector.doCommand(String.format("softap fwreload " + wlanIface + " STA"));
+            wifiFirmwareReload(wlanIface, "STA");
         } catch (NativeDaemonConnectorException e) {
             throw new IllegalStateException("Error communicating to native daemon to stop soft AP",
                     e);
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 4a0dcdf..77d0457 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -315,6 +315,7 @@
                 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
                 networkStats.bindConnectivityManager(connectivity);
                 networkPolicy.bindConnectivityManager(connectivity);
+                wifiP2p.connectivityServiceReady();
             } catch (Throwable e) {
                 Slog.e(TAG, "Failure starting Connectivity Service", e);
             }