From patchwork Fri Apr 23 14:24:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= X-Patchwork-Id: 12102 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id BDFB4BDB15 for ; Fri, 23 Apr 2021 14:25:13 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 310B86887C; Fri, 23 Apr 2021 16:25:13 +0200 (CEST) Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3F78F6885A for ; Fri, 23 Apr 2021 16:25:11 +0200 (CEST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: nfraprado) with ESMTPSA id 841B21F43CDB From: =?utf-8?b?TsOtY29sYXMgRi4gUi4gQS4gUHJhZG8=?= To: libcamera-devel@lists.libcamera.org Date: Fri, 23 Apr 2021 11:24:12 -0300 Message-Id: <20210423142412.460460-1-nfraprado@collabora.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: camera: Make stop() idempotent 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: , Cc: kernel@collabora.com, =?utf-8?q?Andr=C3=A9_Almeida?= Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Make Camera::stop() idempotent so that it can be called in any state and consecutive times. When called in any state other than CameraRunning, it is a no-op. This simplifies the cleanup path for applications. Signed-off-by: NĂ­colas F. R. A. Prado --- This will be used to simplify the cleanup path in lc-compliance without having error messages [1]. Also, I'm not sure if I should add the silent parameter to the other isAccessAllowed variant as well. It would make sense for consistency's sake, but it's not needed currently. [1] https://lists.libcamera.org/pipermail/libcamera-devel/2021-April/019703.html src/libcamera/camera.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 763f3b9926fd..baffdafc8146 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -350,7 +350,7 @@ public: int isAccessAllowed(State state, bool allowDisconnected = false, const char *from = __builtin_FUNCTION()) const; int isAccessAllowed(State low, State high, - bool allowDisconnected = false, + bool allowDisconnected = false, bool silent = false, const char *from = __builtin_FUNCTION()) const; void disconnect(); @@ -408,7 +408,7 @@ int Camera::Private::isAccessAllowed(State state, bool allowDisconnected, } int Camera::Private::isAccessAllowed(State low, State high, - bool allowDisconnected, + bool allowDisconnected, bool silent, const char *from) const { if (!allowDisconnected && disconnected_) @@ -421,11 +421,12 @@ int Camera::Private::isAccessAllowed(State low, State high, ASSERT(static_cast(low) < std::size(camera_state_names) && static_cast(high) < std::size(camera_state_names)); - LOG(Camera, Error) << "Camera in " << camera_state_names[currentState] - << " state trying " << from - << "() requiring state between " - << camera_state_names[low] << " and " - << camera_state_names[high]; + if (!silent) + LOG(Camera, Error) << "Camera in " << camera_state_names[currentState] + << " state trying " << from + << "() requiring state between " + << camera_state_names[low] << " and " + << camera_state_names[high]; return -EACCES; } @@ -1061,9 +1062,10 @@ int Camera::start(const ControlList *controls) * This method stops capturing and processing requests immediately. All pending * requests are cancelled and complete synchronously in an error state. * - * \context This function may only be called when the camera is in the Running - * state as defined in \ref camera_operation, and shall be synchronized by the - * caller with other functions that affect the camera state. + * \context This function may be called even if the camera isn't in the Running + * state as defined in \ref camera_operation, in which cases it is a no-op. It + * shall be synchronized by the caller with other functions that affect the + * camera state. * * \return 0 on success or a negative error code otherwise * \retval -ENODEV The camera has been disconnected from the system @@ -1073,7 +1075,12 @@ int Camera::stop() { Private *const d = LIBCAMERA_D_PTR(); - int ret = d->isAccessAllowed(Private::CameraRunning); + int ret = d->isAccessAllowed(Private::CameraAvailable, + Private::CameraStopping, false, true); + if (!ret) + return 0; + + ret = d->isAccessAllowed(Private::CameraRunning); if (ret < 0) return ret;