{"id":3793,"url":"https://patchwork.libcamera.org/api/patches/3793/?format=json","web_url":"https://patchwork.libcamera.org/patch/3793/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20200513172950.72685-6-email@uajain.com>","date":"2020-05-13T17:30:01","name":"[libcamera-devel,v2,5/5] tests: Introduce hotplug hot-unplug unit test","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"223382705e569ecbe0954071a99c3ec59ce2ccd0","submitter":{"id":1,"url":"https://patchwork.libcamera.org/api/people/1/?format=json","name":"Umang Jain","email":"email@uajain.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3793/mbox/","series":[{"id":903,"url":"https://patchwork.libcamera.org/api/series/903/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=903","date":"2020-05-13T17:29:57","name":"Introduce UVC hotplug support","version":2,"mbox":"https://patchwork.libcamera.org/series/903/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3793/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3793/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 48B6960E05\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 May 2020 19:30:03 +0200 (CEST)","by filterdrecv-p3las1-cb48d7cc9-hbg9w with SMTP id\n\tfilterdrecv-p3las1-cb48d7cc9-hbg9w-19-5EBC2E99-B\n\t2020-05-13 17:30:01.716054493 +0000 UTC m=+4206155.129874852","from mail.uajain.com (unknown)\n\tby ismtpd0008p1hnd1.sendgrid.net (SG) with ESMTP\n\tid 9w0Axt9xTMmt8wzQO4KqqA Wed, 13 May 2020 17:30:01.308 +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=\"i9rVmTIY\"; \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=n3tET7l5JBbSUk38m+eAP7HGUw7BhKcP/HWTY1KRbRE=;\n\tb=i9rVmTIYNQEnnIDBBV3dmlblHYW077K3q3Zrm+wI/mjkE8wOmk1P6jSVpcVyh2Wo+/oK\n\tsoqMcqHaGHKPr97YfW+6rsX33MoHhh19yc4OAjf98zDBUwOLc/UdJZoI7VDLFgaFIh59Mo\n\ti5WcOJVUIQcO1Y/e8CXXOPnHEsv0WB2fw=","From":"Umang Jain <email@uajain.com>","Date":"Wed, 13 May 2020 17:30:01 +0000 (UTC)","Message-Id":"<20200513172950.72685-6-email@uajain.com>","In-Reply-To":"<20200513172950.72685-1-email@uajain.com>","References":"<20200513172950.72685-1-email@uajain.com>","Mime-Version":"1.0","X-SG-EID":"1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcGYE5Iq+t2q0vZlJgodhIO78gONLK2nw2bcPFpm6NV+cHW8A0z64BTrWXbhN7KwVbmo9gzYkGAMrpkCSqX9SEJFEn0q4hz1yW4jm3L04HERlaysAtj3TWBVPlStnh/I+JfJovuDamrZZUZzonzIa4dqvR8M6VMnCHL9pDFVwcCU5bTkNE9Z9Uj/z820x24n9QS9nuUVdSBCSkZhjQOeV3vA==","To":"libcamera-devel <libcamera-devel@lists.libcamera.org>","Content-Transfer-Encoding":"7bit","Content-Type":"text/plain; charset=us-ascii","Subject":"[libcamera-devel] [PATCH v2 5/5] 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":"Wed, 13 May 2020 17:30:05 -0000"},"content":"Signed-off-by: Umang Jain <email@uajain.com>\n---\n test/hotplug-cameras.cpp | 152 +++++++++++++++++++++++++++++++++++++++\n test/meson.build         |   1 +\n 2 files changed, 153 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..cbf68af\n--- /dev/null\n+++ b/test/hotplug-cameras.cpp\n@@ -0,0 +1,152 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Umang Jain <email@uajain.com>\n+ *\n+ * hotplug-cameras.cpp - Emulate 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 <fcntl.h>\n+#include <sys/stat.h>\n+#include <sys/types.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 \"file.h\"\n+#include \"test.h\"\n+#include \"thread.h\"\n+\n+using namespace std;\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\tcameraAddedPass_ = true;\n+\t}\n+\n+\tvoid cameraRemovedHandler(std::shared_ptr<Camera> cam)\n+\t{\n+\t\tcameraRemovedPass_ = 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\tcameraAddedPass_ = false;\n+\t\tcameraRemovedPass_ = false;\n+\n+\t\tcm_->cameraAdded.connect(this, &HotplugTest::cameraAddedHandler);\n+\t\tcm_->cameraRemoved.connect(this, &HotplugTest::cameraRemovedHandler);\n+\n+\t\tuvc_toplevel_ = \"/sys/module/uvcvideo/drivers/usb:uvcvideo/\";\n+\n+\t\treturn 0;\n+\t}\n+\n+\tint run()\n+\t{\n+\t\tDIR *dir;\n+\t\tstruct dirent *dirent, *dirent2;\n+\t\tstd::string uvc_driver_dir;\n+\t\tbool uvc_driver_found = false;\n+\n+\t\tdir = opendir(uvc_toplevel_.c_str());\n+\t\t/* Find a UVC device driver symlink, which we can bind/unbind */\n+\t\twhile ((dirent = readdir(dir)) != nullptr) {\n+\t\t\tif (dirent->d_type != DT_LNK)\n+\t\t\t\tcontinue;\n+\n+\t\t\tstd::string child_dir = uvc_toplevel_ + dirent->d_name;\n+\t\t\tDIR *device_driver = opendir(child_dir.c_str());\n+\t\t\twhile ((dirent2 = readdir(device_driver)) != nullptr) {\n+\t\t\t\tif (strncmp(dirent2->d_name, \"video4linux\", 11) == 0) {\n+\t\t\t\t\tuvc_driver_dir = dirent->d_name;\n+\t\t\t\t\tuvc_driver_found = true;\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tclosedir(device_driver);\n+\n+\t\t\tif (uvc_driver_found)\n+\t\t\t\tbreak;\n+\t\t}\n+\t\tclosedir(dir);\n+\n+\t\t/* If no UVC driver found, skip */\n+\t\tif (!uvc_driver_found)\n+\t\t\treturn TestSkip;\n+\n+\t\t/* Unbind a camera, process events */\n+\t\tint fd1 = open(\"/sys/module/uvcvideo/drivers/usb:uvcvideo/unbind\", O_WRONLY);\n+\t\twrite(fd1, uvc_driver_dir.c_str(), uvc_driver_dir.size());\n+\t\tclose(fd1);\n+\t\tTimer timer;\n+\t\ttimer.start(1000);\n+\t\twhile (timer.isRunning())\n+\t\t\tThread::current()->eventDispatcher()->processEvents();\n+\n+\t\t/* \\todo: Fix this workaround of stopping and starting the camera-manager.\n+\t\t *  We need to do this, so that cm_ release all references to the uvc media symlinks.\n+\t\t */\n+\t\tcm_->stop();\n+\t\tif (cm_->start()) {\n+\t\t\tstd::cout << \"Failed to restart camera-manager\" << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\t/* Bind the camera again, process events */\n+\t\tint fd2 = open(\"/sys/module/uvcvideo/drivers/usb:uvcvideo/bind\", O_WRONLY);\n+\t\twrite(fd2, uvc_driver_dir.c_str(), uvc_driver_dir.size());\n+\t\tclose(fd2);\n+\n+\t\ttimer.start(1000);\n+\t\twhile (timer.isRunning())\n+\t\t\tThread::current()->eventDispatcher()->processEvents();\n+\n+\t\tif (cameraAddedPass_ && cameraRemovedPass_)\n+\t\t\treturn TestPass;\n+\t\telse\n+\t\t\treturn TestFail;\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+\tstd::string uvc_toplevel_;\n+\tbool cameraRemovedPass_;\n+\tbool cameraAddedPass_;\n+};\n+\n+TEST_REGISTER(HotplugTest)\n+\ndiff --git a/test/meson.build b/test/meson.build\nindex 5a45a85..383a7ea 100644\n--- a/test/meson.build\n+++ b/test/meson.build\n@@ -29,6 +29,7 @@ internal_tests = [\n     ['file',                            'file.cpp'],\n     ['file-descriptor',                 'file-descriptor.cpp'],\n     ['message',                         'message.cpp'],\n+    ['hotplug-cameras',                 'hotplug-cameras.cpp'],\n     ['object',                          'object.cpp'],\n     ['object-invoke',                   'object-invoke.cpp'],\n     ['signal-threads',                  'signal-threads.cpp'],\n","prefixes":["libcamera-devel","v2","5/5"]}