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
|
#include "ola.h"
#include <cstring>
#include <ola/DmxBuffer.h>
#include <ola/Logging.h>
#include <ola/OlaClientWrapper.h>
#include <ola/client/OlaClient.h>
#include <ola/io/SelectServer.h>
#include <ola/network/Socket.h>
#define BACKEND_NAME "ola"
static ola::io::SelectServer* ola_select = NULL;
static ola::OlaCallbackClient* ola_client = NULL;
int init(){
backend ola = {
.name = BACKEND_NAME,
.conf = ola_configure,
.create = ola_instance,
.conf_instance = ola_configure_instance,
.channel = ola_channel,
.handle = ola_set,
.process = ola_handle,
.start = ola_start,
.shutdown = ola_shutdown
};
//register backend
if(mm_backend_register(ola)){
fprintf(stderr, "Failed to register OLA backend\n");
return 1;
}
ola::InitLogging(ola::OLA_LOG_WARN, ola::OLA_LOG_STDERR);
return 0;
}
static int ola_configure(char* option, char* value){
fprintf(stderr, "Unknown OLA backend option %s\n", option);
return 1;
}
static instance* ola_instance(){
ola_instance_data* data = NULL;
instance* inst = mm_instance();
if(!inst){
return NULL;
}
data = (ola_instance_data*)calloc(1, sizeof(ola_instance_data));
if(!data){
fprintf(stderr, "Failed to allocate memory\n");
return NULL;
}
inst->impl = data;
return inst;
}
static int ola_configure_instance(instance* inst, char* option, char* value){
ola_instance_data* data = (ola_instance_data*) inst->impl;
if(!strcmp(option, "universe")){
data->universe_id = strtoul(value, NULL, 0);
return 0;
}
fprintf(stderr, "Unknown OLA option %s for instance %s\n", option, inst->name);
return 1;
}
static channel* ola_channel(instance* inst, char* spec){
ola_instance_data* data = (ola_instance_data*) inst->impl;
char* spec_next = spec;
unsigned chan_a = strtoul(spec, &spec_next, 10);
unsigned chan_b = 0;
//primary channel sanity check
if(!chan_a || chan_a > 512){
fprintf(stderr, "Invalid OLA channel specification %s\n", spec);
return NULL;
}
chan_a--;
//secondary channel setup
if(*spec_next == '+'){
chan_b = strtoul(spec_next + 1, NULL, 10);
if(!chan_b || chan_b > 512){
fprintf(stderr, "Invalid wide-channel spec %s\n", spec);
return NULL;
}
chan_b--;
//if mapped mode differs, bail
if(IS_ACTIVE(data->data.map[chan_b]) && data->data.map[chan_b] != (MAP_FINE | chan_a)){
fprintf(stderr, "Fine channel already mapped for OLA spec %s\n", spec);
return NULL;
}
data->data.map[chan_b] = MAP_FINE | chan_a;
}
//check current map mode
if(IS_ACTIVE(data->data.map[chan_a])){
if((*spec_next == '+' && data->data.map[chan_a] != (MAP_COARSE | chan_b))
|| (*spec_next != '+' && data->data.map[chan_a] != (MAP_SINGLE | chan_a))){
fprintf(stderr, "Primary OLA channel already mapped at differing mode: %s\n", spec);
return NULL;
}
}
data->data.map[chan_a] = (*spec_next == '+') ? (MAP_COARSE | chan_b) : (MAP_SINGLE | chan_a);
return mm_channel(inst, chan_a, 1);
}
static int ola_set(instance* inst, size_t num, channel** c, channel_value* v){
size_t u, mark = 0;
ola_instance_data* data = (ola_instance_data*) inst->impl;
for(u = 0; u < num; u++){
if(IS_WIDE(data->data.map[c[u]->ident])){
uint32_t val = v[u].normalised * ((double) 0xFFFF);
//the primary (coarse) channel is the one registered to the core, so we don't have to check for that
if(data->data.data[c[u]->ident] != ((val >> 8) & 0xFF)){
mark = 1;
data->data.data[c[u]->ident] = (val >> 8) & 0xFF;
}
if(data->data.data[MAPPED_CHANNEL(data->data.map[c[u]->ident])] != (val & 0xFF)){
mark = 1;
data->data.data[MAPPED_CHANNEL(data->data.map[c[u]->ident])] = val & 0xFF;
}
}
else if(data->data.data[c[u]->ident] != (v[u].normalised * 255.0)){
mark = 1;
data->data.data[c[u]->ident] = v[u].normalised * 255.0;
}
}
if(mark){
ola_client->SendDmx(data->universe_id, ola::DmxBuffer(data->data.data, 512));
}
return 0;
}
static int ola_handle(size_t num, managed_fd* fds){
if(!num){
return 0;
}
//defer input to ola via the scenic route...
ola_select->RunOnce();
return 0;
}
static void ola_data_receive(unsigned int universe, const ola::DmxBuffer& ola_dmx, const std::string& error) {
size_t p, max_mark = 0;
//this should really be size_t but ola is weird...
unsigned int dmx_length = 512;
uint8_t raw_dmx[dmx_length];
uint16_t wide_val;
channel* chan = NULL;
channel_value val;
instance* inst = mm_instance_find(BACKEND_NAME, universe);
if(!inst){
return;
}
ola_instance_data* data = (ola_instance_data*) inst->impl;
ola_dmx.Get((uint8_t*)raw_dmx, &dmx_length);
//read data into instance universe, mark changed channels
for(p = 0; p < dmx_length; p++){
if(IS_ACTIVE(data->data.map[p]) && raw_dmx[p] != data->data.data[p]){
data->data.data[p] = raw_dmx[p];
data->data.map[p] |= MAP_MARK;
max_mark = p;
}
}
//generate channel events
for(p = 0; p <= max_mark; p++){
if(data->data.map[p] & MAP_MARK){
data->data.map[p] &= ~MAP_MARK;
if(data->data.map[p] & MAP_FINE){
chan = mm_channel(inst, MAPPED_CHANNEL(data->data.map[p]), 0);
}
else{
chan = mm_channel(inst, p, 0);
}
if(!chan){
fprintf(stderr, "Active channel %zu on %s not known to core\n", p, inst->name);
return;
}
if(IS_WIDE(data->data.map[p])){
data->data.map[MAPPED_CHANNEL(data->data.map[p])] &= ~MAP_MARK;
wide_val = data->data.data[p] << ((data->data.map[p] & MAP_COARSE) ? 8 : 0);
wide_val |= data->data.data[MAPPED_CHANNEL(data->data.map[p])] << ((data->data.map[p] & MAP_COARSE) ? 0 : 8);
val.raw.u64 = wide_val;
val.normalised = (double) wide_val / (double) 0xFFFF;
}
else{
val.raw.u64 = data->data.data[p];
val.normalised = (double) data->data.data[p] / 255.0;
}
if(mm_channel_event(chan, val)){
fprintf(stderr, "Failed to push OLA channel event to core\n");
return;
}
}
}
}
static void ola_register_callback(const std::string &error) {
if(!error.empty()){
fprintf(stderr, "OLA backend failed to register for universe: %s\n", error.c_str());
}
}
static int ola_start(){
size_t n, u, p;
instance** inst = NULL;
ola_instance_data* data = NULL;
ola_select = new ola::io::SelectServer();
ola::network::IPV4SocketAddress ola_server(ola::network::IPV4Address::Loopback(), ola::OLA_DEFAULT_PORT);
ola::network::TCPSocket* ola_socket = ola::network::TCPSocket::Connect(ola_server);
if(!ola_socket){
fprintf(stderr, "Failed to connect to OLA server\n");
return 1;
}
ola_client = new ola::OlaCallbackClient(ola_socket);
if(!ola_client->Setup()){
fprintf(stderr, "Failed to start OLA client\n");
goto bail;
}
ola_select->AddReadDescriptor(ola_socket);
fprintf(stderr, "OLA backend registering connection descriptor to core\n");
if(mm_manage_fd(ola_socket->ReadDescriptor(), BACKEND_NAME, 1, NULL)){
goto bail;
}
ola_client->SetDmxCallback(ola::NewCallback(&ola_data_receive));
//fetch all defined instances
if(mm_backend_instances(BACKEND_NAME, &n, &inst)){
fprintf(stderr, "Failed to fetch instance list\n");
goto bail;
}
//this should not happen anymore (backends without instances are not started anymore)
if(!n){
free(inst);
return 0;
}
for(u = 0; u < n; u++){
data = (ola_instance_data*) inst[u]->impl;
inst[u]->ident = data->universe_id;
//check for duplicate instances (using the same universe)
for(p = 0; p < u; p++){
if(inst[u]->ident == inst[p]->ident){
fprintf(stderr, "OLA universe used in multiple instances, use one instance: %s - %s\n", inst[u]->name, inst[p]->name);
goto bail;
}
}
ola_client->RegisterUniverse(data->universe_id, ola::REGISTER, ola::NewSingleCallback(&ola_register_callback));
}
//run the ola select implementation to run all commands
ola_select->RunOnce();
free(inst);
return 0;
bail:
free(inst);
delete ola_client;
ola_client = NULL;
delete ola_select;
ola_select = NULL;
return 1;
}
static int ola_shutdown(){
size_t n, p;
instance** inst = NULL;
if(mm_backend_instances(BACKEND_NAME, &n, &inst)){
fprintf(stderr, "Failed to fetch instance list\n");
return 1;
}
for(p = 0; p < n; p++){
free(inst[p]->impl);
}
free(inst);
if(ola_client){
ola_client->Stop();
delete ola_client;
ola_client = NULL;
}
if(ola_select){
ola_select->Terminate();
delete ola_select;
ola_select = NULL;
}
fprintf(stderr, "OLA backend shut down\n");
return 0;
}
|