Ejemplo n.º 1
0
int main(void){
  HIDInterface* hid;
  hid_return ret;
  HIDInterfaceMatcher matcher = { VENDOR, PRODUCT, NULL, NULL, 0 };
  char packet[PACKET_LEN];

  hid_set_debug(HID_DEBUG_NONE);
  hid_set_debug_stream(stderr);
  hid_set_usb_debug(0);
  
  hid_init();
  hid = hid_new_HIDInterface();
  ret = hid_force_open(hid, 0, &matcher, 3);

  if (ret == HID_RET_FAIL_DETACH_DRIVER) {
      printf("Failed to detach Driver for Device %04x:%04x; Permission?\n", VENDOR, PRODUCT);
      return 1;
  }

  if (ret != HID_RET_SUCCESS) {
      printf("Could not open HID device %04x:%04x (ret was %d)\n", VENDOR, PRODUCT, ret);
      return 1;
  }

  // printf("hid_force_open ret=%d\n", ret);

  // hid_set_idle(hid,0,0);

  // Discard till first '\n'
  do {
    memset(packet,0,sizeof(packet));
    ret = hid_interrupt_read(hid,0x81,packet,PACKET_LEN,1000);
  } while (getTheChar(packet[1]) != '\n');

  while (1) {
    memset(packet,0,sizeof(packet));

    ret = hid_interrupt_read(hid,0x81,packet,PACKET_LEN,1000);

    /*
    if (ret == HID_RET_FAIL_INT_READ) {
        printf("Fail hid_interrupt_read\n");
    } else {
        printf("hid_interrupt_read ret=%d\n", ret);
    }
    */

    if( ret == HID_RET_SUCCESS ) {
      //printPacket(packet,PACKET_LEN);
      showTemperature(packet);
    }
  }

  hid_close(hid);
  hid_delete_HIDInterface(&hid);
  hid_cleanup();
  return 0;
}
Ejemplo n.º 2
0
Archivo: core.c Proyecto: axlrose/cells
/*
 *set cmd len is 8
 *set retbuf len is 64
 * */
int get_device_package( unsigned char *retbuf)
{
	hid_return ret;
	ret = hid_interrupt_read(hid, 3, retbuf, 64, 1000); 

	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_close failed with return code %d\n", ret);
		return -1;
	}
	return 0; 
}
Ejemplo n.º 3
0
void Set_Attenuation (float AttValue,HIDInterface* hid)
{
	int i;
	char PACKETreceive[SEND_PACKET_LEN];
	PACKET[0]=19; // Set Attenuation code is 19.
	PACKET[1]= (int)(AttValue);                  
	PACKET[2]= (int) ((AttValue-PACKET[1])*4);   
	ret = hid_interrupt_write(hid, 0x01, PACKET, SEND_PACKET_LEN,1000);
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_interrupt_write failed with return code %d\\n", ret);
	}	
	ret = hid_interrupt_read(hid, 0x01, PACKETreceive, SEND_PACKET_LEN,1000); //  Read packet  Packetreceive[0]=1    
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_interrupt_read failed with return code %d\n", ret);   }
}
Ejemplo n.º 4
0
void ReadAtt (char* AttStr,HIDInterface* hid)
{
	int i;
	char PACKETreceive[SEND_PACKET_LEN];
	PACKET[0]=18;  //     Ruturn attenuation  code 
	ret = hid_interrupt_write(hid, 0x01, PACKET, SEND_PACKET_LEN,1000);
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_interrupt_write failed with return code %d\n", ret);
	}
	ret = hid_interrupt_read(hid, 0x01, PACKETreceive, SEND_PACKET_LEN,1000);
	if (ret == HID_RET_SUCCESS) {strncpy(AttStr,PACKETreceive,SEND_PACKET_LEN);
		for(i=0;AttStr[i+1]!='\0';i++) {
			AttStr[i]=AttStr[i+1];
		}  
		AttStr[i]='\0';}
if (ret != HID_RET_SUCCESS) {
	fprintf(stderr, "hid_interrupt_read failed with return code %d\n", ret);}
}
Ejemplo n.º 5
0
void Get_PN (char* PNstr,HIDInterface* hid)  
{
	int i;
	char PACKETreceive[SEND_PACKET_LEN];
	PACKET[0]=40;  //     PN code
	ret = hid_interrupt_write(hid, 0x01, PACKET, SEND_PACKET_LEN,1000);
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_interrupt_write failed with return code %d\n", ret);
	}
	ret = hid_interrupt_read(hid, 0x01, PACKETreceive, SEND_PACKET_LEN,1000);
	if (ret == HID_RET_SUCCESS) {
		strncpy(PNstr,PACKETreceive,SEND_PACKET_LEN); 
		for (i=0;PNstr[i+1]!='\0';i++) {
			PNstr[i]=PNstr[i+1];
						}						  
		PNstr[i]='\0';
	}		 
	if (ret != HID_RET_SUCCESS) {
		fprintf(stderr, "hid_interrupt_read failed with return code %d\n", ret);}
}
Ejemplo n.º 6
0
static void RFIDCommunication(hid_return ret, HIDInterface** const hid)
{
    int const PATH_IN[] = { PATHIN };
    unsigned int SEND_PACKET_LEN1 = 3;
    unsigned int REPLY_PACKET_LENGTH1 = 3;

    printf("\n/* * * * * * * * BEGIN COMMUNICATION WITH RFID DEVICE * * * * * * * */");

    // SET ANTENNA POWER
    char PACKET1[] = { 0xC0, 0x03, 0x12 };
    ret = hid_set_output_report(*hid, PATH_IN, PATHLEN, PACKET1, SEND_PACKET_LEN1);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
    } else {
      printf("\n\n----->First sent: \n");
      printHex(PACKET1, SEND_PACKET_LEN1);
    }
    char reply1[REPLY_PACKET_LENGTH1+1];
    ret = hid_interrupt_read(*hid, EP_INTERRUPT_IN, reply1, REPLY_PACKET_LENGTH1, 100);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "interrupt read failed with return code %d\n", ret);
    } else {
      printf("\n\n----->First reply:\n");
      printHex(reply1, REPLY_PACKET_LENGTH1);
    }

    // TAG INVENTORY OPERATION
    // XXX: returns EPC Data
    unsigned int SEND_PACKET_LEN2 = 3;
    unsigned int REPLY_PACKET_LENGTH2 = 64;
    char PACKET2[] = { 0x31, 0x03, 0x01 };
    ret = hid_set_output_report(*hid, PATH_IN, PATHLEN, PACKET2, SEND_PACKET_LEN2);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
    } else {
      printf("\n\n----->Second sent:\n");
      printHex(PACKET2, SEND_PACKET_LEN2);
    }
    char reply2[REPLY_PACKET_LENGTH2+1];
    ret = hid_interrupt_read(*hid, EP_INTERRUPT_IN, reply2, REPLY_PACKET_LENGTH2, 100);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "interrupt read failed with return code %d\n", ret);
    } else {
      printf("\n\n----->Second reply:\n");
      printHex(reply2, REPLY_PACKET_LENGTH2);
    }

    // NEXT TAG 
    //unsigned int SEND_PACKET_LEN3 = 3;
    //unsigned int REPLY_PACKET_LENGTH3 = 64;
    //char PACKET3[] = { 0x31, 0x03, 0x02 };
    //ret = hid_set_output_report(*hid, PATH_IN, PATHLEN, PACKET3, SEND_PACKET_LEN3);
    //if (ret != HID_RET_SUCCESS) {
    //  fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
    //} else {
    //  printf("\n\n----->Third sent: \n");
    //  printHex(PACKET3, SEND_PACKET_LEN3);
    //}
    //char reply3[REPLY_PACKET_LENGTH3+1];
    //ret = hid_interrupt_read(*hid, EP_INTERRUPT_IN, reply3, REPLY_PACKET_LENGTH3, 100);
    //if (ret != HID_RET_SUCCESS) {
    //  fprintf(stderr, "interrupt read failed with return code %d\n", ret);
    //} else {
    //  printf("\n\n----->Third reply: \n", reply3);
    //  printHex(reply3, REPLY_PACKET_LENGTH3);
    //}

    // TAG SELECT OPERATION
    unsigned int SEND_PACKET_LEN4 = 15;
    unsigned int REPLY_PACKET_LENGTH4 = 3;
    char PACKET4[] = { 0x33, 0x0F, 0x0C, 0xCF, 0xFA, 0x04, 0x9C, 0x53, 0xE0, 0xC9, 0x7E, 0x13, 0x77, 0xFB, 0x97 };
    ret = hid_set_output_report(*hid, PATH_IN, PATHLEN, PACKET4, SEND_PACKET_LEN4);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
    } else {
      printf("\n\n----->Fourth sent: \n");
      printHex(PACKET4, SEND_PACKET_LEN4);
    }
    char reply4[REPLY_PACKET_LENGTH4+1];
    ret = hid_interrupt_read(*hid, EP_INTERRUPT_IN, reply4, REPLY_PACKET_LENGTH4, 100);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "interrupt read failed with return code %d\n", ret);
    } else {
      printf("\n\n----->Fourth reply: \n", reply4);
      printHex(reply4, REPLY_PACKET_LENGTH4);
    }

    // TAG READ EPC MEMORY OPERATION
    unsigned int SEND_PACKET_LEN5 = 9;
    unsigned int REPLY_PACKET_LENGTH5 = 64;
    char PACKET5[] = { 0x37, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04 };
    ret = hid_set_output_report(*hid, PATH_IN, PATHLEN, PACKET5, SEND_PACKET_LEN5);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
    } else {
      printf("\n\n----->Fifth sent: \n");
      printHex(PACKET5, SEND_PACKET_LEN5);
    }
    char reply5[REPLY_PACKET_LENGTH5+1];
    ret = hid_interrupt_read(*hid, EP_INTERRUPT_IN, reply5, REPLY_PACKET_LENGTH5, 100);
    if (ret != HID_RET_SUCCESS) {
      fprintf(stderr, "interrupt read failed with return code %d\n", ret);
    } else {
      printf("\n\n----->Fifth reply: \n", reply5);
      printHex(reply5, REPLY_PACKET_LENGTH5);
    }
}
Ejemplo n.º 7
0
Archivo: main.c Proyecto: patzy/rcd
int main(int argc,char **argv)
{
  ir_receiver_t* receiver;

  // parse command line args
  if (argc!=2) {
    printf("You must specify the receiver type: %s type\n",argv[0]);
    printf("Valid values for type are:\n");
    unsigned int i=0;
    for (i=0;i<NB_IRR;i++)
      {
        printf("\t- %s\n",receivers[i].description);
      }
    return 1;
  }
  else {
    unsigned int i=0;
    for (i=0;i<NB_IRR;i++)
      {
        if (strncmp(receivers[i].description,argv[1],256)==0) {
          receiver=&receivers[i];
          break;
        }
      }
  }
  printf("Running for type: %s\n",receiver->description);

  HIDInterface* hid;
  hid_return ret;
  hid_set_debug(0/*HID_DEBUG_ALL*/);
  hid_set_debug_stream(stderr);
  /* passed directly to libusb */
  hid_set_usb_debug(0);
  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return 1;
  }

  hid = hid_new_HIDInterface();
  if (hid == 0) {
    fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
    return 1;
  }

  ret = hid_force_open(hid, 0, &receiver->matcher, 3);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
    return 1;
  }

  ret = hid_write_identification(stderr, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_write_identification failed with return code %d\n",
            ret);
    return 1;
  }

  ret = hid_dump_tree(stderr, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret);
    return 1;
  }

  /* Infinite parsing loop use C-c to stop the program */

  while (1)
    {
      char packet[receiver->packetSize];
      ret = hid_interrupt_read(hid, 0x81,  packet, receiver->packetSize,10000);
      if (ret == HID_RET_SUCCESS) {
          fflush(stdout);
       }
    }
  ret = hid_close(hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_close failed with return code %d\n", ret);
    return 1;
  }

  hid_delete_HIDInterface(&hid);

  ret = hid_cleanup();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
    return 1;
  }

  return 0;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[])
{
  HIDInterface* hid;
  int iface_num = 2;
  hid_return ret;
  int index=0;
  char buff[42];
  unsigned short vendor_id  = 0x0451;
  unsigned short product_id = 0x2100;
  int flag;
  uint16_t addr=0;

  uint8_t eepromBuff[512];
  memset(eepromBuff, 0x00, 512);

  // HIDInterfaceMatcher matcher = { 0x0925, 0x1237, NULL, NULL, 0 };
  HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 };

#if 1
  /* see include/debug.h for possible values */
  hid_set_debug(HID_DEBUG_ALL);
  hid_set_debug_stream(stderr);
  /* passed directly to libusb */
  hid_set_usb_debug(0);
  
  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return 1;
  }

  hid = hid_new_HIDInterface();
  if (hid == 0) {
    fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
    return 1;
  }

  ret = hid_force_open(hid, iface_num, &matcher, 3);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
    return 1;
  }

  ret = hid_write_identification(stdout, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_write_identification failed with return code %d\n", ret);
    return 1;
  }

  ret = hid_dump_tree(stdout, hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_dump_tree failed with return code %d\n", ret);
    return 1;
  }

//#define _READ
#ifdef _READ
  memset(eepromBuff, 0x00, 512);
  for (index=0;index < 64;index++) {
      memset(buff, 0x00, 42);
      addr = 8*index;
      buff[0] = PCTransfer;
      buff[1] = RequestEepromSectionRead;
      buff[2] = (0xFF00 & addr) >> 8; //M:offset
      buff[3] = 0x00FF & addr; //L:offset
      buff[4] = 8; //bytes

      fprintf(stderr, "sending:\n");
      print_bytes(LOG_ERROR, buff, 42);

      ret = hid_interrupt_write(hid, 0x04, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }
      ret = hid_interrupt_read(hid, 0x03, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }

      fprintf(stderr, "readed:\n");
      print_bytes(LOG_ERROR, buff, 42);

      if(buff[0] & PCRequestError) {
        logwrite(LOG_ERROR, "USB: request error");
      }

      if(!(buff[1] & RequestDone)) {
        logwrite(LOG_ERROR, "USB: request is not done!");
      }

      logwrite(LOG_ERROR, "USB: retval: %02x", buff[2]);
      memcpy(&eepromBuff[8*index], &buff[3], 8);
  }
#endif

#define _WRITE
#ifdef _WRITE
  memset(eepromBuff, 0xFF, 512);
  int commands = eeprom_data_size()/8;
  if(commands < 0 || commands > 62) {
    logwrite(LOG_ERROR, "WRONG COMMANDS NUMBER: %d", commands);
    exit(1);
  }
  logwrite(LOG_ERROR, "total commands: %d", commands);
  /* 2 bytes of sync + number of commands! */
  eepromBuff[0] = 0xb0;
  eepromBuff[1] = 0x0b;
  eepromBuff[2] = commands;

  for (index=0;index < commands;index++) {
      fprintf(stderr, "w%d: eepromBuff[%d], eepromData[%d]\n", index, (index+1)*8, index*8);
      memcpy(&eepromBuff[(index+1)*8], &eepromData[8*index], 8);
  }

  for (index=0;index <= commands;index++) {
      addr = 8*index;
      memset(buff, 0x00, 42);
      buff[0] = PCTransfer;
      buff[1] = RequestEepromSectionWrite;
      buff[2] = (0xFF00 & addr) >> 8; //M:offset
      buff[3] = 0x00FF & addr; //L:offset
      buff[4] = 8; //bytes
      memcpy(&buff[5], &eepromBuff[8*index], 8);

      fprintf(stderr, "sending:\n");
      print_bytes(LOG_ERROR, buff, 42);

      ret = hid_interrupt_write(hid, 0x04, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }
      ret = hid_interrupt_read(hid, 0x03, buff, 42, 1000);
      if (ret != HID_RET_SUCCESS) {
        fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
      }

      fprintf(stderr, "readed:\n");
      print_bytes(LOG_ERROR, buff, 42);

      if(buff[0] & PCRequestError) {
        logwrite(LOG_ERROR, "USB: request error");
      }

      if(!(buff[1] & RequestDone)) {
        logwrite(LOG_ERROR, "USB: request is not done!");
      }

      logwrite(LOG_ERROR, "USB: retval: %02x", buff[2]);
  }
#endif

  ret = hid_close(hid);
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_close failed with return code %d\n", ret);
    return 1;
  }

  hid_delete_HIDInterface(&hid);

  ret = hid_cleanup();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
    return 1;
  }
#endif

  fprintf(stderr, "EEPROM DATA:\n");
  for (index=0;index < 64;index++) {
    print_bytes(LOG_ERROR, &eepromBuff[8*index], 8);
  }


  return 0;
}
Ejemplo n.º 9
0
int main (int argc, char *argv[])
{
	char buffer[64];
	HIDInterface* hid;
	HIDInterfaceMatcher matcher = { CH_USB_VID, CH_USB_PID_FIRMWARE, NULL, NULL, 0 };
	hid_return ret;
	int i;
	int iface_num = 0;
	int length;
	unsigned int timeout = 1000; /* ms */

	hid_set_debug (HID_DEBUG_ALL);
	hid_set_debug_stream (stderr);
	hid_set_usb_debug (0);

	ret = hid_init ();
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_init failed with return code %d\n", ret);
		return 1;
	}

	hid = hid_new_HIDInterface ();
	if (hid == 0) {
		fprintf (stderr, "hid_new_HIDInterface () failed\n");
		return 1;
	}

	ret = hid_force_open (hid, iface_num, &matcher, 3);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_force_open failed with return code %d\n", ret);
		return 1;
	}

	ret = hid_write_identification (stdout, hid);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_write_identification failed with return code %d\n", ret);
		return 1;
	}

	ret = hid_dump_tree (stdout, hid);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_dump_tree failed with return code %d\n", ret);
		return 1;
	}

	/* turn all the LEDs on */
	memset (buffer, 0x00, sizeof (buffer));
	buffer[0] = 0x0e;
	buffer[1] = 0x03;
	ret = hid_interrupt_write (hid, 0x01,  buffer, sizeof (buffer), timeout);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "failed calling hid_interrupt_write\n");
		return 1;
	}

	/* read the return message */
	length = 2;
	ret = hid_interrupt_read (hid, 0x01, buffer, length, timeout);
	if (ret != HID_RET_SUCCESS) {
		printf ("hid_interrupt_read failed\n");
		return 1;
	}
	printf ("Data received from USB device: ");
	for (i=0; i < length; i++) {
		printf ("0x%02x, ", buffer[i]);
	}
	printf ("\n");

	ret = hid_close (hid);
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_close failed with return code %d\n", ret);
		return 1;
	}

	hid_delete_HIDInterface (&hid);

	ret = hid_cleanup ();
	if (ret != HID_RET_SUCCESS) {
		fprintf (stderr, "hid_cleanup failed with return code %d\n", ret);
		return 1;
	}

	return 0;
}