dependencies for debian install
[librfid] / pegoda / pegoda.c
index 56f425e..7422efe 100644 (file)
@@ -12,7 +12,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
  */
 
 
@@ -26,7 +26,7 @@
 #include "pegoda.h"
 
 const char *
-rfid_hexdump(const void *data, unsigned int len)
+hexdump(const void *data, unsigned int len)
 {
        static char string[1024];
        unsigned char *d = (unsigned char *) data;
@@ -65,7 +65,7 @@ struct usb_device *find_device(u_int16_t vendor, u_int16_t device)
        return NULL;
 }
 
-int pegoda_transcieve(struct pegoda_handle *ph,
+int pegoda_transceive(struct pegoda_handle *ph,
                      u_int8_t cmd, unsigned char *tx, unsigned int tx_len,
                      unsigned char *rx, unsigned int *rx_len)
 {
@@ -74,6 +74,7 @@ int pegoda_transcieve(struct pegoda_handle *ph,
        int rc;
        unsigned int len_expected;
        struct pegoda_cmd_hdr *hdr = (struct pegoda_cmd_hdr *)txbuf;
+       struct pegoda_cmd_hdr *rxhdr = (struct pegoda_cmd_hdr *)rxbuf;
 
        hdr->seq = ++(ph->seq);
        hdr->cmd = cmd;
@@ -81,7 +82,7 @@ int pegoda_transcieve(struct pegoda_handle *ph,
        memcpy(txbuf + sizeof(*hdr), tx, tx_len);
 
        printf("tx [%u]: %s\n", tx_len+sizeof(*hdr),
-               rfid_hexdump(txbuf, tx_len + sizeof(*hdr)));
+               hexdump(txbuf, tx_len + sizeof(*hdr)));
        rc = usb_bulk_write(ph->handle, 0x02, (char *)txbuf,
                            tx_len + sizeof(*hdr), 0);
        if (rc < 0)
@@ -95,7 +96,7 @@ int pegoda_transcieve(struct pegoda_handle *ph,
                fprintf(stderr, "unexpected: received %u bytes as length?\n");
                return -EIO;
        }
-       printf("len [%u]: %s\n", rc, rfid_hexdump(rxbuf, rc));
+       printf("len [%u]: %s\n", rc, hexdump(rxbuf, rc));
 
        len_expected = rxbuf[0];
 
@@ -105,12 +106,19 @@ int pegoda_transcieve(struct pegoda_handle *ph,
        rc = usb_bulk_read(ph->handle, 0x81, (char *)rxbuf, len_expected, 0);
        if (rc <= 0)
                return rc;
-       printf("rx [%u]: %s\n", rc, rfid_hexdump(rxbuf, rc));
+       printf("rx [%u]: %s\n", rc, hexdump(rxbuf, rc));
 
-       memcpy(rx, rxbuf+1, rc-1);
-       *rx_len = rc - 1;
+       if (rc < 4)
+               return -EIO;
 
-       return 0;
+       if (rxhdr->seq != hdr->seq)
+               return -EIO;
+
+       *rx_len = ntohs(rxhdr->len);
+
+       memcpy(rx, rxbuf+sizeof(*rxhdr), rc-sizeof(*rxhdr));
+
+       return rxhdr->cmd;
 }
 
 struct pegoda_handle *pegoda_open(void)
@@ -163,7 +171,7 @@ struct pegoda_handle *pegoda_open(void)
 
        printf("alt setting 1 selected\n");
 
-       pegoda_transcieve(ph, PEGODA_CMD_PCD_CONFIG, NULL, 0, rbuf, &rlen);
+       pegoda_transceive(ph, PEGODA_CMD_PCD_CONFIG, NULL, 0, rbuf, &rlen);
 
        return ph;
 out_free:
@@ -199,7 +207,7 @@ static int pegoda_auth_e2(struct pegoda_handle *ph,
        buf[1] = keynr;         /* key number */
        buf[2] = sector;        /* sector */
        rlen = sizeof(rbuf);
-       pegoda_transcieve(ph, PEGODA_CMD_PICC_AUTH, buf, 3, rbuf, &rlen);
+       pegoda_transceive(ph, PEGODA_CMD_PICC_AUTH, buf, 3, rbuf, &rlen);
 
        /* FIXME: check response */
 
@@ -218,7 +226,7 @@ static int pegoda_auth_key(struct pegoda_handle *ph,
        mifare_transform_key(key6, buf+5);
        buf[17] = sector;
 
-       pegoda_transcieve(ph, PEGODA_CMD_PICC_AUTH_KEY, buf, 18, rbuf, &rlen);
+       pegoda_transceive(ph, PEGODA_CMD_PICC_AUTH_KEY, buf, 18, rbuf, &rlen);
 
        /* FIXME: check response */
 
@@ -228,10 +236,15 @@ static int pegoda_auth_key(struct pegoda_handle *ph,
 static int pegoda_read16(struct pegoda_handle *ph,
                         u_int8_t page, unsigned char *rx)
 {
-       unsigned int rlen = 24;
+       int rc;
+       unsigned int rlen = 16;
 
-       return pegoda_transcieve(ph, PEGODA_CMD_PICC_READ,
-                                &page, 1, rx, &rlen);
+       rc = pegoda_transceive(ph, PEGODA_CMD_PICC_READ,
+                               &page, 1, rx, &rlen);
+       if (rlen != 16)
+               return -EIO;
+
+       return 0;
 }
 
 int main(int argc, char **argv)
@@ -240,6 +253,7 @@ int main(int argc, char **argv)
        unsigned char rbuf[256];
        unsigned int rlen = sizeof(rbuf);
        struct pegoda_handle *ph;
+       int i;
 
        ph = pegoda_open();
        if (!ph)
@@ -248,31 +262,37 @@ int main(int argc, char **argv)
        /* LED off */
        buf[0] = 0x00;
        rlen = sizeof(rbuf);
-       pegoda_transcieve(ph, PEGODA_CMD_SWITCH_LED, buf, 1, rbuf, &rlen);
+       pegoda_transceive(ph, PEGODA_CMD_SWITCH_LED, buf, 1, rbuf, &rlen);
 
        /* anticollision */
 
        buf[0] = 0x26;
        rlen = sizeof(rbuf);
-       pegoda_transcieve(ph, PEGODA_CMD_PICC_COMMON_REQUEST, 
+       pegoda_transceive(ph, PEGODA_CMD_PICC_COMMON_REQUEST, 
                          buf, 1, rbuf, &rlen);
 
        buf[0] = 0x93;
        memset(buf+1, 0, 5);
        rlen = sizeof(rbuf);
-       pegoda_transcieve(ph, PEGODA_CMD_PICC_CASC_ANTICOLL, 
+       pegoda_transceive(ph, PEGODA_CMD_PICC_CASC_ANTICOLL, 
                          buf, 6, rbuf, &rlen);
 
-       memcpy(ph->snr, rbuf+3, 4);
+       memcpy(ph->snr, rbuf, 4);
 
        buf[0] = 0x93;
        memcpy(buf+1, ph->snr, 4);
        rlen = sizeof(rbuf);
-       pegoda_transcieve(ph, PEGODA_CMD_PICC_CASC_SELECT, 
+       pegoda_transceive(ph, PEGODA_CMD_PICC_CASC_SELECT, 
                          buf, 5, rbuf, &rlen);
 
-       pegoda_auth_key(ph, 0, "\xff\xff\xff\xff\xff\xff");
-       pegoda_read16(ph, 0, rbuf);
+       for (i = 0; i < 16; i++) {
+               int j;
+               pegoda_auth_key(ph, i, "\xff\xff\xff\xff\xff\xff");
+               for (j = 0; j < 4; j++) {
+                       pegoda_read16(ph, (i*4)+j, rbuf);
+                       printf("read16[%u:%u] = %s\n", i,j,hexdump(rbuf, 16));
+               }
+       }
        
        exit(0);
 }