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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
/*
Q Light Controller
peperonidevice.cpp
Copyright (c) Heikki Junnila
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 <QDebug>
#include <libusb.h>
#include "peperonidevice.h"
#include "peperonidefs.h"
#include "peperoni.h"
/****************************************************************************
* Initialization
****************************************************************************/
PeperoniDevice::PeperoniDevice(Peperoni* parent, struct libusb_device* device,
struct libusb_device_descriptor* desc,
quint32 line)
: QThread(parent)
, m_baseLine(line)
, m_device(device)
, m_handle(NULL)
, m_descriptor(desc)
{
Q_ASSERT(device != NULL);
/* Store fw version so we don't need to rely on libusb's volatile data */
m_firmwareVersion = desc->bcdDevice;
qDebug() << "[Peperoni] detected device firmware version:" << QString::number(m_firmwareVersion, 16);
m_operatingModes[line] = CloseMode;
if (desc->idProduct == PEPERONI_PID_USBDMX21)
m_operatingModes[line + 1] = CloseMode;
extractName();
}
PeperoniDevice::~PeperoniDevice()
{
closeAll();
}
/****************************************************************************
* Device information
****************************************************************************/
bool PeperoniDevice::isPeperoniDevice(const struct libusb_device_descriptor* desc)
{
/* If there's nothing to inspect, it can't be what we're looking for */
if (desc == NULL)
return false;
/* If it's not manufactured by them, we're not interested in it */
if (!isPeperoniDevice(desc->idVendor, desc->idProduct))
return false;
/* We need one interface */
if (desc->bNumConfigurations < 1)
return false;
return true;
}
bool PeperoniDevice::isPeperoniDevice(int vid, int pid)
{
if (vid != PEPERONI_VID)
return false;
if (pid == PEPERONI_PID_RODIN1 ||
pid == PEPERONI_PID_RODIN1_MK3 ||
pid == PEPERONI_PID_RODIN2 ||
pid == PEPERONI_PID_RODINT ||
pid == PEPERONI_PID_XSWITCH ||
pid == PEPERONI_PID_USBDMX21)
return true;
return false;
}
int PeperoniDevice::outputsNumber(libusb_device_descriptor *desc)
{
/* If there's nothing to inspect, it can't be what we're looking for */
if (desc == NULL)
return 0;
/* If it's not manufactured by them, we're not interested in it */
if (desc->idVendor != PEPERONI_VID)
return 0;
if (desc->idProduct == PEPERONI_PID_USBDMX21)
{
return 2;
}
else if (desc->idProduct == PEPERONI_PID_RODIN1 ||
desc->idProduct == PEPERONI_PID_RODIN1_MK3 ||
desc->idProduct == PEPERONI_PID_RODIN2 ||
desc->idProduct == PEPERONI_PID_RODINT ||
desc->idProduct == PEPERONI_PID_XSWITCH)
{
return 1;
}
return 0;
}
void PeperoniDevice::extractName()
{
Q_ASSERT(m_device != NULL);
libusb_device_handle *handle = NULL;
int r = libusb_open(m_device, &handle);
if (r == 0)
{
char buf[256];
int len = 0;
/* Extract the name */
len = libusb_get_string_descriptor_ascii(handle, m_descriptor->iProduct,
(uchar*) &buf, sizeof(buf));
if (len > 0)
{
m_name = QString(QByteArray(buf, len));
}
else
{
m_name = tr("Unknown");
qWarning() << "Unable to get product name:" << len;
}
/*
// UID
len = libusb_get_string_descriptor_ascii(handle, m_descriptor->iSerialNumber,
(uchar*) &buf, sizeof(buf));
if (len > 0)
m_serial = QString(QByteArray(buf, len));
else
qWarning() << "Unable to get device serial:" << len;
*/
}
libusb_close(handle);
}
QString PeperoniDevice::name(quint32 line) const
{
if (m_descriptor->idProduct == PEPERONI_PID_USBDMX21)
return QString("%1 - %2 %3").arg(m_name).arg(tr("Universe")).arg((line - m_baseLine) + 1);
return m_name;
}
QString PeperoniDevice::baseInfoText(quint32 line) const
{
QString info;
if (m_device != NULL)
{
info += QString("<B>%1</B>").arg(name(line));
info += QString("<P>");
info += tr("Device is working correctly.");
info += QString("<BR/>");
info += tr("Firmware version: %1").arg(m_firmwareVersion, 4, 16, QChar('0'));
info += QString("</P>");
}
else
{
info += QString("<B>");
info += tr("Unknown device");
info += QString("</B>");
info += QString("<P>");
info += tr("Cannot connect to USB device.");
info += QString("</P>");
}
return info;
}
QString PeperoniDevice::inputInfoText(quint32 line) const
{
QString info;
if (m_device != NULL)
{
info += QString("<B>%1:</B> ").arg(tr("Input line"));
if (m_operatingModes[line] & InputMode)
info += QString("%1").arg(tr("Open"));
else
info += QString("%1").arg(tr("Close"));
info += QString("<BR>");
}
return info;
}
QString PeperoniDevice::outputInfoText(quint32 line) const
{
QString info;
if (m_device != NULL)
{
info += QString("<B>%1:</B> ").arg(tr("Output line"));
if (m_operatingModes[line] & OutputMode)
info += QString("%1").arg(tr("Open"));
else
info += QString("%1").arg(tr("Close"));
info += QString("<BR>");
}
return info;
}
/****************************************************************************
* Open & close
****************************************************************************/
bool PeperoniDevice::open(quint32 line, OperatingMode mode)
{
m_operatingModes[line] |= mode;
if (m_device != NULL && m_handle == NULL)
{
int r = -1;
int configuration = PEPERONI_CONF_TXRX;
r = libusb_open(m_device, &m_handle);
if (r < 0)
{
qWarning() << "Unable to open PeperoniDevice with idProduct:" << m_descriptor->idProduct;
m_handle = NULL;
return false;
}
/* Use configuration #2 on X-Switch */
if (m_descriptor->idProduct == PEPERONI_PID_XSWITCH)
configuration = PEPERONI_CONF_TXRX;
else
configuration = PEPERONI_CONF_TXONLY;
/* Set selected configuration */
r = libusb_set_configuration(m_handle, configuration);
if (r < 0)
qWarning() << "PeperoniDevice is unable to set configuration #" << configuration;
/* We must claim the interface before doing anything */
r = libusb_claim_interface(m_handle, PEPERONI_IFACE_EP0);
if (r < 0)
qWarning() << "PeperoniDevice is unable to claim interface EP0!";
/* Set TX DMX startcode */
r = libusb_control_transfer(m_handle,
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE |
LIBUSB_ENDPOINT_OUT,
PEPERONI_TX_STARTCODE, // Set DMX startcode
0, // Standard startcode is 0
0, // No index
NULL, // No data
0, // Zero data length
50); // Timeout (ms)
if (r < 0)
qWarning() << "PeperoniDevice is unable to set 0 as the DMX output startcode!";
/* Set RX DMX startcode */
r = libusb_control_transfer(m_handle,
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE |
LIBUSB_ENDPOINT_IN,
PEPERONI_RX_STARTCODE, // Set DMX startcode
0, // Standard startcode is 0
0, // No index
NULL, // No data
0, // Zero data length
50); // Timeout (ms)
if (r < 0)
qWarning() << "PeperoniDevice is unable to set 0 as the DMX input startcode!";
if (m_firmwareVersion >= PEPERONI_FW_OLD_BULK_SUPPORT)
{
/* Sometimes you need a little jolt to get the device on its feet. */
r = libusb_clear_halt(m_handle, PEPERONI_BULK_OUT_ENDPOINT);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "is unable to reset bulk OUT endpoint.";
r = libusb_clear_halt(m_handle, PEPERONI_BULK_IN_ENDPOINT);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "is unable to reset bulk IN endpoint.";
}
}
if (m_operatingModes[line] & InputMode && m_running == false)
{
qDebug() << "[Peperoni] open input line:" << m_baseLine;
m_dmxInputBuffer.clear();
m_dmxInputBuffer.fill(0, 512);
m_running = true;
start();
}
return true;
}
void PeperoniDevice::close(quint32 line, OperatingMode mode)
{
m_operatingModes[line] &= ~mode;
if (mode == InputMode && m_running == true)
{
m_running = false;
wait();
}
if (m_operatingModes[line] != CloseMode)
return;
QMutexLocker lock(&m_ioMutex);
if (m_device != NULL && m_handle != NULL)
{
/* Release the interface in case we claimed it */
int r = libusb_release_interface(m_handle, PEPERONI_IFACE_EP0);
if (r < 0)
{
qWarning() << "PeperoniDevice" << name(m_baseLine)
<< "is unable to release interface EP0!";
}
libusb_close(m_handle);
}
m_handle = NULL;
}
void PeperoniDevice::closeAll()
{
qDebug() << "[Peperoni] close input...";
close(m_baseLine, InputMode);
qDebug() << "[Peperoni] close output...";
close(m_baseLine, OutputMode);
}
const struct libusb_device* PeperoniDevice::device() const
{
return m_device;
}
/****************************************************************************
* Input Thread
****************************************************************************/
void PeperoniDevice::run()
{
if (m_device == NULL)
return;
qDebug() << "[Peperoni] input thread started correctly";
while (m_running == true)
{
QByteArray tmpBuffer(512, 0);
/* Choose write method based on firmware version. One has to unplug
and then re-plug the dongle in apple for bulk write to work,
so disable it for apple, since control msg should work for all. */
#if !defined(__APPLE__) && !defined(Q_OS_MAC)
if (1 || m_firmwareVersion < PEPERONI_FW_NEW_BULK_SUPPORT)
{
#endif
unsigned short rxslots;
unsigned char rxstartcode;
unsigned int block;
// read memory blocking if firmware is >= 0x0500
if (m_firmwareVersion >= PEPERONI_FW_NEW_BULK_SUPPORT)
{
block = 1;
}
else
{
block = 0;
// if we don't block sleep for 10ms
usleep(10000);
}
{
int r = -1;
QMutexLocker lock(&m_ioMutex);
r = libusb_control_transfer(m_handle,
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE |
LIBUSB_ENDPOINT_IN,
PEPERONI_RX_MEM_REQUEST, // We are READING DMX data
block, // Blocking does not work on all firmware versions -> don't block
0, // Start at DMX address 0
(uchar *)tmpBuffer.data(), // The DMX universe data
tmpBuffer.size(), // Size of DMX universe
100); // Timeout (ms)
if (r < 0)
{
qWarning() << "PeperoniDevice" << name(m_baseLine) << "failed control_msg:" << libusb_strerror(libusb_error(r));
r = libusb_clear_halt(m_handle, PEPERONI_BULK_IN_ENDPOINT);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "is unable to reset bulk IN endpoint.";
}
else
{
/* read received startcode */
r = libusb_control_transfer(m_handle,
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE |
LIBUSB_ENDPOINT_IN,
PEPERONI_RX_STARTCODE,
0,
0,
(uchar *)&rxstartcode,
sizeof(rxstartcode),
10);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "failed to read receiver startcode:" << libusb_strerror(libusb_error(r));
else
{
/* read number of received slots */
r = libusb_control_transfer(m_handle,
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE |
LIBUSB_ENDPOINT_IN,
PEPERONI_RX_SLOTS,
0,
0,
(uchar *)&rxslots,
sizeof(rxslots),
10);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "failed to read receiver slot counter:" << libusb_strerror(libusb_error(r));
else
{
if (rxslots > m_dmxInputBuffer.size())
{
rxslots = m_dmxInputBuffer.size();
qWarning() << "PeperoniDevice" << name(m_baseLine) << "input frame too long, truncated";
}
//qDebug() << "[Peperoni] input frame has startcode" << rxstartcode << "and is" << rxslots << "slots long";
/* only accept DMX512 data */
if (rxstartcode == 0)
{
for (int i = 0; i < rxslots; i++)
{
if (tmpBuffer.at(i) != m_dmxInputBuffer.at(i))
{
emit valueChanged(UINT_MAX, m_baseLine, i, tmpBuffer.at(i));
m_dmxInputBuffer[i] = tmpBuffer.at(i);
}
}
}
}
}
}
}
#if !defined(__APPLE__) && !defined(Q_OS_MAC)
}
else
{
//TODO
}
#endif
}
}
/****************************************************************************
* Write
****************************************************************************/
void PeperoniDevice::outputDMX(quint32 line, const QByteArray& universe)
{
Q_UNUSED(line)
int r = -1;
if (m_handle == NULL)
return;
{
QMutexLocker lock(&m_ioMutex);
/* Choose write method based on firmware version. One has to unplug
and then re-plug the dongle in apple for bulk write to work,
so disable it for apple, since control msg should work for all. */
#if !defined(__APPLE__) && !defined(Q_OS_MAC)
if (m_firmwareVersion < PEPERONI_FW_OLD_BULK_SUPPORT)
{
#endif
//qDebug() << "[Peperoni] control pipe write. size:" << universe.size();
r = libusb_control_transfer(m_handle,
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE |
LIBUSB_ENDPOINT_OUT,
PEPERONI_TX_MEM_REQUEST, // We are WRITING DMX data
0, // Blocking does not work on all firmware versions -> don't block
0, // Start at DMX address 0
(uchar*) universe.data(), // The DMX universe data
universe.size(), // Size of DMX universe
50); // Timeout (ms)
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "failed control write:" << libusb_strerror(libusb_error(r));
#if !defined(__APPLE__) && !defined(Q_OS_MAC)
}
else if (m_firmwareVersion < PEPERONI_FW_NEW_BULK_SUPPORT)
{
char requestType = char(PEPERONI_OLD_BULK_HEADER_REQUEST_TX_SET);
if (line - m_baseLine == 1)
requestType = PEPERONI_OLD_BULK_HEADER_REQUEST_TX2_SET;
//qDebug() << "Old bulk pipe write. Size:" << universe.size();
/* Construct a bulk header first */
m_bulkBuffer.clear();
m_bulkBuffer.append(char(PEPERONI_OLD_BULK_HEADER_ID));
m_bulkBuffer.append(requestType);
m_bulkBuffer.append(char(0x00)); // 512 - LSB
m_bulkBuffer.append(char(0x02)); // 512 - MSB
/* Append universe data to the bulk buffer */
m_bulkBuffer.append(universe);
/* Append trailing zeros to reach size of 512 bytes */
m_bulkBuffer.append(QByteArray(int(512 - universe.size()), char(0)));
/* Perform a bulk write */
int written = 0;
r = libusb_bulk_transfer(m_handle,
PEPERONI_BULK_OUT_ENDPOINT,
(uchar *)m_bulkBuffer.data(),
m_bulkBuffer.size(),
&written,
50);
if (r < 0)
{
qWarning() << "PeperoniDevice" << name(m_baseLine) << "failed 'old' bulk write:" << libusb_strerror(libusb_error(r));
qWarning() << "Resetting bulk endpoint.";
r = libusb_clear_halt(m_handle, PEPERONI_BULK_OUT_ENDPOINT);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "is unable to reset bulk endpoint.";
}
}
else
{
//qDebug() << "New bulk pipe write. Size:" << universe.size();
m_bulkBuffer.clear();
/* Construct a bulk command header first */
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID1));
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID2));
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID3));
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID4));
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_REQUEST_SET));
/** universe number: Rodins only support index 0, DMX21 supports 0 and 1 */
m_bulkBuffer.append(char(line - m_baseLine));
m_bulkBuffer.append(char(0x07)); /** length of data stage (512 + 7 bytes), LSB */
m_bulkBuffer.append(char(0x02)); /** length of data state (512 + 7 bytes), MSB */
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_CONFIG_BLOCK /** transmitter configuration: block while transmitter is busy */
| PEPERONI_NEW_BULK_CONFIG_FORCETX)); /** transmitter configuration: force transmission */
m_bulkBuffer.append(char(50)); /** timeout LSB, 50ms */
m_bulkBuffer.append(char(0)); /** timeout MSB, 50ms */
m_bulkBuffer.append(char(181)); /** length of break, 200 us */
m_bulkBuffer.append(char(250)); /** length of Mark after Break, 20us */
/* Construct a bulk data state header */
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID1));
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID2));
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID3));
m_bulkBuffer.append(char(PEPERONI_NEW_BULK_HEADER_ID4));
m_bulkBuffer.append(char(0x01)); /** number of bytes to send, incl. startcode, LSB */
m_bulkBuffer.append(char(0x02)); /** number of bytes to send, incl. startcode, MSB */
m_bulkBuffer.append(char(0)); /** internal buffer comes without startcode -> insert it */
/* Append universe data to the bulk buffer */
m_bulkBuffer.append(universe);
/* Append trailing zeros to reach size of 512 bytes */
m_bulkBuffer.append(QByteArray(int(512 - universe.size()), char(0)));
/* Perform a bulk write */
int written = 0;
r = libusb_bulk_transfer(m_handle,
PEPERONI_BULK_OUT_ENDPOINT,
(uchar *)m_bulkBuffer.data(),
m_bulkBuffer.size(),
&written,
100); /* use larger timeout then specified in the command header above */
if (r < 0)
{
qWarning() << "PeperoniDevice" << name(m_baseLine) << "failed 'new' bulk write:" << libusb_strerror(libusb_error(r));
}
else
{
char status[8];
int read = 0;
r = libusb_bulk_transfer(m_handle,
PEPERONI_BULK_IN_ENDPOINT,
(uchar *)status,
sizeof(status),
&read,
100); /* use larger timeout then specified in the command header above */
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "failed 'new' bulk read:" << libusb_strerror(libusb_error(r));
}
if (r < 0)
{
qWarning() << "Resetting bulk endpoints.";
r = libusb_clear_halt(m_handle, PEPERONI_BULK_OUT_ENDPOINT);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "is unable to reset bulk OUT endpoint.";
r = libusb_clear_halt(m_handle, PEPERONI_BULK_IN_ENDPOINT);
if (r < 0)
qWarning() << "PeperoniDevice" << name(m_baseLine) << "is unable to reset bulk IN endpoint.";
}
else
{
/** optionally analyse status buffer: ID (4 bytes), Timestamp (2 bytes, ms), status (1 byte), unused (1 byte) */
}
}
#endif
}
}
|