{"id":18329,"url":"https://patchwork.libcamera.org/api/1.1/patches/18329/?format=json","web_url":"https://patchwork.libcamera.org/patch/18329/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20230302005415.1789544-2-dev@flowerpot.me>","date":"2023-03-02T00:54:15","name":"[libcamera-devel,1/1] libcamera: enumeration: Generalize DeviceMatch","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"59a5d88a0636daab5d4d82325a0aad50b8a773b4","submitter":{"id":154,"url":"https://patchwork.libcamera.org/api/1.1/people/154/?format=json","name":"Sophie Friedrich","email":"dev@flowerpot.me"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18329/mbox/","series":[{"id":3782,"url":"https://patchwork.libcamera.org/api/1.1/series/3782/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3782","date":"2023-03-02T00:54:14","name":"Generalized DeviceMatch","version":1,"mbox":"https://patchwork.libcamera.org/series/3782/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18329/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18329/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id A9E03C3261\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  2 Mar 2023 09:29:57 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9BEE2626C8;\n\tThu,  2 Mar 2023 10:29:56 +0100 (CET)","from mout-p-101.mailbox.org (mout-p-101.mailbox.org\n\t[80.241.56.151])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8A0606266A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  2 Mar 2023 01:54:30 +0100 (CET)","from smtp1.mailbox.org (smtp1.mailbox.org\n\t[IPv6:2001:67c:2050:b231:465::1])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mout-p-101.mailbox.org (Postfix) with ESMTPS id 4PRt0h1Tdwz9sdv;\n\tThu,  2 Mar 2023 01:54:28 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1677749396;\n\tbh=9U6iY4+Q2s1En2SBDBaj6yQqUpGudxyqScncLVANyKo=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=Jnml9E2h3EAPyUoRvU7nUQWSkpLAOSL9UUaIl9H6jko4ySKTAA3CEzqMnfQ7/K5Kd\n\tOcx1rA9jT1IcOeLsCZWR8/JVQ2anJjDeqsfoCvhIPpHr0/BnKLXQLkH55sHe0SSADX\n\tiIahjbq6Jrq8dwgjKkH0afrWzB5wD7o8LKU84kMvW66IBrxerku9olrXDu2sbe3ZVR\n\tHgMX9XFbphgaFWqNf5O4RIXxwmP9niK55uYnVc2nX5tLKXLLQP/owkIB8p7If1xYn0\n\ttir5TVHNvXOdhe8IUJM8WWQRMzUMLtt2SkoXAV2xcSQkLHeGZHyqutU5CYd7G0ghye\n\tIEfpZcg6X0PBg==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=flowerpot.me;\n\ts=MBO0001; t=1677718468;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=a1iL2p36+0aqfwJleMxVyqBAc3Z4KQrI57d9DUmusgI=;\n\tb=F6Y96M+3GiP0AseEaALqM/ys2pYXlnX7fkoNyNeivdw6ttIHzx2PWqlrEUf/EJh0/PljaL\n\ttRt2bEGim/fObCGuJLmSi63NAiTVIodTinys0Yd477MvilEfVUvE0mOhAx7f8+5DD8XOUc\n\tFO4pCVNorEt4O9cAiPUjAJwhNQ/w+ZlfwgilgdxtNOa+kxSxLFmv8atvYiSnnzzXKWxmfR\n\tVquaZVRLHuqkLCiZxnT15wDHGtBIB8sfbsz7Ni3xDaMDt/4himMHUJEKunwJ3Iiqt4hLYY\n\ttTvvJaC6zfRHoHayqBsg8ADjEFJzKJ1f1RPo41O5GXK/iZ8G3zQ3Dxm+CeBOdA=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=flowerpot.me\n\theader.i=@flowerpot.me header.b=\"F6Y96M+3\"; \n\tdkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  2 Mar 2023 01:54:15 +0100","Message-Id":"<20230302005415.1789544-2-dev@flowerpot.me>","In-Reply-To":"<20230302005415.1789544-1-dev@flowerpot.me>","References":"<20230302005415.1789544-1-dev@flowerpot.me>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-Rspamd-Queue-Id":"4PRt0h1Tdwz9sdv","X-Mailman-Approved-At":"Thu, 02 Mar 2023 10:29:54 +0100","Subject":"[libcamera-devel] [PATCH 1/1] libcamera: enumeration: Generalize\n\tDeviceMatch","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","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>","From":"Sophie Friedrich via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Sophie Friedrich <dev@flowerpot.me>","Cc":"Sophie Friedrich <dev@flowerpot.me>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Currently `DeviceMatch` is very specialized to handle `MediaDevice`s, which\nhas strong dependencies to V4L2 internals.\n\nThis patch enables `DeviceMatch` to match using key/value pairs instead\nof a simple list of strings and introduces a `DeviceMatchEntityInterface` to\nabstract away the likes of `MediaEntity` in order to search for a\ndevice.\n\nSigned-off-by: Sophie Friedrich <dev@flowerpot.me>\n---\n .../libcamera/internal/device_enumerator.h    |  16 +--\n include/libcamera/internal/device_match.h     |  46 +++++++\n include/libcamera/internal/media_device.h     |   1 +\n include/libcamera/internal/media_object.h     |   7 +-\n include/libcamera/internal/meson.build        |   1 +\n src/libcamera/device_enumerator.cpp           |  73 ----------\n src/libcamera/device_match.cpp                | 126 ++++++++++++++++++\n src/libcamera/meson.build                     |   1 +\n 8 files changed, 183 insertions(+), 88 deletions(-)\n create mode 100644 include/libcamera/internal/device_match.h\n create mode 100644 src/libcamera/device_match.cpp","diff":"diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h\nindex 72ec9a60..259a9e46 100644\n--- a/include/libcamera/internal/device_enumerator.h\n+++ b/include/libcamera/internal/device_enumerator.h\n@@ -13,24 +13,12 @@\n \n #include <libcamera/base/signal.h>\n \n+#include \"libcamera/internal/device_match.h\"\n+\n namespace libcamera {\n \n class MediaDevice;\n \n-class DeviceMatch\n-{\n-public:\n-\tDeviceMatch(const std::string &driver);\n-\n-\tvoid add(const std::string &entity);\n-\n-\tbool match(const MediaDevice *device) const;\n-\n-private:\n-\tstd::string driver_;\n-\tstd::vector<std::string> entities_;\n-};\n-\n class DeviceEnumerator\n {\n public:\ndiff --git a/include/libcamera/internal/device_match.h b/include/libcamera/internal/device_match.h\nnew file mode 100644\nindex 00000000..e4563d3c\n--- /dev/null\n+++ b/include/libcamera/internal/device_match.h\n@@ -0,0 +1,46 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2023, Sophie Friedrich\n+ *\n+ * device_match.h - API to match devices\n+ */\n+\n+#pragma once\n+\n+#include <string>\n+#include <vector>\n+\n+#include <libcamera/base/class.h>\n+\n+\n+namespace libcamera {\n+\n+class MediaDevice;\n+\n+class DeviceMatchEntityInterface\n+{\n+public:\n+\tvirtual const std::string &match_key() const = 0;\n+\tvirtual const std::string &match_value() const = 0;\n+\tvirtual ~DeviceMatchEntityInterface() = default;\n+\n+\tstatic const std::string MATCH_ALL_KEY;\n+};\n+\n+class DeviceMatch\n+{\n+public:\n+\tDeviceMatch(const std::string &driver);\n+\n+\tvoid add(const std::string &entity);\n+\tvoid add(const std::string &key, const std::string &value);\n+\n+\tbool match(const MediaDevice *device) const;\n+\n+private:\n+\tstd::string driver_;\n+\tstd::vector<std::pair<std::string, std::string>> map_;\n+};\n+\n+\n+}\n\\ No newline at end of file\ndiff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h\nindex eb8cfde4..b10bb2eb 100644\n--- a/include/libcamera/internal/media_device.h\n+++ b/include/libcamera/internal/media_device.h\n@@ -19,6 +19,7 @@\n #include <libcamera/base/unique_fd.h>\n \n #include \"libcamera/internal/media_object.h\"\n+#include \"libcamera/internal/device_match.h\"\n \n namespace libcamera {\n \ndiff --git a/include/libcamera/internal/media_object.h b/include/libcamera/internal/media_object.h\nindex b1572968..d07ed698 100644\n--- a/include/libcamera/internal/media_object.h\n+++ b/include/libcamera/internal/media_object.h\n@@ -14,6 +14,8 @@\n \n #include <libcamera/base/class.h>\n \n+#include \"libcamera/internal/device_match.h\"\n+\n namespace libcamera {\n \n class MediaDevice;\n@@ -85,7 +87,7 @@ private:\n \tstd::vector<MediaLink *> links_;\n };\n \n-class MediaEntity : public MediaObject\n+class MediaEntity : public MediaObject, public DeviceMatchEntityInterface\n {\n public:\n \tenum class Type {\n@@ -111,6 +113,9 @@ public:\n \n \tint setDeviceNode(const std::string &deviceNode);\n \n+\tconst std::string &match_key() const{ return DeviceMatchEntityInterface::MATCH_ALL_KEY; }\n+\tconst std::string &match_value() const { return name_; }\n+\n private:\n \tLIBCAMERA_DISABLE_COPY_AND_MOVE(MediaEntity)\n \ndiff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\nindex d7508805..1e3c15ed 100644\n--- a/include/libcamera/internal/meson.build\n+++ b/include/libcamera/internal/meson.build\n@@ -21,6 +21,7 @@ libcamera_internal_headers = files([\n     'control_validator.h',\n     'converter.h',\n     'delayed_controls.h',\n+    'device_match.h',\n     'device_enumerator.h',\n     'device_enumerator_sysfs.h',\n     'device_enumerator_udev.h',\ndiff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\nindex f2e055de..49afd783 100644\n--- a/src/libcamera/device_enumerator.cpp\n+++ b/src/libcamera/device_enumerator.cpp\n@@ -40,79 +40,6 @@ namespace libcamera {\n \n LOG_DEFINE_CATEGORY(DeviceEnumerator)\n \n-/**\n- * \\class DeviceMatch\n- * \\brief Description of a media device search pattern\n- *\n- * The DeviceMatch class describes a media device using properties from the\n- * Media Controller struct media_device_info, entity names in the media graph\n- * or other properties that can be used to identify a media device.\n- *\n- * The description is meant to be filled by pipeline managers and passed to a\n- * device enumerator to find matching media devices.\n- *\n- * A DeviceMatch is created with a specific Linux device driver in mind,\n- * therefore the name of the driver is a required property. One or more Entity\n- * names can be added as match criteria.\n- *\n- * Pipeline handlers are recommended to add entities to DeviceMatch as\n- * appropriare to ensure that the media device they need can be uniquely\n- * identified. This is useful when the corresponding kernel driver can produce\n- * different graphs, for instance as a result of different driver versions or\n- * hardware configurations, and not all those graphs are suitable for a pipeline\n- * handler.\n- */\n-\n-/**\n- * \\brief Construct a media device search pattern\n- * \\param[in] driver The Linux device driver name that created the media device\n- */\n-DeviceMatch::DeviceMatch(const std::string &driver)\n-\t: driver_(driver)\n-{\n-}\n-\n-/**\n- * \\brief Add a media entity name to the search pattern\n- * \\param[in] entity The name of the entity in the media graph\n- */\n-void DeviceMatch::add(const std::string &entity)\n-{\n-\tentities_.push_back(entity);\n-}\n-\n-/**\n- * \\brief Compare a search pattern with a media device\n- * \\param[in] device The media device\n- *\n- * Matching is performed on the Linux device driver name and entity names from\n- * the media graph. A match is found if both the driver name matches and the\n- * media device contains all the entities listed in the search pattern.\n- *\n- * \\return true if the media device matches the search pattern, false otherwise\n- */\n-bool DeviceMatch::match(const MediaDevice *device) const\n-{\n-\tif (driver_ != device->driver())\n-\t\treturn false;\n-\n-\tfor (const std::string &name : entities_) {\n-\t\tbool found = false;\n-\n-\t\tfor (const MediaEntity *entity : device->entities()) {\n-\t\t\tif (name == entity->name()) {\n-\t\t\t\tfound = true;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t}\n-\n-\t\tif (!found)\n-\t\t\treturn false;\n-\t}\n-\n-\treturn true;\n-}\n-\n /**\n  * \\class DeviceEnumerator\n  * \\brief Enumerate, store and search media devices\ndiff --git a/src/libcamera/device_match.cpp b/src/libcamera/device_match.cpp\nnew file mode 100644\nindex 00000000..0c205f93\n--- /dev/null\n+++ b/src/libcamera/device_match.cpp\n@@ -0,0 +1,126 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2023, Sophie Friedrich\n+ *\n+ * device_match.cpp - API to match devices\n+ */\n+\n+#include \"libcamera/internal/device_match.h\"\n+\n+#include <string.h>\n+\n+#include <libcamera/base/log.h>\n+\n+#include \"libcamera/internal/media_device.h\"\n+\n+/**\n+ * \\file device_match.h\n+ * \\brief Matching of media devices\n+ *\n+ * Pipelines find compatible devices by matching against known strings or\n+ * key/value pairs during device enumeration. MediaDevice are responsible for\n+ * finding these entities during their population phase.\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(DeviceMatch)\n+\n+/**\n+ * \\interface DeviceMatchInterface\n+ * \\brief Interface which enables matching using DeviceMatch\n+ *\n+ *\n+ *\n+ */\n+const std::string DeviceMatchEntityInterface::MATCH_ALL_KEY = \"*\";\n+\n+/**\n+ * \\class DeviceMatch\n+ * \\brief Description of a media device search pattern\n+ *\n+ * The DeviceMatch class describes a media device using properties from the\n+ * Media Controller struct media_device_info, entity names in the media graph\n+ * or other properties that can be used to identify a media device.\n+ *\n+ * The description is meant to be filled by pipeline managers and passed to a\n+ * device enumerator to find matching media devices.\n+ *\n+ * A DeviceMatch is created with a specific Linux device driver in mind,\n+ * therefore the name of the driver is a required property. One or more Entity\n+ * names can be added as match criteria.\n+ *\n+ * Pipeline handlers are recommended to add entities to DeviceMatch as\n+ * appropriare to ensure that the media device they need can be uniquely\n+ * identified. This is useful when the corresponding kernel driver can produce\n+ * different graphs, for instance as a result of different driver versions or\n+ * hardware configurations, and not all those graphs are suitable for a pipeline\n+ * handler.\n+ */\n+\n+/**\n+ * \\brief Construct a media device search pattern\n+ * \\param[in] driver The Linux device driver name that created the media device\n+ */\n+DeviceMatch::DeviceMatch(const std::string &driver)\n+\t: driver_(driver)\n+{\n+}\n+\n+/**\n+ * \\brief Add a media entity name to the search pattern\n+ * \\param[in] entity The name of the entity in the media graph\n+ */\n+void DeviceMatch::add(const std::string &entity)\n+{\n+\tadd(DeviceMatchEntityInterface::MATCH_ALL_KEY, entity);\n+}\n+\n+/**\n+ * \\brief Add key / value pair to search pattern\n+ * \\param[in] key The key the value is associated to\n+ * \\param[in] value The value that should be searched for\n+ *\n+ * There is no uniqueness enforced to key/value. Mentioning a certain\n+ * key multiple times (in best case with different values) allows matching\n+ * multiple properties on the same key.\n+ */\n+void DeviceMatch::add(const std::string &key, const std::string &value)\n+{\n+\tmap_.push_back(std::pair(key, value));\n+}\n+\n+/**\n+ * \\brief Compare a search pattern with a media device\n+ * \\param[in] device The media device\n+ *\n+ * Matching is performed on the Linux device driver name and entity names from\n+ * the media graph. A match is found if both the driver name matches and the\n+ * media device contains all the entities listed in the search pattern.\n+ *\n+ * \\return true if the media device matches the search pattern, false otherwise\n+ */\n+bool DeviceMatch::match(const MediaDevice *device) const\n+{\n+\tif (driver_ != device->driver())\n+\t\treturn false;\n+\n+\tfor (const std::pair<std::string, std::string> &item : map_) {\n+\t\tbool found = false;\n+\n+\t\tfor (const DeviceMatchEntityInterface *entity : device->entities()) {\n+\t\t\tif (item.first == entity->match_key() &&\n+\t\t\t\titem.second == entity->match_value()) {\n+\t\t\t\tfound = true;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (!found)\n+\t\t\treturn false;\n+\t}\n+\n+\treturn true;\n+}\n+\n+}\n\\ No newline at end of file\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 9869bfe7..445eb8e9 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -15,6 +15,7 @@ libcamera_sources = files([\n     'control_validator.cpp',\n     'converter.cpp',\n     'delayed_controls.cpp',\n+    'device_match.cpp',\n     'device_enumerator.cpp',\n     'device_enumerator_sysfs.cpp',\n     'fence.cpp',\n","prefixes":["libcamera-devel","1/1"]}