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()
00076 {
00077 int errVal = usb_close( usbLibDevHdl);
00078 if ( errVal != 0 )
00079 LOW_helper_msglog::printPerror( errVal, "~LOW_portUsbDevice_Linux: error calling usb_close(): %s", libUsbErrMsg().c_str());
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 LOW_portUsbDevice_Linux::usbVendorID_t LOW_portUsbDevice_Linux::getVendorID()
00091 {
00092 __LOW_SYNCHRONIZE_METHOD_READ__
00093
00094 return usbLibDevice->descriptor.idVendor;
00095 }
00096
00097
00098 LOW_portUsbDevice_Linux::usbProductID_t LOW_portUsbDevice_Linux::getProductID()
00099 {
00100 __LOW_SYNCHRONIZE_METHOD_READ__
00101
00102 return usbLibDevice->descriptor.idProduct;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 void LOW_portUsbDevice_Linux::setConfiguration( const usbConfig_t inConfig)
00124 {
00125 __LOW_SYNCHRONIZE_METHOD_WRITE__
00126
00127 int errVal = usb_set_configuration( usbLibDevHdl, inConfig);
00128 if ( errVal != 0 )
00129 throw portUsbDevice_error( errVal, "error calling usb_set_configuration(): "+libUsbErrMsg(), __FILE__, __LINE__);
00130 }
00131
00132
00133
00134
00135 void LOW_portUsbDevice_Linux::claimInterface( const usbInterface_t inInterface)
00136 {
00137 __LOW_SYNCHRONIZE_METHOD_WRITE__
00138
00139 int errVal = usb_claim_interface( usbLibDevHdl, inInterface);
00140
00141 if ( errVal != 0 )
00142 throw portUsbDevice_error( errVal, "error calling usb_claim_interface(): "+libUsbErrMsg(), __FILE__, __LINE__);
00143 }
00144
00145
00146 void LOW_portUsbDevice_Linux::releaseInterface( const usbInterface_t inInterface)
00147 {
00148 __LOW_SYNCHRONIZE_METHOD_WRITE__
00149
00150 int errVal = usb_release_interface( usbLibDevHdl, inInterface);
00151 if ( errVal != 0 )
00152 throw portUsbDevice_error( errVal, "error calling usb_release_interface(): "+libUsbErrMsg(), __FILE__, __LINE__);
00153 }
00154
00155
00156 void LOW_portUsbDevice_Linux::setIfaceAltSetting( const usbSetting_t inAltSetting)
00157 {
00158 __LOW_SYNCHRONIZE_METHOD_WRITE__
00159
00160 int errVal = usb_set_altinterface( usbLibDevHdl, inAltSetting);
00161 if ( errVal != 0 )
00162 throw portUsbDevice_error( errVal, "error calling usb_set_altinterface(): "+libUsbErrMsg(), __FILE__, __LINE__);
00163 }
00164
00165
00166
00167
00168 void LOW_portUsbDevice_Linux::controlMsg( const bmRequestType_t inReqType,
00169 const bRequest_t inRequest,
00170 const wValue_t inValue,
00171 const wIndex_t inIndex,
00172 const wLength_t inLength,
00173 msgData_t inOutData,
00174 const usbTimeout_t inTimeout)
00175 {
00176 __LOW_SYNCHRONIZE_METHOD_WRITE__
00177
00178 int errVal = usb_control_msg( usbLibDevHdl, inReqType, inRequest, inValue, inIndex, reinterpret_cast<char*>(inOutData), inLength, inTimeout);
00179 if ( errVal != 0 )
00180 throw portUsbDevice_error( errVal, "error calling usb_control_msg(): "+libUsbErrMsg(), __FILE__, __LINE__);
00181 }
00182
00183
00184 void LOW_portUsbDevice_Linux::controlMsg( const bmRequestType_t inReqType,
00185 const bRequest_t inRequest,
00186 const wValue_t inValue,
00187 const wIndex_t inIndex,
00188 byteVec_t &inOutData,
00189 const usbTimeout_t inTimeout)
00190 {
00191 __LOW_SYNCHRONIZE_METHOD_WRITE__
00192
00193 msgData_t buffer = new uint8_t[inOutData.size()];
00194 try {
00195 std::copy( inOutData.begin(), inOutData.end(), buffer );
00196 controlMsg( inReqType, inRequest, inValue, inIndex, inOutData.size(), buffer, inTimeout);
00197 std::copy( buffer, buffer+inOutData.size(), inOutData.begin());
00198 }
00199 catch( ... ) {
00200 delete[] buffer;
00201 throw;
00202 }
00203 delete[] buffer;
00204 }
00205
00206
00207
00208
00209 void LOW_portUsbDevice_Linux::clearHalt( const usbEndpoint_t inEP)
00210 {
00211 __LOW_SYNCHRONIZE_METHOD_WRITE__
00212
00213 int errVal = usb_clear_halt( usbLibDevHdl, inEP);
00214 if ( errVal != 0 )
00215 throw portUsbDevice_error( errVal, "error calling usb_clear_halt(): "+libUsbErrMsg(), __FILE__, __LINE__);
00216 }
00217
00218
00219
00220
00221 unsigned int LOW_portUsbDevice_Linux::bulkWrite( const usbEndpoint_t inEP, const wLength_t inLength,
00222 const msgData_t inData, const usbTimeout_t inTimeout)
00223 {
00224 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00225
00226 int transferBytes = usb_bulk_write( usbLibDevHdl, inEP, reinterpret_cast<char*>(inData), inLength, inTimeout);
00227 if ( transferBytes <= 0 )
00228 throw portUsbDevice_error( "error calling usb_bulk_write(): "+libUsbErrMsg(), __FILE__, __LINE__);
00229 return transferBytes;
00230 }
00231
00232
00233 unsigned int LOW_portUsbDevice_Linux::bulkWrite( const usbEndpoint_t inEP,
00234 const byteVec_t &inData, const usbTimeout_t inTimeout)
00235 {
00236 __LOW_SYNCHRONIZE_METHOD_WRITE__
00237
00238 unsigned int transferBytes = 0;
00239 msgData_t buffer = new uint8_t[inData.size()];
00240 try {
00241 std::copy( inData.begin(), inData.end(), buffer );
00242 transferBytes = bulkWrite( inEP, inData.size(), buffer, inTimeout);
00243 }
00244 catch( ... ) {
00245 delete[] buffer;
00246 throw;
00247 }
00248 delete[] buffer;
00249 return transferBytes;
00250 }
00251
00252
00253 unsigned int LOW_portUsbDevice_Linux::bulkRead( const usbEndpoint_t inEP, const wLength_t inLength,
00254 msgData_t outData, const usbTimeout_t inTimeout)
00255 {
00256 __LOW_SYNCHRONIZE_METHOD_WRITE_WEAK__
00257
00258 int transferBytes = usb_bulk_read( usbLibDevHdl, inEP, reinterpret_cast<char*>(outData), inLength, inTimeout);
00259 if ( transferBytes <= 0 )
00260 throw portUsbDevice_error( "error calling usb_bulk_read(): "+libUsbErrMsg(), __FILE__, __LINE__);
00261 return transferBytes;
00262 }
00263
00264
00265 unsigned int LOW_portUsbDevice_Linux::bulkRead( const usbEndpoint_t inEP,
00266 byteVec_t &outData, const usbTimeout_t inTimeout)
00267 {
00268 __LOW_SYNCHRONIZE_METHOD_WRITE__
00269
00270 unsigned int transferBytes = 0;
00271 msgData_t buffer = new uint8_t[outData.size()];
00272 try {
00273 transferBytes = bulkRead( inEP, outData.size(), buffer, inTimeout);
00274 std::copy( buffer, buffer+transferBytes, outData.begin());
00275 }
00276 catch( ... ) {
00277 delete[] buffer;
00278 throw;
00279 }
00280 delete[] buffer;
00281 return transferBytes;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 LOW_portUsb_Factory::usbDevSpecVec_t LOW_portUsbDevice_Linux::getPortSpecifiers( const usbVendorID_t inVendorID,
00294 const usbProductID_t inProductID)
00295 {
00296 rescanBusses();
00297
00298 LOW_portUsb_Factory::usbDevSpecVec_t specifierVec;
00299
00300 for ( struct usb_bus *bus = usb_busses; bus; bus = bus->next) {
00301 for ( struct usb_device *dev = bus->devices; dev; dev = dev->next) {
00302 if ( dev->descriptor.idVendor==inVendorID && dev->descriptor.idProduct==inProductID ) {
00303 std::string specifier = std::string( bus->dirname);
00304 specifier += "/";
00305 specifier += dev->filename;
00306 specifierVec.push_back( specifier);
00307 }
00308 }
00309 }
00310
00311 return specifierVec;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 void LOW_portUsbDevice_Linux::rescanBusses()
00323 {
00324
00325 usb_find_busses();
00326
00327
00328 usb_find_devices();
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338 std::string LOW_portUsbDevice_Linux::libUsbErrMsg()
00339 {
00340 std::string tmpStr = std::string( usb_strerror());
00341 return tmpStr;
00342 }
00343
00344
00345