00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "LOW_portUsbDevice_Linux.h"
00019
00020
00021 #include "usb.h"
00022
00023
00024
00025
00026
00027
00028
00029 int LOW_portUsbDevice_Linux::initHelper = initialize();
00030 int LOW_portUsbDevice_Linux::initialize()
00031 {
00032
00033 usb_init();
00034
00035
00036
00037
00038 return 0;
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048 LOW_portUsbDevice_Linux::LOW_portUsbDevice_Linux( const LOW_portUsb_Factory::usbDeviceSpecifier_t inUsbDevSpec)
00049 {
00050 rescanBusses();
00051
00052 usbLibDevice = 0;
00053 for ( struct usb_bus *bus = usb_busses; bus; bus = bus->next) {
00054 for ( struct usb_device *dev = bus->devices; dev; dev = dev->next) {
00055 std::string tmpStr = std::string( bus->dirname);
00056 tmpStr += "/";
00057 tmpStr += dev->filename;
00058 if ( inUsbDevSpec == tmpStr ) {
00059 usbLibDevice = dev;
00060 break;
00061 }
00062 }
00063 }
00064
00065 if ( usbLibDevice == 0 )
00066 throw noSuchDevice_error( "specified device not found", __FILE__, __LINE__);
00067
00068 usbLibDevHdl = usb_open( usbLibDevice);
00069 if ( usbLibDevHdl == 0 )
00070 throw portUsbDevice_error( "error calling usb_open(): "+libUsbErrMsg(), __FILE__, __LINE__);
00071
00072 }
00073
00074
00075 LOW_portUsbDevice_Linux::LOW_portUsbDevice_Linux( const usbVendorID_t inVendorID, const usbProductID_t inProductID)
00076 {
00077 rescanBusses();
00078
00079 usbLibDevice = 0;
00080 for ( struct usb_bus *bus = usb_busses; bus; bus = bus->next) {
00081 for ( struct usb_device *dev = bus->devices; dev; dev = dev->next) {
00082 if ( dev->descriptor.idVendor==inVendorID && dev->descriptor.idProduct==inProductID ) {
00083 usbLibDevice = dev;
00084 break;
00085 }
00086 }
00087 }
00088
00089 if ( usbLibDevice == 0 )
00090 throw noSuchDevice_error( "no device with specified vendor/product ID found", __FILE__, __LINE__);
00091
00092 usbLibDevHdl = usb_open( usbLibDevice);
00093 if ( usbLibDevHdl == 0 )
00094 throw portUsbDevice_error( "error calling usb_open(): "+libUsbErrMsg(), __FILE__, __LINE__);
00095
00096 }
00097
00098
00099 LOW_portUsbDevice_Linux::~LOW_portUsbDevice_Linux()
00100 {
00101 int errVal = usb_close( usbLibDevHdl);
00102 if ( errVal != 0 )
00103 LOW_helper_msglog::printPerror( errVal, "~LOW_portUsbDevice_Linux: error calling usb_close(): %s", libUsbErrMsg().c_str());
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 LOW_portUsbDevice_Linux::usbVendorID_t LOW_portUsbDevice_Linux::getVendorID()
00115 {
00116 __LOW_SYNCHRONIZE_METHOD_READ__
00117
00118 return usbLibDevice->descriptor.idVendor;
00119 }
00120
00121
00122 LOW_portUsbDevice_Linux::usbProductID_t LOW_portUsbDevice_Linux::getProductID()
00123 {
00124 __LOW_SYNCHRONIZE_METHOD_READ__
00125
00126 return usbLibDevice->descriptor.idProduct;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 void LOW_portUsbDevice_Linux::setConfiguration( const usbConfig_t inConfig)
00148 {
00149 __LOW_SYNCHRONIZE_METHOD_WRITE__
00150
00151 int errVal = usb_set_configuration( usbLibDevHdl, inConfig);
00152 if ( errVal != 0 )
00153 throw portUsbDevice_error( errVal, "error calling usb_set_configuration(): "+libUsbErrMsg(), __FILE__, __LINE__);
00154 }
00155
00156
00157
00158
00159 void LOW_portUsbDevice_Linux::claimInterface( const usbInterface_t inInterface)
00160 {
00161 __LOW_SYNCHRONIZE_METHOD_WRITE__
00162
00163 int errVal = usb_claim_interface( usbLibDevHdl, inInterface);
00164
00165 if ( errVal != 0 )
00166 throw portUsbDevice_error( errVal, "error calling usb_claim_interface(): "+libUsbErrMsg(), __FILE__, __LINE__);
00167 }
00168
00169
00170 void LOW_portUsbDevice_Linux::releaseInterface( const usbInterface_t inInterface)
00171 {
00172 __LOW_SYNCHRONIZE_METHOD_WRITE__
00173
00174 int errVal = usb_release_interface( usbLibDevHdl, inInterface);
00175 if ( errVal != 0 )
00176 throw portUsbDevice_error( errVal, "error calling usb_release_interface(): "+libUsbErrMsg(), __FILE__, __LINE__);
00177 }
00178
00179
00180 void LOW_portUsbDevice_Linux::setIfaceAltSetting( const usbSetting_t inAltSetting)
00181 {
00182 __LOW_SYNCHRONIZE_METHOD_WRITE__
00183
00184 int errVal = usb_set_altinterface( usbLibDevHdl, inAltSetting);
00185 if ( errVal != 0 )
00186 throw portUsbDevice_error( errVal, "error calling usb_set_altinterface(): "+libUsbErrMsg(), __FILE__, __LINE__);
00187 }
00188
00189
00190
00191
00192 void LOW_portUsbDevice_Linux::controlMsg( const bmRequestType_t inReqType,
00193 const bRequest_t inRequest,
00194 const wValue_t inValue,
00195 const wIndex_t inIndex,
00196 const wLength_t inLength,
00197 msgData_t inOutData,
00198 const usbTimeout_t inTimeout)
00199 {
00200 __LOW_SYNCHRONIZE_METHOD_WRITE__
00201
00202 int errVal = usb_control_msg( usbLibDevHdl, inReqType, inRequest, inValue, inIndex, reinterpret_cast<char*>(inOutData), inLength, inTimeout);
00203 if ( errVal != 0 )
00204 throw portUsbDevice_error( errVal, "error calling usb_control_msg(): "+libUsbErrMsg(), __FILE__, __LINE__);
00205 }
00206
00207
00208 void LOW_portUsbDevice_Linux::controlMsg( const bmRequestType_t inReqType,
00209 const bRequest_t inRequest,
00210 const wValue_t inValue,
00211 const wIndex_t inIndex,
00212 byteVec_t &inOutData,
00213 const usbTimeout_t inTimeout)
00214 {
00215 __LOW_SYNCHRONIZE_METHOD_WRITE__
00216
00217 msgData_t buffer = new uint8_t[inOutData.size()];
00218 try {
00219 std::copy( inOutData.begin(), inOutData.end(), buffer );
00220 controlMsg( inReqType, inRequest, inValue, inIndex, inOutData.size(), buffer, inTimeout);
00221 std::copy( buffer, buffer+inOutData.size(), inOutData.begin());
00222 }
00223 catch( ... ) {
00224 delete[] buffer;
00225 throw;
00226 }
00227 delete[] buffer;
00228 }
00229
00230
00231
00232
00233 void LOW_portUsbDevice_Linux::clearHalt( const usbEndpoint_t inEP)
00234 {
00235 __LOW_SYNCHRONIZE_METHOD_WRITE__
00236
00237 int errVal = usb_clear_halt( usbLibDevHdl, inEP);
00238 if ( errVal != 0 )
00239 throw portUsbDevice_error( errVal, "error calling usb_clear_halt(): "+libUsbErrMsg(), __FILE__, __LINE__);
00240 }
00241
00242
00243
00244
00245 unsigned int LOW_portUsbDevice_Linux::bulkWrite( const usbEndpoint_t inEP, const wLength_t inLength,
00246 const msgData_t inData, const usbTimeout_t inTimeout)
00247 {
00248 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00249
00250 int transferBytes = usb_bulk_write( usbLibDevHdl, inEP, reinterpret_cast<char*>(inData), inLength, inTimeout);
00251 if ( transferBytes <= 0 )
00252 throw portUsbDevice_error( "error calling usb_bulk_write(): "+libUsbErrMsg(), __FILE__, __LINE__);
00253 return transferBytes;
00254 }
00255
00256
00257 unsigned int LOW_portUsbDevice_Linux::bulkWrite( const usbEndpoint_t inEP,
00258 const byteVec_t &inData, const usbTimeout_t inTimeout)
00259 {
00260 __LOW_SYNCHRONIZE_METHOD_WRITE__
00261
00262 unsigned int transferBytes = 0;
00263 msgData_t buffer = new uint8_t[inData.size()];
00264 try {
00265 std::copy( inData.begin(), inData.end(), buffer );
00266 transferBytes = bulkWrite( inEP, inData.size(), buffer, inTimeout);
00267 }
00268 catch( ... ) {
00269 delete[] buffer;
00270 throw;
00271 }
00272 delete[] buffer;
00273 return transferBytes;
00274 }
00275
00276
00277 unsigned int LOW_portUsbDevice_Linux::bulkRead( const usbEndpoint_t inEP, const wLength_t inLength,
00278 msgData_t outData, const usbTimeout_t inTimeout)
00279 {
00280 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00281
00282 int transferBytes = usb_bulk_read( usbLibDevHdl, inEP, reinterpret_cast<char*>(outData), inLength, inTimeout);
00283 if ( transferBytes <= 0 )
00284 throw portUsbDevice_error( "error calling usb_bulk_read(): "+libUsbErrMsg(), __FILE__, __LINE__);
00285 return transferBytes;
00286 }
00287
00288
00289 unsigned int LOW_portUsbDevice_Linux::bulkRead( const usbEndpoint_t inEP,
00290 byteVec_t &outData, const usbTimeout_t inTimeout)
00291 {
00292 __LOW_SYNCHRONIZE_METHOD_WRITE__
00293
00294 unsigned int transferBytes = 0;
00295 msgData_t buffer = new uint8_t[outData.size()];
00296 try {
00297 transferBytes = bulkRead( inEP, outData.size(), buffer, inTimeout);
00298 std::copy( buffer, buffer+transferBytes, outData.begin());
00299 }
00300 catch( ... ) {
00301 delete[] buffer;
00302 throw;
00303 }
00304 delete[] buffer;
00305 return transferBytes;
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 void LOW_portUsbDevice_Linux::rescanBusses()
00317 {
00318
00319 usb_find_busses();
00320
00321
00322 usb_find_devices();
00323 }
00324
00325
00326 std::string LOW_portUsbDevice_Linux::libUsbErrMsg()
00327 {
00328 std::string tmpStr = std::string( usb_strerror());
00329 return tmpStr;
00330 }
00331
00332
00333