From e51252e189d554614281c75ff608741b8f2a6697 Mon Sep 17 00:00:00 2001 From: Cooper Werner Date: Sun, 15 Aug 2021 15:05:42 -0400 Subject: [PATCH] fix: more code quality improvements --- .../importerexporters/UsbDevice/usbdevice.cpp | 49 ++++++++-------- .../importerexporters/UsbDevice/usbdevice.h | 56 ++++++++++--------- .../UsbDevice/usbdeviceimporteditor.h | 2 +- 3 files changed, 52 insertions(+), 55 deletions(-) diff --git a/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.cpp b/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.cpp index bb023756..821acb8e 100644 --- a/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.cpp +++ b/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.cpp @@ -133,20 +133,19 @@ QSharedPointer 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 ¶ms) { int r = libusb_init(¶ms.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, ¶ms.devs); params.dev = params.devs[params.deviceNum]; @@ -165,7 +164,6 @@ struct usbParams UsbDevice::setupLibusb(usbParams params) r = libusb_open(params.dev, ¶ms.handle); params.errorCode = r; - return params; } /** @@ -173,9 +171,9 @@ struct usbParams UsbDevice::setupLibusb(usbParams params) * * @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 ¶ms) { if (closeDevice) { //check if there is a device handle to close @@ -183,7 +181,6 @@ struct usbParams UsbDevice::exitLibusb(bool closeDevice, usbParams params) } libusb_free_config_descriptor(params.config); libusb_exit(params.ctx); - return params; } /** @@ -206,7 +203,7 @@ QSharedPointer UsbDevice::importBits(const Parameters ¶meters, } //setting up our struct - usbParams params; + UsbParams params; //getting parameters params.deviceNum = parameters.value("DeviceNum").toInt(); params.interfaceNum = parameters.value("InterfaceNum").toInt(); @@ -227,19 +224,16 @@ QSharedPointer UsbDevice::importBits(const Parameters ¶meters, 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); @@ -294,9 +288,9 @@ QSharedPointer UsbDevice::importBits(const Parameters ¶meters, 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. @@ -305,7 +299,7 @@ QSharedPointer UsbDevice::importBits(const Parameters ¶meters, */ transferTypeStr += ", Interrupt Transfer"; - params = setupLibusb(params); + setupLibusb(params); if (params.errorCode < 0) { @@ -360,12 +354,13 @@ QSharedPointer UsbDevice::importBits(const Parameters ¶meters, 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 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 info = BitInfo::create(container->bits()->sizeInBits()); //creating a bit infor for setting frames diff --git a/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.h b/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.h index 84a6c511..c16e65c7 100644 --- a/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.h +++ b/src/hobbits-plugins/importerexporters/UsbDevice/usbdevice.h @@ -4,6 +4,32 @@ #include "parameterdelegate.h" #include +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 @@ -33,9 +59,9 @@ class UsbDevice : public QObject, ImporterExporterInterface const Parameters ¶meters, QSharedPointer progress) override; - struct usbParams setupLibusb(usbParams params); + void setupLibusb(UsbParams ¶ms); QSharedPointer returnError(int errorCode); - struct usbParams exitLibusb(bool closeDevice, usbParams params); + void exitLibusb(bool closeDevice, UsbParams ¶ms); private: QSharedPointer m_importDelegate; @@ -43,28 +69,4 @@ class UsbDevice : public QObject, ImporterExporterInterface }; -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; -}; + diff --git a/src/hobbits-plugins/importerexporters/UsbDevice/usbdeviceimporteditor.h b/src/hobbits-plugins/importerexporters/UsbDevice/usbdeviceimporteditor.h index 4ad0e67d..e028e7b3 100644 --- a/src/hobbits-plugins/importerexporters/UsbDevice/usbdeviceimporteditor.h +++ b/src/hobbits-plugins/importerexporters/UsbDevice/usbdeviceimporteditor.h @@ -75,4 +75,4 @@ public slots: int m_cnt; // the string descriptor of the device selected QString m_device; -}; \ No newline at end of file +};