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 #endif
// network lights // network lights
LedDevice::addToDeviceMap("tmp2net", LedDeviceTpm2net::construct); LedDevice::addToDeviceMap("tpm2net", LedDeviceTpm2net::construct);
LedDevice::addToDeviceMap("udpraw", LedDeviceUdpRaw::construct); LedDevice::addToDeviceMap("udpraw", LedDeviceUdpRaw::construct);
LedDevice::addToDeviceMap("e131", LedDeviceUdpE131::construct); LedDevice::addToDeviceMap("e131", LedDeviceUdpE131::construct);
#ifdef ENABLE_TINKERFORGE #ifdef ENABLE_TINKERFORGE

View File

@ -21,7 +21,7 @@
<file alias="schema-sedu">schemas/schema-sedu.json</file> <file alias="schema-sedu">schemas/schema-sedu.json</file>
<file alias="schema-sk6812rgbw-spi">schemas/schema-sk6812rgbw-spi.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-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-tpm2.json">schemas/schema-tpm2.json</file>
<file alias="schema-udpraw">schemas/schema-udpraw.json</file> <file alias="schema-udpraw">schemas/schema-udpraw.json</file>
<file alias="schema-ws2801">schemas/schema-ws2801.json</file> <file alias="schema-ws2801">schemas/schema-ws2801.json</file>

View File

@ -1,29 +1,32 @@
// STL includes // STL includes
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
#include <iostream> #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" #include "LedDeviceTpm2net.h"
LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig) LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig)
: LedDevice() : ProviderUdp(deviceConfig)
, _socket(this)
{ {
setConfig(deviceConfig); setConfig(deviceConfig);
} }
LedDeviceTpm2net::~LedDeviceTpm2net()
{
}
bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig) bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig)
{ {
_host = QHostAddress(QString::fromStdString(deviceConfig.get("output", "127.0.0.1").asString())); ProviderUdp::setConfig(deviceConfig);
_port = deviceConfig.get("port", 65506).asInt(); _LatchTime_ns = deviceConfig.get("latchtime",104000).asInt();
_tpm2_max = deviceConfig.get("max-packet",170).asInt();
return true; return true;
} }
@ -32,32 +35,53 @@ LedDevice* LedDeviceTpm2net::construct(const Json::Value &deviceConfig)
return new LedDeviceTpm2net(deviceConfig); return new LedDeviceTpm2net(deviceConfig);
} }
int LedDeviceTpm2net::write(const std::vector<ColorRgb> & ledValues)
// 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()); if (rawIdx % _tpm2_max == 0) // start of new packet
_ledBuffer[0] = 0x9c; // block-start byte TPM.NET {
_ledBuffer[1] = 0xDA; _thisPacketBytes = (_tpm2ByteCount - rawIdx < _tpm2_max) ? _tpm2ByteCount % _tpm2_max : _tpm2_max;
_ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // frame size high byte // is this the last packet? ? ^^ last packet : ^^ earlier packets
_ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // frame size low byte
_ledBuffer[4] = 1; // packets number _tpm2_buffer[0] = 0x9c; // Packet start byte
_ledBuffer[5] = 1; // Number of packets _tpm2_buffer[1] = 0xda; // Packet type Data frame
_ledBuffer[(int)(7 + 3*ledValues.size()-1)] = 0x36; // block-end byte _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 _tpm2_buffer [6 + rawIdx%_tpm2_max] = rawdata[rawIdx];
memcpy(6 + _ledBuffer.data(), ledValues.data() /*Max 1,490 bytes*/, ledValues.size() * 3);
_socket.connectToHost(_host, _port); // is this the last byte of last packet || last byte of other packets
_socket.write((const char *)_ledBuffer.data()); if ( (rawIdx == _tpm2ByteCount-1) || (rawIdx %_tpm2_max == _tpm2_max-1) )
_socket.close(); {
_tpm2_buffer [6 + rawIdx%_tpm2_max +1] = 0x36; // Packet end byte
retVal &= writeBytes(_thisPacketBytes+7, _tpm2_buffer);
}
}
return 0; return retVal;
} }
int LedDeviceTpm2net::switchOff() int LedDeviceTpm2net::switchOff()
{ {
memset(6 + _ledBuffer.data(), 0, _ledBuffer.size() - 5); return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
return 0;
} }

View File

@ -1,15 +1,17 @@
#pragma once #pragma once
// STL includes // STL includes
#include <fstream>
#include <string> #include <string>
#include <QUdpSocket>
#include <QStringList> // hyperion includes
// Leddevice includes #include "ProviderUdp.h"
#include <leddevice/LedDevice.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: public:
/// ///
@ -19,8 +21,6 @@ public:
/// ///
LedDeviceTpm2net(const Json::Value &deviceConfig); LedDeviceTpm2net(const Json::Value &deviceConfig);
virtual ~LedDeviceTpm2net();
/// ///
/// Sets configuration /// Sets configuration
/// ///
@ -31,23 +31,21 @@ public:
/// constructs leddevice /// constructs leddevice
static LedDevice* construct(const Json::Value &deviceConfig); 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 /// @param ledValues The color-value per led
/// @return Zero on succes else negative
/// ///
/// @return Zero on success else negative virtual int write(const std::vector<ColorRgb> &ledValues);
///
virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off /// Switch the leds off
virtual int switchOff(); virtual int switchOff();
private: private:
/// The host of the master brick int _tpm2_max;
QHostAddress _host; int _tpm2ByteCount;
int _tpm2TotalPackets;
/// The port of the master brick int _tpm2ThisPacket;
uint16_t _port;
QUdpSocket _socket;
}; };

View File

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