summaryrefslogtreecommitdiff
path: root/src/lib/eeze_udev_private.c
blob: 7e5b5dd8a4814835ff921eb06efa6935281e5229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <Eeze.h>
#include "eeze_udev_private.h"

/*
 * helper function to set up a new device from a syspath
 * which may or may not include /sys at the beginning
 */
_udev_device *
_new_device(const char *syspath)
{
   _udev_device *device;

   device = udev_device_new_from_syspath(udev, syspath);
   if (!device)
     ERR("device %s does not exist!", syspath);
   return device;
}

/*
 * copies a device
 */
_udev_device *
_copy_device(_udev_device *device)
{
   return udev_device_ref(device);
}

/*
 * private function to simulate udevadm info -a
 * walks up the device tree checking each node for sysattr
 * with value $value
 */
Eina_Bool
_walk_parents_test_attr(_udev_device *device,
                        const char   *sysattr,
                        const char   *value)
{
   _udev_device *parent, *child = device;
   const char *test;

   if (udev_device_get_sysattr_value(device, sysattr))
     return EINA_TRUE;

   parent = udev_device_get_parent(child);

   for (; parent; child = parent, parent = udev_device_get_parent(child))
     {
        if (!(test = udev_device_get_sysattr_value(parent, sysattr)))
          continue;

        if (!value)
          return EINA_TRUE;
        else
        if (!strcmp(test, value))
          return EINA_TRUE;
     }

   return EINA_FALSE;
}

const char *
_walk_parents_get_attr(_udev_device *device,
                       const char   *sysattr,
                       Eina_Bool property)
{
   _udev_device *parent, *child = device;
   const char *test;

   if (property)
     test = udev_device_get_property_value(device, sysattr);
   else
     test = udev_device_get_sysattr_value(device, sysattr);
   if (test) return eina_stringshare_add(test);

   parent = udev_device_get_parent(child);

   for (; parent; child = parent, parent = udev_device_get_parent(child))
     {
        if (property)
          test = udev_device_get_property_value(parent, sysattr);
        else
          test = udev_device_get_sysattr_value(parent, sysattr);
        if (test) return eina_stringshare_add(test);
     }

   return NULL;
}

const char *
_walk_children_get_attr(const char *syspath,
                        const char *sysattr,
                        const char *subsystem,
                        Eina_Bool property)
{
   char buf[PATH_MAX];
   const char *path, *ret = NULL;
   _udev_enumerate *en;
   _udev_list_entry *devs, *cur;

   en = udev_enumerate_new(udev);
   EINA_SAFETY_ON_NULL_RETURN_VAL(en, NULL);
   path = strrchr(syspath, '/');
   if (path) path++;
   else path = syspath;
   snprintf(buf, sizeof(buf), "%s*", path);
   udev_enumerate_add_match_sysname(en, buf);
   if (subsystem) udev_enumerate_add_match_subsystem(en, subsystem);
   udev_enumerate_scan_devices(en);
   devs = udev_enumerate_get_list_entry(en);
   udev_list_entry_foreach(cur, devs)
     {
        const char *devname, *test;
        _udev_device *device;

        devname = udev_list_entry_get_name(cur);
        device = _new_device(devname);
        if (property)
          test = udev_device_get_property_value(device, sysattr);
        else
          test = udev_device_get_sysattr_value(device, sysattr);
        if (test)
          {
             ret = eina_stringshare_add(test);
             udev_device_unref(device);
             break;
          }
        udev_device_unref(device);
     }
   udev_enumerate_unref(en);
   return ret;
}

/*
 * check a list for all parents of a device,
 * stringshare adding all devices that are not in the list
 */
Eina_List *
_get_unlisted_parents(Eina_List    *list,
                      _udev_device *device)
{
   _udev_device *parent, *child = device;
   const char *test, *devname, *vendor, *vendor2, *model, *model2;
   Eina_List *l;
   Eina_Bool found;

   if (!(vendor = udev_device_get_property_value(child, "ID_VENDOR_ID")))
     vendor = udev_device_get_property_value(child, "ID_VENDOR");
   if (!vendor) vendor = udev_device_get_sysattr_value(child, "vendor");
   if (!vendor) vendor = udev_device_get_sysattr_value(child, "manufacturer");

   if (!(model = udev_device_get_property_value(child, "ID_MODEL_ID")))
     model = udev_device_get_property_value(child, "ID_MODEL");
   if (!model) model = udev_device_get_sysattr_value(child, "model");
   if (!model) model = udev_device_get_sysattr_value(child, "product");

   parent = udev_device_get_parent(child);

   for (; parent; child = parent, parent = udev_device_get_parent(child))
     {
        found = EINA_FALSE;

        if (!(vendor2 = udev_device_get_property_value(child, "ID_VENDOR_ID")))
          vendor2 = udev_device_get_property_value(child, "ID_VENDOR");
        if (!vendor2) vendor2 = udev_device_get_sysattr_value(child, "vendor");
        if (!vendor2) vendor2 = udev_device_get_sysattr_value(child, "manufacturer");

        if (!(model2 = udev_device_get_property_value(child, "ID_MODEL_ID")))
          model2 = udev_device_get_property_value(child, "ID_MODEL");
        if (!model2) model2 = udev_device_get_sysattr_value(child, "model");
        if (!model2) model2 = udev_device_get_sysattr_value(child, "product");

        if ((!model2 && model) || (model2 && !model) || (!vendor2 && vendor)
            || (vendor2 && !vendor))
          break;
        else
        if (((model && model2) && (strcmp(model, model2))) ||
            ((vendor && vendor2) && (strcmp(vendor, vendor2))))
          break;

        devname = udev_device_get_syspath(parent);
        EINA_LIST_FOREACH(list, l, test)
          {
             if (!strcmp(test, devname))
               {
                  found = EINA_TRUE;
                  break;
               }
          }

        if (!found)
          list = eina_list_prepend(list, eina_stringshare_add(devname));
     }

   return list;
}