Fix a warning (Rainer Keller <mail@rainerkeller.de>)
[librfid] / src / rfid_layer2_iso14443a.c
index a83c71a..429577c 100644 (file)
@@ -1,6 +1,6 @@
 /* ISO 14443-3 A anticollision implementation
  *
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  *
  */
 
 #include <librfid/rfid_layer2.h>
 #include <librfid/rfid_reader.h>
 #include <librfid/rfid_layer2_iso14443a.h>
+#include <librfid/rfid_protocol.h>
 
 #define TIMEOUT 1236
 
 /* Transceive a 7-bit short frame */
-static int
+int
 iso14443a_transceive_sf(struct rfid_layer2_handle *handle,
                         unsigned char cmd,
                         struct iso14443a_atqa *atqa)
@@ -99,19 +100,22 @@ iso14443a_anticol(struct rfid_layer2_handle *handle)
        int ret;
        unsigned int uid_size;
        struct iso14443a_handle *h = &handle->priv.iso14443a;
-       struct iso14443a_atqa atqa;
+       struct iso14443a_atqa *atqa = &h->atqa;
        struct iso14443a_anticol_cmd acf;
        unsigned int bit_of_col;
        unsigned char sak[3];
        unsigned int rx_len = sizeof(sak);
-       char *aqptr = (char *) &atqa;
+       char *aqptr = (char *) atqa;
 
        memset(handle->uid, 0, sizeof(handle->uid));
        memset(sak, 0, sizeof(sak));
-       memset(&atqa, 0, sizeof(atqa));
+       memset(atqa, 0, sizeof(&atqa));
        memset(&acf, 0, sizeof(acf));
 
-       ret = iso14443a_transceive_sf(handle, ISO14443A_SF_CMD_REQA, &atqa);
+       if (handle->flags & RFID_OPT_LAYER2_WUP)
+               ret = iso14443a_transceive_sf(handle, ISO14443A_SF_CMD_WUPA, atqa);
+       else
+               ret = iso14443a_transceive_sf(handle, ISO14443A_SF_CMD_REQA, atqa);
        if (ret < 0) {
                h->state = ISO14443A_STATE_REQA_SENT;
                DEBUGP("error during transceive_sf: %d\n", ret);
@@ -121,15 +125,15 @@ iso14443a_anticol(struct rfid_layer2_handle *handle)
 
        DEBUGP("ATQA: 0x%02x 0x%02x\n", *aqptr, *(aqptr+1));
 
-       if (!atqa.bf_anticol) {
+       if (!atqa->bf_anticol) {
                h->state = ISO14443A_STATE_NO_BITFRAME_ANTICOL;
                DEBUGP("no bitframe anticollission bits set, aborting\n");
                return -1;
        }
 
-       if (atqa.uid_size == 2 || atqa.uid_size == 3)
+       if (atqa->uid_size == 2 || atqa->uid_size == 3)
                uid_size = 3;
-       else if (atqa.uid_size == 1)
+       else if (atqa->uid_size == 1)
                uid_size = 2;
        else
                uid_size = 1;
@@ -225,9 +229,12 @@ cascade:
 
        if (sak[0] & 0x20) {
                DEBUGP("we have a T=CL compliant PICC\n");
+               handle->proto_supported = 1 << RFID_PROTOCOL_TCL;
                h->tcl_capable = 1;
        } else {
                DEBUGP("we have a T!=CL PICC\n");
+               handle->proto_supported = (1 << RFID_PROTOCOL_MIFARE_UL)|
+                                         (1 << RFID_PROTOCOL_MIFARE_CLASSIC);
                h->tcl_capable = 0;
        }
 
@@ -278,15 +285,35 @@ iso14443a_setopt(struct rfid_layer2_handle *handle, int optname,
        return ret;
 }
 
+static int
+iso14443a_getopt(struct rfid_layer2_handle *handle, int optname,
+                void *optval, unsigned int optlen)
+{
+       int ret = -EINVAL;
+       struct iso14443a_handle *h = &handle->priv.iso14443a;
+       struct iso14443a_atqa *atqa = optval;
+
+       switch (optname) {
+       case RFID_OPT_14443A_ATQA:
+               *atqa = h->atqa;
+               ret = 0;
+               break;
+       };
+
+       return ret;
+}
+
 
 static struct rfid_layer2_handle *
 iso14443a_init(struct rfid_reader_handle *rh)
 {
        int ret;
-       struct rfid_layer2_handle *h = malloc(sizeof(*h));
+       struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
        if (!h)
                return NULL;
 
+       memset(h, 0, sizeof(*h));
+
        h->l2 = &rfid_layer2_iso14443a;
        h->rh = rh;
        h->priv.iso14443a.state = ISO14443A_STATE_NONE;
@@ -294,7 +321,7 @@ iso14443a_init(struct rfid_reader_handle *rh)
 
        ret = h->rh->reader->iso14443a.init(h->rh);
        if (ret < 0) {
-               free(h);
+               free_layer2_handle(h);
                return NULL;
        }
 
@@ -304,12 +331,12 @@ iso14443a_init(struct rfid_reader_handle *rh)
 static int
 iso14443a_fini(struct rfid_layer2_handle *handle)
 {
-       free(handle);
+       free_layer2_handle(handle);
        return 0;
 }
 
 
-struct rfid_layer2 rfid_layer2_iso14443a = {
+const struct rfid_layer2 rfid_layer2_iso14443a = {
        .id     = RFID_LAYER2_ISO14443A,
        .name   = "ISO 14443-3 A",
        .fn     = {
@@ -319,6 +346,7 @@ struct rfid_layer2 rfid_layer2_iso14443a = {
                .close          = &iso14443a_hlta,
                .fini           = &iso14443a_fini,
                .setopt         = &iso14443a_setopt,
+               .getopt         = &iso14443a_getopt,
        },
 };