Prepare RFID compilation in firmware mode
authorlaforge <laforge@e0336214-984f-0b4b-a45f-81c69e1f0ede>
Sat, 14 Oct 2006 12:34:38 +0000 (12:34 +0000)
committerlaforge <laforge@e0336214-984f-0b4b-a45f-81c69e1f0ede>
Sat, 14 Oct 2006 12:34:38 +0000 (12:34 +0000)
- switch from linked lists to static arrays
- remove all non-handle dynamic allocations (at the expense of stack)
- declare all proto/reader/asic/layer2 structures as const
- wrap all handle allocations in macros that expand to references to
  static structures in case of firmware mode
- update copyright notices
- add skeleton code for openpcd-inside-firmware driver
- update TODO with remaining TODO for firmware mode

git-svn-id: https://svn.gnumonks.org/trunk/librfid@1903 e0336214-984f-0b4b-a45f-81c69e1f0ede

25 files changed:
TODO
include/librfid/Makefile.am
include/librfid/rfid.h
include/librfid/rfid_layer2_iso14443a.h
include/librfid/rfid_layer2_iso14443b.h
include/librfid/rfid_layer2_iso15693.h
include/librfid/rfid_protocol_mifare_classic.h
include/librfid/rfid_protocol_mifare_ul.h
include/librfid/rfid_protocol_tcl.h
include/librfid/rfid_reader_cm5121.h
include/librfid/rfid_reader_openpcd.h
include/librfid/rfid_system.h [new file with mode: 0644]
src/rfid.c
src/rfid_asic_rc632.c
src/rfid_layer2.c
src/rfid_layer2_iso14443a.c
src/rfid_layer2_iso14443b.c
src/rfid_layer2_iso15693.c
src/rfid_proto_mifare_classic.c
src/rfid_proto_mifare_ul.c
src/rfid_proto_tcl.c
src/rfid_protocol.c
src/rfid_reader.c
src/rfid_reader_cm5121.c
src/rfid_reader_openpcd.c

diff --git a/TODO b/TODO
index 136a254..736cb6a 100644 (file)
--- a/TODO
+++ b/TODO
@@ -16,7 +16,6 @@ iso15693:
 - implement all the rest
 
 mifare_clasic:
 - implement all the rest
 
 mifare_clasic:
-- figure out why authentication doensn't work, even though it is exactly like in other drivers
 
 tcl:
 - implement pps for asymmetric (rx/tx) speeds
 
 tcl:
 - implement pps for asymmetric (rx/tx) speeds
@@ -28,3 +27,12 @@ openct:
 other:
 - implementation of code for various passive tags
 - documentation
 other:
 - implementation of code for various passive tags
 - documentation
+
+firmware compilation:
+- add configure '--firmware-mode'
+       - define LIBRFID_FIRMWARE
+       - don't link with libusb
+       - don't include CCID code
+       - specify include path for reader firmware system include files
+       - get rid of remaining malloc()s in 14443b and tcl
+       - fix 'mru' allocation size problem in tcl
index 4b1337d..1edb60b 100644 (file)
@@ -6,6 +6,7 @@ pkginclude_HEADERS = rfid.h rfid_scan.h rfid_asic.h rfid_asic_rc632.h \
                        rfid_protocol_mifare_ul.h \
                        rfid_protocol_mifare_classic.h \
                        rfid_reader.h \
                        rfid_protocol_mifare_ul.h \
                        rfid_protocol_mifare_classic.h \
                        rfid_reader.h \
+                       rfid_system.h \
                        rfid_reader_cm5121.h \
                        rfid_reader_openpcd.h
 
                        rfid_reader_cm5121.h \
                        rfid_reader_openpcd.h
 
index c3f8fc1..f0ecc16 100644 (file)
@@ -5,6 +5,8 @@
 
 #ifdef __LIBRFID__
 
 
 #ifdef __LIBRFID__
 
+#include <librfid/rfid_system.h>
+
 enum rfid_frametype {
        RFID_14443A_FRAME_REGULAR,
        RFID_14443B_FRAME_REGULAR,
 enum rfid_frametype {
        RFID_14443A_FRAME_REGULAR,
        RFID_14443B_FRAME_REGULAR,
index 87b6be0..283e6b6 100644 (file)
@@ -84,7 +84,7 @@ enum iso14443a_state {
 
 
 #include <librfid/rfid_layer2.h>
 
 
 #include <librfid/rfid_layer2.h>
-struct rfid_layer2 rfid_layer2_iso14443a;
+extern const struct rfid_layer2 rfid_layer2_iso14443a;
 
 #endif /* __LIBRFID__ */
 
 
 #endif /* __LIBRFID__ */
 
index 07c185f..7d2cb29 100644 (file)
@@ -26,7 +26,7 @@ struct iso14443b_atqb {
                              adc:2,
                              fwi:4;
        } protocol_info;
                              adc:2,
                              fwi:4;
        } protocol_info;
-};
+} __attribute__ ((packed));
 
 struct iso14443b_attrib_hdr {
        unsigned char one_d;
 
 struct iso14443b_attrib_hdr {
        unsigned char one_d;
@@ -51,7 +51,7 @@ struct iso14443b_attrib_hdr {
                unsigned char cid:4,
                              rfu:4;
        } param4;
                unsigned char cid:4,
                              rfu:4;
        } param4;
-};
+} __attribute__ ((packed));
 
 struct iso14443b_handle {
        unsigned int tcl_capable; /* do we support T=CL */
 
 struct iso14443b_handle {
        unsigned int tcl_capable; /* do we support T=CL */
@@ -89,7 +89,7 @@ enum {
 };
 
 #include <librfid/rfid_layer2.h>
 };
 
 #include <librfid/rfid_layer2.h>
-struct rfid_layer2 rfid_layer2_iso14443b;
+extern const struct rfid_layer2 rfid_layer2_iso14443b;
 
 #endif /* __LIBRFID__ */
 
 
 #endif /* __LIBRFID__ */
 
index d91b4ec..37d204d 100644 (file)
@@ -50,6 +50,6 @@ enum iso15693_state {
 };
 
 #include <librfid/rfid_layer2.h>
 };
 
 #include <librfid/rfid_layer2.h>
-extern struct rfid_layer2 rfid_layer2_iso15693;
+extern const struct rfid_layer2 rfid_layer2_iso15693;
 
 #endif /* _ISO15693_H */
 
 #endif /* _ISO15693_H */
index e6b2400..06235b8 100644 (file)
@@ -1,5 +1,7 @@
 #ifndef _MIFARE_CLASSIC_H
 
 #ifndef _MIFARE_CLASSIC_H
 
+#include <librfid/rfid_protocol.h>
+
 #define MIFARE_CL_KEYA_DEFAULT "\xa0\xa1\xa2\xa3\xa4\xa5"
 #define MIFARE_CL_KEYB_DEFAULT "\xb0\xb1\xb2\xb3\xb4\xb5"
 
 #define MIFARE_CL_KEYA_DEFAULT "\xa0\xa1\xa2\xa3\xa4\xa5"
 #define MIFARE_CL_KEYB_DEFAULT "\xb0\xb1\xb2\xb3\xb4\xb5"
 
@@ -13,7 +15,7 @@
 
 #ifdef __LIBRFID__
 
 
 #ifdef __LIBRFID__
 
-extern struct rfid_protocol rfid_protocol_mfcl;
+extern const struct rfid_protocol rfid_protocol_mfcl;
 
 
 #define MIFARE_CL_CMD_WRITE16  0xA0
 
 
 #define MIFARE_CL_CMD_WRITE16  0xA0
index cc93b6b..593894e 100644 (file)
@@ -17,7 +17,7 @@ int rfid_mful_lock_otp(struct rfid_protocol_handle *ph);
 #define MIFARE_UL_PAGE_LOCK    2
 #define MIFARE_UL_PAGE_OTP     3
 
 #define MIFARE_UL_PAGE_LOCK    2
 #define MIFARE_UL_PAGE_OTP     3
 
-extern struct rfid_protocol rfid_protocol_mful;
+extern const struct rfid_protocol rfid_protocol_mful;
 
 #endif /* __LIBRFID__ */
 
 
 #endif /* __LIBRFID__ */
 
index 180c008..2ee1c27 100644 (file)
@@ -36,7 +36,8 @@ struct tcl_handle {
        unsigned int toggle;    /* send toggle with next frame */
 
        unsigned int ats_len;
        unsigned int toggle;    /* send toggle with next frame */
 
        unsigned int ats_len;
-       unsigned char ats[0];
+       unsigned char ats[256]; /* ATS cannot be bigger than FSD-2 bytes,
+                                  according to ISO 14443-4 5.2.2 */
 };
 
 enum tcl_handle_flags {
 };
 
 enum tcl_handle_flags {
@@ -63,7 +64,7 @@ enum tcl_pcd_state {
        TCL_STATE_DESELECTED,           /* card deselected or HLTA'd */
 };
 
        TCL_STATE_DESELECTED,           /* card deselected or HLTA'd */
 };
 
-struct rfid_protocol rfid_protocol_tcl;
+extern const struct rfid_protocol rfid_protocol_tcl;
 
 #endif /* __LIBRFID__ */
 
 
 #endif /* __LIBRFID__ */
 
index 8d9a312..5a1b7c3 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef _RFID_READER_CM5121_H
 #define _RFID_READER_CM5121_H
 
 #ifndef _RFID_READER_CM5121_H
 #define _RFID_READER_CM5121_H
 
+#include <librfid/rfid_reader.h>
+
 #define CM5121_CW_CONDUCTANCE  0x3f
 #define CM5121_MOD_CONDUCTANCE 0x3f
 #define CM5121_14443A_BITPHASE 0xa9
 #define CM5121_CW_CONDUCTANCE  0x3f
 #define CM5121_MOD_CONDUCTANCE 0x3f
 #define CM5121_14443A_BITPHASE 0xa9
 #define CM5121_14443B_BITPHASE 0xad
 #define CM5121_14443B_THRESHOLD        0xff
 
 #define CM5121_14443B_BITPHASE 0xad
 #define CM5121_14443B_THRESHOLD        0xff
 
-
 extern int
 PC_to_RDR_Escape(void *handle,
                const unsigned char *tx_buf, unsigned int tx_len,
                unsigned char *rx_buf, unsigned int *rx_len);
 
 extern int
 PC_to_RDR_Escape(void *handle,
                const unsigned char *tx_buf, unsigned int tx_len,
                unsigned char *rx_buf, unsigned int *rx_len);
 
-extern struct rfid_reader rfid_reader_cm5121;
+extern const struct rfid_reader rfid_reader_cm5121;
 // extern struct rfid_asic_transport cm5121_ccid;
 
 #endif
 // extern struct rfid_asic_transport cm5121_ccid;
 
 #endif
index e345132..49ee31c 100644 (file)
@@ -92,6 +92,6 @@ enum openpcd_cmd_class {
 #define OPENPCD_IN_EP          0x82
 #define OPENPCD_IRQ_EP         0x83
 
 #define OPENPCD_IN_EP          0x82
 #define OPENPCD_IRQ_EP         0x83
 
-extern struct rfid_reader rfid_reader_openpcd;
+extern const struct rfid_reader rfid_reader_openpcd;
 
 #endif
 
 #endif
diff --git a/include/librfid/rfid_system.h b/include/librfid/rfid_system.h
new file mode 100644 (file)
index 0000000..56441bb
--- /dev/null
@@ -0,0 +1,53 @@
+/* system / environment specific defines */
+
+//#define LIBRFID_FIRMWARE
+
+#ifdef __LIBRFID__
+
+#ifndef LIBRFID_FIRMWARE
+/* If we're not doing a firmware compile, then we just use the regular
+ * malloc()/free() functions as expected */
+
+#define malloc_asic_handle(x)  malloc(x)
+#define free_asic_handle(x)    free(x)
+
+#define malloc_layer2_handle(x)        malloc(x)
+#define free_layer2_handle(x)  free(x)
+
+#define malloc_protocol_handle(x)      malloc(x)
+#define free_protocol_handle(x)        free(x)
+
+#define malloc_rat_handle(x)   malloc(x)
+#define free_rat_handle(x)     free(x)
+
+#define malloc_reader_handle(x)        malloc(x)
+#define free_reader_handle(x)  free(x)
+
+#else
+/* If we're actually doing a firmware compile, then we use pre-allocated
+ * handles in order to avoid dynamic memory allocation. */
+
+#define EMPTY_STATEMENT        do {} while(0)
+extern struct rfid_asic_handle rfid_ah;
+#define malloc_asic_handle(x)  &rfid_ah
+#define free_asic_handle(x)    EMPTY_STATEMENT
+
+extern struct rfid_layer2_handle rfid_l2h;
+#define malloc_layer2_handle(x)        &rfid_l2h
+#define free_layer2_handle(x)  EMPTY_STATEMENT
+
+extern struct rfid_protocol_handle rfid_ph;
+#define malloc_protocol_handle(x)      &rfid_ph
+#define free_protocol_handle(x)                EMPTY_STATEMENT
+
+extern struct rfid_asic_transport_handle rfid_ath;
+#define malloc_rat_handle(x)   &rfid_ath
+#define free_rat_handle(x)     EMPTY_STATEMENT
+
+extern struct rfid_reader_handle rfid_rh;
+#define malloc_reader_handle(x)        &rfid_rh
+#define free_reader_handle(x)  EMPTY_STATEMENT
+
+#endif /* LIBRFID_FIRMWARE */
+
+#endif /* __LIBRFID__ */
index ba166a2..ec0ce1f 100644 (file)
 #include <string.h>
 
 #include <librfid/rfid_reader.h>
 #include <string.h>
 
 #include <librfid/rfid_reader.h>
-#include <librfid/rfid_reader_cm5121.h>
-#include <librfid/rfid_reader_openpcd.h>
 #include <librfid/rfid_protocol.h>
 #include <librfid/rfid_protocol_tcl.h>
 #include <librfid/rfid_protocol_mifare_ul.h>
 #include <librfid/rfid_protocol_mifare_classic.h>
 
 #include <librfid/rfid_protocol.h>
 #include <librfid/rfid_protocol_tcl.h>
 #include <librfid/rfid_protocol_mifare_ul.h>
 #include <librfid/rfid_protocol_mifare_classic.h>
 
+#ifdef LIBRFID_FIRMWARE
+struct rfid_asic_handle rfid_ah;
+struct rfid_layer2_handle rfid_l2h;
+struct rfid_protocol_handle rfid_ph;
+struct rfid_asic_transport_handle rfid_ath;
+struct rfid_reader_handle rfid_rh;
+#endif
+
 const char *
 rfid_hexdump(const void *data, unsigned int len)
 {
 const char *
 rfid_hexdump(const void *data, unsigned int len)
 {
@@ -92,6 +98,7 @@ int rfid_getopt(struct rfid_handle *rh, unsigned int level,
 
 int rfid_init()
 {
 
 int rfid_init()
 {
+#if 0
        rfid_reader_register(&rfid_reader_cm5121);
        rfid_reader_register(&rfid_reader_openpcd);
        rfid_layer2_register(&rfid_layer2_iso14443a);
        rfid_reader_register(&rfid_reader_cm5121);
        rfid_reader_register(&rfid_reader_openpcd);
        rfid_layer2_register(&rfid_layer2_iso14443a);
@@ -99,6 +106,7 @@ int rfid_init()
        rfid_protocol_register(&rfid_protocol_tcl);
        rfid_protocol_register(&rfid_protocol_mful);
        rfid_protocol_register(&rfid_protocol_mfcl);
        rfid_protocol_register(&rfid_protocol_tcl);
        rfid_protocol_register(&rfid_protocol_mful);
        rfid_protocol_register(&rfid_protocol_mfcl);
+#endif
 
        return 0;
 }
 
        return 0;
 }
index 05ed2cd..f8a8483 100644 (file)
@@ -1,6 +1,6 @@
 /* Generic Philips CL RC632 Routines
  *
 /* Generic Philips CL RC632 Routines
  *
- * (C) Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
  *
  */
 
  *
  */
 
@@ -41,7 +41,7 @@
 #define RC632_TMO_AUTH1        14000
 
 #define ENTER()                DEBUGP("entering\n")
 #define RC632_TMO_AUTH1        14000
 
 #define ENTER()                DEBUGP("entering\n")
-struct rfid_asic rc632;
+const struct rfid_asic rc632;
 
 /* Register and FIFO Access functions */
 static int 
 
 /* Register and FIFO Access functions */
 static int 
@@ -596,7 +596,7 @@ rc632_open(struct rfid_asic_transport_handle *th)
 {
        struct rfid_asic_handle *h;
 
 {
        struct rfid_asic_handle *h;
 
-       h = malloc(sizeof(*h));
+       h = malloc_asic_handle(sizeof(*h));
        if (!h)
                return NULL;
        memset(h, 0, sizeof(*h));
        if (!h)
                return NULL;
        memset(h, 0, sizeof(*h));
@@ -609,7 +609,7 @@ rc632_open(struct rfid_asic_transport_handle *th)
        h->mtu = h->mru = 64;
 
        if (rc632_init(h) < 0) {
        h->mtu = h->mru = 64;
 
        if (rc632_init(h) < 0) {
-               free(h);
+               free_asic_handle(h);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -620,14 +620,14 @@ void
 rc632_close(struct rfid_asic_handle *h)
 {
        rc632_fini(h);
 rc632_close(struct rfid_asic_handle *h)
 {
        rc632_fini(h);
-       free(h);
+       free_asic_handle(h);
 }
 
 
 /* 
  * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
  *
 }
 
 
 /* 
  * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
  *
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  *
  */
 
  *
  */
 
@@ -1581,7 +1581,7 @@ rc632_mifare_transceive(struct rfid_asic_handle *handle,
        return 0; 
 }
 
        return 0; 
 }
 
-struct rfid_asic rc632 = {
+const struct rfid_asic rc632 = {
        .name   = "Philips CL RC632",
        .fc     = ISO14443_FREQ_CARRIER,
        .priv.rc632 = {
        .name   = "Philips CL RC632",
        .fc     = ISO14443_FREQ_CARRIER,
        .priv.rc632 = {
index a6ee43f..ff6d2b0 100644 (file)
 #include <librfid/rfid.h>
 #include <librfid/rfid_layer2.h>
 
 #include <librfid/rfid.h>
 #include <librfid/rfid_layer2.h>
 
-static struct rfid_layer2 *rfid_layer2_list;
+static const struct rfid_layer2 *rfid_layer2s[] = {
+       [RFID_LAYER2_ISO14443A] = &rfid_layer2_iso14443a,
+       [RFID_LAYER2_ISO14443B] = &rfid_layer2_iso14443b,
+       [RFID_LAYER2_ISO15693]  = &rfid_layer2_iso15693,
+};
 
 struct rfid_layer2_handle *
 rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id)
 {
        struct rfid_layer2 *p;
 
 
 struct rfid_layer2_handle *
 rfid_layer2_init(struct rfid_reader_handle *rh, unsigned int id)
 {
        struct rfid_layer2 *p;
 
-       for (p = rfid_layer2_list; p; p = p->next)
-               if (p->id == id)
-                       return p->fn.init(rh);
+       if (id >= ARRAY_SIZE(rfid_layer2s)) {
+               DEBUGP("unable to find matching layer2 protocol\n");
+               return NULL;
+       }
 
 
-       DEBUGP("unable to find matching layer2 protocol\n");
-       return NULL;
+       p = rfid_layer2s[id];
+       return p->fn.init(rh);
 }
 
 int
 }
 
 int
@@ -79,15 +84,6 @@ rfid_layer2_close(struct rfid_layer2_handle *ph)
        return ph->l2->fn.close(ph);
 }
 
        return ph->l2->fn.close(ph);
 }
 
-int
-rfid_layer2_register(struct rfid_layer2 *p)
-{
-       p->next = rfid_layer2_list;
-       rfid_layer2_list = p;
-
-       return 0;
-}
-
 int
 rfid_layer2_getopt(struct rfid_layer2_handle *ph, int optname,
                   void *optval, unsigned int *optlen)
 int
 rfid_layer2_getopt(struct rfid_layer2_handle *ph, int optname,
                   void *optval, unsigned int *optlen)
index 7ebc240..34bd93f 100644 (file)
@@ -287,7 +287,7 @@ static struct rfid_layer2_handle *
 iso14443a_init(struct rfid_reader_handle *rh)
 {
        int ret;
 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;
 
        if (!h)
                return NULL;
 
@@ -298,7 +298,7 @@ iso14443a_init(struct rfid_reader_handle *rh)
 
        ret = h->rh->reader->iso14443a.init(h->rh);
        if (ret < 0) {
 
        ret = h->rh->reader->iso14443a.init(h->rh);
        if (ret < 0) {
-               free(h);
+               free_layer2_handle(h);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -308,12 +308,12 @@ iso14443a_init(struct rfid_reader_handle *rh)
 static int
 iso14443a_fini(struct rfid_layer2_handle *handle)
 {
 static int
 iso14443a_fini(struct rfid_layer2_handle *handle)
 {
-       free(handle);
+       free_layer2_handle(handle);
        return 0;
 }
 
 
        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     = {
        .id     = RFID_LAYER2_ISO14443A,
        .name   = "ISO 14443-3 A",
        .fn     = {
index 8227a32..d88fee5 100644 (file)
@@ -162,29 +162,24 @@ static int
 transceive_attrib(struct rfid_layer2_handle *h, const unsigned char *inf,
            unsigned int inf_len, unsigned char *rx_data, unsigned int *rx_len)
 {
 transceive_attrib(struct rfid_layer2_handle *h, const unsigned char *inf,
            unsigned int inf_len, unsigned char *rx_data, unsigned int *rx_len)
 {
-       struct iso14443b_attrib_hdr *attrib;
-       unsigned int attrib_size = sizeof(*attrib) + inf_len;
-       unsigned char *rx_buf;
+       struct {
+               struct iso14443b_attrib_hdr attrib;
+               char buf[256-3];
+       } _attrib_buf;
+
+       struct iso14443b_attrib_hdr *attrib = &_attrib_buf.attrib;
+       unsigned char rx_buf[256];
        unsigned char fsdi;
        int ret = 0;
        
        DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
        unsigned char fsdi;
        int ret = 0;
        
        DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
-       attrib = malloc(attrib_size);
-       if (!attrib) {
-               perror("attrib_alloc");
-               return -1;
-       }
-
-       DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
-       rx_buf = malloc(*rx_len+1);
-       if (!rx_buf) {
-               perror("rx_buf malloc");
-               ret = -1;
+       if (rx_len >= rx_len-1) {
+               perror("rx_len too large\n");
                goto out_attrib;
        }
 
        /* initialize attrib frame */
                goto out_attrib;
        }
 
        /* initialize attrib frame */
-       memset(attrib, 0, attrib_size);
+       memset(&_attrib_buf, 0, sizeof(_attrib_buf));
        if (inf_len)
                memcpy((unsigned char *)attrib+sizeof(*attrib), inf, inf_len);
 
        if (inf_len)
                memcpy((unsigned char *)attrib+sizeof(*attrib), inf, inf_len);
 
@@ -237,7 +232,6 @@ transceive_attrib(struct rfid_layer2_handle *h, const unsigned char *inf,
 out_rx:
        free(rx_buf);
 out_attrib:
 out_rx:
        free(rx_buf);
 out_attrib:
-       free(attrib);
 
        return ret;
 }
 
        return ret;
 }
@@ -295,7 +289,7 @@ static struct rfid_layer2_handle *
 iso14443b_init(struct rfid_reader_handle *rh)
 {
        int ret;
 iso14443b_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;
 
        if (!h)
                return NULL;
 
@@ -317,7 +311,7 @@ iso14443b_init(struct rfid_reader_handle *rh)
        ret = h->rh->reader->iso14443b.init(h->rh);
        if (ret < 0) {
                DEBUGP("error during reader 14443b init\n");
        ret = h->rh->reader->iso14443b.init(h->rh);
        if (ret < 0) {
                DEBUGP("error during reader 14443b init\n");
-               free(h);
+               free_layer2_handle(h);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -327,7 +321,7 @@ iso14443b_init(struct rfid_reader_handle *rh)
 static int
 iso14443b_fini(struct rfid_layer2_handle *handle)
 {
 static int
 iso14443b_fini(struct rfid_layer2_handle *handle)
 {
-       free(handle);
+       free_layer2_handle(handle);
        return 0;
 }
 
        return 0;
 }
 
@@ -394,7 +388,7 @@ iso14443b_setopt(struct rfid_layer2_handle *handle,
 }
 
 
 }
 
 
-struct rfid_layer2 rfid_layer2_iso14443b = {
+const struct rfid_layer2 rfid_layer2_iso14443b = {
        .id     = RFID_LAYER2_ISO14443B,
        .name   = "ISO 14443-3 B",
        .fn     = {
        .id     = RFID_LAYER2_ISO14443B,
        .name   = "ISO 14443-3 B",
        .fn     = {
index 8c5d808..8ab0b1e 100644 (file)
@@ -1,6 +1,6 @@
 /* ISO 15693 anticollision implementation
  *
 /* ISO 15693 anticollision implementation
  *
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  *
  */
 
  *
  */
 
@@ -263,7 +263,7 @@ static struct rfid_layer2_handle *
 iso15693_init(struct rfid_reader_handle *rh)
 {
        int ret;
 iso15693_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;
 
        if (!h)
                return NULL;
 
@@ -273,7 +273,7 @@ iso15693_init(struct rfid_reader_handle *rh)
 
        ret = h->rh->reader->iso15693.init(h->rh);
        if (ret < 0) {
 
        ret = h->rh->reader->iso15693.init(h->rh);
        if (ret < 0) {
-               free(h);
+               free_layer2_handle(h);
                return NULL;
        }
 
                return NULL;
        }
 
@@ -283,12 +283,12 @@ iso15693_init(struct rfid_reader_handle *rh)
 static int
 iso15693_fini(struct rfid_layer2_handle *handle)
 {
 static int
 iso15693_fini(struct rfid_layer2_handle *handle)
 {
-       free(handle);
+       free_layer2_handle(handle);
        return 0;
 }
 
 
        return 0;
 }
 
 
-struct rfid_layer2 rfid_layer2_iso15693 = {
+const struct rfid_layer2 rfid_layer2_iso15693 = {
        .id     = RFID_LAYER2_ISO15693,
        .name   = "ISO 15693",
        .fn     = {
        .id     = RFID_LAYER2_ISO15693,
        .name   = "ISO 15693",
        .fn     = {
index 5d2b3ff..d45eefc 100644 (file)
@@ -1,7 +1,7 @@
 
 /* Mifare Classic implementation, PCD side.
  *
 
 /* Mifare Classic implementation, PCD side.
  *
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  *
  */
 
  *
  */
 
@@ -110,17 +110,17 @@ static struct rfid_protocol_handle *
 mfcl_init(struct rfid_layer2_handle *l2h)
 {
        struct rfid_protocol_handle *ph;
 mfcl_init(struct rfid_layer2_handle *l2h)
 {
        struct rfid_protocol_handle *ph;
-       ph = malloc(sizeof(struct rfid_protocol_handle));
+       ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
        return ph;
 }
 
 static int mfcl_fini(struct rfid_protocol_handle *ph)
 {
        return ph;
 }
 
 static int mfcl_fini(struct rfid_protocol_handle *ph)
 {
-       free(ph);
+       free_protocol_handle(ph);
        return 0;
 }
 
        return 0;
 }
 
-struct rfid_protocol rfid_protocol_mfcl = {
+const struct rfid_protocol rfid_protocol_mfcl = {
        .id     = RFID_PROTOCOL_MIFARE_CLASSIC,
        .name   = "Mifare Classic",
        .fn     = {
        .id     = RFID_PROTOCOL_MIFARE_CLASSIC,
        .name   = "Mifare Classic",
        .fn     = {
index 5de050d..0ea55d2 100644 (file)
@@ -1,7 +1,7 @@
 
 /* Mifare Ultralight implementation, PCD side.
  *
 
 /* Mifare Ultralight implementation, PCD side.
  *
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  *
  */
 
  *
  */
 
@@ -112,17 +112,17 @@ static struct rfid_protocol_handle *
 mful_init(struct rfid_layer2_handle *l2h)
 {
        struct rfid_protocol_handle *ph;
 mful_init(struct rfid_layer2_handle *l2h)
 {
        struct rfid_protocol_handle *ph;
-       ph = malloc(sizeof(struct rfid_protocol_handle));
+       ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
        return ph;
 }
 
 static int mful_fini(struct rfid_protocol_handle *ph)
 {
        return ph;
 }
 
 static int mful_fini(struct rfid_protocol_handle *ph)
 {
-       free(ph);
+       free_protocol_handle(ph);
        return 0;
 }
 
        return 0;
 }
 
-struct rfid_protocol rfid_protocol_mful = {
+const struct rfid_protocol rfid_protocol_mful = {
        .id     = RFID_PROTOCOL_MIFARE_UL,
        .name   = "Mifare Ultralight",
        .fn     = {
        .id     = RFID_PROTOCOL_MIFARE_UL,
        .name   = "Mifare Ultralight",
        .fn     = {
index 2ed7a7c..d00e966 100644 (file)
@@ -1,6 +1,6 @@
 /* ISO 14443-4 (T=CL) implementation, PCD side.
  *
 /* ISO 14443-4 (T=CL) implementation, PCD side.
  *
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  *
  */
 
  *
  */
 
@@ -737,13 +737,13 @@ tcl_init(struct rfid_layer2_handle *l2h)
        struct rfid_protocol_handle *th;
        unsigned int mru = l2h->rh->ah->mru;
 
        struct rfid_protocol_handle *th;
        unsigned int mru = l2h->rh->ah->mru;
 
-       th = malloc(sizeof(struct rfid_protocol_handle) + mru);
+       th = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
        if (!th)
                return NULL;
 
        /* FIXME: mru should be attribute of layer2 (in case it adds/removes
         * some overhead */
        if (!th)
                return NULL;
 
        /* FIXME: mru should be attribute of layer2 (in case it adds/removes
         * some overhead */
-       memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
+       memset(th, 0, sizeof(struct rfid_protocol_handle));
 
        /* maximum received ats length equals mru of asic/reader */
        th->priv.tcl.state = TCL_STATE_INITIAL;
 
        /* maximum received ats length equals mru of asic/reader */
        th->priv.tcl.state = TCL_STATE_INITIAL;
@@ -758,11 +758,11 @@ tcl_init(struct rfid_layer2_handle *l2h)
 static int
 tcl_fini(struct rfid_protocol_handle *ph)
 {
 static int
 tcl_fini(struct rfid_protocol_handle *ph)
 {
-       free(ph);
+       free_protocol_handle(ph);
        return 0;
 }
 
        return 0;
 }
 
-struct rfid_protocol rfid_protocol_tcl = {
+const struct rfid_protocol rfid_protocol_tcl = {
        .id     = RFID_PROTOCOL_TCL,
        .name   = "ISO 14443-4 / T=CL",
        .fn     = {
        .id     = RFID_PROTOCOL_TCL,
        .name   = "ISO 14443-4 / T=CL",
        .fn     = {
index b24243a..1a4262a 100644 (file)
@@ -1,5 +1,5 @@
 /* librfid - layer 3 protocol handler 
 /* librfid - layer 3 protocol handler 
- * (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_protocol.h>
 
 #include <librfid/rfid_layer2.h>
 #include <librfid/rfid_protocol.h>
 
-static struct rfid_protocol *rfid_protocol_list;
+static const struct rfid_protocol *rfid_protocols[] = {
+       [RFID_PROTOCOL_MIFARE_CLASSIC]  = &rfid_protocol_mfcl,
+       [RFID_PROTOCOL_MIFARE_UL]       = &rfid_protocol_mful,
+       [RFID_PROTOCOL_TCL]             = &rfid_protocol_tcl,
+};
 
 struct rfid_protocol_handle *
 rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id)
 
 struct rfid_protocol_handle *
 rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id)
@@ -32,13 +36,12 @@ rfid_protocol_init(struct rfid_layer2_handle *l2h, unsigned int id)
        struct rfid_protocol *p;
        struct rfid_protocol_handle *ph = NULL;
 
        struct rfid_protocol *p;
        struct rfid_protocol_handle *ph = NULL;
 
-       for (p = rfid_protocol_list; p; p = p->next) {
-               if (p->id == id) {
-                       ph = p->fn.init(l2h);
-                       break;
-               }
-       }
+       if (id >= ARRAY_SIZE(rfid_protocols))
+               return NULL;
+
+       p = rfid_protocols[id];
 
 
+       ph = p->fn.init(l2h);
        if (!ph)
                return NULL;
 
        if (!ph)
                return NULL;
 
@@ -103,15 +106,6 @@ rfid_protocol_close(struct rfid_protocol_handle *ph)
        return 0;
 }
 
        return 0;
 }
 
-int
-rfid_protocol_register(struct rfid_protocol *p)
-{
-       p->next = rfid_protocol_list;
-       rfid_protocol_list = p;
-
-       return 0;
-}
-
 char *rfid_protocol_name(struct rfid_protocol_handle *ph)
 {
        return ph->proto->name;
 char *rfid_protocol_name(struct rfid_protocol_handle *ph)
 {
        return ph->proto->name;
index f41ae8f..a5dbeb2 100644 (file)
@@ -1,5 +1,5 @@
 /* librfid - core reader handling
 /* librfid - core reader handling
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  */
 
 /*
  */
 
 /*
 
 #include <librfid/rfid.h>
 #include <librfid/rfid_reader.h>
 
 #include <librfid/rfid.h>
 #include <librfid/rfid_reader.h>
+#include <librfid/rfid_reader_cm5121.h>
+#include <librfid/rfid_reader_openpcd.h>
 
 
-static struct rfid_reader *rfid_reader_list;
+static const struct rfid_reader *rfid_readers[] = {
+#ifndef LIBRFID_FIRMWARE
+       [RFID_READER_CM5121]    = &rfid_reader_cm5121,
+#endif
+       [RFID_READER_OPENPCD]   = &rfid_reader_openpcd,
+};
 
 struct rfid_reader_handle *
 rfid_reader_open(void *data, unsigned int id)
 {
 
 struct rfid_reader_handle *
 rfid_reader_open(void *data, unsigned int id)
 {
-       struct rfid_reader *p;
+       const struct rfid_reader *p;
 
 
-       for (p = rfid_reader_list; p; p = p->next)
-               if (p->id == id)
-                       return p->open(data);
+       if (id >= ARRAY_SIZE(rfid_readers)) {
+               DEBUGP("unable to find matching reader\n");
+               return NULL;
+       }
 
 
-       DEBUGP("unable to find matching reader\n");
-       return NULL;
+       p = rfid_readers[id];
+
+       return p->open(data);
 }
 
 int
 }
 
 int
@@ -54,12 +63,3 @@ rfid_reader_close(struct rfid_reader_handle *rh)
 {
        rh->reader->close(rh);
 }
 {
        rh->reader->close(rh);
 }
-
-int
-rfid_reader_register(struct rfid_reader *r)
-{
-       r->next = rfid_reader_list;
-       rfid_reader_list = r;
-
-       return 0;
-}
index a9907ad..dd15fd6 100644 (file)
@@ -27,6 +27,8 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#ifndef LIBRFID_FIRMWARE
+
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -314,12 +316,12 @@ cm5121_open(void *data)
        struct rfid_reader_handle *rh;
        struct rfid_asic_transport_handle *rath;
 
        struct rfid_reader_handle *rh;
        struct rfid_asic_transport_handle *rath;
 
-       rh = malloc(sizeof(*rh));
+       rh = malloc_reader_handle(sizeof(*rh));
        if (!rh)
                return NULL;
        memset(rh, 0, sizeof(*rh));
 
        if (!rh)
                return NULL;
        memset(rh, 0, sizeof(*rh));
 
-       rath = malloc(sizeof(*rath));
+       rath = malloc_rat_handle(sizeof(*rath));
        if (!rath)
                goto out_rh;
        memset(rath, 0, sizeof(*rath));
        if (!rath)
                goto out_rh;
        memset(rath, 0, sizeof(*rath));
@@ -341,9 +343,9 @@ cm5121_open(void *data)
        return rh;
 
 out_rath:
        return rh;
 
 out_rath:
-       free(rath);
+       free_rat_handle(rath);
 out_rh:
 out_rh:
-       free(rh);
+       free_reader_handle(rh);
 
        return NULL;
 }
 
        return NULL;
 }
@@ -353,11 +355,11 @@ cm5121_close(struct rfid_reader_handle *rh)
 {
        struct rfid_asic_transport_handle *rath = rh->ah->rath;
        rc632_close(rh->ah);
 {
        struct rfid_asic_transport_handle *rath = rh->ah->rath;
        rc632_close(rh->ah);
-       free(rath);
-       free(rh);
+       free_rat_handle(rath);
+       free_reader_handle(rh);
 }
 
 }
 
-struct rfid_reader rfid_reader_cm5121 = {
+const struct rfid_reader rfid_reader_cm5121 = {
        .name   = "Omnikey CardMan 5121 RFID",
        .open = &cm5121_open,
        .close = &cm5121_close,
        .name   = "Omnikey CardMan 5121 RFID",
        .open = &cm5121_open,
        .close = &cm5121_close,
@@ -385,4 +387,4 @@ struct rfid_reader rfid_reader_cm5121 = {
        },
 };
 
        },
 };
 
-
+#endif /* LIBRFID_FIRMWARE */
index ca2cdff..15486b6 100644 (file)
@@ -45,7 +45,6 @@
 /* FIXME */
 #include "rc632.h"
 
 /* FIXME */
 #include "rc632.h"
 
-
 #define SENDBUF_LEN    (256+4+10) /* 256bytes max FSD/FSC, plus 4 bytes header,
                                    plus 10 bytes reserve */
 #define RECVBUF_LEN    SENDBUF_LEN
 #define SENDBUF_LEN    (256+4+10) /* 256bytes max FSD/FSC, plus 4 bytes header,
                                    plus 10 bytes reserve */
 #define RECVBUF_LEN    SENDBUF_LEN
@@ -56,6 +55,8 @@ static struct openpcd_hdr *snd_hdr;
 static struct openpcd_hdr *rcv_hdr;
 
 
 static struct openpcd_hdr *rcv_hdr;
 
 
+#ifndef LIBRFID_FIRMWARE
+
 static struct usb_device *dev;
 static struct usb_dev_handle *hdl;
 
 static struct usb_device *dev;
 static struct usb_dev_handle *hdl;
 
@@ -129,6 +130,8 @@ static struct usb_device *find_opcd_device(void)
        return NULL;
 }
 
        return NULL;
 }
 
+/* RC632 access primitives for librfid inside reader firmware */
+
 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
                             unsigned char reg, unsigned char value)
 {
 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
                             unsigned char reg, unsigned char value)
 {
@@ -205,6 +208,60 @@ static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
        return ret;
 }
 
        return ret;
 }
 
+const struct rfid_asic_transport openpcd_rat = {
+       .name = "OpenPCD Dumb USB Protocol",
+       .priv.rc632 = {
+               .fn = {
+                       .reg_write      = &openpcd_reg_write,
+                       .reg_read       = &openpcd_reg_read,
+                       .fifo_write     = &openpcd_fifo_write,
+                       .fifo_read      = &openpcd_fifo_read,
+               },
+       },
+};
+
+#else
+/* RC632 access primitives for librfid inside reader firmware */
+
+static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
+                            unsigned char reg, unsigned char value)
+{
+}
+
+static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
+                           unsigned char reg,
+                           unsigned char *value)
+{
+}
+
+
+static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
+                            unsigned char num_bytes,
+                            unsigned char *buf)
+{
+}
+
+static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
+                            unsigned char len,
+                            const unsigned char *bytes,
+                            unsigned char flags)
+{
+}
+
+const struct rfid_asic_transport openpcd_rat = {
+       .name = "OpenPCD Firmware RC632 Access",
+       .priv.rc632 = {
+               .fn = {
+                       .reg_write      = &openpcd_reg_write,
+                       .reg_read       = &openpcd_reg_read,
+                       .fifo_write     = &openpcd_fifo_write,
+                       .fifo_read      = &openpcd_fifo_read,
+               },
+       },
+};
+
+#endif /* LIBRFID_FIRMWARE */
+
 static int openpcd_transceive(struct rfid_reader_handle *rh,
                             enum rfid_frametype frametype,
                             const unsigned char *tx_data, unsigned int tx_len,
 static int openpcd_transceive(struct rfid_reader_handle *rh,
                             enum rfid_frametype frametype,
                             const unsigned char *tx_data, unsigned int tx_len,
@@ -299,18 +356,6 @@ openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
                                                        cmd, serno, block);
 }
 
                                                        cmd, serno, block);
 }
 
-struct rfid_asic_transport openpcd_ccid = {
-       .name = "OpenPCD Dumb USB Protocol",
-       .priv.rc632 = {
-               .fn = {
-                       .reg_write      = &openpcd_reg_write,
-                       .reg_read       = &openpcd_reg_read,
-                       .fifo_write     = &openpcd_fifo_write,
-                       .fifo_read      = &openpcd_fifo_read,
-               },
-       },
-};
-
 static struct rfid_reader_handle *
 openpcd_open(void *data)
 {
 static struct rfid_reader_handle *
 openpcd_open(void *data)
 {
@@ -344,17 +389,17 @@ openpcd_open(void *data)
                return NULL;
        }
 
                return NULL;
        }
 
-       rh = malloc(sizeof(*rh));
+       rh = malloc_reader_handle(sizeof(*rh));
        if (!rh)
                return NULL;
        memset(rh, 0, sizeof(*rh));
 
        if (!rh)
                return NULL;
        memset(rh, 0, sizeof(*rh));
 
-       rath = malloc(sizeof(*rath));
+       rath = malloc_rat_handle(sizeof(*rath));
        if (!rath)
                goto out_rh;
        memset(rath, 0, sizeof(*rath));
 
        if (!rath)
                goto out_rh;
        memset(rath, 0, sizeof(*rath));
 
-       rath->rat = &openpcd_ccid;
+       rath->rat = &openpcd_rat;
        rh->reader = &rfid_reader_openpcd;
 
        rh->ah = rc632_open(rath);
        rh->reader = &rfid_reader_openpcd;
 
        rh->ah = rc632_open(rath);
@@ -365,9 +410,9 @@ openpcd_open(void *data)
        return rh;
 
 out_rath:
        return rh;
 
 out_rath:
-       free(rath);
+       free_rat_handle(rath);
 out_rh:
 out_rh:
-       free(rh);
+       free_reader_handle(rh);
 
        return NULL;
 }
 
        return NULL;
 }
@@ -378,13 +423,13 @@ openpcd_close(struct rfid_reader_handle *rh)
        struct rfid_asic_transport_handle *rath = rh->ah->rath;
 
        rc632_close(rh->ah);
        struct rfid_asic_transport_handle *rath = rh->ah->rath;
 
        rc632_close(rh->ah);
-       free(rath);
-       free(rh);
+       free_rat_handle(rath);
+       free_reader_handle(rh);
 
        usb_close(hdl);
 }
 
 
        usb_close(hdl);
 }
 
-struct rfid_reader rfid_reader_openpcd = {
+const struct rfid_reader rfid_reader_openpcd = {
        .name   = "OpenPCD RFID Reader",
        .id = RFID_READER_OPENPCD,
        .open = &openpcd_open,
        .name   = "OpenPCD RFID Reader",
        .id = RFID_READER_OPENPCD,
        .open = &openpcd_open,