Commit 3e373b84 authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Server automatically check changes on usb

 + Server version
parent 93d7974b
......@@ -19,11 +19,17 @@ Rest of DSerial project is NOT released under CC0
List of ports.
Change signal is only emitted upon interaction with server.
Server does not actively check for port changes.
Server checks for changes every second.
-->
<property name='PortList' type='as' access='read'/>
<!--
ServerVersion:
Prints version of server
-->
<property name='ServerVersion' type='s' access='read'/>
<!--
CreateFakePort:
......
......@@ -90,16 +90,21 @@ void Controller::removePort(QDBusObjectPath id)
}
}
QString Controller::ServerVersion() const
{
return "0.5";
}
void Controller::notifyPropertyChanged( const char *propertyName )
{
QDBusMessage signal = QDBusMessage::createSignal(
"/controller",
"org.freedesktop.DBus.Properties",
"PropertiesChanged");
signal << QString("info.skorepa.DSerial.controller");
QVariantMap changedProps;
changedProps.insert(propertyName, property(propertyName));
signal << changedProps;
signal << QStringList();
QDBusConnection::sessionBus().send(signal);
QDBusMessage signal = QDBusMessage::createSignal(
"/controller",
"org.freedesktop.DBus.Properties",
"PropertiesChanged");
signal << QString("info.skorepa.DSerial.controller");
QVariantMap changedProps;
changedProps.insert(propertyName, property(propertyName));
signal << changedProps;
signal << QStringList();
QDBusConnection::sessionBus().send(signal);
}
......@@ -9,33 +9,35 @@
class Controller : public QObject
{
Q_OBJECT
QVector<Port*> ports;
Q_OBJECT
QVector<Port*> ports;
public:
explicit Controller(QObject *parent = 0);
Q_PROPERTY(QStringList PortList READ PortList NOTIFY PortListChanged)
public:
explicit Controller(QObject *parent = 0);
Q_PROPERTY(QStringList PortList READ PortList NOTIFY PortListChanged)
Q_PROPERTY(QString ServerVersion READ ServerVersion)
QStringList PortList() const;
QStringList PortList() const;
public slots:
void CreateFakePort(QString);
void CreateFilePort(QString);
void Autodetect();
public slots:
void CreateFakePort(QString);
void CreateFilePort(QString);
void Autodetect();
QDBusObjectPath GetPath(QString name);
QDBusObjectPath GetPath(QString name);
signals:
void PortListChanged(QList<QDBusObjectPath> PortList);
signals:
void PortListChanged(QList<QDBusObjectPath> PortList);
private slots:
void removePort(QDBusObjectPath id);
private slots:
void removePort(QDBusObjectPath id);
public:
QDBusConnection connection;
public:
QDBusConnection connection;
QString ServerVersion() const;
private:
void notifyPropertyChanged( const char *propertyName );
private:
void notifyPropertyChanged( const char *propertyName );
};
#endif // CONTROLLER_H
......@@ -19,11 +19,15 @@ DBUS_ADAPTORS += ../dserial-protocol/port.xml ../dserial-protocol/controller.xml
SOURCES += main.cpp \
port.cpp \
controller.cpp
controller.cpp \
udevmonitor.cpp
HEADERS += \
port.h \
controller.h
controller.h \
udevmonitor.h
LIBS += -ludev
documentation.path = .
......
......@@ -4,6 +4,7 @@
#include <QLoggingCategory>
#include "controller.h"
#include "controller_adaptor.h"
#include "udevmonitor.h"
void msgHandler( QtMsgType type, const QMessageLogContext& context, const QString& msg )
{
......@@ -29,5 +30,14 @@ int main(int argc, char *argv[])
new ControllerAdaptor(controller);
controller->connection.registerObject("/controller", controller);
controller->connection.registerService("info.skorepa.DSerial1");
QTimer timer;
UdevMonitor mon;
QObject::connect(&timer, &QTimer::timeout,
[&controller,&mon](){
if(mon.checkChange()) controller->Autodetect();
});
timer.setInterval(1000);
timer.start();
return a.exec();
}
#include "udevmonitor.h"
UdevMonitor::UdevMonitor(QObject *parent) : QObject(parent)
{
udev = udev_new();
mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "usb-serial", NULL);
udev_monitor_enable_receiving(mon);
fd = udev_monitor_get_fd(mon);
}
UdevMonitor::~UdevMonitor()
{
udev_unref(udev);
udev_monitor_unref(mon);
}
bool UdevMonitor::checkChange()
{
fd_set fds;
struct timeval tv;
int ret;
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 0;
tv.tv_usec = 0;
ret = select(fd+1, &fds, NULL, NULL, &tv);
/* Check if our file descriptor has received data. */
if (ret > 0 && FD_ISSET(fd, &fds)) {
struct udev_device *dev = udev_monitor_receive_device(mon);
udev_device_unref(dev);
emit changeDetected();
return true;
}
return false;
}
#ifndef UDEVMONITOR_H
#define UDEVMONITOR_H
#include <QObject>
#include <libudev.h>
class UdevMonitor : public QObject
{
Q_OBJECT
public:
explicit UdevMonitor(QObject *parent = 0);
~UdevMonitor();
signals:
void changeDetected();
public slots:
bool checkChange();
private:
struct udev *udev;
struct udev_monitor *mon;
int fd;
};
#endif // UDEVMONITOR_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment