Tpm2net rewrite (#217)

* initial tpm2net rewrite - doesnt compile yet

* new tpm2.net drive compiles.
rename old tmp2net typos to tpm2net
fixed compiler warning in ws2812b

* added packetsize calculations and the end of frame character.
Output looks good on tcpdump

* removed old tpm2net driver
renamed tpm2netnew to tpm2net
This commit is contained in:
penfold42 2016-09-04 17:07:10 +10:00 committed by redPanther
parent 7ac31efd4f
commit 4968a11816
6 changed files with 75 additions and 53 deletions

View File

@ -93,7 +93,7 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
#endif
// network lights
LedDevice::addToDeviceMap("tmp2net", LedDeviceTpm2net::construct);
LedDevice::addToDeviceMap("tpm2net", LedDeviceTpm2net::construct);
LedDevice::addToDeviceMap("udpraw", LedDeviceUdpRaw::construct);
LedDevice::addToDeviceMap("e131", LedDeviceUdpE131::construct);
#ifdef ENABLE_TINKERFORGE

View File

@ -21,7 +21,7 @@
<file alias="schema-sedu">schemas/schema-sedu.json</file>
<file alias="schema-sk6812rgbw-spi">schemas/schema-sk6812rgbw-spi.json</file>
<file alias="schema-tinkerforge">schemas/schema-tinkerforge.json</file>
<file alias="schema-tmp2net">schemas/schema-tmp2net.json</file>
<file alias="schema-tpm2net">schemas/schema-tpm2net.json</file>
<file alias="schema-tpm2.json">schemas/schema-tpm2.json</file>
<file alias="schema-udpraw">schemas/schema-udpraw.json</file>
<file alias="schema-ws2801">schemas/schema-ws2801.json</file>

View File

@ -1,29 +1,32 @@
// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
#include <sstream>
// Local-Hyperion includes
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <QHostInfo>
#include <QUuid>
// hyperion local includes
#include "LedDeviceTpm2net.h"
LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig)
: LedDevice()
, _socket(this)
: ProviderUdp(deviceConfig)
{
setConfig(deviceConfig);
}
LedDeviceTpm2net::~LedDeviceTpm2net()
{
}
bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig)
{
_host = QHostAddress(QString::fromStdString(deviceConfig.get("output", "127.0.0.1").asString()));
_port = deviceConfig.get("port", 65506).asInt();
ProviderUdp::setConfig(deviceConfig);
_LatchTime_ns = deviceConfig.get("latchtime",104000).asInt();
_tpm2_max = deviceConfig.get("max-packet",170).asInt();
return true;
}
@ -32,32 +35,53 @@ LedDevice* LedDeviceTpm2net::construct(const Json::Value &deviceConfig)
return new LedDeviceTpm2net(deviceConfig);
}
// populates the headers
int LedDeviceTpm2net::write(const std::vector<ColorRgb> &ledValues)
{
if (_ledBuffer.size() == 0)
uint8_t * _tpm2_buffer = (uint8_t*) malloc(_tpm2_max+7);
int retVal = 0;
_ledCount = ledValues.size();
_tpm2ByteCount = 3 * _ledCount;
_tpm2TotalPackets = 1 + _tpm2ByteCount / _tpm2_max;
int _thisPacketBytes = 0;
_tpm2ThisPacket = 1;
const uint8_t * rawdata = reinterpret_cast<const uint8_t *>(ledValues.data());
for (int rawIdx = 0; rawIdx < _tpm2ByteCount; rawIdx++)
{
_ledBuffer.resize(7 + 3*ledValues.size());
_ledBuffer[0] = 0x9c; // block-start byte TPM.NET
_ledBuffer[1] = 0xDA;
_ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // frame size high byte
_ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // frame size low byte
_ledBuffer[4] = 1; // packets number
_ledBuffer[5] = 1; // Number of packets
_ledBuffer[(int)(7 + 3*ledValues.size()-1)] = 0x36; // block-end byte
if (rawIdx % _tpm2_max == 0) // start of new packet
{
_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
}
// write data
memcpy(6 + _ledBuffer.data(), ledValues.data() /*Max 1,490 bytes*/, ledValues.size() * 3);
_tpm2_buffer [6 + rawIdx%_tpm2_max] = rawdata[rawIdx];
_socket.connectToHost(_host, _port);
_socket.write((const char *)_ledBuffer.data());
_socket.close();
// 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);
}
}
return 0;
return retVal;
}
int LedDeviceTpm2net::switchOff()
{
memset(6 + _ledBuffer.data(), 0, _ledBuffer.size() - 5);
return 0;
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
}

View File

@ -1,15 +1,17 @@
#pragma once
// STL includes
#include <fstream>
#include <string>
#include <QUdpSocket>
#include <QStringList>
// Leddevice includes
#include <leddevice/LedDevice.h>
// hyperion includes
#include "ProviderUdp.h"
#define TPM2_DEFAULT_PORT 65506
///
class LedDeviceTpm2net : public LedDevice
/// Implementation of the LedDevice interface for sending led colors via udp/E1.31 packets
///
class LedDeviceTpm2net : public ProviderUdp
{
public:
///
@ -19,8 +21,6 @@ public:
///
LedDeviceTpm2net(const Json::Value &deviceConfig);
virtual ~LedDeviceTpm2net();
///
/// Sets configuration
///
@ -31,12 +31,12 @@ public:
/// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig);
///
/// Writes the given led-color values to the output stream
/// Writes the led color values to the led-device
///
/// @param ledValues The color-value per led
///
/// @return Zero on success else negative
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> &ledValues);
@ -44,10 +44,8 @@ public:
virtual int switchOff();
private:
/// The host of the master brick
QHostAddress _host;
/// The port of the master brick
uint16_t _port;
QUdpSocket _socket;
int _tpm2_max;
int _tpm2ByteCount;
int _tpm2TotalPackets;
int _tpm2ThisPacket;
};

View File

@ -146,7 +146,7 @@ public:
///
/// @param deviceConfig the json device config
/// @return true if success
bool setConfig(const Json::Value&) {};
bool setConfig(const Json::Value&) {return true;};
/// constructs leddevice
static LedDevice* construct(const Json::Value &);