void showHelp() { QFile help_file(":/help.html"); if (!help_file.open(QIODevice::ReadOnly)) return; QString help = help_file.readAll(); help_file.close(); qInformal() << help.remove(QRegExp("(<[^>]+>)|\t\b")); // Clearing HTML tags. qInformal() << "Version:" << __QSTL_VER__; }
void MainWindow::Disconnect() { this->log("Disconnecting..."); this->btl->disconnect(); this->log("Disconnected."); qInformal() << "Disconnected."; this->ui->b_disconnect->setEnabled(false); this->ui->b_connect->setEnabled(true); this->ui->gb_bottom->setEnabled(false); this->ui->b_send->setEnabled(false); this->ui->b_verify->setEnabled(false); this->ui->b_repeat->setEnabled(false); this->ui->b_reset->setEnabled(false); }
DeviceList::DeviceList(QObject *parent) : QObject(parent) { this->loaded = false; qDebug("Loading device list."); this->doc = new QDomDocument("stlink"); QFile file("/usr/share/qstlink2/devices.xml"); if (!file.open(QIODevice::ReadOnly)) { qInformal() << "Could not open the devices.xml file. Using internal data."; file.setFileName(":/devices.xml"); } if (!doc->setContent(&file)) { file.close(); qCritical() << "Devices list failed to load."; return; } file.close(); qInformal() << "Devices list loaded."; this->default_device = new Device(this); bool isInt; QDomElement docElem = doc->documentElement(); QDomNode n = docElem.firstChild(); while(!n.isNull()) { QDomElement e = n.toElement(); // try to convert the node to an element. if(!e.isNull()) { if (e.tagName() == "devices_default") { QDomNodeList childs = e.childNodes(); for (int i = 0;i < childs.count();i++) { QDomElement el = childs.at(i).toElement(); qDebug() << e.tagName() << "->" << el.tagName(); (*this->default_device)[el.tagName()] = (quint32)el.text().toInt(&isInt, 16); if (!isInt) qCritical() << el.tagName() << "Failed to parse number!"; } } else if (e.tagName() == "regs_default") { QDomNodeList regs = e.childNodes(); for (int a = 0;a < regs.count(); a++) { QDomElement el = regs.at(a).toElement(); qDebug() << el.tagName() << "->" << el.text().toInt(0, 16); (*this->default_device)[el.tagName()] = (quint32)el.text().toInt(&isInt, 16); if (!isInt) qCritical() << el.tagName() << "Failed to parse number!"; } } } n = n.nextSibling(); } n = docElem.firstChild(); while(!n.isNull()) { QDomElement e = n.toElement(); // try to convert the node to an element. if(!e.isNull()) { if (e.tagName() == "devices") { QDomNodeList devices = e.childNodes(); for (int a = 0;a < devices.count(); a++) { this->devices.append(new Device(this->default_device)); // Copy from the default device. QDomElement device = devices.at(a).toElement(); this->devices.last()->type = device.attribute("type"); QDomNodeList childs = device.childNodes(); for (int i = 0;i < childs.count();i++) { QDomElement el = childs.at(i).toElement(); qDebug() << device.tagName() << "->" << device.attribute("type") << "->" << el.tagName() ; (*this->devices.last())[el.tagName()] = (quint32)el.text().toInt(&isInt, 16); if (!isInt && el.tagName() != "loader") qCritical() << el.tagName() << "Failed to parse number!"; if (el.tagName() == "loader") { this->devices.last()->loader_file = el.text(); } } } } } n = n.nextSibling(); } this->loaded = true; return; }
void transferThread::send(const QString &filename) { QFile file(filename); if (!file.open(QIODevice::ReadOnly)) { qCritical("Could not open the file."); return; } emit sendLock(true); this->m_stop = false; quint32 step_size = 116; const quint32 from = 0; const quint32 to = file.size(); quint32 progress, oldprogress; qint32 read; char *buf2 = new char[step_size]; qDebug() << "File size" << file.size(); if (!this->m_btl->eraseFlash(file.size())) { emit sendStatus("Erase failed"); emit sendLog("Erase failed"); qDebug() << "Erase failed"; emit sendLock(false); return; } else { emit sendStatus("Erase OK"); emit sendLog("Erase OK"); qDebug() << "Erase OK"; } qInformal() << "Writing from" << "0x"+QString::number(from, 16) << "to" << "0x"+QString::number(to, 16); emit sendStatus("Transfering"); emit sendLog("Transfering"); progress = 0; for (int i=0; i<=file.size(); i+=step_size) { if (this->m_stop) break; if (file.atEnd()) { qDebug() << "End Of File"; break; } memset(buf2, 0, step_size); if ((read = file.read(buf2, step_size)) <= 0) break; qDebug() << "Read" << read << "Bytes from disk"; QByteArray buf(buf2, read); if (this->m_btl->writeFlash(i, &buf, step_size) < read){ emit sendStatus("Transfer failed"); emit sendLog("Transfer failed"); break; } oldprogress = progress; progress = (i*100)/file.size(); if (progress > oldprogress) { // Push only if number has increased emit sendProgress(progress); qInformal() << "Progress:"<< QString::number(progress)+"%"; } } emit sendLoaderStatus("Idle"); file.close(); delete buf2; emit sendProgress(100); emit sendStatus("Transfer done"); emit sendLog("Transfer done"); qInformal() << "Transfer done"; emit sendLock(false); }
void transferThread::verify(const QString &filename) { QFile file(filename); if (!file.open(QIODevice::ReadOnly)) { qCritical("Could not open the file."); return; } emit sendLock(true); this->m_stop = false; const quint32 buf_size = 512; const quint32 from = 0; const quint32 to = file.size(); qInformal() << "Reading from" << QString::number(from, 16) << "to" << QString::number(to, 16); quint32 addr, progress, oldprogress; QByteArray data_local, data_remote; progress = 0; for (quint32 i=0; i<file.size(); i+=buf_size) { if (this->m_stop) break; usleep(50000); data_local = file.read(buf_size); qDebug() << "Read" << data_local.size() << "Bytes from disk"; addr = i; if (!data_local.size()) break; int read_size = this->m_btl->readMem(addr, &data_remote, data_local.size()); if (read_size < data_local.size()) {// Read same amount of data as from file. file.close(); emit sendProgress(0); emit sendStatus("Verification Failed"); qInformal() << "Verification Failed"; qInformal() << "read" << read_size << "valid" << data_local.size(); emit sendLock(false); return; } if (data_remote != data_local) { file.close(); emit sendProgress(100); emit sendStatus("Verification failed at 0x"+QString::number(addr, 16)); QString stmp, sbuf; for (int b=0;b<data_local.size();b++) { stmp.append(QString().sprintf("%02X ", (uchar)data_local.at(b))); sbuf.append(QString().sprintf("%02X ", (uchar)data_remote.at(b))); } qCritical() << "Verification failed at 0x"+QString::number(addr, 16) << "\r\n Expecting:" << stmp << "\r\n Got:" << sbuf; emit sendLock(false); return; } oldprogress = progress; progress = (i*100)/file.size(); if (progress > oldprogress) { // Push only if number has increased emit sendProgress(progress); qInformal() << "Progress:"<< QString::number(progress)+"%"; } emit sendStatus("Verified "+QString::number(i/1024)+" kilobytes out of "+QString::number(file.size()/1024)); } file.close(); emit sendProgress(100); emit sendStatus("Verification OK"); qInformal() << "Verification OK"; emit sendLock(false); }
int main(int argc, char *argv[]) { timer.start(); QApplication a(argc, argv); quint8 i = 0; QStringList args = QCoreApplication::arguments(); QRegExp path_reg("[\\/]"); // Checks for a Unix or Windows path. foreach (const QString &str, args) { if (!i++) // Skip first one continue; if (!str.contains(path_reg)) { if (str.contains('h') || str == "--help") { showHelp(); return 0; } if (shortParam(str, 'q') || str == "--quiet") verbose_level = 0; if (shortParam(str, 'v') || str == "--verbose") verbose_level = 5; if (shortParam(str, 'c') || str == "--cli") show = false; if (shortParam(str, 'e') || str == "--erase") erase = true; if (shortParam(str, 'w') || str == "--write") write_flash = true; if (shortParam(str, 'r') || str == "--read") read_flash = true; if (shortParam(str, 'V') || str == "--verify") verify = true; } } if (!show) { if ((!erase) && (args.size() <= 2) && (!args.last().contains(path_reg))) { qCritical() << "Invalid options"; showHelp(); return 1; } else if ((!erase) && (args.size() >= 2) && (!args.last().contains(path_reg))) { qCritical() << "Invalid path:" << args.last(); showHelp(); return 1; } if (args.last().contains(path_reg)) path = args.last(); // Path is always the last argument. } qDebug() << "Verbose level:" << verbose_level; qInstallMessageHandler(myMessageOutput); MainWindow *w = new MainWindow; if (show) { w->show(); } else { if (!path.isEmpty()) { qInformal() << "File Path:" << path; qInformal() << "Erase:" << erase; qInformal() << "Write:" << write_flash; qInformal() << "Verify:" << verify; if (!w->Connect()) { return 1; } if (write_flash) { w->Send(path); while (w->tfThread->isRunning()) { usleep(100000); } } else if (read_flash) { w->Receive(path); while (w->tfThread->isRunning()) { usleep(100000); } } if (verify) { w->Verify(path); while (w->tfThread->isRunning()) { usleep(100000); } } usleep(300000); //300 msec w->Disconnect(); w->close(); return 0; } else if (erase) { qInformal() << "Only erasing flash"; if (!w->Connect()) return 1; w->eraseFlash(); w->Disconnect(); return 0; } else if (!show) { showHelp(); w->close(); return 0; } return 1; } return a.exec(); }
struct usb_dev_handle* LibUsb::OpenAntStick() { struct usb_bus* bus; struct usb_device* dev; struct usb_dev_handle* udev; for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == USB_ST_VID && dev->descriptor.idProduct == USB_STLINK_PID) { qCritical() << "Found ST an Link V1, this one is not supported!"; return NULL; } else if (dev->descriptor.idVendor == USB_ST_VID && dev->descriptor.idProduct == USB_STLINKv2_PID) { //Avoid noisy output qInformal() << "Found an ST Link V2."; if ((udev = usb_open(dev))) { qInformal() << "Opening device..."; if (dev->descriptor.bNumConfigurations) { if ((intf = usb_find_interface(&dev->config[0])) != NULL) { // Loading first config. qint32 rc = usb_set_configuration(udev, 1); if (rc < 0) { qCritical()<<"usb_set_configuration Error: "<< usb_strerror(); #ifdef __linux__ // looks like the udev rule has not been implemented qCritical()<<"Check permissions on:"<<QString("/dev/bus/usb/%1/%2").arg(bus->dirname).arg(dev->filename); qCritical()<<"Did you remember to setup a udev rule for this device?"; qCritical()<<"Copy file 49-stlinkv2.rules to /etc/udev/rules.d/ to enable access."; return NULL; #endif } rc = usb_claim_interface(udev, this->interface); if (rc < 0) qCritical()<<"usb_claim_interface Error: "<< usb_strerror(); //#ifndef Q_OS_MAC // // fails on Mac OS X, we don't actually need it anyway // rc = usb_set_altinterface(udev, alternate); // if (rc < 0) qDebug()<<"usb_set_altinterface Error: "<< usb_strerror(); //#endif qInformal() << "Device Open."; return udev; } else qCritical() << "Could not load interface configuration."; } usb_close(udev); } } } } qCritical() << "Found nothing..."; return NULL; }