OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
osvr_list_usbserial.cpp
Go to the documentation of this file.
1 
12 // Copyright 2015 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 // Internal Includes
29 
30 // Library/third-party includes
31 // - none
32 
33 // Standard includes
34 #include <cstdint>
35 #include <cstdlib> // for EXIT_SUCCESS
36 #include <functional>
37 #include <iomanip> // for setfill, setw
38 #include <ios> // for hex
39 #include <iostream> // for cout
40 #include <sstream>
41 
43 std::string padHex(std::uint16_t v) {
44  std::ostringstream os;
45  os << std::setfill('0') << std::setw(4) << std::hex << v;
46  return os.str();
47 }
48 
49 int main(int argc, char *argv[]) {
50  using namespace std;
51 
52  function<osvr::usbserial::Enumerator()> enumerate = [] {
53  return osvr::usbserial::enumerate();
54  };
55 
56  if (argc == 3) {
57 
58  uint16_t vID;
59  {
60  stringstream ss;
61  ss << hex << argv[1];
62  if (!(ss >> vID)) {
63  cerr << "Could not parse first command line argument as a "
64  "hex vendor ID!"
65  << endl;
66  return -1;
67  }
68  }
69  uint16_t pID;
70  {
71  stringstream ss;
72  ss << hex << argv[2];
73  if (!(ss >> pID)) {
74  cerr << "Could not parse second command line argument as "
75  "a hex product ID!"
76  << endl;
77  return -1;
78  }
79  }
80  cout << "Will enumerate USB Serial devices with hex VID:PID "
81  << padHex(vID) << ":" << padHex(pID) << "\n"
82  << endl;
83  enumerate = [vID, pID] { return osvr::usbserial::enumerate(vID, pID); };
84 
85  } else if (argc != 1) {
86  cerr << "Usage: either pass no arguments to enumerate all USB "
87  "Serial devices, or pass two arguments: a hex VID and PID "
88  "to search for."
89  << endl;
90  return -1;
91 
92  } else {
93  cout << "Will enumerate all USB Serial devices.\n" << endl;
94  }
95 
96  for (auto &&dev : enumerate()) {
97  cout << setfill('0') << setw(4) << hex << dev.getVID() << ":"
98  << setfill('0') << setw(4) << hex << dev.getPID() << " "
99  << dev.getPlatformSpecificPath() << endl;
100  }
101 
102  return EXIT_SUCCESS;
103 }
STL namespace.
std::string padHex(std::uint16_t v)
Returns a hex value always padded out to 4 digits.