PeterBee97 commented on code in PR #18745:
URL: https://github.com/apache/nuttx/pull/18745#discussion_r3206138456
##########
arch/sim/src/sim/posix/sim_host_v4l2.c:
##########
@@ -84,27 +86,246 @@ static int host_video_ioctl(int fd, int request, void *arg)
return r;
}
+static int host_video_parse_device_index(const char *name, int *index)
+{
+ long value;
+ char *endptr;
+
+ if (strncmp(name, "video", 5) != 0)
+ {
+ return -EINVAL;
+ }
+
+ name += 5;
+ if (*name == '\0')
+ {
+ return -EINVAL;
+ }
+
+ value = strtol(name, &endptr, 10);
+ if (*endptr != '\0' || value < 0 || value > INT_MAX)
+ {
+ return -EINVAL;
+ }
+
+ *index = value;
+ return 0;
+}
+
+static int host_video_get_next_device_path(int current_index,
+ char *devpath,
+ size_t devpathlen,
+ int *next_index)
+{
+ DIR *dir;
+ struct dirent *entry;
+ int candidate;
+ int found = INT_MAX;
+
+ dir = opendir("/dev");
+ if (dir == NULL)
+ {
+ return -errno;
+ }
+
+ while ((entry = readdir(dir)) != NULL)
+ {
+ if (host_video_parse_device_index(entry->d_name, &candidate) < 0 ||
+ candidate <= current_index || candidate >= found)
+ {
+ continue;
+ }
+
+ found = candidate;
+ }
+
+ closedir(dir);
+
+ if (found == INT_MAX)
+ {
+ return -ENODEV;
+ }
+
+ if (snprintf(devpath, devpathlen, "/dev/video%d", found) >=
+ (int)devpathlen)
+ {
+ return -ENAMETOOLONG;
+ }
+
+ *next_index = found;
+ return 0;
+}
+
+static bool host_video_is_capture_device(const char *host_video_dev_path)
+{
+ struct v4l2_capability cap;
+ int fd;
+ bool available = false;
+
+ fd = open(host_video_dev_path, O_RDWR | O_NONBLOCK);
+ if (fd < 0)
+ {
+ fd = open(host_video_dev_path, O_RDONLY | O_NONBLOCK);
+ if (fd < 0)
+ {
+ return false;
+ }
+ }
+
+ memset(&cap, 0, sizeof(cap));
+ if (host_video_ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0)
+ {
+ uint32_t capabilities = cap.device_caps != 0 ? cap.device_caps :
+ cap.capabilities;
+
+ if ((capabilities & V4L2_CAP_VIDEO_CAPTURE) != 0)
+ {
+ available = true;
+ }
+ }
+
+ close(fd);
+ return available;
+}
+
+static int host_video_get_device_path_by_index(int index,
+ char *devpath,
+ size_t devpathlen)
+{
+ int count = 0;
+ int current_index = -1;
+ int ret;
+ char path[PATH_MAX];
+
+ while ((ret = host_video_get_next_device_path(current_index, path,
+ sizeof(path),
+ ¤t_index)) == 0)
+ {
+ if (!host_video_is_capture_device(path))
+ {
+ continue;
+ }
+
+ if (count == index)
+ {
+ if (snprintf(devpath, devpathlen, "%s", path) >=
+ (int)devpathlen)
+ {
+ return -ENAMETOOLONG;
+ }
+
+ return 0;
+ }
+
+ count++;
+ }
+
+ return ret;
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]