Fix HAL implementation to check for error cases

- Call close() after close()
- Call open() after open()
- Call powerCycle() after close()
- Call write() after close()
- Call coreInitialized() after close()

Test: VTS test passes
Change-Id: Icce0c264cfc9bafb42982ef910433eda36a05729
(cherry picked from commit 17a5634495c4acea7d81222ecf13f232a1efb6a4)
diff --git a/halimpl/pn54x/hal/phNxpNciHal.c b/halimpl/pn54x/hal/phNxpNciHal.c
index 25b37fb..44b26c7 100644
--- a/halimpl/pn54x/hal/phNxpNciHal.c
+++ b/halimpl/pn54x/hal/phNxpNciHal.c
@@ -701,6 +701,11 @@
     static uint8_t cmd_init_nci[] = {0x20,0x01,0x00};
     /*NCI_RESET_CMD*/
     static uint8_t cmd_reset_nci[] = {0x20,0x00,0x01,0x00};
+
+    if (nxpncihal_ctrl.halStatus == HAL_STATUS_OPEN) {
+        NXPLOG_NCIHAL_E("phNxpNciHal_open already open");
+        return NFCSTATUS_SUCCESS;
+    }
     /* reset config cache */
     resetNxpConfig();
 
@@ -904,6 +909,9 @@
 {
     NFCSTATUS status = NFCSTATUS_FAILED;
     static phLibNfc_Message_t msg;
+    if (nxpncihal_ctrl.halStatus != HAL_STATUS_OPEN) {
+        return NFCSTATUS_FAILED;
+    }
     /* hard coded offsets in phNxpNciHal_print_packet */
     const uint16_t NCI_PRINT_OFFSET = 6;
 
@@ -1264,7 +1272,9 @@
     static uint8_t cmd_reset_nci[] = {0x20,0x00,0x01,0x00}; //keep configuration
     /* reset config cache */
     static uint8_t retry_core_init_cnt;
-
+    if (nxpncihal_ctrl.halStatus != HAL_STATUS_OPEN) {
+        return NFCSTATUS_FAILED;
+    }
     if((*p_core_init_rsp_params > 0) && (*p_core_init_rsp_params < 4)) //initializing for recovery.
     {
 retry_core_init:
@@ -2728,6 +2738,11 @@
     uint8_t ptr                         = 4;
     unsigned long uiccListenMask        = 0x00;
 
+    if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE) {
+        NXPLOG_NCIHAL_E("phNxpNciHal_close is already closed, ignoring close");
+        return NFCSTATUS_FAILED;
+    }
+
     if (!(GetNxpNumValue(NAME_NXP_UICC_LISTEN_TECH_MASK, &uiccListenMask, sizeof(uiccListenMask))))
     {
         uiccListenMask = 0x07;
@@ -3013,6 +3028,10 @@
 
     NFCSTATUS status = NFCSTATUS_FAILED;
 
+    if (nxpncihal_ctrl.halStatus != HAL_STATUS_OPEN) {
+        NXPLOG_NCIHAL_D("Power Cycle failed due to hal status not open");
+        return NFCSTATUS_FAILED;
+    }
     status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
 
     if(NFCSTATUS_SUCCESS == status)
diff --git a/halimpl/pn54x/hal/phNxpNciHal.h b/halimpl/pn54x/hal/phNxpNciHal.h
index cd65c88..c6e67d7 100644
--- a/halimpl/pn54x/hal/phNxpNciHal.h
+++ b/halimpl/pn54x/hal/phNxpNciHal.h
@@ -72,11 +72,7 @@
     uint8_t p_data[NCI_MAX_DATA_LEN];
 } nci_data_t;
 
-typedef enum
-{
-   HAL_STATUS_OPEN = 0,
-   HAL_STATUS_CLOSE
-} phNxpNci_HalStatus;
+typedef enum { HAL_STATUS_CLOSE = 0, HAL_STATUS_OPEN } phNxpNci_HalStatus;
 
 /* Macros to enable and disable extensions */
 #define HAL_ENABLE_EXT()    (nxpncihal_ctrl.hal_ext_enabled = 1)