MERGE: svn merge -r 20:23 trunk/src branches/portability/src
This commit is contained in:
parent
5391dfc902
commit
d74f0d13dd
8 changed files with 832 additions and 2 deletions
139
src/dev_arygon.c
Normal file
139
src/dev_arygon.c
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Public platform independent Near Field Communication (NFC) library
|
||||||
|
Copyright (C) 2009, Roel Verdult
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "dev_arygon.h"
|
||||||
|
#include "rs232.h"
|
||||||
|
#include "bitutils.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define SERIAL_STRING "COM"
|
||||||
|
#endif
|
||||||
|
#ifdef _LINUX
|
||||||
|
#define SERIAL_STRING "/dev/ttyusb"
|
||||||
|
#endif
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#define SERIAL_STRING "/dev/tty.SLAB_USBtoUART"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define BUFFER_LENGTH 256
|
||||||
|
#define USB_TIMEOUT 30000
|
||||||
|
static byte abtTxBuf[BUFFER_LENGTH] = { 0x32, 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
|
||||||
|
|
||||||
|
dev_info* dev_arygon_connect(const ui32 uiIndex)
|
||||||
|
{
|
||||||
|
ui32 uiDevNr;
|
||||||
|
serial_port sp;
|
||||||
|
char acConnect[BUFFER_LENGTH];
|
||||||
|
dev_info* pdi = INVALID_DEVICE_INFO;
|
||||||
|
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Trying to find ARYGON device on serial port: %s#\n",SERIAL_STRING);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// I have no idea how MAC OS X deals with multiple devices, so a quick workaround
|
||||||
|
for (uiDevNr=0; uiDevNr<MAX_DEVICES; uiDevNr++)
|
||||||
|
{
|
||||||
|
#ifdef __APPLE__
|
||||||
|
strcpy(acConnect,SERIAL_STRING);
|
||||||
|
sp = rs232_open(acConnect);
|
||||||
|
#else
|
||||||
|
sprintf(acConnect,"%s%d",SERIAL_STRING,uiDevNr);
|
||||||
|
sp = rs232_open(acConnect);
|
||||||
|
#endif
|
||||||
|
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) break;
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
if (sp == INVALID_SERIAL_PORT) printf("invalid serial port: %s\n",acConnect);
|
||||||
|
if (sp == CLAIMED_SERIAL_PORT) printf("serial port already claimed: %s\n",acConnect);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
// Test if we have found a device
|
||||||
|
if (uiDevNr == MAX_DEVICES) return INVALID_DEVICE_INFO;
|
||||||
|
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Succesfully connected to: %s\n",acConnect);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// We have a connection
|
||||||
|
pdi = malloc(sizeof(dev_info));
|
||||||
|
strcpy(pdi->acName,"ARYGON");
|
||||||
|
pdi->ct = CT_PN532;
|
||||||
|
pdi->ds = (dev_spec)sp;
|
||||||
|
pdi->bActive = true;
|
||||||
|
pdi->bCrc = true;
|
||||||
|
pdi->bPar = true;
|
||||||
|
pdi->ui8TxBits = 0;
|
||||||
|
return pdi;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dev_arygon_disconnect(dev_info* pdi)
|
||||||
|
{
|
||||||
|
rs232_close((serial_port)pdi->ds);
|
||||||
|
free(pdi);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen)
|
||||||
|
{
|
||||||
|
byte abtRxBuf[BUFFER_LENGTH];
|
||||||
|
ui32 uiRxBufLen = BUFFER_LENGTH;
|
||||||
|
ui32 uiPos;
|
||||||
|
|
||||||
|
// Packet length = data length (len) + checksum (1) + end of stream marker (1)
|
||||||
|
abtTxBuf[4] = uiTxLen;
|
||||||
|
// Packet length checksum
|
||||||
|
abtTxBuf[5] = BUFFER_LENGTH - abtTxBuf[4];
|
||||||
|
// Copy the PN53X command into the packet buffer
|
||||||
|
memmove(abtTxBuf+6,pbtTx,uiTxLen);
|
||||||
|
|
||||||
|
// Calculate data payload checksum
|
||||||
|
abtTxBuf[uiTxLen+6] = 0;
|
||||||
|
for(uiPos=0; uiPos < uiTxLen; uiPos++)
|
||||||
|
{
|
||||||
|
abtTxBuf[uiTxLen+6] -= abtTxBuf[uiPos+6];
|
||||||
|
}
|
||||||
|
|
||||||
|
// End of stream marker
|
||||||
|
abtTxBuf[uiTxLen+7] = 0;
|
||||||
|
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Tx: ");
|
||||||
|
print_hex(abtTxBuf,uiTxLen+8);
|
||||||
|
#endif
|
||||||
|
if (!rs232_send((serial_port)ds,abtTxBuf,uiTxLen+8)) return false;
|
||||||
|
|
||||||
|
if (!rs232_receive((serial_port)ds,abtRxBuf,&uiRxBufLen)) return false;
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Rx: ");
|
||||||
|
print_hex(abtRxBuf,uiRxBufLen);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// When the answer should be ignored, just return a succesful result
|
||||||
|
if(pbtRx == null || puiRxLen == null) return true;
|
||||||
|
|
||||||
|
// Only succeed when the result is at least 00 00 ff 00 ff 00 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
|
||||||
|
if(uiRxBufLen < 15) return false;
|
||||||
|
|
||||||
|
// Remove the preceding and appending bytes 00 00 ff 00 ff 00 00 00 FF xx Fx .. .. .. xx 00 (x = variable)
|
||||||
|
*puiRxLen = uiRxBufLen - 15;
|
||||||
|
memcpy(pbtRx, abtRxBuf+13, *puiRxLen);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
35
src/dev_arygon.h
Normal file
35
src/dev_arygon.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Public platform independent Near Field Communication (NFC) library
|
||||||
|
Copyright (C) 2009, Roel Verdult
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBNFC_DEV_ARYGON_H_
|
||||||
|
#define _LIBNFC_DEV_ARYGON_H_
|
||||||
|
|
||||||
|
#include "defines.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
// Functions used by developer to handle connection to this device
|
||||||
|
dev_info* dev_arygon_connect(const ui32 uiIndex);
|
||||||
|
void dev_arygon_disconnect(dev_info* pdi);
|
||||||
|
|
||||||
|
// Callback function used by libnfc to transmit commands to the PN53X chip
|
||||||
|
bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen);
|
||||||
|
|
||||||
|
#endif // _LIBNFC_DEV_ARYGON_H_
|
||||||
|
|
|
@ -41,7 +41,7 @@ typedef struct {
|
||||||
} dev_spec_pn531;
|
} dev_spec_pn531;
|
||||||
|
|
||||||
// Find transfer endpoints for bulk transfers
|
// Find transfer endpoints for bulk transfers
|
||||||
void get_end_points(struct usb_device *dev, dev_spec_pn531* pdsp)
|
static void get_end_points(struct usb_device *dev, dev_spec_pn531* pdsp)
|
||||||
{
|
{
|
||||||
uint32_t uiIndex;
|
uint32_t uiIndex;
|
||||||
uint32_t uiEndPoint;
|
uint32_t uiEndPoint;
|
||||||
|
@ -80,6 +80,8 @@ dev_info* dev_pn531_connect(const uint32_t uiIndex)
|
||||||
{
|
{
|
||||||
int idvendor = 0x04CC;
|
int idvendor = 0x04CC;
|
||||||
int idproduct = 0x0531;
|
int idproduct = 0x0531;
|
||||||
|
int idvendor_alt = 0x054c;
|
||||||
|
int idproduct_alt = 0x0193;
|
||||||
struct usb_bus *bus;
|
struct usb_bus *bus;
|
||||||
struct usb_device *dev;
|
struct usb_device *dev;
|
||||||
dev_info* pdi = INVALID_DEVICE_INFO;
|
dev_info* pdi = INVALID_DEVICE_INFO;
|
||||||
|
@ -102,7 +104,8 @@ dev_info* dev_pn531_connect(const uint32_t uiIndex)
|
||||||
{
|
{
|
||||||
for (dev = bus->devices; dev; dev = dev->next)
|
for (dev = bus->devices; dev; dev = dev->next)
|
||||||
{
|
{
|
||||||
if (idvendor==dev->descriptor.idVendor && idproduct==dev->descriptor.idProduct)
|
if ((idvendor==dev->descriptor.idVendor && idproduct==dev->descriptor.idProduct) ||
|
||||||
|
(idvendor_alt==dev->descriptor.idVendor && idproduct_alt==dev->descriptor.idProduct))
|
||||||
{
|
{
|
||||||
// Make sure there are 2 endpoints available
|
// Make sure there are 2 endpoints available
|
||||||
if (dev->config->interface->altsetting->bNumEndpoints < 2) return pdi;
|
if (dev->config->interface->altsetting->bNumEndpoints < 2) return pdi;
|
||||||
|
|
256
src/dev_pn533.c
Normal file
256
src/dev_pn533.c
Normal file
|
@ -0,0 +1,256 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Public platform independent Near Field Communication (NFC) library
|
||||||
|
Copyright (C) 2009, Roel Verdult
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Thanks to d18c7db and Okko for example code
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "defines.h"
|
||||||
|
|
||||||
|
#include <usb.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "dev_pn533.h"
|
||||||
|
#include "bitutils.h"
|
||||||
|
|
||||||
|
#define BUFFER_LENGTH 256
|
||||||
|
#define USB_TIMEOUT 30000
|
||||||
|
static char buffer[BUFFER_LENGTH] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
usb_dev_handle* pudh;
|
||||||
|
ui32 uiEndPointIn;
|
||||||
|
ui32 uiEndPointOut;
|
||||||
|
} dev_spec_pn533;
|
||||||
|
|
||||||
|
// Find transfer endpoints for bulk transfers
|
||||||
|
static void get_end_points(struct usb_device *dev, dev_spec_pn533* pdsp)
|
||||||
|
{
|
||||||
|
ui32 uiIndex;
|
||||||
|
ui32 uiEndPoint;
|
||||||
|
struct usb_interface_descriptor* puid = dev->config->interface->altsetting;
|
||||||
|
|
||||||
|
// 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out
|
||||||
|
for(uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++)
|
||||||
|
{
|
||||||
|
// Only accept bulk transfer endpoints (ignore interrupt endpoints)
|
||||||
|
if(puid->endpoint[uiIndex].bmAttributes != USB_ENDPOINT_TYPE_BULK) continue;
|
||||||
|
|
||||||
|
// Copy the endpoint to a local var, makes it more readable code
|
||||||
|
uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress;
|
||||||
|
|
||||||
|
// Test if we dealing with a bulk IN endpoint
|
||||||
|
if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN)
|
||||||
|
{
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
|
||||||
|
#endif
|
||||||
|
pdsp->uiEndPointIn = uiEndPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test if we dealing with a bulk OUT endpoint
|
||||||
|
if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT)
|
||||||
|
{
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
|
||||||
|
#endif
|
||||||
|
pdsp->uiEndPointOut = uiEndPoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dev_info* dev_pn533_connect(const ui32 uiIndex)
|
||||||
|
{
|
||||||
|
int idvendor = 0x04e6;
|
||||||
|
int idproduct = 0x5591;
|
||||||
|
struct usb_bus *bus;
|
||||||
|
struct usb_device *dev;
|
||||||
|
dev_info* pdi = INVALID_DEVICE_INFO;
|
||||||
|
dev_spec_pn533* pdsp;
|
||||||
|
dev_spec_pn533 dsp;
|
||||||
|
ui32 uiDevIndex;
|
||||||
|
|
||||||
|
dsp.uiEndPointIn = 0;
|
||||||
|
dsp.uiEndPointOut = 0;
|
||||||
|
dsp.pudh = null;
|
||||||
|
|
||||||
|
usb_init();
|
||||||
|
if (usb_find_busses() < 0) return INVALID_DEVICE_INFO;
|
||||||
|
if (usb_find_devices() < 0) return INVALID_DEVICE_INFO;
|
||||||
|
|
||||||
|
// Initialize the device index we are seaching for
|
||||||
|
uiDevIndex = uiIndex;
|
||||||
|
|
||||||
|
for (bus = usb_get_busses(); bus; bus = bus->next)
|
||||||
|
{
|
||||||
|
for (dev = bus->devices; dev; dev = dev->next)
|
||||||
|
{
|
||||||
|
if (idvendor==dev->descriptor.idVendor && idproduct==dev->descriptor.idProduct)
|
||||||
|
{
|
||||||
|
// Make sure there are 2 endpoints available
|
||||||
|
if (dev->config->interface->altsetting->bNumEndpoints < 2) return pdi;
|
||||||
|
|
||||||
|
// Test if we are looking for this device according to the current index
|
||||||
|
if (uiDevIndex != 0)
|
||||||
|
{
|
||||||
|
// Nope, we maybe want the next one, let's try to find another
|
||||||
|
uiDevIndex--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Found PN533 device\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Open the PN533 USB device
|
||||||
|
dsp.pudh = usb_open(dev);
|
||||||
|
|
||||||
|
get_end_points(dev,&dsp);
|
||||||
|
if(usb_set_configuration(dsp.pudh,1) < 0)
|
||||||
|
{
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Setting config failed\n");
|
||||||
|
#endif
|
||||||
|
usb_close(dsp.pudh);
|
||||||
|
return INVALID_DEVICE_INFO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(usb_claim_interface(dsp.pudh,0) < 0)
|
||||||
|
{
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Can't claim interface\n");
|
||||||
|
#endif
|
||||||
|
usb_close(dsp.pudh);
|
||||||
|
return INVALID_DEVICE_INFO;
|
||||||
|
}
|
||||||
|
// Allocate memory for the device info and specification, fill it and return the info
|
||||||
|
pdsp = malloc(sizeof(dev_spec_pn533));
|
||||||
|
*pdsp = dsp;
|
||||||
|
pdi = malloc(sizeof(dev_info));
|
||||||
|
strcpy(pdi->acName,"PN533USB");
|
||||||
|
pdi->ct = CT_PN533;
|
||||||
|
pdi->ds = (dev_spec)pdsp;
|
||||||
|
pdi->bActive = true;
|
||||||
|
pdi->bCrc = true;
|
||||||
|
pdi->bPar = true;
|
||||||
|
pdi->ui8TxBits = 0;
|
||||||
|
return pdi;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pdi;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dev_pn533_disconnect(dev_info* pdi)
|
||||||
|
{
|
||||||
|
dev_spec_pn533* pdsp = (dev_spec_pn533*)pdi->ds;
|
||||||
|
usb_release_interface(pdsp->pudh,0);
|
||||||
|
usb_close(pdsp->pudh);
|
||||||
|
free(pdi->ds);
|
||||||
|
free(pdi);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen)
|
||||||
|
{
|
||||||
|
ui32 uiPos = 0;
|
||||||
|
int ret = 0;
|
||||||
|
char buf[BUFFER_LENGTH];
|
||||||
|
dev_spec_pn533* pdsp = (dev_spec_pn533*)ds;
|
||||||
|
|
||||||
|
// Packet length = data length (len) + checksum (1) + end of stream marker (1)
|
||||||
|
buffer[3] = uiTxLen;
|
||||||
|
// Packet length checksum
|
||||||
|
buffer[4] = BUFFER_LENGTH - buffer[3];
|
||||||
|
// Copy the PN53X command into the packet buffer
|
||||||
|
memmove(buffer+5,pbtTx,uiTxLen);
|
||||||
|
|
||||||
|
// Calculate data payload checksum
|
||||||
|
buffer[uiTxLen+5] = 0;
|
||||||
|
for(uiPos=0; uiPos < uiTxLen; uiPos++)
|
||||||
|
{
|
||||||
|
buffer[uiTxLen+5] -= buffer[uiPos+5];
|
||||||
|
}
|
||||||
|
|
||||||
|
// End of stream marker
|
||||||
|
buffer[uiTxLen+6] = 0;
|
||||||
|
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Tx: ");
|
||||||
|
print_hex((byte*)buffer,uiTxLen+7);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, buffer, uiTxLen+7, USB_TIMEOUT);
|
||||||
|
if( ret < 0 )
|
||||||
|
{
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("usb_bulk_write failed with error %d\n", ret);
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT);
|
||||||
|
if( ret < 0 )
|
||||||
|
{
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf( "usb_bulk_read failed with error %d\n", ret);
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Rx: ");
|
||||||
|
print_hex((byte*)buf,ret);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if( ret == 6 )
|
||||||
|
{
|
||||||
|
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT);
|
||||||
|
if( ret < 0 )
|
||||||
|
{
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("usb_bulk_read failed with error %d\n", ret);
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _LIBNFC_VERBOSE_
|
||||||
|
printf("Rx: ");
|
||||||
|
print_hex((byte*)buf,ret);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the answer should be ignored, just return a succesful result
|
||||||
|
if(pbtRx == null || puiRxLen == null) return true;
|
||||||
|
|
||||||
|
// Only succeed when the result is at least 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
|
||||||
|
if(ret < 9) return false;
|
||||||
|
|
||||||
|
// Remove the preceding and appending bytes 00 00 FF xx Fx .. .. .. xx 00 (x = variable)
|
||||||
|
*puiRxLen = ret - 7 - 2;
|
||||||
|
|
||||||
|
// Get register: nuke extra byte (awful hack)
|
||||||
|
if ((buf[5]==(char)0xd5) && (buf[6]==(char)0x07) && (*puiRxLen==2)) {
|
||||||
|
// printf("Got %02x %02x, keep %02x\n", buf[7], buf[8], buf[8]);
|
||||||
|
*puiRxLen = (*puiRxLen) - 1;
|
||||||
|
memcpy( pbtRx, buf + 8, *puiRxLen);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy( pbtRx, buf + 7, *puiRxLen);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
35
src/dev_pn533.h
Normal file
35
src/dev_pn533.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Public platform independent Near Field Communication (NFC) library
|
||||||
|
Copyright (C) 2009, Roel Verdult
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBNFC_DEV_PN533_H_
|
||||||
|
#define _LIBNFC_DEV_PN533_H_
|
||||||
|
|
||||||
|
#include "defines.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
// Functions used by developer to handle connection to this device
|
||||||
|
dev_info* dev_pn533_connect(const ui32 uiIndex);
|
||||||
|
void dev_pn533_disconnect(dev_info* pdi);
|
||||||
|
|
||||||
|
// Callback function used by libnfc to transmit commands to the PN53X chip
|
||||||
|
bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen);
|
||||||
|
|
||||||
|
#endif // _LIBNFC_DEV_PN533_H_
|
||||||
|
|
|
@ -25,12 +25,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "dev_acr122.h"
|
#include "dev_acr122.h"
|
||||||
#include "dev_pn531.h"
|
#include "dev_pn531.h"
|
||||||
|
#include "dev_pn533.h"
|
||||||
#include "dev_arygon.h"
|
#include "dev_arygon.h"
|
||||||
|
|
||||||
const static struct dev_callbacks dev_callbacks_list[] = {
|
const static struct dev_callbacks dev_callbacks_list[] = {
|
||||||
// Driver Name Connect Transceive Disconect
|
// Driver Name Connect Transceive Disconect
|
||||||
{ "ACR122", dev_acr122_connect, dev_acr122_transceive, dev_acr122_disconnect },
|
{ "ACR122", dev_acr122_connect, dev_acr122_transceive, dev_acr122_disconnect },
|
||||||
{ "PN531USB", dev_pn531_connect, dev_pn531_transceive, dev_pn531_disconnect },
|
{ "PN531USB", dev_pn531_connect, dev_pn531_transceive, dev_pn531_disconnect },
|
||||||
|
{ "PN533USB", dev_pn533_connect, dev_pn533_transceive, dev_pn533_disconnect },
|
||||||
{ "ARYGON", dev_arygon_connect, dev_arygon_transceive, dev_arygon_disconnect }
|
{ "ARYGON", dev_arygon_connect, dev_arygon_transceive, dev_arygon_disconnect }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
304
src/rs232.c
Normal file
304
src/rs232.c
Normal file
|
@ -0,0 +1,304 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Public platform independent Near Field Communication (NFC) library
|
||||||
|
Copyright (C) 2009, Roel Verdult
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Based on rs232-code written by Teunis van Beelen
|
||||||
|
available: http://www.teuniz.net/RS-232/index.html
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "rs232.h"
|
||||||
|
|
||||||
|
#ifndef _WIN32 /* Linux */
|
||||||
|
|
||||||
|
typedef struct termios term_info;
|
||||||
|
typedef struct {
|
||||||
|
int fd; // Serial port file descriptor
|
||||||
|
term_info tiOld; // Terminal info before using the port
|
||||||
|
term_info tiNew; // Terminal info during the transaction
|
||||||
|
} serial_port_unix;
|
||||||
|
|
||||||
|
// Set time-out on 30 miliseconds
|
||||||
|
struct timeval tv = {
|
||||||
|
.tv_sec = 0, // No seconds
|
||||||
|
.tv_usec = 30000 // 30,000 micro seconds
|
||||||
|
};
|
||||||
|
|
||||||
|
// Work-around to claim rs232 interface using the c_iflag (software input processing) from the termios struct
|
||||||
|
#define CCLAIMED 0x80000000
|
||||||
|
|
||||||
|
serial_port rs232_open(const char* pcPortName)
|
||||||
|
{
|
||||||
|
serial_port_unix* sp = malloc(sizeof(serial_port_unix));
|
||||||
|
|
||||||
|
if (sp == 0) return INVALID_SERIAL_PORT;
|
||||||
|
|
||||||
|
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
|
||||||
|
if(sp->fd == -1)
|
||||||
|
{
|
||||||
|
rs232_close(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(tcgetattr(sp->fd,&sp->tiOld) == -1)
|
||||||
|
{
|
||||||
|
rs232_close(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the port is not claimed already
|
||||||
|
if (sp->tiOld.c_iflag & CCLAIMED)
|
||||||
|
{
|
||||||
|
rs232_close(sp);
|
||||||
|
return CLAIMED_SERIAL_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the old terminal info struct
|
||||||
|
sp->tiNew = sp->tiOld;
|
||||||
|
sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;
|
||||||
|
sp->tiNew.c_iflag = CCLAIMED | IGNPAR;
|
||||||
|
sp->tiNew.c_oflag = 0;
|
||||||
|
sp->tiNew.c_lflag = 0;
|
||||||
|
sp->tiNew.c_cc[VMIN] = 0; // block untill n bytes are received
|
||||||
|
sp->tiNew.c_cc[VTIME] = 0; // block untill a timer expires (n * 100 mSec.)
|
||||||
|
if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1)
|
||||||
|
{
|
||||||
|
rs232_close(sp);
|
||||||
|
return INVALID_SERIAL_PORT;
|
||||||
|
}
|
||||||
|
return sp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rs232_close(const serial_port sp)
|
||||||
|
{
|
||||||
|
tcsetattr(((serial_port_unix*)sp)->fd,TCSANOW,&((serial_port_unix*)sp)->tiOld);
|
||||||
|
close(((serial_port_unix*)sp)->fd);
|
||||||
|
free(sp);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rs232_cts(const serial_port sp)
|
||||||
|
{
|
||||||
|
ulong ulStatus;
|
||||||
|
if (ioctl(((serial_port_unix*)sp)->fd,TIOCMGET,&ulStatus) < 0) return false;
|
||||||
|
return (ulStatus & TIOCM_CTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rs232_receive(const serial_port sp, byte* pbtRx, ui32* puiRxLen)
|
||||||
|
{
|
||||||
|
int iResult;
|
||||||
|
ui32 uiCount = 0;
|
||||||
|
fd_set rfds;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
// Reset file descriptor
|
||||||
|
FD_ZERO(&rfds);
|
||||||
|
FD_SET(((serial_port_unix*)sp)->fd,&rfds);
|
||||||
|
iResult = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv);
|
||||||
|
|
||||||
|
// Read error
|
||||||
|
if (iResult < 0) return false;
|
||||||
|
|
||||||
|
// Read time-out
|
||||||
|
if (iResult == 0)
|
||||||
|
{
|
||||||
|
// Test if we at least have received something
|
||||||
|
if (uiCount == 0) return false;
|
||||||
|
// Store the received byte count and return succesful
|
||||||
|
*puiRxLen = uiCount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// There is something available, read the data
|
||||||
|
uiCount += read(((serial_port_unix*)sp)->fd,pbtRx+uiCount,*puiRxLen-uiCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rs232_send(const serial_port sp, const byte* pbtTx, const ui32 uiTxLen)
|
||||||
|
{
|
||||||
|
int iResult;
|
||||||
|
iResult = write(((serial_port_unix*)sp)->fd,pbtTx,uiTxLen);
|
||||||
|
return (iResult >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else /* windows */
|
||||||
|
|
||||||
|
|
||||||
|
HANDLE Cport[16];
|
||||||
|
|
||||||
|
|
||||||
|
char comports[16][10]={"\\\\.\\COM1", "\\\\.\\COM2", "\\\\.\\COM3", "\\\\.\\COM4",
|
||||||
|
"\\\\.\\COM5", "\\\\.\\COM6", "\\\\.\\COM7", "\\\\.\\COM8",
|
||||||
|
"\\\\.\\COM9", "\\\\.\\COM10", "\\\\.\\COM11", "\\\\.\\COM12",
|
||||||
|
"\\\\.\\COM13", "\\\\.\\COM14", "\\\\.\\COM15", "\\\\.\\COM16"};
|
||||||
|
|
||||||
|
char baudr[64];
|
||||||
|
|
||||||
|
|
||||||
|
int OpenComport(int comport_number, int baudrate)
|
||||||
|
{
|
||||||
|
if((comport_number>15)||(comport_number<0))
|
||||||
|
{
|
||||||
|
printf("illegal comport number\n");
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(baudrate)
|
||||||
|
{
|
||||||
|
sscanf(baudr,"baud=%d data=8 parity=N stop=1",baudrate);
|
||||||
|
/*
|
||||||
|
case 110 : strcpy(baudr, "baud=110 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 300 : strcpy(baudr, "baud=300 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 600 : strcpy(baudr, "baud=600 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 1200 : strcpy(baudr, "baud=1200 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 2400 : strcpy(baudr, "baud=2400 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 4800 : strcpy(baudr, "baud=4800 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 9600 : strcpy(baudr, "baud=9600 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 19200 : strcpy(baudr, "baud=19200 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 38400 : strcpy(baudr, "baud=38400 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 57600 : strcpy(baudr, "baud=57600 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 115200 : strcpy(baudr, "baud=115200 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 128000 : strcpy(baudr, "baud=128000 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
case 256000 : strcpy(baudr, "baud=256000 data=8 parity=N stop=1");
|
||||||
|
break;
|
||||||
|
default : printf("invalid baudrate\n");
|
||||||
|
return(1);
|
||||||
|
break;
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
Cport[comport_number] = CreateFileA(comports[comport_number],
|
||||||
|
GENERIC_READ|GENERIC_WRITE,
|
||||||
|
0, /* no share */
|
||||||
|
NULL, /* no security */
|
||||||
|
OPEN_EXISTING,
|
||||||
|
0, /* no threads */
|
||||||
|
NULL); /* no templates */
|
||||||
|
|
||||||
|
if(Cport[comport_number]==INVALID_HANDLE_VALUE)
|
||||||
|
{
|
||||||
|
printf("unable to open comport\n");
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
DCB port_settings;
|
||||||
|
memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */
|
||||||
|
port_settings.DCBlength = sizeof(port_settings);
|
||||||
|
|
||||||
|
if(!BuildCommDCBA(baudr, &port_settings))
|
||||||
|
{
|
||||||
|
printf("unable to set comport dcb settings\n");
|
||||||
|
CloseHandle(Cport[comport_number]);
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!SetCommState(Cport[comport_number], &port_settings))
|
||||||
|
{
|
||||||
|
printf("unable to set comport cfg settings\n");
|
||||||
|
CloseHandle(Cport[comport_number]);
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
COMMTIMEOUTS Cptimeouts;
|
||||||
|
|
||||||
|
Cptimeouts.ReadIntervalTimeout = MAXDWORD;
|
||||||
|
Cptimeouts.ReadTotalTimeoutMultiplier = 0;
|
||||||
|
Cptimeouts.ReadTotalTimeoutConstant = 0;
|
||||||
|
Cptimeouts.WriteTotalTimeoutMultiplier = 0;
|
||||||
|
Cptimeouts.WriteTotalTimeoutConstant = 0;
|
||||||
|
|
||||||
|
if(!SetCommTimeouts(Cport[comport_number], &Cptimeouts))
|
||||||
|
{
|
||||||
|
printf("unable to set comport time-out settings\n");
|
||||||
|
CloseHandle(Cport[comport_number]);
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int PollComport(int comport_number, unsigned char *buf, int size)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
if(size>4096) size = 4096;
|
||||||
|
|
||||||
|
/* added the void pointer cast, otherwise gcc will complain about */
|
||||||
|
/* "warning: dereferencing type-punned pointer will break strict aliasing rules" */
|
||||||
|
|
||||||
|
ReadFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL);
|
||||||
|
|
||||||
|
return(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int SendByte(int comport_number, unsigned char byte)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
WriteFile(Cport[comport_number], &byte, 1, (LPDWORD)((void *)&n), NULL);
|
||||||
|
|
||||||
|
if(n<0) return(1);
|
||||||
|
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int SendBuf(int comport_number, unsigned char *buf, int size)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
if(WriteFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL))
|
||||||
|
{
|
||||||
|
return(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rs232_cts(const serial_port sp);
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
|
||||||
|
GetCommModemStatus(Cport[comport_number], (LPDWORD)((void *)&status));
|
||||||
|
|
||||||
|
if(status&MS_CTS_ON) return(1);
|
||||||
|
else return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CloseComport(int comport_number)
|
||||||
|
{
|
||||||
|
CloseHandle(Cport[comport_number]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
56
src/rs232.h
Normal file
56
src/rs232.h
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Public platform independent Near Field Communication (NFC) library
|
||||||
|
Copyright (C) 2009, Roel Verdult
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBNFC_RS232_H_
|
||||||
|
#define _LIBNFC_RS232_H_
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "defines.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
// Handle platform specific includes
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <termios.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#else
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Define shortcut to types to make code more readable
|
||||||
|
typedef void* serial_port;
|
||||||
|
#define INVALID_SERIAL_PORT (void*)(~1)
|
||||||
|
#define CLAIMED_SERIAL_PORT (void*)(~2)
|
||||||
|
|
||||||
|
serial_port rs232_open(const char* pcPortName);
|
||||||
|
void rs232_close(const serial_port sp);
|
||||||
|
bool rs232_cts(const serial_port sp);
|
||||||
|
bool rs232_receive(const serial_port sp, byte* pbtRx, ui32* puiRxLen);
|
||||||
|
bool rs232_send(const serial_port sp, const byte* pbtTx, const ui32 uiTxLen);
|
||||||
|
|
||||||
|
#endif // _LIBNFC_RS232_H_
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue