GCC Code Coverage Report


Directory: ./
File: sensors/IIOSensorSource.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 161 180 89.4%
Functions: 9 10 90.0%
Branches: 171 294 58.2%

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 LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #include <cmath>
18 #include <fcntl.h>
19 #include <unistd.h>
20
21 #include "../Warnings.h"
22 #include "../common.h"
23 #include "../log.h"
24
25 #include "IIOSensorSource.h"
26
27 // A driver whose buffer is filled by an IIO trigger (ad799x, most I2C/SPI
28 // ADCs) exposes trigger/current_trigger. Until a trigger is assigned there,
29 // the kernel refuses to enable the buffer. Drivers that fill the buffer from
30 // their own hardware timer (ti_am335x_adc) have no trigger directory.
31 2 static bool BufferNeedsTrigger(int iioDevNumber) {
32
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 std::string path = "/sys/bus/iio/devices/iio:device" + std::to_string(iioDevNumber) + "/trigger/current_trigger";
33
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 if (!FileExists(path)) {
34 1 return false;
35 }
36
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::string trig = GetFileContents(path);
37
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 TrimWhiteSpace(trig);
38 1 return trig.empty();
39 2 }
40
41 22 IIOSensorSource::IIOSensorSource(Json::Value& config) :
42 22 SensorSource(config) {
43
3/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 2 times.
22 if (config.isMember("devId")) {
44
3/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 1 times.
20 iioDevNumber = config["devId"].asInt();
45 } else {
46
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 int i2cBus = config["i2cBus"].asInt();
47
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 int i2cDev = config["i2cDev"].asInt();
48 uint8_t buf[8];
49 2 snprintf((char*)buf, 8, "%04x", i2cDev);
50
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::string d = (char*)buf;
51
6/12
✓ 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.
✓ 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 not taken.
2 std::string basePath = "/sys/class/i2c-dev/i2c-" + std::to_string(i2cBus) + "/device/" + std::to_string(i2cBus) + "-" + d + "/iio:device";
52
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 for (int x = 0; x < 16; x++) {
53
4/6
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 19 times.
20 if (FileExists(basePath + std::to_string(x))) {
54 1 iioDevNumber = x;
55 1 break;
56 }
57 }
58 2 }
59
3/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 3 times.
21 if (config.isMember("useBuffers")) {
60
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 usingBuffers = config["useBuffers"].asBool();
61 } else {
62
18/38
✓ 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.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✓ Branch 17 taken 1 times.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 1 times.
✓ Branch 20 taken 2 times.
✓ Branch 21 taken 1 times.
✓ Branch 22 taken 3 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 3 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 3 times.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
3 usingBuffers = FileExists("/sys/bus/iio/devices/iio:device" + std::to_string(iioDevNumber) + "/buffer/enable") && FileExists("/dev/iio:device" + std::to_string(iioDevNumber)) && !BufferNeedsTrigger(iioDevNumber);
63 }
64
65
3/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 3 times.
21 if (config.isMember("vScale")) {
66
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 vScale = config["vScale"].asFloat();
67 } else {
68 #ifdef PLATFORM_BB64
69
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 vs = GetFileContents("/sys/bus/iio/devices/iio:device" + std::to_string(iioDevNumber) + "/in_voltage_scale");
70 2 vScale = std::atof(vs.c_str());
71 // original reference is 1.8V which for 12bit would be 0.439453125.
72 // If the cape doesn't specify a reference scale, we'll adjust and assume
73 // the params are based on 1.8 ref, but we're reading to 3.3V
74 2 vScale = vScale / 0.439453125;
75 #else
76 1 vScale = 1.0f;
77 #endif
78 2 }
79
3/6
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
21 readIterations = config.get("readIterations", 3).asInt();
80 // update() averages over readIterations reads: 0 divided by zero
81
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (readIterations < 1) {
82 1 readIterations = 1;
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 } else if (readIterations > 100) {
84 ✗ readIterations = 100;
85 }
86
87 // usingBuffers = false;
88
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 std::string base = "/sys/bus/iio/devices/iio:device" + std::to_string(iioDevNumber) + "/in_voltage";
89 21 int max = -1;
90
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 21 times.
357 for (int x = 0; x < 16; x++) {
91
5/8
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 336 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 336 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✓ Branch 7 taken 298 times.
336 if (FileExists(base + std::to_string(x) + "_raw")) {
92 38 max = x;
93 }
94 }
95
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
21 if (max >= 0) {
96 19 ++max;
97
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 channelMapping.resize(max);
98
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 values.resize(max);
99
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 19 times.
57 for (int x = 0; x < max; x++) {
100 38 channelMapping[x] = -1;
101 38 values[x] = 0;
102 }
103 } else {
104
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
6 WarningHolder::AddWarning(53, "Could not enable IIOSensorSource");
105 }
106 24 }
107 8 IIOSensorSource::~IIOSensorSource() {
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 if (usingBuffers) {
109 char buf[256];
110 ✗ snprintf(buf, 256, "/sys/bus/iio/devices/iio:device%d/buffer/enable", iioDevNumber);
111 ✗ int f = open(buf, O_WRONLY);
112 ✗ write(f, "0\n", 2);
113 ✗ close(f);
114
115 ✗ close(iioDevFile);
116 } else {
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 for (int x = 0; x < channelMapping.size(); x++) {
118 ✗ if (channelMapping[x] != -1) {
119 ✗ close(channelMapping[x]);
120 }
121 }
122 }
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 if (readBuffer) {
124 ✗ delete[] readBuffer;
125 }
126 8 }
127 21 bool IIOSensorSource::isOK() const {
128
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 return FileExists("/dev/iio:device" + std::to_string(iioDevNumber));
129 }
130
131 23 void IIOSensorSource::Init(std::map<int, std::function<bool(int)>>& callbacks) {
132
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 20 times.
23 if (usingBuffers) {
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (iioDevFile >= 0) {
134 1 return;
135 }
136 char buf[256];
137 3 snprintf(buf, 256, "/sys/bus/iio/devices/iio:device%d/buffer/length", iioDevNumber);
138
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 int f = open(buf, O_WRONLY);
139
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 write(f, "8\n", 2);
140
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 close(f);
141
142 3 snprintf(buf, 256, "/sys/bus/iio/devices/iio:device%d/buffer/enable", iioDevNumber);
143
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 f = open(buf, O_WRONLY);
144
4/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
3 bool bufferOn = (f >= 0) && (write(f, "1\n", 2) == 2);
145
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (f >= 0) {
146
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 close(f);
147 }
148
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!bufferOn) {
149 // The kernel refused the buffer (for example a triggered buffer
150 // with no trigger, or "useBuffers" forced on). Read the channels
151 // one at a time through sysfs instead, starting with the ones
152 // already enabled for the buffer.
153
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_GENERAL, "IIOSensorSource: could not enable the buffer of iio:device%d, reading channels directly\n", iioDevNumber);
154 1 usingBuffers = false;
155
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int x = 0; x < channelMapping.size(); x++) {
156 1 snprintf(buf, 256, "/sys/bus/iio/devices/iio:device%d/scan_elements/in_voltage%d_en", iioDevNumber, x);
157
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 std::string en = GetFileContents(buf);
158
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.
1 if (!en.empty() && en[0] == '1') {
159
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 f = open(buf, O_WRONLY);
160
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (f >= 0) {
161
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 write(f, "0\n", 2);
162
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 close(f);
163 }
164
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 enable(x);
165 }
166 1 }
167 1 return;
168 }
169
170 2 snprintf(buf, 256, "/dev/iio:device%d", iioDevNumber);
171
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 iioDevFile = open(buf, O_RDONLY | O_NONBLOCK);
172
173 2 int curIdx = 0;
174
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int x = 0; x < channelMapping.size(); x++) {
175 2 snprintf(buf, 256, "/sys/bus/iio/devices/iio:device%d/scan_elements/in_voltage%d_en", iioDevNumber, x);
176
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 int f = open(buf, O_RDONLY);
177
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 read(f, buf, 240);
178
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (buf[0] == '1') {
179 2 channelMapping[curIdx] = x;
180 2 curIdx++;
181 }
182
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 close(f);
183 }
184
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 channelMapping.resize(curIdx);
185
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 callbacks[iioDevFile] = [this](int) {
186 ✗ update(false, true);
187 ✗ return false;
188 2 };
189 2 readBufferSize = channelMapping.size() * 64 * 2;
190
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 readBuffer = new uint16_t[readBufferSize];
191 }
192 }
193 30 void IIOSensorSource::update(bool forceInstant) {
194 30 update(forceInstant, false);
195 30 }
196
197 30 void IIOSensorSource::update(bool forceInstant, bool fromSelect) {
198
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::unique_lock<std::mutex> lock(updateMutex);
199
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
30 if (usingBuffers) {
200
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 int i = read(iioDevFile, readBuffer, readBufferSize * sizeof(uint16_t));
201
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (i > 1) {
202 2 int countRead = i / sizeof(uint16_t);
203
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 std::vector<uint32_t> sums(channelMapping.size());
204
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 std::vector<uint16_t> mins(channelMapping.size());
205
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<uint16_t> maxes(channelMapping.size());
206 2 int samples = 0;
207
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int x = 0; x < channelMapping.size(); x++) {
208 2 maxes[x] = 0;
209 2 mins[x] = 0xFFFF;
210 2 sums[x] = 0;
211 }
212
213
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 for (int y = 0; y < countRead; y += channelMapping.size()) {
214 12 samples++;
215
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 for (int x = 0; x < channelMapping.size(); x++) {
216 12 uint16_t v = readBuffer[y + x];
217 12 sums[x] += v;
218 12 maxes[x] = std::max(maxes[x], v);
219 12 mins[x] = std::min(mins[x], v);
220 }
221 }
222
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (samples > 3) {
223
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int x = 0; x < channelMapping.size(); x++) {
224 2 sums[x] -= mins[x];
225 2 sums[x] -= maxes[x];
226 2 float f = sums[x];
227 2 f /= (float)(samples - 2);
228 2 f *= vScale;
229
230 2 int idx = channelMapping[x];
231 2 values[idx] = f;
232 }
233 } else {
234 ✗ for (int x = 0; x < channelMapping.size(); x++) {
235 ✗ float f = sums[x];
236 ✗ f /= (float)(samples);
237 ✗ f *= vScale;
238 ✗ int idx = channelMapping[x];
239 ✗ values[idx] = f;
240 }
241 }
242 2 }
243 } else {
244 char buf[24];
245
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 28 times.
86 for (int x = 0; x < channelMapping.size(); x++) {
246
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 14 times.
58 if (channelMapping[x] >= 0) {
247 44 float s = 0;
248
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 44 times.
170 for (int m = 0; m < readIterations; m++) {
249 126 lseek(channelMapping[x], 0, SEEK_SET);
250
1/2
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
126 int r = read(channelMapping[x], buf, 23);
251 // a failed read (-1) wrote buf[-1]
252 126 buf[r > 0 ? r : 0] = 0;
253 126 s += std::atoi(buf);
254 }
255 44 s /= ((float)readIterations);
256 44 s *= vScale;
257 44 values[x] = std::round(s);
258 }
259 }
260 }
261 30 }
262 43 void IIOSensorSource::enable(int id) {
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (id < 0) {
264 ✗ return;
265 }
266
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 40 times.
43 if (usingBuffers) {
267 char buf[256];
268 3 snprintf(buf, 256, "/sys/bus/iio/devices/iio:device%d/scan_elements/in_voltage%d_en", iioDevNumber, id);
269
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (FileExists(buf)) {
270
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 int f = open(buf, O_WRONLY);
271
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 write(f, "1\n", 2);
272
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 close(f);
273 }
274 } else {
275 // enable() is called once per consumer, not once per channel: every
276 // port's current monitor enables its channel again on each channel
277 // output reload, and a MuxSensorSource enables the same underlying
278 // channel for every port it multiplexes. Opening unconditionally
279 // overwrote the previous descriptor without closing it, leaking one
280 // fd per call until fppd hit EMFILE.
281
6/8
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 18 times.
✓ Branch 7 taken 22 times.
40 if (id < 0 || id >= (int)channelMapping.size() || channelMapping[id] >= 0) {
282 18 return;
283 }
284 char buf[256];
285 22 snprintf(buf, 256, "/sys/bus/iio/devices/iio:device%d/in_voltage%d_raw", iioDevNumber, id);
286
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 channelMapping[id] = open(buf, O_RDONLY | O_CLOEXEC);
287 }
288 }
289
290 52 int32_t IIOSensorSource::getValue(int id) {
291
3/6
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 52 times.
52 if (id < 0 || (size_t)id >= values.size()) {
292 ✗ return 0;
293 }
294 52 return values[(size_t)id];
295 };
296