Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
      3  * Use of this source code is governed by a BSD-style license that can be
      4  * found in the LICENSE file.
      5  */
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 
     10 #include <glib.h>
     11 #include <glib-object.h>
     12 
     13 #define G_UDEV_API_IS_SUBJECT_TO_CHANGE
     14 #include <gudev/gudev.h>
     15 
     16 gboolean lookup (const gpointer data);
     17 
     18 static GMainLoop* loop;
     19 
     20 int
     21 main (int argc, const char *argv[])
     22 {
     23   int i;
     24 
     25   #if !GLIB_CHECK_VERSION(2,35,0)
     26   g_type_init ();
     27   #endif
     28 
     29   loop = g_main_loop_new (NULL, FALSE);
     30 
     31   for (i = 1 ; i < argc ; i++)
     32     g_idle_add (lookup, (const gpointer)argv[i]);
     33 
     34   g_main_loop_run (loop);
     35 
     36   g_main_loop_unref (loop);
     37 
     38   return 0;
     39 }
     40 
     41 static void
     42 print_device(GUdevDevice *device)
     43 {
     44   GHashTable *properties;
     45   GHashTableIter iter;
     46   gpointer key, value;
     47 
     48   printf (" Name:        %s\n", g_udev_device_get_name (device));
     49   printf (" Device file: %s\n", g_udev_device_get_device_file (device));
     50   printf (" Devtype:     %s\n", g_udev_device_get_devtype (device));
     51   printf (" Driver:      %s\n", g_udev_device_get_driver (device));
     52   printf (" Subsystem:   %s\n", g_udev_device_get_subsystem (device));
     53   printf (" Sysfs path:  %s\n", g_udev_device_get_sysfs_path (device));
     54 
     55   /* We want to print out properties in some fixed order every time.
     56    * To do this, we hash on the property name, and then iterate.
     57    */
     58   const gchar * const * keys = g_udev_device_get_property_keys (device);
     59   properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
     60   for (;*keys;++keys) {
     61     const gchar * prop;
     62 
     63     prop = g_udev_device_get_property (device, *keys);
     64     g_hash_table_insert (properties, g_strdup (*keys), g_strdup (prop));
     65   }
     66 
     67   g_hash_table_iter_init (&iter, properties);
     68   while (g_hash_table_iter_next (&iter, &key, &value))
     69     printf ("  Property %s: %s\n", (gchar *)key, (gchar *)value);
     70 
     71   g_hash_table_unref (properties);
     72 }
     73 
     74 gboolean
     75 lookup (const gpointer data)
     76 {
     77   const char *path = data;
     78 
     79   GUdevClient *guclient = g_udev_client_new (NULL);
     80   GUdevDevice *device;
     81 
     82   if (path[0] == '=') {
     83     gchar **parts;
     84     parts = g_strsplit (path+1, ",", 2);
     85 
     86     device = g_udev_client_query_by_subsystem_and_name (guclient, parts[0],
     87                                                         parts[1]);
     88     g_strfreev (parts);
     89   } else if (strncmp (path, "/sys/", 5) == 0) {
     90     device = g_udev_client_query_by_sysfs_path (guclient, path);
     91   } else {
     92     device = g_udev_client_query_by_device_file (guclient, path);
     93   }
     94 
     95   if (device) {
     96     print_device (device);
     97     if (1) {
     98       GUdevDevice *parent;
     99       parent = g_udev_device_get_parent (device);
    100       if (parent) {
    101         printf ("Parent device:\n");
    102         print_device (parent);
    103         g_object_unref (parent);
    104       }
    105     }
    106     g_object_unref (device);
    107   }
    108   printf("\n");
    109 
    110   g_object_unref (guclient);
    111 
    112   g_main_loop_quit (loop);
    113 
    114   return FALSE;
    115 }
    116