1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
/*
Q Light Controller Plus
osccontroller.cpp
Copyright (c) Massimo Callegari
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "osccontroller.h"
#include <QMutexLocker>
#include <QByteArray>
#include <QDebug>
OSCController::OSCController(QString ipaddr, Type type, quint32 line, QObject *parent)
: QObject(parent)
, m_ipAddr(ipaddr)
, m_packetSent(0)
, m_packetReceived(0)
, m_line(line)
, m_outputSocket(new QUdpSocket(this))
, m_packetizer(new OSCPacketizer())
{
qDebug() << "[OSCController] type: " << type;
// Ensure packets will be sent from the correct interface
m_outputSocket->bind(m_ipAddr, 0);
}
OSCController::~OSCController()
{
qDebug() << Q_FUNC_INFO;
qDeleteAll(m_dmxValuesMap);
}
QHostAddress OSCController::getNetworkIP() const
{
return m_ipAddr;
}
void OSCController::addUniverse(quint32 universe, OSCController::Type type)
{
qDebug() << "[OSC] addUniverse - universe" << universe << ", type" << type;
if (m_universeMap.contains(universe))
{
m_universeMap[universe].type |= (int)type;
}
else
{
UniverseInfo info;
info.inputSocket.clear();
info.inputPort = 7700 + universe;
if (m_ipAddr == QHostAddress::LocalHost)
{
info.feedbackAddress = QHostAddress::LocalHost;
info.outputAddress = QHostAddress::LocalHost;
}
else
{
info.feedbackAddress = QHostAddress::Null;
info.outputAddress = QHostAddress::Null;
}
info.feedbackPort = 9000 + universe;
info.outputPort = 9000 + universe;
info.type = type;
m_universeMap[universe] = info;
}
if (type == Input)
{
UniverseInfo& info = m_universeMap[universe];
info.inputSocket.clear();
info.inputSocket = getInputSocket(info.inputPort);
}
}
void OSCController::removeUniverse(quint32 universe, OSCController::Type type)
{
qDebug() << "[OSC] removeUniverse - universe" << universe << ", type" << type;
if (m_universeMap.contains(universe))
{
UniverseInfo& info = m_universeMap[universe];
if (type == Input)
info.inputSocket.clear();
if (info.type == type)
m_universeMap.take(universe);
else
info.type &= ~type;
}
}
bool OSCController::setInputPort(quint32 universe, quint16 port)
{
if (!m_universeMap.contains(universe))
return false;
QMutexLocker locker(&m_dataMutex);
UniverseInfo& info = m_universeMap[universe];
if (info.inputPort == port)
return port == 7700 + universe;
info.inputPort = port;
info.inputSocket.clear();
info.inputSocket = getInputSocket(port);
return port == 7700 + universe;
}
QSharedPointer<QUdpSocket> OSCController::getInputSocket(quint16 port)
{
foreach (UniverseInfo const& info, m_universeMap)
{
if (info.inputSocket && info.inputPort == port)
return info.inputSocket;
}
QSharedPointer<QUdpSocket> inputSocket(new QUdpSocket(this));
inputSocket->bind(QHostAddress::Any, port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
connect(inputSocket.data(), SIGNAL(readyRead()),
this, SLOT(processPendingPackets()));
return inputSocket;
}
bool OSCController::setFeedbackIPAddress(quint32 universe, QString address)
{
if (m_universeMap.contains(universe) == false)
return false;
QMutexLocker locker(&m_dataMutex);
m_universeMap[universe].feedbackAddress = QHostAddress(address);
if (m_ipAddr == QHostAddress::LocalHost)
return QHostAddress(address) == QHostAddress::LocalHost;
return QHostAddress(address) == QHostAddress::Null;
}
bool OSCController::setFeedbackPort(quint32 universe, quint16 port)
{
if (m_universeMap.contains(universe) == false)
return false;
QMutexLocker locker(&m_dataMutex);
if (m_universeMap.contains(universe))
m_universeMap[universe].feedbackPort = port;
return port == 9000 + universe;
}
bool OSCController::setOutputIPAddress(quint32 universe, QString address)
{
if (m_universeMap.contains(universe) == false)
return false;
QMutexLocker locker(&m_dataMutex);
m_universeMap[universe].outputAddress = QHostAddress(address);
if (m_ipAddr == QHostAddress::LocalHost)
return QHostAddress(address) == QHostAddress::LocalHost;
return QHostAddress(address) == QHostAddress::Null;
}
bool OSCController::setOutputPort(quint32 universe, quint16 port)
{
if (m_universeMap.contains(universe) == false)
return false;
QMutexLocker locker(&m_dataMutex);
if (m_universeMap.contains(universe))
m_universeMap[universe].outputPort = port;
return port == 9000 + universe;
}
QList<quint32> OSCController::universesList() const
{
return m_universeMap.keys();
}
UniverseInfo* OSCController::getUniverseInfo(quint32 universe)
{
if (m_universeMap.contains(universe))
return &m_universeMap[universe];
return NULL;
}
OSCController::Type OSCController::type() const
{
int type = Unknown;
foreach (UniverseInfo info, m_universeMap)
{
type |= info.type;
}
return Type(type);
}
quint32 OSCController::line() const
{
return m_line;
}
quint64 OSCController::getPacketSentNumber() const
{
return m_packetSent;
}
quint64 OSCController::getPacketReceivedNumber() const
{
return m_packetReceived;
}
quint16 OSCController::getHash(QString path)
{
quint16 hash;
if (m_hashMap.contains(path))
hash = m_hashMap[path];
else
{
/** No existing hash found. Add a new key to the table */
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
hash = qChecksum(path.toUtf8().data(), path.length());
#else
QByteArrayView bav(path.toUtf8().data(), path.length());
hash = qChecksum(bav);
#endif
m_hashMap[path] = hash;
}
return hash;
}
void OSCController::sendDmx(const quint32 universe, const QByteArray &dmxData)
{
QMutexLocker locker(&m_dataMutex);
QByteArray dmxPacket;
QHostAddress outAddress = QHostAddress::Null;
quint32 outPort = 7700 + universe;
if (m_universeMap.contains(universe))
{
outAddress = m_universeMap[universe].outputAddress;
outPort = m_universeMap[universe].outputPort;
}
for (int i = 0; i < dmxData.length(); i++)
{
if (m_dmxValuesMap.contains(universe) == false)
m_dmxValuesMap[universe] = new QByteArray(512, 0);
QByteArray *dmxValues = m_dmxValuesMap[universe];
if (dmxData[i] != dmxValues->at(i))
{
dmxValues->replace(i, 1, (const char *)(dmxData.data() + i), 1);
m_packetizer->setupOSCDmx(dmxPacket, universe, i, dmxData[i]);
qint64 sent = m_outputSocket->writeDatagram(dmxPacket.data(), dmxPacket.size(),
outAddress, outPort);
if (sent < 0)
{
qDebug() << "[OSC] sendDmx failed. Errno: " << m_outputSocket->error();
qDebug() << "Errmgs: " << m_outputSocket->errorString();
}
else
m_packetSent++;
}
}
}
void OSCController::sendFeedback(const quint32 universe, quint32 channel, uchar value, const QString &key)
{
QMutexLocker locker(&m_dataMutex);
QHostAddress outAddress = QHostAddress::Null;
quint32 outPort = 9000 + universe;
if (m_universeMap.contains(universe))
{
outAddress = m_universeMap[universe].feedbackAddress;
outPort = m_universeMap[universe].feedbackPort;
}
QString path = key;
// on invalid key try to retrieve the OSC path from the hash table.
// This works only if the OSC widget has been previously moved by the user
if (key.isEmpty())
path = m_hashMap.key(channel);
qDebug() << "[OSC] sendFeedBack - Key:" << path << "value:" << value;
QByteArray values;
QByteArray oscPacket;
// multiple value path
if (path.length() > 2 && path.at(path.length() - 2) == '_')
{
int valIdx = QString(path.at(path.length() - 1)).toInt();
path.chop(2);
if (m_universeMap[universe].multipartCache.contains(path) == false)
{
qDebug() << "[OSC] Multi-value path NOT in cache. Allocating default.";
m_universeMap[universe].multipartCache[path] = QByteArray((int)2, (char)0);
}
values = m_universeMap[universe].multipartCache[path];
if (values.length() <= valIdx)
values.resize(valIdx + 1);
values[valIdx] = (char)value;
m_universeMap[universe].multipartCache[path] = values;
//qDebug() << "Values to send:" << QString::number((uchar)values.at(0)) <<
// QString::number((uchar)values.at(1)) << values.length();
}
else
values.append((char)value); // single value path
QString pTypes;
pTypes.fill('f', values.length());
m_packetizer->setupOSCGeneric(oscPacket, path, pTypes, values);
qint64 sent = m_outputSocket->writeDatagram(oscPacket.data(), oscPacket.size(),
outAddress, outPort);
if (sent < 0)
{
qDebug() << "[OSC] sendDmx failed. Errno: " << m_outputSocket->error();
qDebug() << "Errmgs: " << m_outputSocket->errorString();
}
else
m_packetSent++;
}
void OSCController::handlePacket(QUdpSocket* socket, QByteArray const& datagram, QHostAddress const& senderAddress)
{
#if _DEBUG_RECEIVED_PACKETS
qDebug() << "Received packet with size: " << datagram.size() << ", host: " << senderAddress.toString();
#else
Q_UNUSED(senderAddress);
#endif
QList< QPair<QString, QByteArray> > messages = m_packetizer->parsePacket(datagram);
QListIterator <QPair<QString,QByteArray> > it(messages);
while (it.hasNext() == true)
{
QPair <QString,QByteArray> msg(it.next());
QString path = msg.first;
QByteArray values = msg.second;
qDebug() << "[OSC] message has path:" << path << "values:" << values.length();
if (values.isEmpty())
continue;
for (QMap<quint32, UniverseInfo>::iterator it = m_universeMap.begin(); it != m_universeMap.end(); ++it)
{
quint32 universe = it.key();
UniverseInfo& info = it.value();
if (info.inputSocket == socket)
{
if (values.length() > 1)
{
info.multipartCache[path] = values;
for (int i = 0; i < values.length(); i++)
{
QString modPath = QString("%1_%2").arg(path).arg(i);
emit valueChanged(universe, m_line, getHash(modPath), (uchar)values.at(i), modPath);
}
}
else
emit valueChanged(universe, m_line, getHash(path), (uchar)values.at(0), path);
}
}
}
m_packetReceived++;
}
void OSCController::processPendingPackets()
{
QUdpSocket *socket = qobject_cast<QUdpSocket *>(sender());
QByteArray datagram;
QHostAddress senderAddress;
while (socket->hasPendingDatagrams())
{
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(), datagram.size(), &senderAddress);
handlePacket(socket, datagram, senderAddress);
}
}
|