[{"id":1551,"web_url":"https://patchwork.libcamera.org/comment/1551/","msgid":"<20190502235245.GA11358@pendragon.ideasonboard.com>","date":"2019-05-02T23:52:45","subject":"Re: [libcamera-devel] [PATCH v2] libcamera: device_enumerator: add\n\tDeviceEnumeratorSysfs class","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"On Thu, May 02, 2019 at 07:42:48PM -0400, Paul Elder wrote:\n> A udev-based device enumerator is not sufficient, since libudev is an\n> optional dependency, or udev might fail. In these cases, we should fall\n> back to using sysfs to enumerate devices.\n> \n> Add a DeviceEnumeratorSysfs class which is a specialization of\n> DeviceEnumerator that uses sysfs to enumerate media devices on the\n> system.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> ---\n> Changes in v2:\n> - style changes\n> - change sysfs directory checking list to remove block and add subsystem\n> - add warning output to alert if media device (/dev/media*) should exist\n>   based on sysfs but doesn't\n> \n>  Documentation/Doxyfile.in                     |   4 +-\n>  src/libcamera/device_enumerator.cpp           |   6 +-\n>  src/libcamera/device_enumerator_sysfs.cpp     | 110 ++++++++++++++++++\n>  .../include/device_enumerator_sysfs.h         |  31 +++++\n>  src/libcamera/meson.build                     |   2 +\n>  5 files changed, 150 insertions(+), 3 deletions(-)\n>  create mode 100644 src/libcamera/device_enumerator_sysfs.cpp\n>  create mode 100644 src/libcamera/include/device_enumerator_sysfs.h\n> \n> diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\n> index 950ad4f..381f777 100644\n> --- a/Documentation/Doxyfile.in\n> +++ b/Documentation/Doxyfile.in\n> @@ -833,7 +833,9 @@ RECURSIVE              = YES\n>  # Note that relative paths are relative to the directory from which doxygen is\n>  # run.\n>  \n> -EXCLUDE                = ../src/libcamera/device_enumerator_udev.cpp \\\n> +EXCLUDE                = ../src/libcamera/device_enumerator_sysfs.cpp \\\n> +\t\t\t ../src/libcamera/device_enumerator_udev.cpp \\\n> +\t\t\t ../src/libcamera/include/device_enumerator_sysfs.h \\\n>  \t\t\t ../src/libcamera/include/device_enumerator_udev.h \\\n>  \t\t\t ../src/libcamera/pipeline/\n>  \n> diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\n> index f6878b3..259eec4 100644\n> --- a/src/libcamera/device_enumerator.cpp\n> +++ b/src/libcamera/device_enumerator.cpp\n> @@ -6,6 +6,7 @@\n>   */\n>  \n>  #include \"device_enumerator.h\"\n> +#include \"device_enumerator_sysfs.h\"\n>  #include \"device_enumerator_udev.h\"\n>  \n>  #include <string.h>\n> @@ -153,8 +154,9 @@ std::unique_ptr<DeviceEnumerator> DeviceEnumerator::create()\n>  \t * Either udev is not available or udev initialization failed. Fall back\n>  \t * on the sysfs enumerator.\n>  \t */\n> -\n> -\t/** \\todo Add a sysfs-based enumerator. */\n> +\tenumerator = utils::make_unique<DeviceEnumeratorSysfs>();\n> +\tif (!enumerator->init())\n> +\t\treturn enumerator;\n>  \n>  \treturn nullptr;\n>  }\n> diff --git a/src/libcamera/device_enumerator_sysfs.cpp b/src/libcamera/device_enumerator_sysfs.cpp\n> new file mode 100644\n> index 0000000..943cab2\n> --- /dev/null\n> +++ b/src/libcamera/device_enumerator_sysfs.cpp\n> @@ -0,0 +1,110 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * device_enumerator_sysfs.cpp - sysfs-based device enumerator\n> + */\n> +\n> +#include \"device_enumerator_sysfs.h\"\n> +\n> +#include <dirent.h>\n> +#include <fcntl.h>\n> +#include <fstream>\n> +#include <stdlib.h>\n> +#include <string.h>\n> +#include <sys/ioctl.h>\n> +#include <sys/stat.h>\n> +#include <sys/types.h>\n> +#include <unistd.h>\n> +\n> +#include \"log.h\"\n> +\n> +namespace libcamera {\n> +\n> +LOG_DECLARE_CATEGORY(DeviceEnumerator)\n> +\n> +DeviceEnumeratorSysfs::DeviceEnumeratorSysfs()\n> +{\n> +}\n> +\n> +DeviceEnumeratorSysfs::~DeviceEnumeratorSysfs()\n> +{\n> +}\n> +\n> +int DeviceEnumeratorSysfs::init()\n> +{\n> +\treturn 0;\n> +}\n> +\n> +int DeviceEnumeratorSysfs::enumerate()\n> +{\n> +\tstruct dirent *ent;\n> +\tDIR *dir;\n> +\n> +\tstatic const char *sysfs_dirs[] = {\n\nTo make both the array and its contents const, this should be\n\n\tstatic const char * const sysfs_dirs[] = {\n\n> +\t\t\"/sys/subsystem/media/devices\",\n> +\t\t\"/sys/bus/media/devices\",\n> +\t\t\"/sys/class/media/devices\",\n> +\t};\n> +\n> +\tfor (const char *dirname : sysfs_dirs) {\n> +\t\tdir = opendir(dirname);\n> +\t\tif (dir)\n> +\t\t\tbreak;\n> +\t}\n\nShould you have a\n\n\tif (!dir) {\n\t\tLOG(...) << ...;\n\t\treturn -ENODEV;\n\t}\n\n> +\n> +\twhile ((ent = readdir(dir)) != nullptr) {\n> +\t\tunsigned int idx;\n> +\t\tstruct stat devstat;\n> +\t\tchar *end;\n> +\t\tstd::string devnode;\n> +\n> +\t\tif (strncmp(ent->d_name, \"media\", 5))\n> +\t\t\tcontinue;\n> +\n> +\t\tidx = strtoul(ent->d_name + 5, &end, 10);\n> +\t\tif (*end != '\\0')\n> +\t\t\tcontinue;\n> +\n> +\t\tdevnode = \"/dev/media\" + std::to_string(idx);\n> +\n> +\t\t/* Verify that the device node exists. */\n> +\t\tif (stat(devnode.c_str(), &devstat) < 0) {\n> +\t\t\tLOG(DeviceEnumerator, Warning)\n> +\t\t\t\t<< \"Device node /dev/media\" << idx\n> +\t\t\t\t<< \" should exist but doesn't\";\n> +\t\t\tcontinue;\n> +\t\t}\n\nI know you'll need some time to get used to it, but in C++ variables\ndon't have to be declared at the beginning of the scope, so you can\nwrite\n\n\t\tif (strncmp(ent->d_name, \"media\", 5))\n\t\t\tcontinue;\n\n\t\tchar *end;\n\t\tunsigned int idx = strtoul(ent->d_name + 5, &end, 10);\n\t\tif (*end != '\\0')\n\t\t\tcontinue;\n\n\t\tstd::string devnode = \"/dev/media\" + std::to_string(idx);\n\n\t\t/* Verify that the device node exists. */\n\t\tstruct stat devstat;\n\t\tif (stat(devnode.c_str(), &devstat) < 0) {\n\t\t\tLOG(DeviceEnumerator, Warning)\n\t\t\t\t<< \"Device node /dev/media\" << idx\n\t\t\t\t<< \" should exist but doesn't\";\n\t\t\tcontinue;\n\t\t}\n\n> +\n> +\t\taddDevice(devnode);\n> +\t}\n> +\n> +\tclosedir(dir);\n> +\n> +\treturn 0;\n> +}\n> +\n> +std::string DeviceEnumeratorSysfs::lookupDeviceNode(int major, int minor)\n> +{\n> +\tstd::string deviceNode;\n> +\tstd::string line;\n> +\tstd::ifstream ueventFile;\n> +\n> +\tueventFile.open(\"/sys/dev/char/\" + std::to_string(major) + \":\" +\n> +\t\t\tstd::to_string(minor) + \"/uevent\");\n> +\tif (!ueventFile)\n> +\t\treturn std::string();\n> +\n> +\twhile (ueventFile >> line) {\n> +\t\tif (line.find(\"DEVNAME\") == 0) {\n\nShould this be DEVNAME= instead of DEVNAME ?\n\n> +\t\t\tdeviceNode = \"/dev/\" + line.substr(strlen(\"DEVNAME=\"));\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n> +\n> +\tueventFile.close();\n> +\n> +\treturn deviceNode;\n> +}\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/include/device_enumerator_sysfs.h b/src/libcamera/include/device_enumerator_sysfs.h\n> new file mode 100644\n> index 0000000..534b28c\n> --- /dev/null\n> +++ b/src/libcamera/include/device_enumerator_sysfs.h\n> @@ -0,0 +1,31 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * device_enumerator_sysfs.h - sysfs-based device enumerator\n> + */\n> +#ifndef __LIBCAMERA_DEVICE_ENUMERATOR_SYSFS_H__\n> +#define __LIBCAMERA_DEVICE_ENUMERATOR_SYSFS_H__\n> +\n> +#include <string>\n> +\n> +#include \"device_enumerator.h\"\n> +\n> +namespace libcamera {\n> +\n> +class DeviceEnumeratorSysfs final : public DeviceEnumerator\n> +{\n> +public:\n> +\tDeviceEnumeratorSysfs();\n> +\t~DeviceEnumeratorSysfs();\n> +\n> +\tint init();\n> +\tint enumerate();\n> +\n> +private:\n> +\tstd::string lookupDeviceNode(int major, int minor);\n> +};\n> +\n> +} /* namespace libcamera */\n> +\n> +#endif /* __LIBCAMERA_DEVICE_ENUMERATOR_SYSFS_H__ */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index 2b67823..8796f49 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -4,6 +4,7 @@ libcamera_sources = files([\n>      'camera_manager.cpp',\n>      'camera_sensor.cpp',\n>      'device_enumerator.cpp',\n> +    'device_enumerator_sysfs.cpp',\n>      'event_dispatcher.cpp',\n>      'event_dispatcher_poll.cpp',\n>      'event_notifier.cpp',\n> @@ -26,6 +27,7 @@ libcamera_sources = files([\n>  libcamera_headers = files([\n>      'include/camera_sensor.h',\n>      'include/device_enumerator.h',\n> +    'include/device_enumerator_sysfs.h',\n>      'include/device_enumerator_udev.h',\n>      'include/event_dispatcher_poll.h',\n>      'include/formats.h',","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CEECB60E61\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  3 May 2019 01:52:59 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 0B11F2D1;\n\tFri,  3 May 2019 01:52:58 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1556841179;\n\tbh=JnJz1NJOoWLBRmZSemqYX2nuVHiBq1Sf46OLCqVZ5yE=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=g3CEgg5tnM1CdQrL2vkuFClePvxQ72HuWO8INUZw2BG6ZNI/TupvXFvsqT4C+UUxZ\n\tKUrhoeEygtBJdO5RIlSBbL4G4Og/Yg8WKixi8mkuLaLUWSKPMRwngxPsvQZpdXtuAd\n\tQmnfsOZdcQU8RlBcMkH3ZmEs+ivCCrYjBruOUGnQ=","Date":"Fri, 3 May 2019 02:52:45 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190502235245.GA11358@pendragon.ideasonboard.com>","References":"<20190502234248.15722-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190502234248.15722-1-paul.elder@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v2] libcamera: device_enumerator: add\n\tDeviceEnumeratorSysfs class","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Thu, 02 May 2019 23:53:00 -0000"}}]