summaryrefslogtreecommitdiffstats
path: root/device/usb/usb_ids.cc
blob: 229873b3c22df4814133e24d3d5d6185ed02ea1d (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/usb/usb_ids.h"

#include <stdlib.h>

namespace device {

namespace {

static int CompareVendors(const void* a, const void* b) {
  const UsbVendor* vendor_a = static_cast<const UsbVendor*>(a);
  const UsbVendor* vendor_b = static_cast<const UsbVendor*>(b);
  return vendor_a->id - vendor_b->id;
}

static int CompareProducts(const void* a, const void* b) {
  const UsbProduct* product_a = static_cast<const UsbProduct*>(a);
  const UsbProduct* product_b = static_cast<const UsbProduct*>(b);
  return product_a->id - product_b->id;
}

}  // namespace

const UsbVendor* UsbIds::FindVendor(uint16_t vendor_id) {
  const UsbVendor key = {vendor_id, NULL, 0, NULL};
  void* result = bsearch(&key, vendors_, vendor_size_, sizeof(vendors_[0]),
                         &CompareVendors);
  if (!result)
    return NULL;
  return static_cast<const UsbVendor*>(result);
}

const char* UsbIds::GetVendorName(uint16_t vendor_id) {
  const UsbVendor* vendor = FindVendor(vendor_id);
  if (!vendor)
    return NULL;
  return vendor->name;
}

const char* UsbIds::GetProductName(uint16_t vendor_id, uint16_t product_id) {
  const UsbVendor* vendor = FindVendor(vendor_id);
  if (!vendor)
    return NULL;

  const UsbProduct key = {product_id, NULL};
  void* result = bsearch(&key, vendor->products, vendor->product_size,
                         sizeof(vendor->products[0]), &CompareProducts);
  if (!result)
    return NULL;
  return static_cast<const UsbProduct*>(result)->name;
}

}  // namespace device