Esempio n. 1
0
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;
}