{"id":9159,"url":"https://patchwork.libcamera.org/api/patches/9159/?format=json","web_url":"https://patchwork.libcamera.org/patch/9159/","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":"<20200803211733.1037019-6-niklas.soderlund@ragnatech.se>","date":"2020-08-03T21:17:29","name":"[libcamera-devel,v6,5/9] libcamera: camera_sensor: Generate a sensor ID","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"ae19ff5354d1c2c3908b152bcf3c7c7b58c2ff3d","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9159/mbox/","series":[{"id":1185,"url":"https://patchwork.libcamera.org/api/series/1185/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1185","date":"2020-08-03T21:17:24","name":"libcamera: Generate unique and stable camera IDs","version":6,"mbox":"https://patchwork.libcamera.org/series/1185/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9159/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9159/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 50305BD87A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  3 Aug 2020 21:18:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0387661B3B;\n\tMon,  3 Aug 2020 23:18:05 +0200 (CEST)","from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 96E4460398\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  3 Aug 2020 23:18:02 +0200 (CEST)","from bismarck.berto.se (p54ac52a8.dip0.t-ipconnect.de\n\t[84.172.82.168]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA\n\tid 9e259e50-d5ce-11ea-86ee-0050569116f7;\n\tMon, 03 Aug 2020 23:16:38 +0200 (CEST)"],"X-Halon-ID":"9e259e50-d5ce-11ea-86ee-0050569116f7","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon,  3 Aug 2020 23:17:29 +0200","Message-Id":"<20200803211733.1037019-6-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.28.0","In-Reply-To":"<20200803211733.1037019-1-niklas.soderlund@ragnatech.se>","References":"<20200803211733.1037019-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v6 5/9] libcamera: camera_sensor: Generate\n\ta sensor ID","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>","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Generate a constant and unique string ID for the sensor. The ID is\ngenerated from information from the firmware description of the camera\nimage sensor. The ID is unique and persistent across reboots of the\nsystem and may be used as a camera ID.\n\nThe CameraSensor is intended to be sub-classed for specific sensor\nmodules in the future to enable more advanced use-cases. Controlling the\nID is one such feature that is already needed by the VIMC sensor. As the\nVIMC sensor is a virtual pipeline its sensor is not described in the\ndevice firmware and a subclass of CameraSensor is needed to generate a\nID for VIMC sensors.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n* Changes since v5\n- Use new utils:: helper.\n- Allow for subclassing\n\n* Changes since v4\n- Fix spelling.\n\n* Changes since v3\n- Update commit message.\n- Add description of how ID are generated to comment.\n---\n include/libcamera/internal/camera_sensor.h    |  3 ++\n src/libcamera/camera_sensor.cpp               | 40 +++++++++++++++++++\n src/libcamera/pipeline/vimc/vimc.cpp          | 19 ++++++++-\n test/camera-sensor.cpp                        |  3 +-\n test/libtest/vimc_sensor_test.h               | 29 ++++++++++++++\n .../v4l2_videodevice_test.cpp                 |  4 +-\n 6 files changed, 94 insertions(+), 4 deletions(-)\n create mode 100644 test/libtest/vimc_sensor_test.h","diff":"diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\nindex 06c8292ca30129de..b3aaaeaf829d97c0 100644\n--- a/include/libcamera/internal/camera_sensor.h\n+++ b/include/libcamera/internal/camera_sensor.h\n@@ -47,6 +47,7 @@ public:\n \tint init();\n \n \tconst std::string &model() const { return model_; }\n+\tconst std::string &id() const { return id_; }\n \tconst MediaEntity *entity() const { return entity_; }\n \tconst std::vector<unsigned int> &mbusCodes() const { return mbusCodes_; }\n \tconst std::vector<Size> &sizes() const { return sizes_; }\n@@ -65,6 +66,7 @@ public:\n \n protected:\n \tstd::string logPrefix() const override;\n+\tvirtual std::string generateID() const;\n \n private:\n \tconst MediaEntity *entity_;\n@@ -72,6 +74,7 @@ private:\n \tunsigned int pad_;\n \n \tstd::string model_;\n+\tstd::string id_;\n \n \tV4L2Subdevice::Formats formats_;\n \tSize resolution_;\ndiff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\nindex 350f49accad99c7b..282cedecaf28fb3c 100644\n--- a/src/libcamera/camera_sensor.cpp\n+++ b/src/libcamera/camera_sensor.cpp\n@@ -204,6 +204,11 @@ int CameraSensor::init()\n \tif (ret < 0)\n \t\treturn ret;\n \n+\t/* Generate a unique ID for the sensor. */\n+\tid_ = generateID();\n+\tif (id_.empty())\n+\t\treturn -EINVAL;\n+\n \t/* Retrieve and store the camera sensor properties. */\n \tconst ControlInfoMap &controls = subdev_->controls();\n \tint32_t propertyValue;\n@@ -283,6 +288,16 @@ int CameraSensor::init()\n  * \\return The sensor model name\n  */\n \n+/**\n+ * \\fn CameraSensor::id()\n+ * \\brief Retrieve the sensor ID\n+ *\n+ * The sensor ID is a free-form string that uniquely identifies the sensor in\n+ * the system. The ID satisfy the requirements to be used as a camera ID.\n+ *\n+ * \\return The sensor ID\n+ */\n+\n /**\n  * \\fn CameraSensor::entity()\n  * \\brief Retrieve the sensor media entity\n@@ -541,4 +556,29 @@ std::string CameraSensor::logPrefix() const\n \treturn \"'\" + entity_->name() + \"'\";\n }\n \n+/**\n+ * \\brief Generate a sensor ID for the sensor\n+ *\n+ * The generated string satisfy the requirements to be used as ID for a camera.\n+ * This default implementation retrieves the ID form the firmware ID associated\n+ * with the sensors V4L2 subdevice, that meets this criteria.\n+ *\n+ * It is possible to override this function to allow for more advanced IDs to be\n+ * generated for specific sensors. Any alternative implementation is responsible\n+ * for that the generated ID satisfy the requirements to be used as a camera ID.\n+ *\n+ * \\sa id()\n+ * \\return Sensor ID on success or empty string on failure\n+ */\n+std::string CameraSensor::generateID() const\n+{\n+\tstd::string id;\n+\tif (utils::tryLookupFirmwareID(subdev_->devicePath(), &id)) {\n+\t\tLOG(CameraSensor, Error) << \"Can't get sensor ID from firmware\";\n+\t\treturn {};\n+\t}\n+\n+\treturn id;\n+}\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/vimc/vimc.cpp b/src/libcamera/pipeline/vimc/vimc.cpp\nindex 4f461b928514022d..b3348ab5d987d506 100644\n--- a/src/libcamera/pipeline/vimc/vimc.cpp\n+++ b/src/libcamera/pipeline/vimc/vimc.cpp\n@@ -38,6 +38,21 @@ namespace libcamera {\n \n LOG_DEFINE_CATEGORY(VIMC)\n \n+class VimcCameraSensor : public CameraSensor\n+{\n+public:\n+\tVimcCameraSensor(const MediaEntity *entity)\n+\t\t: CameraSensor(entity)\n+\t{\n+\t}\n+\n+protected:\n+\tstd::string generateID() const override\n+\t{\n+\t\treturn \"VIMC \" + entity()->name();\n+\t}\n+};\n+\n class VimcCameraData : public CameraData\n {\n public:\n@@ -61,7 +76,7 @@ public:\n \tvoid bufferReady(FrameBuffer *buffer);\n \n \tMediaDevice *media_;\n-\tCameraSensor *sensor_;\n+\tVimcCameraSensor *sensor_;\n \tV4L2Subdevice *debayer_;\n \tV4L2Subdevice *scaler_;\n \tV4L2VideoDevice *video_;\n@@ -457,7 +472,7 @@ int VimcCameraData::init()\n \t\treturn ret;\n \n \t/* Create and open the camera sensor, debayer, scaler and video device. */\n-\tsensor_ = new CameraSensor(media_->getEntityByName(\"Sensor B\"));\n+\tsensor_ = new VimcCameraSensor(media_->getEntityByName(\"Sensor B\"));\n \tret = sensor_->init();\n \tif (ret)\n \t\treturn ret;\ndiff --git a/test/camera-sensor.cpp b/test/camera-sensor.cpp\nindex 8c7fd1d2d4445db3..50157b234fd40933 100644\n--- a/test/camera-sensor.cpp\n+++ b/test/camera-sensor.cpp\n@@ -17,6 +17,7 @@\n #include \"libcamera/internal/v4l2_subdevice.h\"\n \n #include \"test.h\"\n+#include \"vimc_sensor_test.h\"\n \n using namespace std;\n using namespace libcamera;\n@@ -50,7 +51,7 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\tsensor_ = new CameraSensor(entity);\n+\t\tsensor_ = new TestVimcCameraSensor(entity);\n \t\tif (sensor_->init() < 0) {\n \t\t\tcerr << \"Unable to initialise camera sensor\" << endl;\n \t\t\treturn TestFail;\ndiff --git a/test/libtest/vimc_sensor_test.h b/test/libtest/vimc_sensor_test.h\nnew file mode 100644\nindex 0000000000000000..1454a1c310f0dc9c\n--- /dev/null\n+++ b/test/libtest/vimc_sensor_test.h\n@@ -0,0 +1,29 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * vimc_sensor_test.h - vimc_sensor test class\n+ */\n+#ifndef __LIBCAMERA_VIMC_SENSOR_TEST_H__\n+#define __LIBCAMERA_VIMC_SENSOR_TEST_H__\n+\n+#include \"libcamera/internal/camera_sensor.h\"\n+\n+using namespace libcamera;\n+\n+class TestVimcCameraSensor : public CameraSensor\n+{\n+public:\n+\tTestVimcCameraSensor(const MediaEntity *entity)\n+\t\t: CameraSensor(entity)\n+\t{\n+\t}\n+\n+protected:\n+\tstd::string generateID() const override\n+\t{\n+\t\treturn \"VIMC \" + entity()->name();\n+\t}\n+};\n+\n+#endif /* __LIBCAMERA_VIMC_SENSOR_TEST_H__ */\ndiff --git a/test/v4l2_videodevice/v4l2_videodevice_test.cpp b/test/v4l2_videodevice/v4l2_videodevice_test.cpp\nindex f23aaf8f514bc050..c7a3479963be9b78 100644\n--- a/test/v4l2_videodevice/v4l2_videodevice_test.cpp\n+++ b/test/v4l2_videodevice/v4l2_videodevice_test.cpp\n@@ -12,6 +12,8 @@\n #include \"libcamera/internal/device_enumerator.h\"\n #include \"libcamera/internal/media_device.h\"\n \n+#include \"vimc_sensor_test.h\"\n+\n #include \"v4l2_videodevice_test.h\"\n \n using namespace std;\n@@ -61,7 +63,7 @@ int V4L2VideoDeviceTest::init()\n \t\treturn TestFail;\n \n \tif (driver_ == \"vimc\") {\n-\t\tsensor_ = new CameraSensor(media_->getEntityByName(\"Sensor A\"));\n+\t\tsensor_ = new TestVimcCameraSensor(media_->getEntityByName(\"Sensor A\"));\n \t\tif (sensor_->init())\n \t\t\treturn TestFail;\n \n","prefixes":["libcamera-devel","v6","5/9"]}