2017-10-21 06:11:49 +01:00
|
|
|
#ifndef unixUsbDriver_H
|
|
|
|
#define unixUsbDriver_H
|
|
|
|
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
#include "genericusbdriver.h"
|
|
|
|
#include "libusb.h"
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include "libdfuprog.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_ALLOWABLE_CUMULATIVE_FRAME_ERROR 50
|
|
|
|
|
|
|
|
//tcBlock is fed to the callback in the libusb user data section.
|
|
|
|
typedef struct tcBlock{
|
|
|
|
int number;
|
|
|
|
bool completed;
|
|
|
|
qint64 timeReceived;
|
|
|
|
} tcBlock;
|
|
|
|
|
|
|
|
//Oddly, libusb requires you to make a blocking libusb_handle_events() call in order to execute the callbacks for an asynchronous transfer.
|
|
|
|
//Since the call is blocking, this worker must exist in a separate, low priority thread!
|
|
|
|
class worker : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
worker(){};
|
|
|
|
~worker(){};
|
|
|
|
libusb_context *ctx;
|
|
|
|
bool stopTime = false;
|
2018-02-17 03:50:14 +00:00
|
|
|
unsigned char cleanupRemaining = 4;
|
2017-10-21 06:11:49 +01:00
|
|
|
public slots:
|
|
|
|
void handle(){
|
|
|
|
qDebug() << "SUB THREAD ID" << QThread::currentThreadId();
|
2018-02-17 03:50:14 +00:00
|
|
|
while(cleanupRemaining){
|
2017-10-21 06:11:49 +01:00
|
|
|
//qDebug() << cleanupRemaining;
|
|
|
|
if(libusb_event_handling_ok(ctx)){
|
|
|
|
libusb_handle_events_timeout(ctx, &tv);
|
|
|
|
//qDebug() << "HANDLED";
|
|
|
|
}
|
|
|
|
if(stopTime){
|
|
|
|
if(cleanupRemaining){
|
|
|
|
cleanupRemaining--;
|
2018-02-17 03:50:14 +00:00
|
|
|
qDebug("Cleaning... #%hhu phases remain.\n", cleanupRemaining);
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-17 03:50:14 +00:00
|
|
|
qDebug() << "Cleanup complete"; //THIS THREAD STILL EXISTS
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//This is the actual unixUsbDriver
|
|
|
|
//It handles the Mac/Linux specific parts of USB communication, through libusb.
|
|
|
|
//See genericUsbDriver for the non-platform-specific parts.
|
|
|
|
class unixUsbDriver : public genericUsbDriver
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit unixUsbDriver(QWidget *parent = 0);
|
|
|
|
~unixUsbDriver();
|
|
|
|
void usbSendControl(uint8_t RequestType, uint8_t Request, uint16_t Value, uint16_t Index, uint16_t Length, unsigned char *LDATA);
|
|
|
|
char *isoRead(unsigned int *newLength);
|
2017-12-24 02:07:28 +00:00
|
|
|
void manualFirmwareRecovery(void);
|
2017-10-21 06:11:49 +01:00
|
|
|
protected:
|
|
|
|
//USB Vars
|
|
|
|
libusb_context *ctx = NULL;
|
|
|
|
libusb_device_handle *handle = NULL;
|
|
|
|
//USBIso Vars
|
|
|
|
unsigned char *midBuffer_current[NUM_ISO_ENDPOINTS];
|
|
|
|
unsigned char *midBuffer_prev[NUM_ISO_ENDPOINTS];
|
|
|
|
qint64 midBufferOffsets[NUM_ISO_ENDPOINTS];
|
2018-09-10 23:46:15 +01:00
|
|
|
libusb_transfer *isoCtx[NUM_ISO_ENDPOINTS][NUM_FUTURE_CTX] = { };
|
2017-10-21 06:11:49 +01:00
|
|
|
tcBlock transferCompleted[NUM_ISO_ENDPOINTS][NUM_FUTURE_CTX];
|
|
|
|
unsigned char dataBuffer[NUM_ISO_ENDPOINTS][NUM_FUTURE_CTX][ISO_PACKET_SIZE*ISO_PACKETS_PER_CTX];
|
2018-09-10 23:46:15 +01:00
|
|
|
worker *isoHandler = nullptr;
|
|
|
|
QThread *workerThread = nullptr;
|
2017-10-21 06:11:49 +01:00
|
|
|
int cumulativeFramePhaseErrors = 0;
|
|
|
|
//Generic Functions
|
|
|
|
virtual unsigned char usbInit(unsigned long VIDin, unsigned long PIDin);
|
2018-08-14 08:18:58 +01:00
|
|
|
int usbIsoInit(void);
|
2017-10-21 06:11:49 +01:00
|
|
|
virtual int flashFirmware(void);
|
|
|
|
bool allEndpointsComplete(int n);
|
|
|
|
bool shutdownMode = false;
|
|
|
|
int numCancelled = 0;
|
|
|
|
signals:
|
|
|
|
public slots:
|
|
|
|
void isoTimerTick(void);
|
|
|
|
void recoveryTick(void);
|
|
|
|
void shutdownProcedure(void);
|
|
|
|
void backupCleanup(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Callback on iso transfer complete.
|
|
|
|
static void LIBUSB_CALL isoCallback(struct libusb_transfer * transfer){
|
|
|
|
tcBlockMutex.lock();
|
|
|
|
//int number = ((tcBlock *)transfer->user_data)->number;
|
|
|
|
//bool completed = ((tcBlock *)transfer->user_data)->completed;
|
|
|
|
|
|
|
|
//qDebug() << "CALLBACK" << number;
|
|
|
|
//qDebug() << completed;
|
|
|
|
|
|
|
|
if(transfer->status!=LIBUSB_TRANSFER_CANCELLED){
|
|
|
|
((tcBlock *)transfer->user_data)->completed = true;
|
|
|
|
((tcBlock *)transfer->user_data)->timeReceived = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
}
|
|
|
|
//qDebug() << ((tcBlock *)transfer->user_data)->timeReceived;
|
|
|
|
tcBlockMutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // unixUsbDriver_H
|