-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathSiudi.cpp
More file actions
174 lines (149 loc) · 5.63 KB
/
Siudi.cpp
File metadata and controls
174 lines (149 loc) · 5.63 KB
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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Siudi.cpp
* The synchronous SIUDI widgets.
* Copyright (C) 2023 Alexander Simon
*/
#include "plugins/usbdmx/Siudi.h"
#include <string.h>
#include <unistd.h>
#include "libs/usb/LibUsbAdaptor.h"
#include "ola/Constants.h"
#include "ola/Logging.h"
#include "ola/strings/Format.h"
#include "plugins/usbdmx/AsyncUsbSender.h"
#include "plugins/usbdmx/ThreadedUsbSender.h"
namespace ola {
namespace plugin {
namespace usbdmx {
using ola::usb::LibUsbAdaptor;
namespace {
static const uint8_t ENDPOINT = 2;
// SIUDI-6 blocks USB transfers during an ongoing DMX TX.
// One package needs about 32 ms to be sent.
// Wait 30 ms between two USB bulk transfers and expect 2 ms USB response delay.
static const unsigned int BULK_TIMEOUT = 10;
static const unsigned int BULK_DELAY = (30 * 1000);
static const unsigned int CONTROL_TIMEOUT = 500;
static const unsigned int DEVINFO_REQUEST = 0x3f;
static const unsigned int DEVINFO_SIZE = 64;
} // namespace
// SiudiThreadedSender
// -----------------------------------------------------------------------------
/*
* Sends messages to a SIUDI device in a separate thread.
*/
class SiudiThreadedSender: public ThreadedUsbSender {
public:
SiudiThreadedSender(LibUsbAdaptor *adaptor,
libusb_device *usb_device,
libusb_device_handle *handle);
bool Start();
private:
LibUsbAdaptor* const m_adaptor;
libusb_device_handle* const m_usb_handle;
bool TransmitBuffer(libusb_device_handle *handle,
const DmxBuffer &buffer);
};
SiudiThreadedSender::SiudiThreadedSender(
LibUsbAdaptor *adaptor,
libusb_device *usb_device,
libusb_device_handle *usb_handle)
: ThreadedUsbSender(usb_device, usb_handle),
m_adaptor(adaptor), m_usb_handle(usb_handle) {
}
bool SiudiThreadedSender::Start() {
if (!ThreadedUsbSender::Start()) {
return false;
}
// Read device info. This call takes about 270 ms.
// Discard the buffer as the format is currently unknown.
uint8_t buf[DEVINFO_SIZE];
int ret = libusb_control_transfer(m_usb_handle,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
DEVINFO_REQUEST, 0x0000, 1, buf, DEVINFO_SIZE, CONTROL_TIMEOUT);
if (ret != DEVINFO_SIZE) {
OLA_WARN << "Failed to read SIUDI information: "
<< (ret < 0 ? LibUsbAdaptor::ErrorCodeToString(ret) : "Short read");
return false;
}
if (ola::OLA_LOG_DEBUG <= ola::LogLevel()) {
ola::LogLine(__FILE__, __LINE__, ola::OLA_LOG_DEBUG).stream() << "SIUDI device info:";
ola::strings::FormatData(&std::cout, buf, DEVINFO_SIZE, 4);
ola::LogLine(__FILE__, __LINE__, ola::OLA_LOG_DEBUG).stream() << "If you know how to interprete this, please let us know.";
}
// Unstall the endpoint. The original software seems to call this regularly.
ret = libusb_clear_halt(m_usb_handle, ENDPOINT);
if (ret != 0) {
OLA_WARN << "Failed to reset SIUDI endpoint: "
<< (ret < 0 ? LibUsbAdaptor::ErrorCodeToString(ret) : "Unknown");
return false;
}
usleep(BULK_DELAY); // Might receive errors if writing too early.
return true;
}
bool SiudiThreadedSender::TransmitBuffer(libusb_device_handle *handle,
const DmxBuffer &buffer) {
int transferred, r;
if (buffer.Size() == ola::DMX_UNIVERSE_SIZE) {
// As we are sending, we can cast the const buffer to a writeable pointer.
r = m_adaptor->BulkTransfer(
handle, ENDPOINT, (unsigned char*)buffer.GetRaw(),
ola::DMX_UNIVERSE_SIZE, &transferred, BULK_TIMEOUT);
} else {
unsigned char buf[ola::DMX_UNIVERSE_SIZE];
unsigned int buf_get_size = ola::DMX_UNIVERSE_SIZE;
buffer.GetRange(0, buf, &buf_get_size);
if (buf_get_size < ola::DMX_UNIVERSE_SIZE) {
memset(&buf[buf_get_size], 0x00, ola::DMX_UNIVERSE_SIZE - buf_get_size);
}
r = m_adaptor->BulkTransfer(
handle, ENDPOINT, buf, ola::DMX_UNIVERSE_SIZE, &transferred, BULK_TIMEOUT);
}
if (transferred != ola::DMX_UNIVERSE_SIZE) {
// not sure if this is fatal or not
OLA_WARN << "SIUDI driver failed to transfer all data";
}
usleep(BULK_DELAY);
return r == 0;
}
// SynchronousSiudi
// -----------------------------------------------------------------------------
SynchronousSiudi::SynchronousSiudi(LibUsbAdaptor *adaptor,
libusb_device *usb_device)
: Siudi(adaptor, usb_device) {
}
bool SynchronousSiudi::Init() {
libusb_device_handle *usb_handle;
bool ok = m_adaptor->OpenDeviceAndClaimInterface(
m_usb_device, 0, &usb_handle);
if (!ok) {
return false;
}
std::auto_ptr<SiudiThreadedSender> sender(
new SiudiThreadedSender(m_adaptor, m_usb_device, usb_handle));
if (!sender->Start()) {
return false;
}
m_sender.reset(sender.release());
return true;
}
bool SynchronousSiudi::SendDMX(const DmxBuffer &buffer) {
return m_sender.get() ? m_sender->SendDMX(buffer) : false;
}
} // namespace usbdmx
} // namespace plugin
} // namespace ola