aboutsummaryrefslogtreecommitdiffhomepage
path: root/backends/openpixelcontrol.c
blob: 062c015f1886fcccef782a07cd0a1dd38f257a5c (plain)
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
#define BACKEND_NAME "openpixelcontrol"

#include <string.h>

#include "libmmbackend.h"
#include "openpixelcontrol.h"

/*
 * TODO handle destination close/unregister/reopen
 */

MM_PLUGIN_API int init(){
	backend openpixel = {
		.name = BACKEND_NAME,
		.conf = openpixel_configure,
		.create = openpixel_instance,
		.conf_instance = openpixel_configure_instance,
		.channel = openpixel_channel,
		.handle = openpixel_set,
		.process = openpixel_handle,
		.start = openpixel_start,
		.shutdown = openpixel_shutdown
	};

	//register backend
	if(mm_backend_register(openpixel)){
		LOG("Failed to register backend");
		return 1;
	}
	return 0;
}

static int openpixel_configure(char* option, char* value){
	//no global configuration
	LOG("No backend configuration possible");
	return 1;
}

static int openpixel_configure_instance(instance* inst, char* option, char* value){
	char* host = NULL, *port = NULL;
	openpixel_instance_data* data = (openpixel_instance_data*) inst->impl;

	//FIXME this should store the destination/listen address and establish on _start
	if(!strcmp(option, "destination")){
		mmbackend_parse_hostspec(value, &host, &port, NULL);
		if(!host || !port){
			LOGPF("Invalid destination address specified for instance %s", inst->name);
			return 1;
		}

		data->dest_fd = mmbackend_socket(host, port, SOCK_STREAM, 0, 0);
		if(data->dest_fd >= 0){
			return 0;
		}
		return 1;
	}
	if(!strcmp(option, "listen")){
		mmbackend_parse_hostspec(value, &host, &port, NULL);
		if(!host || !port){
			LOGPF("Invalid listen address specified for instance %s", inst->name);
			return 1;
		}

		data->listen_fd = mmbackend_socket(host, port, SOCK_STREAM, 1, 0);
		if(data->listen_fd >= 0 && listen(data->listen_fd, SOMAXCONN)){
			return 0;
		}
		return 1;
	}
	else if(!strcmp(option, "mode")){
		if(!strcmp(value, "16bit")){
			data->mode = rgb16;
			return 0;
		}
		else if(!strcmp(value, "8bit")){
			data->mode = rgb8;
			return 0;
		}
		LOGPF("Unknown instance mode %s\n", value);
		return 1;
	}

	LOGPF("Unknown instance option %s for instance %s", option, inst->name);
	return 1;
}

static int openpixel_instance(instance* inst){
	openpixel_instance_data* data = calloc(1, sizeof(openpixel_instance_data));
	inst->impl = data;
	if(!inst->impl){
		LOG("Failed to allocate memory");
		return 1;
	}

	data->dest_fd = -1;
	data->listen_fd = -1;
	return 0;
}

static ssize_t openpixel_buffer_find(openpixel_instance_data* data, uint8_t strip, uint8_t input){
	ssize_t n = 0;

	for(n = 0; n < data->buffers; n++){
		if(data->buffer[n].strip == strip
				&& (data->buffer[n].flags & OPENPIXEL_INPUT) >= input){
			return n;
		}
	}
	return -1;
}

static int openpixel_buffer_extend(openpixel_instance_data* data, uint8_t strip, uint8_t input, uint8_t length){
	ssize_t buffer = openpixel_buffer_find(data, strip, input);
	length = (length % 3) ? ((length / 3) + 1) * 3 : length;
	size_t bytes_required = (data->mode == rgb8) ? length : length * 2;
	if(buffer < 0){
		//allocate new buffer
		data->buffer = realloc(data->buffer, (data->buffers + 1) * sizeof(openpixel_buffer));
		if(!data->buffer){
			data->buffers = 0;
			LOG("Failed to allocate memory");
			return -1;
		}

		buffer = data->buffers;
		data->buffers++;

		data->buffer[buffer].strip = strip;
		data->buffer[buffer].flags = input ? OPENPIXEL_INPUT : 0;
		data->buffer[buffer].bytes = 0;
		data->buffer[buffer].data.u8 = NULL;
	}

	if(data->buffer[buffer].bytes < bytes_required){
		//resize buffer
		data->buffer[buffer].data.u8 = realloc(data->buffer[buffer].data.u8, bytes_required);
		if(!data->buffer[buffer].data.u8){
			data->buffer[buffer].bytes = 0;
			LOG("Failed to allocate memory");
			return 1;
		}
		//FIXME might want to memset() only newly allocated channels
		memset(data->buffer[buffer].data.u8, 0, bytes_required);
		data->buffer[buffer].bytes = bytes_required;
	}
	return 0;
}

static channel* openpixel_channel(instance* inst, char* spec, uint8_t flags){
	uint32_t strip = 0, channel = 0;
	char* token = spec;
	openpixel_instance_data* data = (openpixel_instance_data*) inst->impl;

	//read strip index if supplied
	if(!strncmp(spec, "strip", 5)){
		strip = strtoul(spec + 5, &token, 10);
		//skip the dot
		token++;
	}

	//read (and calculate) channel index
	if(!strncmp(token, "channel", 7)){
		channel = strtoul(token + 7, NULL, 10);
	}
	else if(!strncmp(token, "red", 3)){
		channel = strtoul(token + 3, NULL, 10) * 3 - 2;
	}
	else if(!strncmp(token, "green", 5)){
		channel = strtoul(token + 5, NULL, 10) * 3 - 1;
	}
	else if(!strncmp(token, "blue", 4)){
		channel = strtoul(token + 4, NULL, 10) * 3;
	}

	if(!channel){
		LOGPF("Invalid channel specification %s", spec);
		return NULL;
	}

	//check channel direction
	if(flags & mmchannel_input){
		//strip 0 (bcast) can not be mapped as input
		if(!strip){
			LOGPF("Broadcast channel %s.%s can not be mapped as an input", inst->name, spec);
			return NULL;
		}
		if(data->listen_fd < 0){
			LOGPF("Channel %s mapped as input, but instance %s is not accepting input", spec, inst->name);
			return NULL;
		}

		if(openpixel_buffer_extend(data, strip, 1, channel)){
			return NULL;
		}
	}

	if(flags & mmchannel_output){
		if(data->dest_fd < 0){
			LOGPF("Channel %s mapped as output, but instance %s is not sending output", spec, inst->name);
			return NULL;
		}

		if(openpixel_buffer_extend(data, strip, 0, channel)){
			return NULL;
		}
	}

	return mm_channel(inst, ((uint64_t) strip) << 32 | channel, 1);
}

static int openpixel_set(instance* inst, size_t num, channel** c, channel_value* v){
	openpixel_instance_data* data = (openpixel_instance_data*) inst->impl;
	size_t u, p;
	ssize_t buffer;
	uint32_t strip, channel;
	openpixel_header hdr;

	for(u = 0; u < num; u++){
		//read strip/channel
		strip = c[u]->ident >> 32;
		channel = c[u]->ident & 0xFFFFFFFF;
		channel--;

		//find the buffer
		buffer = openpixel_buffer_find(data, strip, 0);
		if(buffer < 0){
			LOGPF("No buffer for channel %s.%d.%d\n", inst->name, strip, channel);
			continue;
		}

		//mark buffer for output
		data->buffer[buffer].flags |= OPENPIXEL_MARK;

		//update data
		switch(data->mode){
			case rgb8:
				data->buffer[buffer].data.u8[channel] = ((uint8_t)(v[u].normalised * 255.0));
				break;
			case rgb16:
				data->buffer[buffer].data.u16[channel] = ((uint16_t)(v[u].normalised * 65535.0));
				break;
		}

		if(strip == 0){
			//update values in all other output strips, dont mark
			for(p = 0; p < data->buffers; p++){
				if(!(data->buffer[p].flags & OPENPIXEL_INPUT)){
					//check whether the buffer is large enough
					if(data->mode == rgb8 && data->buffer[p].bytes >= channel){
						data->buffer[p].data.u8[channel] = ((uint8_t)(v[u].normalised * 255.0));
					}
					else if(data->mode == rgb16 && data->buffer[p].bytes >= channel * 2){
						data->buffer[p].data.u16[channel] = ((uint16_t)(v[u].normalised * 65535.0));
					}
				}
			}
		}
	}

	//send updated strips
	for(u = 0; u < data->buffers; u++){
		if(!(data->buffer[u].flags & OPENPIXEL_INPUT) && (data->buffer[u].flags & OPENPIXEL_MARK)){
			//remove mark
			data->buffer[u].flags &= ~OPENPIXEL_MARK;

			//prepare header
			hdr.strip = data->buffer[u].strip;
			hdr.mode = data->mode;
			hdr.length = htobe16(data->buffer[u].bytes);

			//output data
			if(mmbackend_send(data->dest_fd, (uint8_t*) &hdr, sizeof(hdr))
					|| mmbackend_send(data->dest_fd, data->buffer[u].data.u8, data->buffer[u].bytes)){
				return 1;
			}
		}
	}
	return 0;
}

static int openpixel_handle(size_t num, managed_fd* fds){
	//TODO handle bcast
	return 0;
}

static int openpixel_start(size_t n, instance** inst){
	int rv = -1;
	size_t u, nfds = 0;
	openpixel_instance_data* data = NULL;

	for(u = 0; u < n; u++){
		data = (openpixel_instance_data*) inst[u]->impl;

		//register fds
		if(data->dest_fd >= 0){
			if(mm_manage_fd(data->dest_fd, BACKEND_NAME, 1, inst[u])){
				LOGPF("Failed to register destination descriptor for instance %s with core", inst[u]->name);
				goto bail;
			}
			nfds++;
		}
		if(data->listen_fd >= 0){
			if(mm_manage_fd(data->listen_fd, BACKEND_NAME, 1, inst[u])){
				LOGPF("Failed to register host descriptor for instance %s with core", inst[u]->name);
				goto bail;
			}
			nfds++;
		}
	}

	LOGPF("Registered %" PRIsize_t " descriptors to core", nfds);
	rv = 0;
bail:
	return rv;
}

static int openpixel_shutdown(size_t n, instance** inst){
	size_t u, p;
	openpixel_instance_data* data = NULL;

	for(u = 0; u < n; u++){
		data = (openpixel_instance_data*) inst[u]->impl;

		//shutdown all clients
		for(p = 0; p < data->clients; p++){
			if(data->client_fd[p] >= 0){
				close(data->client_fd[p]);
			}
		}
		free(data->client_fd);
		free(data->bytes_left);

		//close all configured fds
		if(data->listen_fd >= 0){
			close(data->listen_fd);
		}
		if(data->dest_fd >= 0){
			close(data->dest_fd);
		}

		//free all buffers
		for(p = 0; p < data->buffers; p++){
			free(data->buffer[p].data.u8);
		}
		free(data->buffer);

		free(data);
		inst[u]->impl = NULL;
	}

	LOG("Backend shut down");
	return 0;
}