コード例 #1
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
NFCSTATUS phDal4Nfc_Write( void *pContext, void *pHwRef,uint8_t *pBuffer, uint16_t length)
{
    NFCSTATUS result = NFCSTATUS_SUCCESS;
    static int       MsgType= PHDAL4NFC_WRITE_MESSAGE;
    int *            pmsgType=&MsgType;
    phDal4Nfc_Message_t      sMsg;
    phOsalNfc_Message_t      OsalMsg;

    if ((NULL != pContext) && (NULL != pHwRef)&&
            (NULL != pBuffer) && (0 != length))
    {
        if( gDalContext.hw_valid== TRUE)
        {
            if((!gReadWriteContext.nWriteBusy)&&
                    (!gReadWriteContext.nWaitingOnWrite))
            {
                DAL_PRINT("phDal4Nfc_Write() : Temporary buffer !! \n");
                gReadWriteContext.pTempWriteBuffer = (uint8_t*)malloc(length * sizeof(uint8_t));
                /* Make a copy of the passed arguments */
                memcpy(gReadWriteContext.pTempWriteBuffer,pBuffer,length);
                DAL_DEBUG("phDal4Nfc_Write(): %d\n", length);
                gReadWriteContext.pWriteBuffer = gReadWriteContext.pTempWriteBuffer;
                gReadWriteContext.nNbOfBytesToWrite  = length;
                /* Change the write state so that thread can take over the write */
                gReadWriteContext.nWriteBusy = TRUE;
                /* Just set variable here. This is the trigger for the Write thread */
                gReadWriteContext.nWaitingOnWrite = TRUE;
                /* Update the error state */
                result = NFCSTATUS_PENDING;
                /* Send Message and perform physical write in the DefferedCallback */
                /* read completed immediately */
                sMsg.eMsgType= PHDAL4NFC_WRITE_MESSAGE;
                /* Update the state */
                phDal4Nfc_FillMsg(&sMsg,&OsalMsg);
                phDal4Nfc_DeferredCall((pphDal4Nfc_DeferFuncPointer_t)phDal4Nfc_DeferredCb,(void *)pmsgType);
                memset(&sMsg,0,sizeof(phDal4Nfc_Message_t));
                memset(&OsalMsg,0,sizeof(phOsalNfc_Message_t));
            }
            else
            {
                /* Driver is BUSY with previous Write */
                DAL_PRINT("phDal4Nfc_Write() : Busy \n");
                result = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_BUSY) ;
            }
        }
        else
        {
            /* TBD :Additional error code : NOT_INITIALISED */
            result = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE);
        }

    }/*end if-Input parametrs valid-check*/
    else
    {
        result = NFCSTATUS_INVALID_PARAMETER;
    }
    return result;
}
コード例 #2
0
ファイル: phDal4Nfc.c プロジェクト: Abocer/android-4.2_r1
NFCSTATUS phDal4Nfc_ConfigRelease(void *pHwRef)
{

   NFCSTATUS result = NFCSTATUS_SUCCESS;
   void * pThreadReturn;
   
   DAL_PRINT("phDal4Nfc_ConfigRelease ");

   if (gDalContext.hw_valid == TRUE)
   {
       /* Signal the read and write threads to exit.  NOTE: there
          actually is no write thread!  :)  */
       DAL_PRINT("Stop Reader Thread");
       gReadWriteContext.nReadThreadAlive = 0;
       gReadWriteContext.nWriteThreadAlive = 0;

       /* Wake up the read thread so it can exit */
       DAL_PRINT("Release Read Semaphore");
       sem_post(&nfc_read_sem);

       DAL_DEBUG("phDal4Nfc_ConfigRelease - doing pthread_join(%d)",
                 gReadWriteContext.nReadThread);
       if (pthread_join(gReadWriteContext.nReadThread,  &pThreadReturn) != 0)
       {
           result = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED);
           DAL_PRINT("phDal4Nfc_ConfigRelease  KO");
       }

      /* Close the message queue */
#ifdef USE_MQ_MESSAGE_QUEUE
       mq_close(nDeferedCallMessageQueueId);
#endif

       /* Shutdown NFC Chip */
       phDal4Nfc_Reset(0);

      /* Close the link */
      gLinkFunc.close();

      if (gDalContext.pDev != NULL) {
          nfc_pn544_close(gDalContext.pDev);
      }
      /* Reset the Read Writer context to NULL */
      memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext));
      /* Reset the DAL context values to NULL */
      memset((void *)&gDalContext,0,sizeof(gDalContext));
   }

   gDalContext.hw_valid = FALSE;
   
   DAL_DEBUG("phDal4Nfc_ConfigRelease(): %04x\n", result);


   return result;
}
コード例 #3
0
int phDal4Nfc_uart_write(uint8_t * pBuffer, int nNbBytesToWrite)
{
    int ret;
    int numWrote = 0;

    DAL_ASSERT_STR(gComPortContext.nOpened == 1, "write called but not opened!");
    DAL_DEBUG("_uart_write() called to write %d bytes\n", nNbBytesToWrite);

/* Samsung modify for TIZEN */
#if 0
    while (numWrote < nNbBytesToWrite) {
        ret = write(gComPortContext.nHandle, pBuffer + numWrote, nNbBytesToWrite - numWrote);
        if (ret > 0) {
            DAL_DEBUG("wrote %d bytes", ret);
            numWrote += ret;
        } else if (ret == 0) {
            DAL_PRINT("_uart_write() EOF");
            return -1;
        } else {
            DAL_DEBUG("_uart_write() errno=%d", errno);
            if (errno == EINTR || errno == EAGAIN) {
                continue;
            }
            return -1;
        }
    }
#endif
    return numWrote;
}
コード例 #4
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
NFCSTATUS phDal4Nfc_ReadWaitCancel( void *pContext, void *pHwRef)
{
    DAL_PRINT("phDal4Nfc_ReadWaitCancel");

    /* unlock read semaphore */
    sem_post(&nfc_read_sem);

    return 0;
}
コード例 #5
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
NFCSTATUS phDal4Nfc_Read( void *pContext, void *pHwRef,uint8_t *pBuffer, uint16_t length)
{
    NFCSTATUS result = NFCSTATUS_SUCCESS;

    if ((NULL != pContext) && (NULL != pHwRef)&&
            (NULL != pBuffer) && (0 != length))
    {
        if ( gDalContext.hw_valid== TRUE)
        {
            if((!gReadWriteContext.nReadBusy)&&
                    (!gReadWriteContext.nWaitingOnRead))
            {
                DAL_DEBUG("*****DAl Read called  length : %d\n", length);

                /* Make a copy of the passed arguments */
                gReadWriteContext.pReadBuffer = pBuffer;
                gReadWriteContext.nNbOfBytesToRead  = length;
                /* Change the Read state so that thread can take over the read */
                gReadWriteContext.nReadBusy = TRUE;
                /* Just set variable here. This is the trigger for the Reader thread */
                gReadWriteContext.nWaitingOnRead = TRUE;
                /* Update the return state */
                result = NFCSTATUS_PENDING;
                /* unlock reader thread */
                sem_post(&nfc_read_sem);
            }
            else
            {
                /* Driver is BUSY with prev Read */
                DAL_PRINT("DAL BUSY\n");
                /* Make a copy of the passed arguments */
                gReadWriteContext.pReadBuffer = pBuffer;
                gReadWriteContext.nNbOfBytesToRead  = length;
                result = NFCSTATUS_PENDING;
            }
        }
        else
        {
            /* TBD :Additional error code : NOT_INITIALISED */
            result = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE);
        }
    }/*end if-Input parametrs valid-check*/
    else
    {
        result = NFCSTATUS_INVALID_PARAMETER;
    }
    DAL_DEBUG("*****DAl Read called  result : %x\n", result);
    return result;
}
コード例 #6
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
/**
 * \ingroup grp_nfc_dal
 *
 * \brief DAL Start threads function
 * This function is called from phDal4Nfc_Config and is responsible of creating the
 * reader thread.
 *
 * \retval NFCSTATUS_SUCCESS                    If success.
 * \retval NFCSTATUS_FAILED                     Can not create thread or retreive its attributes
 */
NFCSTATUS phDal4Nfc_StartThreads(void)
{
    pthread_attr_t nReadThreadAttributes;
    pthread_attr_t nWriteThreadAttributes;
    int ret;

    if(sem_init(&nfc_read_sem, 0, 0) == -1)
    {
        DAL_PRINT("NFC Init Semaphore creation Error");
        return -1;
    }

    ret = pthread_create(&gReadWriteContext.nReadThread, NULL,  (pphDal4Nfc_thread_handler_t)phDal4Nfc_ReaderThread,  (void*) "dal_read_thread");
    if(ret != 0)
        return(PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED));

    return NFCSTATUS_SUCCESS;
}
コード例 #7
0
ファイル: dal_mpool.c プロジェクト: Azure/sonic-buildimage
int
dal_mpool_debug(dal_mpool_mem_t* pool)
{
    dal_mpool_mem_t* ptr;
    int index = 0;

    MPOOL_LOCK();

    for (ptr = pool; ptr; ptr = ptr->next)
    {
//        DAL_PRINT("%2dst mpool block: address=0x%8x, size=0x%x \n", index, (unsigned int)ptr->address, ptr->size);
	 DAL_PRINT("%2dst mpool block: address=%p, size=0x%x \n", index, ptr->address, ptr->size); // note
        index++;
    }

    MPOOL_UNLOCK();

    return 0;
}
コード例 #8
0
/*-----------------------------------------------------------------------------

FUNCTION: phDal4Nfc_uart_write

PURPOSE:  Put the PN544 in download mode, using the GPIO4 pin

-----------------------------------------------------------------------------*/
int phDal4Nfc_uart_download()
{
    DAL_PRINT("phDal4Nfc_uart_download");

    return NFCSTATUS_FEATURE_NOT_SUPPORTED;
}
コード例 #9
0
/*-----------------------------------------------------------------------------

FUNCTION: phDal4Nfc_uart_reset

PURPOSE:  Reset the PN544, using the VEN pin

-----------------------------------------------------------------------------*/
int phDal4Nfc_uart_reset()
{
    DAL_PRINT("phDal4Nfc_uart_reset");

    return NFCSTATUS_FEATURE_NOT_SUPPORTED;
}
コード例 #10
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
/**
 * \ingroup grp_nfc_dal
 *
 * \brief DAL deferred callback function
 * Generic handler function called by a client thread when reading a message from the queue.
 * Will function will directly call the client function (same context). See phDal4Nfc_DeferredCall
 *
  * \param[in]       params    Parameter that will be passed to the client function.
 *
 */
void phDal4Nfc_DeferredCb (void  *params)
{
    int*    pParam=NULL;
    int     i;
    phNfc_sTransactionInfo_t TransactionInfo;

    pParam=(int*)params;

    switch(*pParam)
    {
    case PHDAL4NFC_READ_MESSAGE:
        DAL_PRINT(" Dal deferred read called \n");
        TransactionInfo.buffer=gReadWriteContext.pReadBuffer;
        TransactionInfo.length=(uint16_t)gReadWriteContext.nNbOfBytesRead;
        TransactionInfo.status=NFCSTATUS_SUCCESS;
        gReadWriteContext.nReadBusy = FALSE;


        /*  Reset flag so that another opertion can be issued.*/
        gReadWriteContext.nWaitingOnRead = FALSE;
        if ((NULL != pgDalContext) && (NULL != pgDalContext->cb_if.receive_complete))
        {
            pgDalContext->cb_if.receive_complete(pgDalContext->cb_if.pif_ctxt,
                                                 pgDalHwContext,&TransactionInfo);
        }

        break;
    case PHDAL4NFC_WRITE_MESSAGE:
        DAL_PRINT(" Dal deferred write called \n");

#ifdef LOW_LEVEL_TRACES
        phOsalNfc_PrintData("Send buffer", (uint16_t)gReadWriteContext.nNbOfBytesToWrite, gReadWriteContext.pWriteBuffer);
#endif

        /* DAL_DEBUG("dalMsg->transactInfo.length : %d\n", dalMsg->transactInfo.length); */
        /* Make a Physical WRITE */
        /* NOTE: need to usleep(3000) here if the write is for SWP */
        usleep(500);  /* NXP advise 500us sleep required between I2C writes */
        gReadWriteContext.nNbOfBytesWritten = gLinkFunc.write(gReadWriteContext.pWriteBuffer, gReadWriteContext.nNbOfBytesToWrite);
        if (gReadWriteContext.nNbOfBytesWritten != gReadWriteContext.nNbOfBytesToWrite)
        {
            /* controller may be in standby. do it again! */
            usleep(10000); /* wait 10 ms */
            gReadWriteContext.nNbOfBytesWritten = gLinkFunc.write(gReadWriteContext.pWriteBuffer, gReadWriteContext.nNbOfBytesToWrite);
        }
        if (gReadWriteContext.nNbOfBytesWritten != gReadWriteContext.nNbOfBytesToWrite)
        {
            /* Report write failure or timeout */
            DAL_PRINT(" Physical Write Error !!! \n");
            TransactionInfo.length=(uint16_t)gReadWriteContext.nNbOfBytesWritten;
            TransactionInfo.status = PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_BOARD_COMMUNICATION_ERROR);
        }
        else
        {
            DAL_PRINT(" Physical Write Success \n");
            TransactionInfo.length=(uint16_t)gReadWriteContext.nNbOfBytesWritten;
            TransactionInfo.status=NFCSTATUS_SUCCESS;
            DAL_PRINT("WriteBuff[]={ ");
            for (i = 0; i < gReadWriteContext.nNbOfBytesWritten; i++)
            {
                DAL_DEBUG("0x%x ", gReadWriteContext.pWriteBuffer[i]);
            }
            DAL_PRINT("}\n");

            // Free TempWriteBuffer
            if(gReadWriteContext.pTempWriteBuffer != NULL)
            {
                free(gReadWriteContext.pTempWriteBuffer);
            }
        }
        /* Reset Write context */
        gReadWriteContext.nWriteBusy = FALSE;
        gReadWriteContext.nWaitingOnWrite = FALSE;

        /* call LLC callback */
        if ((NULL != pgDalContext) && (NULL != pgDalContext->cb_if.send_complete))
        {
            pgDalContext->cb_if.send_complete(pgDalContext->cb_if.pif_ctxt,
                                              pgDalHwContext,&TransactionInfo);
        }
        break;
    default:
        break;
    }
}
コード例 #11
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
int phDal4Nfc_ReaderThread(void * pArg)
{
    char      retvalue;
    NFCSTATUS result = NFCSTATUS_SUCCESS;
    uint8_t   retry_cnt=0;
    void *    memsetRet;

    static int       MsgType= PHDAL4NFC_READ_MESSAGE;
    int *     pmsgType=&MsgType;

    phDal4Nfc_Message_t      sMsg;
    phOsalNfc_Message_t      OsalMsg ;
    int i;
    int i2c_error_count;

    pthread_setname_np(pthread_self(), "reader");

    /* Create the overlapped event. Must be closed before exiting
    to avoid a handle leak. This event is used READ API and the Reader thread*/

    DAL_PRINT("RX Thread \n");
    DAL_DEBUG("\nRX Thread nReadThreadAlive = %d",gReadWriteContext.nReadThreadAlive);
    DAL_DEBUG("\nRX Thread nWaitingOnRead = %d",gReadWriteContext.nWaitingOnRead);
    while(gReadWriteContext.nReadThreadAlive) /* Thread Loop */
    {
        /* Check for the read request from user */
        DAL_PRINT("RX Thread Sem Lock\n");
        sem_wait(&nfc_read_sem);
        DAL_PRINT("RX Thread Sem UnLock\n");

        if (!gReadWriteContext.nReadThreadAlive)
        {
            /* got the signal that we should exit.  NOTE: we don't
               attempt to read below, since the read may block */
            break;
        }

        /* Issue read operation.*/

        i2c_error_count = 0;
retry:
        gReadWriteContext.nNbOfBytesRead=0;
        DAL_DEBUG("RX Thread *New *** *****Request Length = %d",gReadWriteContext.nNbOfBytesToRead);
        memsetRet=memset(gReadWriteContext.pReadBuffer,0,gReadWriteContext.nNbOfBytesToRead);

        /* Wait for IRQ !!!  */
        gReadWriteContext.nNbOfBytesRead = gLinkFunc.read(gReadWriteContext.pReadBuffer, gReadWriteContext.nNbOfBytesToRead);

        /* TODO: Remove this hack
         * Reading the value 0x57 indicates a HW I2C error at I2C address 0x57
         * (pn544). There should not be false positives because a read of length 1
         * must be a HCI length read, and a length of 0x57 is impossible (max is 33).
         */
        if(gReadWriteContext.nNbOfBytesToRead == 1 && gReadWriteContext.pReadBuffer[0] == 0x57)
        {
            i2c_error_count++;
            DAL_DEBUG("RX Thread Read 0x57 %d times\n", i2c_error_count);
            if (i2c_error_count < 5) {
                usleep(2000);
                goto retry;
            }
            DAL_PRINT("RX Thread NOTHING TO READ, RECOVER");
            phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1);
        }
        else
        {
            i2c_error_count = 0;
#ifdef LOW_LEVEL_TRACES
            phOsalNfc_PrintData("Received buffer", (uint16_t)gReadWriteContext.nNbOfBytesRead, gReadWriteContext.pReadBuffer);
#endif
            DAL_DEBUG("RX Thread Read ok. nbToRead=%d\n", gReadWriteContext.nNbOfBytesToRead);
            DAL_DEBUG("RX Thread NbReallyRead=%d\n", gReadWriteContext.nNbOfBytesRead);
            DAL_PRINT("RX Thread ReadBuff[]={ ");
            for (i = 0; i < gReadWriteContext.nNbOfBytesRead; i++)
            {
                DAL_DEBUG("RX Thread 0x%x ", gReadWriteContext.pReadBuffer[i]);
            }
            DAL_PRINT("RX Thread }\n");

            /* read completed immediately */
            sMsg.eMsgType= PHDAL4NFC_READ_MESSAGE;
            /* Update the state */
            phDal4Nfc_FillMsg(&sMsg,&OsalMsg);
            phDal4Nfc_DeferredCall((pphDal4Nfc_DeferFuncPointer_t)phDal4Nfc_DeferredCb,(void *)pmsgType);
            memsetRet=memset(&sMsg,0,sizeof(phDal4Nfc_Message_t));
            memsetRet=memset(&OsalMsg,0,sizeof(phOsalNfc_Message_t));
        }

    } /* End of thread Loop*/

    DAL_PRINT("RX Thread  exiting");

    return TRUE;
}
コード例 #12
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
/*-----------------------------------------------------------------------------

FUNCTION: phDal4Nfc_Config

PURPOSE: Configure the serial port.

-----------------------------------------------------------------------------*/
NFCSTATUS phDal4Nfc_Config(pphDal4Nfc_sConfig_t config,void **phwref)
{
    NFCSTATUS                       retstatus = NFCSTATUS_SUCCESS;

    DAL_PRINT("phDal4Nfc_Config");

    if ((config == NULL) || (phwref == NULL) || (config->nClientId == -1))
        return NFCSTATUS_INVALID_PARAMETER;

    /* Register the link callbacks */
    memset(&gLinkFunc, 0, sizeof(phDal4Nfc_link_cbk_interface_t));
    switch(config->nLinkType)
    {
    case ENUM_DAL_LINK_TYPE_COM1:
    case ENUM_DAL_LINK_TYPE_COM2:
    case ENUM_DAL_LINK_TYPE_COM3:
    case ENUM_DAL_LINK_TYPE_COM4:
    case ENUM_DAL_LINK_TYPE_COM5:
    case ENUM_DAL_LINK_TYPE_USB:
    {
        DAL_PRINT("UART link Config");
        /* Uart link interface */
        gLinkFunc.init               = phDal4Nfc_uart_initialize;
        gLinkFunc.open_from_handle   = phDal4Nfc_uart_set_open_from_handle;
        gLinkFunc.is_opened          = phDal4Nfc_uart_is_opened;
        gLinkFunc.flush              = phDal4Nfc_uart_flush;
        gLinkFunc.close              = phDal4Nfc_uart_close;
        gLinkFunc.open_and_configure = phDal4Nfc_uart_open_and_configure;
        gLinkFunc.read               = phDal4Nfc_uart_read;
        gLinkFunc.write              = phDal4Nfc_uart_write;
        gLinkFunc.download           = phDal4Nfc_uart_download;
        gLinkFunc.reset              = phDal4Nfc_uart_reset;
    }
    break;

    case ENUM_DAL_LINK_TYPE_I2C:
    {
        DAL_PRINT("I2C link Config");
        /* i2c link interface */
        gLinkFunc.init               = phDal4Nfc_i2c_initialize;
        gLinkFunc.open_from_handle   = phDal4Nfc_i2c_set_open_from_handle;
        gLinkFunc.is_opened          = phDal4Nfc_i2c_is_opened;
        gLinkFunc.flush              = phDal4Nfc_i2c_flush;
        gLinkFunc.close              = phDal4Nfc_i2c_close;
        gLinkFunc.open_and_configure = phDal4Nfc_i2c_open_and_configure;
        gLinkFunc.read               = phDal4Nfc_i2c_read;
        gLinkFunc.write              = phDal4Nfc_i2c_write;
        gLinkFunc.reset              = phDal4Nfc_i2c_reset;
        break;
    }

    default:
    {
        /* Shound not happen : Bad parameter */
        return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_PARAMETER);
    }
    }

    gLinkFunc.init(); /* So that link interface can initialize its internal state */
    retstatus = gLinkFunc.open_and_configure(config, phwref);
    if (retstatus != NFCSTATUS_SUCCESS)
        return retstatus;

    /* Iniatilize the DAL context */
    (void)memset(&gDalContext,0,sizeof(phDal4Nfc_SContext_t));
    pgDalContext = &gDalContext;

    /* Reset the Reader Thread values to NULL */
    memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext));
    gReadWriteContext.nReadThreadAlive     = TRUE;
    gReadWriteContext.nWriteBusy = FALSE;
    gReadWriteContext.nWaitingOnWrite = FALSE;

    /* Prepare the message queue for the defered calls */
#ifdef USE_MQ_MESSAGE_QUEUE
    nDeferedCallMessageQueueId = mq_open(MQ_NAME_IDENTIFIER, O_CREAT|O_RDWR, 0666, &MQ_QUEUE_ATTRIBUTES);
#else
    nDeferedCallMessageQueueId = config->nClientId;
#endif
    /* Start Read and Write Threads */
    if(NFCSTATUS_SUCCESS != phDal4Nfc_StartThreads())
    {
        return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED);
    }

    gDalContext.hw_valid = TRUE;

    phDal4Nfc_Reset(1);
    phDal4Nfc_Reset(0);
    phDal4Nfc_Reset(1);

    return NFCSTATUS_SUCCESS;
}
コード例 #13
0
ファイル: phDal4Nfc.c プロジェクト: johnny/CobraDroidBeta
NFCSTATUS phDal4Nfc_ReadWait(void *pContext, void *pHwRef,uint8_t *pBuffer, uint16_t length)
{
    /* not used */
    DAL_PRINT("phDal4Nfc_ReadWait");
    return 0;
}
コード例 #14
0
ファイル: phDal4Nfc.c プロジェクト: Abocer/android-4.2_r1
/*-----------------------------------------------------------------------------

FUNCTION: phDal4Nfc_Config

PURPOSE: Configure the serial port.

-----------------------------------------------------------------------------*/
NFCSTATUS phDal4Nfc_Config(pphDal4Nfc_sConfig_t config,void **phwref)
{
   NFCSTATUS                       retstatus = NFCSTATUS_SUCCESS;
   const hw_module_t* hw_module;
   nfc_pn544_device_t* pn544_dev;
   uint8_t num_eeprom_settings;
   uint8_t* eeprom_settings;
   int ret;

   /* Retrieve the hw module from the Android NFC HAL */
   ret = hw_get_module(NFC_HARDWARE_MODULE_ID, &hw_module);
   if (ret) {
       ALOGE("hw_get_module() failed");
       return NFCSTATUS_FAILED;
   }
   ret = nfc_pn544_open(hw_module, &pn544_dev);
   if (ret) {
       ALOGE("Could not open pn544 hw_module");
       return NFCSTATUS_FAILED;
   }
   config->deviceNode = pn544_dev->device_node;
   if (config->deviceNode == NULL) {
       ALOGE("deviceNode NULL");
       return NFCSTATUS_FAILED;
   }

   DAL_PRINT("phDal4Nfc_Config");

   if ((config == NULL) || (phwref == NULL))
      return NFCSTATUS_INVALID_PARAMETER;

   /* Register the link callbacks */
   memset(&gLinkFunc, 0, sizeof(phDal4Nfc_link_cbk_interface_t));
   switch(pn544_dev->linktype)
   {
      case PN544_LINK_TYPE_UART:
      case PN544_LINK_TYPE_USB:
      {
	 DAL_PRINT("UART link Config");
         /* Uart link interface */
         gLinkFunc.init               = phDal4Nfc_uart_initialize;
         gLinkFunc.open_from_handle   = phDal4Nfc_uart_set_open_from_handle;
         gLinkFunc.is_opened          = phDal4Nfc_uart_is_opened;
         gLinkFunc.flush              = phDal4Nfc_uart_flush;
         gLinkFunc.close              = phDal4Nfc_uart_close;
         gLinkFunc.open_and_configure = phDal4Nfc_uart_open_and_configure;
         gLinkFunc.read               = phDal4Nfc_uart_read;
         gLinkFunc.write              = phDal4Nfc_uart_write;
         gLinkFunc.reset              = phDal4Nfc_uart_reset;
      }
      break;

      case PN544_LINK_TYPE_I2C:
      {
	 DAL_PRINT("I2C link Config");
         /* i2c link interface */
         gLinkFunc.init               = phDal4Nfc_i2c_initialize;
         gLinkFunc.open_from_handle   = phDal4Nfc_i2c_set_open_from_handle;
         gLinkFunc.is_opened          = phDal4Nfc_i2c_is_opened;
         gLinkFunc.flush              = phDal4Nfc_i2c_flush;
         gLinkFunc.close              = phDal4Nfc_i2c_close;
         gLinkFunc.open_and_configure = phDal4Nfc_i2c_open_and_configure;
         gLinkFunc.read               = phDal4Nfc_i2c_read;
         gLinkFunc.write              = phDal4Nfc_i2c_write;
         gLinkFunc.reset              = phDal4Nfc_i2c_reset;
         break;
      }

      default:
      {
         /* Shound not happen : Bad parameter */
         return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_PARAMETER);
      }
   }

   gLinkFunc.init(); /* So that link interface can initialize its internal state */
   retstatus = gLinkFunc.open_and_configure(config, phwref);
   if (retstatus != NFCSTATUS_SUCCESS)
      return retstatus;

   /* Iniatilize the DAL context */
   (void)memset(&gDalContext,0,sizeof(phDal4Nfc_SContext_t));
   pgDalContext = &gDalContext;
   
   /* Reset the Reader Thread values to NULL */
   memset((void *)&gReadWriteContext,0,sizeof(gReadWriteContext));
   gReadWriteContext.nReadThreadAlive     = TRUE;
   gReadWriteContext.nWriteBusy = FALSE;
   gReadWriteContext.nWaitingOnWrite = FALSE;
   
   /* Prepare the message queue for the defered calls */
#ifdef USE_MQ_MESSAGE_QUEUE
   nDeferedCallMessageQueueId = mq_open(MQ_NAME_IDENTIFIER, O_CREAT|O_RDWR, 0666, &MQ_QUEUE_ATTRIBUTES);
#else
   nDeferedCallMessageQueueId = config->nClientId;
#endif

   gDalContext.pDev = pn544_dev;

   /* Start Read and Write Threads */
   if(NFCSTATUS_SUCCESS != phDal4Nfc_StartThreads())
   {
      return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_FAILED);
   }

   gDalContext.hw_valid = TRUE;
   phDal4Nfc_Reset(1);
   phDal4Nfc_Reset(0);
   phDal4Nfc_Reset(1);

   return NFCSTATUS_SUCCESS;
}
コード例 #15
0
/*-----------------------------------------------------------------------------

FUNCTION: phDal4Nfc_uart_read

PURPOSE:  Reads nNbBytesToRead bytes and writes them in pBuffer.
          Returns the number of bytes really read or -1 in case of error.

-----------------------------------------------------------------------------*/
int phDal4Nfc_uart_read(uint8_t * pBuffer, int nNbBytesToRead)
{
    int ret;
    int numRead = 0;
    struct timeval tv;
    struct timeval *ptv;
    struct timespec timeout;
    fd_set rfds;

    DAL_ASSERT_STR(gComPortContext.nOpened == 1, "read called but not opened!");
    DAL_DEBUG("_uart_read() called to read %d bytes", nNbBytesToRead);

/* Samsung modify for TIZEN */
#if 0
//    read_property();

    // Read timeout:
    // FW mode: 10s timeout
    // 1 byte read: steady-state LLC length read, allowed to block forever
    // >1 byte read: LLC payload, 100ms timeout (before pn544 re-transmit)
    if (nNbBytesToRead > 1 && !libnfc_firmware_mode) {
        clock_gettime(CLOCK_MONOTONIC, &timeout);
        timeout.tv_nsec += 100000000;
        if (timeout.tv_nsec > 1000000000) {
            timeout.tv_sec++;
            timeout.tv_nsec -= 1000000000;
        }
        ptv = &tv;
    } else if (libnfc_firmware_mode) {
        clock_gettime(CLOCK_MONOTONIC, &timeout);
        timeout.tv_sec += 10;
        ptv = &tv;
    } else {
        ptv = NULL;
    }

    while (numRead < nNbBytesToRead) {
       FD_ZERO(&rfds);
       FD_SET(gComPortContext.nHandle, &rfds);

       if (ptv) {
          tv = timeval_remaining(timeout);
          ptv = &tv;
       }

       ret = select(gComPortContext.nHandle + 1, &rfds, NULL, NULL, ptv);
       if (ret < 0) {
           DAL_DEBUG("select() errno=%d", errno);
           if (errno == EINTR || errno == EAGAIN) {
               continue;
           }
           return -1;
       } else if (ret == 0) {
           LOGW("timeout!");
           break;  // return partial response
       }
       ret = read(gComPortContext.nHandle, pBuffer + numRead, nNbBytesToRead - numRead);
       if (ret > 0) {
           ret = apply_errors(pBuffer + numRead, ret);

           DAL_DEBUG("read %d bytes", ret);
           numRead += ret;
       } else if (ret == 0) {
           DAL_PRINT("_uart_read() EOF");
           return 0;
       } else {
           DAL_DEBUG("_uart_read() errno=%d", errno);
           if (errno == EINTR || errno == EAGAIN) {
               continue;
           }
           return -1;
       }
    }
#endif

    return numRead;
}
コード例 #16
0
/*-----------------------------------------------------------------------------

FUNCTION: phDal4Nfc_uart_read

PURPOSE:  Reads nNbBytesToRead bytes and writes them in pBuffer.
          Returns the number of bytes really read or -1 in case of error.

-----------------------------------------------------------------------------*/
int phDal4Nfc_uart_read(uint8_t * pBuffer, int nNbBytesToRead)
{
    int ret;
    int numRead = 0;
    struct timeval tv;
    struct timeval *ptv;
    struct timespec timeout;
    fd_set rfds;
#ifdef SWISSKNIFEVERSION
    struct timespec time,time2;
    clock_gettime(CLOCK_MONOTONIC, &time);
#endif

    DAL_ASSERT_STR(gComPortContext.nOpened == 1, "read called but not opened!");
    DAL_DEBUG("_uart_read() called to read %d bytes", nNbBytesToRead);

    read_property();

    // Read timeout:
    // FW mode: 10s timeout
    // 1 byte read: steady-state LLC length read, allowed to block forever
    // >1 byte read: LLC payload, 100ms timeout (before pn544 re-transmit)
    if (nNbBytesToRead > 1 && !libnfc_firmware_mode) {
        clock_gettime(CLOCK_MONOTONIC, &timeout);
        timeout.tv_nsec += 100000000;
        if (timeout.tv_nsec > 1000000000) {
            timeout.tv_sec++;
            timeout.tv_nsec -= 1000000000;
        }
        ptv = &tv;
    } else if (libnfc_firmware_mode) {
        clock_gettime(CLOCK_MONOTONIC, &timeout);
        timeout.tv_sec += 10;
        ptv = &tv;
    } else {
        ptv = NULL;
    }

    while (numRead < nNbBytesToRead) {
        FD_ZERO(&rfds);
        FD_SET(gComPortContext.nHandle, &rfds);

        if (ptv) {
            tv = timeval_remaining(timeout);
            ptv = &tv;
        }

        ret = select(gComPortContext.nHandle + 1, &rfds, NULL, NULL, ptv);

        if (ret < 0) {
            DAL_DEBUG("select() errno=%d", errno);
            if (errno == EINTR || errno == EAGAIN) {
                continue;
            }
            return -1;
        } else if (ret == 0) {
            ALOGW("timeout!");
            break;  // return partial response
        }
        ret = read(gComPortContext.nHandle, pBuffer + numRead, nNbBytesToRead - numRead);
        if (ret > 0) {
//           ret = apply_errors(pBuffer + numRead, ret);

            DAL_DEBUG("read %d bytes", ret);
            numRead += ret;
        } else if (ret == 0) {
            DAL_PRINT("_uart_read() EOF");
            return 0;
        } else {
            DAL_DEBUG("_uart_read() errno=%d", errno);
            if (errno == EINTR || errno == EAGAIN) {
                continue;
            }
            return -1;
        }
    }

#ifdef SWISSKNIFEVERSION
    clock_gettime(CLOCK_MONOTONIC, &time2);

#ifdef LOGSWISSKNIFE
    SwissKnife_Log(pBuffer,nNbBytesToRead,"Receiving message:");
#endif

    if(rapidBitExchange && receivedMessages < 32) {
        if(nNbBytesToRead == 1) {
            if(pBuffer[0] == 0x03) {
                msg3[receivedMessages] = (time2.tv_sec*1000000000)+time2.tv_nsec;
            }
            else if (pBuffer[0] == 0x07) {
                msgsize[receivedMessages] = (time2.tv_sec*1000000000)+time2.tv_nsec;
            }
        } else if(nNbBytesToRead == 7) {
            data[receivedMessages] = (time2.tv_sec*1000000000)+time2.tv_nsec;
        }
    }
#endif

    return numRead;
}