FactoryOTA: Disable factory OTA mode

When factoryOTA client set the property to "terminate", factoryOTA related setting should be cleared.
Check chipset's factory OTA setting and set the property to "terminated" if factoryOTA mode is disabled successfully.

Bug: 111528718
Test: NFC on/off, factoryOTA mode disabled and property set correctly
Change-Id: Iba1d7d99eb8ad3cf3375d6cfd5735d0093b0b1c6
(cherry picked from commit ea5d45291f2e740440adfa74f143299496db8c24)
diff --git a/halimpl/hal/phNxpNciHal.cc b/halimpl/hal/phNxpNciHal.cc
index 6a487fe..bf74a72 100755
--- a/halimpl/hal/phNxpNciHal.cc
+++ b/halimpl/hal/phNxpNciHal.cc
@@ -118,6 +118,8 @@
 static NFCSTATUS phNxpNciHal_CheckRFCmdRespStatus();
 static void phNxpNciHal_getPersistUiccSetting();
 int check_config_parameter();
+void phNxpNciHal_isFactoryOTAModeActive();
+static NFCSTATUS phNxpNciHal_disableFactoryOTAMode(void);
 /******************************************************************************
  * Function         phNxpNciHal_initialize_debug_enabled_flag
  *
@@ -1870,6 +1872,48 @@
 #endif
   return NFCSTATUS_SUCCESS;
 }
+
+void phNxpNciHal_isFactoryOTAModeActive() {
+  uint8_t check_factoryOTA[] = {0x20, 0x03, 0x05, 0x02, 0xA0, 0x08, 0xA0, 0x88};
+  NFCSTATUS status = NFCSTATUS_FAILED;
+  NXPLOG_NCIHAL_D("check FactoryOTA mode status");
+
+  status = phNxpNciHal_send_ext_cmd(sizeof(check_factoryOTA), check_factoryOTA);
+
+  if (status == NFCSTATUS_SUCCESS) {
+    if(nxpncihal_ctrl.p_rx_data[9] == 0x1 && nxpncihal_ctrl.p_rx_data[13] == 0x1) {
+      NXPLOG_NCIHAL_E("FactoryOTA mode is active");
+    } else {
+      NXPLOG_NCIHAL_D("FactoryOTA mode is disabled");
+      if (property_set("persist.factoryota.reboot", "terminated") < 0) {
+        NXPLOG_NCIHAL_E("Fail to set factoryOTA property");
+      }
+    }
+  } else {
+    NXPLOG_NCIHAL_E("Fail to get FactoryOTA mode status");
+  }
+  return;
+}
+
+NFCSTATUS phNxpNciHal_disableFactoryOTAMode() {
+  // NFCC GPIO output control
+  uint8_t nfcc_system_gpio[] = {0x20, 0x02, 0x06, 0x01, 0xA0, 0x08, 0x02, 0x00, 0x00};
+  // NFCC automatically sets GPIO once a specific RF pattern is detected
+  uint8_t nfcc_gpio_pattern[] = {0x20, 0x02, 0x08, 0x01, 0xA0, 0x88, 0x04, 0x00, 0x96, 0x96, 0x03};
+
+  NFCSTATUS status = NFCSTATUS_SUCCESS;
+  NXPLOG_NCIHAL_D("Disable FactoryOTA mode");
+  status = phNxpNciHal_send_ext_cmd(sizeof(nfcc_system_gpio), nfcc_system_gpio);
+  if (status != NFCSTATUS_SUCCESS ) {
+    NXPLOG_NCIHAL_E("Can't disable A008 for FactoryOTA mode");
+  }
+  status = phNxpNciHal_send_ext_cmd(sizeof(nfcc_gpio_pattern), nfcc_gpio_pattern);
+  if (status != NFCSTATUS_SUCCESS ) {
+    NXPLOG_NCIHAL_E("Can't disable A088 for FactoryOTA mode");
+  }
+  return status;
+}
+
 /******************************************************************************
  * Function         phNxpNciHal_CheckRFCmdRespStatus
  *
@@ -2020,6 +2064,18 @@
     }
   }
 
+  char valueStr[PROPERTY_VALUE_MAX] = {0};
+  bool factoryOTA_terminate = false;
+  int len = property_get("persist.factoryota.reboot", valueStr, "normal");
+  if (len > 0) {
+    factoryOTA_terminate = (len == 9 && (memcmp(valueStr, "terminate", len) == 0)) ? true : false;
+  }
+  NXPLOG_NCIHAL_D("factoryOTA_terminate: %d", factoryOTA_terminate);
+  if (factoryOTA_terminate) {
+    phNxpNciHal_disableFactoryOTAMode();
+    phNxpNciHal_isFactoryOTAModeActive();
+  }
+
   nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
 
   status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci), cmd_reset_nci);