Snap for 5130385 from ab41ec0c1e8e602a0b8698e4498bbf3821ee9d02 to pi-qpr2-release

Change-Id: Ie529a1e079afa545642a46cf79ebb645112fba23
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);