2009-06-11 09:12:00 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
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
|
2009-06-26 09:05:25 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2009-06-11 09:12:00 +00:00
|
|
|
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.
|
|
|
|
|
2009-06-26 09:05:25 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
2009-06-11 09:12:00 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dev_arygon.h"
|
2009-09-08 10:25:59 +00:00
|
|
|
|
2009-06-11 09:12:00 +00:00
|
|
|
#include "rs232.h"
|
|
|
|
#include "bitutils.h"
|
2009-09-08 10:25:59 +00:00
|
|
|
#include "messages.h"
|
2009-06-11 09:12:00 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define SERIAL_STRING "COM"
|
2009-06-11 10:16:27 +00:00
|
|
|
#else
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define SERIAL_STRING "/dev/tty.SLAB_USBtoUART"
|
|
|
|
#else
|
2009-09-04 13:24:34 +00:00
|
|
|
// unistd.h is needed for usleep() fct.
|
|
|
|
#include <unistd.h>
|
2009-06-11 10:16:27 +00:00
|
|
|
#define SERIAL_STRING "/dev/ttyUSB"
|
|
|
|
#endif
|
2009-06-11 09:12:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BUFFER_LENGTH 256
|
|
|
|
#define USB_TIMEOUT 30000
|
2009-08-28 16:54:04 +00:00
|
|
|
|
|
|
|
/** @def DEV_ARYGON_PROTOCOL_ARYGON_ASCII
|
|
|
|
* @brief High level language in ASCII format. (Common µC commands and Mifare® commands)
|
|
|
|
*/
|
|
|
|
#define DEV_ARYGON_PROTOCOL_ARYGON_ASCII '0'
|
|
|
|
/** @def DEV_ARYGON_MODE_HL_ASCII
|
|
|
|
* @brief High level language in Binary format With AddressingByte for party line. (Common µC commands and Mifare® commands)
|
|
|
|
*/
|
|
|
|
#define DEV_ARYGON_PROTOCOL_ARYGON_BINARY_WAB '1'
|
|
|
|
/** @def DEV_ARYGON_PROTOCOL_TAMA
|
|
|
|
* @brief Philips protocol (TAMA language) in binary format.
|
|
|
|
*/
|
|
|
|
#define DEV_ARYGON_PROTOCOL_TAMA '2'
|
|
|
|
/** @def DEV_ARYGON_PROTOCOL_TAMA_WAB
|
|
|
|
* @brief Philips protocol (TAMA language) in binary With AddressingByte for party line.
|
|
|
|
*/
|
|
|
|
#define DEV_ARYGON_PROTOCOL_TAMA_WAB '3'
|
|
|
|
|
|
|
|
static byte_t abtTxBuf[BUFFER_LENGTH] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff"
|
2009-06-11 09:12:00 +00:00
|
|
|
|
2009-09-04 13:24:34 +00:00
|
|
|
dev_info* dev_arygon_connect(const nfc_device_desc_t* device_desc)
|
2009-06-11 09:12:00 +00:00
|
|
|
{
|
2009-06-11 10:16:27 +00:00
|
|
|
uint32_t uiDevNr;
|
2009-06-11 09:12:00 +00:00
|
|
|
serial_port sp;
|
|
|
|
char acConnect[BUFFER_LENGTH];
|
|
|
|
dev_info* pdi = INVALID_DEVICE_INFO;
|
2009-08-28 16:54:04 +00:00
|
|
|
|
2009-09-04 13:24:34 +00:00
|
|
|
if( device_desc == NULL ) {
|
|
|
|
#ifdef DISABLE_SERIAL_AUTOPROBE
|
|
|
|
INFO("Sorry, serial auto-probing have been disabled at compile time.");
|
|
|
|
return INVALID_DEVICE_INFO;
|
|
|
|
#else
|
|
|
|
DBG("Trying to find ARYGON device on serial port: %s#",SERIAL_STRING);
|
|
|
|
// 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__
|
2009-06-11 09:12:00 +00:00
|
|
|
strcpy(acConnect,SERIAL_STRING);
|
|
|
|
sp = rs232_open(acConnect);
|
2009-09-04 13:24:34 +00:00
|
|
|
#else
|
2009-06-11 09:12:00 +00:00
|
|
|
sprintf(acConnect,"%s%d",SERIAL_STRING,uiDevNr);
|
|
|
|
sp = rs232_open(acConnect);
|
2009-09-04 13:24:34 +00:00
|
|
|
#endif /* __APPLE__ */
|
|
|
|
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) break;
|
|
|
|
#ifdef DEBUG
|
2009-08-28 16:54:04 +00:00
|
|
|
if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s",acConnect);
|
|
|
|
if (sp == CLAIMED_SERIAL_PORT) DBG("Serial port already claimed: %s",acConnect);
|
2009-09-04 13:24:34 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Test if we have found a device
|
|
|
|
if (uiDevNr == MAX_DEVICES) return INVALID_DEVICE_INFO;
|
|
|
|
} else {
|
|
|
|
strcpy(acConnect,device_desc->port);
|
|
|
|
sp = rs232_open(acConnect);
|
|
|
|
if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",acConnect);
|
|
|
|
if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",acConnect);
|
|
|
|
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return INVALID_DEVICE_INFO;
|
2009-06-11 09:12:00 +00:00
|
|
|
}
|
2009-08-28 16:54:04 +00:00
|
|
|
|
|
|
|
DBG("Successfully connected to: %s",acConnect);
|
|
|
|
|
2009-06-11 09:12:00 +00:00
|
|
|
// 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;
|
2009-08-28 16:54:04 +00:00
|
|
|
}
|
2009-06-11 09:12:00 +00:00
|
|
|
|
|
|
|
void dev_arygon_disconnect(dev_info* pdi)
|
|
|
|
{
|
|
|
|
rs232_close((serial_port)pdi->ds);
|
|
|
|
free(pdi);
|
2009-08-28 16:54:04 +00:00
|
|
|
}
|
2009-06-11 09:12:00 +00:00
|
|
|
|
2009-07-16 12:09:06 +00:00
|
|
|
bool dev_arygon_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen)
|
2009-06-11 09:12:00 +00:00
|
|
|
{
|
2009-07-16 12:09:06 +00:00
|
|
|
byte_t abtRxBuf[BUFFER_LENGTH];
|
2009-06-11 10:16:27 +00:00
|
|
|
uint32_t uiRxBufLen = BUFFER_LENGTH;
|
|
|
|
uint32_t uiPos;
|
2009-08-28 16:54:04 +00:00
|
|
|
|
2009-06-11 09:12:00 +00:00
|
|
|
// Packet length = data length (len) + checksum (1) + end of stream marker (1)
|
2009-08-28 16:54:04 +00:00
|
|
|
abtTxBuf[4] = uiTxLen;
|
|
|
|
// Packet length checksum
|
|
|
|
abtTxBuf[5] = BUFFER_LENGTH - abtTxBuf[4];
|
2009-06-11 09:12:00 +00:00
|
|
|
// Copy the PN53X command into the packet buffer
|
|
|
|
memmove(abtTxBuf+6,pbtTx,uiTxLen);
|
2009-08-28 16:54:04 +00:00
|
|
|
|
2009-06-11 09:12:00 +00:00
|
|
|
// Calculate data payload checksum
|
2009-08-28 16:54:04 +00:00
|
|
|
abtTxBuf[uiTxLen+6] = 0;
|
2009-06-11 09:12:00 +00:00
|
|
|
for(uiPos=0; uiPos < uiTxLen; uiPos++)
|
|
|
|
{
|
|
|
|
abtTxBuf[uiTxLen+6] -= abtTxBuf[uiPos+6];
|
|
|
|
}
|
2009-08-28 16:54:04 +00:00
|
|
|
|
2009-06-11 09:12:00 +00:00
|
|
|
// End of stream marker
|
2009-08-28 16:54:04 +00:00
|
|
|
abtTxBuf[uiTxLen+7] = 0;
|
|
|
|
|
2009-06-11 10:16:27 +00:00
|
|
|
#ifdef DEBUG
|
2009-08-28 16:54:04 +00:00
|
|
|
printf(" TX: ");
|
2009-06-11 09:12:00 +00:00
|
|
|
print_hex(abtTxBuf,uiTxLen+8);
|
|
|
|
#endif
|
2009-08-28 16:54:04 +00:00
|
|
|
if (!rs232_send((serial_port)ds,abtTxBuf,uiTxLen+8)) {
|
|
|
|
ERR("Unable to transmit data. (TX)");
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-11 09:12:00 +00:00
|
|
|
|
2009-09-09 13:43:16 +00:00
|
|
|
/** @todo Find why this delay are needed. Maybe in PN532 datasheet, but I (Romuald) haven't it... */
|
2009-09-03 12:26:54 +00:00
|
|
|
/** @note ARYGON-APDB need 20ms between sending and receiving frame. No information regarding this in ARYGON datasheet... */
|
2009-09-04 08:32:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
Sleep(20);
|
|
|
|
#else
|
|
|
|
usleep(20000);
|
|
|
|
#endif
|
2009-09-03 12:26:54 +00:00
|
|
|
|
2009-09-09 13:43:16 +00:00
|
|
|
/** @note ARYGON-APDB need 30ms more to be stable (report correctly present tag, at each try: 20ms seems to be enought for one shot...) */
|
2009-09-04 08:32:32 +00:00
|
|
|
#ifdef _WIN32
|
2009-09-09 13:43:16 +00:00
|
|
|
Sleep(30);
|
2009-09-04 08:32:32 +00:00
|
|
|
#else
|
2009-09-09 13:43:16 +00:00
|
|
|
usleep(30000);
|
2009-09-04 08:32:32 +00:00
|
|
|
#endif
|
2009-09-03 12:26:54 +00:00
|
|
|
|
2009-08-28 16:54:04 +00:00
|
|
|
if (!rs232_receive((serial_port)ds,abtRxBuf,&uiRxBufLen)) {
|
|
|
|
ERR("Unable to receive data. (RX)");
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-11 09:12:00 +00:00
|
|
|
|
2009-06-11 10:16:27 +00:00
|
|
|
#ifdef DEBUG
|
2009-08-28 16:54:04 +00:00
|
|
|
printf(" RX: ");
|
2009-06-11 09:12:00 +00:00
|
|
|
print_hex(abtRxBuf,uiRxBufLen);
|
|
|
|
#endif
|
2009-08-28 16:54:04 +00:00
|
|
|
|
|
|
|
// When the answer should be ignored, just return a successful result
|
2009-06-11 10:16:27 +00:00
|
|
|
if(pbtRx == NULL || puiRxLen == NULL) return true;
|
2009-06-11 09:12:00 +00:00
|
|
|
|
|
|
|
// 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;
|
2009-08-28 16:54:04 +00:00
|
|
|
|
2009-06-11 09:12:00 +00:00
|
|
|
// 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);
|
2009-08-28 16:54:04 +00:00
|
|
|
|
2009-06-11 09:12:00 +00:00
|
|
|
return true;
|
|
|
|
}
|