basic transport working, unsorted defines.
[rfid/librfid.git] / gemtag / gemtag.c
index 7a2e8ce..c2b24d7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * (C) 2006 by Frank Zirkelbach <hackbard@hackdaworld.org>
+ *  (C) 2006 by Frank Zirkelbach <hackbard@hackdaworld.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 
 #include <usb.h>
 #include "gemtag.h"
 
+int asciidump(unsigned char *data,int len) {
+       int i;
+
+       for(i=0;i<len;i++)
+               if((data[i]>0x19)&&(data[i]<0x7f)) printf("%c",data[i]);
+               else printf("_");
+       printf("\n");
+
+       return 0;
+}
+
 int hexdump(unsigned char *data,int len) {
        int i;
 
@@ -83,8 +94,8 @@ int gemtag_transcieve(struct gemtag_handle *gh,unsigned char cmd,
        unsigned char rxbuf[256];
        struct gemtag_cmd_hdr *txhdr;
        struct gemtag_cmd_hdr *rxhdr;
-       u_int16_t *crcptr;
-       int ret,size;
+       u_int16_t crc,*crcptr;
+       int i,ret,size,rest;
 
        txhdr=(struct gemtag_cmd_hdr *)txbuf;
        rxhdr=(struct gemtag_cmd_hdr *)rxbuf;
@@ -92,71 +103,97 @@ int gemtag_transcieve(struct gemtag_handle *gh,unsigned char cmd,
        txhdr->start=0xa5;
        txhdr->seq=++(gh->seq);
        txhdr->cmd=cmd;
-       txhdr->len=htons(tx_len);
+       txhdr->len=tx_len;
        size=sizeof(struct gemtag_cmd_hdr);
        memcpy(txbuf+size,tx,tx_len);
+       size+=tx_len;
 
        /* crc check */
-       if(gh->capabilities&GEMTAG_CAP_CRC) {
-               size+=tx_len;
+       if(gh->caps&GEMTAG_CAP_CRC) {
                crcptr=(u_int16_t *)(txbuf+size);
-               *crcptr=htons(gemtag_calc_crc(txbuf,size));
+               crc=gemtag_calc_crc(txbuf,size);
+               //*crcptr=(crc>>8)|(crc<<8);
+               *crcptr=crc;
                size+=2;
        }
 
        /* usb write */
-       printf("-> ");
-       hexdump(txbuf,size);
-       if(usb_clear_halt(gh->handle,0x02))
-               perror("clear halt (out)");
-       ret=usb_bulk_write(gh->handle,0x02,txbuf,size,0);
+       if(gh->caps&GEMTAG_CAP_VERB_TRANSMIT) {
+               printf("(%02d) -> ",size);
+               hexdump(txbuf,size);
+       }
+       ret=usb_interrupt_write(gh->handle,0x02,txbuf,size,0);
        if(ret<=0) {
-               perror("usb bulk write");
+               perror("usb interrupt write");
                return ret;
        }
 
        /* usb read */
-       if(usb_clear_halt(gh->handle,0x81))
-               perror("clear halt (in)");
-       ret=usb_bulk_read(gh->handle,0x81,rxbuf,sizeof(rxbuf),0);
-       size=ret;
+       ret=usb_interrupt_read(gh->handle,0x81,rxbuf,32,0);
        if(ret<=0) {
-               perror("usb bulk read");
+               perror("usb interrupt read");
                return ret;
        }
-       printf("<- ");
-       hexdump(rxbuf,ret);
-       
-       if(rxhdr->seq!=txhdr->seq)
-               puts("transmitted/recieved sequence number do not match");
+       if(ret<5) {
+               if(gh->caps&GEMTAG_CAP_VERB_TRANSMIT)
+                       printf("short answer (%d)\n",ret);
+               return -SHORT_ANSWER;
+       }
+
+       *rx_len=rxbuf[3]|(rxbuf[4]<<8);
+       size=*rx_len+sizeof(struct gemtag_cmd_hdr); 
+       if(gh->caps&GEMTAG_CAP_CRC) size+=2;
+
+       i=1;
+       rest=size-ret;
+       while(rest>=0) {
+               ret=usb_interrupt_read(gh->handle,0x81,rxbuf+i*32,32,0);
+               if(ret<=0) {
+                       perror("usb interrupt read (missing bytes)");
+                       return ret;
+               }
+               i++;
+               rest-=ret;
+       }
+
+       if(gh->caps&GEMTAG_CAP_VERB_TRANSMIT) {
+               printf("(%02d) <- ",size);
+               hexdump(rxbuf,size);
+       }
 
        /* crc check */
-       
+       if(gh->caps&GEMTAG_CAP_CRC) {
+               size-=2;
+               crcptr=(u_int16_t *)(rxbuf+size);
+               crc=gemtag_calc_crc(rxbuf,size);
+               if(((crc>>8)!=rxbuf[size+1])||((crc&0xff)!=rxbuf[size])) {
+                       printf("bad crc! (%04x)\n",crc);
+                       return -BAD_CRC;
+               }
+       }
 
-       *rx_len=ntohs(rxhdr->len);
-       memcpy(rx,rxbuf+sizeof(struct gemtag_cmd_hdr),
-              ret-sizeof(struct gemtag_cmd_hdr)+2);
-       hexdump(rxbuf,ret+2);
+       /* check sequence number */     
+       if(rxhdr->seq!=txhdr->seq) {
+               puts("transmitted/recieved sequence number do not match");
+               return -SEQ_MISMATCH;
+       }
+
+       memcpy(rx,rxbuf+sizeof(struct gemtag_cmd_hdr),*rx_len);
 
        return 0;
 }
 
 struct gemtag_handle *gemtag_open(void) {
        struct usb_device *gemtag;
-       unsigned char rbuf[16];
-       unsigned int rlen;
-       unsigned int i,numconf;
-       unsigned int j,numint;
-       unsigned int k,numalt;
+       unsigned int i;
        struct gemtag_handle *gh;
-
-       rlen=sizeof(rbuf);
+       char info[64];
 
        usb_init();
        usb_find_busses();
        usb_find_devices();
 
-       gemtag=find_device(USB_VENDOR_GEMTAG, USB_DEVICE_X501);
+       gemtag=find_device(USB_VENDOR_GEMTAG,USB_DEVICE_X501);
        if(!gemtag) return NULL;
 
        gh=malloc(sizeof(struct gemtag_handle));
@@ -164,51 +201,32 @@ struct gemtag_handle *gemtag_open(void) {
 
        memset(gh,0,sizeof(struct gemtag_handle));
 
-       numconf=gemtag->descriptor.bNumConfigurations;
-       printf("found gemtag (%02x/%02x), %u configuration(s)\n",
-              gemtag->descriptor.idVendor,
-              gemtag->descriptor.idProduct,numconf);
-       for(i=0;i<numconf;i++) {
-               numint=gemtag->config[i].bNumInterfaces;
-               printf("  config %u [nr %u] has %u interface(s)\n",
-                      i,gemtag->config[i].bConfigurationValue,
-                      numint);
-               for(j=0;j<numint;j++) {
-                       numalt=gemtag->config[i].interface[j].num_altsetting;
-                       printf("    interface %u has %u altsetting(s): ",
-                              j,numalt);
-                       for(k=0;k<numalt;k++)
-                               printf("%u ",
-               gemtag->config[i].interface[j].altsetting[k].bAlternateSetting);
-                       printf("\n");
-               }
-       }
-
        gh->handle=usb_open(gemtag);
        if(!gh->handle)
                goto out_free;
-       puts("usb_open successfull");
+
+       for(i=1;i<4;i++) {
+               memset(info,0,sizeof(info));
+               usb_get_string_simple(gh->handle,i,info,sizeof(info));
+               printf("%s ",info);
+       }
+       printf("opened successfully\n");
 
        if(usb_set_configuration(gh->handle,1)) {
                perror("set config");
                goto out_free;
        }
-       puts("configuration 1 successfully set");
+       printf("set configuration 1, ");
 
        if(usb_claim_interface(gh->handle,0)) {
                perror("claim interface");
                goto out_free;
        }
-       puts("interface 0 claimed");
-
-       while(usb_set_altinterface(gh->handle,0))
-               printf("trying to set alt interface\n");
-       puts("alt setting 0 selected");
+       printf("claimed interface 0, ");
 
-       gh->capabilities|=GEMTAG_CAP_CRC;
-
-       gemtag_transcieve(gh,GEMTAG_CMD_GET_FW_VERSION,
-                         NULL,0,rbuf,&rlen);
+       if(usb_set_altinterface(gh->handle,0))
+               perror("set alt interface");
+       printf("activated alt setting 0\n");
 
        return gh;
 
@@ -216,13 +234,58 @@ out_free:
        free(gh);
        return NULL;
 }
+
+int gemtag_close(struct gemtag_handle *gh) {
+
+       if(gh->handle) usb_close(gh->handle);
+       if(gh) free(gh);
+
+       return 0;
+}
+
        
 int main(int argc, char **argv) {
 
        struct gemtag_handle *gh;
+       unsigned char buf[256];
+       unsigned int buflen;
 
        gh=gemtag_open();
 
+       gh->caps|=GEMTAG_CAP_CRC;
+       gh->caps|=GEMTAG_CAP_VERB_TRANSMIT;
+
+       printf("Device:\n");
+       gemtag_transcieve(gh,GEMTAG_CMD_GET_FW_VERSION,
+                         NULL,0,buf,&buflen);
+       asciidump(buf,buflen);
+       printf("\n");
+
+       printf("Serial Number:\n");
+       gemtag_transcieve(gh,GEMTAG_CMD_GET_SERIAL_NUMBER,
+                         NULL,0,buf,&buflen);
+       printf("Snr: %d (%04x)\n",buf[1]<<8|buf[0],buf[1]<<8|buf[0]);
+       printf("\n");
+
+       printf("RC632 Version:\n");
+       gemtag_transcieve(gh,GEMTAG_CMD_GET_RIC_VERSION,
+                         NULL,0,buf,&buflen);
+       printf("\n");
+
+       printf("Switching off the LED!\n");
+       buf[0]=GEMTAG_LED_OFF;
+       gemtag_transcieve(gh,GEMTAG_CMD_SWITCH_LED,
+                         buf,1,buf,&buflen);
+       printf("\n");
+
+       printf("Detecting Card ...\n");
+       gemtag_transcieve(gh,GEMTAG_CMD_DETECT_CARD,
+                         NULL,0,buf,&buflen);
+       asciidump(buf,buflen);
+       printf("\n");
+
+       gemtag_close(gh);
+
        return 1;
 }