void phDal4Nfc_uart_flush(void) { int ret; /* flushes the com port */ ret = tcflush(gComPortContext.nHandle, TCIFLUSH); DAL_ASSERT_STR(ret!=-1, "tcflush failed"); }
int phDal4Nfc_uart_write(uint8_t * pBuffer, int nNbBytesToWrite) { fd_set wfds; struct timeval tv; int ret; DAL_ASSERT_STR(gComPortContext.nOpened == 1, "write called but not opened!"); FD_ZERO(&wfds); FD_SET(gComPortContext.nHandle, &wfds); /* select will block for 10 sec */ tv.tv_sec = 2; tv.tv_usec = 0; ret = select(gComPortContext.nHandle + 1, NULL, &wfds, NULL, &tv); if (ret == -1) return -1; if (ret) return write(gComPortContext.nHandle, pBuffer, nNbBytesToWrite); return 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; }
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); #ifdef SWISSKNIFEVERSION if(nNbBytesToWrite > 4) { last_ns = pBuffer[1] & 0x07; last_nr = (pBuffer[1] & 0x38) >> 3; }
void phDal4Nfc_uart_set_open_from_handle(phHal_sHwReference_t * pDalHwContext) { gComPortContext.nHandle = (int) pDalHwContext->p_board_driver; DAL_ASSERT_STR(gComPortContext.nHandle >= 0, "Bad passed com port handle"); gComPortContext.nOpened = 1; }
NFCSTATUS phDal4Nfc_uart_open_and_configure(pphDal4Nfc_sConfig_t pConfig, void ** pLinkHandle) { char * pComPort; int nComStatus; NFCSTATUS nfcret = NFCSTATUS_SUCCESS; int ret; DAL_ASSERT_STR(gComPortContext.nOpened==0, "Trying to open but already done!"); switch(pConfig->nLinkType) { case ENUM_DAL_LINK_TYPE_COM1: pComPort = "/dev/ttyS0"; break; case ENUM_DAL_LINK_TYPE_COM2: pComPort = "/dev/ttyS1"; break; case ENUM_DAL_LINK_TYPE_COM3: pComPort = "/dev/ttyS2"; break; case ENUM_DAL_LINK_TYPE_COM4: pComPort = "/dev/ttyS3"; break; case ENUM_DAL_LINK_TYPE_COM5: pComPort = "/dev/ttyS4"; break; case ENUM_DAL_LINK_TYPE_COM6: pComPort = "/dev/ttyS5"; break; case ENUM_DAL_LINK_TYPE_COM7: pComPort = "/dev/ttyS6"; break; case ENUM_DAL_LINK_TYPE_COM8: pComPort = "/dev/ttyS7"; break; case ENUM_DAL_LINK_TYPE_USB: pComPort = "/dev/ttyUSB0"; break; default: return NFCSTATUS_INVALID_PARAMETER; } /* open communication port handle */ gComPortContext.nHandle = open(pComPort, O_RDWR | O_NOCTTY); if (gComPortContext.nHandle < 0) { *pLinkHandle = NULL; return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE); } gComPortContext.nOpened = 1; *pLinkHandle = (void*)gComPortContext.nHandle; /* * Now configure the com port */ ret = tcgetattr(gComPortContext.nHandle, &gComPortContext.nIoConfigBackup); /* save the old io config */ if (ret == -1) { /* tcgetattr failed -- it is likely that the provided port is invalid */ *pLinkHandle = NULL; return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE); } ret = fcntl(gComPortContext.nHandle, F_SETFL, 0); /* Makes the read blocking (default). */ DAL_ASSERT_STR(ret != -1, "fcntl failed"); /* Configures the io */ memset((void *)&gComPortContext.nIoConfig, (int)0, (size_t)sizeof(struct termios)); /* BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. CRTSCTS : output hardware flow control (only used if the cable has all necessary lines. See sect. 7 of Serial-HOWTO) CS8 : 8n1 (8bit,no parity,1 stopbit) CLOCAL : local connection, no modem contol CREAD : enable receiving characters */ gComPortContext.nIoConfig.c_cflag = DAL_BAUD_RATE | CS8 | CLOCAL | CREAD; /* Control mode flags */ gComPortContext.nIoConfig.c_iflag = IGNPAR; /* Input mode flags : IGNPAR Ignore parity errors */ gComPortContext.nIoConfig.c_oflag = 0; /* Output mode flags */ gComPortContext.nIoConfig.c_lflag = 0; /* Local mode flags. Read mode : non canonical, no echo */ gComPortContext.nIoConfig.c_cc[VTIME] = 0; /* Control characters. No inter-character timer */ gComPortContext.nIoConfig.c_cc[VMIN] = 1; /* Control characters. Read is blocking until X characters are read */ /* TCSANOW Make changes now without waiting for data to complete TCSADRAIN Wait until everything has been transmitted TCSAFLUSH Flush input and output buffers and make the change */ ret = tcsetattr(gComPortContext.nHandle, TCSANOW, &gComPortContext.nIoConfig); DAL_ASSERT_STR(ret != -1, "tcsetattr failed"); /* On linux the DTR signal is set by default. That causes a problem for pn544 chip because this signal is connected to "reset". So we clear it. (on windows it is cleared by default). */ ret = ioctl(gComPortContext.nHandle, TIOCMGET, &nComStatus); DAL_ASSERT_STR(ret != -1, "ioctl TIOCMGET failed"); nComStatus &= ~TIOCM_DTR; ret = ioctl(gComPortContext.nHandle, TIOCMSET, &nComStatus); DAL_ASSERT_STR(ret != -1, "ioctl TIOCMSET failed"); DAL_DEBUG("Com port status=%d\n", nComStatus); usleep(10000); /* Mandatory sleep so that the DTR line is ready before continuing */ return nfcret; }
/*----------------------------------------------------------------------------- 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; }
/*----------------------------------------------------------------------------- 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; }