Skip to content

Commit

Permalink
feat: UI for USB reader plugin is almost done
Browse files Browse the repository at this point in the history
+ UI is in a fully functioning state for Linux, but will not work on windows and is untested on Mac

- there are still a couple hack together things that need to be addressed  and they will be eventually, but im going to try to get full linux functionality working before i address those
  • Loading branch information
CooperW824 committed Aug 2, 2021
1 parent 7e415c7 commit 495ffb1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@ USBDeviceImportEditor::USBDeviceImportEditor(QSharedPointer<ParameterDelegate> d
// return QJsonValue(ui->spinBox->value());
// });

libusb_context *ctx;
initLibusb(ctx);

initLibusb();

m_paramHelper->addComboBoxParameter("DeviceNum", ui->DeviceSelector);
m_paramHelper->addComboBoxParameter("InterfaceNum", ui->InterfaceSelector);
m_paramHelper->addComboBoxParameter("AltSetNum", ui->AltSetSelector);
m_paramHelper->addComboBoxParameter("EndpointNum", ui->InterfaceSelector);
m_paramHelper->addComboBoxParameter("DeviceNum", ui->DeviceSelector, Qt::UserRole);
m_paramHelper->addComboBoxParameter("InterfaceNum", ui->InterfaceSelector, Qt::UserRole);
m_paramHelper->addComboBoxParameter("AltSetNum", ui->AltSetSelector, Qt::UserRole);
m_paramHelper->addComboBoxParameter("EndpointNum", ui->EndpointSelector, Qt::UserRole);
m_paramHelper->addSpinBoxIntParameter("TransferNum", ui->TransferNum);
m_paramHelper->addSpinBoxIntParameter("TransferDelay", ui->TransferDelay);
m_paramHelper->addSpinBoxIntParameter("TransferTimeout", ui->TransferTimeout);

m_devices = this->getUsbDevices();
this->updateSelector(ui->DeviceSelector, m_devices);
connect(ui->DeviceSelector, SIGNAL(currentTextChanged(const QString &)), this, SLOT(setInterfaces(const QString &)));
connect(ui->InterfaceSelector, SIGNAL(currentTextChanged(const QString &)), this, SLOT(setAltSet(const QString &)));
connect(ui->AltSetSelector, SIGNAL(currentTextChanged(const QString &)), this, SLOT(setEndpoint(const QString &)));
connect(ui->DeviceSelector, SIGNAL(currentTextChanged(const QString &)), this, SLOT(populateInterfaces(const QString &)));
connect(ui->InterfaceSelector, SIGNAL(currentTextChanged(const QString &)), this, SLOT(populateAltSet(const QString &)));
connect(ui->AltSetSelector, SIGNAL(currentTextChanged(const QString &)), this, SLOT(populateEndpoint(const QString &)));
connect(ui->EndpointSelector, SIGNAL(currentTextChanged(const QString &)), this, SLOT(configureEndpoint(const QString &)));

}

USBDeviceImportEditor::~USBDeviceImportEditor()
{
libusb_exit(m_ctx);
delete ui;
}

Expand All @@ -56,57 +58,52 @@ QString USBDeviceImportEditor::title()

Parameters USBDeviceImportEditor::parameters()
{
return m_paramHelper->getParametersFromUi();
auto params = m_paramHelper->getParametersFromUi();
params.insert("TransferType", (int)m_transferType);
params.insert("TransferSize", (int)m_transferSize);
return params;
}

bool USBDeviceImportEditor::setParameters(const Parameters &parameters)
{
return m_paramHelper->applyParametersToUi(parameters);
}

void USBDeviceImportEditor::previewBitsImpl(QSharedPointer<BitContainerPreview> container,
QSharedPointer<PluginActionProgress> progress)
{
// TODO: (Optional) Preview the currently active BitContainer (preprocess it, enrich it, etc)
}

void USBDeviceImportEditor::previewBitsUiImpl(QSharedPointer<BitContainerPreview> container)
{
// TODO: (Optional) Update UI elements to account for preprocessing in previewBitsImpl and/or other metadata
}

void USBDeviceImportEditor::initLibusb(libusb_context *ctx){
void USBDeviceImportEditor::initLibusb(){
m_devices.clear();
int r = libusb_init(&ctx); //initialize a library session
int r = libusb_init(&m_ctx); //initialize a library session
if(r < 0) {
std::runtime_error libusb_init_error("Error while trying to initialize Libusb");
throw libusb_init_error;
}
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, 4 );
int cnt = libusb_get_device_list(ctx, &m_devs);
if (cnt < 0){
std::runtime_error libusb_device_list_error("Error getting device list");
throw libusb_device_list_error;
}

libusb_set_option(m_ctx, LIBUSB_OPTION_LOG_LEVEL, 4 );
}

QStringList USBDeviceImportEditor::getUsbDevices(){
//TODO: see if you can use the library/system that lsusb uses to get device information
auto obuf = sp::check_output({"lsusb"});
QString output = obuf.buf.data();
QStringList devices = output.split("\n");
devices.removeLast();
return devices;
}

void USBDeviceImportEditor::setInterfaces(QString device){

//Redefine to populateInterfaces to make clearer
void USBDeviceImportEditor::populateInterfaces(QString device){

int deviceNum;
int cnt = libusb_get_device_list(m_ctx, &m_devs);
if (cnt < 0){
std::runtime_error libusb_device_list_error("Error getting device list");
throw libusb_device_list_error;
}

m_interfaces.clear();
if(m_devices.contains(device) == true){

deviceNum = m_devices.indexOf(device);
m_dev = m_devs[deviceNum];
m_deviceNum = m_devices.indexOf(device);
m_dev = m_devs[m_deviceNum];
libusb_config_descriptor *config;
libusb_get_active_config_descriptor(m_dev, &config);

Expand All @@ -122,13 +119,15 @@ void USBDeviceImportEditor::setInterfaces(QString device){
ui->AltSetSelector->clear();
m_endpoints.clear();
ui->EndpointSelector->clear();
libusb_free_device_list(m_devs, m_deviceNum);
libusb_free_config_descriptor(config);
updateSelector(ui->InterfaceSelector, m_interfaces);
}


}

void USBDeviceImportEditor::setAltSet(QString interface){
void USBDeviceImportEditor::populateAltSet(QString interface){

m_altSets.clear();
if(m_interfaces.contains(interface) == true){
Expand All @@ -149,11 +148,12 @@ void USBDeviceImportEditor::setAltSet(QString interface){
}
m_endpoints.clear();
ui->EndpointSelector->clear();
libusb_free_config_descriptor(config);
updateSelector(ui->AltSetSelector, m_altSets);
}
}

void USBDeviceImportEditor::setEndpoint(QString altSet){
void USBDeviceImportEditor::populateEndpoint(QString altSet){
m_endpoints.clear();
if(m_altSets.contains(altSet) == true){

Expand All @@ -174,11 +174,33 @@ void USBDeviceImportEditor::setEndpoint(QString altSet){
m_endpoints.append(endpt);
}
}
libusb_free_config_descriptor(config);
updateSelector(ui->EndpointSelector, m_endpoints);
}
}
//rename to make clearer
void USBDeviceImportEditor::configureEndpoint(QString endpoint){
if(m_endpoints.contains(endpoint)){
m_endpointNum = m_endpoints.indexOf(endpoint);

libusb_config_descriptor *config;
libusb_get_active_config_descriptor(m_dev, &config);

const libusb_interface *inter = &config->interface[m_interfaceNum];
const libusb_interface_descriptor *interdesc = &inter->altsetting[m_altSetNum];
const libusb_endpoint_descriptor *epdesc = &interdesc->endpoint[m_endpointNum];

m_transferType = epdesc->bmAttributes;
m_transferSize = epdesc->wMaxPacketSize;
libusb_free_config_descriptor(config);
}
}

void USBDeviceImportEditor::updateSelector(QComboBox *selector, QStringList items){
selector->clear();
selector->addItems(items);
for(int i = 0; i < items.length(); i++){
selector->addItem(items[i], i);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "abstractparametereditor.h"
#include "parameterhelper.h"
#include <libusb-1.0/libusb.h>
#include "importresult.h"

namespace Ui
{
Expand All @@ -21,39 +22,42 @@ class USBDeviceImportEditor : public AbstractParameterEditor

bool setParameters(const Parameters &parameters) override;
Parameters parameters() override;
static QSharedPointer<ImportResult> importData(const Parameters &parameters, QSharedPointer<PluginActionProgress> progress);



uint8_t m_transferType;
uint16_t m_transferSize;
int m_interfaceNum;
int m_altSetNum;
int m_endpointNum;
unsigned char m_endpoint;
public slots:


void setInterfaces(QString device);
void setAltSet(QString interface);
void setEndpoint(QString altSet);
void populateInterfaces(QString device);
void populateAltSet(QString interface);
void populateEndpoint(QString altSet);
void configurEndpoint(QString endpoint);

private:

void initLibusb(libusb_context *ctx);
void initLibusb();
QStringList getUsbDevices();
void updateSelector(QComboBox *selector, QStringList items);
void clearSelectors(QComboBox *boxes[]);

void previewBitsImpl(QSharedPointer<BitContainerPreview> container,
QSharedPointer<PluginActionProgress> progress) override;
void previewBitsUiImpl(QSharedPointer<BitContainerPreview> container) override;

Ui::USBDeviceImportEditor *ui;
QSharedPointer<ParameterHelper> m_paramHelper;

QStringList m_devices;
QStringList m_interfaces;
QStringList m_altSets;
QStringList m_endpoints;
libusb_device *m_dev;
libusb_device **m_devs;
libusb_config_descriptor *m_config;
int m_interfaceNum;
int m_altSetNum;
int m_endpointNum;
unsigned char m_endpoint;
libusb_context *m_ctx;
int m_deviceNum;
};


Expand Down

0 comments on commit 495ffb1

Please sign in to comment.