From patchwork Fri May 24 17:11:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 13673421 Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D2C712E1DE for ; Fri, 24 May 2024 17:11:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716570712; cv=none; b=StkO3B46c+0PW9tpWUZjWbsvAggPXRpdaqRRePpIIqPFFf1/MAHbzaaZmtvbL+ORI48ErunsFNSU70CyMcTKbFHtysXgrhcszZv01N17aEjQCbVu4XSVpiSFdadASVVfEgQ4ejt/0PsMjlhl6EWGYUroVVOWzCV6t3QADIRd/Co= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716570712; c=relaxed/simple; bh=U3JXHJoXfxXplytEMZ36wZ4tJSsPc/8Q27PTauh6eWU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=eQ1eAqIkqSZ+dV9yh08CUqmfpfOxsVDTRSthoh7fDlmb6uVtE/yB21wXcf8o3SnRBLDI+mtPCEO9DUxb+cm/wbYzpPpGu8QIbXFgpmQED/z4i24Z9szcRjz1qztdCtFWGyUulxFXyYmZUfcNXgMk9Kny/qRTa4qfUxtwTbEn8YM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Z6imXdsX; arc=none smtp.client-ip=209.85.160.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z6imXdsX" Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-24c9f297524so652966fac.0 for ; Fri, 24 May 2024 10:11:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716570709; x=1717175509; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=9B5sTBsDV/euPRNapmwxQ9v3fRRczY8l/+j7l6rxQ+I=; b=Z6imXdsXJ5+SvE1cVsusl1OSSrW0bCOsH7BrugrTJ85Xd+moFeKxw6zzKLrHPPOaAv aWtnhceoN0fAwP6co3MsswKTpUfFWQkoykwLsRwX3Ueg3C7w5VtwR6/gr4GGgcUm2XtD MSrKE4nF+i4M7qzmKPn3HVlAvd6JBQtF0VHw7hr//rl3C4q/fCpy+DYtNKLcB/Lvia9T 7ZA4nJUKXsMCA35w2fMW7fp4LGMBniI95p2hfPKFSSnGdcgX0CVEXGM9NnwwK2fFncmN BtCxD4VNF7Jh/ZDJzNsIxeKmNjN+QABrFH7ejslApjjn9Jyc2jCS/bU7NF0PqW4gT6AE U/rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716570709; x=1717175509; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9B5sTBsDV/euPRNapmwxQ9v3fRRczY8l/+j7l6rxQ+I=; b=Xm3LML+a3uZ6k8v+wO9aNfd8n27MONtWAWJaZRLG9Zo1PvTziluPI71zgrfRhmxDq0 jiXaThSQE20jODzZozNetQVhlcVLViW9hrZH8xV+mPOG/hlqCk/uB57hYCwkakkSma6e 1gSx9geIwPO5MNeZyZficSc2mqCeU3V4wGBVYkfJwnQ3G4q3rV/z/kxsRfvWyG1ploOV 0npmpx4/7X+IbwMPSLNkBMbTDD23Dt3IHz0k9TFaAjK4vXaJOvBxKg9CImJvJK28MRHx SsTuxDmT8nm0l9XQOpCqfYvuV/T+VbaypShNHy9jxnBXxUexj63c0FfVTB94owv0gV+I 7bdQ== X-Gm-Message-State: AOJu0Yx2MXo96ApbIlcrmxjTYLItDljeAhkuNODPwWJMgd/HLf3orKVz 6gS9Nn6mbOWcL6Q+N+9NcTrbaMJ9iFi27c9TwZlR3JRqIS6iQQt/MzgWPQ== X-Google-Smtp-Source: AGHT+IH7aQ8J+HNTgruVsagONJcmw51TAOLwrfaC6OfW3oHjuNync9r/pXOODHF5mbDjE14iATWdBA== X-Received: by 2002:a05:6871:80d:b0:233:5570:a2c6 with SMTP id 586e51a60fabf-24ca1170593mr3222990fac.12.1716570709399; Fri, 24 May 2024 10:11:49 -0700 (PDT) Received: from lvondent-mobl4.. (syn-107-146-107-067.res.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-804cc1158ebsm256676241.29.2024.05.24.10.11.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 10:11:48 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v1 1/3] shared/csip: Fix memory leak Date: Fri, 24 May 2024 13:11:45 -0400 Message-ID: <20240524171147.2733570-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.45.1 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Luiz Augusto von Dentz This fixes the following leak: 102 bytes in 6 blocks are definitely lost in loss record 660 of 909 at 0x484282F: malloc (vg_replace_malloc.c:446) by 0x5A078B: util_malloc (util.c:46) by 0x649162: read_sirk (csip.c:485) by 0x5C74FA: read_cb (gatt-client.c:2713) by 0x5C4137: handle_rsp (att.c:880) by 0x5C4137: can_read_data (att.c:1072) by 0x65DDA4: watch_callback (io-glib.c:157) by 0x49656AB: ??? (in /usr/lib64/libglib-2.0.so.0.8000.2) by 0x49C6707: ??? (in /usr/lib64/libglib-2.0.so.0.8000.2) by 0x496B666: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.8000.2) by 0x65FE3D: mainloop_run (mainloop-glib.c:66) by 0x6605A3: mainloop_run_with_signal (mainloop-notify.c:188) by 0x31DEFA: main (main.c:1468) --- src/shared/csip.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/shared/csip.c b/src/shared/csip.c index e13efb6ce634..87b4590d926d 100644 --- a/src/shared/csip.c +++ b/src/shared/csip.c @@ -128,6 +128,15 @@ void bt_csip_detach(struct bt_csip *csip) queue_foreach(csip_cbs, csip_detached, csip); } +static void csis_free(struct bt_csis *csis) +{ + if (!csis) + return; + + free(csis->sirk_val); + free(csis); +} + static void csip_db_free(void *data) { struct bt_csip_db *cdb = data; @@ -137,7 +146,7 @@ static void csip_db_free(void *data) gatt_db_unref(cdb->db); - free(cdb->csis); + csis_free(cdb->csis); free(cdb); } From patchwork Fri May 24 17:11:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 13673422 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93EF312E1DE for ; Fri, 24 May 2024 17:11:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716570715; cv=none; b=nrwOTgu7/B1UW3FFk71QMnejOaaN2RHNS5sfG05mJtIUCk0l+BBWsrGngEYGl513MGhhGAwe7hjqQPkEmAOktD7qMVpQ8UgvuyxCjClDYibgRDF/9bait9QwrvFHZjn4hF70DqzxJWun7BOFmD/f5+9Qo8WbAZbCCT40yw/YI0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716570715; c=relaxed/simple; bh=kkcVm0s7sXwSpFq2JNi67OCg/OZxDC4zT0wLCc8FK/8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rqd36epDegWbkSICJ6fBBk39Mk3lWfWsjfrKQsFJ75J6jLxnxNrFkKE1k1Xhod8I8z24utxatuVcwZoXp3h049v3GH2WSXczG8mpbUJ/meF7i4f0+IDAAz8FCdvADedmfhj27l8e+bC4+XGsbNIcpYt1LzIMqre+o/YHPVGHib0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hgBp2ejm; arc=none smtp.client-ip=209.85.210.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hgBp2ejm" Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-6f0ef6bee72so4445213a34.0 for ; Fri, 24 May 2024 10:11:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716570712; x=1717175512; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=j+2G4fh1U2AG7YPtyqS0H3AcZdTJpV1UkkKB/mA255g=; b=hgBp2ejmUZ/khnn+dvnQkx0iT1J1OoU06iyOouwqiPH73z1xwq3n9K9KKRZiJLXQMV sa9/MwqfCn/55UfhYMAyeu20i/wIFZMWNLoLi9CFSnwM8MEu0qwP7WBJlXkz7yvMSOJG 1uVhMtK6CekhdPICbNV0qvJx79iu/ac6WII3Ui3WP47nMLDcawOCV7nr7LTwLNO9F782 0sJvEMHfkDymF9vQnIv1Wxlxbx+tPUO7CLYUX4LZxoYx94NQ0YstH84X7J1F+eaHOfVb tGpqXJzI4TUIiWOcJfkMspX1VZ1+5uaSCuHh+3Ad+nUtp30U2hTYBMlYCJu/kwUt8/Av 2o7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716570712; x=1717175512; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j+2G4fh1U2AG7YPtyqS0H3AcZdTJpV1UkkKB/mA255g=; b=rfzhKBiSIeLUNYT3aKLdv7G8ZKzvpyrX6TR8tu06zvePshSx/g6svtG4J99jz/JAe4 evd7/k4oYPnW6YR3HNycCxr3hgzUzIY//nkAJaY/mT7/nhjgZZS6DVs3gqTmPS7JzuKj rTvgzh0mt21S+gvk25Fd9D/+EwinxsBX1/iQBHHLyn2++khQNPcsX4FXdIOn1iCiB0vW mo3dmLN7UrV3p9TnmSQX4Xu6nrHhHhyCWjp3GfYn7rNxnEdk4aCYqg42SkleMNQ4Vi+x ht5XZjwCZuMudaOtqkPMM4UjKmRaXcgUPsUp5/yDy5YcwtOSIWJBAbPM8wtryq7GMmFK xMMA== X-Gm-Message-State: AOJu0YwZ/Pi+0RQrEJOwNsJVGVCh3lgH2+UnsG0Fcay0wOGKaP944O4b 1boVyZXZaIMbpy+IpHDUHa2tLytWvXgL/qIGa646V7LWBLwir+SpNKOLLw== X-Google-Smtp-Source: AGHT+IH3ng4awri9zOLJN7ite4FDJMuuRtiT1HZXj3EpLfTzoQrKXKUbtKj/J47aJvY5ponKSh1aFQ== X-Received: by 2002:a05:6870:e2d0:b0:234:581d:28e0 with SMTP id 586e51a60fabf-24ca11d1fabmr3250655fac.10.1716570711593; Fri, 24 May 2024 10:11:51 -0700 (PDT) Received: from lvondent-mobl4.. (syn-107-146-107-067.res.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-804cc1158ebsm256676241.29.2024.05.24.10.11.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 10:11:50 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v1 2/3] shared/gatt-db: Introduce gatt_db_clone Date: Fri, 24 May 2024 13:11:46 -0400 Message-ID: <20240524171147.2733570-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240524171147.2733570-1-luiz.dentz@gmail.com> References: <20240524171147.2733570-1-luiz.dentz@gmail.com> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Luiz Augusto von Dentz This introduces gatt_db_clone which can be used to clonse/deep copy and existing database. --- src/shared/gatt-db.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/shared/gatt-db.h | 1 + 2 files changed, 43 insertions(+) diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c index 2c8e7d31eda1..28da9488fedf 100644 --- a/src/shared/gatt-db.c +++ b/src/shared/gatt-db.c @@ -260,6 +260,48 @@ struct gatt_db *gatt_db_new(void) return gatt_db_ref(db); } +static void service_clone(void *data, void *user_data) +{ + struct gatt_db_service *service = data; + struct gatt_db *db = user_data; + struct gatt_db_service *clone; + int i; + + clone = new0(struct gatt_db_service, 1); + clone->db = db; + clone->active = service->active; + clone->num_handles = service->num_handles; + clone->attributes = new0(struct gatt_db_attribute *, + service->num_handles); + + /* Clone attributes */ + for (i = 0; i < service->num_handles; i++) { + struct gatt_db_attribute *attribute = service->attributes[i]; + + clone->attributes[i] = new_attribute(clone, attribute->handle, + &attribute->uuid, + NULL, 0); + } + + queue_push_tail(db->services, clone); +} + +struct gatt_db *gatt_db_clone(struct gatt_db *db) +{ + struct gatt_db *clone; + + if (!db) + return NULL; + + clone = gatt_db_new(); + if (!clone) + return NULL; + + queue_foreach(db->services, service_clone, clone); + + return clone; +} + static void notify_destroy(void *data) { struct notify *notify = data; diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h index f7596e33529a..dc2daf7fc1ba 100644 --- a/src/shared/gatt-db.h +++ b/src/shared/gatt-db.h @@ -12,6 +12,7 @@ struct gatt_db; struct gatt_db_attribute; struct gatt_db *gatt_db_new(void); +struct gatt_db *gatt_db_clone(struct gatt_db *db); struct gatt_db *gatt_db_ref(struct gatt_db *db); void gatt_db_unref(struct gatt_db *db); From patchwork Fri May 24 17:11:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 13673423 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B004712F37F for ; Fri, 24 May 2024 17:11:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716570717; cv=none; b=iVpEU5VYvuicLYd9GMH0eSfV6lTGZIhhlv9WF825EQNHN5PRwTKXjeb5rOdOEq6JmstfDo1ct8ce2aUTNm8W3j/z6HDfl6xsgxGqlwNYcOdY/D//W+dEaibR/1qnFYuaNE2qbaO96/Dm18AvlrGvg3WQAGmSejnUQETfm2EcCiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716570717; c=relaxed/simple; bh=HXWN0zTDul6ayGihyYGbtmkCozuuUhZ9B1AzDIze3rU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Hl4YJ0j2SI8G3ejMUkFPT/dzRqW6b5MP24wEhQORpTC69rbb28xBBiJuVdh5UH5Fw6arSn/2fxanAnaFf9nR+//TJludtJAYC+WmZNGAtVGJ/RkitVqhzqqKaWRwBUOdkWlMwJOzbI/TSmLI1KecRztQ9ulVrCZkBZwtebkX1Hs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=d6SLJG0s; arc=none smtp.client-ip=209.85.210.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="d6SLJG0s" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-6f10092c8c7so4701035a34.1 for ; Fri, 24 May 2024 10:11:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716570714; x=1717175514; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=/RsqxAXu1+uvHb4FWDtAGaP/L0RYLDqIjQFYbG0JlUA=; b=d6SLJG0sOP0Kuwb4y0k8vZtRsdWUM/HHbK45K4BQsfZJk65bIezUu5MdZbTH0CwBJQ dk4u7qhVC9V2/pTTL460tUtJa+HuXovfQIxAOc3L+AfrANHXS1W0lqPdDAL0dcAdM8h9 dlOzDSejpaPwqrbwQIhkd99ySQx90yAfmoAa/M1sjNjdKR6W7E3RlOkqoAuZuGofD/Qt 6zrNcj93KTy/V/0D5tv5fLKzBCj7OUEVzf49fcmLN4m3DxOXDgpGnDLevrnP7xgktKz/ jR/JJgGPCDNF+vDJR/RCATMRmGLwlc4V4Pyt0fj7Jh0DXN676dK+sZMBeuGxCh4IxSzM mYAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716570714; x=1717175514; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/RsqxAXu1+uvHb4FWDtAGaP/L0RYLDqIjQFYbG0JlUA=; b=ErZyqZCoDE0gWywmv4NJiPrvurGCqEQLfFkVDktbLrLyjII8ELvle+KZozoDZlw0Bc VAVizx+SYzeon7U+8AJRHtBOr+Y8Xhu41SX90Bn47eSQl9hZuHqL6DYXGf6+XpM1MXy6 knHkdGhC1aG9u+xTpHvl6yDk5QtjqBcKCNRszEvQcgC3PgXD4tde86kjMnkdH6YJIOTo POHm3uN30MuaWG+IENtgRO6zDQNd62JCqBdSRMcUGfCFAZckUpx0nHq3mxFu5sAJUgRr 75ktI0Nfl9DL3lZ0oSuDkD/0vOky7/ue3E3QrecBiutRNRZI6nzxFeZZPGciuZ1dMyYt CuRA== X-Gm-Message-State: AOJu0YzMryHuFFxceGayCQvBBRypgXsFkh7L86QdxXv6xyOmo+wPqvYJ +LjiJFkLyEKqspQd0x36i065BBtKJjMrMnnO3G8iwxTvejQHBKhKBqb7Hg== X-Google-Smtp-Source: AGHT+IGjWwcxbVUSypgOB0c21RsimQCZ1uwi29k3FAGAP7X7wDm4hYYMl5oV0Wg5obs3jqw7vii0ug== X-Received: by 2002:a05:6871:829:b0:24c:aada:a58b with SMTP id 586e51a60fabf-24caadac300mr3054870fac.33.1716570713635; Fri, 24 May 2024 10:11:53 -0700 (PDT) Received: from lvondent-mobl4.. (syn-107-146-107-067.res.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-804cc1158ebsm256676241.29.2024.05.24.10.11.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 10:11:52 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v1 3/3] set: Attempt to use existing set gatt-db Date: Fri, 24 May 2024 13:11:47 -0400 Message-ID: <20240524171147.2733570-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240524171147.2733570-1-luiz.dentz@gmail.com> References: <20240524171147.2733570-1-luiz.dentz@gmail.com> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Luiz Augusto von Dentz Most sets should be clone of each other, or at least very similar, so this attempts to clone the existing gatt-db of the first member found when connecting new sets, this substantially speed up the process of bonding sets if their database matches which is something that is currently ranging from 20-30 seconds depending on the manufacturer and with this changes it takes a good 5-10 seconds. If the dbs don't really match bt_gatt_client instance will attempt to rediscover the ranges that don't match. --- src/device.c | 21 +++++++++++++++++++++ src/device.h | 1 + src/set.c | 22 ++++++++++++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/device.c b/src/device.c index 620bbd55ebad..5dc1cd0cdbf2 100644 --- a/src/device.c +++ b/src/device.c @@ -6994,6 +6994,27 @@ struct gatt_db *btd_device_get_gatt_db(struct btd_device *device) return device->db; } +bool btd_device_set_gatt_db(struct btd_device *device, struct gatt_db *db) +{ + struct gatt_db *clone; + + if (!device) + return false; + + clone = gatt_db_clone(db); + if (clone) + return false; + + gatt_db_unregister(device->db, device->db_id); + gatt_db_unref(device->db); + + device->db = clone; + device->db_id = gatt_db_register(device->db, gatt_service_added, + gatt_service_removed, device, NULL); + + return true; +} + struct bt_gatt_client *btd_device_get_gatt_client(struct btd_device *device) { if (!device) diff --git a/src/device.h b/src/device.h index a2b7bb15d200..0794f92d0178 100644 --- a/src/device.h +++ b/src/device.h @@ -66,6 +66,7 @@ struct gatt_primary *btd_device_get_primary(struct btd_device *device, const char *uuid); GSList *btd_device_get_primaries(struct btd_device *device); struct gatt_db *btd_device_get_gatt_db(struct btd_device *device); +bool btd_device_set_gatt_db(struct btd_device *device, struct gatt_db *db); struct bt_gatt_client *btd_device_get_gatt_client(struct btd_device *device); struct bt_gatt_server *btd_device_get_gatt_server(struct btd_device *device); bool btd_device_is_initiator(struct btd_device *device); diff --git a/src/set.c b/src/set.c index bf35ee403b39..4ca2f78c3702 100644 --- a/src/set.c +++ b/src/set.c @@ -28,6 +28,8 @@ #include "src/shared/queue.h" #include "src/shared/ad.h" #include "src/shared/crypto.h" +#include "src/shared/att.h" +#include "src/shared/gatt-db.h" #include "log.h" #include "error.h" @@ -277,8 +279,24 @@ static void foreach_rsi(void *data, void *user_data) bt_crypto_unref(crypto); - if (!memcmp(ad->data, res, sizeof(res))) - device_connect_le(set->device); + if (memcmp(ad->data, res, sizeof(res))) + return; + + /* Attempt to use existing gatt_db from set if device has never been + * connected before. + * + * If dbs don't really match bt_gatt_client will attempt to rediscover + * the ranges that don't match. + */ + if (gatt_db_isempty(btd_device_get_gatt_db(set->device))) { + struct btd_device *device; + + device = queue_get_entries(set->devices)->data; + btd_device_set_gatt_db(set->device, + btd_device_get_gatt_db(device)); + } + + device_connect_le(set->device); } static void foreach_device(struct btd_device *device, void *data)