Exemplo n.º 1
0
 /*
  ** rfidtag_xchange(udp, *outp, out_len, *inp, in_len)
  **      send a command to the reader and gets the answer
  **      returns 0 if all goes well; -1 if not
  **LOCKED IN
  */
int
rfidtag_xchange (struct usb_dev_handle *udp, char *outp, int out_len, char *inp,
	      int in_len)
{
  int bytes_sent;
  int bytes_recv;

  count++;
  if ((bytes_sent =
       usb_bulk_write (udp, 0x02, (char *) outp, out_len, 10)) < 0)
    {
      if ((bytes_sent =
	   usb_bulk_write (udp, 0x02, (char *) outp, out_len, 10)) < 0)
	{
	  perror ("usb_bulk_write");
	}
    }
  if ((bytes_recv = usb_bulk_read (udp, 0x82, (char *) inp, in_len, 10)) < 0)
    {
      if ((bytes_recv =
	   usb_bulk_read (udp, 0x82, (char *) inp, in_len, 100)) < 0)
	{
	  perror ("usb_bulk_read");
	  return (-1);
	}
    }

  return bytes_recv;
}
Exemplo n.º 2
0
void queryData() {
    if ( Connection_Status  == CONNECTED) {
        char OutputPacketBuffer[EP_SIZE]; 
        char InputPacketBuffer[2][EP_SIZE]; 
        OutputPacketBuffer[0] = 0x81; //0x81 is the "Get Pushbutton State" command in the firmware

        /*
        //The following call to usb_bulk_write() sends EP_SIZE bytes of data to the USB device.
        if(usb_bulk_write(MyLibusbDeviceHandle, EP_OUT, &OutputPacketBuffer[0], EP_SIZE, 5000) != EP_SIZE) {
            fprintf(stderr, "queryData write failed\n");
            return;
        }
        */

        //Now get the response packet from the firmware.
        //The following call to usb_bulk_read() retrieves EP_SIZE bytes of data from the USB device.
        if(usb_bulk_read(MyLibusbDeviceHandle, EP_IN + 0, InputPacketBuffer[0], EP_SIZE, 5000) != EP_SIZE) {
            fprintf(stderr, "queryData read failed\n");
            return;
        }
        if(usb_bulk_read(MyLibusbDeviceHandle, EP_IN + 1, InputPacketBuffer[1], EP_SIZE, 5000) != EP_SIZE) {
            fprintf(stderr, "queryData read failed\n");
            return;
        }

        //printf("Received: [%s]", InputPacketBuffer);
    }
}
Exemplo n.º 3
0
  int ldnvf_read_packet(unsigned char *buffer, struct usb_dev_handle *hdl){
	 int i;
	 TMemoryCmd MemCmd;
	 unsigned int rxframesize;
	 
	 // send a vendor request to check if packet is available
	 MemCmd.dwAddress = 0x0;
	 MemCmd.dwLength = 0x0;
	 i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0xF0, 0, 0, (char *)&MemCmd, sizeof(MemCmd), 1000);
	 if (i < 0){
		  printf("%s: usb_control_msg failed %d\n", __FUNCTION__, i);
		  return 0;		
	 }

   i = usb_bulk_read(hdl, NETIF_RECV_EP, (char *)&rxframesize, sizeof(rxframesize), 2000);
   if (i < 0) {
  		printf("%s:usb_bulk_read failed %d\n", __FUNCTION__, i);
  		return 0;
   }
 
   //if there is a packet we read it via the bulk
   if(rxframesize){
      	 i = usb_bulk_read(hdl, NETIF_RECV_EP, (char *)buffer, rxframesize, 2000);
	       if (i < 0) {
	 	       printf("\n%s: usb_bulk_read failed %d", __FUNCTION__, i);
	 	       return 0;
	       }
   }
 
 
   return rxframesize;
  }
Exemplo n.º 4
0
int usbScanRead_USB_CTR(usb_dev_handle *udev, int count, int lastElement, __u16 *data)
{
  char value[64];
  int ret = -1;
  int nbytes = count*lastElement*2;    // nuber of bytes to read;
  __u8 status;

  ret = usb_bulk_read(udev, USB_ENDPOINT_IN|6, (char *) data, nbytes, HS_DELAY);

  if (ret != nbytes) {
    perror("usbScanRead_USB_CTR: error in usb_bulk_read.");
  }

  // if nbytes is a multiple of wMaxPacketSize the device will send a zero byte packet.
  if ((nbytes%wMaxPacketSize) == 0) {
    usb_bulk_read(udev, USB_ENDPOINT_IN|6, value, 2, 100);
  }

  status = usbStatus_USB_CTR(udev);
  if ((status & SCAN_OVERRUN)) {
    printf("Scan overrun.\n");
    usbScanStop_USB_CTR(udev);
    usbScanClearFIFO_USB_CTR(udev);
    usbScanBulkFlush_USB_CTR(udev, 5);
  }

  return ret;
}
Exemplo n.º 5
0
u8 usb_storage_read_capacity(u8 device)
{
	/* send cwb "usbc" */

	u8 tmp[8];
	u8 i;
	usb_storage_cbw  * cbw = (usb_storage_cbw*)malloc(sizeof(usb_storage_cbw));

	usb_control_msg(massstorage[device], 0x02,1,0, 0x8100, 0,tmp, 0);

	cbw->dCBWSignature= 0x43425355;
	cbw->dCBWTag=0x826A6008;
	cbw->dCBWDataTransferLength=0x00000008;
	cbw->bCWDFlags=0x80;
	cbw->bCBWLun=0x00;
	cbw->bCBWCBLength=0x0A;

	for(i=0;i<16;i++)
		cbw->CBWCB[i]=0x00;

	cbw->CBWCB[0]=0x25; // 0x12 = INQUIRY

	usb_bulk_write(massstorage[device], 2, (u8*)cbw, 31, 0); 
	usb_bulk_read(massstorage[device], 1, (u8*)cbw, 8, 0); 
	usb_bulk_read(massstorage[device], 1, (u8*)cbw, 13, 0); 

	free(cbw);

	return 0;
}
Exemplo n.º 6
0
u8 usb_storage_inquiry(u8 device)
{
	/* send cwb "usbc" */
	
	usb_storage_cbw *cbw = (usb_storage_cbw*)malloc(sizeof(usb_storage_cbw));
	cbw->dCBWSignature= 0x43425355;
	cbw->dCBWTag=0x826A6008;
	cbw->dCBWDataTransferLength=0x00000024;
	cbw->bCWDFlags=0x80;
	cbw->bCBWLun=0x00;
	cbw->bCBWCBLength=0x01;

	u8 i;
	for(i=0;i<16;i++)
		cbw->CBWCB[i]=0x00;

	cbw->CBWCB[0]=0x12; // 0x12 = INQUIRY

	usb_bulk_write(massstorage[device], 2, (u8*)cbw, 31, 0); 
	usb_bulk_read(massstorage[device], 1, (u8*)cbw, 36, 0); 
	usb_bulk_read(massstorage[device], 1, (u8*)cbw, 13, 0); 

	free(cbw);

	return 0;
}
Exemplo n.º 7
0
/*
 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
 */
static int dtc_start_download(void)
{
	int usb_err;
	uint8_t ep2txr;

	/* set up for download mode and make sure EP2 is set up to transmit */
	usb_err = ep1_generic_commandl(
			pHDev, 7,

			EP1_CMD_DTC_STOP,
			EP1_CMD_SET_UPLOAD,
			EP1_CMD_SET_DOWNLOAD,
			EP1_CMD_MEMORY_READ,	/* read EP2TXR for its data toggle */
			ST7_EP2TXR >> 8,
			ST7_EP2TXR,
			1
			);
	if (usb_err < 0)
		return usb_err;

	/* read back ep2txr */
	usb_err = usb_bulk_read(
			pHDev, USB_EP1IN_ADDR,
			(char *)&ep2txr, 1,
			USB_TIMEOUT_MS
			);
	if (usb_err < 0)
		return usb_err;

	usb_err = ep1_generic_commandl(
			pHDev, 13,

			EP1_CMD_MEMORY_WRITE,	/* preinitialize poll byte */
			DTC_STATUS_POLL_BYTE >> 8,
			DTC_STATUS_POLL_BYTE,
			1,
			0x00,
			EP1_CMD_MEMORY_WRITE,	/* set EP2IN to return data */
			ST7_EP2TXR >> 8,
			ST7_EP2TXR,
			1,
			(ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
			EP1_CMD_DTC_CALL,	/* start running the DTC */
			dtc_entry_download,
			EP1_CMD_DTC_GET_CACHED_STATUS
			);
	if (usb_err < 0)
		return usb_err;

	/* wait for completion */
	usb_err = usb_bulk_read(
			pHDev, USB_EP1IN_ADDR,
			(char *)&ep2txr, 1,
			USB_TIMEOUT_MS
			);

	return usb_err;
}
Exemplo n.º 8
0
APN_USB_TYPE ApnUsbGetImage( unsigned short *pMem )
{
	BOOLEAN Success;
	ULONG	ImageBytesRemaining;
	ULONG	ReceivedSize;

	Success = 1;
//	if ( (g_hSysDriver) == 0 )
//	{
//		return APN_USB_ERR_OPEN;
//	}

	ImageBytesRemaining = g_UsbImgSizeBytes;


	////////////////////////
	ULONG LoopCount = g_UsbImgSizeBytes / IMAGE_BUFFER_SIZE;
	ULONG Remainder	= g_UsbImgSizeBytes - ( LoopCount * IMAGE_BUFFER_SIZE );
	ULONG MemIterator = IMAGE_BUFFER_SIZE / 2;
	ULONG Counter;


	for ( Counter=0; Counter<LoopCount; Counter++ )
	{
		ReceivedSize = usb_bulk_read(g_hSysDriver, 0x86, 
				 (char *)pMem, IMAGE_BUFFER_SIZE, 1000);
//        printf("DRIVER: bulkread size=%x\n",ReceivedSize);

		if ( ReceivedSize != IMAGE_BUFFER_SIZE )
		{
			Success = 0;
			break;
		}
		else
		{
			pMem += MemIterator;
	printf(".");
		}
	}
	
	if ( Remainder != 0 )
	{
		ReceivedSize = usb_bulk_read(g_hSysDriver, 0x86, 
				(char *)pMem, Remainder, 1000);
//        printf("DRIVER: bulkread2 size=%x\n",ReceivedSize);

		if ( ReceivedSize != Remainder )
			Success = 0;
	}
	printf("\n");

	if ( !Success )
		return APN_USB_ERR_IMAGE_DOWNLOAD;

	return APN_USB_SUCCESS;		// Success
}
Exemplo n.º 9
0
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)
{
	unsigned char txbuf[256];
	unsigned char rxbuf[256];
	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;
	hdr->len = htons(tx_len);
	memcpy(txbuf + sizeof(*hdr), tx, tx_len);

	printf("tx [%u]: %s\n", 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)
		return rc;

	rc = usb_bulk_read(ph->handle, 0x81, (char *)rxbuf, sizeof(rxbuf), 0);
	if (rc <= 0)
		return rc;

	if (rc != 2) {
		fprintf(stderr, "unexpected: received %u bytes as length?\n");
		return -EIO;
	}
	printf("len [%u]: %s\n", rc, hexdump(rxbuf, rc));

	len_expected = rxbuf[0];

	if (len_expected > sizeof(rxbuf))
		return -EIO;

	rc = usb_bulk_read(ph->handle, 0x81, (char *)rxbuf, len_expected, 0);
	if (rc <= 0)
		return rc;
	printf("rx [%u]: %s\n", rc, hexdump(rxbuf, rc));

	if (rc < 4)
		return -EIO;

	if (rxhdr->seq != hdr->seq)
		return -EIO;

	*rx_len = ntohs(rxhdr->len);

	memcpy(rx, rxbuf+sizeof(*rxhdr), rc-sizeof(*rxhdr));

	return rxhdr->cmd;
}
Exemplo n.º 10
0
static int wrap_scsi(DPFContext * h, unsigned char *cmd, int cmdlen, char out,
		     unsigned char *data, unsigned long block_len)
{
    int len;
    int ret;
    static unsigned char ansbuf[13];	// Do not change size.

    g_buf[14] = cmdlen;
    memcpy(&g_buf[15], cmd, cmdlen);

    g_buf[8] = block_len;
    g_buf[9] = block_len >> 8;
    g_buf[10] = block_len >> 16;
    g_buf[11] = block_len >> 24;

    ret = usb_bulk_write(h->udev, ENDPT_OUT, (const char *) g_buf, sizeof(g_buf), 1000);
    if (ret < 0)
	return ret;

    if (out == DIR_OUT) {
	if (data) {
	    ret = usb_bulk_write(h->udev, ENDPT_OUT, (const char *) data, block_len, 3000);
	    if (ret != (int) block_len) {
		fprintf(stderr, "dpf_ax ERROR: bulk write.\n");
		return ret;
	    }
	}
    } else if (data) {
	ret = usb_bulk_read(h->udev, ENDPT_IN, (char *) data, block_len, 4000);
	if (ret != (int) block_len) {
	    fprintf(stderr, "dpf_ax ERROR: bulk read.\n");
	    return ret;
	}
    }
    // get ACK:
    len = sizeof(ansbuf);
    int retry = 0;
    int timeout = 0;
    do {
	timeout = 0;
	ret = usb_bulk_read(h->udev, ENDPT_IN, (char *) ansbuf, len, 5000);
	if (ret != len) {
	    fprintf(stderr, "dpf_ax ERROR: bulk ACK read.\n");
	    timeout = 1;
	}
	retry++;
    } while (timeout && retry < 5);
    if (strncmp((char *) ansbuf, "USBS", 4)) {
	fprintf(stderr, "dpf_ax ERROR: got invalid reply\n.");
	return -1;
    }
    // pass back return code set by peer:
    return ansbuf[12];
}
Exemplo n.º 11
0
int emulate_scsi(usb_dev_handle *dev, unsigned char *cmd, int cmdlen, char out,
	unsigned char *data, unsigned long block_len)
{
	int len;
	int ret;
	static unsigned char ansbuf[13]; // Do not change size.

	g_buf[14] = cmdlen;
	memcpy(&g_buf[15], cmd, cmdlen);

	g_buf[8] = block_len;
	g_buf[9] = block_len >> 8;
	g_buf[10] = block_len >> 16;
	g_buf[11] = block_len >> 24;

	ret = usb_bulk_write(dev, ENDPT_OUT, g_buf, sizeof(g_buf), 1000);
	if (ret < 0) return ret;

	if (out == DIR_OUT) {
		if (data) {
			ret = usb_bulk_write(dev, ENDPT_OUT, (const char* )data,
					block_len, 3000);
			if (ret != block_len) {
				perror("bulk write");
				return ret;
			}
		}
	} else if (data) {
		ret = usb_bulk_read(dev, ENDPT_IN, (char *) data, block_len, 4000);
		if (ret != block_len) {
			perror("bulk data read");
		}
	}
	// get ACK:
	len = sizeof(ansbuf);
	int retry = 0;
	do {
		ret = usb_bulk_read(dev, ENDPT_IN, (char *) ansbuf, len, 5000);
		if (ret != len) {
			perror("bulk ACK read");
			ret = DEVERR_TIMEOUT;
		}
		retry++;
	} while (ret == DEVERR_TIMEOUT && retry < 5);
	if (strncmp((char *) ansbuf, "USBS", 4)) {
		return handle_error("Got invalid reply\n");
	}
	// pass back return code set by peer:
	return ansbuf[12];
}
Exemplo n.º 12
0
int main(int argc, char **argv)
{
	unsigned char response[1024];
	int ret;
	int cnt;
	int last=0xff;

	usb_init();
	init();

	while(1) {
		ret = usb_bulk_read(handle, 0x86, (char*)response, 1024, 1000);
		printf("%d\n",ret);
		for(cnt=0;cnt<ret;cnt++) {
			if(response[cnt] != ((last+1)&0xff))
				printf("%d: last %d got %d\n",cnt,last, response[cnt]);
			last = response[cnt];
		}
		
		if(ret>0) {
	//		hexdump(response,ret);
	//		printf("\n");
			//fflush(stdout);
		}
		
	}

	return 0;
}
Exemplo n.º 13
0
//return 1 if the button was pressed, else 0
int usbdevice_checkbuttonstatus(struct usb_dev_handle *hdl){
	int i;
	char buttonstatus;	
	TMemoryCmd MemCmd;

	// send a vendor request to check button status
	MemCmd.dwAddress = 0x0;
	MemCmd.dwLength = 0x0;
	i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0xA0, 0, 0, (char *)&MemCmd, sizeof(MemCmd), 1000);
	if (i < 0){
		//fprintf(stderr, "usb_control_msg failed %d\n", i);
		return -1;		
	}

	//fprintf(stderr, "request success!\n");

	i = usb_bulk_read(hdl, 0x82, (char *)&buttonstatus, sizeof(buttonstatus), 2000);
	if (i < 0) {
		//fprintf(stderr, "usb_bulk_read failed %d\n", i);
		return -1;
	}

	if(buttonstatus){
		//printf("\nbutton press detected...");
		return 1;
	}


	return 0;
}
Exemplo n.º 14
0
bool ReceiveCommandPoll(UsbCommand *c)
{
  int ret;

  memset(c, 0, sizeof (UsbCommand));
  ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500);
  if (ret<0) {
    if (ret != -ETIMEDOUT) {
      error_occured = 1;
      if (return_on_error)
        return false;

      fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
        usb_strerror(), ret);

      if (devh) {
        usb_close(devh);
        devh = NULL;
      }
      while(!OpenProxmark(0)) { sleep(1); }
      printf(PROXPROMPT);
      fflush(NULL);

      return false;
    }
  } else {
    if (ret && (ret < sizeof(UsbCommand))) {
      fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
        ret, (int)sizeof(UsbCommand));
    }
  }

  return ret > 0;
}
Exemplo n.º 15
0
int xusb_recv(struct xusb *xusb, char *buf, size_t len, int timeout)
{
	int	ret;
	int	retries = 0;

	if (EP_IN(xusb) & USB_ENDPOINT_OUT) {
		ERR("%s called with an output endpoint 0x%x\n",
			__func__, EP_IN(xusb));
		return -EINVAL;
	}
retry_read:
	ret = usb_bulk_read(xusb->handle, EP_IN(xusb), buf, len, timeout);
	if (ret < 0) {
		DBG("bulk_read from endpoint 0x%x failed: (%d) %s\n",
			EP_IN(xusb), ret, usb_strerror());
		memset(buf, 0, len);
		return ret;
	}
	if (!ret) {
		ERR("bulk_read to endpoint 0x%x short read[%d]: (%d)\n",
			EP_IN(xusb), retries, ret);
		if (retries++ > MAX_RETRIES)
			return -EFAULT;
		usleep(100);
		goto retry_read;
	}
	dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, ret);
	return ret;
}
Exemplo n.º 16
0
wxThread::ExitCode GammaBlockUSB::Entry()
{
	//wxMutexLocker locker(m_processDataMutex);

	if(deviceFind() && deviceInit())
	{
		while(!GetThread()->TestDestroy())
		{
			GammaDataUSB* pDataOut(new GammaDataUSB);

			int bytesRead = 0x200;
#if defined(_MSC_VER) && defined(GAMMA_USB_CYAPI)
			LONG length = bytesRead;
			m_usbDevice->BulkInEndPt->XferData(pDataOut->data, length);
			bytesRead = length;
#else
			//usb_claim_interface(m_usbDevice, 0);

			bytesRead = usb_bulk_read(m_usbDevice, EP6IN, 
				(char*)pDataOut->data, bytesRead, GAMMA_READ_TIMEOUT);

			//usb_release_interface(m_usbDevice, 0);
#endif
			wxASSERT(GAMMA_READ_SIZE == bytesRead);
			
			pushData(wxSharedPtr<GammaData>(pDataOut));
		}
	}

	return 0;
}
Exemplo n.º 17
0
int usbcom_receive(usbcom_t com, int pipe, void *buf, int max_len)
{
  int    r;
  r = usb_bulk_read(com->dev, pipe, buf, max_len, 10000);
  if (debug) fprintf(stderr, "usbcom: max_len=%d r=%d\n", max_len, r);
  return r;
}
Exemplo n.º 18
0
Arquivo: grecko.c Projeto: 111X/radare
int grecko_read(unsigned long addr, unsigned char *buf, int size)
{
	unsigned char data[8];
	unsigned long addr2 = addr+size;
	unsigned char *ptr = (unsigned char*) &addr;
	int i, ret;

	addr = htonl(addr - (addr%4));
	addr2 = htonl(addr2 - (addr2%4));

	/* read command */
	grecko_send_command(4);

	/* memory offsets */
	memcpy(data, addr, 4);
	memcpy(data, addr2, 4);
	usb_bulk_write(dev, ep, (unsigned char *)&data, 8, timeout);

	/* data read */
	ret = usb_bulk_read(dev, ep, (unsigned char *)buf, 4096, timeout);
	if (ret < 0)
		return -1;
	for(i=0;i<ret;i++) {
		printf("%02x ", readbuf[i]);
	}

	return ret;
}
Exemplo n.º 19
0
static int io_loop(int dev_id)
{
  int ret;
  usb_dev_handle *hdev = g_devices[dev_id].hdev;
  int epin = g_devices[dev_id].epin;
  unsigned char buffer[512];
  unsigned char word[11];

  memset(buffer, 0, sizeof(buffer));
  memset(word, 0, sizeof(word));

  while(1)
  {
    memset(buffer, 0, sizeof(buffer));
    ret = usb_bulk_read(hdev, epin, (char*)buffer, sizeof(buffer), 10000);
    if(ret < 0)
    {
      printf("bulk_read returned %d (%s)\n", ret, usb_strerror());
      return -1;
    }
//    printf("read %d bytes: \n", ret);
    unsigned char *bufptr = (unsigned char *)buffer;
    int nb_words = ret/11; // incomplete words are resent
    while(nb_words--)
    {
      memcpy(word, bufptr, 11);
      bufptr+=11;
      process_frame(dev_id, word);
    }
  }
  return 0;
}
Exemplo n.º 20
0
nimu_data nimu::GetData()
{
	int ret;
	nimu_data ret_data;
	unsigned char data[NIMU_DATA_SIZE];

	ret = usb_bulk_read(nimu_dev, 0x82, (char*)data, NIMU_DATA_SIZE, 1000);
	if (ret < 0)
	{
		printf("Error reading data: %d (%s)\n",ret,usb_strerror());
		return ret_data;
	}

	ret_data.DeviceID = data[5];
	ret_data.MessageID = data[6];
	ret_data.SampleTimer = ntohs(*(uint16_t*)&data[7]);
	ret_data.GyroX = (short)ntohs(*(uint16_t*)&data[13]);
	ret_data.GyroY = (short)ntohs(*(uint16_t*)&data[15]);
	ret_data.GyroZ = (short)ntohs(*(uint16_t*)&data[17]);
	ret_data.AccelX = (short)ntohs(*(uint16_t*)&data[19]);
	ret_data.AccelY = (short)ntohs(*(uint16_t*)&data[21]);
	ret_data.AccelZ = (short)ntohs(*(uint16_t*)&data[23]);
	ret_data.MagX = (short)ntohs(*(uint16_t*)&data[25]);
	ret_data.MagY = (short)ntohs(*(uint16_t*)&data[27]);
	ret_data.MagZ = (short)ntohs(*(uint16_t*)&data[29]);
	ret_data.GyroTempX = (short)ntohs(*(uint16_t*)&data[31]);
	ret_data.GyroTempY = (short)ntohs(*(uint16_t*)&data[33]);
	ret_data.GyroTempZ = (short)ntohs(*(uint16_t*)&data[35]);

	return ret_data;
}
Exemplo n.º 21
0
 void test_bulk_transfer(usb_dev_handle *dev)
 {
   int r,i;
   char answer[reqBulkLen];
   char question[reqBulkLen];
   for (i=0;i<reqBulkLen; i++) question[i]=i;
   r = usb_bulk_write(dev, endpoint_Bulk_out, question, reqBulkLen, timeout);
   if( r < 0 )
   {
	  perror("USB bulk write"); bad("USB write failed"); 
   }
   r = usb_bulk_read(dev, endpoint_Bulk_in, answer, reqBulkLen, timeout);
   if( r != reqBulkLen )
   {
	  perror("USB bulk read"); bad("USB read failed"); 
   }
//   for (i=0;i<reqBulkLen;i++) printf("%i, %i, \n",question[i],answer[i]);
	for(i = 0;i < reqBulkLen; i++) {
		if(i%8 == 0)
			printf("\n");
		printf("%02x, %02x; ",question[i],answer[i]);
	}
	printf("\n");
//   usb_set_altinterface(dev, 0);
   usb_release_interface(dev, 0);
 }
Exemplo n.º 22
0
static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
{
	struct cp210x_transport *tr = (struct cp210x_transport *)tr_base;
	int rlen;
	time_t deadline = time(NULL) + TIMEOUT_S;

#ifdef DEBUG_CP210X
	printc(__FILE__": %s : read max %d\n", __FUNCTION__, max_len);
#endif

	while (time(NULL) < deadline) {
		rlen = usb_bulk_read(tr->handle, V1_IN_EP, (char *)databuf,
				     max_len, TIMEOUT_S * 1000);

#ifdef DEBUG_CP210X
		printc(__FILE__": %s : read %d\n", __FUNCTION__, rlen);
#endif

		if (rlen < 0) {
			pr_error(__FILE__": can't receive data");
			return -1;
		}

		if (rlen > 0) {
#ifdef DEBUG_CP210X
			debug_hexdump(__FILE__": USB transfer in", databuf, rlen);
#endif
			return rlen;
		}
	}

	pr_error(__FILE__": read operation timed out");
	return -1;
}
Exemplo n.º 23
0
static vsf_err_t versaloon_libusb_comm_transact(uint8_t *buffer_out,
        uint16_t out_len, uint8_t *buffer_in, uint16_t *in_len)
{
    int ret;

    ret = usb_bulk_write(versaloon_device_handle, usb_param_epout(),
                         (char *)buffer_out, out_len, versaloon_to);
    if (ret != out_len)
    {
        LOG_ERROR(ERRMSG_FAILURE_OPERATION_ERRSTRING, "send usb data",
                  usb_strerror());
        return ERRCODE_FAILURE_OPERATION;
    }

    if (in_len != NULL)
    {
        ret = usb_bulk_read(versaloon_device_handle, usb_param_epin(),
                            (char *)buffer_in, *in_len, versaloon_to);
        if (ret > 0)
        {
            *in_len = (uint16_t)ret;
            return VSFERR_NONE;
        }
        else
        {
            LOG_ERROR(ERRMSG_FAILURE_OPERATION_ERRSTRING, "receive usb data",
                      usb_strerror());
            return VSFERR_FAIL;
        }
    }
    else
    {
        return VSFERR_NONE;
    }
}
Exemplo n.º 24
0
static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
{
	struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
	int rlen;

	if (tr->offset >= tr->len) {
		if (usb_bulk_read(tr->handle, USB_FET_IN_EP,
				(char *)tr->buf, sizeof(tr->buf),
				10000) < 0) {
			pr_error("rf2500: can't receive data");
			return -1;
		}

#ifdef DEBUG_USBTR
		debug_hexdump("USB transfer in", tr->buf, 64);
#endif

		tr->len = tr->buf[1] + 2;
		if (tr->len > sizeof(tr->buf))
			tr->len = sizeof(tr->buf);
		tr->offset = 2;
	}

	rlen = tr->len - tr->offset;
	if (rlen > max_len)
		rlen = max_len;
	memcpy(databuf, tr->buf + tr->offset, rlen);
	tr->offset += rlen;

	return rlen;
}
Exemplo n.º 25
0
grub_usb_err_t
grub_usb_bulk_read (grub_usb_device_t dev,
		    int endpoint, grub_size_t size, char *data)
{
  usb_dev_handle *devh;
  struct usb_device *d = dev->data;

  devh = usb_open (d);
  if (usb_claim_interface (devh, 0) < 1)
    {
      usb_close (devh);
      return GRUB_USB_ERR_STALL;
    }

  if (usb_bulk_read (devh, endpoint, data, size, 20) < 1)
    {
      usb_close (devh);
      return GRUB_USB_ERR_STALL;
    }

  usb_release_interface (devh, 0);
  usb_close (devh);

  return GRUB_USB_ERR_NONE;
}
Exemplo n.º 26
0
int camera_bulk_read(usb_dev_handle *handle, char *buffer, int length)
{
  int bytes_read = 0, read_chunk, actually_read;

  if(verbose)
    fprintf(stderr, "CAMERA BULK TOTAL READ %x\n", length);

  bytes_read = 0;
  do {
    if(length - bytes_read > 0x5000)
      read_chunk = 0x5000;
    else if(length - bytes_read > 0x40)
      read_chunk = (length - bytes_read) / 0x40 * 0x40;
    else
      read_chunk = length - bytes_read;
    if(verbose)
      fprintf(stderr, "CAMERA BULK SUB READ %x\n", read_chunk);
    actually_read = usb_bulk_read(handle, CAMERA_BULK_READ_EP, 
				  buffer + bytes_read, 
				  read_chunk, USB_TIMEOUT);
    if(actually_read != read_chunk)
      return -1;
    bytes_read += read_chunk;
  } while(bytes_read < length);
  return length;
}
Exemplo n.º 27
0
	/// \brief Multi packet bulk read from the oscilloscope.
	/// \param data Buffer for the sent/recieved data.
	/// \param length The length of data contained in the packets.
	/// \param attempts The number of attempts, that are done on timeouts.
	/// \return Number of received bytes on success, libusb error code on error.
	int Device::bulkReadMulti(unsigned char *data, unsigned int length, int attempts) {
		if(!this->handle)
			return LIBUSB_ERROR_NO_DEVICE;
		
		int errorCode = 0;
		
		errorCode = this->getConnectionSpeed();
		if(errorCode < 0)
			return errorCode;
		
		errorCode = this->inPacketLength;
		unsigned int packet, received = 0;
		for(packet = 0; received < length && errorCode == this->inPacketLength; ++packet) {
#if LIBUSB_VERSION == 0
			errorCode = LIBUSB_ERROR_TIMEOUT;
			for(int attempt = 0; (attempt < attempts || attempts == -1) && errorCode == LIBUSB_ERROR_TIMEOUT; ++attempt)
				errorCode = usb_bulk_read(this->handle, HANTEK_EP_IN, (char *) data + packet * this->inPacketLength, qMin(length - received, (unsigned int) this->inPacketLength), HANTEK_TIMEOUT);
#else
			errorCode = this->bulkTransfer(HANTEK_EP_IN, data + packet * this->inPacketLength, qMin(length - received, (unsigned int) this->inPacketLength), attempts, HANTEK_TIMEOUT_MULTI);
#endif
			if(errorCode > 0)
				received += errorCode;
		}
		
		if(received > 0)
			return received;
		else
			return errorCode;
	}
Exemplo n.º 28
0
int lothar_usb_read(void *connection, uint8_t *data, size_t len)
{
  int status = usb_bulk_read((usb_dev_handle *)connection, USB_ENDPOINT_IN | 2, (char *)data, len, 0);
  if(status < 0)
    LOTHAR_RETURN_ERROR(LOTHAR_ERROR_NXT_READ_ERROR);
  return status;
}
Exemplo n.º 29
0
int write_cmd(usb_dev_handle *udev, unsigned char *cmd, int read)
{
	int ret=0, i=0;

	unsigned char receive_data[1024];
	unsigned char interrupt_data;
	for (i=0; i<1024; i++)
		receive_data[i] = 0x00;

	printf("write_cmd\n");
	ret = usb_bulk_write(udev, 0x02, cmd, sizeof(cmd), 10000);
	if (ret != sizeof(cmd))
		printf("Error!\n");

	//if (read)
	{
		ret = usb_bulk_read(udev, 0x81, receive_data, sizeof(receive_data), 10000);
		mcrw_debug(receive_data, ret);
	}
	//else
	{
		ret = usb_interrupt_read(udev, 0x83, &interrupt_data, 1, 10000);
		mcrw_debug(&interrupt_data, ret);
	}
	return 0;
}
Exemplo n.º 30
0
bool MotorController::sendSyncronousFrame(string command, string payload) {
    char InputPacketBuffer[64] = {0}; //Allocate a memory buffer for the data which we will read from the USB device
        
    bool result = sendCommandWithPayload(command, payload);
	
	
	const char *commandChar = NULL;
	commandChar = command.c_str();
	int commandInt = (int)commandChar[0];
    if(result) printf ("command [%i] was sent successfully\n",commandInt);
    else cout << "failed to send with payload" << endl;
    
    wchar_t *tempReceiveChar = (wchar_t*)malloc(255);
    usb_bulk_read(usbDeviceHandle, 0x81, &InputPacketBuffer[0], 64, 200);
    tempReceiveChar[1+InputPacketBuffer[1]] = 0;
    
    if (InputPacketBuffer != 0) {
		cout << "Frame returned data:" <<endl;
        for (int i = 0; i < 2+InputPacketBuffer[1];i++) { 
            tempReceiveChar[i] = InputPacketBuffer[i];
			printf ("Byte in #%i: [%i] \n",i,InputPacketBuffer[i]);
        }
    }

    return (tempReceiveChar[0] == 1);
}