Commit Graph

57 Commits

Author SHA1 Message Date
Bastien Nocera
d87732b64c iio: Fix channel info leak in build_channel_array()
On error, the channel info would just be left unfreed.

From Coverity:
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:330: leaked_storage: Variable "current" going out of scope leaks the storage it points to.
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:312: leaked_storage: Variable "current" going out of scope leaks the storage it points to.
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:304: leaked_storage: Variable "current" going out of scope leaks the storage it points to.
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:302: leaked_storage: Variable "current" going out of scope leaks the storage it points to.
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:293: leaked_storage: Variable "current" going out of scope leaks the storage it points to.
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:289: leaked_storage: Variable "current" going out of scope leaks the storage it points to.
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:350:2: warning[unix.Malloc]: Potential leak of memory pointed to by 'current'
2022-03-25 16:45:18 +01:00
Bastien Nocera
18a043f37f iio: Fix leak in buffer_drv_data_free()
From Coverity:
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:769: noescape: Resource "buffer_data" is not freed or pointed-to in "buffer_drv_data_free".
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:770:3: warning[unix.Malloc]: Potential leak of memory pointed to by 'buffer_data'
2022-03-25 16:45:04 +01:00
Bastien Nocera
a279a80502 iio: Fix filename leak in iioutils_get_param_float()
From Coverity:
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:173: leaked_storage: Variable "filename" going out of scope leaks the storage it points to.
2022-03-25 12:41:07 +01:00
Bastien Nocera
1f4e5cff01 iio: Fix filename variable leak in iioutils_get_type()
From Coverity:
iio-sensor-proxy-3.3/src/iio-buffer-utils.c:111: overwrite_var: Overwriting "filename" in "filename = g_build_filename(device_dir, "scan_elements", builtname, NULL)" leaks the storage that "filename" points to.
2022-03-25 12:41:01 +01:00
Bastien Nocera
0ca8016560 iio: Simplify iioutils_break_up_name()
Use g_auto() to simplify the memory management.
2022-03-25 11:49:39 +01:00
Bastien Nocera
8980b8b74f iio: Allow "0.0" as an offset
0.0 is a valid offset.

https://bugzilla.redhat.com/show_bug.cgi?id=1978419#c11
2021-08-16 14:26:04 +02:00
Bastien Nocera
2f94e4981f iio: Error out when scale is parsed as 0.0 2021-08-14 22:48:54 +02:00
Bastien Nocera
a0d058eeb1 iio: Fix iioutils_get_param_float() in some locales
Reimplement iioutils_get_param_float() to parse float data like it would
in the C locale. This fixes the scale being '0' for every axis on some
systems and in some locales.

This is used to get the scale and offset parameters from buffer IIO drivers.

Fixes: 9f49caaf17

Closes: #331
2021-08-14 22:48:54 +02:00
Bastien Nocera
7ab8378cdb iio: Add g_auto() helpers for IIOSensorData 2021-01-21 16:15:46 +01:00
Bastien Nocera
e3594b879c main: Use new g_auto* to close file descriptors 2021-01-21 16:15:46 +01:00
Guido Günther
ba7a80dc93 main: Use g_autoptr(GError)
This does away with some cleanups.
2020-03-04 09:22:52 +01:00
Bastien Nocera
2d818e0992 iio: Check for fscanf() return values
Fixes the build on Ubuntu systems.

Closes: #248
2020-01-09 17:28:05 +01:00
Brett Dutro
9615ceac7c iio: Fix possible incorrect mask calculation
On some machines, iio-sensor-proxy was returning all 0's for IIO sensor
values. It turns out that the bits_used for this sensor is 32, which makes
the mask calculation:

*mask = (1 << 32) - 1;

If the compiler interprets the 1 literals as 32-bit ints, it generates
undefined behavior depending on compiler version and optimization level.
On my system, it optimizes out the shift, so the mask value becomes

*mask = (1) - 1;

With a mask value of 0, iio-sensor-proxy will always return 0 for every axis.

Avoid incorrect 0 values caused by compiler optimization.

Closes: #270
2019-05-20 10:47:19 +02:00
Bastien Nocera
1ed26fd8e4 iio: And turn channel value processing into a macro
So that the code is shared between all 4 value sizes.
2019-05-14 12:47:26 +02:00
Bastien Nocera
b468ed6384 iio: Implement support for 8-, 16- and 64- bits channels 2019-05-14 12:47:26 +02:00
Bastien Nocera
6a67ead786 iio: Simplify getting 32-bit channel values
Use code from the IIO example tools at tools/iio/iio_generic_buffer.c in
the Linux kernel tree to parse the IIO data.
2019-05-14 12:47:26 +02:00
Bastien Nocera
2fb71895dd iio: Add stub for 1 byte channels 2019-05-14 12:47:26 +02:00
Bastien Nocera
e941f9ff5d iio: Break from the loop when we've found the right channel
No need to keep looping once we've found it.
2019-05-14 12:47:26 +02:00
Bastien Nocera
4f850f0be5 iio: Simplify setting the scale and presence return values
The scale should always be set to 1.0 by default, and only ever be
overridden if we could successfully read it from sysfs, and ch_present
is obviously false until we've found the channel in question.
2019-05-14 12:47:26 +02:00
Bastien Nocera
ec0d992c18 iio: Check whether getting float values fails
If we cannot parse the contents of sysfs attribute that's supposed to
contain floats, throw an error.
2019-05-14 12:47:26 +02:00
Bastien Nocera
f521ed1d87 iio: Use info temporary variable
Using a temporary struct iio_channel_info makes that piece of code much
more readable.
2019-05-14 12:47:26 +02:00
Bastien Nocera
55cf113780 iio: Better struct documentation for iio_channel_info 2019-05-14 12:47:26 +02:00
Bastien Nocera
51524ec8b4 iio: Fix possible crash on device removal
Check whether we managed to open the index file instead of crashing.

 #0  0x00007f31843660c7 in __vfscanf_internal () at /lib64/libc.so.6
 #1  0x00007f31843657ed in __isoc99_fscanf () at /lib64/libc.so.6
 #2  0x0000560bfd6b942b in build_channel_array (counter=0x560bfdea01c8, device_dir=0x560bfde90810 "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:273F:1008.0044/HID-SENSOR-200041.2.auto/iio:device0")
     at iio-buffer-utils.c:320
 #3  0x0000560bfd6b942b in build_channels (data=0x560bfdea01b0) at iio-buffer-utils.c:739

(gdb) frame 2
 #2  0x0000560bfd6b942b in build_channel_array (counter=0x560bfdea01c8, device_dir=0x560bfde90810 "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:273F:1008.0044/HID-SENSOR-200041.2.auto/iio:device0")
    at iio-buffer-utils.c:320
320				fscanf (sysfsfp, "%u", &current->index);
(gdb) p sysfsfp
$1 = (FILE *) 0x0
2019-05-14 12:47:26 +02:00
Bastien Nocera
1585412ef7 iio: Fix incorrect error debug in iioutils_get_param_float()
The "ret" variable was not set, so we'd end up with "failed: success".
2019-05-14 12:47:26 +02:00
Bastien Nocera
746271f238 iio: Simplify signchar parsing 2019-05-14 12:47:26 +02:00
Bastien Nocera
f90db92ca1 iio: Add documentation references for iioutils_get_type()
Link to where the structure and format are defined.
2019-05-14 12:47:26 +02:00
Bastien Nocera
43fb8be70b iio: More debug in process_scan_1() 2019-03-16 12:26:01 +01:00
Bastien Nocera
afe1f6c7f3 iio: Fix indentation 2018-10-09 09:55:08 +02:00
Bastien Nocera
7424113cfc iio: Also set a scale for unsigned channels
Avoids the default value being used when a scale is present.

Closes: #238
2018-10-09 09:55:08 +02:00
Bastien Nocera
d7725e8b3a iio: Always set the scale
When calling process_scan_1(), make sure to always set the scale to 1.0
so it always has a default value.
2018-10-09 09:55:08 +02:00
Bastien Nocera
af44746c96 iio-buffer-utils: Add more debug to some utility functions 2018-05-14 07:01:37 +02:00
Hans de Goede
5828b05b0c iio-buffer-utils: Make sure sampling_frequency is 10Hz or more
Add a new iio_fixup_sampling_frequency() helper to iio-buffer-utils.c and
call this for all iio devices. iio_fixup_sampling_frequency() makes sure that
devices with *sampling_frequency attributes are sampling at 10Hz or more.

This fixes 2 problems:

1) Some buffered devices default their sampling_frequency to 0Hz and then
never produce any readings. This fixes e.g. the sensors on Asus T100HA
2-in-1s not working.

2) Some polled devices default to 1Hz and wait for a fresh sample before
returning from sysfs *_raw reads, blocking all of iio-sensor-proxy for
multiple seconds, this fixes e.g. rotation being detected up to 10 seconds
later then the actual rotating of the device on a Lenovo Miix 320.
2018-04-17 13:00:47 +02:00
Bastien Nocera
4bf3b38a59 iio: Fix 32-bit unaligned helper
0640bf0 added helpers to fetch unaligned memory, but didn't take
the offset to the data location into account, reading from
the header instead.

Closes: #195
2017-10-24 13:46:29 +02:00
Bastien Nocera
393c1139dd iio: Use unaligned access helpers to access buffer values
So as to avoid unaligned access warnings on ARM, or unsigned values be
interpreted as signed.

Closes: #191
2017-10-11 18:24:03 +02:00
Bastien Nocera
0640bf01a6 iio: Add helper to get unaligned 32-bit integers 2017-10-11 18:23:23 +02:00
Bastien Nocera
70da85b8c3 Revert "iio-buffer-utils: Fix cast-align warnings on ARM"
This was causing unsigned buffer values to be read as signed.

This reverts commit ebc348abfe.
2017-10-11 18:22:44 +02:00
Bastien Nocera
8e994daa3b iio-buffer-utils: Print error when reading from device fails
Rather than just "Failed to read float from...", also print the
errno so can start taking guesses at what the problem is.
2017-09-06 10:21:52 +02:00
Bastien Nocera
ebc348abfe iio-buffer-utils: Fix cast-align warnings on ARM
iio-buffer-utils.c: In function 'process_scan_1':
iio-buffer-utils.c:495:20: error: cast increases required alignment of target type [-Werror=cast-align]
     guint32 val = *(guint32 *) (data + buffer_data->channels[k]->location);
                    ^
iio-buffer-utils.c:501:19: error: cast increases required alignment of target type [-Werror=cast-align]
     gint32 val = *(gint32 *) (data + buffer_data->channels[k]->location);
                   ^

Looks like there's no need to cast.

Closes: #113
2017-08-17 01:10:10 +02:00
Bastien Nocera
e69580583b iio-buffer-utils: Indentation changes 2017-08-17 01:09:55 +02:00
Bastien Nocera
d0ced55668 iio-buffer-utils: Don't throw warnings on ENOENT
As we don't return when the file doesn't exist, don't print a warning
either, as this would pollute logs and throw people off when debugging
real problems.

Closes: #180
2017-08-16 16:52:23 +02:00
Bastien Nocera
d71d4342e4 iio-buffer-utils: Better debug in enable_sensors() 2016-11-22 14:14:18 +01:00
Srinivas Pandruvada
12c137bcf1 iio-buffer-utils: Don't fail on non-existing attributes
build_channel_array() goes through each directory member in
scan_elements folder and tries to get scale and offset of matching
elements. But there will be no scale and offset for some elements
like timestamp.
For example:
in_accel_x_en     in_accel_y_en     in_accel_z_en     in_timestamp_en
in_accel_x_index  in_accel_y_index  in_accel_z_index  in_timestamp_index
in_accel_x_type   in_accel_y_type   in_accel_z_type   in_timestamp_type

When scale and offset not present, iioutils_get_param_float() will fail,
this causes return from the build_channel_array().

It shouldn't return when the error is caused by the attribute not being
present as, in this case, in_time_stamp_scale.

This was fixed in the kernel's iio_utils.c in commit 7868dfd.
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=7868dfd216074fc5f902e7befacda2a0ec76e403

Closes: #114
2016-11-18 11:46:57 +01:00
Srinivas Pandruvada
5d3a83d77d iio-buffer-utils: Really fix sorting of channels
As per the documentation of g_ptr_array_sort
"Note that the comparison function for g_ptr_array_sort() doesn't take the
pointers from the array as arguments, it takes pointers to the pointers in
the array"
So the arguments to the function
	compare_channel_index (gconstpointer a, gconstpointer b)
needs to be dereferenced to get pointer to iio_channel_info.

The error was included in the fixup for commit 984803b by
the maintainer.

Closes #99
2016-09-17 00:49:59 +02:00
Bastien Nocera
c7d00d278d buffer-utils: Fix possible crash handling compasses
The generic_name is supposed to be an allocated string. As we were
passing it a static string, it might have crashed later on.
2016-09-06 14:47:24 +02:00
Bastien Nocera
26b414681b buffer-utils: Fix "const char" build warning 2016-09-06 14:47:20 +02:00
Srinivas Pandruvada
984803bc1b buffer-utils: Fix orientation axis switch using 4.8 kernel
Using 4.8-rc kernel, orientation got flipped. The right-up became normal
and normal became right-up. The reason for this is that orientation code
of iio-sensor-proxy observes x and y swapped (actually they are not in
the data provided by IIO).

Although it is triggered by some kernel change, the reliance on
g_dir_read_name() to get the correct order of channels is not correct.
We should look at the in_xxx_x_index field and calculate correct byte
offset in the iio buffer data.

For example with 4.8 kernel: from log with added index location printed

** (process:4945): DEBUG: Built channel array for in_accel_y: index: 1, is signed: 1, bytes: 4, bits_used: 32, shift: 0, mask: 0x0, be: 0
** (process:4945): DEBUG: Built channel array for in_accel_x: index: 0, is signed: 1, bytes: 4, bits_used: 32, shift: 0, mask: 0x0, be: 0
** (process:4945): DEBUG: Built channel array for in_accel_z: index: 2, is signed: 1, bytes: 4, bits_used: 32, shift: 0, mask: 0x0, be: 0

(Here in_accel_y appeared before in_accel_x in g_dir_read_name(), because
of that channel array ordering is wrong hence location was
calculated wrong as below)
...
...
** (process:4945): DEBUG: process_scan_1: channel_index: 1, chan_name: in_accel_x, channel_data_index: 0 location: 4
** (process:4945): DEBUG: process_scan_1: channel_index: 0, chan_name: in_accel_y, channel_data_index: 1 location: 0
** (process:4945): DEBUG: process_scan_1: channel_index: 2, chan_name: in_accel_z, channel_data_index: 2 location: 8
** (process:4945): DEBUG: Read from IIO: -880449, 32851, -457812

To fix this we need to calculate the byte offset location using
channel_index not in the order they are returned by g_dir_read_name().

The easiest fix is to sort the array of channels in
build_channel_array() based on index. Also added some debug to print
index and location.

After the fix, the above log will change to:
** (process:4674): DEBUG: Built channel array for in_accel_x: index: 0, is signed: 1, bytes: 4, bits_used: 32, shift: 0, mask: 0x0, be: 0
** (process:4674): DEBUG: Built channel array for in_accel_y: index: 1, is signed: 1, bytes: 4, bits_used: 32, shift: 0, mask: 0x0, be: 0
** (process:4674): DEBUG: Built channel array for in_accel_z: index: 2, is signed: 1, bytes: 4, bits_used: 32, shift: 0, mask: 0x0, be: 0

(Sorted above, so location of byte offset is correct)

** (process:4674): DEBUG: process_scan_1: channel_index: 0, chan_name: in_accel_x, channel_data_index: 0 location: 0
** (process:4674): DEBUG: process_scan_1: channel_index: 1, chan_name: in_accel_y, channel_data_index: 1 location: 4
** (process:4674): DEBUG: process_scan_1: channel_index: 2, chan_name: in_accel_z, channel_data_index: 2 location: 8
** (process:4674): DEBUG: Read from IIO: 34804, -878496, -448046

Closes: #99
2016-09-06 14:21:46 +02:00
Bastien Nocera
92fa6ea9ec buffer-utils: Don't warn when sensor was already enabled
Slight regression in commit 3816a34, where an already enabled sensor
would still make enable_sensors() fail.
2016-06-14 18:47:10 +02:00
Bastien Nocera
207b5bbd87 buffer-utils: Warn if a ring buffer could not be setup 2016-06-14 17:07:03 +02:00
Bastien Nocera
3816a34120 buffer-utils: Error out if no sensor is enabled in enable_sensors()
Error out if none of the sensors could be enabled, and throw a warning.
2016-06-14 17:07:03 +02:00
Bastien Nocera
e5ec57dca1 buffer-utils: Add more debug to the scan_elements parsing 2015-07-25 20:40:03 +02:00