summaryrefslogtreecommitdiff
path: root/Linux_x86/phDal4Nfc_i2c.c
diff options
context:
space:
mode:
authorNick Pelly <npelly@google.com>2010-09-23 12:47:58 -0700
committerNick Pelly <npelly@google.com>2010-09-23 13:53:18 -0700
commit5d9927ba30ba449badb9f6df0fbeb4d6aedc6e2a (patch)
tree190f9251c6db03d3550ec7f30b51a2561c01d9cf /Linux_x86/phDal4Nfc_i2c.c
parent4ff7c86a2c706b150078274455406f1b04966e1a (diff)
downloadlibnfc-nxp-5d9927ba30ba449badb9f6df0fbeb4d6aedc6e2a.tar.gz
Initial libnfc checkin
Source: Trusted_NFC_Device_Host_AA03.01e02_google.zip code drop (23-Sep-2010) Change-Id: Ie47f18423f949a8d3e0815d13f55c814312add24 Signed-off-by: Nick Pelly <npelly@google.com>
Diffstat (limited to 'Linux_x86/phDal4Nfc_i2c.c')
-rw-r--r--Linux_x86/phDal4Nfc_i2c.c226
1 files changed, 226 insertions, 0 deletions
diff --git a/Linux_x86/phDal4Nfc_i2c.c b/Linux_x86/phDal4Nfc_i2c.c
new file mode 100644
index 0000000..8a54bee
--- /dev/null
+++ b/Linux_x86/phDal4Nfc_i2c.c
@@ -0,0 +1,226 @@
+/*
+ * Copyright (C) 2010 NXP Semiconductors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * \file phDalNfc_i2c.c
+ * \brief DAL I2C port implementation for linux
+ *
+ * Project: Trusted NFC Linux
+ *
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <sys/ioctl.h>
+#include <sys/select.h>
+
+#include <phDal4Nfc_debug.h>
+#include <phDal4Nfc_i2c.h>
+#include <phOsalNfc.h>
+#include <phNfcStatus.h>
+#if defined(ANDROID)
+#include <string.h>
+#endif
+
+typedef struct
+{
+ int nHandle;
+ char nOpened;
+
+} phDal4Nfc_I2cPortContext_t;
+
+
+/*-----------------------------------------------------------------------------------
+ VARIABLES
+------------------------------------------------------------------------------------*/
+static phDal4Nfc_I2cPortContext_t gI2cPortContext;
+
+
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_set_open_from_handle
+
+PURPOSE: Initialize internal variables
+
+-----------------------------------------------------------------------------*/
+
+void phDal4Nfc_i2c_initialize(void)
+{
+ memset(&gI2cPortContext, 0, sizeof(phDal4Nfc_I2cPortContext_t));
+}
+
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_set_open_from_handle
+
+PURPOSE: The application could have opened the link itself. So we just need
+ to get the handle and consider that the open operation has already
+ been done.
+
+-----------------------------------------------------------------------------*/
+
+void phDal4Nfc_i2c_set_open_from_handle(phHal_sHwReference_t * pDalHwContext)
+{
+ gI2cPortContext.nHandle = (int) pDalHwContext->p_board_driver;
+ DAL_ASSERT_STR(gComPortContext.nHandle >= 0, "Bad passed com port handle");
+ gI2cPortContext.nOpened = 1;
+}
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_is_opened
+
+PURPOSE: Returns if the link is opened or not. (0 = not opened; 1 = opened)
+
+-----------------------------------------------------------------------------*/
+
+int phDal4Nfc_i2c_is_opened(void)
+{
+ return gI2cPortContext.nOpened;
+}
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_flush
+
+PURPOSE: Flushes the link ; clears the link buffers
+
+-----------------------------------------------------------------------------*/
+
+void phDal4Nfc_i2c_flush(void)
+{
+ /* Nothing to do (driver has no internal buffers) */
+}
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_close
+
+PURPOSE: Closes the link
+
+-----------------------------------------------------------------------------*/
+
+void phDal4Nfc_i2c_close(void)
+{
+ DAL_PRINT("Closing port\n");
+ if (gI2cPortContext.nOpened == 1)
+ {
+ close(gI2cPortContext.nHandle);
+ gI2cPortContext.nHandle = 0;
+ gI2cPortContext.nOpened = 0;
+ }
+}
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_open_and_configure
+
+PURPOSE: Closes the link
+
+-----------------------------------------------------------------------------*/
+
+NFCSTATUS phDal4Nfc_i2c_open_and_configure(pphDal4Nfc_sConfig_t pConfig, void ** pLinkHandle)
+{
+ char * pComPort;
+
+ DAL_ASSERT_STR(gI2cPortContext.nOpened==0, "Trying to open but already done!");
+
+ switch(pConfig->nLinkType)
+ {
+ case ENUM_DAL_LINK_TYPE_I2C:
+ pComPort = "/dev/pn544";
+ break;
+ default:
+ DAL_DEBUG("Open failed: unknown type %d\n", pConfig->nLinkType);
+ return NFCSTATUS_INVALID_PARAMETER;
+ }
+
+ DAL_DEBUG("Opening port=%s\n", pComPort);
+
+ /* open port */
+ gI2cPortContext.nHandle = open(pComPort, O_RDWR | O_NOCTTY);
+ if (gI2cPortContext.nHandle < 0)
+ {
+ DAL_DEBUG("Open failed: open() returned %d\n", gI2cPortContext.nHandle);
+ *pLinkHandle = NULL;
+ return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE);
+ }
+
+ gI2cPortContext.nOpened = 1;
+ *pLinkHandle = (void*)gI2cPortContext.nHandle;
+
+ DAL_PRINT("Open succeed\n");
+
+ return NFCSTATUS_SUCCESS;
+}
+
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_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_i2c_read(uint8_t * pBuffer, int nNbBytesToRead)
+{
+ int ret;
+ DAL_ASSERT_STR(gComPortContext.nOpened == 1, "read called but not opened!");
+
+ DAL_DEBUG("Reading %d bytes\n", nNbBytesToRead);
+ ret = read(gI2cPortContext.nHandle, pBuffer, nNbBytesToRead);
+ if (ret < 0)
+ {
+ DAL_DEBUG("Read failed: read() returned %d\n", ret);
+ }
+ else
+ {
+ DAL_DEBUG("Read succeed (%d bytes)\n", ret);
+ }
+ return ret;
+}
+
+/*-----------------------------------------------------------------------------
+
+FUNCTION: phDal4Nfc_i2c_write
+
+PURPOSE: Writes nNbBytesToWrite bytes from pBuffer to the link
+ Returns the number of bytes that have been wrote to the interface or -1 in case of error.
+
+-----------------------------------------------------------------------------*/
+
+int phDal4Nfc_i2c_write(uint8_t * pBuffer, int nNbBytesToWrite)
+{
+ int ret;
+ DAL_ASSERT_STR(gComPortContext.nOpened == 1, "write called but not opened!");
+
+ DAL_DEBUG("Writing %d bytes\n", nNbBytesToWrite);
+ ret = write(gI2cPortContext.nHandle, pBuffer, nNbBytesToWrite);
+ if (ret < 0)
+ {
+ DAL_DEBUG("Write failed: write() returned %d \n", ret);
+ }
+ else
+ {
+ DAL_DEBUG("Write succeed (%d bytes)\n", ret);
+ }
+ return ret;
+}