accel: Add support for the new "accel-*" IIO labels

See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=9a6df4b1ab0e467f23ccdcbb82700cfb3eaf44a3
This commit is contained in:
Bastien Nocera
2021-04-16 13:44:50 +02:00
parent a1eb647c88
commit 27a325b2f5
3 changed files with 47 additions and 0 deletions

View File

@@ -24,6 +24,11 @@ setup_accel_location (GUdevDevice *device)
g_warning ("Failed to parse ACCEL_LOCATION ('%s') from udev", g_warning ("Failed to parse ACCEL_LOCATION ('%s') from udev",
location); location);
} }
location = g_udev_device_get_sysfs_attr (device, "label");
if (location) {
if (parse_accel_label (location, &ret))
return ret;
}
location = g_udev_device_get_sysfs_attr (device, "location"); location = g_udev_device_get_sysfs_attr (device, "location");
if (location) { if (location) {
if (parse_accel_location (location, &ret)) if (parse_accel_location (location, &ret))
@@ -38,6 +43,23 @@ setup_accel_location (GUdevDevice *device)
return ret; return ret;
} }
gboolean
parse_accel_label (const char *location, AccelLocation *value)
{
if (location == NULL ||
*location == '\0')
return FALSE;
if (g_str_equal (location, "accel-base")) {
*value = ACCEL_LOCATION_BASE;
return TRUE;
} else if (g_str_equal (location, "accel-display")) {
*value = ACCEL_LOCATION_DISPLAY;
return TRUE;
}
g_debug ("Failed to parse label '%s' as a location", location);
return FALSE;
}
gboolean gboolean
parse_accel_location (const char *location, AccelLocation *value) parse_accel_location (const char *location, AccelLocation *value)
{ {

View File

@@ -22,5 +22,7 @@ AccelLocation setup_accel_location (GUdevDevice *device);
gboolean parse_accel_location (const char *location, gboolean parse_accel_location (const char *location,
AccelLocation *value); AccelLocation *value);
gboolean parse_accel_label (const char *location,
AccelLocation *value);
gboolean get_accel_scale (GUdevDevice *device, AccelScale *scale_vec); gboolean get_accel_scale (GUdevDevice *device, AccelScale *scale_vec);

View File

@@ -13,6 +13,28 @@
#define VALID_BASE_LOCATION "base" #define VALID_BASE_LOCATION "base"
#define INVALID_LOCATION "invalid" #define INVALID_LOCATION "invalid"
#define VALID_DISPLAY_LOCATION_LABEL "accel-display"
#define VALID_BASE_LOCATION_LABEL "accel-base"
#define INVALID_LOCATION_LABEL "proximity-foo-bar"
static void
test_accel_label (void)
{
AccelLocation location;
/* display location */
g_assert_true (parse_accel_label (VALID_DISPLAY_LOCATION_LABEL, &location));
g_assert_true (location == ACCEL_LOCATION_DISPLAY);
/* base location */
g_assert_true (parse_accel_label (VALID_BASE_LOCATION_LABEL, &location));
g_assert_true (location == ACCEL_LOCATION_BASE);
/* invalid label */
g_assert_false (parse_accel_location (NULL, &location));
g_assert_false (parse_accel_location (INVALID_LOCATION_LABEL, &location));
}
static void static void
test_accel_location (void) test_accel_location (void)
{ {
@@ -41,6 +63,7 @@ int main (int argc, char **argv)
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/iio-sensor-proxy/accel-location", test_accel_location); g_test_add_func ("/iio-sensor-proxy/accel-location", test_accel_location);
g_test_add_func ("/iio-sensor-proxy/accel-label", test_accel_label);
return g_test_run (); return g_test_run ();
} }