Skip to content

Instantly share code, notes, and snippets.

@fourdollars
Created September 2, 2013 07:39
Show Gist options
  • Save fourdollars/6410173 to your computer and use it in GitHub Desktop.
Save fourdollars/6410173 to your computer and use it in GitHub Desktop.
A example by gudev to list if the power_supply is from bluetooth or not.
/* -*- coding: utf-8; indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- */
/**
* Copyright (C) 2013 Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// gcc -Wall -Werror -g -std=c99 udev-bluetooth-power_supply.c `pkg-config --cflags --libs gudev-1.0` -o udev-bluetooth-power_supply
#include <stdio.h>
#include <gudev/gudev.h>
int main(int argc, char* argv[])
{
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
#endif
GList *l = NULL;
const gchar *subsystems[] = {"power_supply", NULL};
GUdevClient *gudev_client = g_udev_client_new (subsystems);
for (int i=0; subsystems[i] != NULL; i++) {
GList *devices = g_udev_client_query_by_subsystem (gudev_client, subsystems[i]);
for (l = devices; l != NULL; l = l->next) {
GUdevDevice *native = l->data;
printf("name: %s\n", g_udev_device_get_name (native));
printf("sysfs: %s\n", g_udev_device_get_sysfs_path (native));
printf("subsystem: %s\n", g_udev_device_get_subsystem (native));
printf("devtype: %s\n", g_udev_device_get_devtype (native));
printf("device type: %c\n", g_udev_device_get_device_type (native));
printf("device file: %s\n", g_udev_device_get_device_file (native));
printf("driver: %s\n", g_udev_device_get_driver (native));
const gchar * const * symlinks = g_udev_device_get_device_file_symlinks (native);
for (int j = 0; symlinks[j] != NULL; j++) {
g_print("symlink: %s\n", symlinks[j]);
}
const gchar * const * tags = g_udev_device_get_tags (native);
for (int j = 0; tags[j] != NULL; j++) {
g_print("tag: %s\n", tags[j]);
}
GUdevDevice *parent = g_udev_device_get_parent_with_subsystem (native, "bluetooth", NULL);
if (parent != NULL) {
printf("\tname: %s\n", g_udev_device_get_name (parent));
printf("\tsysfs: %s\n", g_udev_device_get_sysfs_path (parent));
printf("\tsubsystem: %s\n", g_udev_device_get_subsystem (parent));
printf("\tdevtype: %s\n", g_udev_device_get_devtype (parent));
printf("\tdevice type: %c\n", g_udev_device_get_device_type (parent));
printf("\tdevice file: %s\n", g_udev_device_get_device_file (parent));
printf("\tdriver: %s\n", g_udev_device_get_driver (parent));
const gchar * const * symlinks = g_udev_device_get_device_file_symlinks (parent);
for (int j = 0; symlinks[j] != NULL; j++) {
g_print("\tsymlink: %s\n", symlinks[j]);
}
const gchar * const * tags = g_udev_device_get_tags (parent);
for (int j = 0; tags[j] != NULL; j++) {
g_print("\ttag: %s\n", tags[j]);
}
g_object_unref (parent);
} else {
printf("\tNo parent\n");
}
printf("\n");
}
g_list_foreach (devices, (GFunc) g_object_unref, NULL);
g_list_free (devices);
}
g_object_unref (gudev_client);
return 0;
}
/* vim:set fileencodings=utf-8 tabstop=4 expandtab shiftwidth=4 softtabstop=4: */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment