sensor_enumerator_imx.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  */
32 
34 #include "target_definitions.h"
35 
36 #include <dirent.h>
37 #ifdef USE_GLOG
38 #include <glog/logging.h>
39 #else
40 #include <aditof/log.h>
41 #endif
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <string>
46 #include <sys/stat.h>
47 #include <unistd.h>
48 
49 using namespace aditof;
50 
51 namespace local {
52 
54  std::string &dev_path,
55  std::string &subdev_path,
56  std::string &device_name) {
57  using namespace aditof;
58  using namespace std;
59 
60  char *buf;
61  int size = 0;
62 
63  /* Run media-ctl to get the video processing pipes */
64  char cmd[64];
65  sprintf(cmd, "media-ctl -d %s --print-dot", video.c_str());
66  FILE *fp = popen(cmd, "r");
67  if (!fp) {
68  LOG(WARNING) << "Error running media-ctl";
69  return Status::GENERIC_ERROR;
70  }
71 
72  /* Read the media-ctl output stream */
73  buf = (char *)malloc(128 * 1024);
74  while (!feof(fp)) {
75  fread(&buf[size], 1, 1, fp);
76  size++;
77  }
78  pclose(fp);
79  buf[size] = '\0';
80 
81  /* Search command media-ctl for device/subdevice name */
82  string str(buf);
83  free(buf);
84 
85  size_t pos = str.find("mxc_isi.0.capture");
86  if (pos != string::npos) {
87  dev_path = str.substr(pos + strlen("mxc_isi.0.capture") + 2,
88  strlen("/dev/mediaX"));
89  } else {
90  return Status::GENERIC_ERROR;
91  }
92 
93  if (str.find("adsd3500") != string::npos) {
94  device_name = "adsd3500";
95  pos = str.find("adsd3500");
96  subdev_path = str.substr(pos + strlen("adsd3500") + 9,
97  strlen("/dev/v4l-subdevX"));
98  } else {
99  return Status::GENERIC_ERROR;
100  }
101  return Status::OK;
102 }
103 
104 }; // namespace local
105 
107  Status status = Status::OK;
108 
109  LOG(INFO) << "Looking for sensors on the target";
110 
111  // Find all video device paths
112  std::vector<std::string> videoPaths;
113  const std::string videoDirPath("/dev/");
114  const std::string videoBaseName("media");
115  std::string deviceName;
116 
117  DIR *dirp = opendir(videoDirPath.c_str());
118  struct dirent *dp;
119  while ((dp = readdir(dirp))) {
120  if (!strncmp(dp->d_name, videoBaseName.c_str(),
121  videoBaseName.length())) {
122  std::string fullvideoPath = videoDirPath + std::string(dp->d_name);
123  videoPaths.emplace_back(fullvideoPath);
124  }
125  }
126  closedir(dirp);
127 
128  // Identify any eligible time of flight cameras
129  for (const auto &video : videoPaths) {
130  DLOG(INFO) << "Looking at: " << video << " for an eligible TOF camera";
131 
132  std::string devPath;
133  std::string subdevPath;
134 
135  status = local::findDevicePathsAtVideo(video, devPath, subdevPath,
136  deviceName);
137  if (status != Status::OK) {
138  LOG(WARNING) << "failed to find device paths at video: " << video;
139  return status;
140  }
141 
142  if (devPath.empty() || subdevPath.empty()) {
143  continue;
144  }
145 
146  DLOG(INFO) << "Considering: " << video << " an eligible TOF camera";
147 
148  SensorInfo sInfo;
149 
150  if (deviceName == "adsd3500") {
151  sInfo.sensorType = SensorType::SENSOR_ADSD3500;
152  }
153 
154  sInfo.driverPath = devPath;
155  sInfo.subDevPath = subdevPath;
157  m_sensorsInfo.emplace_back(sInfo);
158  }
159 
160  return status;
161 }
INFO
const int INFO
Definition: log_severity.h:59
TargetSensorEnumerator::SensorInfo::subDevPath
std::string subDevPath
Definition: target_sensor_enumerator.h:64
aditof::Status::GENERIC_ERROR
@ GENERIC_ERROR
An error occured but there are no details available.
TargetSensorEnumerator::SensorInfo::captureDev
std::string captureDev
Definition: target_sensor_enumerator.h:65
readdir
static struct dirent * readdir(DIR *dirp)
Definition: dirent.h:732
log.h
dirent::d_name
char d_name[PATH_MAX+1]
Definition: dirent.h:278
local
Definition: sensor_enumerator_imx.cpp:51
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
WARNING
const int WARNING
Definition: log_severity.h:59
DLOG
#define DLOG(x)
Definition: sdk/include/aditof/log.h:73
closedir
static int closedir(DIR *dirp)
Definition: dirent.h:843
target_definitions.h
dirent.h
CAPTURE_DEVICE_NAME
static const char * CAPTURE_DEVICE_NAME
Definition: imx/target_definitions.h:35
aditof
Namespace aditof.
Definition: adsd_errs.h:40
update_failure_list.str
str
Definition: update_failure_list.py:41
dirent
Definition: dirent.h:261
size
#define size
Definition: glcorearb.h:2944
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
TargetSensorEnumerator::SensorInfo::sensorType
SensorType sensorType
Definition: target_sensor_enumerator.h:62
TargetSensorEnumerator::SensorInfo::driverPath
std::string driverPath
Definition: target_sensor_enumerator.h:63
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72
aditof::Status::OK
@ OK
Success.
DIR
Definition: dirent.h:282
TargetSensorEnumerator::SensorInfo
Definition: target_sensor_enumerator.h:61
size
GLsizeiptr size
Definition: glcorearb.h:2943
std
local::findDevicePathsAtVideo
aditof::Status findDevicePathsAtVideo(const std::string &video, std::string &dev_path, std::string &subdev_path, std::string &device_name)
Definition: sensor_enumerator_imx.cpp:53
target_sensor_enumerator.h
opendir
static DIR * opendir(const char *dirname)
Definition: dirent.h:676
TargetSensorEnumerator::searchSensors
virtual aditof::Status searchSensors() override
Do a search for the available sensors.
Definition: sensor_enumerator_imx.cpp:106


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58