Example #1
0
ConnectionPointer<Connection> ConnectionManager2::getConnWithConfig(quint8 type, const QHash<QString, QVariant> &cfg)
{
    this->refresh();

#ifdef HAVE_LIBYB
    // We want to receive first USB enumeration events,
    // so that connections created here can be opened
    QApplication::processEvents();
#endif

    Connection *enumCon = NULL;
    for(int i = 0; i < m_conns.size(); ++i)
    {
        if(m_conns[i]->getType() != type)
            continue;

        switch(type)
        {
            case CONNECTION_SERIAL_PORT:
            {
                SerialPort *sp = (SerialPort*)m_conns[i];
                if(sp->deviceName() == cfg["device_name"])
                {
                    if(!sp->removable())
                        enumCon = sp;
                    else if(sp->baudRate() == cfg["baud_rate"])
                        return ConnectionPointer<Connection>::fromPtr(sp);
                }
                break;
            }
            case CONNECTION_TCP_SOCKET:
            {
                TcpSocket *socket = (TcpSocket*)m_conns[i];
                if(socket->host() == cfg["host"] && socket->port() == cfg["port"])
                    return ConnectionPointer<Connection>::fromPtr(socket);
                break;
            }
            case CONNECTION_UDP_SOCKET:
            {
                UdpSocket *socket = (UdpSocket*)m_conns[i];
                if (socket->name() == cfg["name"] &&
                    socket->host() == cfg["host"] && socket->port() == cfg["port"])
                    return ConnectionPointer<Connection>::fromPtr(socket);
                break;
            }
            case CONNECTION_PROXY_TUNNEL:
            {
                ProxyTunnel *tunnel = (ProxyTunnel*)m_conns[i];
                if(tunnel->name() == cfg["name"])
                    return ConnectionPointer<Connection>::fromPtr(tunnel);
                break;
            }
            case CONNECTION_SHUPITO_TUNNEL:
            {
                QHash<QString, QVariant> c = cfg.value("companions").toHash();
                QHash<QString, QVariant>::iterator itr = c.find(ShupitoTunnel::getCompanionName());
                if(itr == c.end())
                    return ConnectionPointer<Connection>();

                qint64 id = itr.value().toLongLong();
                if(id != 0 && id == m_conns[i]->getCompanionId(ShupitoTunnel::getCompanionName()))
                    return ConnectionPointer<Connection>::fromPtr((ShupitoTunnel*)m_conns[i]);
                break;
            }
            case CONNECTION_SHUPITO_SPI_TUNNEL:
            {
                QHash<QString, QVariant> c = cfg.value("companions").toHash();
                QHash<QString, QVariant>::iterator itr = c.find(ShupitoSpiTunnelConn::getCompanionName());
                if(itr == c.end())
                    return ConnectionPointer<Connection>();

                qint64 id = itr.value().toLongLong();
                if(id != 0 && id == m_conns[i]->getCompanionId(ShupitoSpiTunnelConn::getCompanionName()))
                    return ConnectionPointer<Connection>::fromPtr((ShupitoSpiTunnelConn*)m_conns[i]);
                break;
            }
#ifdef HAVE_LIBYB
            case CONNECTION_USB_ACM2:
            {
                UsbAcmConnection2 *usb = (UsbAcmConnection2*)m_conns[i];
                if (usb->vid() == cfg.value("vid", 0).toInt() &&
                    usb->pid() == cfg.value("pid", 0).toInt() &&
                    usb->serialNumber() == cfg.value("serial_number").toString() &&
                    usb->intfName() == cfg.value("intf_name").toString() &&
                    usb->baudRate() == cfg.value("baud_rate", 115200).toInt() &&
                    usb->stopBits() == cfg.value("stop_bits", 0).toInt() &&
                    usb->parity() == (UsbAcmConnection2::parity_t)cfg.value("parity", 0).toInt() &&
                    usb->dataBits() == cfg.value("data_bits", 8).toInt())
                {
                    usb->applyConfig(cfg);
                    return ConnectionPointer<Connection>::fromPtr(usb);
                }
                break;
            }
            case CONNECTION_SHUPITO23:
            {
                UsbShupito23Connection *usb = (UsbShupito23Connection*)m_conns[i];
                if (usb->vid() == cfg.value("vid", 0).toInt() &&
                    usb->pid() == cfg.value("pid", 0).toInt() &&
                    usb->serialNumber() == cfg.value("serial_number").toString() &&
                    usb->intfName() == cfg.value("intf_name").toString())
                {
                    usb->applyConfig(cfg);
                    return ConnectionPointer<Connection>::fromPtr(usb);
                }
                break;
            }
            case CONNECTION_STM32:
            {
                STM32Connection *stm32 = (STM32Connection*)m_conns[i];
                if (stm32->vid() == cfg.value("vid", 0).toInt() &&
                    stm32->pid() == cfg.value("pid", 0).toInt() &&
                    stm32->serialNumber() == cfg.value("serial_number").toString() &&
                    stm32->intfName() == cfg.value("intf_name").toString())
                {
                    return ConnectionPointer<Connection>::fromPtr(stm32);
                }
                break;
            }
#endif
            default:
                return ConnectionPointer<Connection>();
        }
    }

    if(enumCon)
    {
        enumCon->applyConfig(cfg);
        return ConnectionPointer<Connection>::fromPtr(enumCon);
    }

    if(type == CONNECTION_SHUPITO_TUNNEL)
    {
        QHash<QString, QVariant> c = cfg.value("companions").toHash();
        QHash<QString, QVariant>::iterator itr = c.find(ShupitoTunnel::getCompanionName());
        if(itr == c.end())
            return ConnectionPointer<Connection>();

        qint64 id = itr.value().toLongLong();
        if(id == 0)
            return ConnectionPointer<Connection>();

        ConnectionPointer<Connection> tunnel(new ShupitoTunnel());
        tunnel->applyConfig(cfg);
        tunnel->setRemovable(false);
        this->addConnection(tunnel.data());
        return tunnel;
    }
    else if(type == CONNECTION_SHUPITO_SPI_TUNNEL)
    {
        QHash<QString, QVariant> c = cfg.value("companions").toHash();
        QHash<QString, QVariant>::iterator itr = c.find(ShupitoSpiTunnelConn::getCompanionName());
        if(itr == c.end())
            return ConnectionPointer<Connection>();

        qint64 id = itr.value().toLongLong();
        if(id == 0)
            return ConnectionPointer<Connection>();

        ConnectionPointer<Connection> tunnel(new ShupitoSpiTunnelConn());
        tunnel->applyConfig(cfg);
        tunnel->setRemovable(false);
        this->addConnection(tunnel.data());
        return tunnel;
    }
#ifdef HAVE_LIBYB
    else if(type == CONNECTION_USB_ACM2)
    {
        ConnectionPointer<Connection> usb(createUsbAcmConn());
        usb->applyConfig(cfg);
        return usb;
    }
    else if(type == CONNECTION_STM32)
    {
        ConnectionPointer<Connection> stm32(createSTM32Conn());
        stm32->applyConfig(cfg);
        return stm32;
    }
#endif

    return ConnectionPointer<Connection>();
}
void ChooseConnectionDlg::updateDetailsUi(Connection * conn)
{
    updateEditText(ui->connectionNameEdit, conn->name());
    ui->actionRemoveConnection->setEnabled(conn->removable());
    ui->actionConnect->setEnabled(conn->state() == st_disconnected);
    ui->actionDisconnect->setEnabled(conn->state() == st_connected || conn->state() == st_connect_pending || conn->state() == st_disconnecting);
    ui->programmerSelection->setVisible(false);
    ui->persistNameButton->setVisible(conn->isNamePersistable());
    ui->persistNameButton->setEnabled(!conn->hasDefaultName());

    switch (conn->getType())
    {
    case CONNECTION_SERIAL_PORT:
        {
            SerialPort * sp = static_cast<SerialPort *>(conn);
            ui->settingsStack->setCurrentWidget(ui->serialPortPage);
            updateEditText(ui->spBaudRateEdit->lineEdit(), QString::number((int)sp->baudRate()));
            updateEditText(ui->spDeviceNameEdit, sp->deviceName());
            updateComboIndex(ui->spParity, (int)sp->parity());
            updateComboIndex(ui->spStopBits, (int)sp->stopBits());
            updateComboText(ui->spDataBits, QString::number(sp->dataBits()));
            ui->spDeviceNameEdit->setEnabled(sp->devNameEditable());
            ui->programmerSelection->setVisible(m_allowedConns & pct_port_programmable);
            setActiveProgBtn(sp->programmerType());
        }
        break;
    case CONNECTION_TCP_SOCKET:
        {
            TcpSocket * tc = static_cast<TcpSocket *>(conn);
            ui->settingsStack->setCurrentWidget(ui->tcpClientPage);
            updateEditText(ui->tcHostEdit, tc->host());
            ui->tcPortEdit->setValue(tc->port());
            ui->programmerSelection->setVisible(m_allowedConns & pct_port_programmable);
            setActiveProgBtn(tc->programmerType());
        }
        break;
    case CONNECTION_USB_ACM2:
        {
            UsbAcmConnection2 * c = static_cast<UsbAcmConnection2 *>(conn);
            ui->settingsStack->setCurrentWidget(ui->usbAcmConnPage);

            updateComboText(ui->usbBaudRateEdit, QString::number((int)c->baudRate()));
            updateComboIndex(ui->usbParityCombo, (int)c->parity());
            updateComboIndex(ui->usbStopBitsCombo, (int)c->stopBits());
            updateComboText(ui->usbDataBitsCombo, QString::number(c->dataBits()));

            updateEditText(ui->usbVidEdit, QString("%1").arg(c->vid(), 4, 16, QChar('0')));
            updateEditText(ui->usbPidEdit, QString("%1").arg(c->pid(), 4, 16, QChar('0')));
            updateEditText(ui->usbAcmSnEdit, c->serialNumber());
            updateEditText(ui->usbIntfNameEdit, c->intfName());

            bool editable = !c->enumerated() && (c->state() == st_disconnected || c->state() == st_missing);
            ui->usbVidEdit->setEnabled(editable);
            ui->usbPidEdit->setEnabled(editable);
            ui->usbAcmSnEdit->setEnabled(editable);
            ui->usbIntfNameEdit->setEnabled(editable);

            ui->programmerSelection->setVisible(m_allowedConns & pct_port_programmable);
            setActiveProgBtn(c->programmerType());
        }
        break;
    case CONNECTION_USB_SHUPITO:
    case CONNECTION_SHUPITO23:
        {
            ShupitoConnection * c = static_cast<ShupitoConnection *>(conn);
            ShupitoFirmwareDetails fd;
            if (c->getFirmwareDetails(fd))
            {
                ui->shupito23HardwareLabel->setText(QString("%1.%2").arg(fd.hw_major).arg(fd.hw_minor));
                ui->shupito23FirmwareLabel->setText(fd.firmwareFilename());
                ui->settingsStack->setCurrentWidget(ui->shupito23Page);
            }
            else
            {
                ui->settingsStack->setCurrentWidget(ui->noSettingsPage);
            }
        }
        break;
    case CONNECTION_SHUPITO_TUNNEL:
        {
            ShupitoTunnel * st = static_cast<ShupitoTunnel *>(conn);
            ui->programmerSelection->setVisible(m_allowedConns & pct_port_programmable);
            setActiveProgBtn(st->programmerType());

            ui->settingsStack->setCurrentWidget(ui->noSettingsPage);
        }
        break;
    default:
        {
            ui->settingsStack->setCurrentWidget(ui->noSettingsPage);
        }
        break;
    }
}