#include "stdafx.h"
#include "USB_Com.h"


static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen){

	char buffer[256];
	int rval, i;

	// make standard request GET_DESCRIPTOR, type string and given index 
	// (e.g. dev->iProduct)
	rval = usb_control_msg(dev, USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, 
		buffer, sizeof(buffer), 1000);

	if(rval < 0) // error
		return rval;

	// rval should be bytes read, but buffer[0] contains the actual response size
	if((unsigned char)buffer[0] < rval)
		rval = (unsigned char)buffer[0]; // string is shorter than bytes read

	if(buffer[1] != USB_DT_STRING) // second byte is the data type
		return 0; // invalid return type

	// we're dealing with UTF-16LE here so actual chars is half of rval,
	// and index 0 doesn't count
	rval /= 2;

	/* lossy conversion to ISO Latin1 */
	for(i = 1; i < rval && i < buflen; i++) {
		if(buffer[2 * i + 1] == 0)
			buf[i-1] = buffer[2 * i];
		else
			buf[i-1] = '?'; /* outside of ISO Latin1 range */
	}
	buf[i-1] = 0;

	return i-1;
}

static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, int product, char *productName) {

	struct usb_bus *bus;
	struct usb_device *dev;
	char devVendor[256], devProduct[256];

	usb_dev_handle * handle = NULL;

	usb_init();
	usb_find_busses();
	usb_find_devices();

	for(bus=usb_get_busses(); bus; bus=bus->next) {
		for(dev=bus->devices; dev; dev=dev->next) {                     
			if(dev->descriptor.idVendor != vendor ||
				dev->descriptor.idProduct != product)
				continue;

			/* we need to open the device in order to query strings */
			if(!(handle = usb_open(dev))) {
				fprintf(stderr, "Warning: cannot open USB device: %s\n",
					usb_strerror());
				continue;
			}

			/* get vendor name */
			if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 
				0x0409, devVendor, sizeof(devVendor)) < 0) {
					fprintf(stderr, 
						"Warning: cannot query manufacturer for device: %s\n", 
						usb_strerror());
					usb_close(handle);
					continue;
			}

			/* get product name */
			if(usbGetDescriptorString(handle, dev->descriptor.iProduct, 
				0x0409, devProduct, sizeof(devVendor)) < 0) {
					fprintf(stderr, 
						"Warning: cannot query product for device: %s\n", 
						usb_strerror());
					usb_close(handle);
					continue;
			}

			if(strcmp(devVendor, vendorName) == 0 && 
				strcmp(devProduct, productName) == 0)
				return handle;
			else
				usb_close(handle);
		}
	}

	return NULL;
}

usb_dev_handle * usbopenMyDevice(){
	usb_dev_handle *pomoc = NULL;
	pomoc = usbOpenDevice(0x17C0, "MDbakalp", 0x06DC, "USBlinks");
	if(pomoc != NULL)
		return pomoc;
	return NULL;
}
int usbSendMyMsg(usb_dev_handle *handle, int cmd, unsigned char param1[2],unsigned char param2[2], char* buffer){
	int nBytes = sizeof(buffer);
	UINT8 aux[2]={0};
	char aux2[16];
	unsigned int msg[2]={0,0};
	usb_dev_handle *handletemp = NULL;
	if(param1==0){
		param1 = aux;
		param2 = aux;
	}
	if(buffer==NULL)
		buffer= aux2;

	
	msg[0] = param1[0]+(param1[1]<<8);
	msg[1] = param2[0]+(param2[1]<<8); 
	//msg[0] = (2<<8)+1;
	handletemp = usbopenMyDevice();
	if(handletemp == NULL)
		return -1;
 	switch(cmd){
	case USB_LED_OFF:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
            USB_LED_OFF, 0, 0, (char *)aux2, sizeof(aux2), 5000);
		break;
	case USB_LED_ON:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
            USB_LED_ON, 0, 0, (char *)aux2, sizeof(aux2), 5000);
		break;
	case USB_DPOT_PERM:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
            USB_DPOT_PERM,
			msg[0], 
			msg[1],
			(char *)aux2, sizeof(aux2), 5000);
		break;
	case USB_DPOT_TEMP:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, 
            USB_DPOT_TEMP, 
			0, 
			0,
			(char *)buffer, 7, 5000);
		break;
		case USB_DPOT_RESET:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, 
            USB_DPOT_RESET, 
			0, 
			0,
			(char *)aux2, sizeof(aux2), 5000);
		break;
	case USB_SEMAF_MODE:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
            USB_SEMAF_MODE, 
			msg[0], 
			0, 
			(char *)aux2, sizeof(aux2), 5000);
		break;
	case USB_SEMAF_STATE:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, 
            USB_SEMAF_STATE, 
			0,
			0,
			(char *)buffer, sizeof(buffer)+1, 5000);
		break;
	case USB_FB:
		nBytes = usb_control_msg(handletemp, 
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
            USB_FB, 
			0, 
			0,
			(char *)aux2, sizeof(aux2), 5000);
			nBytes = aux2[0];
			nBytes = aux2[1];
			nBytes = aux2[2];
			nBytes = aux2[3];
			nBytes = aux2[4];
			nBytes = aux2[5];
			nBytes = aux2[6];
			nBytes = aux2[7];


		break;
	default:
		break;
	}
	usb_close(handletemp);
return nBytes;
}
