/* dmxwidget.cpp Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "dmxwidget.h" #include DmxWidget::DmxWidget(QObject *parent) : QObject{parent} , port_(new QSerialPort(this)) { connect(this, &DmxWidget::serialDataRead, this, &DmxWidget::parseMessageBuffer); connect(port_, &QSerialPort::errorOccurred, this, &DmxWidget::serialErrorOccured); } DmxWidget::~DmxWidget() { DmxWidget::close(); } void DmxWidget::open() { if (!port_->open(QIODeviceBase::ReadWrite)) return; ENTTEC::Widget::open(); qInfo() << "Port opened on" << port_->portName(); getSerialNumber(); getParameters(0); if (port_->waitForReadyRead(500) && port_->bytesAvailable() >= 19) { char snhead[] = {ENTTEC::Pro::START_DELIMITER, ENTTEC::Pro::OpGetWidgetSerial, 0x04, 0x00}; // expect 4 bytes of message data message_rx_buffer_.append(port_->readAll()); if (!message_rx_buffer_.startsWith(snhead)) { message_rx_buffer_.clear(); close(); return; } parseMessageBuffer(); } else { close(); return; } connect(port_, &QSerialPort::readyRead, this, [this]() { message_rx_buffer_.append(port_->readAll()); emit serialDataRead(); }); emit connectedChanged(isConnected()); } void DmxWidget::close() { port_->close(); disconnect(port_, &QSerialPort::readyRead, nullptr, nullptr); ENTTEC::Widget::close(); emit connectedChanged(isConnected()); } /** * @brief DmxWidget::parseMessageBuffer */ void DmxWidget::parseMessageBuffer() { qDebug() << "Rx :" << message_rx_buffer_.size() << "bytes :" << message_rx_buffer_.toHex(); OperatingMode other_side = USBunknown; switch (usb_mode) { case ENTTEC::Widget::USBdevice: other_side = USBhost; break; case ENTTEC::Widget::USBhost: other_side = USBdevice; break; default: break; } uint16_t length; const int fixed_length = 5; // start + label + 2xlength + end auto fast_forward = [this](int offset = 0) { if (message_rx_buffer_.isEmpty()) return false; if (offset == 0) offset = message_rx_buffer_.indexOf(ENTTEC::Pro::START_DELIMITER); if (offset < 0) message_rx_buffer_.clear(); // start delimiter not found else if (offset > 0) message_rx_buffer_.remove(0, offset); // delimiter now at start of buffer return (message_rx_buffer_.size() >= fixed_length && (uint8_t)message_rx_buffer_[0] == ENTTEC::Pro::START_DELIMITER); }; while(fast_forward()) { // read and check the data length length = message_rx_buffer_[2] | message_rx_buffer_[3] << 8; if (message_rx_buffer_.size() < length + fixed_length) return; // message in buffer is incomplete // look for the end delimeter if ((uint8_t)message_rx_buffer_[length + fixed_length - 1] != ENTTEC::Pro::END_DELIMITER) { fast_forward(length + fixed_length - 1); // discard corrupted bytes continue; } // make a new message data auto msg = MessageDataFactory((MESSAGE_LABEL)message_rx_buffer_[1], other_side); // fill it with data std::shared_ptr stream(new bufferstream( reinterpret_cast(message_rx_buffer_.data()+fixed_length-1), length, bufferstream::LittleEndian)); msg->iStream(stream); // ship it routeRxMessage(msg); // discard processed bytes fast_forward(length + fixed_length); } } void DmxWidget::sendMessage(std::shared_ptr msg) const { uint16_t length = msg->streamSize(); const int fixed_length = 5; char buffer[length + fixed_length]; std::shared_ptr stream(new bufferstream(reinterpret_cast(buffer), sizeof(buffer), bufferstream::LittleEndian)); *stream << ENTTEC::Pro::START_DELIMITER; *stream << msg->label; *stream << length; msg->oStream(stream); *stream << ENTTEC::Pro::END_DELIMITER; auto data = QByteArray(buffer, sizeof(buffer)); qDebug() << "Tx : label" << msg->label << ":" << data.toHex(); port_->write(buffer, sizeof(buffer)); } /** * @brief DmxWidget::serialErrorOccured * @param error */ void DmxWidget::serialErrorOccured(QSerialPort::SerialPortError error) { switch (error) { case QSerialPort::WriteError: case QSerialPort::ReadError: case QSerialPort::ResourceError: case QSerialPort::TimeoutError: close(); break; default: break; } } /** * @brief DmxWidget::availableWidgets * @return */ QList> DmxWidget::availableWidgets() { QList> wdgts; // evaluate all system serial ports const auto ports = QSerialPortInfo::availablePorts(); for (const QSerialPortInfo &port : ports) { // try to open a widget on each port auto widget = std::make_shared(); widget->setPort(port); widget->open(); if (widget->isConnected()) wdgts.push_back(widget); // add valid widgets to the list else widget->close(); } return wdgts; }