Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

LOW_portUsbDevice_Linux.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           LOW_portUsbDevice_Linux.cpp  -  description
00003                              -------------------
00004     begin                : Sun Oct 12 2003
00005     copyright            : (C) 2003 by Harald Roelle
00006     email                : roelle@informatik.uni-muenchen.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "LOW_portUsbDevice_Linux.h"
00019 
00020 
00021 #include "usb.h"
00022 
00023 
00024 //=====================================================================================
00025 //
00026 // static initializer
00027 //
00028 
00029 int LOW_portUsbDevice_Linux::initHelper = initialize();
00030 int LOW_portUsbDevice_Linux::initialize()
00031 {
00032   // init usb library
00033   usb_init();
00034 
00035   // set debugging
00036   //usb_set_debug(int level);
00037 
00038   return 0;
00039 }
00040 
00041 
00042 
00043 //=====================================================================================
00044 //
00045 // constructors
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 // public methods
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 //  /** Resets a device.
00132 //      Resets the specified device by sending a RESET down the port it is connected to.
00133 //
00134 //   */
00135 //LOW_portUsbDevice_Linux::reset()
00136 //{
00137 //  // Causes re-enumeration: After calling usb_reset, the device will need to
00138 //  // re-enumerate and thusly, requires you to find the new device and open a
00139 //  // new handle. The handle used to call usb_reset will no longer work.
00140 //  int errVal = usb_reset( usbLibDevHdl);
00141 //  if ( errVal != 0 )
00142 //    throw portUsbDevice_error( errVal, "error calling usb_reset()", __FILE__, __LINE__);
00143 //  XXXXXXXXXXXXX
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 // private methods
00314 //
00315 
00316 void LOW_portUsbDevice_Linux::rescanBusses()
00317 {
00318   // find all/new busses
00319   usb_find_busses();
00320 
00321   // find all/new devices
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 

Generated on Mon Oct 27 22:56:09 2003 by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001