blob: 8f011ca5b0e22df42bd67104854eb0b7e2e46562 [file] [log] [blame]
Ben Chan120c11c2012-09-10 23:10:18 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ben Chan0a389a62011-10-03 15:02:34 -07002// 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 Chan0a389a62011-10-03 15:02:34 -07008#include <map>
Ben Chan32e4b4b2014-07-23 16:18:37 -07009#include <string>
Ben Chan0a389a62011-10-03 15:02:34 -070010
Ben Chan6d0b2722011-11-18 08:24:14 -080011#include <chromeos/dbus/service_constants.h>
Ben Chan0a389a62011-10-03 15:02:34 -070012#include <gtest/gtest_prod.h>
13
Ben Chan0a389a62011-10-03 15:02:34 -070014namespace cros_disks {
15
16struct USBDeviceEntry;
17
18// A class for querying information from a USB device info file.
19class USBDeviceInfo {
20 public:
21 USBDeviceInfo();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090022 USBDeviceInfo(const USBDeviceInfo&) = delete;
23 USBDeviceInfo& operator=(const USBDeviceInfo&) = delete;
24
Ben Chan0a389a62011-10-03 15:02:34 -070025 ~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 Chan120c11c2012-09-10 23:10:18 -070036 // 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 Chan0a389a62011-10-03 15:02:34 -070044 private:
45 // Converts from string to enum of a device media type.
46 DeviceMediaType ConvertToDeviceMediaType(const std::string& str) const;
47
Ben Chan120c11c2012-09-10 23:10:18 -070048 // 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 Chande0e3f62017-09-26 06:28:39 -070053 std::string* id,
54 std::string* name) const;
Ben Chan120c11c2012-09-10 23:10:18 -070055
56 // Returns true if |line| is skippable, i.e. an empty or comment line.
57 bool IsLineSkippable(const std::string& line) const;
58
Ben Chan0a389a62011-10-03 15:02:34 -070059 // 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 Chan120c11c2012-09-10 23:10:18 -070064 FRIEND_TEST(USBDeviceInfoTest, ExtractIdAndName);
65 FRIEND_TEST(USBDeviceInfoTest, IsLineSkippable);
Ben Chan0a389a62011-10-03 15:02:34 -070066};
67
68} // namespace cros_disks
69
70#endif // CROS_DISKS_USB_DEVICE_INFO_H_