From d293d3ece0c11fabecda987b639f431bbcbdc274 Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Thu, 21 Mar 2019 21:45:25 +0100 Subject: [PATCH] net/bluetil/ad: add bluetil_ad_find_and_cmp() --- sys/include/net/bluetil/ad.h | 16 +++++++++++++++- sys/net/ble/bluetil/bluetil_ad/bluetil_ad.c | 13 ++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/sys/include/net/bluetil/ad.h b/sys/include/net/bluetil/ad.h index 59ba6379b..191763472 100644 --- a/sys/include/net/bluetil/ad.h +++ b/sys/include/net/bluetil/ad.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Freie Universität Berlin + * Copyright (C) 2018,2019 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level @@ -97,6 +97,20 @@ void bluetil_ad_init(bluetil_ad_t *ad, void *buf, size_t pos, size_t size); int bluetil_ad_find(const bluetil_ad_t *ad, uint8_t type, bluetil_ad_data_t *data); +/** + * @brief Find a specific field and compare its value against the given data + * + * @param[in] ad advertising data descriptor + * @param[in] type field to search for + * @param[in] val data to compare against + * @param[in] val_len size of @p val in byte + * + * @return true if the field was found and its data is equal to the given data + * @return false if the field was not found or the data is not equal + */ +int bluetil_ad_find_and_cmp(const bluetil_ad_t *ad, uint8_t type, + const void *val, size_t val_len); + /** * @brief Find the given field and copy its payload into a string * diff --git a/sys/net/ble/bluetil/bluetil_ad/bluetil_ad.c b/sys/net/ble/bluetil/bluetil_ad/bluetil_ad.c index f07a95516..a9f8ea2e3 100644 --- a/sys/net/ble/bluetil/bluetil_ad/bluetil_ad.c +++ b/sys/net/ble/bluetil/bluetil_ad/bluetil_ad.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Freie Universität Berlin + * Copyright (C) 2018,2019 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level @@ -59,6 +59,17 @@ int bluetil_ad_find(const bluetil_ad_t *ad, uint8_t type, return BLUETIL_AD_NOTFOUND; } +int bluetil_ad_find_and_cmp(const bluetil_ad_t *ad, uint8_t type, + const void *val, size_t val_len) +{ + bluetil_ad_data_t field; + + if (bluetil_ad_find(ad, type, &field) == BLUETIL_AD_OK) { + return ((field.len == val_len) && memcmp(val, field.data, val_len) == 0); + } + return 0; +} + int bluetil_ad_find_str(const bluetil_ad_t *ad, uint8_t type, char *str, size_t str_len) {