Skip to content

Commit

Permalink
fix: more code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
CooperW824 committed Aug 15, 2021
1 parent fc602c2 commit e51252e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
49 changes: 22 additions & 27 deletions src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,19 @@ QSharedPointer<ParameterDelegate> UsbDevice::exportParameterDelegate()
* @brief initializes libusb session to params.ctx, gets the device, gets the config descriptor, gets the relevant information,
* gets the endpoint addr, and sets the device handle returns an int depending on success of the initialization.
*
* @param params the usbParams struct that contains all the necessary data for libusb to read from the device properly
* @param params the UsbParams struct that contains all the necessary data for libusb to read from the device properly
*
* @return int - returns 0 on success and returns a negative number on a failure or error, -1 on libusb init failure,
* -2 on libusb device handle no mem failure, -3 on libusb handle no access failure, -4 on device handle no device failure.
*/

//create parameters structure to handle variables
struct usbParams UsbDevice::setupLibusb(usbParams params)
void UsbDevice::setupLibusb(UsbParams &params)
{
int r = libusb_init(&params.ctx); //try initializing libusb
if (r < 0)
{ //checking for errors, if r < 0 then there is an error
params.errorCode = r;
return params;
}
libusb_get_device_list(params.ctx, &params.devs);
params.dev = params.devs[params.deviceNum];
Expand All @@ -165,25 +164,23 @@ struct usbParams UsbDevice::setupLibusb(usbParams params)
r = libusb_open(params.dev, &params.handle);

params.errorCode = r;
return params;
}

/**
* @brief closes all the libusb variables and exits the libusb session after reading all needed data or encountering an error.
*
* @param closeDevice is there a device handle initialized to be able to close, not always true as if there
* is an error initializing a device handle.
* @param params a usbParams struct that handles all the parameters that need to be passed for libusb to close properly
* @param params a UsbParams struct that handles all the parameters that need to be passed for libusb to close properly
*/
struct usbParams UsbDevice::exitLibusb(bool closeDevice, usbParams params)
void UsbDevice::exitLibusb(bool closeDevice, UsbParams &params)
{
if (closeDevice)
{ //check if there is a device handle to close
libusb_close(params.handle);
}
libusb_free_config_descriptor(params.config);
libusb_exit(params.ctx);
return params;
}

/**
Expand All @@ -206,7 +203,7 @@ QSharedPointer<ImportResult> UsbDevice::importBits(const Parameters &parameters,
}

//setting up our struct
usbParams params;
UsbParams params;
//getting parameters
params.deviceNum = parameters.value("DeviceNum").toInt();
params.interfaceNum = parameters.value("InterfaceNum").toInt();
Expand All @@ -227,19 +224,16 @@ QSharedPointer<ImportResult> UsbDevice::importBits(const Parameters &parameters,
bool attach;

//determine transfer type, as some transfers we can't handle yet.
switch (transferType)
{
case 0:

if (transferType == 0){
return ImportResult::error("Control Transfer Endpoints Not Supported");
break;

case 1:
}
else if(transferType == 1){
return ImportResult::error("Isochronous Transfer Endpoints Not Supported");
break;

case 2:
}
else if(transferType == 2){
transferTypeStr += ", Bulk Transfer"; //for setting metadata
params = setupLibusb(params); //try to init libusb
setupLibusb(params); //try to init libusb
if (params.errorCode < 0)
{
return returnError(params.errorCode);
Expand Down Expand Up @@ -294,9 +288,9 @@ QSharedPointer<ImportResult> UsbDevice::importBits(const Parameters &parameters,
progress->setProgress(i, transferNum); //update the progressbar
std::this_thread::sleep_for(std::chrono::milliseconds(transferDelay)); //wait for the durration of the transfer delay
}
params = exitLibusb(true, params); //exit libusb with closing the device
break;
case 3:
exitLibusb(true, params); //exit libusb with closing the device
}
else if (transferType == 3){
/**
* This is the same thing as the bulk transfer implementation, but instead with an interrup transfer, only 2 lines of
* code are different, these lines replace bulk transfer with interrupt transfer.
Expand All @@ -305,7 +299,7 @@ QSharedPointer<ImportResult> UsbDevice::importBits(const Parameters &parameters,
*/

transferTypeStr += ", Interrupt Transfer";
params = setupLibusb(params);
setupLibusb(params);

if (params.errorCode < 0)
{
Expand Down Expand Up @@ -360,12 +354,13 @@ QSharedPointer<ImportResult> UsbDevice::importBits(const Parameters &parameters,
progress->setProgress(i, transferNum);
std::this_thread::sleep_for(std::chrono::milliseconds(transferDelay));
}
params = exitLibusb(true, params);
break;

default:
break;
exitLibusb(true, params);
}
else
{
return ImportResult::error("Invalid Transfer Type Error, please reselect settings and try again.");
}

QSharedPointer<BitContainer> container = BitContainer::create(largeBuffer); //creating a bit container with all the data in it
container->setName("USB " + deviceName); //esetting the name you see on the side of hobbits
QSharedPointer<BitInfo> info = BitInfo::create(container->bits()->sizeInBits()); //creating a bit infor for setting frames
Expand Down
56 changes: 29 additions & 27 deletions src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@
#include "parameterdelegate.h"
#include <libusb-1.0/libusb.h>

typedef struct UsbParams
{
//the pointer to the libusb device selected
libusb_device *dev;
//the pointer to the device list generated
libusb_device **devs;
//the pointer to the configuration descriptor generated
libusb_config_descriptor *config;
// the pointer to the libusb context for the active libusb session
libusb_context *ctx;
//the handle of the device used for transfers and device interactions
libusb_device_handle *handle;
//the number of the device selected
int deviceNum;
//the number of the interface selected
int interfaceNum;
//the number of the alternate setting selected
int altSetNum;
//number of the endpoint selected
int endpointNum;
//the address of the endpoint selected
unsigned char endpoint;
//any possible error codes passed
int errorCode;
}StructName;

class UsbDevice : public QObject, ImporterExporterInterface
{
Q_OBJECT
Expand Down Expand Up @@ -33,38 +59,14 @@ class UsbDevice : public QObject, ImporterExporterInterface
const Parameters &parameters,
QSharedPointer<PluginActionProgress> progress) override;

struct usbParams setupLibusb(usbParams params);
void setupLibusb(UsbParams &params);
QSharedPointer<ImportResult> returnError(int errorCode);
struct usbParams exitLibusb(bool closeDevice, usbParams params);
void exitLibusb(bool closeDevice, UsbParams &params);

private:
QSharedPointer<ParameterDelegate> m_importDelegate;
QSharedPointer<ParameterDelegate> m_exportDelegate;

};

struct usbParams
{
//the pointer to the libusb device selected
libusb_device *dev;
//the pointer to the device list generated
libusb_device **devs;
//the pointer to the configuration descriptor generated
libusb_config_descriptor *config;
// the pointer to the libusb context for the active libusb session
libusb_context *ctx;
//the handle of the device used for transfers and device interactions
libusb_device_handle *handle;
//the number of the device selected
int deviceNum;
//the number of the interface selected
int interfaceNum;
//the number of the alternate setting selected
int altSetNum;
//number of the endpoint selected
int endpointNum;
//the address of the endpoint selected
unsigned char endpoint;
//any possible error codes passed
int errorCode;
};

Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ public slots:
int m_cnt;
// the string descriptor of the device selected
QString m_device;
};
};

0 comments on commit e51252e

Please sign in to comment.