GCC Code Coverage Report


Directory: ./
File: channeloutput/ColorLight-5a-75.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 532 581 91.6%
Functions: 22 22 100.0%
Branches: 399 709 56.3%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2025 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the GPL v2 as described in the
10 * included LICENSE.GPL file.
11 */
12
13 /*
14 * Packet Format:
15 *
16 * All sent packets have the same source and destination MAC addresses:
17 * - Destination MAC: 11:22:33:44:55:66
18 * - Source MAC: 22:22:33:44:55:66 (doesn't seem necessary to harcoded this)
19 *
20 * Received packets are broadcast with a source MAC of 22:22:33:44:55:66, dest of FF:FF:FF:FF:FF:FF
21 *
22 * Packet Type is a single byte. First byte of data is stored with the packet
23 * type in the 'ether_type' field in the ethernet header. Data Byte count
24 * includes first byte of data stored in the ether_type field.
25 *
26 * Packet types:
27 * - 0x01 - Display/Sync Frame (sent twice for v13+ FW) (CL_SYNC_PACKET_TYPE)
28 * Older FW accepts this twice from LEDVision but is timing sensitive.
29 * FPP only sends once for older firmware.
30 * - Data Bytes: 99 (CL_SYNC_PACKET_SIZE - 13)
31 * - Data[0]: 0x00 - Sent from S2 Sender
32 * 0x07 - Sent from PC/Netcard
33 * - Data[22]: 0xff - Brightness
34 * - Data[23]: 0x00 - Value in 0x01 packet from S2 Sender
35 * 0x05 - Value in 0x01 packet from PC/Netcard
36 * - Data[25]: 0xff - Also brightness related
37 * - Data[26]: 0xff - Also brightness related
38 * - Data[27]: 0xff - Also brightness related
39 *
40 * 0x02 - Write receiver layout (dual 32x16 receivers, #0 @ 0,0, #1 @ 32,0
41 * - Data[0]: 0x00 - Receiver #
42 * - Data[1]: 0x00 - Receiver #
43 * - Data[2]: 0x00 - Receiver #
44 *
45 * Receiver #0 (of 64, #0-63)
46 * - Data[3]: 0x00 - Receiver #
47 * - Data[4]: 0x00 - Receiver #
48 * - Data[5]: 0x00 - Receiver #
49 * - Data[6]: 0x00 - Receiver #
50 * - Data[7]: 0x00 - Receiver # receiver width MSB
51 * - Data[8]: 0x20 - Receiver # receiver width LSB
52 * - Data[9]: 0x00 - Receiver # receiver height MSB
53 * - Data[10]: 0x10 - Receiver # receiver height LSB
54 * - Data[11]: 0x00 - Receiver #
55 * - Data[12]: 0x00 - Receiver #
56 * - Data[13]: 0x00 - Receiver # next receiver xOffset MSB
57 * - Data[14]: 0x20 - Receiver # next receiver xOffset LSB
58 * - Data[15]: 0x00 - Receiver # next receiver yOffset MSB
59 * - Data[16]: 0x00 - Receiver # next receiver yOffset LSB
60 * - Data[17]: 0x00 - Receiver # total display width MSB
61 * - Data[18]: 0x40 - Receiver # total display width LSB
62 * - Data[19]: 0x00 - Receiver # total display height MSB
63 * - Data[20]: 0x10 - Receiver # total display height LSB
64 * - Data[21]: 0x00 - Receiver #
65 * - Data[22]: 0x00 - Receiver #
66 *
67 * Receiver #1-63)
68 * - repeating 20 bytes of data in same format as receiver #0
69 *
70 * 0x07 - Discover (CL_DISC_PACKET_TYPE)
71 * - Data Bytes: 271 (CL_DISC_PACKET_SIZE - 13)
72 * - Data[3]: 0x00 - first receiver
73 * 0x01 - second receiver, etc.
74 *
75 * 0x08 - Discover Reply (from receiver to sender) (CL_RESP_PACKET_TYPE)
76 * - Data Bytes: 1057 (CL_RESP_PACKET_SIZE - 13)
77 * - Data[0]: 0x05 - 5A card
78 * - Data[2]: Firmware Version MSB
79 * - Data[3]: Firmware Version LSB
80 * - Data[38]: Received Packet Count? MSB
81 * - Data[39]: Received Packet Count?
82 * - Data[40]: Received Packet Count?
83 * - Data[41]: Received Packet Count? LSB
84 * - Data[46]: Uptime in ms MSB
85 * - Data[47]: Uptime in ms
86 * - Data[48]: Uptime in ms
87 * - Data[49]: Uptime in ms LSB
88 * - Data[85]: Receiver Card # (0x00, 0x01, etc.)
89 *
90 * These don't seem totally correct, offsetting a receiver card doesn't result in values that
91 * match the cabinet width/height fields, so there seems to be some other math going on.
92 * - Data[21]: Cabinet Width? MSB
93 * - Data[22]: Cabinet Width? LSB
94 * - Data[23]: Cabinet Height? MSB
95 * - Data[24]: Cabinet Height? LSB
96 * - Data[??]: Cabinet X-Offset MSB
97 * - Data[??]: Cabinet X-Offset LSB
98 * - Data[??]: Cabinet Y-Offset MSB
99 * - Data[??]: Cabinet Y-Offset LSB
100 *
101 * Data * * * * * * <-- Fields of interest
102 * Offset 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 - Offsets in packet data
103 * 00 00 00 20 00 10 00 00 00 00 00 00 01 02 03 ff - 32x16 size, r0: 32x16 cabinet, offset 0,0
104 * 00 10 00 20 00 20 00 00 00 00 00 00 01 02 03 ff - 32x32 size, r1: 32x16 cabinet, offset 0,16
105 *
106 * 00 00 00 20 00 10 00 00 00 00 00 00 01 02 03 ff - 32x16 size, r0: 32x16 cabinet, offset 0,0
107 * 00 10 00 40 00 20 00 00 00 00 00 00 01 02 03 ff - 64x32 size, r1: 32x16 cabinet, offset 32,16
108 *
109 * 00 00 00 20 00 10 00 00 00 00 00 00 01 02 03 ff - 32x16 size, r0: 32x16 cabinet, offset 0,0
110 * 00 10 00 20 00 20 00 00 00 00 00 00 01 02 03 ff - 64x16 size, r1: 32x16 cabinet, offset 32,0
111 *
112 * 0x10 - ???? - sent when sending config to receiver
113 * - Data Length: 1026
114 *
115 * 0x11 - Save Config??
116 * - Data Bytes: 1283
117 * - Data[3]: 0x00 - first receiver, 0x01 2nd, etc.
118 *
119 * 0x18 - ???? - sent when sending config to receiver
120 *
121 * 0x1F - ???? - sent when sending config to receiver
122 * - Data Length: 1034
123 *
124 * 0x26 - ???? - sent when sending config to receiver
125 * - Data Length: 264
126 *
127 * 0x31 - ???? - sent when sending config to receiver. Some kind of table, 2 bytes per entry?
128 *
129 * 0x32 - ???? - sent when sending config to receiver
130 *
131 * 0x76 - ???? - sent when sending config to receiver. Some kind of table, 3 bytes per entry?
132 *
133 * 0x0A - Panel Brightness (sent twice for v13+ FW, but possbily uncessary) (CL_BRIG_PACKET_TYPE)
134 * - Data Length: 64 (CL_BRIG_PACKET_SIZE - 13)
135 * - Data[0]: 0xFF - Brightness
136 * - Data[1]: 0xFF - Brightness
137 * - Data[2]: 0xFF - Brightness
138 * - Data[3]: 0xFF
139 *
140 * 0x55 - Pixel Data (sent twice per row for v13+ FW, but seems uncessary to do so) (CL_PIXL_PACKET_TYPE)
141 * - Data Length: CL_PIXL_HEADER_SIZE + (Row_Width * 3)
142 * - Data[0]: Row Number MSB
143 * - Data[1]: Row Number LSB
144 * - Data[2]: MSB of pixel offset for this packet
145 * - Data[3]: LSB of pixel offset for this packet
146 * - Data[4]: MSB of pixel count in packet
147 * - Data[5]: LSB of pixel count in packet
148 * - Data[6]: 0x08 - ?? unsure what this is
149 * - Data[7]: 0x88 - ?? unsure what this is
150 * - Data[8-end]: RGB order pixel data
151 *
152 * Notes:
153 * - Brightness may only work on newer firmware. Doesn't seem to work on v2.x and v3.x ColorLight FW.
154 *
155 * Other sources of info:
156 * https://www.mylifesucks.de/oss/mplayer-colorlight/
157 * https://hkubota.wordpress.com/2022/01/31/winter-project-colorlight-5a-75b-protocol/
158 */
159 #include "fpp-pch.h"
160
161 #include "fpp-json.h"
162
163 #ifndef PLATFORM_OSX
164 #include <linux/if_packet.h>
165 #include <linux/sockios.h>
166 #include <netinet/ether.h>
167 #else
168 #include <net/bpf.h>
169 #endif
170
171 #include "../SysSocket.h"
172 #include <arpa/inet.h>
173 #include <unistd.h>
174 #include <net/if.h>
175 #include <sys/ioctl.h>
176 #include <sys/socket.h>
177 #include <cmath>
178 #include <errno.h>
179 #include <fstream>
180 #include <iostream>
181
182 #include "../Timers.h"
183 #include "../Warnings.h"
184 #include "../common.h"
185 #include "../log.h"
186 #include "../settings.h"
187
188 #include "ChannelOutputSetup.h"
189 #include "ColorLight-5a-75.h"
190 #include "GammaLUT.h"
191 #include "overlays/PixelOverlay.h"
192
193 #include "Plugin.h"
194
195 static constexpr const char* CL_DROPPED_FRAMES_WARNING = "ColorLight: The network is not keeping up, frames are being dropped";
196
197 #ifdef PLATFORM_OSX
198 int bindBPFSocket(const std::string iface) {
199 char buf[11] = { 0 };
200 int i = 0;
201 int m_fd = -1;
202 for (int i = 0; i < 255; i++) {
203 snprintf(buf, sizeof(buf), "/dev/bpf%i", i);
204 m_fd = open(buf, O_RDWR);
205 if (m_fd != -1) {
206 break;
207 }
208 }
209 if (m_fd == -1) {
210 LogErr(VB_CHANNELOUT, "Error opening bpf file: %s\n", FPPstrerror(errno));
211 return -1;
212 }
213
214 struct ifreq bound_if;
215 memset(&bound_if, 0, sizeof(bound_if));
216 // ifr_name is IFNAMSIZ bytes; callers pass the validated m_ifName, but
217 // guard the copy itself so a future caller cannot reintroduce the overflow.
218 if (iface.size() >= sizeof(bound_if.ifr_name)) {
219 LogErr(VB_CHANNELOUT, "Interface name too long for bind (max %d chars): %s\n",
220 IFNAMSIZ - 1, iface.c_str());
221 close(m_fd);
222 return -1;
223 }
224 strcpy(bound_if.ifr_name, iface.c_str());
225 if (ioctl(m_fd, BIOCSETIF, &bound_if) > 0) {
226 LogErr(VB_CHANNELOUT, "Cannot bind bpf device to physical device %s, exiting\n", iface.c_str());
227 }
228 int yes = 1;
229 ioctl(m_fd, BIOCSHDRCMPLT, &yes);
230 return m_fd;
231 }
232 #endif
233
234 class ColorLight5a75Plugin : public FPPPlugins::Plugin, public FPPPlugins::ChannelOutputPlugin {
235 public:
236 138 ColorLight5a75Plugin() :
237
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
414 FPPPlugins::Plugin("ColorLight5a75") {
238 138 }
239 138 virtual ChannelOutput* createChannelOutput(unsigned int startChannel, unsigned int channelCount) override {
240
2/6
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
276 return new ColorLight5a75Output(startChannel, channelCount);
241 }
242 };
243
244 extern "C" {
245 138 FPPPlugins::Plugin* createPlugin() {
246
2/6
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
276 return new ColorLight5a75Plugin();
247 }
248 }
249
250 /*
251 *
252 */
253 138 ColorLight5a75Output::ColorLight5a75Output(unsigned int startChannel, unsigned int channelCount) :
254 ChannelOutput(startChannel, channelCount),
255 138 m_width(0),
256 138 m_height(0),
257 138 m_colorOrder(FPPColorOrder::kColorOrderRGB),
258 138 m_fd(-1),
259 138 m_rowSize(0),
260 138 m_panelWidth(0),
261 138 m_panelHeight(0),
262 138 m_panels(0),
263 138 m_rows(0),
264 138 m_outputs(0),
265 138 m_longestChain(0),
266 138 m_invertedData(0),
267 138 m_frameCharge(0),
268 138 m_sendBufferSize(0),
269 138 m_frameSent(false),
270 138 m_rowsSentTime(0),
271 138 m_rowsWireUS(0),
272 138 m_syncGapUS(500),
273 138 m_droppedFrames(0),
274 138 m_droppedLogTime(0),
275 138 m_flippedLayout(0),
276 276 m_highestFirmwareVersion(0) {
277
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 LogDebug(VB_CHANNELOUT, "ColorLight5a75Output::ColorLight5a75Output(%u, %u)\n",
278 startChannel, channelCount);
279 138 }
280
281 /*
282 *
283 */
284 552 ColorLight5a75Output::~ColorLight5a75Output() {
285 276 LogDebug(VB_CHANNELOUT, "ColorLight5a75Output::~ColorLight5a75Output()\n");
286
287
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 138 times.
542 for (int i = 0; i < m_synciovecs.size(); i++) {
288 266 free(m_synciovecs[i].iov_base);
289
290 // Higher firmware has duplicate sync packet which re-uses same buffer
291
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 129 times.
266 if (m_highestFirmwareVersion >= 13)
292 8 i++;
293 }
294
295
2/2
✓ Branch 0 taken 2821 times.
✓ Branch 1 taken 138 times.
5918 for (int i = 0; i < m_iovecs.size(); i++) {
296 5642 free(m_iovecs[i].iov_base);
297
298 // Higher firmware has duplicate brightness packet which re-uses same buffer
299
4/4
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 2688 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 129 times.
5642 if ((i == 0) && (m_highestFirmwareVersion >= 13))
300 8 i += 3;
301 else
302 5634 i++; // Every second iovec is either part of the header malloc() or external data
303 }
304
305
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 4 times.
276 if (m_fd >= 0)
306 268 close(m_fd);
307 552 }
308
309 /*
310 *
311 */
312 138 int ColorLight5a75Output::Init(Json::Value config) {
313
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 LogDebug(VB_CHANNELOUT, "ColorLight5a75Output::Init(JSON)\n");
314
315
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 m_panelWidth = config["panelWidth"].asInt();
316
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 m_panelHeight = config["panelHeight"].asInt();
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (!m_panelWidth)
319 ✗ m_panelWidth = 32;
320
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (!m_panelHeight)
322 ✗ m_panelHeight = 16;
323
324
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 m_invertedData = config["invertedData"].asInt();
325
326
3/6
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 138 times.
✗ Branch 5 not taken.
138 m_colorOrder = ColorOrderFromString(config["colorOrder"].asString());
327
328 m_panelMatrix =
329
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 std::make_unique<PanelMatrix>(m_panelWidth, m_panelHeight, m_invertedData);
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (!m_panelMatrix) {
332 ✗ LogErr(VB_CHANNELOUT, "Unable to create PanelMatrix\n");
333 ✗ return 0;
334 }
335
336
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 if (config.isMember("cfgVersion")) {
337
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 m_flippedLayout = config["cfgVersion"].asInt() >= 2 ? 0 : 1;
338 } else {
339 ✗ m_flippedLayout = 1;
340 }
341
342
4/6
✓ Branch 0 taken 326 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 326 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
✓ Branch 5 taken 138 times.
326 for (int i = 0; i < config["panels"].size(); i++) {
343
3/6
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
✗ Branch 5 not taken.
188 Json::Value p = config["panels"][i];
344 188 char orientation = 'N';
345
2/4
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
188 std::string o = p["orientation"].asString();
346
2/4
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
188 if (o != "") {
347 188 orientation = o[0];
348 }
349
350
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 186 times.
188 if (m_flippedLayout) {
351
1/5
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2 switch (orientation) {
352 2 case 'N':
353 2 orientation = 'U';
354 2 break;
355 ✗ case 'U':
356 ✗ orientation = 'N';
357 ✗ break;
358 ✗ case 'R':
359 ✗ orientation = 'L';
360 ✗ break;
361 ✗ case 'L':
362 ✗ orientation = 'R';
363 ✗ break;
364 }
365 }
366
367
4/8
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 188 times.
188 if (p["colorOrder"].asString() == "")
368 ✗ p["colorOrder"] = ColorOrderToString(m_colorOrder);
369
370 // a refused panel (out of range) must not count toward the outputs or chains below
371
4/8
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 188 times.
✗ Branch 7 not taken.
564 if (!m_panelMatrix->AddPanel(p["outputNumber"].asInt(),
372
2/4
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
188 p["panelNumber"].asInt(), orientation,
373
4/8
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 188 times.
✗ Branch 7 not taken.
188 p["xOffset"].asInt(), p["yOffset"].asInt(),
374
4/6
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 185 times.
376 ColorOrderFromString(p["colorOrder"].asString()))) {
375 3 continue;
376 }
377
378
4/6
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 181 times.
185 if (p["outputNumber"].asInt() > m_outputs)
379
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 m_outputs = p["outputNumber"].asInt();
380
381
4/6
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✓ Branch 5 taken 162 times.
185 if (p["panelNumber"].asInt() > m_longestChain)
382
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 m_longestChain = p["panelNumber"].asInt();
383
4/4
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 185 times.
✓ Branch 3 taken 3 times.
191 }
384
385 // Both of these are 0-based, so bump them up by 1 for comparisons
386 138 m_outputs++;
387 138 m_longestChain++;
388
389 138 m_panels = m_panelMatrix->PanelCount();
390
391 138 m_rows = m_outputs * m_panelHeight;
392
393 138 m_width = m_panelMatrix->Width();
394 138 m_height = m_panelMatrix->Height();
395
396 138 m_channelCount = m_width * m_height * 3;
397
398
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 m_outputFrame = std::make_unique<char[]>(m_outputs * m_longestChain * m_panelHeight * m_panelWidth * 3);
399
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 BuildRemap();
400
401
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 m_matrix = std::make_unique<Matrix>(m_startChannel, m_width, m_height);
402
403
3/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 135 times.
138 if (config.isMember("subMatrices")) {
404
4/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
6 for (int i = 0; i < config["subMatrices"].size(); i++) {
405
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 Json::Value sm = config["subMatrices"][i];
406
407
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 m_matrix->AddSubMatrix(
408
1/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 sm["enabled"].asInt(),
409
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 sm["startChannel"].asInt() - 1,
410
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 sm["width"].asInt(),
411
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 sm["height"].asInt(),
412
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 sm["xOffset"].asInt(),
413
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 sm["yOffset"].asInt());
414 3 }
415 }
416
417
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 GammaLUT::Build(m_gammaCurve, GammaLUT::ParseConfig(config, 1.0f));
418
419
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 if (config.isMember("interface"))
420
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 m_ifName = config["interface"].asString();
421 else
422 ✗ m_ifName = "eth1";
423
424 // ifr_name is IFNAMSIZ (16) bytes at every use below; an overlong name
425 // would overflow the stack copies. It never worked (ioctl/sysfs lookups
426 // fail on it), so fail init with an error like other bad config here.
427
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 137 times.
138 if (m_ifName.size() >= IFNAMSIZ) {
428
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_CHANNELOUT, "Error ColorLight: Configured interface name too long (max %d chars): %s\n",
429 IFNAMSIZ - 1, m_ifName.c_str());
430 1 return 0;
431 }
432
433 137 m_rowSize = m_longestChain * m_panelWidth * 3;
434
435 #ifndef PLATFORM_OSX
436
437 // Check if interface is up
438
3/6
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 137 times.
✗ Branch 5 not taken.
274 std::ifstream ifstate_src("/sys/class/net/" + m_ifName + "/operstate");
439 274 std::string ifstate;
440
441
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 if (ifstate_src.is_open()) {
442
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 ifstate_src >> ifstate; // pipe file's content into stream
443
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 ifstate_src.close();
444 }
445
446
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 m_highestFirmwareVersion = getSettingInt("ColorlightFirmwareVersion");
447
2/4
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
137 m_syncGapUS = std::clamp(getSettingInt("ColorlightSyncGapUS", 500), 0, 20000);
448 137 m_colorlightDisable = true;
449
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 m_colorlightDisable = getSettingInt("ColorlightLinkDownDisable") == 1;
450
451
2/4
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
137 if (config.isMember("linkCheck")) {
452
2/4
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
137 m_colorlightDisable = config["linkCheck"].asBool();
453 }
454
3/4
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 80 times.
137 if (config.isMember("firmwareVersion")) {
455
2/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
57 m_highestFirmwareVersion = config["firmwareVersion"].asInt();
456 }
457
458
2/4
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
274 std::string linkWarning = "ColorLight: Configured interface " + m_ifName + " does not have link";
459
3/4
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 50 times.
137 if (ifstate != "up") {
460
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 LogErr(VB_CHANNELOUT, "Error ColorLight: Configured interface %s does not have link\n", m_ifName.c_str());
461
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 84 times.
87 if (m_colorlightDisable) {
462
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 AddOutputConfigWarning(7, linkWarning + " (output disabled until the output is re-saved or fppd restarts)");
463 3 return 0;
464 }
465
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 AddOutputConfigWarning(7, linkWarning);
466 // FIXME FPP 11: boot race, port may still be negotiating, check once more
467 // (safe after the .so unloads only because fppd exits via _exit())
468
3/6
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
84 Timers::INSTANCE.addTimer("ColorLightLinkRetry-" + m_ifName, GetTimeMS() + 10000,
469
3/8
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
168 [ifName = m_ifName, linkWarning]() {
470
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 std::string state = GetFileContents("/sys/class/net/" + ifName + "/operstate");
471
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 TrimWhiteSpace(state);
472
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 if (state == "up") {
473
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_CHANNELOUT, "ColorLight: Interface %s now has link\n", ifName.c_str());
474
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 WarningHolder::RemoveWarning(7, linkWarning);
475 } else {
476
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_CHANNELOUT, "ColorLight: Interface %s still has no link after 10s\n", ifName.c_str());
477 }
478 2 });
479 } else {
480
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
100 WarningHolder::RemoveWarning(7, linkWarning);
481 }
482
483 // Check interface is 1000Mbps capable and display error if not
484 134 ifspeed = 0;
485
3/6
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 134 times.
✗ Branch 5 not taken.
268 std::ifstream ifspeed_src("/sys/class/net/" + m_ifName + "/speed");
486
487
1/2
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
134 if (ifspeed_src.is_open()) { // always check whether the file is open
488
1/2
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
134 ifspeed_src >> ifspeed; // pipe file's content into stream
489
1/2
✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
134 ifspeed_src.close();
490 }
491
492
4/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 53 times.
134 if (ifspeed > 0 && ifspeed < 1000) {
493
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_CHANNELOUT, "Error ColorLight: Configured interface %s is not 1000Mbps Capable\n", m_ifName.c_str());
494
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 AddOutputConfigWarning(8, "ColorLight: Configured interface " + m_ifName + " is not 1000Mbps Capable");
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_colorlightDisable) {
496 ✗ return 0;
497 }
498 } else {
499
4/8
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 133 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 133 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 133 times.
✗ Branch 7 not taken.
266 WarningHolder::RemoveWarning(8, "ColorLight: Configured interface " + m_ifName + " is not 1000Mbps Capable");
500 }
501
502 // Open our raw socket
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if ((m_fd = OpenSocket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
504 ✗ LogErr(VB_CHANNELOUT, "Error creating raw socket: %s\n", FPPstrerror(errno));
505 ✗ return 0;
506 }
507
508 // Get the output interface ID
509 134 memset(&m_if_idx, 0, sizeof(struct ifreq));
510 134 strcpy(m_if_idx.ifr_name, m_ifName.c_str());
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if (ioctl(m_fd, SIOCGIFINDEX, &m_if_idx) < 0) {
512 ✗ LogErr(VB_CHANNELOUT, "Error getting index of %s interface: %s\n",
513 m_ifName.c_str(), FPPstrerror(errno));
514 ✗ return 0;
515 }
516
517 134 m_sock_addr.sll_family = AF_PACKET;
518 134 m_sock_addr.sll_ifindex = m_if_idx.ifr_ifindex;
519 134 m_sock_addr.sll_halen = ETH_ALEN;
520
521 134 unsigned char dhost[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
522 134 memcpy(m_sock_addr.sll_addr, dhost, 6);
523
524 // Force packets out the desired interface
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if ((bind(m_fd, (struct sockaddr*)&m_sock_addr, sizeof(m_sock_addr))) == -1) {
526 ✗ LogErr(VB_CHANNELOUT, "bind() failed\n");
527 ✗ return 0;
528 }
529
530 // 0 is auto-detect, else use the value loaded from the ColorlightFirmwareVersion setting
531
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 49 times.
134 if (!m_highestFirmwareVersion) {
532 // Query the receivers to find out what firmware vesion is installed.
533
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 GetReceiverInfo();
534
535
6/6
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 82 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 84 times.
85 if (!m_receivers.size() && m_colorlightDisable) {
536 1 return 0;
537 }
538 }
539
540 #else
541 m_fd = bindBPFSocket(m_ifName);
542 #endif
543
544 133 unsigned int byteCount = 0;
545 133 unsigned int p = 0;
546 133 unsigned char* header = nullptr;
547 133 unsigned char* data = nullptr;
548 133 unsigned char brightness = 0xff;
549
550
3/4
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 132 times.
133 if (config.isMember("brightness")) {
551
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 brightness = config["brightness"].asInt(); // 0-100% scale
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!brightness)
553 ✗ brightness = 0xFF;
554 else
555 1 brightness = 2.55 * brightness; // Scale up to 0-255
556 }
557
558 /////////////////////////////////////////////////
559 // Prep the brightness and pixel data packets
560 133 int packetCount = 1 + (m_rows * (((int)(m_rowSize - 1) / CL_MAX_CHAN_PER_PACKET) + 1));
561
562
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 129 times.
133 if (m_highestFirmwareVersion >= 13) {
563 4 packetCount += 1; // 0x0A brightness sent twice
564 }
565
566
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 m_msgs.resize(packetCount);
567
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 m_iovecs.resize(packetCount * 2);
568
569 // Brightness packet
570 133 byteCount = CL_BRIG_PACKET_SIZE;
571 133 header = (unsigned char*)malloc(byteCount);
572 133 memset(header, 0, byteCount);
573
574
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 SetHostMACs(header);
575
576 // Packet Type follows Dest MAC, data follows type
577 133 header[CL_PACKET_TYPE_OFFSET] = CL_BRIG_PACKET_TYPE;
578 133 data = header + CL_PACKET_DATA_OFFSET;
579
580 133 data[0] = brightness;
581 133 data[1] = brightness;
582 133 data[2] = brightness;
583 133 data[3] = 0xff;
584
585 // Split this apart since it is grouped with pixel data packets which are split into two iovec entries
586 133 m_iovecs[p * 2].iov_base = header;
587 133 m_iovecs[p * 2].iov_len = CL_PACKET_DATA_OFFSET;
588 133 m_iovecs[p * 2 + 1].iov_base = header + CL_PACKET_DATA_OFFSET;
589 133 m_iovecs[p * 2 + 1].iov_len = byteCount - CL_PACKET_DATA_OFFSET;
590 133 p++;
591
592
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 129 times.
133 if (m_highestFirmwareVersion >= 13) {
593 // Send duplicate brightness packet
594 4 m_iovecs[p * 2].iov_base = m_iovecs[(p - 1) * 2].iov_base;
595 4 m_iovecs[p * 2].iov_len = m_iovecs[(p - 1) * 2].iov_len;
596 4 m_iovecs[p * 2 + 1].iov_base = m_iovecs[(p - 1) * 2 + 1].iov_base;
597 4 m_iovecs[p * 2 + 1].iov_len = m_iovecs[(p - 1) * 2 + 1].iov_len;
598 4 p++;
599 }
600
601 // Pixel Data row packets - Type 0x55
602 133 char* rowPtr = m_outputFrame.get();
603 133 unsigned int dSize = 0;
604 133 unsigned int part = 0;
605 133 unsigned int hSize = CL_PACKET_DATA_OFFSET + CL_PIXL_HEADER_SIZE;
606 133 unsigned int offset = 0;
607 133 unsigned int bytesInPacket = 0;
608 133 unsigned int pixelOffset = 0;
609 133 unsigned int pixelsInPacket = 0;
610
611
2/2
✓ Branch 0 taken 2656 times.
✓ Branch 1 taken 133 times.
2789 for (int row = 0; row < m_rows; row++) {
612 2656 part = 0;
613 2656 offset = 0;
614
615
2/2
✓ Branch 0 taken 2688 times.
✓ Branch 1 taken 2656 times.
5344 while (offset < m_rowSize) {
616 2688 header = (unsigned char*)malloc(hSize);
617 2688 memset(header, 0, hSize);
618
619
1/2
✓ Branch 0 taken 2688 times.
✗ Branch 1 not taken.
2688 SetHostMACs(header);
620
621 // Packet Type follows Dest MAC, data follows type
622 2688 header[CL_PACKET_TYPE_OFFSET] = CL_PIXL_PACKET_TYPE;
623 2688 data = header + CL_PACKET_DATA_OFFSET;
624
625
2/2
✓ Branch 0 taken 2656 times.
✓ Branch 1 taken 32 times.
2688 if ((offset + CL_MAX_CHAN_PER_PACKET) > m_rowSize)
626 2656 bytesInPacket = m_rowSize - offset;
627 else
628 32 bytesInPacket = CL_MAX_CHAN_PER_PACKET;
629
630 2688 pixelOffset = offset / 3;
631 2688 pixelsInPacket = bytesInPacket / 3;
632
633 2688 data[0] = row >> 8; // Pixel Row MSB
634 2688 data[1] = row & 0xFF; // Pixel Row LSB
635 2688 data[2] = pixelOffset >> 8; // Pixel Offset MSB
636 2688 data[3] = pixelOffset & 0xFF; // Pixel Offset LSB
637 2688 data[4] = pixelsInPacket >> 8; // Pixels In Packet MSB
638 2688 data[5] = pixelsInPacket & 0xFF; // Pixels In Packet LSB
639 2688 data[6] = 0x08; // ?? not sure what this value is
640 2688 data[7] = 0x88; // ?? not sure what this value is
641
642 2688 m_iovecs[p * 2].iov_base = header;
643 2688 m_iovecs[p * 2].iov_len = hSize;
644 2688 m_iovecs[p * 2 + 1].iov_base = rowPtr + offset;
645 2688 m_iovecs[p * 2 + 1].iov_len = bytesInPacket;
646 2688 p++;
647
648 #if 0
649 // LEDVision sends data rows duplicated as well, but doesn't seem necessary.
650 // If this is enabled, the packetCount needs to be adjusted when set above.
651 if (0 && m_highestFirmwareVersion >= 13) {
652 // Send duplicate row data packet
653 m_iovecs[p * 2].iov_base = m_iovecs[(p-1) * 2].iov_base;
654 m_iovecs[p * 2].iov_len = m_iovecs[(p-1) * 2].iov_len;
655 m_iovecs[p * 2 + 1].iov_base = m_iovecs[(p-1) * 2 + 1].iov_base;
656 m_iovecs[p * 2 + 1].iov_len = m_iovecs[(p-1) * 2 + 1].iov_len;
657 p++;
658 }
659 #endif
660
661 2688 offset += bytesInPacket;
662 2688 part++;
663 }
664
665 2656 rowPtr += m_rowSize;
666 }
667
668
2/2
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 133 times.
2958 for (int m = 0; m < packetCount; m++) {
669 struct mmsghdr msg;
670 2825 memset(&msg, 0, sizeof(msg));
671 2825 msg.msg_hdr.msg_iov = &m_iovecs[m * 2];
672 2825 msg.msg_hdr.msg_iovlen = 2;
673 2825 m_msgs[m] = msg;
674 }
675
676 ////////////////////////////////////////////////////////////////////
677 // Prep the Display Frame packet(s) - sent in SendData()
678
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 129 times.
133 if (m_highestFirmwareVersion >= 13) {
679
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 m_syncMsgs.resize(2);
680
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 m_synciovecs.resize(2);
681 } else {
682
1/2
✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
129 m_syncMsgs.resize(1);
683
1/2
✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
129 m_synciovecs.resize(1);
684 }
685
686 133 p = 0;
687 133 byteCount = CL_SYNC_PACKET_SIZE;
688 133 header = (unsigned char*)malloc(byteCount);
689 133 memset(header, 0, byteCount);
690
691
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 SetHostMACs(header);
692
693 // Packet Type follows Dest MAC, data follows type
694 133 header[CL_PACKET_TYPE_OFFSET] = CL_SYNC_PACKET_TYPE;
695 133 data = header + CL_PACKET_DATA_OFFSET;
696
697 133 data[0] = 0x07;
698
699 133 data[22] = brightness;
700 133 data[23] = 0x05; // ?? not sure what this value is
701
702 133 data[25] = brightness;
703 133 data[26] = brightness;
704 133 data[27] = brightness;
705
706 133 m_synciovecs[p].iov_base = header;
707 133 m_synciovecs[p].iov_len = byteCount;
708 133 p++;
709
710
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 129 times.
133 if (m_highestFirmwareVersion >= 13) {
711 // Send duplicate display frame packet
712 4 m_synciovecs[p].iov_base = m_synciovecs[p - 1].iov_base;
713 4 m_synciovecs[p].iov_len = m_synciovecs[p - 1].iov_len;
714 4 p++;
715 }
716
717
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 133 times.
270 for (int m = 0; m < p; m++) {
718 struct mmsghdr msg;
719 137 memset(&msg, 0, sizeof(msg));
720 137 msg.msg_hdr.msg_iov = &m_synciovecs[m];
721 137 msg.msg_hdr.msg_iovlen = 1;
722 137 m_syncMsgs[m] = msg;
723 }
724
725 // Wire time of the rows: payload plus preamble, FCS and inter-frame gap
726 // (24 bytes) per packet, at the link speed (1000 if unknown).
727 133 long long rowBits = 0;
728
2/2
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 133 times.
2958 for (auto& msg : m_msgs) {
729
2/2
✓ Branch 0 taken 5650 times.
✓ Branch 1 taken 2825 times.
8475 for (size_t i = 0; i < msg.msg_hdr.msg_iovlen; i++)
730 5650 rowBits += msg.msg_hdr.msg_iov[i].iov_len * 8;
731 2825 rowBits += 24 * 8;
732 }
733
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 80 times.
133 m_rowsWireUS = rowBits / (ifspeed > 0 ? ifspeed : 1000);
734
735 #ifndef PLATFORM_OSX
736 // Size the send buffer for two whole frames, so a frame normally queues
737 // in one go and FrameFits() can let one frame wait behind another before
738 // dropping. The kernel charges each packet its skb overhead on top of
739 // the payload, measured at ~1.3KB on a 32 bit Beagle kernel; 2048 bytes
740 // leaves room for 64 bit kernels. An estimate that comes up short only
741 // means the odd frame is found not to fit while it is being queued,
742 // and is dropped then instead. The kernel doubles what is asked for,
743 // so asking for one frame gets two.
744 133 m_frameCharge = 0;
745
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 133 times.
399 for (auto* msgs : { &m_msgs, &m_syncMsgs }) {
746
2/2
✓ Branch 0 taken 2962 times.
✓ Branch 1 taken 266 times.
3228 for (auto& msg : *msgs) {
747
2/2
✓ Branch 0 taken 5787 times.
✓ Branch 1 taken 2962 times.
8749 for (size_t i = 0; i < msg.msg_hdr.msg_iovlen; i++)
748 5787 m_frameCharge += msg.msg_hdr.msg_iov[i].iov_len;
749 2962 m_frameCharge += 2048;
750 }
751 }
752 133 int sendBuffer = m_frameCharge;
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (setsockopt(m_fd, SOL_SOCKET, SO_SNDBUFFORCE, &sendBuffer, sizeof(sendBuffer)) == -1)
754 ✗ setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &sendBuffer, sizeof(sendBuffer));
755 133 socklen_t len = sizeof(m_sendBufferSize);
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (getsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &m_sendBufferSize, &len) == -1)
757 ✗ m_sendBufferSize = 0;
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (m_sendBufferSize < m_frameCharge) {
759 // Without room for a whole frame, FrameFits() would drop them all
760 ✗ LogWarn(VB_CHANNELOUT, "ColorLight: Send buffer is %d bytes, a frame needs %d\n", m_sendBufferSize, m_frameCharge);
761 ✗ m_sendBufferSize = 0;
762 }
763 #endif
764
765 // Create Pixel Overlay Models if needed
766
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 if (PixelOverlayManager::INSTANCE.isAutoCreatePixelOverlayModels()) {
767
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 std::string dd = "LED Panels";
768
10/16
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 131 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 131 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 131 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
133 if (config.isMember("LEDPanelMatrixName") && !config["LEDPanelMatrixName"].asString().empty()) {
769
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 dd = config["LEDPanelMatrixName"].asString();
770 }
771
3/4
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 54 times.
133 if (config.isMember("description")) {
772
2/4
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
79 dd = config["description"].asString();
773 }
774
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 std::string desc = dd;
775 133 int count = 0;
776
2/4
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 133 times.
133 while (PixelOverlayManager::INSTANCE.getModel(desc) != nullptr) {
777 ✗ count++;
778 ✗ desc = dd + "-" + std::to_string(count);
779 }
780
4/8
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 133 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 133 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 133 times.
✗ Branch 7 not taken.
665 PixelOverlayManager::INSTANCE.addAutoOverlayModel(desc,
781 m_startChannel, m_channelCount, 3,
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 "H", m_invertedData ? "BL" : "TL",
783 133 m_height, 1);
784
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 m_autoCreatedModelName = desc;
785 133 }
786
787
2/4
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 133 times.
✗ Branch 3 not taken.
133 return ChannelOutput::Init(config);
788 }
789
790 /*
791 *
792 */
793 133 int ColorLight5a75Output::Close(void) {
794 133 LogDebug(VB_CHANNELOUT, "ColorLight5a75Output::Close()\n");
795
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 if (!m_autoCreatedModelName.empty()) {
796 133 PixelOverlayManager::INSTANCE.removeAutoOverlayModel(m_autoCreatedModelName);
797 133 m_autoCreatedModelName.clear();
798 }
799 133 ClearSendErrorWarning();
800 133 return ChannelOutput::Close();
801 }
802
803 268 void ColorLight5a75Output::GetRequiredChannelRanges(const std::function<void(int, int)>& addRange) {
804 268 addRange(m_startChannel, m_startChannel + m_channelCount - 1);
805 268 }
806
807 648 void ColorLight5a75Output::OverlayTestData(unsigned char* channelData, int cycleNum, float percentOfCycle, int testType, const Json::Value& config) {
808
2/2
✓ Branch 0 taken 729 times.
✓ Branch 1 taken 648 times.
1377 for (int output = 0; output < m_outputs; output++) {
809 729 int panelsOnOutput = m_panelMatrix->m_outputPanels[output].size();
810
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 729 times.
1404 for (int i = 0; i < panelsOnOutput; i++) {
811 675 int panel = m_panelMatrix->m_outputPanels[output][i];
812 675 int chain = m_panelMatrix->m_panels[panel].chain;
813
814
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 657 times.
675 if (m_flippedLayout)
815 18 chain = (m_longestChain - 1) - m_panelMatrix->m_panels[panel].chain - 1;
816
817 675 m_panelMatrix->m_panels[panel].drawTestPattern(channelData + m_startChannel, cycleNum, percentOfCycle, testType);
818 }
819 }
820 648 }
821
822 /*
823 * Queue packets without ever waiting for the NIC: this runs on the channel
824 * output thread, and every other output on the box waits behind it.
825 * PrepData() has already checked that the whole frame fits in the socket's
826 * send buffer, so a short send means the network fell behind after all, or
827 * the interface is gone. Returns whether every packet was queued.
828 */
829 7407 bool ColorLight5a75Output::SendMessages(std::vector<struct mmsghdr>& msgsToSend) {
830 7407 struct mmsghdr* msgs = msgsToSend.data();
831 7407 int msgCount = msgsToSend.size();
832 7407 int sent = 0;
833 7407 int oc = 0;
834
835
2/2
✓ Branch 0 taken 7407 times.
✓ Branch 1 taken 7261 times.
14668 while (sent < msgCount) {
836 7407 errno = 0;
837 7407 oc = SendMessagesHelper(&msgs[sent], msgCount - sent);
838
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 7261 times.
7407 if (oc <= 0)
839 146 break;
840 7261 sent += oc;
841 }
842
843
2/2
✓ Branch 0 taken 7261 times.
✓ Branch 1 taken 146 times.
7407 if (sent == msgCount) {
844 7261 ClearSendErrorWarning();
845 7261 return true;
846 }
847
848 146 int err = errno;
849
4/8
✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 146 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 146 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 146 times.
✗ Branch 7 not taken.
146 if (oc < 0 && err != EAGAIN && err != EWOULDBLOCK && err != ENOBUFS) {
850 146 RaiseSendErrorWarning(err);
851 } else {
852 ✗ NoteDroppedFrame();
853 }
854 146 return false;
855 }
856
857 // Raised when sends fail outright (interface down, USB NIC unplugged), and
858 // cleared by the first frame that is sent whole.
859 146 void ColorLight5a75Output::RaiseSendErrorWarning(int err) {
860
4/8
✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 146 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 146 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 146 times.
✗ Branch 7 not taken.
146 std::string warning = "ColorLight: Unable to send on interface " + m_ifName + ": " + FPPstrerror(err);
861
2/2
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 3 times.
146 if (warning == m_sendErrorWarning)
862 143 return;
863
864
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 ClearSendErrorWarning();
865
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogErr(VB_CHANNELOUT, "%s\n", warning.c_str());
866
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 WarningHolder::AddWarning(7, warning);
867
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 m_sendErrorWarning = warning;
868
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 143 times.
146 }
869
870 7397 void ColorLight5a75Output::ClearSendErrorWarning() {
871
2/2
✓ Branch 0 taken 7394 times.
✓ Branch 1 taken 3 times.
7397 if (m_sendErrorWarning.empty())
872 7394 return;
873
874 3 LogInfo(VB_CHANNELOUT, "ColorLight: Sending on interface %s again\n", m_ifName.c_str());
875
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 WarningHolder::RemoveWarning(7, m_sendErrorWarning);
876 3 m_sendErrorWarning.clear();
877 }
878
879 // A frame the network could not take is dropped whole. The warning times
880 // out on its own once frames stop being dropped; the log gets one summary
881 // per interval rather than a line per frame.
882 149 void ColorLight5a75Output::NoteDroppedFrame() {
883
3/6
✓ Branch 0 taken 149 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 149 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 149 times.
✗ Branch 5 not taken.
447 WarningHolder::AddWarningTimeout(30, 21, CL_DROPPED_FRAMES_WARNING);
884
885 149 m_droppedFrames++;
886
887 149 long long now = GetMonotonicTimeMS();
888
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 148 times.
149 if (now - m_droppedLogTime >= 10000) {
889 1 LogWarn(VB_CHANNELOUT, "ColorLight: Interface %s is not keeping up, %d frame(s) dropped since the last report\n",
890 m_ifName.c_str(), m_droppedFrames);
891 1 m_droppedFrames = 0;
892 1 m_droppedLogTime = now;
893 }
894 149 }
895
896 // Whether a whole frame fits in what is left of the socket's send buffer.
897 // If the previous frames have not drained by now, the network (or the USB
898 // NIC behind it) cannot keep up, and queueing more only adds latency.
899 4036 bool ColorLight5a75Output::FrameFits() {
900 #ifndef PLATFORM_OSX
901 4036 int queued = 0;
902
3/6
✓ Branch 0 taken 4036 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4036 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4036 times.
✗ Branch 5 not taken.
4036 if (m_sendBufferSize && ioctl(m_fd, SIOCOUTQ, &queued) == 0)
903 4036 return queued + m_frameCharge <= m_sendBufferSize;
904 #endif
905 ✗ return true;
906 }
907
908 /*
909 *
910 */
911 4036 void ColorLight5a75Output::PrepData(unsigned char* channelData) {
912 4036 m_matrix->OverlaySubMatrices(channelData);
913
914 4036 m_frameSent = false;
915
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 3887 times.
4036 if (!FrameFits()) {
916 149 NoteDroppedFrame();
917 149 return;
918 }
919
920 // Every pointer is a local __restrict copy: the stores are through a
921 // char pointer, which may alias anything, so reading the maps through
922 // members would reload them for every byte.
923 3887 const unsigned char* __restrict src = channelData + m_startChannel; // FIXME, this function gets offset 0
924 3887 unsigned char* __restrict frame = (unsigned char*)m_outputFrame.get();
925 3887 const uint8_t* __restrict gamma = m_gammaCurve;
926 3887 const uint32_t* __restrict pixel = m_remapSrc.data();
927
928
2/2
✓ Branch 0 taken 86976 times.
✓ Branch 1 taken 3887 times.
90863 for (const RemapRun& run : m_remapRuns) {
929 86976 unsigned char* __restrict dst = frame + run.dst;
930 86976 const uint32_t o0 = run.order[0];
931 86976 const uint32_t o1 = run.order[1];
932 86976 const uint32_t o2 = run.order[2];
933
934
2/2
✓ Branch 0 taken 3340288 times.
✓ Branch 1 taken 86976 times.
3427264 for (uint32_t x = 0; x < run.count; x++, dst += 3) {
935 3340288 const unsigned char* s = src + *(pixel++);
936 3340288 dst[0] = gamma[s[o0]];
937 3340288 dst[1] = gamma[s[o1]];
938 3340288 dst[2] = gamma[s[o2]];
939 }
940 }
941
942 3887 m_frameSent = SendMessages(m_msgs);
943 3887 m_rowsSentTime = GetMonotonicTimeMicros();
944 }
945
946 /*
947 * Flatten the panel maps into the order PrepData() writes the output frame.
948 * A map with one int per byte is three times the reads of one per pixel, and
949 * on a single-core Beagle it does not fit in cache, so the color order is
950 * pulled out into per-run offsets. PanelMatrix gives every pixel a single
951 * color order, so a run is normally a whole panel row; a pixel whose offsets
952 * differ from its neighbor's just starts a new run.
953 */
954 138 void ColorLight5a75Output::BuildRemap(void) {
955 138 m_remapRuns.clear();
956 138 m_remapSrc.clear();
957
958
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 138 times.
307 for (int output = 0; output < m_outputs; output++) {
959
2/2
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 169 times.
354 for (int panel : m_panelMatrix->m_outputPanels[output]) {
960 185 const LEDPanel& p = m_panelMatrix->m_panels[panel];
961 185 int chain = (m_longestChain - 1) - p.chain;
962
963
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
185 if (m_flippedLayout)
964 2 chain = p.chain;
965
966
2/2
✓ Branch 0 taken 3088 times.
✓ Branch 1 taken 185 times.
3273 for (int y = 0; y < m_panelHeight; y++) {
967 3088 uint32_t dst = (((((output * m_panelHeight) + y) * m_panelWidth * m_longestChain) + (chain * m_panelWidth)) * 3);
968 3088 const int* map = &p.pixelMap[y * m_panelWidth * 3];
969 3088 RemapRun* run = nullptr;
970
971
2/2
✓ Branch 0 taken 107008 times.
✓ Branch 1 taken 3088 times.
110096 for (int x = 0; x < m_panelWidth; x++, map += 3, dst += 3) {
972 107008 uint32_t base = std::min(map[0], std::min(map[1], map[2]));
973 107008 uint32_t order[3] = { map[0] - base, map[1] - base, map[2] - base };
974
975
3/4
✓ Branch 0 taken 103920 times.
✓ Branch 1 taken 3088 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 103920 times.
107008 if (!run || memcmp(run->order, order, sizeof(order))) {
976
1/2
✓ Branch 0 taken 3088 times.
✗ Branch 1 not taken.
3088 m_remapRuns.push_back({ dst, 0, { order[0], order[1], order[2] } });
977 3088 run = &m_remapRuns.back();
978 }
979 107008 run->count++;
980
1/2
✓ Branch 0 taken 107008 times.
✗ Branch 1 not taken.
107008 m_remapSrc.push_back(base);
981 }
982 }
983 }
984 }
985
986 138 LogDebug(VB_CHANNELOUT, "ColorLight remap: %d pixels in %d runs\n",
987 (int)m_remapSrc.size(), (int)m_remapRuns.size());
988 138 }
989
990 7407 int ColorLight5a75Output::SendMessagesHelper(struct mmsghdr* msgs, int msgCount) {
991 #ifdef PLATFORM_OSX
992 char buf[1500];
993 for (int m = 0; m < msgCount; m++) {
994 int cur = 0;
995 for (int io = 0; io < msgs[m].msg_hdr.msg_iovlen; io++) {
996 memcpy(&buf[cur], msgs[m].msg_hdr.msg_iov[io].iov_base, msgs[m].msg_hdr.msg_iov[io].iov_len);
997 cur += msgs[m].msg_hdr.msg_iov[io].iov_len;
998 }
999 int bytes_sent = write(m_fd, buf, cur);
1000 if (bytes_sent != cur) {
1001 return m;
1002 }
1003 }
1004 return msgCount;
1005 #else
1006 7407 return sendmmsg(m_fd, msgs, msgCount, MSG_DONTWAIT);
1007 #endif
1008 }
1009
1010 /*
1011 *
1012 */
1013 3875 int ColorLight5a75Output::SendData(unsigned char* channelData) {
1014 3875 LogExcess(VB_CHANNELOUT, "ColorLight5a75Output::SendData(%p)\n", channelData);
1015
1016 // Latching a frame whose rows did not all go out would show part of it
1017 // over the last one, so the receiver keeps the last whole frame instead.
1018
2/2
✓ Branch 0 taken 3520 times.
✓ Branch 1 taken 355 times.
3875 if (m_frameSent) {
1019 // The receiver latches on the sync, and one that follows the last
1020 // row too closely on the wire arrives before it has stored that row:
1021 // the row then lights only part way. Normally a whole frame has
1022 // passed since PrepData(), but while bridging the two run back to
1023 // back, so wait until the rows have left (the socket's queue is
1024 // empty) and then m_syncGapUS more. Bounded at 20ms after the rows,
1025 // and only while bridging, so the output thread does not otherwise
1026 // wait.
1027
1/2
✓ Branch 0 taken 3520 times.
✗ Branch 1 not taken.
3520 long long now = GetMonotonicTimeMicros();
1028 3520 const long long limit = m_rowsSentTime + 20000;
1029 3520 bool drainSeen = false;
1030 3520 int queued = 0;
1031 #ifndef PLATFORM_OSX
1032
5/8
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 3301 times.
✓ Branch 2 taken 219 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 219 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3520 times.
3520 while (now < limit && ioctl(m_fd, SIOCOUTQ, &queued) == 0 && queued > 0) {
1033 ✗ drainSeen = true;
1034 ✗ std::this_thread::sleep_for(std::chrono::microseconds(50));
1035 ✗ now = GetMonotonicTimeMicros();
1036 }
1037 #endif
1038 // Already empty: the rows left no later than now, nor than their
1039 // estimated wire time after they were queued.
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3520 times.
3520 long long rowsOut = drainSeen ? now : std::min(now, m_rowsSentTime + m_rowsWireUS);
1041 3520 long long wait = std::min(rowsOut + m_syncGapUS, limit) - now;
1042
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 3314 times.
3520 if (wait > 0)
1043
1/2
✓ Branch 0 taken 206 times.
✗ Branch 1 not taken.
206 std::this_thread::sleep_for(std::chrono::microseconds(wait));
1044
1/2
✓ Branch 0 taken 3520 times.
✗ Branch 1 not taken.
3520 SendMessages(m_syncMsgs);
1045 }
1046
1047 3875 return m_channelCount;
1048 }
1049
1050 /*
1051 *
1052 */
1053 133 void ColorLight5a75Output::DumpConfig(void) {
1054 133 LogDebug(VB_CHANNELOUT, "ColorLight5a75Output::DumpConfig()\n");
1055
1056 133 LogDebug(VB_CHANNELOUT, " Width : %d\n", m_width);
1057 133 LogDebug(VB_CHANNELOUT, " Height : %d\n", m_height);
1058 133 LogDebug(VB_CHANNELOUT, " Rows : %d\n", m_rows);
1059 133 LogDebug(VB_CHANNELOUT, " Row Size : %d\n", m_rowSize);
1060 133 LogDebug(VB_CHANNELOUT, " m_fd : %d\n", m_fd);
1061 133 LogDebug(VB_CHANNELOUT, " Outputs : %d\n", m_outputs);
1062 133 LogDebug(VB_CHANNELOUT, " Longest Chain : %d\n", m_longestChain);
1063 133 LogDebug(VB_CHANNELOUT, " Inverted Data : %d\n", m_invertedData);
1064 133 LogDebug(VB_CHANNELOUT, " Interface : %s\n", m_ifName.c_str());
1065
1066 133 ChannelOutput::DumpConfig();
1067 133 }
1068
1069 /*
1070 *
1071 */
1072 3039 void ColorLight5a75Output::SetHostMACs(void* ptr) {
1073 3039 struct ether_header* eh = (struct ether_header*)ptr;
1074
1075 // Set the source MAC address
1076 3039 eh->ether_shost[0] = 0x22;
1077 3039 eh->ether_shost[1] = 0x22;
1078 3039 eh->ether_shost[2] = 0x33;
1079 3039 eh->ether_shost[3] = 0x44;
1080 3039 eh->ether_shost[4] = 0x55;
1081 3039 eh->ether_shost[5] = 0x66;
1082
1083 // Set the dest MAC address
1084 3039 eh->ether_dhost[0] = 0x11;
1085 3039 eh->ether_dhost[1] = 0x22;
1086 3039 eh->ether_dhost[2] = 0x33;
1087 3039 eh->ether_dhost[3] = 0x44;
1088 3039 eh->ether_dhost[4] = 0x55;
1089 3039 eh->ether_dhost[5] = 0x66;
1090 3039 }
1091
1092 /*
1093 *
1094 */
1095 85 void ColorLight5a75Output::GetReceiverInfo() {
1096 int sockfd;
1097 int sockopt;
1098 struct ifreq ifopts;
1099 struct mmsghdr msgs[1];
1100 struct iovec iovecs[1];
1101
1102 unsigned char discoverBuffer[CL_DISC_PACKET_SIZE];
1103 85 memset(discoverBuffer, 0, CL_DISC_PACKET_SIZE);
1104
1105 // Setup Discover packet
1106
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 SetHostMACs(discoverBuffer);
1107
1108 85 discoverBuffer[CL_PACKET_TYPE_OFFSET] = CL_DISC_PACKET_TYPE;
1109
1110 85 iovecs[0].iov_base = discoverBuffer;
1111 85 iovecs[0].iov_len = CL_DISC_PACKET_SIZE;
1112
1113 85 memset(msgs, 0, sizeof(struct mmsghdr));
1114 85 msgs[0].msg_hdr.msg_iov = &iovecs[0];
1115 85 msgs[0].msg_hdr.msg_iovlen = 1;
1116
1117 #ifndef PLATFORM_OSX
1118 // Setup socket to receive response(s)
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if ((sockfd = OpenSocket(PF_PACKET, SOCK_RAW, htons(0x0805))) == -1) {
1120 ✗ LogErr(VB_CHANNELOUT, "Unable to open receive socket\n");
1121 ✗ return;
1122 }
1123
1124 85 memset(&ifopts, 0, sizeof(ifopts));
1125 85 strncpy(ifopts.ifr_name, m_ifName.c_str(), IFNAMSIZ - 1);
1126 85 ioctl(sockfd, SIOCGIFFLAGS, &ifopts);
1127 85 ifopts.ifr_flags |= IFF_PROMISC;
1128 85 ioctl(sockfd, SIOCSIFFLAGS, &ifopts);
1129
1130 // Wait up to 300ms for responses, shouldn't take anywhere near this long
1131 struct timeval tv;
1132 85 tv.tv_sec = 0;
1133 85 tv.tv_usec = 300000;
1134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) == -1) {
1135 ✗ LogErr(VB_CHANNELOUT, "Unable to set socket receive timeout\n");
1136 ✗ close(sockfd);
1137 ✗ return;
1138 }
1139
1140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof sockopt) == -1) {
1141 ✗ LogErr(VB_CHANNELOUT, "Unable to set receive socket options\n");
1142 ✗ close(sockfd);
1143 ✗ return;
1144 }
1145
1146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, m_ifName.c_str(), IFNAMSIZ - 1) == -1) {
1147 ✗ LogErr(VB_CHANNELOUT, "Unable to bind receive socket\n");
1148 ✗ close(sockfd);
1149 ✗ return;
1150 }
1151 #else
1152 sockfd = bindBPFSocket(m_ifName);
1153 #endif
1154
1155
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 LogInfo(VB_CHANNELOUT, "Searching for ColorLight receivers\n");
1156
1157 unsigned char receiveBuffer[CL_RESP_PACKET_SIZE];
1158 85 unsigned char* data = receiveBuffer + CL_PACKET_DATA_OFFSET;
1159 85 int recvBytes = 0;
1160 85 int r = 0;
1161 85 bool tryNextReceiver = true;
1162 85 bool foundReceiver = false;
1163
1164 85 memset(receiveBuffer, 0, CL_RESP_PACKET_SIZE);
1165
1166 85 int retryCount = 2;
1167
3/4
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 253 times.
✗ Branch 3 not taken.
338 while (tryNextReceiver && r < 32) {
1168 253 foundReceiver = false;
1169 253 discoverBuffer[16] = r;
1170
1/2
✓ Branch 0 taken 253 times.
✗ Branch 1 not taken.
253 sendmmsg(m_fd, msgs, 1, MSG_DONTWAIT);
1171
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 170 times.
253 if (retryCount == 0) {
1172
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 sendmmsg(m_fd, msgs, 1, MSG_DONTWAIT);
1173
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 sendmmsg(m_fd, msgs, 1, MSG_DONTWAIT);
1174 }
1175
1176 // Should only get one packet from a receiver, but consume all just in case
1177
3/4
✓ Branch 0 taken 255 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 253 times.
255 while ((recvBytes = recvfrom(sockfd, receiveBuffer, CL_RESP_PACKET_SIZE, 0, NULL, NULL)) > 0) {
1178
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (recvBytes > 1000) {
1179 // Shouldn't be receiving anything, but ignore anything other than receiver replies
1180
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if ((receiveBuffer[6] != 0x11) ||
1181
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (receiveBuffer[7] != 0x22) ||
1182
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (receiveBuffer[8] != 0x33) ||
1183
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (receiveBuffer[9] != 0x44) ||
1184
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (receiveBuffer[10] != 0x55) ||
1185
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (receiveBuffer[11] != 0x66) ||
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 (receiveBuffer[CL_PACKET_TYPE_OFFSET] != CL_RESP_PACKET_TYPE)) {
1187 ✗ continue;
1188 }
1189
1190
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (WillLog(LOG_DEBUG, VB_CHANNELOUT)) {
1191 ✗ LogDebug(VB_CHANNELOUT, "Response received from receiver id %d\n", r);
1192 ✗ HexDump("DiscoverResponse", receiveBuffer, 256, VB_CHANNELOUT);
1193 }
1194
1195 2 foundReceiver = true;
1196 ColorLightReceiver receiver;
1197
1198 2 receiver.id = data[85];
1199 2 receiver.majorFirmwareVersion = data[2];
1200 2 receiver.minorFirmwareVersion = data[3];
1201
1202 2 receiver.width = (data[21] << 8) | data[22];
1203 2 receiver.height = (data[23] << 8) | data[24];
1204 2 receiver.packets = (data[38] << 24) | (data[39] << 16) | (data[40] << 8) | data[41];
1205 2 receiver.uptime = (data[46] << 24) | (data[47] << 16) | (data[48] << 8) | data[49];
1206
1207
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogInfo(VB_CHANNELOUT, "Found Receiver #%d:\n", receiver.id);
1208
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogInfo(VB_CHANNELOUT, "- Firmware Version : v%d.%d\n",
1209 receiver.majorFirmwareVersion, receiver.minorFirmwareVersion);
1210
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogInfo(VB_CHANNELOUT, "- Cabinet Size (WxH) : %dx%d\n", receiver.width, receiver.height);
1211 // LogInfo(VB_CHANNELOUT, "- Cabinet Offset (X,Y): %d,%d\n", receiver.xOffset, receiver.yOffset);
1212
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogInfo(VB_CHANNELOUT, "- Uptime : %ums\n", receiver.uptime);
1213
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogInfo(VB_CHANNELOUT, "- Packets : %u\n", receiver.packets);
1214
1215
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (receiver.majorFirmwareVersion > m_highestFirmwareVersion) {
1216 2 m_highestFirmwareVersion = receiver.majorFirmwareVersion;
1217 }
1218
1219
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 m_receivers.resize(r + 1);
1220 2 m_receivers[r] = receiver;
1221 }
1222 }
1223
2/2
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 2 times.
253 if (!foundReceiver) {
1224
4/4
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 166 times.
✓ Branch 3 taken 83 times.
251 if (r == 0 && retryCount > 0) {
1225 166 r--;
1226 166 retryCount--;
1227 } else {
1228 85 tryNextReceiver = false;
1229 }
1230 }
1231 253 r++;
1232 }
1233
1234
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 83 times.
85 if (m_receivers.size()) {
1235
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogInfo(VB_CHANNELOUT, "Found %d ColorLight receiver(s)\n", m_receivers.size());
1236 } else {
1237
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 std::string warning = "ColorLight: No receiver cards were detected on interface " + m_ifName;
1238
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 AddOutputConfigWarning(9, warning);
1239
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 LogWarn(VB_CHANNELOUT, "WARNING: %s\n", warning.c_str());
1240 83 }
1241
1242
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 close(sockfd);
1243 }
1244