{"id":3836,"url":"https://patchwork.libcamera.org/api/1.1/patches/3836/?format=json","web_url":"https://patchwork.libcamera.org/patch/3836/","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":"<20200521135416.13685-6-email@uajain.com>","date":"2020-05-21T13:54:27","name":"[libcamera-devel,v3,5/5] tests: Introduce hotplug hot-unplug unit test","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"abb8e9a8b12d814d9850e1ddfe24834c18e307a0","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/3836/mbox/","series":[{"id":920,"url":"https://patchwork.libcamera.org/api/1.1/series/920/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=920","date":"2020-05-21T13:54:23","name":"Introduce UVC hotplugging support","version":3,"mbox":"https://patchwork.libcamera.org/series/920/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3836/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3836/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 A6950603D9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 21 May 2020 15:54:28 +0200 (CEST)","by filterdrecv-p3iad2-8ddf98858-4fqk8 with SMTP id\n\tfilterdrecv-p3iad2-8ddf98858-4fqk8-19-5EC68813-26\n\t2020-05-21 13:54:27.596622901 +0000 UTC m=+4884413.031055259","from mail.uajain.com (unknown)\n\tby ismtpd0007p1maa1.sendgrid.net (SG) with ESMTP id\n\t7RRIfSyHQZOK4rWW4y9ucg for <libcamera-devel@lists.libcamera.org>;\n\tThu, 21 May 2020 13:54:27.117 +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=\"IGpZuLTC\"; \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=eAp+C9bfhjTo63Nn7YEXdnItNWT1wnBN473amMBJM7Y=;\n\tb=IGpZuLTCjHNnmXkUgBQ0gUE4hokBNvwrztNJnki4QYIWPSdXJvpagMAFWYmfDDiLhKVI\n\tnaHJ44QeTx+kkRD4K/HkENQsrJtz7gW950mKhrpNyaPqKkpgYXOLvMO+FHtf4mEzU/io/r\n\t/CuNLhdpIyBeQsYTZ5znOzlE2Q0REyKWw=","From":"Umang Jain <email@uajain.com>","Date":"Thu, 21 May 2020 13:54:27 +0000 (UTC)","Message-Id":"<20200521135416.13685-6-email@uajain.com>","In-Reply-To":"<20200521135416.13685-1-email@uajain.com>","References":"<20200521135416.13685-1-email@uajain.com>","Mime-Version":"1.0","X-SG-EID":"1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcDltxpLqwX5mosojZSfQDSt90SyLMoXpNcGo9OiQynrD+lJlSt65IbYy8vmLB7GCUkaLWxXssjT8bzIZQ8GazNkLMqyNmHHfWM3VAsw052SSAf/2sw5Qk0Qdn0W7ZoeVvtdnStJjuCLzG4FH5WZwXwZue1FzX6g0x4Rjrkv0+79ic0vDDoMp69U5IiwF51DTXErAGp/koldEmuEaYz1L//g==","To":"libcamera-devel@lists.libcamera.org","Content-Transfer-Encoding":"7bit","Content-Type":"text/plain; charset=us-ascii","Subject":"[libcamera-devel] [PATCH v3 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":"Thu, 21 May 2020 13:54:30 -0000"},"content":"This test checks the code-paths for camera's hotplugged and\nunplugged support. It is based off bind/unbind of a UVC device\nfrom sysfs. Hence, this tests required root permissions to run\nand should have atleast one already bound UVC device present\non the system.\n\nSigned-off-by: Umang Jain <email@uajain.com>\n---\n test/hotplug-cameras.cpp | 153 +++++++++++++++++++++++++++++++++++++++\n test/meson.build         |   1 +\n 2 files changed, 154 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..72c370f\n--- /dev/null\n+++ b/test/hotplug-cameras.cpp\n@@ -0,0 +1,153 @@\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 \"libcamera/internal/file.h\"\n+#include \"libcamera/internal/thread.h\"\n+\n+#include \"test.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_->newCameraAdded.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 bd7da14..f7e27d7 100644\n--- a/test/meson.build\n+++ b/test/meson.build\n@@ -31,6 +31,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","v3","5/5"]}