From patchwork Mon Sep 30 05:35:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 2070 X-Patchwork-Delegate: paul.elder@ideasonboard.com Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6FDFA6101D for ; Mon, 30 Sep 2019 07:35:38 +0200 (CEST) Received: from neptunite.amanokami.net (unknown [96.44.9.94]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C5415311; Mon, 30 Sep 2019 07:35:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1569821738; bh=Spx0K1o4ot3i00+ww3EvoCG0uSp+P1+EzVEIWstK66M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B6nYfWfj3i/gRrlErZMJl/sYQ2ukMRymB22EAiszpJ+qpU/m+zCYjg3AwueBB7ZnS /qg3HatZ0RH9TnPo1tQPn5x/iLjevU7u0JrjCbm7bCThbx6rvHlimi96sonhQYIkdx vuCbwjbUvpUlednSob7vBIj2vtlKjNrBNqrWbeqA= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Mon, 30 Sep 2019 01:35:19 -0400 Message-Id: <20190930053520.2711-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190930053520.2711-1-paul.elder@ideasonboard.com> References: <20190930053520.2711-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 2/3] libcamera: device_enumerator: Check that expected cameras are available X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 30 Sep 2019 05:35:38 -0000 Add a static method to DeviceEnumerator to check if the expected number of cameras have been enumerated. The expected cameras are the supported cameras declared by the pipeline handlers that the system has. Signed-off-by: Paul Elder --- src/libcamera/device_enumerator.cpp | 69 +++++++++++++++++++++++ src/libcamera/include/device_enumerator.h | 4 ++ 2 files changed, 73 insertions(+) diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index 0b596bce..7d28f9b8 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -9,10 +9,15 @@ #include "device_enumerator_sysfs.h" #include "device_enumerator_udev.h" +#include +#include +#include #include +#include #include "log.h" #include "media_device.h" +#include "pipeline_handler.h" #include "utils.h" /** @@ -306,4 +311,68 @@ std::shared_ptr DeviceEnumerator::search(const DeviceMatch &dm) return nullptr; } +int extractNumberFromFile(std::string &path) +{ + std::ifstream file; + std::string line; + + file.open(path); + if (!file) + return -1; + + file >> line; + int i = std::stoi(line, 0, 16); + file.close(); + + return i; +} + +bool DeviceEnumerator::haveExpectedCameras(CameraManager *cm) +{ + std::vector> &supportedDevices = + PipelineHandlerFactory::getDeviceIDs(); + std::vector availableDevices; + std::vector intersection; + struct dirent *ent; + DIR *dir; + const char *pciDir = "/sys/bus/pci/devices"; + + dir = opendir(pciDir); + if (!dir) { + LOG(DeviceEnumerator, Error) + << "Failed to open sysfs PCI directory"; + /* We can't expect any cameras, so we vacuously have them all. */ + return true; + } + + while ((ent = readdir(dir)) != nullptr) { + std::string path = pciDir + std::string("/") + ent->d_name + "/vendor"; + int vendor = extractNumberFromFile(path); + if (vendor < 0) + continue; + + path = pciDir + std::string("/") + ent->d_name + "/device"; + int device = extractNumberFromFile(path); + if (device < 0) + continue; + + availableDevices.push_back(PCIDeviceID(vendor, device)); + } + + closedir(dir); + + std::set_intersection(supportedDevices.begin(), supportedDevices.end(), + availableDevices.begin(), availableDevices.end(), + back_inserter(intersection), + compareDevices); + + if (cm->cameras().size() < intersection.size()) { + LOG(DeviceEnumerator, Warning) << "Not enough cameras!"; + return false; + } + + LOG(DeviceEnumerator, Debug) << "All cameras correctly initialized"; + return true; +} + } /* namespace libcamera */ diff --git a/src/libcamera/include/device_enumerator.h b/src/libcamera/include/device_enumerator.h index 770f4277..11c4cdfb 100644 --- a/src/libcamera/include/device_enumerator.h +++ b/src/libcamera/include/device_enumerator.h @@ -13,6 +13,8 @@ #include +#include + namespace libcamera { class MediaDevice; @@ -43,6 +45,8 @@ public: std::shared_ptr search(const DeviceMatch &dm); + static bool haveExpectedCameras(CameraManager *cm); + protected: std::shared_ptr createDevice(const std::string &deviceNode); void addDevice(const std::shared_ptr &media);