fix plugin and add some documentation

This commit is contained in:
Simon Stürz 2016-05-04 18:51:54 +02:00 committed by Michael Zanetti
parent fb91f05b15
commit cddb23182f
7 changed files with 117 additions and 20 deletions

View File

@ -1,5 +1,5 @@
usr/lib/guh/plugins/libguh_devicepluginavahimonitor.so
usr/lib/guh/plugins/libguh_devicepluginlircd.so
usr/lib/guh/plugins/libguh_devicepluginwifidetector.so
usr/lib/guh/plugins/libguh_deviceplugincommandlauncher.so
usr/lib/guh/plugins/libguh_devicepluginudpcommander.so
usr/lib/guh/plugins/libguh_devicepluginavahimonitor.so

View File

@ -18,8 +18,18 @@
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*!
\class AvahiServiceEntry
\brief Holds information about an avahi service entry.
\ingroup types
\inmodule libguh
*/
#include "avahiserviceentry.h"
/*! Constructs an empty invalid \l{AvahiServiceEntry}*/
AvahiServiceEntry::AvahiServiceEntry() :
m_port(0),
m_protocol(QAbstractSocket::UnknownNetworkLayerProtocol)
@ -27,6 +37,8 @@ AvahiServiceEntry::AvahiServiceEntry() :
}
/*! Constructs a new \l{AvahiServiceEntry} with the given \a name, \a hostAddress, \a domain, \a hostName, \a port, \a protocol, \a txt and \a flags.*/
AvahiServiceEntry::AvahiServiceEntry(QString name, QString serviceType, QHostAddress hostAddress, QString domain, QString hostName, quint16 port, QAbstractSocket::NetworkLayerProtocol protocol, QStringList txt, AvahiLookupResultFlags flags) :
m_name(name),
m_serviceType(serviceType),
@ -41,81 +53,97 @@ AvahiServiceEntry::AvahiServiceEntry(QString name, QString serviceType, QHostAdd
}
/*! Returns the name of this \l{AvahiServiceEntry}.*/
QString AvahiServiceEntry::name() const
{
return m_name;
}
/*! Returns the service type of this \l{AvahiServiceEntry}.*/
QString AvahiServiceEntry::serviceType() const
{
return m_serviceType;
}
/*! Returns the host address of this \l{AvahiServiceEntry}.*/
QHostAddress AvahiServiceEntry::hostAddress() const
{
return m_hostAddress;
}
/*! Returns the domain of this \l{AvahiServiceEntry}.*/
QString AvahiServiceEntry::domain() const
{
return m_domain;
}
/*! Returns the host name of this \l{AvahiServiceEntry}.*/
QString AvahiServiceEntry::hostName() const
{
return m_hostName;
}
/*! Returns the port of this \l{AvahiServiceEntry}.*/
quint16 AvahiServiceEntry::port() const
{
return m_port;
}
/*! Returns the network protocol of this \l{AvahiServiceEntry}.*/
QAbstractSocket::NetworkLayerProtocol AvahiServiceEntry::protocol() const
{
return m_protocol;
}
/*! Returns the avahi flags of this \l{AvahiServiceEntry}.*/
AvahiLookupResultFlags AvahiServiceEntry::flags() const
{
return m_flags;
}
/*! Returns the txt string list of this \l{AvahiServiceEntry}.*/
QStringList AvahiServiceEntry::txt() const
{
return m_txt;
}
/*! Returns true if this \l{AvahiServiceEntry} is valid.*/
bool AvahiServiceEntry::isValid() const
{
return !m_hostAddress.isNull() && !m_hostName.isEmpty() && m_port != 0 && m_protocol != QAbstractSocket::UnknownNetworkLayerProtocol;
}
/*! Returns true if this \l{AvahiServiceEntry} is cached.*/
bool AvahiServiceEntry::isChached() const
{
return m_flags & AVAHI_LOOKUP_RESULT_CACHED;
}
/*! Returns true if this \l{AvahiServiceEntry} was found in the wide area.*/
bool AvahiServiceEntry::isWideArea() const
{
return m_flags & AVAHI_LOOKUP_RESULT_WIDE_AREA;
}
/*! Returns true if this \l{AvahiServiceEntry} is a multicast service.*/
bool AvahiServiceEntry::isMulticast() const
{
return m_flags & AVAHI_LOOKUP_RESULT_MULTICAST;
}
/*! Returns true if this \l{AvahiServiceEntry} was found local.*/
bool AvahiServiceEntry::isLocal() const
{
return m_flags & AVAHI_LOOKUP_RESULT_LOCAL;
}
/*! Returns true if this \l{AvahiServiceEntry} is our own service.*/
bool AvahiServiceEntry::isOurOwn() const
{
return m_flags & AVAHI_LOOKUP_RESULT_OUR_OWN;
}
/*! Returns true if this \l{AvahiServiceEntry} is equal to \a other; otherwise returns false.*/
bool AvahiServiceEntry::operator ==(const AvahiServiceEntry &other) const
{
return other.name() == m_name &&
@ -129,12 +157,13 @@ bool AvahiServiceEntry::operator ==(const AvahiServiceEntry &other) const
other.txt() == m_txt;
}
/*! Returns true if this \l{AvahiServiceEntry} is not equal to \a other; otherwise returns false.*/
bool AvahiServiceEntry::operator !=(const AvahiServiceEntry &other) const
{
return !operator==(other);
}
/*! Writes the given \a entry to the specified \a dbg.*/
QDebug operator <<(QDebug dbg, const AvahiServiceEntry &entry)
{
dbg.nospace() << "AvahiServiceEntry(";

View File

@ -18,9 +18,38 @@
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*!
\class QtAvahiService
\brief Allowes to publish an avahi service to the network.
\inmodule libguh
*/
/*! \enum QtAvahiService::QtAvahiServiceState
This enum type specifies the state of a \l{QtAvahiService}.
\value QtAvahiServiceStateUncomitted
The group has not yet been commited, the user must still call avahi_entry_group_commit().
\value QtAvahiServiceStateRegistering
The entries of the group are currently being registered.
\value QtAvahiServiceStateEstablished
The entries have successfully been established.
\value QtAvahiServiceStateCollision
A name collision for one of the entries in the group has been detected, the entries have been withdrawn.
\value QtAvahiServiceStateFailure
Some kind of failure happened, the entries have been withdrawn.
*/
/*! \fn void QtAvahiService::serviceStateChanged(const QtAvahiServiceState &state);
This signal will be emitted when the \a state of this \l{QtAvahiService} has changed.
*/
#include "qtavahiservice.h"
#include "qtavahiservice_p.h"
/*! Constructs a new \l{QtAvahiService} with the given \a parent. */
QtAvahiService::QtAvahiService(QObject *parent) :
QObject(parent),
d_ptr(new QtAvahiServicePrivate)
@ -29,6 +58,7 @@ QtAvahiService::QtAvahiService(QObject *parent) :
d_ptr->client->start();
}
/*! Destructs this \l{QtAvahiService}. */
QtAvahiService::~QtAvahiService()
{
if (d_ptr->group)
@ -37,22 +67,26 @@ QtAvahiService::~QtAvahiService()
delete d_ptr;
}
/*! Returns the port of this \l{QtAvahiService}. */
quint16 QtAvahiService::port() const
{
return d_ptr->port;
}
/*! Returns the name of this \l{QtAvahiService}. */
QString QtAvahiService::name() const
{
return d_ptr->name;
}
/*! Returns the service type of this \l{QtAvahiService}. */
QString QtAvahiService::serviceType() const
{
return d_ptr->type;
}
bool QtAvahiService::registerService(QString name, quint16 port, QString type)
/*! Returns true if a new avahi service to the network with the given \a name, \a port and \a serviceType can be registered. */
bool QtAvahiService::registerService(QString name, quint16 port, QString serviceType)
{
// check if the client is running
if (!d_ptr->client->client || AVAHI_CLIENT_S_RUNNING != avahi_client_get_state(d_ptr->client->client))
@ -60,7 +94,7 @@ bool QtAvahiService::registerService(QString name, quint16 port, QString type)
d_ptr->name = name;
d_ptr->port = port;
d_ptr->type = type;
d_ptr->type = serviceType;
// if the group is not set yet, create it
if (!d_ptr->group)
@ -93,16 +127,22 @@ bool QtAvahiService::registerService(QString name, quint16 port, QString type)
return true;
}
/*! Remove this service from the local network. This \l{QtAvahiService} can be reused to register a new avahi service.
\sa registerService()
*/
void QtAvahiService::resetService()
{
avahi_entry_group_reset(d_ptr->group);
}
/*! Returns true if the service group was added and commited to the network without errors. */
bool QtAvahiService::isValid() const
{
return (d_ptr->group && !d_ptr->error);
}
/*! Returns the error string of this \l{QtAvahiService}. */
QString QtAvahiService::errorString() const
{
if (!d_ptr->client->client)

View File

@ -48,7 +48,7 @@ public:
QString name() const;
QString serviceType() const;
bool registerService(QString name, quint16 port, QString type = "_http._tcp");
bool registerService(QString name, quint16 port, QString serviceType = "_http._tcp");
void resetService();
bool isValid() const;

View File

@ -20,16 +20,12 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*!
\page wifidetector.html
\title WiFi Detector
\brief Plugin to monitor devices in the local network.
\page avahimonitor.html
\title Avahi Monitor
\brief Plugin to monitor zeroconf devices in the local network.
\ingroup plugins
\ingroup guh-plugins
This plugin allows to find and monitor network devices in your local network by using the MAC address.
\underline{NOTE}: the application \c nmap has to be installed and guh has to run as root.
\ingroup guh-plugins-maker
\chapter Plugin properties
Following JSON file contains the definition and the description of all available \l{DeviceClass}{DeviceClasses}
@ -37,7 +33,7 @@
For more details how to read this JSON file please check out the documentation for \l{The plugin JSON File}.
\quotefile plugins/deviceplugins/wifidetector/devicepluginwifidetector.json
\quotefile plugins/deviceplugins/avahimonitor/devicepluginavahimonitor.json
*/
@ -53,15 +49,19 @@
DevicePluginAvahiMonitor::DevicePluginAvahiMonitor()
{
}
void DevicePluginAvahiMonitor::init()
{
connect(avahiServiceBrowser(), SIGNAL(serviceEntryAdded(AvahiServiceEntry)), this, SLOT(onServiceEntryAdded(AvahiServiceEntry)));
connect(avahiServiceBrowser(), SIGNAL(serviceEntryRemoved(AvahiServiceEntry)), this, SLOT(onServiceEntryRemoved(AvahiServiceEntry)));
}
DeviceManager::DeviceSetupStatus DevicePluginAvahiMonitor::setupDevice(Device *device)
{
qCDebug(dcAvahiMonitor()) << "Setup" << device->name() << device->params();
connect(avahiServiceBrowser(), SIGNAL(serviceEntryAdded(AvahiServiceEntry)), this, SLOT(onServiceEntryAdded(AvahiServiceEntry)));
connect(avahiServiceBrowser(), SIGNAL(serviceEntryRemoved(AvahiServiceEntry)), this, SLOT(onServiceEntryRemoved(AvahiServiceEntry)));
return DeviceManager::DeviceSetupStatusSuccess;
}
@ -75,6 +75,10 @@ DeviceManager::DeviceError DevicePluginAvahiMonitor::discoverDevices(const Devic
QList<DeviceDescriptor> deviceDescriptors;
foreach (const AvahiServiceEntry &service, avahiServiceBrowser()->serviceEntries()) {
DeviceDescriptor deviceDescriptor(avahiDeviceClassId, service.name(), service.hostAddress().toString());
ParamList params;
params.append(Param("service name", service.name()));
params.append(Param("host name", service.hostName()));
deviceDescriptor.setParams(params);
deviceDescriptors.append(deviceDescriptor);
}
@ -90,10 +94,18 @@ DeviceManager::HardwareResources DevicePluginAvahiMonitor::requiredHardware() co
void DevicePluginAvahiMonitor::onServiceEntryAdded(const AvahiServiceEntry &serviceEntry)
{
Q_UNUSED(serviceEntry)
foreach (Device *device, myDevices()) {
if (device->paramValue("service name").toString() == serviceEntry.name()) {
device->setStateValue(onlineStateTypeId, true);
}
}
}
void DevicePluginAvahiMonitor::onServiceEntryRemoved(const AvahiServiceEntry &serviceEntry)
{
Q_UNUSED(serviceEntry)
foreach (Device *device, myDevices()) {
if (device->paramValue("service name").toString() == serviceEntry.name()) {
device->setStateValue(onlineStateTypeId, false);
}
}
}

View File

@ -36,6 +36,8 @@ class DevicePluginAvahiMonitor : public DevicePlugin
public:
explicit DevicePluginAvahiMonitor();
void init() override;
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
DeviceManager::HardwareResources requiredHardware() const override;

View File

@ -20,9 +20,23 @@
"createMethods": ["discovery"],
"paramTypes": [
{
"name": "name",
"name": "service name",
"type": "QString",
"inputType": "TextLine"
},
{
"name": "host name",
"type": "QString",
"inputType": "TextLine"
}
],
"stateTypes": [
{
"id": "b5616fd3-da12-4613-9576-6516b2267180",
"name": "online",
"idName": "online",
"type": "bool",
"defaultValue": false
}
]
}