[libcamera-devel,v2,1/3] libcamera: camera_sensor: Add model() function

Message ID 20200428181650.5071-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit 68863657530812a21073fa463987fd414e1236bb
Headers show
Series
  • [libcamera-devel,v2,1/3] libcamera: camera_sensor: Add model() function
Related show

Commit Message

Laurent Pinchart April 28, 2020, 6:16 p.m. UTC
Add a new model() function to the CameraSensor class to report the
camera sensor model.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
Changes since v1:

- Improve heuristics to extract the sensor model
- Cache the model in the CameraSensorClass
---
 src/libcamera/camera_sensor.cpp       | 40 +++++++++++++++++++++++++++
 src/libcamera/include/camera_sensor.h |  2 ++
 2 files changed, 42 insertions(+)

Comments

Jacopo Mondi April 28, 2020, 8:09 p.m. UTC | #1
Hi Laurent,

On Tue, Apr 28, 2020 at 09:16:48PM +0300, Laurent Pinchart wrote:
> Add a new model() function to the CameraSensor class to report the
> camera sensor model.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
> Changes since v1:
>
> - Improve heuristics to extract the sensor model
> - Cache the model in the CameraSensorClass
> ---
>  src/libcamera/camera_sensor.cpp       | 40 +++++++++++++++++++++++++++
>  src/libcamera/include/camera_sensor.h |  2 ++
>  2 files changed, 42 insertions(+)
>
> diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
> index 70fd5feaae3b..4d1082105504 100644
> --- a/src/libcamera/camera_sensor.cpp
> +++ b/src/libcamera/camera_sensor.cpp
> @@ -12,6 +12,7 @@
>  #include <iomanip>
>  #include <limits.h>
>  #include <math.h>
> +#include <regex>
>
>  #include <libcamera/property_ids.h>
>
> @@ -87,6 +88,35 @@ int CameraSensor::init()
>  		return -EINVAL;
>  	}
>
> +	/*
> +	 * Extract the camera sensor model name from the media entity name.
> +	 *
> +	 * There is no standardized naming scheme for sensor entities in the
> +	 * Linux kernel at the moment.
> +	 *
> +	 * - The most common rule, used by I2C sensors, associates the model
> +	 *   name with the I2C bus number and address (e.g. 'imx219 0-0010').
> +	 *
> +	 * - When the sensor exposes multiple subdevs, the model name is
> +	 *   usually followed by a function name, as in the smiapp driver (e.g.
> +	 *   'jt8ew9 pixel_array 0-0010').

This will then be "jt8ew9 pixel_array". I like it!

Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
  j

> +	 *
> +	 * - The vimc driver names its sensors 'Sensor A' and 'Sensor B'.
> +	 *
> +	 * Other schemes probably exist. As a best effort heuristic, use the
> +	 * part of the entity name before the first space if the name contains
> +	 * an I2C address, and use the full entity name otherwise.
> +	 */
> +	std::string entityName = entity_->name();
> +	std::regex i2cRegex{ " [0-9]+-[0-9a-f]{4}" };
> +	std::smatch match;
> +
> +	if (std::regex_search(entityName, match, i2cRegex))
> +		model_ = entityName.substr(0, entityName.find(' '));
> +	else
> +		model_ = entityName;
> +
> +	/* Open the subdev. */
>  	ret = subdev_->open();
>  	if (ret < 0)
>  		return ret;
> @@ -163,6 +193,16 @@ int CameraSensor::init()
>  	return 0;
>  }
>
> +/**
> + * \fn CameraSensor::model()
> + * \brief Retrieve the sensor model name
> + *
> + * The sensor model name is a free-formed string that uniquely identifies the
> + * sensor model.
> + *
> + * \return The sensor model name
> + */
> +
>  /**
>   * \fn CameraSensor::entity()
>   * \brief Retrieve the sensor media entity
> diff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h
> index 5277f7f7fe87..4524661be8ce 100644
> --- a/src/libcamera/include/camera_sensor.h
> +++ b/src/libcamera/include/camera_sensor.h
> @@ -33,6 +33,7 @@ public:
>
>  	int init();
>
> +	const std::string &model() const { return model_; }
>  	const MediaEntity *entity() const { return entity_; }
>  	const std::vector<unsigned int> &mbusCodes() const { return mbusCodes_; }
>  	const std::vector<Size> &sizes() const { return sizes_; }
> @@ -54,6 +55,7 @@ protected:
>  private:
>  	const MediaEntity *entity_;
>  	V4L2Subdevice *subdev_;
> +	std::string model_;
>
>  	std::vector<unsigned int> mbusCodes_;
>  	std::vector<Size> sizes_;
> --
> Regards,
>
> Laurent Pinchart
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
index 70fd5feaae3b..4d1082105504 100644
--- a/src/libcamera/camera_sensor.cpp
+++ b/src/libcamera/camera_sensor.cpp
@@ -12,6 +12,7 @@ 
 #include <iomanip>
 #include <limits.h>
 #include <math.h>
+#include <regex>
 
 #include <libcamera/property_ids.h>
 
@@ -87,6 +88,35 @@  int CameraSensor::init()
 		return -EINVAL;
 	}
 
+	/*
+	 * Extract the camera sensor model name from the media entity name.
+	 *
+	 * There is no standardized naming scheme for sensor entities in the
+	 * Linux kernel at the moment.
+	 *
+	 * - The most common rule, used by I2C sensors, associates the model
+	 *   name with the I2C bus number and address (e.g. 'imx219 0-0010').
+	 *
+	 * - When the sensor exposes multiple subdevs, the model name is
+	 *   usually followed by a function name, as in the smiapp driver (e.g.
+	 *   'jt8ew9 pixel_array 0-0010').
+	 *
+	 * - The vimc driver names its sensors 'Sensor A' and 'Sensor B'.
+	 *
+	 * Other schemes probably exist. As a best effort heuristic, use the
+	 * part of the entity name before the first space if the name contains
+	 * an I2C address, and use the full entity name otherwise.
+	 */
+	std::string entityName = entity_->name();
+	std::regex i2cRegex{ " [0-9]+-[0-9a-f]{4}" };
+	std::smatch match;
+
+	if (std::regex_search(entityName, match, i2cRegex))
+		model_ = entityName.substr(0, entityName.find(' '));
+	else
+		model_ = entityName;
+
+	/* Open the subdev. */
 	ret = subdev_->open();
 	if (ret < 0)
 		return ret;
@@ -163,6 +193,16 @@  int CameraSensor::init()
 	return 0;
 }
 
+/**
+ * \fn CameraSensor::model()
+ * \brief Retrieve the sensor model name
+ *
+ * The sensor model name is a free-formed string that uniquely identifies the
+ * sensor model.
+ *
+ * \return The sensor model name
+ */
+
 /**
  * \fn CameraSensor::entity()
  * \brief Retrieve the sensor media entity
diff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h
index 5277f7f7fe87..4524661be8ce 100644
--- a/src/libcamera/include/camera_sensor.h
+++ b/src/libcamera/include/camera_sensor.h
@@ -33,6 +33,7 @@  public:
 
 	int init();
 
+	const std::string &model() const { return model_; }
 	const MediaEntity *entity() const { return entity_; }
 	const std::vector<unsigned int> &mbusCodes() const { return mbusCodes_; }
 	const std::vector<Size> &sizes() const { return sizes_; }
@@ -54,6 +55,7 @@  protected:
 private:
 	const MediaEntity *entity_;
 	V4L2Subdevice *subdev_;
+	std::string model_;
 
 	std::vector<unsigned int> mbusCodes_;
 	std::vector<Size> sizes_;