[{"id":2763,"web_url":"https://patchwork.libcamera.org/comment/2763/","msgid":"<20191003200505.GR1322@bigcity.dyn.berto.se>","date":"2019-10-03T20:05:05","subject":"Re: [libcamera-devel] [RFC PATCH 2/3] libcamera: device_enumerator:\n\tCheck that expected cameras are available","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Paul,\n\nThanks for your work.\n\nOn 2019-09-30 01:35:19 -0400, Paul Elder wrote:\n> Add a static method to DeviceEnumerator to check if the expected number\n> of cameras have been enumerated. The expected cameras are the supported\n> cameras declared by the pipeline handlers that the system has.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> ---\n>  src/libcamera/device_enumerator.cpp       | 69 +++++++++++++++++++++++\n>  src/libcamera/include/device_enumerator.h |  4 ++\n>  2 files changed, 73 insertions(+)\n> \n> diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\n> index 0b596bce..7d28f9b8 100644\n> --- a/src/libcamera/device_enumerator.cpp\n> +++ b/src/libcamera/device_enumerator.cpp\n> @@ -9,10 +9,15 @@\n>  #include \"device_enumerator_sysfs.h\"\n>  #include \"device_enumerator_udev.h\"\n>  \n> +#include <algorithm>\n> +#include <dirent.h>\n> +#include <fstream>\n>  #include <string.h>\n> +#include <sys/types.h>\n>  \n>  #include \"log.h\"\n>  #include \"media_device.h\"\n> +#include \"pipeline_handler.h\"\n>  #include \"utils.h\"\n>  \n>  /**\n> @@ -306,4 +311,68 @@ std::shared_ptr<MediaDevice> DeviceEnumerator::search(const DeviceMatch &dm)\n>  \treturn nullptr;\n>  }\n>  \n> +int extractNumberFromFile(std::string &path)\n> +{\n> +\tstd::ifstream file;\n> +\tstd::string line;\n> +\n> +\tfile.open(path);\n> +\tif (!file)\n> +\t\treturn -1;\n> +\n> +\tfile >> line;\n> +\tint i = std::stoi(line, 0, 16);\n> +\tfile.close();\n> +\n> +\treturn i;\n> +}\n> +\n> +bool DeviceEnumerator::haveExpectedCameras(CameraManager *cm)\n> +{\n> +\tstd::vector<std::reference_wrapper<DeviceID>> &supportedDevices =\n> +\t\tPipelineHandlerFactory::getDeviceIDs();\n> +\tstd::vector<DeviceID> availableDevices;\n> +\tstd::vector<DeviceID> intersection;\n> +\tstruct dirent *ent;\n> +\tDIR *dir;\n> +\tconst char *pciDir = \"/sys/bus/pci/devices\";\n> +\n> +\tdir = opendir(pciDir);\n> +\tif (!dir) {\n> +\t\tLOG(DeviceEnumerator, Error)\n> +\t\t\t<< \"Failed to open sysfs PCI directory\";\n> +\t\t/* We can't expect any cameras, so we vacuously have them all. */\n> +\t\treturn true;\n> +\t}\n> +\n> +\twhile ((ent = readdir(dir)) != nullptr) {\n> +\t\tstd::string path = pciDir + std::string(\"/\") + ent->d_name + \"/vendor\";\n> +\t\tint vendor = extractNumberFromFile(path);\n> +\t\tif (vendor < 0)\n> +\t\t\tcontinue;\n> +\n> +\t\tpath = pciDir + std::string(\"/\") + ent->d_name + \"/device\";\n> +\t\tint device = extractNumberFromFile(path);\n> +\t\tif (device < 0)\n> +\t\t\tcontinue;\n> +\n> +\t\tavailableDevices.push_back(PCIDeviceID(vendor, device));\n> +\t}\n> +\n> +\tclosedir(dir);\n> +\n> +\tstd::set_intersection(supportedDevices.begin(), supportedDevices.end(),\n> +\t\t\t      availableDevices.begin(), availableDevices.end(),\n> +\t\t\t      back_inserter(intersection),\n> +\t\t\t      compareDevices<PCIDeviceID>);\n> +\n> +\tif (cm->cameras().size() < intersection.size()) {\n> +\t\tLOG(DeviceEnumerator, Warning) << \"Not enough cameras!\";\n> +\t\treturn false;\n> +\t}\n\nTo me this seems a bit too strict. Say that I have a Soraka device, run \na kernel on it without IPU3 support, With this change libcamera would \nnot function for a USB camera.\n\nAlso comparing against cm->cameras().size() might be skewed if you run \non a kernel with vivid and/or vimc support built in.\n\n> +\n> +\tLOG(DeviceEnumerator, Debug) << \"All cameras correctly initialized\";\n> +\treturn true;\n> +}\n> +\n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/include/device_enumerator.h b/src/libcamera/include/device_enumerator.h\n> index 770f4277..11c4cdfb 100644\n> --- a/src/libcamera/include/device_enumerator.h\n> +++ b/src/libcamera/include/device_enumerator.h\n> @@ -13,6 +13,8 @@\n>  \n>  #include <linux/media.h>\n>  \n> +#include <libcamera/camera_manager.h>\n> +\n>  namespace libcamera {\n>  \n>  class MediaDevice;\n> @@ -43,6 +45,8 @@ public:\n>  \n>  \tstd::shared_ptr<MediaDevice> search(const DeviceMatch &dm);\n>  \n> +\tstatic bool haveExpectedCameras(CameraManager *cm);\n> +\n>  protected:\n>  \tstd::shared_ptr<MediaDevice> createDevice(const std::string &deviceNode);\n>  \tvoid addDevice(const std::shared_ptr<MediaDevice> &media);\n> -- \n> 2.23.0\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lf1-x142.google.com (mail-lf1-x142.google.com\n\t[IPv6:2a00:1450:4864:20::142])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6C81B60BE8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  3 Oct 2019 22:05:07 +0200 (CEST)","by mail-lf1-x142.google.com with SMTP id d17so2785042lfa.7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 03 Oct 2019 13:05:07 -0700 (PDT)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\tq66sm728738ljq.101.2019.10.03.13.05.06\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 03 Oct 2019 13:05:06 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=A2+2/ghTrhFPDcbUVtkel1rJU+gWtxN8mGUlYQnRMGM=;\n\tb=P8Nt0ipH22biSuu6Oq+b/9N5LcLm/sLHAeSEqhZ/HOwkomh2MvUkA2v8kXuwPihDhd\n\tVwaFwac4pEihjU2aEC/VtOvIQUp+y7gR+FJ7iDH315nM0O4R64mqQvWRyR5aRr8KJMRf\n\tyhJtpiKWfClYMSx7CUwRf4LAhIGthAErWs9VIRq4OnfVOL+g2QTJUJ3CQ21KQTWJiJCT\n\tqlEDMimAEX1wgs72eYqwa8p+41QleymUoAb7c3snj68HTPy82noHMdPM9N8J5wiiAGD+\n\tmishevGY0rc6iz0fRudoea/Ip6WDyepaBtNF2nkVTYVK7H8Vrxq36AUY4rrMfps1w5L8\n\tMtfg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=A2+2/ghTrhFPDcbUVtkel1rJU+gWtxN8mGUlYQnRMGM=;\n\tb=sUtsawp8sKW6uAkVTMfK1zWuv2TBcHSVFplJZfesCwMCXk+sV4cj29lYMV1rQH4F+6\n\tPUMzhYnqdcRfUjDqz48EI2CpbGDM+7LsNnvkVa4Oe3dMSqv4eNORDnv6rCVaIJU+jV+6\n\tm6fEaXfBvIa1ktUDM+oF8p3ZV6HFovqZluLIF+nWXbMTtGkn3rawRmR4TusEnQr6MjcE\n\tdL7q8cIy36Yg95y9zQWKb9mYmABXQ1J8M72cbVbiyFksDTOeDofq1ED7OmOI24C/Yldq\n\tmcjWqsZBy5AWmh3IiDVPWhlHiqIzlvI9UR3Fy136vNCkk5O7ISJOWxkKnKnwi6SApZrG\n\txKow==","X-Gm-Message-State":"APjAAAUJ+/BqtSTu9eaZSH6ALEhWb4mFg7WOfTBu3dzV7L66GSRaqTDJ\n\tbClGZWGT9AUhuHfTrWtFpNVW4qyyq20=","X-Google-Smtp-Source":"APXvYqxkIBqnrssRIfUGemh+VAR80sJEPDpfFwbc+ZecALxw+BUPNLOh4e/w4HZlk+8MyRfPJIEvyg==","X-Received":"by 2002:a19:4b4a:: with SMTP id\n\ty71mr6738909lfa.118.1570133106769; \n\tThu, 03 Oct 2019 13:05:06 -0700 (PDT)","Date":"Thu, 3 Oct 2019 22:05:05 +0200","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191003200505.GR1322@bigcity.dyn.berto.se>","References":"<20190930053520.2711-1-paul.elder@ideasonboard.com>\n\t<20190930053520.2711-2-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190930053520.2711-2-paul.elder@ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [libcamera-devel] [RFC PATCH 2/3] libcamera: device_enumerator:\n\tCheck that expected cameras are available","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, 03 Oct 2019 20:05:07 -0000"}}]