~ubuntu-branches/debian/stretch/libgusb/stretch

« back to all changes in this revision

Viewing changes to tools/gusb-main.c

  • Committer: Package Import Robot
  • Author(s): Michal Čihař
  • Date: 2015-03-16 08:43:22 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20150316084322-i7hnfx9wpstcyc9e
Tags: 0.2.4-1
* New upstream release.
* Raised glib dependency to 2.38.
* New exported symbols.

Show diffs side-by-side

added added

removed removed

Lines of Context:
187
187
}
188
188
 
189
189
/**
 
190
 * gusb_devices_sort_by_platform_id_cb:
 
191
 **/
 
192
static gint
 
193
gusb_devices_sort_by_platform_id_cb (gconstpointer a, gconstpointer b)
 
194
{
 
195
        GUsbDevice *device_a = *((GUsbDevice **) a);
 
196
        GUsbDevice *device_b = *((GUsbDevice **) b);
 
197
        return g_strcmp0 (g_usb_device_get_platform_id (device_a),
 
198
                          g_usb_device_get_platform_id (device_b));
 
199
}
 
200
 
 
201
static gboolean
 
202
moo_cb (GNode *node, gpointer data)
 
203
{
 
204
        GUsbDevice *device = G_USB_DEVICE (node->data);
 
205
        GNode *n;
 
206
        guint i;
 
207
        const gchar *tmp;
 
208
        gchar *product = NULL;
 
209
        gchar *vendor = NULL;
 
210
        GString *str = NULL;
 
211
 
 
212
        if (device == NULL) {
 
213
                g_print ("Root Device\n");
 
214
                return FALSE;
 
215
        }
 
216
 
 
217
        /* indent */
 
218
        str = g_string_new ("");
 
219
        for (n = node; n->data != NULL; n = n->parent)
 
220
                g_string_append (str, " ");
 
221
 
 
222
        /* add bus:address */
 
223
        g_string_append_printf (str, "%02x:%02x [%04x:%04x]",
 
224
                                g_usb_device_get_bus (device),
 
225
                                g_usb_device_get_address (device),
 
226
                                g_usb_device_get_vid (device),
 
227
                                g_usb_device_get_pid (device));
 
228
 
 
229
        /* pad */
 
230
        for (i = str->len; i < 30; i++)
 
231
                g_string_append (str, " ");
 
232
 
 
233
        /* We don't error check these as not all devices have these
 
234
           (and the device_open may have failed). */
 
235
        g_usb_device_open (device, NULL);
 
236
        vendor = g_usb_device_get_string_descriptor (device,
 
237
                        g_usb_device_get_manufacturer_index (device),
 
238
                        NULL);
 
239
        product = g_usb_device_get_string_descriptor (device,
 
240
                        g_usb_device_get_product_index (device),
 
241
                        NULL);
 
242
 
 
243
        /* lookup from usb.ids */
 
244
        if (vendor == NULL) {
 
245
                tmp = g_usb_device_get_vid_as_str (device);
 
246
                if (tmp != NULL)
 
247
                        vendor = g_strdup (tmp);
 
248
        }
 
249
 
 
250
        if (product == NULL) {
 
251
                tmp = g_usb_device_get_pid_as_str (device);
 
252
                if (tmp != NULL)
 
253
                        product = g_strdup (tmp);
 
254
        }
 
255
 
 
256
        /* a hub */
 
257
        if (g_usb_device_get_device_class (device) == 0x09 && product == NULL) {
 
258
                product = g_strdup ("USB HUB");
 
259
        }
 
260
 
 
261
        /* fall back to the VID/PID */
 
262
        if (product == NULL)
 
263
                product = g_strdup ("Unknown");
 
264
 
 
265
        if (vendor == NULL)
 
266
                vendor = g_strdup ("Unknown");
 
267
 
 
268
        /* add bus:address */
 
269
        g_string_append_printf (str, "%s - %s", vendor, product);
 
270
        g_free (product);
 
271
        g_free (vendor);
 
272
 
 
273
        g_print ("%s\n", str->str);
 
274
        g_string_free (str, TRUE);
 
275
 
 
276
        return FALSE;
 
277
}
 
278
 
 
279
/**
190
280
 * gusb_cmd_show:
191
281
 **/
192
282
static gboolean
193
283
gusb_cmd_show (GUsbCmdPrivate *priv, gchar **values, GError **error)
194
284
{
195
 
        gboolean ret = TRUE;
196
 
        GPtrArray *devices;
 
285
        GNode *n;
 
286
        GNode *node;
197
287
        guint i;
198
288
        GUsbDevice *device;
 
289
        GUsbDevice *parent;
 
290
        GPtrArray *devices = NULL;
199
291
 
 
292
        /* sort */
200
293
        devices = g_usb_context_get_devices (priv->usb_ctx);
 
294
        g_ptr_array_sort (devices, gusb_devices_sort_by_platform_id_cb);
 
295
 
 
296
        /* make a tree of the devices */
 
297
        node = g_node_new (NULL);
201
298
        for (i = 0; i < devices->len; i++) {
202
299
                device = g_ptr_array_index (devices, i);
203
 
                g_print ("device present %x:%x\n",
204
 
                         g_usb_device_get_bus (device),
205
 
                         g_usb_device_get_address (device));
 
300
 
 
301
                parent = g_usb_device_get_parent (device);
 
302
                if (parent == NULL) {
 
303
                        g_node_append_data (node, device);
 
304
                        continue;
 
305
                }
 
306
                n = g_node_find (node, G_PRE_ORDER, G_TRAVERSE_ALL, parent);
 
307
                if (n == NULL) {
 
308
                        g_set_error (error, 1, 0,
 
309
                                     "no parent node for %s",
 
310
                                     g_usb_device_get_platform_id (device));
 
311
                        return FALSE;
 
312
                }
 
313
                g_node_append_data (n, device);
 
314
 
206
315
        }
 
316
 
207
317
        g_ptr_array_unref (devices);
208
 
        return ret;
 
318
        g_node_traverse (node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, moo_cb, priv);
 
319
 
 
320
        return TRUE;
209
321
}
210
322
 
211
323
/**
223
335
        devices = g_usb_context_get_devices (priv->usb_ctx);
224
336
        for (i = 0; i < devices->len; i++) {
225
337
                device = g_ptr_array_index (devices, i);
226
 
                g_print ("device already present %x:%x\n",
 
338
                g_print ("device %s already present %x:%x\n",
 
339
                         g_usb_device_get_platform_id (device),
227
340
                         g_usb_device_get_bus (device),
228
341
                         g_usb_device_get_address (device));
229
342
        }