GCC Code Coverage Report


Directory: ./
File: channeloutput/KiNet.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 89 98 90.8%
Functions: 6 7 85.7%
Branches: 54 98 55.1%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 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 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
19 #include <sys/socket.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <signal.h>
26
27 #include "../Warnings.h"
28 #include "ChannelOutputSetup.h"
29 #include "../log.h"
30
31 #include "KiNet.h"
32
33 #define KINET_V1_PACKET_HEADERLEN 21
34 #define KINET_V2_PACKET_HEADERLEN 24
35
36 #define KINET_V1_TYPE 6
37 #define KINET_V2_TYPE 7
38
39 static const uint8_t V1_HEADER[KINET_V1_PACKET_HEADERLEN] = {
40 0x04, 0x01, 0xdc, 0x4a, // magic[4] 0401dc4a
41 0x01, 0x00, // ver[2] 0100
42 0x01, 0x01, // type[2]
43 0x00, 0x00, 0x00, 0x00, // seq[4]
44 0x00, // port
45 0x00, // padding
46 0x00, 0x00, // flags
47 0xFF, 0xFF, 0xFF, 0xFF, // time
48 0x00 // uni
49 };
50 static const uint8_t V2_HEADER[KINET_V2_PACKET_HEADERLEN] = {
51 0x04, 0x01, 0xdc, 0x4a, // magic[4] 0401dc4a
52 0x01, 0x00, // ver[2] 0100
53 0x08, 0x01, // type[2]
54 0x00, 0x00, 0x00, 0x00, // seq[4]
55 0xFF, 0xFF, 0xFF, 0xFF, // time
56 0x00, // port
57 0x00, // sequenceNum
58 0x00, 0x00, // flags?
59 0x00, 0x02,
60 0x00, 0x00
61 };
62
63 static const std::string KINETTYPE = "KiNet";
64
65 ✗ const std::string& KiNetOutputData::GetOutputTypeString() const {
66 ✗ return KINETTYPE;
67 }
68
69 4 KiNetOutputData::KiNetOutputData(const Json::Value& config) :
70 4 UDPOutputData(config) {
71 4 memset((char*)&kinetAddress, 0, sizeof(sockaddr_in));
72 4 kinetAddress.sin_family = AF_INET;
73 4 kinetAddress.sin_port = htons(KINET_PORT);
74
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (active) {
75
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kinetAddress.sin_addr.s_addr = toInetAddr(ipAddress, valid);
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!valid) {
77 ✗ AddOutputConfigWarning(14, "Could not resolve host name " + ipAddress + " - disabling output");
78 ✗ active = false;
79 }
80 }
81
82
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 port = config["id"].asInt();
83
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (config.isMember("universeCount")) {
84
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 portCount = config["universeCount"].asInt();
85 }
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (portCount < 1) {
87 ✗ portCount = 1;
88 }
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (portCount > 255) {
90 ✗ portCount = 255;
91 }
92 // A KiNet port is one byte in both the v1 and v2 header. Writing
93 // port + x into it without checking wraps id 250 + 10 universes around
94 // to ports 0-3, which collide with unrelated outputs. Keep the ports
95 // that fit instead and say what was dropped; a port that cannot be
96 // represented at all disables the output.
97
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
4 if (port < 0 || port > 255) {
98
4/8
✓ 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.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
2 AddOutputConfigWarning(26, "KiNet output " + description + ": id " + std::to_string(port) +
99
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 " is outside the one-byte KiNet port range 0-255 - output disabled");
100 1 active = false;
101
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 } else if (port + portCount > 256) {
102 1 int available = 256 - port;
103
5/10
✓ 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.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 AddOutputConfigWarning(26, "KiNet output " + description + ": id " + std::to_string(port) + " with " +
104
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
4 std::to_string(portCount) + " universes exceeds KiNet port 255 - reducing universeCount to " +
105
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
3 std::to_string(available));
106 1 portCount = available;
107 }
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (channelCount > 512) {
109 ✗ channelCount = 512;
110 }
111
112
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 sentSequence.assign(portCount, 0);
113
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 pendingSequence.assign(portCount, 0);
114 4 kinetIovecs = (struct iovec*)calloc(portCount * 2, sizeof(struct iovec));
115 4 kinetBuffers = (unsigned char**)calloc(portCount, sizeof(unsigned char*));
116
117 4 int chan = startChannel - 1;
118
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
18 for (int x = 0; x < portCount; x++) {
119
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 if (type == KINET_V1_TYPE) {
120 3 kinetBuffers[x] = (unsigned char*)malloc(KINET_V1_PACKET_HEADERLEN);
121 3 memcpy(kinetBuffers[x], V1_HEADER, KINET_V1_PACKET_HEADERLEN);
122 3 kinetBuffers[x][12] = port + x;
123 } else {
124 11 kinetBuffers[x] = (unsigned char*)malloc(KINET_V2_PACKET_HEADERLEN);
125 11 memcpy(kinetBuffers[x], V2_HEADER, KINET_V2_PACKET_HEADERLEN);
126 11 kinetBuffers[x][16] = port + x;
127 }
128
129 // use scatter/gather for the packet. One IOV will contain
130 // the header, the second will point into the raw channel data
131 // and will be set at output time. This avoids any memcpy.
132 14 kinetIovecs[x * 2].iov_base = kinetBuffers[x];
133
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 kinetIovecs[x * 2].iov_len = type == KINET_V1_TYPE ? KINET_V1_PACKET_HEADERLEN : KINET_V2_PACKET_HEADERLEN;
134 14 kinetIovecs[x * 2 + 1].iov_base = nullptr;
135 14 kinetIovecs[x * 2 + 1].iov_len = channelCount;
136
137 14 chan += channelCount;
138 }
139 4 }
140 16 KiNetOutputData::~KiNetOutputData() {
141
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
36 for (int x = 0; x < portCount; x++) {
142 28 free(kinetBuffers[x]);
143 }
144 8 free(kinetBuffers);
145 8 free(kinetIovecs);
146 16 }
147
148 6 void KiNetOutputData::GetRequiredChannelRange(int& min, int& max) {
149 6 min = startChannel - 1;
150 6 max = startChannel + (channelCount * portCount) - 1;
151 6 }
152
153 178 void KiNetOutputData::PrepareData(unsigned char* channelData, UDPOutputMessages& msgs) {
154
3/6
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 178 times.
✗ Branch 5 not taken.
178 if (valid && active) {
155 178 int start = 0;
156 struct mmsghdr msg;
157 178 memset(&msg, 0, sizeof(msg));
158
159 178 msg.msg_hdr.msg_name = &kinetAddress;
160 178 msg.msg_hdr.msg_namelen = sizeof(sockaddr_in);
161 178 bool anySkipped = false;
162 178 bool allSkipped = true;
163
2/2
✓ Branch 0 taken 657 times.
✓ Branch 1 taken 178 times.
835 for (int p = 0; p < portCount; p++) {
164
1/2
✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
657 bool nto = NeedToOutputFrame(channelData, startChannel - 1, start, kinetIovecs[p * 2 + 1].iov_len);
165
1/2
✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
657 if (nto) {
166 657 msg.msg_hdr.msg_iov = &kinetIovecs[p * 2];
167 657 msg.msg_hdr.msg_iovlen = 2;
168 657 msg.msg_len = kinetIovecs[p * 2 + 1].iov_len + kinetIovecs[p * 2].iov_len;
169
170
2/4
✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 657 times.
✗ Branch 3 not taken.
657 msgs[kinetAddress.sin_addr.s_addr].push_back(msg);
171
172
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 204 times.
657 if (type == KINET_V2_TYPE) {
173 // Numbered from the last packet sent, not the last one
174 // prepared: a frame prepared twice before it went out (as
175 // one is when playback starts) skipped a number on the wire.
176 453 uint32_t seq = sentSequence[p] + 1;
177 453 pendingSequence[p] = seq;
178 453 kinetBuffers[p][8] = seq & 0xFF;
179 453 kinetBuffers[p][9] = (seq >> 8) & 0xFF;
180 453 kinetBuffers[p][10] = (seq >> 16) & 0xFF;
181 453 kinetBuffers[p][11] = (seq >> 24) & 0xFF;
182 }
183 // set the pointer to the channelData for the universe
184 657 kinetIovecs[p * 2 + 1].iov_base = (void*)(&channelData[startChannel - 1 + start]);
185 657 allSkipped = false;
186 } else {
187 ✗ anySkipped = true;
188 }
189 657 start += kinetIovecs[p * 2 + 1].iov_len;
190 }
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 178 times.
178 if (anySkipped) {
192 ✗ skippedFrames++;
193 } else {
194 178 skippedFrames = 0;
195 }
196
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 if (!allSkipped) {
197
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 SaveFrame(&channelData[startChannel - 1], start);
198 }
199 }
200 178 }
201 168 void KiNetOutputData::FrameSent() {
202 168 sentSequence = pendingSequence;
203 168 }
204
205 4 void KiNetOutputData::DumpConfig() {
206 4 LogDebug(VB_CHANNELOUT, "KiNet: %s %d:%d:%d:%d %s\n",
207 description.c_str(),
208 active,
209 startChannel,
210 channelCount,
211 type,
212 ipAddress.c_str());
213 4 }
214