diff options
author | gdk@chromium.org <gdk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 22:41:18 +0000 |
---|---|---|
committer | gdk@chromium.org <gdk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 22:41:18 +0000 |
commit | 25d2b1394d6623eabe814c8287c66d4345d2abbe (patch) | |
tree | 1a9de6d8738e8330fb9af9158000b40d84ef12bb /device/usb | |
parent | 13a016d52277755897c27502160a0bfbfb69e455 (diff) | |
download | chromium_src-25d2b1394d6623eabe814c8287c66d4345d2abbe.zip chromium_src-25d2b1394d6623eabe814c8287c66d4345d2abbe.tar.gz chromium_src-25d2b1394d6623eabe814c8287c66d4345d2abbe.tar.bz2 |
Adding USB ID vendor and product lookups.
This change adds a tool for generating the lookup table, a wrapper around the
lookup table, and the build infrastructure required to invoke the rule when
compiling.
BUG=127302
TBR=sky
Review URL: https://chromiumcodereview.appspot.com/11344039
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171339 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device/usb')
-rw-r--r-- | device/usb/OWNERS | 2 | ||||
-rw-r--r-- | device/usb/usb_ids.cc | 58 | ||||
-rw-r--r-- | device/usb/usb_ids.h | 58 | ||||
-rw-r--r-- | device/usb/usb_ids_unittest.cc | 31 |
4 files changed, 149 insertions, 0 deletions
diff --git a/device/usb/OWNERS b/device/usb/OWNERS new file mode 100644 index 0000000..2ffd61a --- /dev/null +++ b/device/usb/OWNERS @@ -0,0 +1,2 @@ +gdk@chromium.org +bryeung@chromium.org diff --git a/device/usb/usb_ids.cc b/device/usb/usb_ids.cc new file mode 100644 index 0000000..fbb5b7e --- /dev/null +++ b/device/usb/usb_ids.cc @@ -0,0 +1,58 @@ +// 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" + +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 diff --git a/device/usb/usb_ids.h b/device/usb/usb_ids.h new file mode 100644 index 0000000..2f6b950 --- /dev/null +++ b/device/usb/usb_ids.h @@ -0,0 +1,58 @@ +// 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. + +#ifndef DEVICE_USB_USB_IDS_H_ +#define DEVICE_USB_USB_IDS_H_ + +#include <stdint.h> + +#include "base/basictypes.h" + +namespace device { + +struct UsbProduct { + const uint16_t id; + const char* name; +}; + +struct UsbVendor { + const uint16_t id; + const char* name; + const size_t product_size; + const UsbProduct* products; +}; + +// UsbIds provides a static mapping from a vendor ID to a name, as well as a +// mapping from a vendor/product ID pair to a product name. +class UsbIds { + public: + // Gets the name of the vendor who owns |vendor_id|. Returns NULL if the + // specified |vendor_id| does not exist. + static const char* GetVendorName(uint16_t vendor_id); + + // Gets the name of a product belonging to a specific vendor. If either + // |vendor_id| refers to a vendor that does not exist, or |vendor_id| is valid + // but |product_id| refers to a product that does not exist, this method + // returns NULL. + static const char* GetProductName(uint16_t vendor_id, uint16_t product_id); + + private: + UsbIds(); + ~UsbIds(); + + // Finds the static UsbVendor associated with |vendor_id|. Returns NULL if no + // such vendor exists. + static const UsbVendor* FindVendor(uint16_t vendor_id); + + // These fields are defined in a generated file. See device/device.gyp for + // more information on how they are generated. + static const size_t vendor_size_; + static const UsbVendor vendors_[]; + + DISALLOW_COPY_AND_ASSIGN(UsbIds); +}; + +} // namespace device + +#endif // DEVICE_USB_USB_IDS_H_ diff --git a/device/usb/usb_ids_unittest.cc b/device/usb/usb_ids_unittest.cc new file mode 100644 index 0000000..decce1e --- /dev/null +++ b/device/usb/usb_ids_unittest.cc @@ -0,0 +1,31 @@ +// 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 <string> + +#include "device/usb/usb_ids.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +static const uint16_t kGoogleVendorId = 0x18d1; +static const uint16_t kNexusSProductId = 0x4e21; + +} // namespace + +namespace device { + +TEST(UsbIdsTest, GetVendorName) { + EXPECT_EQ(NULL, UsbIds::GetVendorName(0)); + EXPECT_EQ(std::string("Google Inc."), UsbIds::GetVendorName(kGoogleVendorId)); +} + +TEST(UsbIdsTest, GetProductName) { + EXPECT_EQ(NULL, UsbIds::GetProductName(0, 0)); + EXPECT_EQ(NULL, UsbIds::GetProductName(kGoogleVendorId, 0)); + EXPECT_EQ(std::string("Nexus S"), UsbIds::GetProductName(kGoogleVendorId, + kNexusSProductId)); +} + +} // namespace device |