From patchwork Fri Oct 18 07:57:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 21666 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 93D37C32FC for ; Fri, 18 Oct 2024 07:59:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 272086538E; Fri, 18 Oct 2024 09:59:56 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="caWPpEPm"; dkim-atps=neutral Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DB49E65390 for ; Fri, 18 Oct 2024 09:59:51 +0200 (CEST) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-71e5a62031aso1259884b3a.1 for ; Fri, 18 Oct 2024 00:59:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1729238389; x=1729843189; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N2LcTi4jGw13ltNq1rVNh6Kk02jZAT/tlEseKphVCtM=; b=caWPpEPmPnpQd9+TP/jnXbTh+8tq1e/k5KAChwg7nnEC/1tQnvvXl6JsOW0U8gTUym +9G3SjNoBSyjmdUQFWNYRXT5DeSNzsdpa2GMC8YtREmkZ6VtbRJrjXBXULv3EFxCqMUC fofIIPvesazAL1ZFr5HESWXChzHMujrPynxss= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729238389; x=1729843189; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N2LcTi4jGw13ltNq1rVNh6Kk02jZAT/tlEseKphVCtM=; b=RAcus9G1bzS+FY4n3xHKe0Kzg0YYt7FBTHDqxUXFZBmCM+gRHZVrXK+IsIiN/XqJIo RhNZ6vfCXjc94bJdxLC2nKWp3pu+pLzokqeYoL+aIjjPCJmWCuysAetnJ+YWOA6WER4c w6XNSt3/y8IyXh/IUBPjYIih5PBykZnM++a5cwCG4+IotutmN4HsBlaQGZTkP2sBmxrm bGd79FC7ZoGHXg9eTzwSuZG7fjVWx8WdQ4iKNlg1Suxox4piH9bn8ZnPW/9O9mFIm/KQ mxUfFrrZN86KVyKs02N5s//UaBUskFfpGafiIAg0B3nBnKEj85wgdo4AOCs+u+hUwsNp yLNA== X-Gm-Message-State: AOJu0YynqKkwhFR+8Zpk4f3Vk6JSaVHMw9bjYmXTqiDRHlGOV4cCQqze 46d5ZYmmwPYWyV+H0oqXYsIQbfTwjqsYVlrmbXyl0YzPh2pQDgxNms0xoRKodKCxXFMC4AmqDd4 = X-Google-Smtp-Source: AGHT+IHDqqE8uPioHbgeggKX8eCwKFk4GN6MkMjTJUJOB4r+yH8oAGUKP6+/LlZiFuambgEaw7j06A== X-Received: by 2002:a05:6a00:17a9:b0:71e:21:d2d8 with SMTP id d2e1a72fcca58-71ea314b973mr2552732b3a.7.1729238389468; Fri, 18 Oct 2024 00:59:49 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ea345b0c3sm901274b3a.172.2024.10.18.00.59.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 00:59:49 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Han-Lin Chen Subject: [PATCH 1/4] libcamera: Add signal disconnected for IPC Date: Fri, 18 Oct 2024 07:57:34 +0000 Message-ID: <20241018075942.1150378-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241018075942.1150378-1-chenghaoyang@chromium.org> References: <20241018075942.1150378-1-chenghaoyang@chromium.org> MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This CL also adds an API in Camera::Private to trigger Camera::disconnected signal. Signed-off-by: Harvey Yang Co-developed-by: Han-Lin Chen Signed-off-by: Han-Lin Chen --- include/libcamera/internal/camera.h | 2 ++ .../libcamera/internal/ipc_pipe_unixsocket.h | 2 ++ include/libcamera/internal/ipc_unixsocket.h | 2 ++ src/libcamera/camera.cpp | 10 ++++++++ src/libcamera/ipc_pipe_unixsocket.cpp | 8 +++++++ src/libcamera/ipc_unixsocket.cpp | 24 +++++++++++++++++-- .../module_ipa_proxy.cpp.tmpl | 8 +++++++ .../module_ipa_proxy.h.tmpl | 2 ++ 8 files changed, 56 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/camera.h b/include/libcamera/internal/camera.h index 0add0428b..0bef0980e 100644 --- a/include/libcamera/internal/camera.h +++ b/include/libcamera/internal/camera.h @@ -33,6 +33,8 @@ public: PipelineHandler *pipe() { return pipe_.get(); } + void notifyDisconnection(); + std::list queuedRequests_; ControlInfoMap controlInfo_; ControlList properties_; diff --git a/include/libcamera/internal/ipc_pipe_unixsocket.h b/include/libcamera/internal/ipc_pipe_unixsocket.h index 8c972613f..09f65b102 100644 --- a/include/libcamera/internal/ipc_pipe_unixsocket.h +++ b/include/libcamera/internal/ipc_pipe_unixsocket.h @@ -28,6 +28,8 @@ public: int sendAsync(const IPCMessage &data) override; + Signal<> *disconnected(); + private: struct CallData { IPCUnixSocket::Payload *response; diff --git a/include/libcamera/internal/ipc_unixsocket.h b/include/libcamera/internal/ipc_unixsocket.h index 48bb7a942..a1e326b6b 100644 --- a/include/libcamera/internal/ipc_unixsocket.h +++ b/include/libcamera/internal/ipc_unixsocket.h @@ -39,6 +39,8 @@ public: Signal<> readyRead; + Signal<> disconnected; + private: struct Header { uint32_t data; diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index a86f552a4..ef5a6725f 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -603,6 +603,16 @@ Camera::Private::~Private() * \return The pipeline handler that created this camera */ +/** + * \brief Notify the application that camera is disconnected with signal + * Camera::disconnected + */ +void Camera::Private::notifyDisconnection() +{ + Camera *o = LIBCAMERA_O_PTR(); + o->disconnected.emit(); +} + /** * \fn Camera::Private::validator() * \brief Retrieve the control validator related to this camera diff --git a/src/libcamera/ipc_pipe_unixsocket.cpp b/src/libcamera/ipc_pipe_unixsocket.cpp index 668ec73b9..51fd3b1fb 100644 --- a/src/libcamera/ipc_pipe_unixsocket.cpp +++ b/src/libcamera/ipc_pipe_unixsocket.cpp @@ -84,6 +84,14 @@ int IPCPipeUnixSocket::sendAsync(const IPCMessage &data) return 0; } +Signal<> *IPCPipeUnixSocket::disconnected() +{ + if (socket_) + return &socket_->disconnected; + + return nullptr; +} + void IPCPipeUnixSocket::readyRead() { IPCUnixSocket::Payload payload; diff --git a/src/libcamera/ipc_unixsocket.cpp b/src/libcamera/ipc_unixsocket.cpp index 002053e35..3d0248857 100644 --- a/src/libcamera/ipc_unixsocket.cpp +++ b/src/libcamera/ipc_unixsocket.cpp @@ -186,11 +186,16 @@ int IPCUnixSocket::send(const Payload &payload) if (!hdr.data && !hdr.fds) return -EINVAL; - ret = ::send(fd_.get(), &hdr, sizeof(hdr), 0); + ret = ::send(fd_.get(), &hdr, sizeof(hdr), MSG_NOSIGNAL); if (ret < 0) { ret = -errno; LOG(IPCUnixSocket, Error) << "Failed to send: " << strerror(-ret); + if (errno == ECONNRESET) { + disconnected.emit(); + fd_.reset(); + } + return ret; } @@ -241,6 +246,11 @@ int IPCUnixSocket::receive(Payload *payload) * \brief A Signal emitted when a message is ready to be read */ +/** + * \var IPCUnixSocket::disconnected + * \brief A Signal emitted when the Unix socket IPC is disconnected + */ + int IPCUnixSocket::sendData(const void *buffer, size_t length, const int32_t *fds, unsigned int num) { @@ -266,10 +276,15 @@ int IPCUnixSocket::sendData(const void *buffer, size_t length, if (fds) memcpy(CMSG_DATA(cmsg), fds, num * sizeof(uint32_t)); - if (sendmsg(fd_.get(), &msg, 0) < 0) { + if (sendmsg(fd_.get(), &msg, MSG_NOSIGNAL) < 0) { int ret = -errno; LOG(IPCUnixSocket, Error) << "Failed to sendmsg: " << strerror(-ret); + if (errno == ECONNRESET) { + disconnected.emit(); + fd_.reset(); + } + return ret; } @@ -324,6 +339,11 @@ void IPCUnixSocket::dataNotifier() ret = -errno; LOG(IPCUnixSocket, Error) << "Failed to receive header: " << strerror(-ret); + if (errno == ECONNRESET) { + disconnected.emit(); + fd_.reset(); + } + return; } diff --git a/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl b/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl index ce3cc5ab6..27f03417a 100644 --- a/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl +++ b/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl @@ -104,6 +104,14 @@ namespace {{ns}} { } } +Signal<> *{{proxy_name}}::disconnected() +{ + if (ipc_) + return ipc_->disconnected(); + + return nullptr; +} + {% if interface_event.methods|length > 0 %} void {{proxy_name}}::recvMessage(const IPCMessage &data) { diff --git a/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.h.tmpl b/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.h.tmpl index e213b18a0..2b7ba872e 100644 --- a/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.h.tmpl +++ b/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.h.tmpl @@ -53,6 +53,8 @@ public: > {{method.mojom_name}}; {% endfor %} + Signal<> *disconnected(); + private: void recvMessage(const IPCMessage &data);