[{"id":2761,"web_url":"https://patchwork.libcamera.org/comment/2761/","msgid":"<20191003195101.GP1322@bigcity.dyn.berto.se>","date":"2019-10-03T19:51:01","subject":"Re: [libcamera-devel] [PATCH v2 13/13] libcamera: controls: Use\n\tControlValidator to validate ControlList","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your patch.\n\nOn 2019-09-29 22:02:54 +0300, Laurent Pinchart wrote:\n> Replace the manual validation of controls against a Camera with usage of\n> the new ControlValidator interface.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  include/libcamera/controls.h   |  6 +++---\n>  include/libcamera/request.h    |  7 ++++---\n>  src/libcamera/controls.cpp     | 27 ++++++++++-----------------\n>  src/libcamera/request.cpp      | 14 ++++++++++++--\n>  test/controls/control_list.cpp | 15 ++++++++++++++-\n>  5 files changed, 43 insertions(+), 26 deletions(-)\n> \n> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\n> index d3eea643c0ec..90426753f40f 100644\n> --- a/include/libcamera/controls.h\n> +++ b/include/libcamera/controls.h\n> @@ -13,7 +13,7 @@\n>  \n>  namespace libcamera {\n>  \n> -class Camera;\n> +class ControlValidator;\n>  \n>  enum ControlType {\n>  \tControlTypeNone,\n> @@ -119,7 +119,7 @@ private:\n>  \tusing ControlListMap = std::unordered_map<const ControlId *, ControlValue>;\n>  \n>  public:\n> -\tControlList(Camera *camera);\n> +\tControlList(ControlValidator *validator);\n>  \n>  \tusing iterator = ControlListMap::iterator;\n>  \tusing const_iterator = ControlListMap::const_iterator;\n> @@ -160,7 +160,7 @@ private:\n>  \tconst ControlValue *find(const ControlId &id) const;\n>  \tControlValue *find(const ControlId &id);\n>  \n> -\tCamera *camera_;\n> +\tControlValidator *validator_;\n>  \tControlListMap controls_;\n>  };\n>  \n> diff --git a/include/libcamera/request.h b/include/libcamera/request.h\n> index 9edf1cedc054..e3db5243aaf3 100644\n> --- a/include/libcamera/request.h\n> +++ b/include/libcamera/request.h\n> @@ -19,9 +19,9 @@ namespace libcamera {\n>  \n>  class Buffer;\n>  class Camera;\n> +class CameraControlValidator;\n>  class Stream;\n>  \n> -\n>  class Request\n>  {\n>  public:\n> @@ -36,7 +36,7 @@ public:\n>  \tRequest &operator=(const Request &) = delete;\n>  \t~Request();\n>  \n> -\tControlList &controls() { return controls_; }\n> +\tControlList &controls() { return *controls_; }\n>  \tconst std::map<Stream *, Buffer *> &buffers() const { return bufferMap_; }\n>  \tint addBuffer(std::unique_ptr<Buffer> buffer);\n>  \tBuffer *findBuffer(Stream *stream) const;\n> @@ -56,7 +56,8 @@ private:\n>  \tbool completeBuffer(Buffer *buffer);\n>  \n>  \tCamera *camera_;\n> -\tControlList controls_;\n> +\tCameraControlValidator *validator_;\n> +\tControlList *controls_;\n>  \tstd::map<Stream *, Buffer *> bufferMap_;\n>  \tstd::unordered_set<Buffer *> pending_;\n>  \n> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\n> index a7e9d069b31a..f3260edce0bc 100644\n> --- a/src/libcamera/controls.cpp\n> +++ b/src/libcamera/controls.cpp\n> @@ -10,8 +10,7 @@\n>  #include <sstream>\n>  #include <string>\n>  \n> -#include <libcamera/camera.h>\n> -\n> +#include \"control_validator.h\"\n>  #include \"log.h\"\n>  #include \"utils.h\"\n>  \n> @@ -365,20 +364,16 @@ std::string ControlRange::toString() const\n>   * \\class ControlList\n>   * \\brief Associate a list of ControlId with their values for a camera\n>   *\n> - * A ControlList wraps a map of ControlId to ControlValue and provide\n> - * additional validation against the control information exposed by a Camera.\n> - *\n> - * A list is only valid for as long as the camera it refers to is valid. After\n> - * that calling any method of the ControlList class other than its destructor\n> - * will cause undefined behaviour.\n> + * A ControlList wraps a map of ControlId to ControlValue and optionally\n> + * validates controls against a ControlValidator.\n>   */\n>  \n>  /**\n> - * \\brief Construct a ControlList with a reference to the Camera it applies on\n> - * \\param[in] camera The camera\n> + * \\brief Construct a ControlList with an optional control validator\n> + * \\param[in] validator The validator (may be null)\n>   */\n> -ControlList::ControlList(Camera *camera)\n> -\t: camera_(camera)\n> +ControlList::ControlList(ControlValidator *validator)\n> +\t: validator_(validator)\n>  {\n>  }\n>  \n> @@ -493,12 +488,10 @@ const ControlValue *ControlList::find(const ControlId &id) const\n>  \n>  ControlValue *ControlList::find(const ControlId &id)\n>  {\n> -\tconst ControlInfoMap &controls = camera_->controls();\n> -\tconst auto iter = controls.find(&id);\n> -\tif (iter == controls.end()) {\n> +\tif (validator_ && !validator_->validate(id)) {\n>  \t\tLOG(Controls, Error)\n> -\t\t\t<< \"Camera \" << camera_->name()\n> -\t\t\t<< \" does not support control \" << id.name();\n> +\t\t\t<< \"Control \" << id.name()\n> +\t\t\t<< \" is not valid for \" << validator_->name();\n>  \t\treturn nullptr;\n>  \t}\n>  \n> diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp\n> index ee2158fc7a9c..19f6d0b9a0ae 100644\n> --- a/src/libcamera/request.cpp\n> +++ b/src/libcamera/request.cpp\n> @@ -13,6 +13,7 @@\n>  #include <libcamera/camera.h>\n>  #include <libcamera/stream.h>\n>  \n> +#include \"camera_controls.h\"\n>  #include \"log.h\"\n>  \n>  /**\n> @@ -55,9 +56,15 @@ LOG_DEFINE_CATEGORY(Request)\n>   *\n>   */\n>  Request::Request(Camera *camera, uint64_t cookie)\n> -\t: camera_(camera), controls_(camera), cookie_(cookie),\n> -\t  status_(RequestPending), cancelled_(false)\n> +\t: camera_(camera), cookie_(cookie), status_(RequestPending),\n> +\t  cancelled_(false)\n>  {\n> +\t/**\n> +\t * \\todo Should the Camera expose a validator instance, to avoid\n> +\t * creating a new instance for each request?\n> +\t */\n> +\tvalidator_ = new CameraControlValidator(camera);\n> +\tcontrols_ = new ControlList(validator_);\n>  }\n>  \n>  Request::~Request()\n> @@ -66,6 +73,9 @@ Request::~Request()\n>  \t\tBuffer *buffer = it.second;\n>  \t\tdelete buffer;\n>  \t}\n> +\n> +\tdelete controls_;\n> +\tdelete validator_;\n>  }\n>  \n>  /**\n> diff --git a/test/controls/control_list.cpp b/test/controls/control_list.cpp\n> index 8469ecf09439..1bcfecc467b5 100644\n> --- a/test/controls/control_list.cpp\n> +++ b/test/controls/control_list.cpp\n> @@ -12,6 +12,7 @@\n>  #include <libcamera/control_ids.h>\n>  #include <libcamera/controls.h>\n>  \n> +#include \"camera_controls.h\"\n>  #include \"test.h\"\n>  \n>  using namespace std;\n> @@ -40,7 +41,8 @@ protected:\n>  \n>  \tint run()\n>  \t{\n> -\t\tControlList list(camera_.get());\n> +\t\tCameraControlValidator validator(camera_.get());\n> +\t\tControlList list(&validator);\n>  \n>  \t\t/* Test that the list is initially empty. */\n>  \t\tif (!list.empty()) {\n> @@ -141,6 +143,17 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> +\t\t/*\n> +\t\t * Attempt to set an invalid control and verify that the\n> +\t\t * operation failed.\n> +\t\t */\n> +\t\tlist.set(controls::AwbEnable, true);\n> +\n> +\t\tif (list.contains(controls::AwbEnable)) {\n> +\t\t\tcout << \"List shouldn't contain AwbEnable control\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n>  \t\treturn TestPass;\n>  \t}\n>  \n> -- \n> Regards,\n> \n> Laurent Pinchart\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-lj1-x230.google.com (mail-lj1-x230.google.com\n\t[IPv6:2a00:1450:4864:20::230])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 077F360BE8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  3 Oct 2019 21:51:03 +0200 (CEST)","by mail-lj1-x230.google.com with SMTP id q64so4065281ljb.12\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 03 Oct 2019 12:51:02 -0700 (PDT)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\tt6sm736130ljd.102.2019.10.03.12.51.01\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 03 Oct 2019 12:51:01 -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=dCfgNLMo4FtiiJmpxb66jMhsorZ9lzqOhGX+0vVBlQA=;\n\tb=aHeQ7h3tFfoMHjzi00jQE+pLYHPRRhlbtsCBPUlmAMwA5ApG4KAApqfBQWtSiVUmWh\n\twggc1qDAr9ddeb8WZiz8XN5PF5+4kMtobb+s9odWhqye8g6Qm3Y6IfQR1nfFfAFmd9uO\n\tSp1nkurUqPjJL7VZG6NPdQ/QkL6+Gh1fSTTU82jwYDYwztu4NpBUHuzGbsBhz6+FHAVY\n\tlxT8go4ioEzA0BbdRyFwT15D1WT+c7Na96DEnFIyDbLag+teenYOrQiVM6JECwVvVMFR\n\tWV/dU97/7NJcyYkS9ehkcD3kuZH2WPy4iyOO/IU2Z6zOqHccSXv4NtaW4f4QrpetKsS7\n\twlww==","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=dCfgNLMo4FtiiJmpxb66jMhsorZ9lzqOhGX+0vVBlQA=;\n\tb=CUdFsQvBg8jQMDRL7HOqwFIiz0mh87Tmj01R0Eg2KT9qfvJquSgRfo10SV7UmpwZvX\n\tGrHq6fiWtwKvBRMfmK3PiQOdyiJOQloWi9JZktnhXGM6cu8uDs89LVHaynuzD5iyWcVw\n\tojriTLFDtRZOVGTtUyg92XyuhGW9i3gOj1Vmm05CJUtGPBDGbBBwDaNp/XZcMAT2RA3L\n\t8gvQbNJFUjsb+N4X2/L8PzN1HSED5fk5oWi3A4kMSosqyKQMm1NNHPjlO7xa6YnPyrtn\n\tlNc5spwMRapg9Tb5gZfwxNMvE3BWGoxaL2ZOpCfHd4UqAt4mTLIqqsLJED8G/Y6gqvO2\n\tKPMg==","X-Gm-Message-State":"APjAAAUMhh4SVxSb5t//85hApQC6vKbDy2S7EaB3b2nvl5RKpl1UvMuj\n\tonj5R/ZU7JaFy98tMvqUl3RnW1XwUJg=","X-Google-Smtp-Source":"APXvYqxZXnqGGTpde8R+odCncq9atb3R3xEm7p3wBOZhaIUSVrcLJt1FYLz66AzxfDn9dYEgKsE+vA==","X-Received":"by 2002:a2e:864a:: with SMTP id\n\ti10mr4570031ljj.245.1570132262322; \n\tThu, 03 Oct 2019 12:51:02 -0700 (PDT)","Date":"Thu, 3 Oct 2019 21:51:01 +0200","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191003195101.GP1322@bigcity.dyn.berto.se>","References":"<20190929190254.18920-1-laurent.pinchart@ideasonboard.com>\n\t<20190929190254.18920-14-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190929190254.18920-14-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [libcamera-devel] [PATCH v2 13/13] libcamera: controls: Use\n\tControlValidator to validate ControlList","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 19:51:03 -0000"}}]