Merge tag 'android-13.0.0_r52' into int/13/fp3

Android 13.0.0 Release 52 (TQ3A.230605.012)

* tag 'android-13.0.0_r52':
  Import translations. DO NOT MERGE ANYWHERE
  Import translations. DO NOT MERGE ANYWHERE
  Import translations. DO NOT MERGE ANYWHERE
  Fix "Connected via app / Connected / No internet access" summary
  Remove IMSI privacy warning from NetworkDetails
  Revert "Revert "[DO NOT MERGE] wifi: remove certificates for network factory reset""
  Revert "Revert "[DO NOT MERGE] wifi: remove certificates for network factory reset""
  Revert "[DO NOT MERGE] wifi: remove certificates for network factory reset"
  Revert "[DO NOT MERGE] wifi: remove certificates for network factory reset"
  Revert "[DO NOT MERGE] wifi: remove certificates for network factory reset"

Change-Id: Id3c81ddc1d64aabe31ad07c371975b1e78c71a73
diff --git a/libwifi_hal/wifi_hal_common.cpp b/libwifi_hal/wifi_hal_common.cpp
index 5c956f2..93d6002 100644
--- a/libwifi_hal/wifi_hal_common.cpp
+++ b/libwifi_hal/wifi_hal_common.cpp
@@ -25,6 +25,7 @@
 #include <cutils/misc.h>
 #include <cutils/properties.h>
 #include <sys/syscall.h>
+#include <android-base/properties.h>
 
 extern "C" int init_module(void *, unsigned long, const char *);
 extern "C" int delete_module(const char *, unsigned int);
@@ -58,7 +59,7 @@
 #endif
 
 static int insmod(const char *filename, const char *args) {
-  int ret;
+  int ret = 0;
   int fd;
 
   fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
@@ -67,13 +68,13 @@
     return -1;
   }
 
-  ret = syscall(__NR_finit_module, fd, args, 0);
-
-  close(fd);
-  if (ret < 0) {
+  if (syscall(__NR_finit_module, fd, args, 0) < 0) {
+    ret = -errno;
     PLOG(ERROR) << "finit_module return: " << ret;
   }
 
+  close(fd);
+
   return ret;
 }
 
@@ -81,6 +82,12 @@
   int ret = -1;
   int maxtry = 10;
 
+  std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
+  if (!powerCtl.empty()) {
+    PLOG(ERROR) << powerCtl<<" :skipping rmmod";
+    return ret;
+  }
+
   while (maxtry-- > 0) {
     ret = delete_module(modname, O_NONBLOCK | O_EXCL);
     if (ret < 0 && errno == EAGAIN)
@@ -183,7 +190,25 @@
     return 0;
   }
 
-  if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0) return -1;
+  /*
+   * Sometimes the wifi HAL can get into a state where the module is already
+   * loaded in a broken state and the wifi HAL isn't aware of this. In this
+   * case try unloading and reloading the kernel module.
+   */
+  int err = insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG);
+  if (err == -EEXIST) {
+    if (rmmod(DRIVER_MODULE_NAME) != 0) {
+      LOG(ERROR) << "After module load failure tried rmmod but failed.";
+      return -1;
+    }
+    if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0) {
+      LOG(ERROR) << "After module load failure tried rmmod and insmod but failed.";
+      return -1;
+    }
+    LOG(INFO) << "After module load failure tried rmmod and insmod and succeeded.";
+  } else if (err < 0) {
+    return -1;
+  }
 #endif
 
 #ifdef WIFI_DRIVER_STATE_CTRL_PARAM
diff --git a/libwifi_system/hostapd_manager.cpp b/libwifi_system/hostapd_manager.cpp
index 3f2e27f..7ce971e 100644
--- a/libwifi_system/hostapd_manager.cpp
+++ b/libwifi_system/hostapd_manager.cpp
@@ -22,8 +22,19 @@
 namespace android {
 namespace wifi_system {
 const char kHostapdServiceName[] = "hostapd";
+const char kHostapdFullServiceName[] = "init.svc.hostapd";
 
 bool HostapdManager::StartHostapd() {
+
+  // Check if hostapd already started
+  char hostapd_status[PROPERTY_VALUE_MAX];
+  property_get(kHostapdFullServiceName, hostapd_status, "");
+
+  if (strcmp(hostapd_status, "running") == 0) {
+    LOG(DEBUG) << "SoftAP already started. Skip another start";
+    return true;
+  }
+
   if (property_set("ctl.start", kHostapdServiceName) != 0) {
     LOG(ERROR) << "Failed to start SoftAP";
     return false;
@@ -36,6 +47,15 @@
 bool HostapdManager::StopHostapd() {
   LOG(DEBUG) << "Stopping the SoftAP service...";
 
+  // Check if hostapd already stopped
+  char hostapd_status[PROPERTY_VALUE_MAX];
+  property_get(kHostapdFullServiceName, hostapd_status, "");
+
+  if (!strlen(hostapd_status) || strcmp(hostapd_status, "stopped") == 0) {
+    LOG(DEBUG) << "SoftAP already stopped. Skip another stop";
+    return true;
+  }
+
   if (property_set("ctl.stop", kHostapdServiceName) < 0) {
     LOG(ERROR) << "Failed to stop hostapd service!";
     return false;
diff --git a/libwifi_system/supplicant_manager.cpp b/libwifi_system/supplicant_manager.cpp
index 60720d4..33f7649 100644
--- a/libwifi_system/supplicant_manager.cpp
+++ b/libwifi_system/supplicant_manager.cpp
@@ -34,6 +34,7 @@
 
 const char kSupplicantInitProperty[] = "init.svc.wpa_supplicant";
 const char kSupplicantServiceName[] = "wpa_supplicant";
+const char kMigrationServiceName[] = "vendor.move_wifi_data";
 
 }  // namespace
 
@@ -61,6 +62,7 @@
     serial = __system_property_serial(pi);
   }
 
+  property_set("ctl.start", kMigrationServiceName);
   property_set("ctl.start", kSupplicantServiceName);
   sched_yield();
 
@@ -97,6 +99,7 @@
     return true;
   }
 
+  property_set("ctl.stop", kMigrationServiceName);
   property_set("ctl.stop", kSupplicantServiceName);
   sched_yield();