From patchwork Sat Dec 22 23:00:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 82 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 12ABD60B2C for ; Sun, 23 Dec 2018 00:02:26 +0100 (CET) X-Halon-ID: 95a1b78c-063d-11e9-9adf-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from wyvern.dyn.berto.se (unknown [217.31.177.236]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 95a1b78c-063d-11e9-9adf-005056917a89; Sun, 23 Dec 2018 00:01:59 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Sun, 23 Dec 2018 00:00:35 +0100 Message-Id: <20181222230041.29999-7-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181222230041.29999-1-niklas.soderlund@ragnatech.se> References: <20181222230041.29999-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 06/12] libcamera: deviceenumerator: add DeviceEnumeratorUdev class X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Dec 2018 23:02:26 -0000 Provide a DeviceEnumeratorUdev class which is a specialization of DeviceEnumerator which uses udev to enumerate information in the system. Signed-off-by: Niklas Söderlund --- src/libcamera/deviceenumerator.cpp | 100 +++++++++++++++++++++++ src/libcamera/include/deviceenumerator.h | 15 ++++ 2 files changed, 115 insertions(+) diff --git a/src/libcamera/deviceenumerator.cpp b/src/libcamera/deviceenumerator.cpp index b2f59017e94d14aa..f4c40bf0376ab453 100644 --- a/src/libcamera/deviceenumerator.cpp +++ b/src/libcamera/deviceenumerator.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include @@ -273,4 +274,103 @@ DeviceInfo *DeviceEnumerator::search(DeviceMatch &dm) const return info; } +/* ----------------------------------------------------------------------------- + * Enumerator Udev + */ + +DeviceEnumeratorUdev::DeviceEnumeratorUdev() + : udev_(NULL) +{ +} + +DeviceEnumeratorUdev::~DeviceEnumeratorUdev() +{ + if (udev_) + udev_unref(udev_); +} + +int DeviceEnumeratorUdev::init() +{ + if (udev_) + return -EBUSY; + + udev_ = udev_new(); + if (!udev_) + return -ENODEV; + + return 0; +} + +int DeviceEnumeratorUdev::enumerate() +{ + struct udev_enumerate *udev_enum = NULL; + struct udev_list_entry *ents, *ent; + int ret; + + udev_enum = udev_enumerate_new(udev_); + if (udev_enum == NULL) + return -ENOMEM; + + ret = udev_enumerate_add_match_subsystem(udev_enum, "media"); + if (ret < 0) + goto done; + + ret = udev_enumerate_scan_devices(udev_enum); + if (ret < 0) + goto done; + + ents = udev_enumerate_get_list_entry(udev_enum); + if (ents == NULL) + goto done; + + udev_list_entry_foreach(ent, ents) { + struct udev_device *dev; + const char *devnode; + const char *syspath = udev_list_entry_get_name(ent); + + dev = udev_device_new_from_syspath(udev_, syspath); + if (dev == NULL) { + LOG(Error) << "Failed to device for '" << syspath << "', skipping"; + continue; + } + + devnode = udev_device_get_devnode(dev); + if (devnode == NULL) { + udev_device_unref(dev); + ret = -ENODEV; + goto done; + } + + addDevice(devnode); + + udev_device_unref(dev); + } +done: + udev_enumerate_unref(udev_enum); + return ret >= 0 ? 0 : ret; +} + +int DeviceEnumeratorUdev::lookupDevnode(std::string &devnode, int major, int minor) +{ + struct udev_device *device; + const char *name; + dev_t devnum; + int ret = 0; + + devnum = makedev(major, minor); + device = udev_device_new_from_devnum(udev_, 'c', devnum); + if (!device) + return -ENODEV; + + name = udev_device_get_devnode(device); + if (name) + devnode = name; + else + ret = -ENODEV; + + udev_device_unref(device); + + return ret; +} + } /* namespace libcamera */ diff --git a/src/libcamera/include/deviceenumerator.h b/src/libcamera/include/deviceenumerator.h index cbe17774edb7fcc5..2c7ff3f336ba127d 100644 --- a/src/libcamera/include/deviceenumerator.h +++ b/src/libcamera/include/deviceenumerator.h @@ -78,6 +78,21 @@ private: virtual int lookupDevnode(std::string &devnode, int major, int minor) = 0; }; +class DeviceEnumeratorUdev: public DeviceEnumerator +{ +public: + DeviceEnumeratorUdev(); + ~DeviceEnumeratorUdev(); + + int init() final; + int enumerate() final; + +private: + struct udev *udev_; + + int lookupDevnode(std::string &devnode, int major, int minor) final; +}; + } /* namespace libcamera */ #endif /* __LIBCAMERA_DEVICEENUMERATE_H__ */