BUGFIX: l3_init can fail on desfire ev3 and produce SIGSEGV
[librfid] / src / rfid_proto_tcl.c
index ae47f87..cfde04b 100644 (file)
@@ -1,6 +1,6 @@
 /* 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>
  *
  */
 
@@ -16,7 +16,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 #include <stdio.h>
@@ -122,7 +122,8 @@ tcl_parse_ats(struct rfid_protocol_handle *h,
                } else {
                        /* Section 7.2: fwi for type B is always in ATQB */
                        /* Value is assigned in tcl_connect() */
-                       /* This function is never called for Type B, since it has no (R)ATS */
+                       /* This function is never called for Type B, 
+                        * since Type B has no (R)ATS */
                }
                return 0;
        }
@@ -163,6 +164,8 @@ tcl_parse_ats(struct rfid_protocol_handle *h,
 
        h->priv.tcl.historical_len = (ats+len) - cur;
        h->priv.tcl.historical_bytes = cur;
+       
+       DEBUGP("ATS parsed: %s\n", rfid_hexdump(ats, size));
 
        return 0;
 }
@@ -259,7 +262,9 @@ tcl_do_pps(struct rfid_protocol_handle *h)
 {
        int ret;
        unsigned char ppss[3];
-       unsigned char pps_response[1];
+       /* FIXME: this stinks like hell. IF we reduce pps_response size to one,
+          we'll get stack corruption! */
+       unsigned char pps_response[10];
        unsigned int rx_len = 1;
        unsigned char Dr, Ds, DrI, DsI;
        unsigned int speed;
@@ -345,9 +350,8 @@ tcl_build_prologue2(struct tcl_handle *th,
                prlg[*prlg_len] = th->cid & 0x0f;
        }
 
-       /* nad only for I-block (0xc0 == 00) */
-       if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
-           ((pcb & 0xc0) == 0x00)) {
+       /* nad only for I-block */
+       if ((th->flags & TCL_HANDLE_F_NAD_USED) && is_i_block(pcb)) {
                /* ISO 14443-4:2000(E) Section 7.1.1.3 */
                /* FIXME: in case of chaining only for first frame */
                *prlg |= TCL_PCB_NAD_FOLLOWING;
@@ -736,13 +740,13 @@ tcl_init(struct rfid_layer2_handle *l2h)
        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 */
-       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;
@@ -757,11 +761,50 @@ tcl_init(struct rfid_layer2_handle *l2h)
 static int
 tcl_fini(struct rfid_protocol_handle *ph)
 {
-       free(ph);
+       free_protocol_handle(ph);
+       return 0;
+}
+
+int 
+tcl_getopt(struct rfid_protocol_handle *h, int optname, void *optval,
+          unsigned int *optlen)
+{
+       u_int8_t *opt_str = optval;
+
+       switch (optname) {
+       case RFID_OPT_P_TCL_ATS:
+               if (h->priv.tcl.ats_len < *optlen)
+                       *optlen = h->priv.tcl.ats_len;
+               memcpy(opt_str, h->priv.tcl.ats, *optlen);
+               break;
+       case RFID_OPT_P_TCL_ATS_LEN:
+               if (*optlen < sizeof(u_int8_t))
+                       return -E2BIG;
+               *optlen = sizeof(u_int8_t);
+               *opt_str = h->priv.tcl.ats_len & 0xff;
+               break;
+       default:
+               return -EINVAL;
+       }
+
        return 0;
 }
 
-struct rfid_protocol rfid_protocol_tcl = {
+int
+tcl_setopt(struct rfid_protocol_handle *h, int optname, const void *optval,
+          unsigned int optlen)
+{
+       int ret = -EINVAL;
+
+       switch (optname) {
+       default:
+               break;
+       }
+
+       return ret;
+}
+
+const struct rfid_protocol rfid_protocol_tcl = {
        .id     = RFID_PROTOCOL_TCL,
        .name   = "ISO 14443-4 / T=CL",
        .fn     = {
@@ -770,5 +813,7 @@ struct rfid_protocol rfid_protocol_tcl = {
                .transceive = &tcl_transceive,
                .close = &tcl_deselect,
                .fini = &tcl_fini,
+               .getopt = &tcl_getopt,
+               .setopt = &tcl_setopt,
        },
 };