- disable debug statements
[librfid] / src / rfid_proto_tcl.c
index 2be20d9..901e42c 100644 (file)
 #include <string.h>
 #include <errno.h>
 
-#include <rfid/rfid.h>
-#include <rfid/rfid_protocol_tcl.h>
-#include <rfid/rfid_protocol.h>
-#include <rfid/rfid_layer2.h>
-#include <rfid/rfid_layer2_iso14443b.h>
+#include <librfid/rfid.h>
+#include <librfid/rfid_protocol_tcl.h>
+#include <librfid/rfid_protocol.h>
+#include <librfid/rfid_layer2.h>
+#include <librfid/rfid_layer2_iso14443b.h>
 
-#include <rfid/rfid_asic.h>
-#include <rfid/rfid_reader.h>
+#include <librfid/rfid_asic.h>
+#include <librfid/rfid_reader.h>
 
 #include "rfid_iso14443_common.h"
 
-#if 1
-#ifdef DEBUGP
-#undef DEBUGP
-#define DEBUGP(x, ...)
-#endif
-#ifdef DEBUGPC
-#undef DEBUGPC
-#define DEBUGPC(x, ...)
-#endif
-#endif
-
 static enum rfid_frametype l2_to_frame(unsigned int layer2)
 {
        switch (layer2) {
@@ -234,27 +223,45 @@ static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
        return DI;
 }
 
+static unsigned int di_to_speed(unsigned char DI)
+{
+       switch (DI) {
+       case PPS_DIV_8:
+               return RFID_14443A_SPEED_848K;
+               break;
+       case PPS_DIV_4:
+               return RFID_14443A_SPEED_424K;
+               break;
+       case PPS_DIV_2:
+               return RFID_14443A_SPEED_212K;
+               break;
+       case PPS_DIV_1:
+               return RFID_14443A_SPEED_106K;
+               break;
+       }
+}
 
 /* start a PPS run (autimatically configure highest possible speed */
 static int 
 tcl_do_pps(struct rfid_protocol_handle *h)
 {
-#if 1
        int ret;
        unsigned char ppss[3];
        unsigned char pps_response[1];
        unsigned int rx_len = 1;
        unsigned char Dr, Ds, DrI, DsI;
-       unsigned int optlen = 0;
+       unsigned int speed;
 
        if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
                return -1;
 
        Dr = h->priv.tcl.ta & 0x07;
        Ds = h->priv.tcl.ta & 0x70 >> 4;
+       DEBUGP("Dr = 0x%x, Ds = 0x%x\n", Dr, Ds);
 
        if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
-               /* device supports different divisors for rx and tx, but not ?!? */
+               /* device supports different divisors for rx and tx, but not
+                * really ?!? */
                DEBUGP("PICC has contradictory TA, aborting PPS\n");
                return -1;
        };
@@ -263,10 +270,12 @@ tcl_do_pps(struct rfid_protocol_handle *h)
 
        ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
        ppss[1] = 0x11;
+       ppss[2] = 0x00;
 
        /* FIXME: deal with different speed for each direction */
        DrI = d_to_di(h, Dr);
        DsI = d_to_di(h, Ds);
+       DEBUGP("DrI = 0x%x, DsI = 0x%x\n", DrI, DsI);
 
        ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
 
@@ -281,13 +290,18 @@ tcl_do_pps(struct rfid_protocol_handle *h)
                return -1;
        }
 
-       ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED,
-                                NULL, &optlen);
+       speed = di_to_speed(DrI);
+
+       ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_RX,
+                                &speed, sizeof(speed));
+       if (ret < 0)
+               return ret;
+
+       ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_TX,
+                                &speed, sizeof(speed));
        if (ret < 0)
                return ret;
        
-       h->priv.tcl.state = TCL_STATE_ESTABLISHED;
-#endif
        return 0;
 }
 
@@ -438,6 +452,8 @@ tcl_connect(struct rfid_protocol_handle *h)
                break;
        }
 
+       h->priv.tcl.state = TCL_STATE_ESTABLISHED;
+
        return 0;
 }
 
@@ -636,13 +652,14 @@ do_tx:
                net_payload_len = _rx_len - (inf - rx_buf);
                DEBUGPC("%u bytes\n", net_payload_len);
                memcpy(_rx_data, inf, net_payload_len);
-               /* increment the number of payload bytes that we actually received */
+               /* increment the number of payload bytes that we actually
+                * received */
                *rx_len += net_payload_len;
                _rx_data += net_payload_len;
 
                if (*rx_buf & 0x10) {
                        /* we're not the last frame in the chain, continue rx */
-                       DEBUGP("we're not the last frame in the chain, continue\n");
+                       DEBUGP("not the last frame in the chain, continue\n");
                        ack_len = sizeof(ack);
                        tcl_build_prologue_r(&h->priv.tcl, ack, &ack_len, 0);
                        _tx = ack;