30 #endif // HAVE_CONFIG_H
32 #include "../drivers.h"
47 #include <sys/param.h>
52 #define DEV_ARYGON_PROTOCOL_ARYGON_ASCII '0'
56 #define DEV_ARYGON_PROTOCOL_ARYGON_BINARY_WAB '1'
60 #define DEV_ARYGON_PROTOCOL_TAMA '2'
64 #define DEV_ARYGON_PROTOCOL_TAMA_WAB '3'
66 #define SERIAL_DEFAULT_PORT_SPEED 9600
69 static const byte_t pn53x_ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
73 static const byte_t arygon_error_none[] =
"FF000000\x0d\x0a";
74 static const byte_t arygon_error_incomplete_command[] =
"FF0C0000\x0d\x0a";
75 static const byte_t arygon_error_unknown_mode[] =
"FF060000\x0d\x0a";
78 bool arygon_reset_tama (
const nfc_device_spec_t nds);
79 void arygon_firmware (
const nfc_device_spec_t nds,
char * str);
84 arygon_pick_device (
void)
88 if ((pndd = malloc (
sizeof (*pndd)))) {
92 DBG (
"%s",
"arygon_list_devices failed");
98 DBG (
"%s",
"No device found");
112 #ifndef SERIAL_AUTOPROBE_ENABLED
116 DBG (
"%s",
"Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
122 const char *pcPorts[] = DEFAULT_SERIAL_PORTS;
126 while ((pcPort = pcPorts[iDevice++])) {
127 sp = uart_open (pcPort);
128 DBG (
"Trying to find ARYGON device on serial port: %s at %d bauds.", pcPort, SERIAL_DEFAULT_PORT_SPEED);
130 if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) {
131 uart_set_speed (sp, SERIAL_DEFAULT_PORT_SPEED);
133 if (!arygon_reset_tama((nfc_device_spec_t) sp))
138 strncpy (pnddDevices[*pszDeviceFound].acDevice,
"ARYGON", DEVICE_NAME_LENGTH - 1);
139 pnddDevices[*pszDeviceFound].
acDevice[DEVICE_NAME_LENGTH - 1] =
'\0';
140 pnddDevices[*pszDeviceFound].
pcDriver = ARYGON_DRIVER_NAME;
141 pnddDevices[*pszDeviceFound].
pcPort = strdup (pcPort);
142 pnddDevices[*pszDeviceFound].
uiSpeed = SERIAL_DEFAULT_PORT_SPEED;
143 DBG (
"Device found: %s (%s)", pnddDevices[*pszDeviceFound].acDevice, pcPort);
147 if ((*pszDeviceFound) >= szDevices)
151 if (sp == INVALID_SERIAL_PORT)
152 DBG (
"Invalid serial port: %s", pcPort);
153 if (sp == CLAIMED_SERIAL_PORT)
154 DBG (
"Serial port already claimed: %s", pcPort);
168 DBG (
"Attempt to connect to: %s at %d bauds.", pndd->
pcPort, pndd->
uiSpeed);
169 sp = uart_open (pndd->
pcPort);
171 if (sp == INVALID_SERIAL_PORT)
172 ERR (
"Invalid serial port: %s", pndd->
pcPort);
173 if (sp == CLAIMED_SERIAL_PORT)
174 ERR (
"Serial port already claimed: %s", pndd->
pcPort);
175 if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT))
178 uart_set_speed (sp, pndd->
uiSpeed);
179 if (!arygon_reset_tama((nfc_device_spec_t) sp)) {
183 DBG (
"Successfully connected to: %s", pndd->
pcPort);
188 arygon_firmware((nfc_device_spec_t) sp, acFirmware);
189 snprintf (pnd->
acName, DEVICE_NAME_LENGTH - 1,
"%s %s (%s)", pndd->
acDevice, acFirmware, pndd->
pcPort);
190 pnd->
acName[DEVICE_NAME_LENGTH - 1] =
'\0';
192 pnd->
nds = (nfc_device_spec_t) sp;
201 uart_close ((serial_port) pnd->
nds);
205 #define TX_BUFFER_LENGTH (300)
206 #define RX_BUFFER_LENGTH (PN53x_EXTENDED_FRAME_MAX_LEN + PN53x_EXTENDED_FRAME_OVERHEAD + sizeof(pn53x_ack_frame))
208 arygon_transceive (
nfc_device_t * pnd,
const byte_t * pbtTx,
const size_t szTx, byte_t * pbtRx,
size_t * pszRx)
211 byte_t abtRxBuf[RX_BUFFER_LENGTH];
213 size_t szReplyMaxLen = MIN(RX_BUFFER_LENGTH, *pszRx);
220 abtTxBuf[5] = 256 - abtTxBuf[4];
222 memmove (abtTxBuf + 6, pbtTx, szTx);
225 abtTxBuf[szTx + 6] = 0;
226 for (szPos = 0; szPos < szTx; szPos++) {
227 abtTxBuf[szTx + 6] -= abtTxBuf[szPos + 6];
231 abtTxBuf[szTx + 7] = 0;
234 PRINT_HEX (
"TX", abtTxBuf, szTx + 8);
236 res =
uart_send ((serial_port) pnd->
nds, abtTxBuf, szTx + 8);
238 ERR (
"%s",
"Unable to transmit data. (TX)");
243 memset (abtRxBuf, 0x00,
sizeof (abtRxBuf));
245 szRxBufLen = szReplyMaxLen;
248 ERR (
"%s",
"Unable to receive data. (RX)");
253 PRINT_HEX (
"RX", abtRxBuf, szRxBufLen);
257 if (!pn53x_check_ack_frame_callback (pnd, abtRxBuf, szRxBufLen))
260 szRxBufLen -=
sizeof (pn53x_ack_frame);
261 memmove (abtRxBuf, abtRxBuf +
sizeof (pn53x_ack_frame), szRxBufLen);
262 szReplyMaxLen -=
sizeof (pn53x_ack_frame);
264 if (szRxBufLen == 0) {
267 szRxBufLen = szReplyMaxLen;
271 PRINT_HEX (
"RX", abtRxBuf, szRxBufLen);
275 if (!pn53x_check_error_frame_callback (pnd, abtRxBuf, szRxBufLen))
279 if (pbtRx == NULL || pszRx == NULL)
287 *pszRx = szRxBufLen - 9;
288 memcpy (pbtRx, abtRxBuf + 7, *pszRx);
294 arygon_firmware (
const nfc_device_spec_t nds,
char * str)
297 byte_t abtRx[RX_BUFFER_LENGTH];
302 PRINT_HEX (
"TX", arygon_firmware_version_cmd,
sizeof (arygon_firmware_version_cmd));
304 uart_send ((serial_port) nds, arygon_firmware_version_cmd,
sizeof (arygon_firmware_version_cmd));
308 DBG (
"Unable to retrieve ARYGON firmware version.");
312 PRINT_HEX (
"RX", abtRx, szRx);
314 if ( 0 == memcmp (abtRx, arygon_error_none, 6)) {
315 byte_t * p = abtRx + 6;
317 sscanf ((
const char*)p,
"%02x%s", &szData, p);
318 memcpy (str, p, szData);
319 *(str + szData) =
'\0';
324 arygon_reset_tama (
const nfc_device_spec_t nds)
327 byte_t abtRx[RX_BUFFER_LENGTH];
333 PRINT_HEX (
"TX", arygon_reset_tama_cmd,
sizeof (arygon_reset_tama_cmd));
335 uart_send ((serial_port) nds, arygon_reset_tama_cmd,
sizeof (arygon_reset_tama_cmd));
341 DBG (
"No reply to 'reset TAMA' command.");
345 PRINT_HEX (
"RX", abtRx, szRx);
347 if ( 0 == memcmp (abtRx, arygon_error_unknown_mode,
sizeof (arygon_error_unknown_mode) - 1)) {
350 PRINT_HEX (
"TX", arygon_reset_tama_cmd,
sizeof (arygon_reset_tama_cmd));
352 uart_send ((serial_port) nds, arygon_reset_tama_cmd,
sizeof (arygon_reset_tama_cmd));
358 PRINT_HEX (
"RX", abtRx, szRx);
361 if (0 != memcmp (abtRx, arygon_error_none,
sizeof (arygon_error_none) - 1)) {
393 byte_t abtRx[RX_BUFFER_LENGTH];
395 const byte_t attempted_result[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00,
396 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd5, 0x01, 0x00,
'l',
'i',
'b',
'n',
'f',
'c', 0xbc, 0x00 };
400 const byte_t pncmd_communication_test[] =
402 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00,
'l',
'i',
'b',
'n',
'f',
'c', 0xbe, 0x00 };
405 PRINT_HEX (
"TX", pncmd_communication_test,
sizeof (pncmd_communication_test));
407 res =
uart_send ((serial_port) nds, pncmd_communication_test,
sizeof (pncmd_communication_test));
409 ERR (
"%s",
"Unable to transmit data. (TX)");
415 ERR (
"%s",
"Unable to receive data. (RX)");
419 PRINT_HEX (
"RX", abtRx, szRx);
422 if (0 != memcmp (abtRx, attempted_result,
sizeof (attempted_result))) {
423 DBG (
"%s",
"Communication test failed, result doesn't match to attempted one.");