{"id":4070,"url":"https://patchwork.libcamera.org/api/1.1/patches/4070/?format=json","web_url":"https://patchwork.libcamera.org/patch/4070/","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":"<20200616194523.23268-7-email@uajain.com>","date":"2020-06-16T19:45:39","name":"[libcamera-devel,v5,6/6] tests: Introduce hotplug hot-unplug unit test","commit_ref":"8960ef9715cfb38a3b205d7916662f31df5ddd35","pull_url":null,"state":"accepted","archived":false,"hash":"0eeaa361634bb8cd03fdf9266777085df23552e2","submitter":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/people/1/?format=json","name":"Umang Jain","email":"email@uajain.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/4070/mbox/","series":[{"id":1009,"url":"https://patchwork.libcamera.org/api/1.1/series/1009/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1009","date":"2020-06-16T19:45:33","name":"Introduce UVC hotplugging support","version":5,"mbox":"https://patchwork.libcamera.org/series/1009/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/4070/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/4070/checks/","tags":{},"headers":{"Return-Path":"<bounces+15657259-5c31-libcamera-devel=lists.libcamera.org@em7280.uajain.com>","Received":["from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BE0D761F24\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 16 Jun 2020 21:45:40 +0200 (CEST)","by filterdrecv-p3mdw1-6f5df8956d-w9s4g with SMTP id\n\tfilterdrecv-p3mdw1-6f5df8956d-w9s4g-18-5EE92163-16\n\t2020-06-16 19:45:39.386303073 +0000 UTC m=+1121510.399594273","from mail.uajain.com (unknown)\n\tby ismtpd0005p1maa1.sendgrid.net (SG) with ESMTP\n\tid QyotJ4fZRr-pQjWE0Cl0yw Tue, 16 Jun 2020 19:45:39.001 +0000 (UTC)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=uajain.com\n\theader.i=@uajain.com header.b=\"hmXbvOGd\"; \n\tdkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com;\n\th=from:subject:in-reply-to:references:mime-version:to:cc:\n\tcontent-transfer-encoding:content-type;\n\ts=s1; bh=lCwG2FumzuGu3eT7GFXMRl8Auk2TG9RkFgA5H2Mq8L0=;\n\tb=hmXbvOGdmLYr/ZnpJ6FRPkY6v/gvqJstUdkHzDZYGZRHB0s7e53Cbev035pzdwcb+14+\n\tBUwY0rNsW8JA5/fYzTJXGKxx9ClqOJBs7U8dTSb2RMbxln4Ek27TG0yyIVAuB8e0uMSl4o\n\t9OH7hLnSSfd4s/IVxWQzqopr0Fwv4Ij+M=","From":"Umang Jain <email@uajain.com>","Date":"Tue, 16 Jun 2020 19:45:39 +0000 (UTC)","Message-Id":"<20200616194523.23268-7-email@uajain.com>","In-Reply-To":"<20200616194523.23268-1-email@uajain.com>","References":"<20200616194523.23268-1-email@uajain.com>","Mime-Version":"1.0","X-SG-EID":"1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcaThFi/Xwo9wPYVdHfbcDxgDhEYNc+7SkskSbO1EHFmIdV6ojG+6PuxutfExZlOvl4kTrL4qFnq5eCS54j8tF04jqQRrY1+pfmLUtMAin6uKCH0BMzEEoFSmw0kl7IbDAOUEh2K/XQijoa/iZfsBL26jtIY3Bc4dln3BQiUPM79vaP4fzoGgIC4PfS08NTVsaqjI+oyqQyWliBlsPBwR+hA==","To":"laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com,\n\tlibcamera-devel@lists.libcamera.org","Content-Transfer-Encoding":"7bit","Content-Type":"text/plain; charset=us-ascii","Subject":"[libcamera-devel] [PATCH v5 6/6] tests: Introduce hotplug\n\thot-unplug unit test","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>","X-List-Received-Date":"Tue, 16 Jun 2020 19:45:41 -0000"},"content":"This test checks the code-paths for camera's hotplugged and\nunplugged support. It is based on bind/unbind of a UVC device\nfrom sysfs. Hence, this test requires root permissions to run\nand should have at least one already bound UVC device present\nin the system.\n\nSigned-off-by: Umang Jain <email@uajain.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n test/hotplug-cameras.cpp | 128 +++++++++++++++++++++++++++++++++++++++\n test/meson.build         |   1 +\n 2 files changed, 129 insertions(+)\n create mode 100644 test/hotplug-cameras.cpp","diff":"diff --git a/test/hotplug-cameras.cpp b/test/hotplug-cameras.cpp\nnew file mode 100644\nindex 0000000..6a94535\n--- /dev/null\n+++ b/test/hotplug-cameras.cpp\n@@ -0,0 +1,128 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Umang Jain <email@uajain.com>\n+ *\n+ * hotplug-cameras.cpp - Test cameraAdded/cameraRemoved signals in CameraManager\n+ */\n+\n+#include <dirent.h>\n+#include <fstream>\n+#include <iostream>\n+#include <string.h>\n+#include <unistd.h>\n+\n+#include <libcamera/camera.h>\n+#include <libcamera/camera_manager.h>\n+#include <libcamera/event_dispatcher.h>\n+#include <libcamera/timer.h>\n+\n+#include \"libcamera/internal/file.h\"\n+#include \"libcamera/internal/thread.h\"\n+\n+#include \"test.h\"\n+\n+using namespace libcamera;\n+\n+class HotplugTest : public Test\n+{\n+protected:\n+\tvoid cameraAddedHandler(std::shared_ptr<Camera> cam)\n+\t{\n+\t\tcameraAdded_ = true;\n+\t}\n+\n+\tvoid cameraRemovedHandler(std::shared_ptr<Camera> cam)\n+\t{\n+\t\tcameraRemoved_ = true;\n+\t}\n+\n+\tint init()\n+\t{\n+\t\tif (!File::exists(\"/sys/module/uvcvideo\")) {\n+\t\t\tstd::cout << \"uvcvideo driver is not loaded, skipping\" << std::endl;\n+\t\t\treturn TestSkip;\n+\t\t}\n+\n+\t\tif (geteuid() != 0) {\n+\t\t\tstd::cout << \"This test requires root permissions, skipping\" << std::endl;\n+\t\t\treturn TestSkip;\n+\t\t}\n+\n+\t\tcm_ = new CameraManager();\n+\t\tif (cm_->start()) {\n+\t\t\tstd::cout << \"Failed to start camera manager\" << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tcameraAdded_ = false;\n+\t\tcameraRemoved_ = false;\n+\n+\t\tcm_->cameraAdded.connect(this, &HotplugTest::cameraAddedHandler);\n+\t\tcm_->cameraRemoved.connect(this, &HotplugTest::cameraRemovedHandler);\n+\n+\t\treturn 0;\n+\t}\n+\n+\tint run()\n+\t{\n+\t\tDIR *dir;\n+\t\tstruct dirent *dirent;\n+\t\tstd::string uvcDeviceDir;\n+\n+\t\tdir = opendir(uvcDriverDir_.c_str());\n+\t\t/* Find a UVC device directory, which we can bind/unbind. */\n+\t\twhile ((dirent = readdir(dir)) != nullptr) {\n+\t\t\tif (!File::exists(uvcDriverDir_ + dirent->d_name + \"/video4linux\"))\n+\t\t\t\tcontinue;\n+\n+\t\t\tuvcDeviceDir = dirent->d_name;\n+\t\t\tbreak;\n+\t\t}\n+\t\tclosedir(dir);\n+\n+\t\t/* If no UVC device found, skip the test. */\n+\t\tif (uvcDeviceDir.empty())\n+\t\t\treturn TestSkip;\n+\n+\t\t/* Unbind a camera and process events. */\n+\t\tstd::ofstream(uvcDriverDir_ + \"unbind\", std::ios::binary)\n+\t\t\t<< uvcDeviceDir;\n+\t\tTimer timer;\n+\t\ttimer.start(1000);\n+\t\twhile (timer.isRunning() && !cameraRemoved_)\n+\t\t\tThread::current()->eventDispatcher()->processEvents();\n+\t\tif (!cameraRemoved_) {\n+\t\t\tstd::cout << \"Camera unplug not detected\" << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\t/* Bind the camera again and process events. */\n+\t\tstd::ofstream(uvcDriverDir_ + \"bind\", std::ios::binary)\n+\t\t\t<< uvcDeviceDir;\n+\t\ttimer.start(1000);\n+\t\twhile (timer.isRunning() && !cameraAdded_)\n+\t\t\tThread::current()->eventDispatcher()->processEvents();\n+\t\tif (!cameraAdded_) {\n+\t\t\tstd::cout << \"Camera plug not detected\" << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n+\tvoid cleanup()\n+\t{\n+\t\tcm_->stop();\n+\t\tdelete cm_;\n+\t}\n+\n+private:\n+\tCameraManager *cm_;\n+\tstatic const std::string uvcDriverDir_;\n+\tbool cameraRemoved_;\n+\tbool cameraAdded_;\n+};\n+\n+const std::string HotplugTest::uvcDriverDir_ = \"/sys/bus/usb/drivers/uvcvideo/\";\n+\n+TEST_REGISTER(HotplugTest)\ndiff --git a/test/meson.build b/test/meson.build\nindex bd7da14..a868813 100644\n--- a/test/meson.build\n+++ b/test/meson.build\n@@ -30,6 +30,7 @@ internal_tests = [\n     ['event-thread',                    'event-thread.cpp'],\n     ['file',                            'file.cpp'],\n     ['file-descriptor',                 'file-descriptor.cpp'],\n+    ['hotplug-cameras',                 'hotplug-cameras.cpp'],\n     ['message',                         'message.cpp'],\n     ['object',                          'object.cpp'],\n     ['object-invoke',                   'object-invoke.cpp'],\n","prefixes":["libcamera-devel","v5","6/6"]}