{"id":365,"url":"https://patchwork.libcamera.org/api/1.1/patches/365/?format=json","web_url":"https://patchwork.libcamera.org/patch/365/","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":"<20190124101651.9993-11-laurent.pinchart@ideasonboard.com>","date":"2019-01-24T10:16:51","name":"[libcamera-devel,10/10] libcamera: device_enumerator: Add hotplug support","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"b24ada56ed84f377628f9a5f9e9e85b4eb9cb371","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/365/mbox/","series":[{"id":125,"url":"https://patchwork.libcamera.org/api/1.1/series/125/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=125","date":"2019-01-24T10:16:41","name":"Hotplug support and object lifetime management","version":1,"mbox":"https://patchwork.libcamera.org/series/125/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/365/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/365/checks/","tags":{},"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 8B13660C83\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 24 Jan 2019 11:17:00 +0100 (CET)","from pendragon.bb.dnainternet.fi\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 15AEB2F6;\n\tThu, 24 Jan 2019 11:17:00 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1548325020;\n\tbh=tEpCdsWCcinodfYHjd5vdST7yxot6qVoCMBXfxD8qqc=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=KQffT/pA0Dtd95bp4su86TtQBb4SVMq1KR178lT5h5hLbueS1F6aKUKTApvKU6bKD\n\tXe98G0snownxu1CgDBombCdf6foU1tbpUvjE0obN7r+hPJ/TmiUaJE6XQ0gp2FtSU/\n\tmwFSewpgCFCCh2xvY3JxFPDJWS0E4kSUD8ZgMGH0=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 24 Jan 2019 12:16:51 +0200","Message-Id":"<20190124101651.9993-11-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.19.2","In-Reply-To":"<20190124101651.9993-1-laurent.pinchart@ideasonboard.com>","References":"<20190124101651.9993-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 10/10] libcamera: device_enumerator: Add\n\thotplug support","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, 24 Jan 2019 10:17:01 -0000"},"content":"Create a udev_monitor in the udev device enumerator to listen to media\ndevice disconnection, and emit the corresponding media device's\ndisconnect signal in response.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/device_enumerator.cpp       | 83 ++++++++++++++++++++++-\n src/libcamera/include/device_enumerator.h |  6 ++\n 2 files changed, 88 insertions(+), 1 deletion(-)","diff":"diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\nindex 149ffbf9aea6..703b03dd418a 100644\n--- a/src/libcamera/device_enumerator.cpp\n+++ b/src/libcamera/device_enumerator.cpp\n@@ -11,6 +11,8 @@\n #include <sys/ioctl.h>\n #include <unistd.h>\n \n+#include <libcamera/event_notifier.h>\n+\n #include \"device_enumerator.h\"\n #include \"log.h\"\n #include \"media_device.h\"\n@@ -243,11 +245,47 @@ int DeviceEnumerator::addDevice(const std::string &deviceNode)\n \n \tmedia->close();\n \n+\tLOG(DeviceEnumerator, Debug)\n+\t\t<< \"Added device \" << deviceNode << \": \" << media->driver();\n+\n \tdevices_.push_back(std::move(media));\n \n \treturn 0;\n }\n \n+/**\n+ * \\brief Remove a media device from the enumerator\n+ * \\param[in] deviceNode Path to the media device to remove\n+ *\n+ * Remove the media device identified by \\a deviceNode previously added to the\n+ * enumerator with addDevice(). The media device's MediaDevice::disconnected\n+ * signal is emitted.\n+ */\n+void DeviceEnumerator::removeDevice(const std::string &deviceNode)\n+{\n+\tstd::shared_ptr<MediaDevice> media;\n+\n+\tfor (auto iter = devices_.begin(); iter != devices_.end(); ++iter) {\n+\t\tif ((*iter)->deviceNode() == deviceNode) {\n+\t\t\tmedia = std::move(*iter);\n+\t\t\tdevices_.erase(iter);\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!media) {\n+\t\tLOG(DeviceEnumerator, Warning)\n+\t\t\t<< \"Media device for node \" << deviceNode\n+\t\t\t<< \" not found\";\n+\t\treturn;\n+\t}\n+\n+\tLOG(DeviceEnumerator, Debug)\n+\t\t<< \"Media device for node \" << deviceNode << \" removed.\";\n+\n+\tmedia->disconnected.emit(media.get());\n+}\n+\n /**\n  * \\brief Search available media devices for a pattern match\n  * \\param[in] dm Search pattern\n@@ -301,12 +339,18 @@ DeviceEnumeratorUdev::DeviceEnumeratorUdev()\n \n DeviceEnumeratorUdev::~DeviceEnumeratorUdev()\n {\n+\tdelete notifier_;\n+\n+\tif (monitor_)\n+\t\tudev_monitor_unref(monitor_);\n \tif (udev_)\n \t\tudev_unref(udev_);\n }\n \n int DeviceEnumeratorUdev::init()\n {\n+\tint ret;\n+\n \tif (udev_)\n \t\treturn -EBUSY;\n \n@@ -314,6 +358,15 @@ int DeviceEnumeratorUdev::init()\n \tif (!udev_)\n \t\treturn -ENODEV;\n \n+\tmonitor_ = udev_monitor_new_from_netlink(udev_, \"udev\");\n+\tif (!monitor_)\n+\t\treturn -ENODEV;\n+\n+\tret = udev_monitor_filter_add_match_subsystem_devtype(monitor_, \"media\",\n+\t\t\t\t\t\t\t      nullptr);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n \treturn 0;\n }\n \n@@ -365,7 +418,18 @@ int DeviceEnumeratorUdev::enumerate()\n \t}\n done:\n \tudev_enumerate_unref(udev_enum);\n-\treturn ret >= 0 ? 0 : ret;\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tret = udev_monitor_enable_receiving(monitor_);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tint fd = udev_monitor_get_fd(monitor_);\n+\tnotifier_ = new EventNotifier(fd, EventNotifier::Read);\n+\tnotifier_->activated.connect(this, &DeviceEnumeratorUdev::udevNotify);\n+\n+\treturn 0;\n }\n \n std::string DeviceEnumeratorUdev::lookupDeviceNode(int major, int minor)\n@@ -389,4 +453,21 @@ std::string DeviceEnumeratorUdev::lookupDeviceNode(int major, int minor)\n \treturn deviceNode;\n }\n \n+void DeviceEnumeratorUdev::udevNotify(EventNotifier *notifier)\n+{\n+\tstruct udev_device *dev = udev_monitor_receive_device(monitor_);\n+\tstd::string action(udev_device_get_action(dev));\n+\tstd::string deviceNode(udev_device_get_devnode(dev));\n+\n+\tLOG(Debug) << action << \" device \" << udev_device_get_devnode(dev);\n+\n+\tif (action == \"add\") {\n+\t\taddDevice(deviceNode);\n+\t} else if (action == \"remove\") {\n+\t\tremoveDevice(deviceNode);\n+\t}\n+\n+\tudev_device_unref(dev);\n+}\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/include/device_enumerator.h b/src/libcamera/include/device_enumerator.h\nindex 3f87a6255303..22ed8dedcb06 100644\n--- a/src/libcamera/include/device_enumerator.h\n+++ b/src/libcamera/include/device_enumerator.h\n@@ -16,6 +16,7 @@\n \n namespace libcamera {\n \n+class EventNotifier;\n class MediaDevice;\n \n class DeviceMatch\n@@ -46,6 +47,7 @@ public:\n \n protected:\n \tint addDevice(const std::string &deviceNode);\n+\tvoid removeDevice(const std::string &deviceNode);\n \n private:\n \tstd::vector<std::shared_ptr<MediaDevice>> devices_;\n@@ -64,8 +66,12 @@ public:\n \n private:\n \tstruct udev *udev_;\n+\tstruct udev_monitor *monitor_;\n+\tEventNotifier *notifier_;\n \n \tstd::string lookupDeviceNode(int major, int minor) final;\n+\n+\tvoid udevNotify(EventNotifier *notifier);\n };\n \n } /* namespace libcamera */\n","prefixes":["libcamera-devel","10/10"]}