Ben Chan | 120c11c | 2012-09-10 23:10:18 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CROS_DISKS_USB_DEVICE_INFO_H_ |
| 6 | #define CROS_DISKS_USB_DEVICE_INFO_H_ |
| 7 | |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 8 | #include <map> |
Ben Chan | 32e4b4b | 2014-07-23 16:18:37 -0700 | [diff] [blame] | 9 | #include <string> |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 10 | |
Ben Chan | 6d0b272 | 2011-11-18 08:24:14 -0800 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 12 | #include <gtest/gtest_prod.h> |
| 13 | |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 14 | namespace cros_disks { |
| 15 | |
| 16 | struct USBDeviceEntry; |
| 17 | |
| 18 | // A class for querying information from a USB device info file. |
| 19 | class USBDeviceInfo { |
| 20 | public: |
| 21 | USBDeviceInfo(); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 22 | USBDeviceInfo(const USBDeviceInfo&) = delete; |
| 23 | USBDeviceInfo& operator=(const USBDeviceInfo&) = delete; |
| 24 | |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 25 | ~USBDeviceInfo(); |
| 26 | |
| 27 | // Returns the device media type of a USB device with |vendor_id| and |
| 28 | // |product_id|. |
| 29 | DeviceMediaType GetDeviceMediaType(const std::string& vendor_id, |
| 30 | const std::string& product_id) const; |
| 31 | |
| 32 | // Retrieves the list of USB device info from a file at |path|. |
| 33 | // Returns true on success. |
| 34 | bool RetrieveFromFile(const std::string& path); |
| 35 | |
Ben Chan | 120c11c | 2012-09-10 23:10:18 -0700 | [diff] [blame] | 36 | // Gets the vendor and product name that correspond to |vendor_id| and |
| 37 | // |product_id| from a USB ID database at |ids_file|. |
| 38 | bool GetVendorAndProductName(const std::string& ids_file, |
| 39 | const std::string& vendor_id, |
| 40 | const std::string& product_id, |
| 41 | std::string* vendor_name, |
| 42 | std::string* product_name); |
| 43 | |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 44 | private: |
| 45 | // Converts from string to enum of a device media type. |
| 46 | DeviceMediaType ConvertToDeviceMediaType(const std::string& str) const; |
| 47 | |
Ben Chan | 120c11c | 2012-09-10 23:10:18 -0700 | [diff] [blame] | 48 | // Returns true if |line| contains a 4-digit hex identifier and a name |
| 49 | // separated by two spaces, i.e. "<4-digit hex ID> <descriptive name>". |
| 50 | // The extracted identifier and name are returned via |id| and |name|, |
| 51 | // respectively. |
| 52 | bool ExtractIdAndName(const std::string& line, |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 53 | std::string* id, |
| 54 | std::string* name) const; |
Ben Chan | 120c11c | 2012-09-10 23:10:18 -0700 | [diff] [blame] | 55 | |
| 56 | // Returns true if |line| is skippable, i.e. an empty or comment line. |
| 57 | bool IsLineSkippable(const std::string& line) const; |
| 58 | |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 59 | // A map from an ID string, in form of <vendor id>:<product id>, to a |
| 60 | // USBDeviceEntry struct. |
| 61 | std::map<std::string, USBDeviceEntry> entries_; |
| 62 | |
| 63 | FRIEND_TEST(USBDeviceInfoTest, ConvertToDeviceMediaType); |
Ben Chan | 120c11c | 2012-09-10 23:10:18 -0700 | [diff] [blame] | 64 | FRIEND_TEST(USBDeviceInfoTest, ExtractIdAndName); |
| 65 | FRIEND_TEST(USBDeviceInfoTest, IsLineSkippable); |
Ben Chan | 0a389a6 | 2011-10-03 15:02:34 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace cros_disks |
| 69 | |
| 70 | #endif // CROS_DISKS_USB_DEVICE_INFO_H_ |