[{"id":11882,"web_url":"https://patchwork.libcamera.org/comment/11882/","msgid":"<20200805133407.GC6751@pendragon.ideasonboard.com>","date":"2020-08-05T13:34:07","subject":"Re: [libcamera-devel] [PATCH v8 4/9] libcamera: camera_sensor:\n\tGenerate a sensor ID","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Wed, Aug 05, 2020 at 12:48:55PM +0200, Niklas Söderlund wrote:\n> The ID is generated from information in the firmware description of the\n> sensor if available or from module and model information if the sensor\n> is virtual (for example VIMC).\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n> * Changes sinve v7\n> - Use device path stripped of /sys/devices/ prefix instead of modalias\n>   for virtual sensors.\n> \n> * Changes sinve v6\n> - Do not allow for subclassing\n> - Add support for virtual sensors\n> - Use new sysfs:: helper.\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            | 37 ++++++++++++++++++++++\n>  2 files changed, 40 insertions(+)\n> \n> diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\n> index 06c8292ca30129de..0f65fca46877e89a 100644\n> --- a/include/libcamera/internal/camera_sensor.h\n> +++ b/include/libcamera/internal/camera_sensor.h\n> @@ -67,11 +67,14 @@ protected:\n>  \tstd::string logPrefix() const override;\n>  \n>  private:\n> +\tint generateId();\n> +\n>  \tconst MediaEntity *entity_;\n>  \tstd::unique_ptr<V4L2Subdevice> subdev_;\n>  \tunsigned int pad_;\n>  \n>  \tstd::string model_;\n> +\tstd::string id_;\n>  \n>  \tV4L2Subdevice::Formats formats_;\n>  \tSize resolution_;\n> diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\n> index 350f49accad99c7b..695c4ad0028d5c4c 100644\n> --- a/src/libcamera/camera_sensor.cpp\n> +++ b/src/libcamera/camera_sensor.cpp\n> @@ -9,6 +9,7 @@\n>  \n>  #include <algorithm>\n>  #include <float.h>\n> +#include <fstream>\n\nDo you still need this ?\n\n>  #include <iomanip>\n>  #include <limits.h>\n>  #include <math.h>\n> @@ -16,7 +17,9 @@\n>  \n>  #include <libcamera/property_ids.h>\n>  \n> +#include \"libcamera/internal/file.h\"\n>  #include \"libcamera/internal/formats.h\"\n> +#include \"libcamera/internal/sysfs.h\"\n>  #include \"libcamera/internal/utils.h\"\n>  \n>  /**\n> @@ -204,6 +207,11 @@ int CameraSensor::init()\n>  \tif (ret < 0)\n>  \t\treturn ret;\n>  \n> +\t/* Generate a unique ID for the sensor. */\n> +\tret = generateId();\n> +\tif (ret)\n> +\t\treturn ret;\n> +\n>  \t/* Retrieve and store the camera sensor properties. */\n>  \tconst ControlInfoMap &controls = subdev_->controls();\n>  \tint32_t propertyValue;\n> @@ -541,4 +549,33 @@ std::string CameraSensor::logPrefix() const\n>  \treturn \"'\" + entity_->name() + \"'\";\n>  }\n>  \n> +int CameraSensor::generateId()\n> +{\n> +\tconst std::string devPath = subdev_->devicePath();\n> +\n> +\t/* Try to get ID from firmware description. */\n> +\tid_ = sysfs::firmwareNodePath(devPath);\n> +\tif (!id_.empty())\n> +\t\treturn 0;\n> +\n> +\t/*\n> +\t * Virtual sensors not described in firmware\n> +\t *\n> +\t * Verify it's a platform device and construct ID from the deive path\n> +\t * and model of sensor.\n> +\t */\n> +\tif (devPath.rfind(\"/sys/devices/platform/\", 0) == 0) {\n\ns/rfind/find/ as the match will occur at position 0 in most cases.\n\nShame https://en.cppreference.com/w/cpp/string/basic_string/starts_with\nwas only introduced in C++20. We could add it to utils:: if needed.\n\n> +\t\tid_ = devPath + \" \" + model();\n> +\n> +\t\tstatic const std::string dropStr = \"/sys/devices/\";\n> +\t\tif (id_.find(dropStr) == 0)\n\nGiven that id_ is guaranteed to start with \"/sys/devices/platform/\", I\nthink you can drop this check :-)\n\n> +\t\t\tid_.erase(0, dropStr.length());\n\n\t\tid_ = devPath.substr(strlen(\"/sys/devices/\")) + \" \" + model();\n\n(you'll need to include string.h)\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\n> +\t\treturn 0;\n> +\t}\n> +\n> +\tLOG(CameraSensor, Error) << \"Can't generate sensor ID\";\n> +\treturn -EINVAL;\n> +}\n> +\n>  } /* namespace libcamera */","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 751BDBD87A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  5 Aug 2020 13:34:24 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 10085605A8;\n\tWed,  5 Aug 2020 15:34:24 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 633CA6039D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  5 Aug 2020 15:34:22 +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 ADD6E2C0;\n\tWed,  5 Aug 2020 15:34:18 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"n9xVBqP0\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1596634461;\n\tbh=DppSdkD0k+/JVxtUV+u3jLjrp7Ta7iGutldft5GrSGw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=n9xVBqP04Hz9HkfLkSW/2xRTQKQLeYOM7VttWxVTFvwwixBZfl504glpZkZeQYoaF\n\tcJc8BYm1Vw9xGIaHqmTS0GXL12h65WbCfl305uLrQwazEOTB3yDBV2blBXskx99zcF\n\tUBGUHuFCIf6j17VZJgZVbZTjNMvnwPQ9Ghawrsqc=","Date":"Wed, 5 Aug 2020 16:34:07 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Message-ID":"<20200805133407.GC6751@pendragon.ideasonboard.com>","References":"<20200805104900.2172763-1-niklas.soderlund@ragnatech.se>\n\t<20200805104900.2172763-5-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200805104900.2172763-5-niklas.soderlund@ragnatech.se>","Subject":"Re: [libcamera-devel] [PATCH v8 4/9] libcamera: camera_sensor:\n\tGenerate a 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>","Cc":"libcamera-devel@lists.libcamera.org","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>"}}]