From f2d44e11ce19814e865b6469f66f3dba01a2f97f Mon Sep 17 00:00:00 2001 From: Teuniz Date: Sat, 9 Sep 2017 18:40:22 +0200 Subject: [PATCH] Added the possibility to use a hostname instead of an IP-address for the LAN connection. --- global.h | 4 +- lan_connect_thread.cpp | 4 +- mainwindow.cpp | 73 ++++++++++++++++------------ settings_dialog.cpp | 108 +++++++++++++++++++++++++++++++++-------- settings_dialog.h | 13 ++++- tmc_lan.c | 45 ++++++++++++++++- 6 files changed, 191 insertions(+), 56 deletions(-) diff --git a/global.h b/global.h index 7c6bdac..fff823e 100644 --- a/global.h +++ b/global.h @@ -35,7 +35,7 @@ #define PROGRAM_NAME "DSRemote" -#define PROGRAM_VERSION "0.35_1707091429" +#define PROGRAM_VERSION "0.35_1709091834" #define MAX_PATHLEN 4096 @@ -124,6 +124,8 @@ struct device_settings int vertdivisions; // number of vertical divisions, 8 or 10 int use_extra_vertdivisions; // If 1: use 10 vertical divisions instead of 8, DS1000Z only + char hostname[128]; + int screentimerival; int channel_cnt; // Device has 2 or 4 channels diff --git a/lan_connect_thread.cpp b/lan_connect_thread.cpp index a7742c9..ad95895 100644 --- a/lan_connect_thread.cpp +++ b/lan_connect_thread.cpp @@ -55,9 +55,9 @@ struct tmcdev * lan_connect_thread::get_device(void) void lan_connect_thread::set_device_address(const char *addr) { - strncpy(dev_str, addr, 16); + strncpy(dev_str, addr, 63); - dev_str[15] = 0; + dev_str[63] = 0; } diff --git a/mainwindow.cpp b/mainwindow.cpp index 373eed8..b7ce3eb 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -98,53 +98,62 @@ void UI_Mainwindow::open_connection() if(devparms.connectiontype == 1) // LAN { - strcpy(dev_str, settings.value("connection/ip", "192.168.1.100").toString().toLatin1().data()); + strcpy(devparms.hostname, settings.value("connection/hostname", "").toString().toLatin1().data()); - if(!strcmp(dev_str, "")) + if(strlen(devparms.hostname)) { - sprintf(str, "No IP address set"); - goto OC_OUT_ERROR; + strcpy(dev_str, devparms.hostname); } - - len = strlen(dev_str); - - if(len < 7) + else { - sprintf(str, "No IP address set"); - goto OC_OUT_ERROR; - } + strcpy(dev_str, settings.value("connection/ip", "192.168.1.100").toString().toLatin1().data()); - int cf = 0; - - for(i=0; isetText("Disconnected"); + + printf("Disconnected from device\n"); } diff --git a/settings_dialog.cpp b/settings_dialog.cpp index b3b4b27..9e43bc1 100644 --- a/settings_dialog.cpp +++ b/settings_dialog.cpp @@ -57,14 +57,19 @@ UI_settings_window::UI_settings_window(QWidget *parnt) usbRadioButton->setChecked(true); } - lanRadioButton = new QRadioButton("LAN", this); - lanRadioButton->setAutoExclusive(true); - lanRadioButton->setGeometry(40, 70, 110, 25); + lanIPRadioButton = new QRadioButton("LAN", this); + lanIPRadioButton->setAutoExclusive(true); + lanIPRadioButton->setGeometry(40, 70, 110, 25); if(mainwindow->devparms.connectiontype == 1) { - lanRadioButton->setChecked(true); + lanIPRadioButton->setChecked(true); } + hostnameLabel = new QLabel(this); + hostnameLabel->setGeometry(40, 120, 120, 35); + hostnameLabel->setText("Hostname\n(overides IP-address)"); + hostnameLabel->setToolTip("Leave empty if you want to use the above IP-address"); + comboBox1 = new QComboBox(this); comboBox1->setGeometry(180, 20, 110, 25); comboBox1->addItem("/dev/usbtmc0"); @@ -129,23 +134,34 @@ UI_settings_window::UI_settings_window(QWidget *parnt) ipSpinbox4->setValue(100); } + if(settings.contains("connection/hostname")) + { + strncpy(mainwindow->devparms.hostname, settings.value("connection/hostname").toString().toLatin1().data(), 63); + } + + HostLineEdit = new QLineEdit(this); + HostLineEdit->setGeometry(180, 120, 240, 25); + HostLineEdit->setMaxLength(63); + HostLineEdit->setText(mainwindow->devparms.hostname); + HostLineEdit->setToolTip("Leave empty if you want to use the above IP-address"); + refreshLabel = new QLabel(this); - refreshLabel->setGeometry(40, 120, 120, 35); - refreshLabel->setText("Screen update\n interval"); + refreshLabel->setGeometry(40, 170, 120, 35); + refreshLabel->setText("Screen update\ninterval"); refreshSpinbox = new QSpinBox(this); - refreshSpinbox->setGeometry(180, 120, 100, 25); + refreshSpinbox->setGeometry(180, 170, 100, 25); refreshSpinbox->setSuffix(" mS"); refreshSpinbox->setRange(50, 2000); refreshSpinbox->setSingleStep(10); refreshSpinbox->setValue(mainwindow->devparms.screentimerival); invScrShtLabel = new QLabel(this); - invScrShtLabel->setGeometry(40, 170, 120, 35); + invScrShtLabel->setGeometry(40, 220, 120, 35); invScrShtLabel->setText("Screenshot invert\n colors"); invScrShtCheckbox = new QCheckBox(this); - invScrShtCheckbox->setGeometry(180, 170, 120, 35); + invScrShtCheckbox->setGeometry(180, 220, 120, 35); invScrShtCheckbox->setTristate(false); if(mainwindow->devparms.screenshot_inv) { @@ -157,11 +173,11 @@ UI_settings_window::UI_settings_window(QWidget *parnt) } showfpsLabel = new QLabel(this); - showfpsLabel->setGeometry(40, 220, 120, 35); + showfpsLabel->setGeometry(40, 270, 120, 35); showfpsLabel->setText("Show frames\n per second"); showfpsCheckbox = new QCheckBox(this); - showfpsCheckbox->setGeometry(180, 220, 120, 35); + showfpsCheckbox->setGeometry(180, 270, 120, 35); showfpsCheckbox->setTristate(false); if(mainwindow->devparms.show_fps) { @@ -173,11 +189,11 @@ UI_settings_window::UI_settings_window(QWidget *parnt) } extendvertdivLabel = new QLabel(this); - extendvertdivLabel->setGeometry(40, 270, 120, 35); + extendvertdivLabel->setGeometry(40, 320, 120, 35); extendvertdivLabel->setText("Use extended\n vertical range"); extendvertdivCheckbox = new QCheckBox(this); - extendvertdivCheckbox->setGeometry(180, 270, 120, 35); + extendvertdivCheckbox->setGeometry(180, 320, 120, 35); extendvertdivCheckbox->setTristate(false); if(mainwindow->devparms.use_extra_vertdivisions) { @@ -210,7 +226,7 @@ UI_settings_window::UI_settings_window(QWidget *parnt) if(mainwindow->devparms.connected) { usbRadioButton->setEnabled(false); - lanRadioButton->setEnabled(false); + lanIPRadioButton->setEnabled(false); ipSpinbox1->setEnabled(false); ipSpinbox2->setEnabled(false); ipSpinbox3->setEnabled(false); @@ -223,11 +239,12 @@ UI_settings_window::UI_settings_window(QWidget *parnt) QObject::connect(applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked())); } - QObject::connect(cancelButton, SIGNAL(clicked()), this, SLOT(close())); - QObject::connect(refreshSpinbox, SIGNAL(valueChanged(int)), this, SLOT(refreshSpinboxChanged(int))); - QObject::connect(invScrShtCheckbox, SIGNAL(stateChanged(int)), this, SLOT(invScrShtCheckboxChanged(int))); - QObject::connect(showfpsCheckbox, SIGNAL(stateChanged(int)), this, SLOT(showfpsCheckboxChanged(int))); - QObject::connect(extendvertdivCheckbox, SIGNAL(stateChanged(int)), this, SLOT(extendvertdivCheckboxChanged(int))); + QObject::connect(cancelButton, SIGNAL(clicked()), this, SLOT(close())); + QObject::connect(refreshSpinbox, SIGNAL(valueChanged(int)), this, SLOT(refreshSpinboxChanged(int))); + QObject::connect(invScrShtCheckbox, SIGNAL(stateChanged(int)), this, SLOT(invScrShtCheckboxChanged(int))); + QObject::connect(showfpsCheckbox, SIGNAL(stateChanged(int)), this, SLOT(showfpsCheckboxChanged(int))); + QObject::connect(extendvertdivCheckbox, SIGNAL(stateChanged(int)), this, SLOT(extendvertdivCheckboxChanged(int))); + QObject::connect(HostLineEdit, SIGNAL(textEdited(QString)), this, SLOT(hostnamechanged(QString))); exec(); } @@ -268,6 +285,10 @@ void UI_settings_window::applyButtonClicked() settings.setValue("connection/ip", dev_str); + strncpy(mainwindow->devparms.hostname, HostLineEdit->text().toLatin1().data(), 64); + + settings.setValue("connection/hostname", mainwindow->devparms.hostname); + if(invScrShtCheckbox->checkState() == Qt::Checked) { mainwindow->devparms.screenshot_inv = 1; @@ -388,6 +409,55 @@ void UI_settings_window::extendvertdivCheckboxChanged(int state) } +void UI_settings_window::hostnamechanged(QString qstr) +{ + int i, j, len, trunc=0; + + char str[128]; + + strncpy(str, qstr.toLatin1().data(), 63); + + str[63] = 0; + + len = strlen(str); + + for(i=0; i '9') && (str[i] < 'A')) || + ((str[i] > 'Z') && (str[i] < 'a')) || + (str[i] > 'z')) + { + for(j=i; j= 'A') && (str[i] <= 'Z')) + { + str[i] += 32; + + trunc = 1; + } + } + + if(trunc) + { + HostLineEdit->setText(str); + } +} + + diff --git a/settings_dialog.h b/settings_dialog.h index e6080ef..ce1e45d 100644 --- a/settings_dialog.h +++ b/settings_dialog.h @@ -43,6 +43,11 @@ #include #include #include +#include + +#include +#include +#include #include "mainwindow.h" #include "global.h" @@ -65,7 +70,7 @@ QPushButton *cancelButton, *applyButton; QRadioButton *usbRadioButton, - *lanRadioButton; + *lanIPRadioButton; QComboBox *comboBox1; @@ -78,12 +83,15 @@ QSpinBox *refreshSpinbox, QLabel *refreshLabel, *invScrShtLabel, *showfpsLabel, - *extendvertdivLabel; + *extendvertdivLabel, + *hostnameLabel; QCheckBox *invScrShtCheckbox, *showfpsCheckbox, *extendvertdivCheckbox; +QLineEdit *HostLineEdit; + UI_Mainwindow *mainwindow; private slots: @@ -93,6 +101,7 @@ void refreshSpinboxChanged(int); void invScrShtCheckboxChanged(int); void showfpsCheckboxChanged(int); void extendvertdivCheckboxChanged(int); +void hostnamechanged(QString); }; diff --git a/tmc_lan.c b/tmc_lan.c index 155319b..4b8e28a 100644 --- a/tmc_lan.c +++ b/tmc_lan.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "tmc_dev.h" #include "utils.h" @@ -103,10 +104,52 @@ static int tmclan_recv(char *buf, int sz) } -struct tmcdev * tmclan_open(const char *ip_address) +struct tmcdev * tmclan_open(const char *host_or_ip) { + char ip_address[256]={""}; + struct tmcdev *tmc_device; + struct addrinfo *addr_result, *res; + + struct sockaddr_in *ipv4_addr; + + if((host_or_ip[0] >= 'a') && (host_or_ip[0] <= 'z')) + { + printf("Resolving hostname: %s...\n", host_or_ip); + if(getaddrinfo(host_or_ip, NULL, NULL, &addr_result)) + { + printf("Error: getaddrinfo() file: %s line: %i", __FILE__, __LINE__); + return NULL; + } + + for(res=addr_result; res!=NULL; res=res->ai_next) + { + if(res->ai_family == AF_INET) + { + if(res->ai_socktype == SOCK_STREAM) + { + break; + } + } + } + + if(res == NULL) + { + printf("Error: getaddrinfo() file: %s line: %i", __FILE__, __LINE__); + return NULL; + } + + ipv4_addr = (struct sockaddr_in *)res->ai_addr; + inet_ntop(AF_INET, &(ipv4_addr->sin_addr), ip_address, INET_ADDRSTRLEN); + freeaddrinfo(addr_result); + printf("Resolved hostname %s into address: %s\n", host_or_ip, ip_address); + } + else + { + strcpy(ip_address, host_or_ip); + } + sockfd = socket(PF_INET, SOCK_STREAM, 0); if(sockfd == -1) {