Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3977306pxp; Tue, 15 Mar 2022 09:54:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJygFOpy1it566hug+ngu0DRhlQTuNBYRioaCkM7LvEwYA+/WY59gFlZMK3fXidu4zp30gey X-Received: by 2002:a17:907:7b8d:b0:6db:a30:8b96 with SMTP id ne13-20020a1709077b8d00b006db0a308b96mr24102798ejc.221.1647363298305; Tue, 15 Mar 2022 09:54:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647363298; cv=none; d=google.com; s=arc-20160816; b=GUqBHiVXLb9cUsetRm2aW6b/sNQVJohYy8n3SsOsTrbuYYZ95WnNDyrunAN56MD0Es VQGEf7qZiG93LTG+IshOA+BtAQAU30inxljC/idmvpSTdL+2Cv2zKq8njEKgJDYVeccX uycWoLHoJv9WLwFJM5q0MnQY6x5PBJiQsStE0u6L2P9zCa8Wuee1Jcj6Qz4zpP921ZGu w+5XuVpZLWbVd1rh3jjtTPpiOWXql8pz0zaItjulIlSkjngw5ANrsRPo2vkDph7E3hCW f1t+dyQ+Dzm4c25jNJJBeRvKvMqSlQLzQOqpTfBhB1Sh+fiZGqJtarjx/2M15OY6OFfb eLjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=njAUkH5dFFKuqO+juI/HLGmCs3nd2qaoyWds8EGJd78=; b=HjvClBjlMk2l4CpHZvuFRd92ZRfPAVJmrQzAfzD8TfdNIuyP3ILIOLcWK/ZiNTvCpv 9vp0UFUlUYs54TxceWoUsoxAPD2B7c4k07pXb3MXKnVgqjrkigPjHXLYWCjzt1nbmZL6 67if+y4rzY065zBcIRkHbVGrWVkr6xohsXKM56nKLVVzEzMrVKHhbbiubsUuS63/8UEn O7fBP5hj1paGsZggumMbTzhgrv17+F/Q68ZE54qxTosEYOrKKQjIFrFuVJ6WbAO5OOih 2riqg9BTld/6h84aGEevtPB6gu3eApEEMkLJFxwrF+5qBmyvO9o2OeKWuMIMfMykg3BA /2HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YE6lKpEe; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id do11-20020a170906c10b00b006daa061a874si11688216ejc.881.2022.03.15.09.54.15; Tue, 15 Mar 2022 09:54:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YE6lKpEe; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245400AbiCNVva (ORCPT + 99 others); Mon, 14 Mar 2022 17:51:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236193AbiCNVv3 (ORCPT ); Mon, 14 Mar 2022 17:51:29 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DE5A344CF for ; Mon, 14 Mar 2022 14:50:19 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id z16so16331305pfh.3 for ; Mon, 14 Mar 2022 14:50:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=njAUkH5dFFKuqO+juI/HLGmCs3nd2qaoyWds8EGJd78=; b=YE6lKpEeqWWr7bxVddJNsxSeXHOsfubyQgK5FMK2g021xkXIq7LtC5F/WcOQgXufx5 mLyh8hhgNV8LUmwEgFeJBL+q+zv2OIItg1FLqHy66MI7LGJowUDsvK45gnESLLVTmCqu Bu2TlFZKYyRdWQFI7CBAw+R+2vpSYG1HqWpGYOU/kJEx67i6jH2RrMJRiryi+kxhayDY gZN8RJ72YgE4GaOfk+Qd7IHmwo4Wv3Of4RBsLmMnyCexXopJNG5OJ2f3wkJhYFUWdk3j 0INGwWDo1uGqkDNPEbIMU/KEwq7H8UxNFDrp/o/MS7wyIEpirTiYeiNPv6+y2xkHOF0S XNVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=njAUkH5dFFKuqO+juI/HLGmCs3nd2qaoyWds8EGJd78=; b=ya31GS/qiqUkYQw5I4QPNCpnX1yc8HBWfR3HZyMIPtlxQfIXuPFMuQkczvd2FVL6kR ffhc1GIKiyCwihZHvddOrXdK/9EaDZdSUfZZYU7rcWno2H0iyApA12m/LXsOf+ypG+FG wLqS7EKqdGHZHEA/ZjMOdCOILhswLVr27mmaj9HtM06h79a/WTVaJiyzkKZaDbKXuYXZ v1BsQpfSTsW7E2HZk7/NkvouKEzDCHiJrZ20b/MjvgZUkZKBVVxWpuB+erkyRfwy6UkN +zYysavFrnkzz9vFE/WvEFQAg3tkv4tN3MXiXhn6UL03CWL4jfxQFhNrVpOzik+Mxf5+ f+dg== X-Gm-Message-State: AOAM530CTE4E1XxIr170FuiDFIYng4HLjJqhSb3mrWKsKvoC5R9rXbqv VntVv9nvYYK9jz20KkenE16WXz4eq6A= X-Received: by 2002:a63:2358:0:b0:380:e306:f0bd with SMTP id u24-20020a632358000000b00380e306f0bdmr19906066pgm.594.1647294618357; Mon, 14 Mar 2022 14:50:18 -0700 (PDT) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id s15-20020a63af4f000000b0037c8875108dsm18029032pgo.45.2022.03.14.14.50.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Mar 2022 14:50:17 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ 2/2] gatt-db: Fix gatt_db_attribute_notify Date: Mon, 14 Mar 2022 14:50:10 -0700 Message-Id: <20220314215010.23822-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314215010.23822-1-luiz.dentz@gmail.com> References: <20220314215010.23822-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz gatt_db_attribute_notify was only accepting passing the Characteristic Declaration instead of accepting its value as well, gatt_db_service_foreach_desc also have similar limitation so both have been updated to allow working with both value and declaration. --- src/shared/gatt-db.c | 63 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c index 53d3e1243..be07cdbe4 100644 --- a/src/shared/gatt-db.c +++ b/src/shared/gatt-db.c @@ -1528,32 +1528,71 @@ void gatt_db_service_foreach_char(struct gatt_db_attribute *attrib, gatt_db_service_foreach(attrib, &characteristic_uuid, func, user_data); } +static int gatt_db_attribute_get_index(struct gatt_db_attribute *attrib) +{ + struct gatt_db_service *service; + int index; + + if (!attrib) + return -1; + + service = attrib->service; + index = attrib->handle - service->attributes[0]->handle; + + if (index > (service->num_handles - 1)) + return -1; + + return index; +} + +static struct gatt_db_attribute * +gatt_db_attribute_get_value(struct gatt_db_attribute *attrib) +{ + struct gatt_db_service *service; + int index; + + if (!attrib) + return NULL; + + index = gatt_db_attribute_get_index(attrib); + if (index < 0) + return NULL; + + service = attrib->service; + + if (!bt_uuid_cmp(&characteristic_uuid, &attrib->uuid)) + index++; + else if (bt_uuid_cmp(&characteristic_uuid, + &service->attributes[index - 1]->uuid)) + return NULL; + + return service->attributes[index]; +} + void gatt_db_service_foreach_desc(struct gatt_db_attribute *attrib, gatt_db_attribute_cb_t func, void *user_data) { struct gatt_db_service *service; struct gatt_db_attribute *attr; + int index; uint16_t i; if (!attrib || !func) return; - /* Return if this attribute is not a characteristic declaration */ - if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid)) + attrib = gatt_db_attribute_get_value(attrib); + if (!attrib) + return; + + index = gatt_db_attribute_get_index(attrib); + if (index < 0) return; service = attrib->service; /* Start from the attribute following the value handle */ - for (i = 0; i < service->num_handles; i++) { - if (service->attributes[i] == attrib) { - i += 2; - break; - } - } - - for (; i < service->num_handles; i++) { + for (i = index + 1; i < service->num_handles; i++) { attr = service->attributes[i]; if (!attr) continue; @@ -2163,8 +2202,8 @@ bool gatt_db_attribute_notify(struct gatt_db_attribute *attrib, if (!attrib || !attrib->notify_func) return false; - /* Return if this attribute is not a characteristic declaration */ - if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid)) + attrib = gatt_db_attribute_get_value(attrib); + if (!attrib) return false; ccc = gatt_db_attribute_get_ccc(attrib); -- 2.35.1