{"id":101,"url":"https://patchwork.libcamera.org/api/1.1/patches/101/?format=json","web_url":"https://patchwork.libcamera.org/patch/101/","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":"<20181229032855.26249-6-niklas.soderlund@ragnatech.se>","date":"2018-12-29T03:28:48","name":"[libcamera-devel,v2,05/12] libcamera: device_enumerator: add DeviceEnumerator class","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"16b0abcf644e3e07c07f6062d73480ce76fce198","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/1.1/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/101/mbox/","series":[{"id":41,"url":"https://patchwork.libcamera.org/api/1.1/series/41/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=41","date":"2018-12-29T03:28:43","name":"Add basic camera enumeration","version":2,"mbox":"https://patchwork.libcamera.org/series/41/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/101/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/101/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net\n\t[195.74.38.229])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9371160B36\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 29 Dec 2018 04:29:55 +0100 (CET)","from bismarck.berto.se (unknown [89.233.230.99])\n\tby bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA\n\tid fadaa1e5-0b19-11e9-911a-0050569116f7;\n\tSat, 29 Dec 2018 04:29:42 +0100 (CET)"],"X-Halon-ID":"fadaa1e5-0b19-11e9-911a-0050569116f7","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 29 Dec 2018 04:28:48 +0100","Message-Id":"<20181229032855.26249-6-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20181229032855.26249-1-niklas.soderlund@ragnatech.se>","References":"<20181229032855.26249-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 05/12] libcamera: device_enumerator:\n\tadd DeviceEnumerator 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":"Sat, 29 Dec 2018 03:29:56 -0000"},"content":"Provide a DeviceEnumerator base class which enumerates all media devices\nin the system and information about them, resolving Media Controller\ndata structures to paths and a method to search in all the enumerated\ninformation.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n* Changes since v1\n- Add strerror() logging if open() or ioctl() fails.\n- Use reinterpret_cast.\n- s/NULL/nullptr.\n- Break some lines to better fit 80 character lines.\n---\n src/libcamera/device_enumerator.cpp       | 155 ++++++++++++++++++++++\n src/libcamera/include/device_enumerator.h |  22 +++\n 2 files changed, 177 insertions(+)","diff":"diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\nindex 61b32bb921581f49..df9e89a1afeecda1 100644\n--- a/src/libcamera/device_enumerator.cpp\n+++ b/src/libcamera/device_enumerator.cpp\n@@ -5,6 +5,12 @@\n  * device_enumerator.cpp - Enumeration and matching\n  */\n \n+#include <fcntl.h>\n+#include <libudev.h>\n+#include <string.h>\n+#include <sys/ioctl.h>\n+#include <unistd.h>\n+\n #include \"device_enumerator.h\"\n #include \"log.h\"\n \n@@ -111,4 +117,153 @@ bool DeviceMatch::match(const DeviceInfo *info) const\n \treturn true;\n }\n \n+/* -----------------------------------------------------------------------------\n+ * Enumerator Base\n+ */\n+\n+DeviceEnumerator::~DeviceEnumerator()\n+{\n+\tfor (DeviceInfo *dev : devices_) {\n+\t\tif (dev->busy())\n+\t\t\tLOG(Error) << \"Removing device info while still in use\";\n+\n+\t\tdelete dev;\n+\t}\n+}\n+\n+int DeviceEnumerator::addDevice(const std::string &devnode)\n+{\n+\tint fd, ret;\n+\n+\tstruct media_device_info info = {};\n+\tstd::map<std::string, std::string> entities;\n+\n+\tfd = open(devnode.c_str(), O_RDWR);\n+\tif (fd < 0) {\n+\t\tret = -errno;\n+\t\tLOG(Info) << \"Unable to open \" << devnode <<\n+\t\t\t  \" (\" << strerror(-ret) << \"), skipping\";\n+\t\treturn ret;\n+\t}\n+\n+\tret = readInfo(fd, info);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = readTopology(fd, entities);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tdevices_.push_back(new DeviceInfo(devnode, info, entities));\n+out:\n+\tclose(fd);\n+\n+\treturn ret;\n+}\n+\n+int DeviceEnumerator::readInfo(int fd, struct media_device_info &info)\n+{\n+\tint ret;\n+\n+\tret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &info);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(Info) << \"Unable to read device info \" <<\n+\t\t\t  \" (\" << strerror(-ret) << \"), skipping\";\n+\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+int DeviceEnumerator::readTopology(int fd, std::map<std::string, std::string> &entities)\n+{\n+\tstruct media_v2_topology topology;\n+\tstruct media_v2_entity *ents = nullptr;\n+\tstruct media_v2_interface *ifaces = nullptr;\n+\tstruct media_v2_link *links = nullptr;\n+\tint ret;\n+\n+\twhile (true) {\n+\t\ttopology = {};\n+\n+\t\tret = ioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology);\n+\t\tif (ret < 0)\n+\t\t\treturn -errno;\n+\n+\t\t__u64 version = topology.topology_version;\n+\n+\t\tents = new media_v2_entity[topology.num_entities]();\n+\t\tifaces = new media_v2_interface[topology.num_interfaces]();\n+\t\tlinks = new media_v2_link[topology.num_links]();\n+\t\ttopology.ptr_entities = reinterpret_cast<__u64>(ents);\n+\t\ttopology.ptr_interfaces = reinterpret_cast<__u64>(ifaces);\n+\t\ttopology.ptr_links = reinterpret_cast<__u64>(links);\n+\n+\t\tret = ioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology);\n+\t\tif (ret < 0) {\n+\t\t\tret = -errno;\n+\t\t\tgoto done;\n+\t\t}\n+\n+\t\tif (version == topology.topology_version)\n+\t\t\tbreak;\n+\n+\t\tdelete[] links;\n+\t\tdelete[] ifaces;\n+\t\tdelete[] ents;\n+\t}\n+\n+\tfor (unsigned int link_id = 0; link_id < topology.num_links; link_id++) {\n+\t\tunsigned int iface_id, ent_id;\n+\t\tstd::string devnode;\n+\n+\t\tif ((links[link_id].flags & MEDIA_LNK_FL_LINK_TYPE) !=\n+\t\t    MEDIA_LNK_FL_INTERFACE_LINK)\n+\t\t\tcontinue;\n+\n+\t\tfor (iface_id = 0; iface_id < topology.num_interfaces; iface_id++)\n+\t\t\tif (links[link_id].source_id == ifaces[iface_id].id)\n+\t\t\t\tbreak;\n+\n+\t\tfor (ent_id = 0; ent_id < topology.num_entities; ent_id++)\n+\t\t\tif (links[link_id].sink_id == ents[ent_id].id)\n+\t\t\t\tbreak;\n+\n+\t\tif (ent_id >= topology.num_entities ||\n+\t\t    iface_id >= topology.num_interfaces)\n+\t\t\tcontinue;\n+\n+\t\tdevnode = lookupDevnode(ifaces[iface_id].devnode.major,\n+\t\t\t\t\tifaces[iface_id].devnode.minor);\n+\t\tif (devnode == \"\")\n+\t\t\tbreak;\n+\n+\t\tentities[ents[ent_id].name] = devnode;\n+\t}\n+done:\n+\tdelete[] links;\n+\tdelete[] ifaces;\n+\tdelete[] ents;\n+\n+\treturn ret;\n+}\n+\n+DeviceInfo *DeviceEnumerator::search(DeviceMatch &dm) const\n+{\n+\tDeviceInfo *info = nullptr;\n+\n+\tfor (DeviceInfo *dev : devices_) {\n+\t\tif (dev->busy())\n+\t\t\tcontinue;\n+\n+\t\tif (dm.match(dev)) {\n+\t\t\tinfo = dev;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\treturn info;\n+}\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/include/device_enumerator.h b/src/libcamera/include/device_enumerator.h\nindex ed1e986ff45f95f5..1f8cef3311012308 100644\n--- a/src/libcamera/include/device_enumerator.h\n+++ b/src/libcamera/include/device_enumerator.h\n@@ -53,6 +53,28 @@ private:\n \tstd::vector<std::string> entities_;\n };\n \n+class DeviceEnumerator\n+{\n+public:\n+\tvirtual ~DeviceEnumerator();\n+\n+\tvirtual int init() = 0;\n+\tvirtual int enumerate() = 0;\n+\n+\tDeviceInfo *search(DeviceMatch &dm) const;\n+\n+protected:\n+\tint addDevice(const std::string &devnode);\n+\n+private:\n+\tstd::vector<DeviceInfo *> devices_;\n+\n+\tint readInfo(int fd, struct media_device_info &info);\n+\tint readTopology(int fd, std::map<std::string, std::string> &entities);\n+\n+\tvirtual std::string lookupDevnode(int major, int minor) = 0;\n+};\n+\n } /* namespace libcamera */\n \n #endif\t/* __LIBCAMERA_DEVICE_ENUMERATOR_H__ */\n","prefixes":["libcamera-devel","v2","05/12"]}