[{"id":5196,"web_url":"https://patchwork.libcamera.org/comment/5196/","msgid":"<20200612114204.GL5957@pendragon.ideasonboard.com>","date":"2020-06-12T11:42:04","subject":"Re: [libcamera-devel] [PATCH v4 6/6] tests: Introduce hotplug\n\thot-unplug unit test","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Umang,\n\nThank you for the patch.\n\nOn Thu, Jun 11, 2020 at 05:16:08PM +0000, Umang Jain wrote:\n> This test checks the code-paths for camera's hotplugged and\n> unplugged support. It is based on bind/unbind of a UVC device\n> from sysfs. Hence, this test requires root permissions to run\n> and should have at least one already bound UVC device present\n> in the system.\n> \n> Signed-off-by: Umang Jain <email@uajain.com>\n> ---\n>  test/hotplug-cameras.cpp | 124 +++++++++++++++++++++++++++++++++++++++\n>  test/meson.build         |   1 +\n>  2 files changed, 125 insertions(+)\n>  create mode 100644 test/hotplug-cameras.cpp\n> \n> diff --git a/test/hotplug-cameras.cpp b/test/hotplug-cameras.cpp\n> new file mode 100644\n> index 0000000..07500fc\n> --- /dev/null\n> +++ b/test/hotplug-cameras.cpp\n> @@ -0,0 +1,124 @@\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\n\t\t\tstd::cout << \"Camera unplug not detected\" << std::endl;\n\n> +\t\t\treturn TestFail;\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\n\t\t\tstd::cout << \"Camera plug not detected\" << std::endl;\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\t\t\treturn TestFail;\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)\n> diff --git a/test/meson.build b/test/meson.build\n> index 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'],","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 41C6D60C4E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 12 Jun 2020 13:42:26 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id AFB5024F;\n\tFri, 12 Jun 2020 13:42:25 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"SGSU8NeY\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1591962145;\n\tbh=tE0DKzpvHjUuNSrzEDjyEwtVMx4u6NYcUCnM7hQMLDM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=SGSU8NeY1N0dnpq/Cb75tk8asS78MroUjH6xWohxSXBE4fequ2yO0x1Z8tipjW9XX\n\t40ziV7jtxedjWXDDXKrwl5Saa5Li/Ew+rJDVBCGB1aECRAckAUBy3Csd6VJRFr3Y9y\n\tpwK5XoyVoFT8kGojDP1DKfxgJNhRkIzGZwspVE9M=","Date":"Fri, 12 Jun 2020 14:42:04 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Umang Jain <email@uajain.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200612114204.GL5957@pendragon.ideasonboard.com>","References":"<20200611171528.9381-1-email@uajain.com>\n\t<20200611171528.9381-7-email@uajain.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20200611171528.9381-7-email@uajain.com>","Subject":"Re: [libcamera-devel] [PATCH v4 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":"Fri, 12 Jun 2020 11:42:26 -0000"}}]