Changeset 32c7682 in thomson for elec/CrO2/software/device.cpp
- Timestamp:
- Jan 28, 2012, 3:13:17 PM (12 years ago)
- Branches:
- main
- Children:
- c7b4218
- Parents:
- 96bc8fa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
elec/CrO2/software/device.cpp
r96bc8fa r32c7682 9 9 #include "device.h" 10 10 11 #include <iostream> // TODO remove12 13 11 bool Device::initOnce = false; 12 Device* Device::instance = NULL; 14 13 15 14 const uint32_t Device::vid = 0x16C0; … … 24 23 #define PSCMD_STATUS 3 25 24 26 /* USB device lookup by VID and PID, then Vendor and Product strings, as we use V-USB shared ID. */ 27 static int usbGetStringAscii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen) 25 26 // Gets the device instance. Throws an error message if something bad happens. 27 Device& Device::getDevice() throw(const char*) 28 { 29 if (instance == NULL) 30 instance = new Device(); 31 32 return *instance; 33 } 34 35 36 /* USB device lookup by VID and PID, then Vendor and Product strings, as we use 37 * V-USB shared ID. */ 38 static int usbGetStringAscii(usb_dev_handle *dev, int index, int langid, 39 char *buf, int buflen) 28 40 { 29 41 char buffer[256]; 30 42 int rval, i; 31 43 32 if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0) 44 if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, 45 (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0) 33 46 return rval; 34 47 if(buffer[1] != USB_DT_STRING) … … 49 62 } 50 63 51 Device::Device() 64 Device::Device() throw(const char*) 52 65 { 53 66 handle = NULL; … … 69 82 continue; 70 83 71 // Found device with correct VID and PID. Now try to match the strings 84 // Found device with correct VID and PID. Now try to match the 85 // vendor and product strings 72 86 char string[256]; 73 87 int len; 74 handle = usb_open(dev); /* we need to open the device in order to query strings */ 88 handle = usb_open(dev); 89 /* we need to open the device in order to query strings */ 75 90 if(!handle){ 76 std::cerr << "Warning: cannot open USB device: " << usb_strerror() << std::endl;77 91 continue; 78 92 } 79 93 /* now check whether the names match: */ 80 len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); 81 if(len < 0) 82 { 83 std::cerr << "Warning: cannot query manufacturer for device: " << usb_strerror() << std::endl; 84 } 85 else 94 len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 95 0x0409, string, sizeof(string)); 96 if(len >= 0) 86 97 { 87 98 if(strcmp(string, vendor) == 0){ 88 len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); 89 if(len < 0){ 90 std::cerr << "Warning: cannot query product for device: " << usb_strerror() << std::endl; 91 }else{ 99 len = usbGetStringAscii(handle, dev->descriptor.iProduct, 100 0x0409, string, sizeof(string)); 101 if(len >= 0){ 92 102 if(strcmp(string, product) == 0) 93 103 break; … … 104 114 // At this point, either we have found a device and handle is pointing to it, 105 115 // or we failed and handle is NULL. 106 //107 // TODO : use exceptions for error handling; instead of fprintf (not useable in GUI mode).108 116 if (!handle) 109 std::cerr << "Could not find USB device \"" << product << "\" with vid=0x" << std::hex << vid << " pid=0x" << pid << std::endl;117 throw "Device not found. Is the USB cable plugged correctly?"; 110 118 } 111 119 … … 119 127 int Device::read(uint8_t* buffer, size_t max) 120 128 { 121 return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0,0, (char*)buffer, max, 5000); 129 return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 130 PSCMD_GET, 0,0, (char*)buffer, max, 5000); 122 131 123 132 } … … 127 136 { 128 137 int rqtype = (size == 0) ? USB_ENDPOINT_IN:USB_ENDPOINT_OUT; 129 return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | rqtype, PSCMD_PUT, blktype,0 /*checksum*/, (char*)buffer, size, 5000); 138 return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | rqtype, 139 PSCMD_PUT, blktype,0 /*checksum*/, (char*)buffer, size, 5000); 130 140 131 141 } … … 134 144 { 135 145 uint8_t status; 136 usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_STATUS, 0,0, (char*)&status, 1, 5000); 146 usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 147 PSCMD_STATUS, 0,0, (char*)&status, 1, 5000); 137 148 // TODO handle errors (return value) 138 149 return status;
Note:
See TracChangeset
for help on using the changeset viewer.