added Lg Smart Tv support (discovery works)

This commit is contained in:
Simon Stürz 2014-08-04 01:21:31 +02:00 committed by Michael Zanetti
parent 22d9bd312f
commit ce54fd166a
11 changed files with 733 additions and 3 deletions

View File

@ -10,7 +10,8 @@ SUBDIRS += elro \
wakeonlan \
mailnotification \
philipshue \
wemo \
lgsmarttv \
boblight {

View File

@ -0,0 +1,146 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "devicepluginlgsmarttv.h"
#include "plugin/device.h"
#include "devicemanager.h"
#include <QDebug>
VendorId lgVendorId = VendorId("a9af9673-78db-4226-a16b-f34b304f7041");
DeviceClassId lgSmartTvDeviceClassId = DeviceClassId("1d41b5a8-74ff-4a12-b365-c7bbe610848f");
DevicePluginLgSmartTv::DevicePluginLgSmartTv()
{
m_discovery = new TvDiscovery(this);
connect(m_discovery,SIGNAL(discoveryDone(QList<TvDevice*>)),this,SLOT(discoveryDone(QList<TvDevice*>)));
}
QList<Vendor> DevicePluginLgSmartTv::supportedVendors() const
{
QList<Vendor> ret;
Vendor lgVendor(lgVendorId, "Lg");
ret.append(lgVendor);
return ret;
}
QList<DeviceClass> DevicePluginLgSmartTv::supportedDevices() const
{
QList<DeviceClass> ret;
DeviceClass deviceClassLgSmartTv(pluginId(), lgVendorId, lgSmartTvDeviceClassId);
deviceClassLgSmartTv.setName("Lg Smart Tv");
deviceClassLgSmartTv.setCreateMethod(DeviceClass::CreateMethodDiscovery);
deviceClassLgSmartTv.setSetupMethod(DeviceClass::SetupMethodEnterPin);
QList<ParamType> paramTypes;
paramTypes.append(ParamType("name", QVariant::String));
paramTypes.append(ParamType("uuid", QVariant::String));
paramTypes.append(ParamType("model", QVariant::String));
paramTypes.append(ParamType("host address", QVariant::String));
paramTypes.append(ParamType("location", QVariant::String));
paramTypes.append(ParamType("manufacturer", QVariant::String));
deviceClassLgSmartTv.setParamTypes(paramTypes);
ret.append(deviceClassLgSmartTv);
return ret;
}
QPair<DeviceManager::DeviceError, QString> DevicePluginLgSmartTv::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{
qDebug() << "should discover devices with params:" << params;
if(deviceClassId != lgSmartTvDeviceClassId){
return report(DeviceManager::DeviceErrorDeviceClassNotFound);
}
m_discovery->discover(2000);
return report(DeviceManager::DeviceErrorAsync);
}
QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginLgSmartTv::setupDevice(Device *device)
{
return reportDeviceSetup();
}
DeviceManager::HardwareResources DevicePluginLgSmartTv::requiredHardware() const
{
return DeviceManager::HardwareResourceTimer;
}
QPair<DeviceManager::DeviceError, QString> DevicePluginLgSmartTv::executeAction(Device *device, const Action &action)
{
return report();
}
QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginLgSmartTv::confirmPairing(const QUuid &pairingTransactionId, const DeviceClassId &deviceClassId, const ParamList &params)
{
foreach (const Param &param, params) {
qDebug() << "confirm pairing param" << param.name();
}
return reportDeviceSetup(DeviceManager::DeviceSetupStatusAsync);
}
QString DevicePluginLgSmartTv::pluginName() const
{
return "Lg Smart Tv";
}
PluginId DevicePluginLgSmartTv::pluginId() const
{
return PluginId("4ef7a68b-9da0-4c62-b9ac-f478dc6f9f52");
}
void DevicePluginLgSmartTv::guhTimer()
{
}
void DevicePluginLgSmartTv::discoveryDone(QList<TvDevice*> tvList)
{
QList<DeviceDescriptor> deviceDescriptors;
foreach (TvDevice *device, tvList) {
DeviceDescriptor descriptor(lgSmartTvDeviceClassId, "Lg Smart Tv", device->modelName());
ParamList params;
params.append(Param("name", device->name()));
params.append(Param("uuid", device->uuid()));
params.append(Param("model", device->modelName()));
params.append(Param("host address", device->hostAddress().toString()));
params.append(Param("location", device->location().toString()));
params.append(Param("manufacturer", device->manufacturer()));
descriptor.setParams(params);
deviceDescriptors.append(descriptor);
}
emit devicesDiscovered(lgSmartTvDeviceClassId, deviceDescriptors);
}

View File

@ -0,0 +1,60 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef DEVICEPLUGINLGSMARTTV_H
#define DEVICEPLUGINLGSMARTTV_H
#include "plugin/deviceplugin.h"
#include "tvdiscovery.h"
class DevicePluginLgSmartTv : public DevicePlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "guru.guh.DevicePlugin" FILE "devicepluginlgsmarttv.json")
Q_INTERFACES(DevicePlugin)
public:
explicit DevicePluginLgSmartTv();
TvDiscovery *m_discovery;
QList<Vendor> supportedVendors() const override;
QList<DeviceClass> supportedDevices() const override;
QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device) override;
DeviceManager::HardwareResources requiredHardware() const override;
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
QPair<DeviceManager::DeviceSetupStatus, QString> confirmPairing(const QUuid &pairingTransactionId, const DeviceClassId &deviceClassId, const ParamList &params) override;
QString pluginName() const override;
PluginId pluginId() const override;
void guhTimer() override;
private slots:
void discoveryDone(QList<TvDevice *> tvList);
public slots:
};
#endif // DEVICEPLUGINLGSMARTTV_H

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,17 @@
include(../../plugins.pri)
TARGET = $$qtLibraryTarget(guh_devicepluginlgsmarttv)
QT+= network
SOURCES += \
devicepluginlgsmarttv.cpp \
tvdiscovery.cpp \
tvdevice.cpp
HEADERS += \
devicepluginlgsmarttv.h \
tvdiscovery.h \
tvdevice.h

View File

@ -0,0 +1,110 @@
#include "tvdevice.h"
TvDevice::TvDevice(QObject *parent) :
QObject(parent)
{
m_manager = new QNetworkAccessManager(this);
}
void TvDevice::setLocation(const QUrl &location)
{
m_location = location;
}
QUrl TvDevice::location() const
{
return m_location;
}
void TvDevice::setHostAddress(const QHostAddress &hostAddress)
{
m_hostAddress = hostAddress;
}
QHostAddress TvDevice::hostAddress() const
{
return m_hostAddress;
}
void TvDevice::setName(const QString &name)
{
m_name = name;
}
QString TvDevice::name() const
{
return m_name;
}
void TvDevice::setModelName(const QString &modelName)
{
m_modelName = modelName;
}
QString TvDevice::modelName() const
{
return m_modelName;
}
void TvDevice::setManufacturer(const QString &manufacturer)
{
m_manufacturer = manufacturer;
}
QString TvDevice::manufacturer() const
{
return m_manufacturer;
}
void TvDevice::setDeviceType(const QString &deviceType)
{
m_deviceType = deviceType;
}
QString TvDevice::deviceType() const
{
return m_deviceType;
}
void TvDevice::setUuid(const QString &uuid)
{
m_uuid = uuid;
}
QString TvDevice::uuid() const
{
return m_uuid;
}
void TvDevice::setKey(const QString &key)
{
m_key = key;
}
QString TvDevice::key() const
{
return m_key;
}
void TvDevice::showPairingKey()
{
qDebug() << "request show pairing key on screen...";
qDebug() << "--------------------------------------------";
QString urlString = "http://" + m_hostAddress.toString() + ":8080/udap/api/pairing";
QNetworkRequest pairingRequest;
pairingRequest.setUrl(QUrl(urlString));
pairingRequest.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml; charset=utf-8"));
pairingRequest.setHeader(QNetworkRequest::UserAgentHeader,QVariant("UDAP/2.0"));
QByteArray data = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envelope><api type=\"pairing\"> <name>showKey</name></api></envelope>";
m_showKeyReplay = m_manager->post(pairingRequest,data);
}

View File

@ -0,0 +1,71 @@
#ifndef TVDEVICE_H
#define TVDEVICE_H
#include <QObject>
#include <QUrl>
#include <QHostAddress>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QXmlStreamAttributes>
class TvDevice : public QObject
{
Q_OBJECT
public:
explicit TvDevice(QObject *parent = 0);
void setLocation(const QUrl &location);
QUrl location() const;
void setHostAddress(const QHostAddress &hostAddress);
QHostAddress hostAddress() const;
void setName(const QString &name);
QString name() const;
void setModelName(const QString &modelName);
QString modelName() const;
void setManufacturer(const QString &manufacturer);
QString manufacturer() const;
void setDeviceType(const QString &deviceType);
QString deviceType() const;
void setUuid(const QString &uuid);
QString uuid() const;
void setKey(const QString &key);
QString key() const;
void showPairingKey();
private:
QUrl m_location;
QHostAddress m_hostAddress;
QString m_name;
QString m_modelName;
QString m_manufacturer;
QString m_deviceType;
QString m_uuid;
QString m_key;
bool m_pairingStatus;
QNetworkAccessManager *m_manager;
QNetworkReply *m_showKeyReplay;
signals:
public slots:
};
#endif // TVDEVICE_H

View File

@ -0,0 +1,270 @@
#include "tvdiscovery.h"
TvDiscovery::TvDiscovery(QObject *parent) :
QUdpSocket(parent)
{
m_timeout = new QTimer(this);
m_timeout->setSingleShot(true);
connect(m_timeout,SIGNAL(timeout()),this,SLOT(discoverTimeout()));
m_manager = new QNetworkAccessManager(this);
connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
m_port = 1900;
m_host = QHostAddress("239.255.255.250");
setSocketOption(QAbstractSocket::MulticastTtlOption,QVariant(1));
setSocketOption(QAbstractSocket::MulticastLoopbackOption,QVariant(1));
bind(QHostAddress::AnyIPv4,m_port,QUdpSocket::ShareAddress);
joinMulticastGroup(m_host);
connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError)));
connect(this,SIGNAL(readyRead()),this,SLOT(readData()));
}
bool TvDiscovery::checkXmlData(QByteArray data)
{
QByteArray xmlOut;
QXmlStreamReader reader(data);
QXmlStreamWriter writer(&xmlOut);
writer.setAutoFormatting(true);
while (!reader.atEnd()) {
reader.readNext();
if (!reader.isWhitespace()) {
writer.writeCurrentToken(reader);
}
}
if(reader.hasError()){
qDebug() << "ERROR reading XML device information: " << reader.errorString();
qDebug() << "--------------------------------------------";
return false;
}
m_deviceInformationData = xmlOut;
return true;
}
QString TvDiscovery::printXmlData(QByteArray data)
{
QString xmlOut;
QXmlStreamReader reader(data);
QXmlStreamWriter writer(&xmlOut);
writer.setAutoFormatting(true);
while (!reader.atEnd()) {
reader.readNext();
if (!reader.isWhitespace()) {
writer.writeCurrentToken(reader);
}
}
if(reader.hasError()){
qDebug() << "ERROR reading XML device information: " << reader.errorString();
qDebug() << "--------------------------------------------";
}
return xmlOut;
}
void TvDiscovery::error(QAbstractSocket::SocketError error)
{
qWarning() << errorString() << error;
}
void TvDiscovery::readData()
{
QByteArray data;
QHostAddress sender;
quint16 udpPort;
// read the answere from the multicast
while (hasPendingDatagrams()) {
data.resize(pendingDatagramSize());
readDatagram(data.data(), data.size(), &sender, &udpPort);
}
if(data.size() > 0){
if(data.contains("HTTP/1.1 200 OK")){
const QStringList lines = QString(data).split("\r\n");
QUrl location;
QString uuid;
QString server;
foreach( const QString& line, lines){
int separatorIndex = line.indexOf(':');
QString key = line.left(separatorIndex).toUpper();
QString value = line.mid(separatorIndex+1).trimmed();
// get location
if(key.contains("LOCATION")){
location = QUrl(value);
}
// get server info
if(key.contains("SERVER")){
// check if it is a LG Smart Tv with UDAP/2.0 protocoll
if(value.contains("UDAP/2.0")){
server = value;
}
}
// get uuid
if(key.contains("USN")){
int startIndex = value.indexOf(":");
int endIndex = value.indexOf("::");
uuid = value.mid(startIndex +1 ,(endIndex - startIndex)-1);
}
if(!location.isEmpty() && !uuid.isEmpty() && !server.isEmpty()){
foreach (TvDevice *device, m_tvList) {
if(device->uuid() == uuid){
return;
}
}
TvDevice *device = new TvDevice(this);
device->setLocation(location);
device->setHostAddress(sender);
device->setUuid(uuid);
// qDebug() << "--> UPnP searcher discovered a TV...";
// qDebug() << "location: " << location.toString();
// qDebug() << "ip: " << sender.toString();
// qDebug() << "uuid: " << uuid;
// qDebug() << "--------------------------------------------";
m_tvList.append(device);
requestDeviceInformation(device);
}
}
}
}
}
void TvDiscovery::discoverTimeout()
{
emit discoveryDone(m_tvList);
}
void TvDiscovery::requestDeviceInformation(TvDevice *device)
{
QNetworkRequest deviceRequest;
deviceRequest.setUrl(device->location());
deviceRequest.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml"));
deviceRequest.setHeader(QNetworkRequest::UserAgentHeader,QVariant("UDAP/2.0"));
m_deviceInformationReplay = m_manager->get(deviceRequest);
}
void TvDiscovery::replyFinished(QNetworkReply *reply)
{
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QByteArray data;
switch (status) {
case(200):
data = reply->readAll();
if(checkXmlData(data)){
parseDeviceInformation(data);
}
break;
case(400):
qDebug() << "ERROR: 400 Bad request. The event format is not valid or it has an incorrect value.";
qDebug() << "--------------------------------------------";
return;
case(401):
qDebug() << "ERROR: 401 Unauthorized. An event is sent when a Host and a Controller are not paired.";
qDebug() << "--------------------------------------------";
return;
case(404):
qDebug() << "ERROR: 404 Not Found. The POST path of an event is incorrect.";
qDebug() << "--------------------------------------------";
return;
case(500):
qDebug() << "ERROR: 500 Internal Server Error. Event Execution Failure.";
qDebug() << "--------------------------------------------";
return;
default:
return;
}
}
void TvDiscovery::parseDeviceInformation(QByteArray data)
{
QXmlStreamReader xml(data);
QString name;
QString uuid;
QString modelName;
QString deviceType;
QString manufacturer;
while(!xml.atEnd() && !xml.hasError()){
xml.readNext();
if(xml.isStartDocument()){
continue;
}
if(xml.isStartElement()){
if(xml.name() == "envelope"){
continue;
}
//check if we have device part of message
if(xml.name() == "device"){
// seems to be device information
while(!xml.atEnd()){
if(xml.name() == "deviceType" && xml.isStartElement()){
deviceType = xml.readElementText();
}
if(xml.name() == "modelName" && xml.isStartElement()){
modelName = xml.readElementText();
}
if(xml.name() == "friendlyName" && xml.isStartElement()){
name = xml.readElementText();
}
if(xml.name() == "manufacturer" && xml.isStartElement()){
manufacturer = xml.readElementText();
}
if(xml.name() == "uuid" && xml.isStartElement()){
uuid = xml.readElementText();
}
xml.readNext();
}
}
}
}
foreach (TvDevice *device, m_tvList) {
// find our device with this uuid
if(device->uuid() == uuid){
device->setName(name);
device->setModelName(modelName);
device->setDeviceType(deviceType);
device->setManufacturer(manufacturer);
qDebug() << "--> fetched TV information...";
qDebug() << "name: " << device->name();
qDebug() << "model name: " << device->modelName();
qDebug() << "device type: " << device->deviceType();
qDebug() << "manufacturer: " << device->manufacturer();
qDebug() << "address: " << device->hostAddress().toString();
qDebug() << "location: " << device->location().toString();
qDebug() << "uuid: " << device->uuid();
qDebug() << "--------------------------------------------";
}
}
}
void TvDiscovery::discover(int timeout)
{
QString searchMessage("M-SEARCH * HTTP/1.1\r\n"
"HOST: 239.255.255.250:1900\r\n"
"MAN: \"ssdp:discover\"\r\n"
"MX: 3\r\n"
"ST: udap:rootservice\r\n"
"USER-AGENT: UDAP/2.0 \r\n\r\n");
m_tvList.clear();
writeDatagram(searchMessage.toUtf8(),m_host,m_port);
writeDatagram(searchMessage.toUtf8(),m_host,m_port);
m_timeout->start(timeout);
}

View File

@ -0,0 +1,54 @@
#ifndef TVDISCOVERY_H
#define TVDISCOVERY_H
#include <QUdpSocket>
#include <QHostAddress>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QXmlStreamAttributes>
#include "tvdevice.h"
class TvDiscovery : public QUdpSocket
{
Q_OBJECT
public:
explicit TvDiscovery(QObject *parent = 0);
private:
QHostAddress m_host;
qint16 m_port;
QTimer *m_timeout;
QList<TvDevice*> m_tvList;
QNetworkAccessManager *m_manager;
QNetworkReply *m_deviceInformationReplay;
QByteArray m_deviceInformationData;
bool checkXmlData(QByteArray data);
QString printXmlData(QByteArray data);
signals:
void discoveryDone(const QList<TvDevice*> deviceList);
private slots:
void error(QAbstractSocket::SocketError error);
void readData();
void discoverTimeout();
void requestDeviceInformation(TvDevice *device);
void replyFinished(QNetworkReply *reply);
void parseDeviceInformation(QByteArray data);
public slots:
void discover(int timeout);
};
#endif // TVDISCOVERY_H

View File

@ -32,7 +32,7 @@ Q_IMPORT_PLUGIN(DevicePluginLircd)
Q_IMPORT_PLUGIN(DevicePluginWakeOnLan)
Q_IMPORT_PLUGIN(DevicePluginMailNotification)
Q_IMPORT_PLUGIN(DevicePluginPhilipsHue)
Q_IMPORT_PLUGIN(DevicePluginWemo)
Q_IMPORT_PLUGIN(DevicePluginLgSmartTv)
#if USE_BOBLIGHT
Q_IMPORT_PLUGIN(DevicePluginBoblight)

View File

@ -30,7 +30,7 @@ LIBS += -L../plugins/deviceplugins/lircd -lguh_devicepluginlircd
LIBS += -L../plugins/deviceplugins/mailnotification -lguh_devicepluginmailnotification
LIBS += -L../plugins/deviceplugins/wakeonlan -lguh_devicepluginwakeonlan
LIBS += -L../plugins/deviceplugins/philipshue -lguh_devicepluginphilipshue
LIBS += -L../plugins/deviceplugins/wemo -lguh_devicepluginwemo
LIBS += -L../plugins/deviceplugins/lgsmarttv -lguh_devicepluginlgsmarttv
boblight {
xcompile {