Fix rs232.* under linux.

Fix merged code to be C99 compiliant.
Try to fix dev_arygon under linux (could not be tested here, sorry).
Try to fix MacOSX makefile (could not be tested here, sorry).
Adjust src/Makefile.am to compile/dist all new files.
This commit is contained in:
Romuald Conty 2009-06-11 10:16:27 +00:00
parent d74f0d13dd
commit eccb9d3d3b
8 changed files with 56 additions and 55 deletions

View file

@ -24,7 +24,7 @@ LD = gcc
CFLAGS = -fPIC -Wall -O4 $(LIBPCSC_HEADERS) $(LIBUSB_HEADERS) CFLAGS = -fPIC -Wall -O4 $(LIBPCSC_HEADERS) $(LIBUSB_HEADERS)
LDFLAGS = -fPIC -Wall -O4 LDFLAGS = -fPIC -Wall -O4
OBJS = dev_pn531.o dev_acr122.o bitutils.o libnfc.o OBJS = dev_pn531.o dev_pn533.o dev_acr122.o dev_arygon.o bitutils.o libnfc.o rs232.o
HEADERS = devices.h bitutils.h defines.h libnfc.h HEADERS = devices.h bitutils.h defines.h libnfc.h
LIBNFC = libnfc.$(LIBNFC_TYPE) LIBNFC = libnfc.$(LIBNFC_TYPE)
EXES = anticol emulate list mftool relay EXES = anticol emulate list mftool relay

View file

@ -5,18 +5,18 @@ bin_PROGRAMS = nfc-anticol nfc-list nfc-mftool nfc-relay nfc-emulate
# set the include path found by configure # set the include path found by configure
INCLUDES= $(all_includes) INCLUDES= $(all_includes)
nfcinclude_HEADERS = libnfc.h bitutils.h defines.h types.h mifaretag.h devices.h nfcinclude_HEADERS = libnfc.h bitutils.h defines.h types.h mifaretag.h devices.h rs232.h dev_arygon.h
nfcincludedir = $(includedir)/libnfc nfcincludedir = $(includedir)/libnfc
lib_LTLIBRARIES = libnfc.la lib_LTLIBRARIES = libnfc.la
libnfc_la_SOURCES = bitutils.c libnfc.c libnfc_la_SOURCES = bitutils.c libnfc.c rs232.c dev_arygon.c
libnfc_la_CFLAGS = libnfc_la_CFLAGS =
libnfc_la_LIBADD = libnfc_la_LIBADD =
if PCSC_LITE_ENABLED if PCSC_LITE_ENABLED
nfcinclude_HEADERS += dev_acr122.h dev_pn531.h nfcinclude_HEADERS += dev_acr122.h dev_pn531.h dev_pn533.h
libnfc_la_CFLAGS += @LIBUSB_CFLAGS@ @LIBPCSCLITE_CFLAGS@ libnfc_la_CFLAGS += @LIBUSB_CFLAGS@ @LIBPCSCLITE_CFLAGS@
libnfc_la_SOURCES += dev_pn531.c dev_acr122.c libnfc_la_SOURCES += dev_acr122.c dev_pn531.c dev_pn533.c
libnfc_la_LIBADD += @LIBUSB_LIBS@ @LIBPCSCLITE_LIBS@ libnfc_la_LIBADD += @LIBUSB_LIBS@ @LIBPCSCLITE_LIBS@
endif endif

View file

@ -24,26 +24,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifdef _WIN32 #ifdef _WIN32
#define SERIAL_STRING "COM" #define SERIAL_STRING "COM"
#endif #else
#ifdef _LINUX #ifdef __APPLE__
#define SERIAL_STRING "/dev/ttyusb"
#endif
#ifdef __APPLE__
#define SERIAL_STRING "/dev/tty.SLAB_USBtoUART" #define SERIAL_STRING "/dev/tty.SLAB_USBtoUART"
#else
#define SERIAL_STRING "/dev/ttyUSB"
#endif
#endif #endif
#define BUFFER_LENGTH 256 #define BUFFER_LENGTH 256
#define USB_TIMEOUT 30000 #define USB_TIMEOUT 30000
static byte abtTxBuf[BUFFER_LENGTH] = { 0x32, 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" 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) dev_info* dev_arygon_connect(const uint32_t uiIndex)
{ {
ui32 uiDevNr; uint32_t uiDevNr;
serial_port sp; serial_port sp;
char acConnect[BUFFER_LENGTH]; char acConnect[BUFFER_LENGTH];
dev_info* pdi = INVALID_DEVICE_INFO; dev_info* pdi = INVALID_DEVICE_INFO;
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Trying to find ARYGON device on serial port: %s#\n",SERIAL_STRING); printf("Trying to find ARYGON device on serial port: %s#\n",SERIAL_STRING);
#endif #endif
@ -58,7 +58,7 @@ dev_info* dev_arygon_connect(const ui32 uiIndex)
sp = rs232_open(acConnect); sp = rs232_open(acConnect);
#endif #endif
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) break; if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) break;
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
if (sp == INVALID_SERIAL_PORT) printf("invalid serial port: %s\n",acConnect); 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); if (sp == CLAIMED_SERIAL_PORT) printf("serial port already claimed: %s\n",acConnect);
#endif #endif
@ -66,7 +66,7 @@ dev_info* dev_arygon_connect(const ui32 uiIndex)
// Test if we have found a device // Test if we have found a device
if (uiDevNr == MAX_DEVICES) return INVALID_DEVICE_INFO; if (uiDevNr == MAX_DEVICES) return INVALID_DEVICE_INFO;
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Succesfully connected to: %s\n",acConnect); printf("Succesfully connected to: %s\n",acConnect);
#endif #endif
@ -88,11 +88,11 @@ void dev_arygon_disconnect(dev_info* pdi)
free(pdi); free(pdi);
} }
bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen) bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen)
{ {
byte abtRxBuf[BUFFER_LENGTH]; byte abtRxBuf[BUFFER_LENGTH];
ui32 uiRxBufLen = BUFFER_LENGTH; uint32_t uiRxBufLen = BUFFER_LENGTH;
ui32 uiPos; uint32_t uiPos;
// Packet length = data length (len) + checksum (1) + end of stream marker (1) // Packet length = data length (len) + checksum (1) + end of stream marker (1)
abtTxBuf[4] = uiTxLen; abtTxBuf[4] = uiTxLen;
@ -111,7 +111,7 @@ bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTx
// End of stream marker // End of stream marker
abtTxBuf[uiTxLen+7] = 0; abtTxBuf[uiTxLen+7] = 0;
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Tx: "); printf("Tx: ");
print_hex(abtTxBuf,uiTxLen+8); print_hex(abtTxBuf,uiTxLen+8);
#endif #endif
@ -120,13 +120,13 @@ bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTx
if (!rs232_receive((serial_port)ds,abtRxBuf,&uiRxBufLen)) return false; if (!rs232_receive((serial_port)ds,abtRxBuf,&uiRxBufLen)) return false;
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Rx: "); printf("Rx: ");
print_hex(abtRxBuf,uiRxBufLen); print_hex(abtRxBuf,uiRxBufLen);
#endif #endif
// When the answer should be ignored, just return a succesful result // When the answer should be ignored, just return a succesful result
if(pbtRx == null || puiRxLen == null) return true; 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) // 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; if(uiRxBufLen < 15) return false;

View file

@ -25,11 +25,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "types.h" #include "types.h"
// Functions used by developer to handle connection to this device // Functions used by developer to handle connection to this device
dev_info* dev_arygon_connect(const ui32 uiIndex); dev_info* dev_arygon_connect(const uint32_t uiIndex);
void dev_arygon_disconnect(dev_info* pdi); void dev_arygon_disconnect(dev_info* pdi);
// Callback function used by libnfc to transmit commands to the PN53X chip // 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); bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen);
#endif // _LIBNFC_DEV_ARYGON_H_ #endif // _LIBNFC_DEV_ARYGON_H_

View file

@ -34,15 +34,15 @@ static char buffer[BUFFER_LENGTH] = { 0x00, 0x00, 0xff }; // Every packet must s
typedef struct { typedef struct {
usb_dev_handle* pudh; usb_dev_handle* pudh;
ui32 uiEndPointIn; uint32_t uiEndPointIn;
ui32 uiEndPointOut; uint32_t uiEndPointOut;
} dev_spec_pn533; } dev_spec_pn533;
// Find transfer endpoints for bulk transfers // Find transfer endpoints for bulk transfers
static void get_end_points(struct usb_device *dev, dev_spec_pn533* pdsp) static void get_end_points(struct usb_device *dev, dev_spec_pn533* pdsp)
{ {
ui32 uiIndex; uint32_t uiIndex;
ui32 uiEndPoint; uint32_t uiEndPoint;
struct usb_interface_descriptor* puid = dev->config->interface->altsetting; struct usb_interface_descriptor* puid = dev->config->interface->altsetting;
// 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out // 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out
@ -57,7 +57,7 @@ static void get_end_points(struct usb_device *dev, dev_spec_pn533* pdsp)
// Test if we dealing with a bulk IN endpoint // Test if we dealing with a bulk IN endpoint
if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN)
{ {
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint); printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
#endif #endif
pdsp->uiEndPointIn = uiEndPoint; pdsp->uiEndPointIn = uiEndPoint;
@ -66,7 +66,7 @@ static void get_end_points(struct usb_device *dev, dev_spec_pn533* pdsp)
// Test if we dealing with a bulk OUT endpoint // Test if we dealing with a bulk OUT endpoint
if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT)
{ {
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint); printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
#endif #endif
pdsp->uiEndPointOut = uiEndPoint; pdsp->uiEndPointOut = uiEndPoint;
@ -74,7 +74,7 @@ static void get_end_points(struct usb_device *dev, dev_spec_pn533* pdsp)
} }
} }
dev_info* dev_pn533_connect(const ui32 uiIndex) dev_info* dev_pn533_connect(const uint32_t uiIndex)
{ {
int idvendor = 0x04e6; int idvendor = 0x04e6;
int idproduct = 0x5591; int idproduct = 0x5591;
@ -83,11 +83,11 @@ dev_info* dev_pn533_connect(const ui32 uiIndex)
dev_info* pdi = INVALID_DEVICE_INFO; dev_info* pdi = INVALID_DEVICE_INFO;
dev_spec_pn533* pdsp; dev_spec_pn533* pdsp;
dev_spec_pn533 dsp; dev_spec_pn533 dsp;
ui32 uiDevIndex; uint32_t uiDevIndex;
dsp.uiEndPointIn = 0; dsp.uiEndPointIn = 0;
dsp.uiEndPointOut = 0; dsp.uiEndPointOut = 0;
dsp.pudh = null; dsp.pudh = NULL;
usb_init(); usb_init();
if (usb_find_busses() < 0) return INVALID_DEVICE_INFO; if (usb_find_busses() < 0) return INVALID_DEVICE_INFO;
@ -112,7 +112,7 @@ dev_info* dev_pn533_connect(const ui32 uiIndex)
uiDevIndex--; uiDevIndex--;
continue; continue;
} }
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Found PN533 device\n"); printf("Found PN533 device\n");
#endif #endif
@ -122,7 +122,7 @@ dev_info* dev_pn533_connect(const ui32 uiIndex)
get_end_points(dev,&dsp); get_end_points(dev,&dsp);
if(usb_set_configuration(dsp.pudh,1) < 0) if(usb_set_configuration(dsp.pudh,1) < 0)
{ {
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Setting config failed\n"); printf("Setting config failed\n");
#endif #endif
usb_close(dsp.pudh); usb_close(dsp.pudh);
@ -131,7 +131,7 @@ dev_info* dev_pn533_connect(const ui32 uiIndex)
if(usb_claim_interface(dsp.pudh,0) < 0) if(usb_claim_interface(dsp.pudh,0) < 0)
{ {
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Can't claim interface\n"); printf("Can't claim interface\n");
#endif #endif
usb_close(dsp.pudh); usb_close(dsp.pudh);
@ -164,9 +164,9 @@ void dev_pn533_disconnect(dev_info* pdi)
free(pdi); free(pdi);
} }
bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxLen, byte* pbtRx, ui32* puiRxLen) bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen)
{ {
ui32 uiPos = 0; uint32_t uiPos = 0;
int ret = 0; int ret = 0;
char buf[BUFFER_LENGTH]; char buf[BUFFER_LENGTH];
dev_spec_pn533* pdsp = (dev_spec_pn533*)ds; dev_spec_pn533* pdsp = (dev_spec_pn533*)ds;
@ -188,7 +188,7 @@ bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxL
// End of stream marker // End of stream marker
buffer[uiTxLen+6] = 0; buffer[uiTxLen+6] = 0;
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Tx: "); printf("Tx: ");
print_hex((byte*)buffer,uiTxLen+7); print_hex((byte*)buffer,uiTxLen+7);
#endif #endif
@ -196,7 +196,7 @@ bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxL
ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, buffer, uiTxLen+7, USB_TIMEOUT); ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, buffer, uiTxLen+7, USB_TIMEOUT);
if( ret < 0 ) if( ret < 0 )
{ {
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("usb_bulk_write failed with error %d\n", ret); printf("usb_bulk_write failed with error %d\n", ret);
#endif #endif
return false; return false;
@ -205,13 +205,13 @@ bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxL
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT); ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 ) if( ret < 0 )
{ {
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf( "usb_bulk_read failed with error %d\n", ret); printf( "usb_bulk_read failed with error %d\n", ret);
#endif #endif
return false; return false;
} }
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Rx: "); printf("Rx: ");
print_hex((byte*)buf,ret); print_hex((byte*)buf,ret);
#endif #endif
@ -221,20 +221,20 @@ bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const ui32 uiTxL
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT); ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, buf, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 ) if( ret < 0 )
{ {
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("usb_bulk_read failed with error %d\n", ret); printf("usb_bulk_read failed with error %d\n", ret);
#endif #endif
return false; return false;
} }
#ifdef _LIBNFC_VERBOSE_ #ifdef DEBUG
printf("Rx: "); printf("Rx: ");
print_hex((byte*)buf,ret); print_hex((byte*)buf,ret);
#endif #endif
} }
// When the answer should be ignored, just return a succesful result // When the answer should be ignored, just return a succesful result
if(pbtRx == null || puiRxLen == null) return true; 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) // Only succeed when the result is at least 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
if(ret < 9) return false; if(ret < 9) return false;

View file

@ -25,11 +25,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "types.h" #include "types.h"
// Functions used by developer to handle connection to this device // Functions used by developer to handle connection to this device
dev_info* dev_pn533_connect(const ui32 uiIndex); dev_info* dev_pn533_connect(const uint32_t uiIndex);
void dev_pn533_disconnect(dev_info* pdi); void dev_pn533_disconnect(dev_info* pdi);
// Callback function used by libnfc to transmit commands to the PN53X chip // 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); bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen);
#endif // _LIBNFC_DEV_PN533_H_ #endif // _LIBNFC_DEV_PN533_H_

View file

@ -93,15 +93,15 @@ void rs232_close(const serial_port sp)
bool rs232_cts(const serial_port sp) bool rs232_cts(const serial_port sp)
{ {
ulong ulStatus; char status;
if (ioctl(((serial_port_unix*)sp)->fd,TIOCMGET,&ulStatus) < 0) return false; if (ioctl(((serial_port_unix*)sp)->fd,TIOCMGET,&status) < 0) return false;
return (ulStatus & TIOCM_CTS); return (status & TIOCM_CTS);
} }
bool rs232_receive(const serial_port sp, byte* pbtRx, ui32* puiRxLen) bool rs232_receive(const serial_port sp, byte* pbtRx, uint32_t* puiRxLen)
{ {
int iResult; int iResult;
ui32 uiCount = 0; uint32_t uiCount = 0;
fd_set rfds; fd_set rfds;
while (true) while (true)
@ -129,7 +129,7 @@ bool rs232_receive(const serial_port sp, byte* pbtRx, ui32* puiRxLen)
} }
} }
bool rs232_send(const serial_port sp, const byte* pbtTx, const ui32 uiTxLen) bool rs232_send(const serial_port sp, const byte* pbtTx, const uint32_t uiTxLen)
{ {
int iResult; int iResult;
iResult = write(((serial_port_unix*)sp)->fd,pbtTx,uiTxLen); iResult = write(((serial_port_unix*)sp)->fd,pbtTx,uiTxLen);

View file

@ -36,6 +36,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <limits.h> #include <limits.h>
#include <sys/time.h>
#else #else
#include <windows.h> #include <windows.h>
#endif #endif
@ -48,8 +49,8 @@ typedef void* serial_port;
serial_port rs232_open(const char* pcPortName); serial_port rs232_open(const char* pcPortName);
void rs232_close(const serial_port sp); void rs232_close(const serial_port sp);
bool rs232_cts(const serial_port sp); bool rs232_cts(const serial_port sp);
bool rs232_receive(const serial_port sp, byte* pbtRx, ui32* puiRxLen); bool rs232_receive(const serial_port sp, byte* pbtRx, uint32_t* puiRxLen);
bool rs232_send(const serial_port sp, const byte* pbtTx, const ui32 uiTxLen); bool rs232_send(const serial_port sp, const byte* pbtTx, const uint32_t uiTxLen);
#endif // _LIBNFC_RS232_H_ #endif // _LIBNFC_RS232_H_