1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "device/usb/usb_ids.h"
6
7#include <stdlib.h>
8
9#include "base/basictypes.h"
10
11namespace device {
12
13namespace {
14
15static int CompareVendors(const void* a, const void* b) {
16  const UsbVendor* vendor_a = static_cast<const UsbVendor*>(a);
17  const UsbVendor* vendor_b = static_cast<const UsbVendor*>(b);
18  return vendor_a->id - vendor_b->id;
19}
20
21static int CompareProducts(const void* a, const void* b) {
22  const UsbProduct* product_a = static_cast<const UsbProduct*>(a);
23  const UsbProduct* product_b = static_cast<const UsbProduct*>(b);
24  return product_a->id - product_b->id;
25}
26
27}  // namespace
28
29const UsbVendor* UsbIds::FindVendor(uint16_t vendor_id) {
30  const UsbVendor key = {vendor_id, NULL, 0, NULL};
31  void* result = bsearch(&key, vendors_, vendor_size_, sizeof(vendors_[0]),
32                         &CompareVendors);
33  if (!result)
34    return NULL;
35  return static_cast<const UsbVendor*>(result);
36}
37
38const char* UsbIds::GetVendorName(uint16_t vendor_id) {
39  const UsbVendor* vendor = FindVendor(vendor_id);
40  if (!vendor)
41    return NULL;
42  return vendor->name;
43}
44
45const char* UsbIds::GetProductName(uint16_t vendor_id, uint16_t product_id) {
46  const UsbVendor* vendor = FindVendor(vendor_id);
47  if (!vendor)
48    return NULL;
49
50  const UsbProduct key = {product_id, NULL};
51  void* result = bsearch(&key, vendor->products, vendor->product_size,
52                         sizeof(vendor->products[0]), &CompareProducts);
53  if (!result)
54    return NULL;
55  return static_cast<const UsbProduct*>(result)->name;
56}
57
58}  // namespace device
59