mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
LED-Device updates (#1315)
* SPI Fix - Free allocated memory in case of invert * Remove development statement * UI updates * Minor corrections * Tpm2net Fix - Free allocated memory * LGTM Finding * Add config directory and user type to SysInfo * WS281x - Ensure that device runs with root priviliges * WS281x - Update DMA defauls as per rpi_ws281x recommendation * Support Sysinfo for Qt < 5.10 * Address LGTM finding
This commit is contained in:
@@ -328,6 +328,7 @@ void JsonAPI::handleSysInfoCommand(const QJsonObject &, const QString &command,
|
||||
system["prettyName"] = data.prettyName;
|
||||
system["hostName"] = data.hostName;
|
||||
system["domainName"] = data.domainName;
|
||||
system["isUserAdmin"] = data.isUserAdmin;
|
||||
system["qtVersion"] = data.qtVersion;
|
||||
system["pyVersion"] = data.pyVersion;
|
||||
info["system"] = system;
|
||||
@@ -338,6 +339,7 @@ void JsonAPI::handleSysInfoCommand(const QJsonObject &, const QString &command,
|
||||
hyperion["gitremote"] = QString(HYPERION_GIT_REMOTE);
|
||||
hyperion["time"] = QString(__DATE__ " " __TIME__);
|
||||
hyperion["id"] = _authManager->getID();
|
||||
hyperion["rootPath"] = _instanceManager->getRootPath();
|
||||
hyperion["readOnlyMode"] = _hyperion->getReadOnlyMode();
|
||||
|
||||
info["hyperion"] = hyperion;
|
||||
|
@@ -1275,8 +1275,6 @@ QJsonArray V4L2Grabber::discover(const QJsonObject& params)
|
||||
device["device_name"] = _deviceProperties.value(it.key()).name;
|
||||
device["type"] = "v4l2";
|
||||
|
||||
Debug( _log, "inputs size [%d], isEmpty [%d]", inputs.size(), inputs.isEmpty());
|
||||
|
||||
for (auto input = inputs.begin(); input != inputs.end(); input++)
|
||||
{
|
||||
in["name"] = input.key();
|
||||
|
@@ -4,9 +4,15 @@ const ushort TPM2_DEFAULT_PORT = 65506;
|
||||
|
||||
LedDeviceTpm2net::LedDeviceTpm2net(const QJsonObject &deviceConfig)
|
||||
: ProviderUdp(deviceConfig)
|
||||
, _tpm2_buffer(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
LedDeviceTpm2net::~LedDeviceTpm2net()
|
||||
{
|
||||
free (_tpm2_buffer);
|
||||
}
|
||||
|
||||
LedDevice* LedDeviceTpm2net::construct(const QJsonObject &deviceConfig)
|
||||
{
|
||||
return new LedDeviceTpm2net(deviceConfig);
|
||||
@@ -25,6 +31,8 @@ bool LedDeviceTpm2net::init(const QJsonObject &deviceConfig)
|
||||
_tpm2ByteCount = 3 * _ledCount;
|
||||
_tpm2TotalPackets = (_tpm2ByteCount / _tpm2_max) + ((_tpm2ByteCount % _tpm2_max) != 0);
|
||||
|
||||
_tpm2_buffer = (uint8_t*) malloc(_tpm2_max+7);
|
||||
|
||||
isInitOK = true;
|
||||
}
|
||||
return isInitOK;
|
||||
@@ -32,8 +40,6 @@ bool LedDeviceTpm2net::init(const QJsonObject &deviceConfig)
|
||||
|
||||
int LedDeviceTpm2net::write(const std::vector<ColorRgb> &ledValues)
|
||||
{
|
||||
uint8_t * tpm2_buffer = (uint8_t*) malloc(_tpm2_max+7);
|
||||
|
||||
int retVal = 0;
|
||||
|
||||
int _thisPacketBytes = 0;
|
||||
@@ -48,23 +54,22 @@ int LedDeviceTpm2net::write(const std::vector<ColorRgb> &ledValues)
|
||||
_thisPacketBytes = (_tpm2ByteCount - rawIdx < _tpm2_max) ? _tpm2ByteCount % _tpm2_max : _tpm2_max;
|
||||
// is this the last packet? ? ^^ last packet : ^^ earlier packets
|
||||
|
||||
tpm2_buffer[0] = 0x9c; // Packet start byte
|
||||
tpm2_buffer[1] = 0xda; // Packet type Data frame
|
||||
tpm2_buffer[2] = (_thisPacketBytes >> 8) & 0xff; // Frame size high
|
||||
tpm2_buffer[3] = _thisPacketBytes & 0xff; // Frame size low
|
||||
tpm2_buffer[4] = _tpm2ThisPacket++; // Packet Number
|
||||
tpm2_buffer[5] = _tpm2TotalPackets; // Number of packets
|
||||
_tpm2_buffer[0] = 0x9c; // Packet start byte
|
||||
_tpm2_buffer[1] = 0xda; // Packet type Data frame
|
||||
_tpm2_buffer[2] = (_thisPacketBytes >> 8) & 0xff; // Frame size high
|
||||
_tpm2_buffer[3] = _thisPacketBytes & 0xff; // Frame size low
|
||||
_tpm2_buffer[4] = _tpm2ThisPacket++; // Packet Number
|
||||
_tpm2_buffer[5] = _tpm2TotalPackets; // Number of packets
|
||||
}
|
||||
|
||||
tpm2_buffer [6 + rawIdx%_tpm2_max] = rawdata[rawIdx];
|
||||
_tpm2_buffer [6 + rawIdx%_tpm2_max] = rawdata[rawIdx];
|
||||
|
||||
// is this the last byte of last packet || last byte of other packets
|
||||
if ( (rawIdx == _tpm2ByteCount-1) || (rawIdx %_tpm2_max == _tpm2_max-1) )
|
||||
{
|
||||
tpm2_buffer [6 + rawIdx%_tpm2_max +1] = 0x36; // Packet end byte
|
||||
retVal &= writeBytes(_thisPacketBytes+7, tpm2_buffer);
|
||||
_tpm2_buffer [6 + rawIdx%_tpm2_max +1] = 0x36; // Packet end byte
|
||||
retVal &= writeBytes(_thisPacketBytes+7, _tpm2_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
@@ -18,6 +18,11 @@ public:
|
||||
///
|
||||
explicit LedDeviceTpm2net(const QJsonObject &deviceConfig);
|
||||
|
||||
///
|
||||
/// @brief Destructor of the TPM2 LED-device
|
||||
///
|
||||
~LedDeviceTpm2net() override;
|
||||
|
||||
///
|
||||
/// @brief Constructs the LED-device
|
||||
///
|
||||
@@ -48,6 +53,8 @@ private:
|
||||
int _tpm2ByteCount;
|
||||
int _tpm2TotalPackets;
|
||||
int _tpm2ThisPacket;
|
||||
|
||||
uint8_t * _tpm2_buffer;
|
||||
};
|
||||
|
||||
#endif // LEDEVICETPM2NET_H
|
||||
|
@@ -1,4 +1,10 @@
|
||||
#include "LedDeviceWS281x.h"
|
||||
#include <utils/SysInfo.h>
|
||||
|
||||
// Constants
|
||||
namespace {
|
||||
const bool verbose = false;
|
||||
} //End of constants
|
||||
|
||||
LedDeviceWS281x::LedDeviceWS281x(const QJsonObject &deviceConfig)
|
||||
: LedDevice(deviceConfig)
|
||||
@@ -75,19 +81,26 @@ int LedDeviceWS281x::open()
|
||||
int retval = -1;
|
||||
_isDeviceReady = false;
|
||||
|
||||
// Try to open the LedDevice
|
||||
|
||||
ws2811_return_t rc = ws2811_init(&_led_string);
|
||||
if ( rc != WS2811_SUCCESS )
|
||||
if (!SysInfo::isUserAdmin())
|
||||
{
|
||||
QString errortext = QString ("Failed to open. Error message: %1").arg( ws2811_get_return_t_str(rc) );
|
||||
QString errortext = QString ("Hyperion must run with \"root\" privileges for this device. Current user is: \"%1\"").arg(SysInfo::userName());
|
||||
this->setInError( errortext );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Everything is OK, device is ready
|
||||
_isDeviceReady = true;
|
||||
retval = 0;
|
||||
// Try to open the LedDevice
|
||||
ws2811_return_t rc = ws2811_init(&_led_string);
|
||||
if ( rc != WS2811_SUCCESS )
|
||||
{
|
||||
QString errortext = QString ("Failed to open. Error message: %1").arg( ws2811_get_return_t_str(rc) );
|
||||
this->setInError( errortext );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Everything is OK, device is ready
|
||||
_isDeviceReady = true;
|
||||
retval = 0;
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
@@ -138,3 +151,22 @@ int LedDeviceWS281x::write(const std::vector<ColorRgb> &ledValues)
|
||||
|
||||
return ws2811_render(&_led_string) ? -1 : 0;
|
||||
}
|
||||
|
||||
QJsonObject LedDeviceWS281x::discover(const QJsonObject& /*params*/)
|
||||
{
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
|
||||
|
||||
QJsonArray deviceList;
|
||||
|
||||
if (SysInfo::isUserAdmin())
|
||||
{
|
||||
//Indicate the general availability of the device, if hyperion is run under root
|
||||
deviceList << QJsonObject ({{"found",true}});
|
||||
devicesDiscovered.insert("devices", deviceList);
|
||||
}
|
||||
|
||||
DebugIf(verbose,_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
return devicesDiscovered;
|
||||
}
|
||||
|
@@ -29,6 +29,15 @@ public:
|
||||
///
|
||||
static LedDevice* construct(const QJsonObject &deviceConfig);
|
||||
|
||||
///
|
||||
/// @brief Discover WS281x devices available (for configuration).
|
||||
///
|
||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
||||
///
|
||||
/// @return A JSON structure holding a list of devices found
|
||||
///
|
||||
QJsonObject discover(const QJsonObject& params) override;
|
||||
|
||||
protected:
|
||||
|
||||
///
|
||||
|
@@ -144,12 +144,14 @@ int ProviderSpi::writeBytes(unsigned size, const uint8_t * data)
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t * newdata {nullptr};
|
||||
|
||||
_spi.tx_buf = __u64(data);
|
||||
_spi.len = __u32(size);
|
||||
|
||||
if (_spiDataInvert)
|
||||
{
|
||||
uint8_t * newdata = (uint8_t *)malloc(size);
|
||||
newdata = static_cast<uint8_t *>(malloc(size));
|
||||
for (unsigned i = 0; i<size; i++) {
|
||||
newdata[i] = data[i] ^ 0xff;
|
||||
}
|
||||
@@ -159,6 +161,8 @@ int ProviderSpi::writeBytes(unsigned size, const uint8_t * data)
|
||||
int retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi);
|
||||
ErrorIf((retVal < 0), _log, "SPI failed to write. errno: %d, %s", errno, strerror(errno) );
|
||||
|
||||
free (newdata);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
@@ -17,7 +17,7 @@
|
||||
"dma": {
|
||||
"type": "integer",
|
||||
"title":"edt_dev_spec_dmaNumber_title",
|
||||
"default": 5,
|
||||
"default": 10,
|
||||
"propertyOrder" : 3
|
||||
},
|
||||
"pwmchannel": {
|
||||
|
@@ -13,7 +13,7 @@
|
||||
#include <QRandomGenerator>
|
||||
#endif
|
||||
|
||||
static const QString SSDP_HYPERION_ST("urn:hyperion-project.org:device:basic:1");
|
||||
static const QString SSDP_IDENTIFIER("urn:hyperion-project.org:device:basic:1");
|
||||
|
||||
SSDPHandler::SSDPHandler(WebServer* webserver, quint16 flatBufPort, quint16 protoBufPort, quint16 jsonServerPort, quint16 sslPort, const QString& name, QObject * parent)
|
||||
: SSDPServer(parent)
|
||||
@@ -41,7 +41,7 @@ void SSDPHandler::initServer()
|
||||
// announce targets
|
||||
_deviceList.push_back("upnp:rootdevice");
|
||||
_deviceList.push_back("uuid:"+_uuid);
|
||||
_deviceList.push_back(SSDP_HYPERION_ST);
|
||||
_deviceList.push_back(SSDP_IDENTIFIER);
|
||||
|
||||
// prep server
|
||||
SSDPServer::initServer();
|
||||
@@ -174,8 +174,8 @@ void SSDPHandler::handleMSearchRequest(const QString& target, const QString& mx,
|
||||
const auto respond = [=] () {
|
||||
// when searched for all devices / root devices / basic device
|
||||
if(target == "ssdp:all")
|
||||
sendMSearchResponse(SSDP_HYPERION_ST, address, port);
|
||||
else if(target == "upnp:rootdevice" || target == "urn:schemas-upnp-org:device:basic:1" || target == SSDP_HYPERION_ST)
|
||||
sendMSearchResponse(SSDP_IDENTIFIER, address, port);
|
||||
else if(target == "upnp:rootdevice" || target == "urn:schemas-upnp-org:device:basic:1" || target == SSDP_IDENTIFIER)
|
||||
sendMSearchResponse(target, address, port);
|
||||
};
|
||||
|
||||
@@ -213,8 +213,8 @@ QString SSDPHandler::getBaseAddress() const
|
||||
|
||||
QString SSDPHandler::buildDesc() const
|
||||
{
|
||||
/// %1 base url http://192.168.0.177:80/
|
||||
/// %2 friendly name Hyperion 2.0.0 (192.168.0.177)
|
||||
/// %1 base url http://192.168.0.177:8090/
|
||||
/// %2 friendly name Hyperion (192.168.0.177)
|
||||
/// %3 modelNumber 2.0.0
|
||||
/// %4 serialNumber / UDN (H ID) Fjsa723dD0....
|
||||
/// %5 json port 19444
|
||||
|
@@ -11,23 +11,28 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <shlobj_core.h>
|
||||
#endif
|
||||
|
||||
SysInfo* SysInfo::_instance = nullptr;
|
||||
|
||||
SysInfo::SysInfo()
|
||||
: QObject()
|
||||
{
|
||||
_sysinfo.kernelType = QSysInfo::kernelType();
|
||||
_sysinfo.kernelVersion = QSysInfo::kernelVersion();
|
||||
_sysinfo.architecture = QSysInfo::currentCpuArchitecture();
|
||||
_sysinfo.wordSize = QString::number(QSysInfo::WordSize);
|
||||
_sysinfo.productType = QSysInfo::productType();
|
||||
_sysinfo.kernelType = QSysInfo::kernelType();
|
||||
_sysinfo.kernelVersion = QSysInfo::kernelVersion();
|
||||
_sysinfo.architecture = QSysInfo::currentCpuArchitecture();
|
||||
_sysinfo.wordSize = QString::number(QSysInfo::WordSize);
|
||||
_sysinfo.productType = QSysInfo::productType();
|
||||
_sysinfo.productVersion = QSysInfo::productVersion();
|
||||
_sysinfo.prettyName = QSysInfo::prettyProductName();
|
||||
_sysinfo.hostName = QHostInfo::localHostName();
|
||||
_sysinfo.domainName = QHostInfo::localDomainName();
|
||||
_sysinfo.prettyName = QSysInfo::prettyProductName();
|
||||
_sysinfo.hostName = QHostInfo::localHostName();
|
||||
_sysinfo.domainName = QHostInfo::localDomainName();
|
||||
_sysinfo.isUserAdmin = isUserAdmin();
|
||||
getCPUInfo();
|
||||
_sysinfo.qtVersion = QT_VERSION_STR;
|
||||
_sysinfo.pyVersion = PY_VERSION;
|
||||
_sysinfo.qtVersion = QT_VERSION_STR;
|
||||
_sysinfo.pyVersion = PY_VERSION;
|
||||
}
|
||||
|
||||
SysInfo::HyperionSysInfo SysInfo::get()
|
||||
@@ -41,44 +46,95 @@ SysInfo::HyperionSysInfo SysInfo::get()
|
||||
void SysInfo::getCPUInfo()
|
||||
{
|
||||
QString cpuInfo;
|
||||
if( FileUtils::readFile("/proc/cpuinfo", cpuInfo, Logger::getInstance("DAEMON"), true) )
|
||||
if (FileUtils::readFile("/proc/cpuinfo", cpuInfo, Logger::getInstance("DAEMON"), true))
|
||||
{
|
||||
QRegularExpression regEx ("^model\\s*:\\s(.*)", QRegularExpression::CaseInsensitiveOption | QRegularExpression::MultilineOption);
|
||||
QRegularExpression regEx("^model\\s*:\\s(.*)", QRegularExpression::CaseInsensitiveOption | QRegularExpression::MultilineOption);
|
||||
QRegularExpressionMatch match;
|
||||
|
||||
match = regEx.match(cpuInfo);
|
||||
if ( match.hasMatch() )
|
||||
if (match.hasMatch())
|
||||
{
|
||||
_sysinfo.cpuModelType = match.captured(1);
|
||||
}
|
||||
|
||||
regEx.setPattern("^model name\\s*:\\s(.*)");
|
||||
match = regEx.match(cpuInfo);
|
||||
if ( match.hasMatch() )
|
||||
if (match.hasMatch())
|
||||
{
|
||||
_sysinfo.cpuModelName = match.captured(1);
|
||||
}
|
||||
|
||||
regEx.setPattern("^hardware\\s*:\\s(.*)");
|
||||
match = regEx.match(cpuInfo);
|
||||
if ( match.hasMatch() )
|
||||
if (match.hasMatch())
|
||||
{
|
||||
_sysinfo.cpuHardware = match.captured(1);
|
||||
}
|
||||
|
||||
regEx.setPattern("^revision\\s*:\\s(.*)");
|
||||
match = regEx.match(cpuInfo);
|
||||
if ( match.hasMatch() )
|
||||
if (match.hasMatch())
|
||||
{
|
||||
_sysinfo.cpuRevision = match.captured(1);
|
||||
}
|
||||
|
||||
regEx.setPattern("^revision\\s*:\\s(.*)");
|
||||
match = regEx.match(cpuInfo);
|
||||
if ( match.hasMatch() )
|
||||
if (match.hasMatch())
|
||||
{
|
||||
_sysinfo.cpuRevision = match.captured(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString SysInfo::userName()
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
|
||||
#if defined (WIN32)
|
||||
return qEnvironmentVariable("USERNAME");
|
||||
#else
|
||||
return qEnvironmentVariable("USER");
|
||||
#endif
|
||||
#else
|
||||
#if defined (WIN32)
|
||||
return getenv("USERNAME");
|
||||
#else
|
||||
return getenv("USER");
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
bool SysInfo::isUserAdmin()
|
||||
{
|
||||
bool isAdmin{ false };
|
||||
|
||||
#ifdef _WIN32
|
||||
BOOL b;
|
||||
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
|
||||
PSID AdministratorsGroup;
|
||||
b = AllocateAndInitializeSid(
|
||||
&NtAuthority,
|
||||
2,
|
||||
SECURITY_BUILTIN_DOMAIN_RID,
|
||||
DOMAIN_ALIAS_RID_ADMINS,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
&AdministratorsGroup);
|
||||
if (b)
|
||||
{
|
||||
if (!CheckTokenMembership(NULL, AdministratorsGroup, &b))
|
||||
{
|
||||
b = false;
|
||||
}
|
||||
FreeSid(AdministratorsGroup);
|
||||
}
|
||||
// TRUE - User has Administrators local group.
|
||||
// FALSE - User does not have Administrators local group.
|
||||
isAdmin = b;
|
||||
#else
|
||||
if (getuid() == 0U)
|
||||
{
|
||||
isAdmin = true;
|
||||
}
|
||||
#endif
|
||||
return isAdmin;
|
||||
}
|
||||
|
Reference in New Issue
Block a user