Index: device/usb/usb_ids.cc |
diff --git a/device/usb/usb_ids.cc b/device/usb/usb_ids.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e94d91bf6a1fe4d72498e1d0b395ea6dbe0a7896 |
--- /dev/null |
+++ b/device/usb/usb_ids.cc |
@@ -0,0 +1,61 @@ |
+// 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> |
+ |
+#include "base/basictypes.h" |
+ |
+using device::UsbProduct; |
bryeung
2012/10/31 15:01:27
seems weird to have these when you're in a device/
Garret Kelly
2012/10/31 15:24:26
Done.
|
+using device::UsbVendor; |
+ |
+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 |
+ |
+namespace device { |
bryeung
2012/10/31 15:01:27
blank line after namespace lines
Garret Kelly
2012/10/31 15:24:26
Done.
|
+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; |
+ |
bryeung
2012/10/31 15:01:27
either remove this blank line or add it below for
Garret Kelly
2012/10/31 15:24:26
Done.
|
+ 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 |