windows/usb_sensor_enumerator.cpp
Go to the documentation of this file.
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2019, Analog Devices, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
36 #include "utils.h"
37 
38 #ifdef USE_GLOG
39 #include <glog/logging.h>
40 #else
41 #include <aditof/log.h>
42 #endif
43 
44 #include <atlstr.h>
45 #include <memory>
46 #include <strmif.h>
47 
48 using namespace aditof;
49 
51 
53  using namespace std;
54  Status status = Status::OK;
55 
56  LOG(INFO) << "Looking for USB connected sensors";
57 
58  HRESULT hr;
59 
60  hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
61 
62  std::string devName("UVC Camera");
63 
64  ICreateDevEnum *DevEnum = NULL;
65 
66  hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
67  IID_PPV_ARGS(&DevEnum));
68  if (FAILED(hr)) {
69  LOG(ERROR) << "Create Device Enumeration Failed" << std::endl;
70  return Status::GENERIC_ERROR;
71  }
72 
73  IEnumMoniker *EnumMoniker = NULL;
74  hr = DevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
75  &EnumMoniker, 0);
76 
77  if (hr != S_OK) {
78  DevEnum->Release();
79  LOG(ERROR) << "Device Enumeration Error" << std::endl;
80  return Status::GENERIC_ERROR;
81  }
82 
83  IMoniker *Moniker = NULL;
84  ULONG cFetched;
85  while (EnumMoniker->Next(1, &Moniker, &cFetched) == S_OK) {
86  IPropertyBag *PropBag;
87  hr = Moniker->BindToStorage(0, 0, IID_PPV_ARGS(&PropBag));
88 
89  if (SUCCEEDED(hr)) {
90  VARIANT varName;
91  VariantInit(&varName);
92  hr = PropBag->Read(L"FriendlyName", &varName, 0);
93 
94  if (SUCCEEDED(hr)) {
95  std::string str(static_cast<LPCTSTR>(CString(varName.bstrVal)));
96  if (str.find(devName) != std::string::npos) {
97  SensorInfo sInfo;
98  sInfo.driverPath = str;
99 
100  DLOG(INFO) << "Found USB capture device: " << str;
101 
102  std::string advertisedSensorData;
103 
104  IBaseFilter *pVideoInputFilter;
105 
106  HRESULT hr =
107  Moniker->BindToObject(nullptr, nullptr, IID_IBaseFilter,
108  (void **)&pVideoInputFilter);
109  if (!SUCCEEDED(hr)) {
110  LOG(WARNING) << "Failed to bind video input filter";
111  return Status::GENERIC_ERROR;
112  }
113 
114  // Query the sensors that are available on target
115 
116  // Send request
117  usb_payload::ClientRequest requestMsg;
118  requestMsg.set_func_name(
119  usb_payload::FunctionName::SEARCH_SENSORS);
120  requestMsg.add_func_int32_param(
121  1); // TO DO: Check if this is needed. Without it, the serialized string will be empty.
122 
123  std::string requestStr;
124  requestMsg.SerializeToString(&requestStr);
126  pVideoInputFilter, requestStr);
127  if (status != aditof::Status::OK) {
128  LOG(ERROR) << "Request to search for sensors failed";
129  return status;
130  }
131 
132  // Read UVC gadget response
135  pVideoInputFilter, responseStr);
136  if (status != aditof::Status::OK) {
137  LOG(ERROR) << "Request to search for sensors failed";
138  return status;
139  }
140  usb_payload::ServerResponse responseMsg;
141  bool parsed = responseMsg.ParseFromString(responseStr);
142  if (!parsed) {
143  LOG(ERROR) << "Failed to deserialize string containing "
144  "UVC gadget response";
146  }
147 
148  DLOG(INFO) << "Received the following message with "
149  "available sensors from target: "
150  << responseMsg.DebugString();
151 
152  if (responseMsg.status() != usb_payload::Status::OK) {
153  LOG(ERROR) << "Search for sensors operation failed on "
154  "UVC gadget";
155  return static_cast<aditof::Status>(
156  responseMsg.status());
157  }
158 
159  // If request and response went well, extract data from response
160  m_sensorsInfo.emplace_back(sInfo);
161 
162  m_sensorName =
163  responseMsg.sensors_info().image_sensors().name();
164 
165  m_kernelVersion =
166  responseMsg.card_image_version().kernelversion();
167  m_sdVersion = responseMsg.card_image_version().sdversion();
168  m_uBootVersion =
169  responseMsg.card_image_version().ubootversion();
170  }
171  }
172  VariantClear(&varName);
173  PropBag->Release();
174  PropBag = NULL;
175  }
176 
177  Moniker->Release();
178  Moniker = NULL;
179  }
180 
181  EnumMoniker->Release();
182  DevEnum->Release();
183 
184  return status;
185 }
186 
188  std::vector<std::shared_ptr<DepthSensorInterface>> &depthSensors) {
189 
190  depthSensors.clear();
191 
192  for (const auto &sInfo : m_sensorsInfo) {
193  auto sensor =
194  std::make_shared<UsbDepthSensor>(m_sensorName, sInfo.driverPath);
195  depthSensors.emplace_back(sensor);
196  }
197 
198  return Status::OK;
199 }
200 
203  uBootVersion = m_uBootVersion;
204  return aditof::Status::OK;
205 }
206 
209  kernelVersion = m_kernelVersion;
210  return aditof::Status::OK;
211 }
212 
215  sdkVersion = m_sdVersion;
216  return aditof::Status::OK;
217 }
usb_depth_sensor.h
INFO
const int INFO
Definition: log_severity.h:59
NULL
NULL
Definition: test_security_zap.cpp:405
ERROR
const int ERROR
Definition: log_severity.h:60
UsbSensorEnumerator::getSdVersion
virtual aditof::Status getSdVersion(std::string &sdVersion) const override
Get the SD card image version on the embedded system that the camera is attached to.
Definition: linux/usb_sensor_enumerator.cpp:225
aditof::Status::GENERIC_ERROR
@ GENERIC_ERROR
An error occured but there are no details available.
UsbSensorEnumerator::getUbootVersion
virtual aditof::Status getUbootVersion(std::string &uBootVersion) const override
Get the U-Boot version that is installed on the embedded system that the camera is attached to.
Definition: linux/usb_sensor_enumerator.cpp:214
log.h
usb_windows_utils.h
UsbWindowsUtils::uvcExUnitSendRequest
static aditof::Status uvcExUnitSendRequest(IBaseFilter *pVideoInputFilter, const std::string &requestStr)
Definition: usb_windows_utils.cpp:282
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
usb_sensor_enumerator.h
WARNING
const int WARNING
Definition: log_severity.h:59
DLOG
#define DLOG(x)
Definition: sdk/include/aditof/log.h:73
UsbSensorEnumerator::~UsbSensorEnumerator
~UsbSensorEnumerator()
responseStr
ROSCPP_DECL XmlRpc::XmlRpcValue responseStr(int code, const std::string &msg, const std::string &response)
aditof
Namespace aditof.
Definition: adsd_errs.h:40
update_failure_list.str
str
Definition: update_failure_list.py:41
google::protobuf::util::error::OK
@ OK
Definition: status.h:47
UsbWindowsUtils::uvcExUnitGetResponse
static aditof::Status uvcExUnitGetResponse(IBaseFilter *pVideoInputFilter, std::string &responseStr)
Definition: usb_windows_utils.cpp:335
UsbSensorEnumerator::getDepthSensors
virtual aditof::Status getDepthSensors(std::vector< std::shared_ptr< aditof::DepthSensorInterface >> &depthSensors) override
Definition: linux/usb_sensor_enumerator.cpp:199
aditof::Status::INVALID_ARGUMENT
@ INVALID_ARGUMENT
Invalid arguments provided.
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
UsbSensorEnumerator::searchSensors
virtual aditof::Status searchSensors() override
Do a search for the available sensors.
Definition: linux/usb_sensor_enumerator.cpp:56
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72
aditof::Status::OK
@ OK
Success.
std
usb_utils.h
UsbSensorEnumerator::getKernelVersion
virtual aditof::Status getKernelVersion(std::string &kernelVersion) const override
Get the kernel version that is installed on the embedded system that the camera is attached to.
Definition: linux/usb_sensor_enumerator.cpp:220


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:01