diff --git a/CMakeLists.txt b/CMakeLists.txt index 815135337..273fbf8f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ endif() option(BUILD_SHARED_LIBS "Build shared libraries" ON) -add_library(iio backend.c channel.c device.c context.c buffer.c utilities.c scan.c sort.c +add_library(iio backend.c channel.c device.c context.c buffer.c mask.c utilities.c scan.c sort.c ${CMAKE_CURRENT_BINARY_DIR}/iio-config.h ) diff --git a/iio-private.h b/iio-private.h index e20cc1cac..76cab072b 100644 --- a/iio-private.h +++ b/iio-private.h @@ -152,6 +152,35 @@ struct iio_context_info { char *uri; }; +struct iio_channels_mask { + size_t words; + uint32_t mask[]; +}; + +struct iio_channels_mask *iio_create_channels_mask(unsigned int nb_channels); + +int iio_channels_mask_copy(struct iio_channels_mask *dst, + const struct iio_channels_mask *src); + +static inline bool +iio_channels_mask_test_bit(const struct iio_channels_mask *mask, + unsigned int bit) +{ + return mask->mask[BIT_WORD(bit)] & BIT_MASK(bit); +} + +static inline void +iio_channels_mask_set_bit(struct iio_channels_mask *mask, unsigned int bit) +{ + mask->mask[BIT_WORD(bit)] |= BIT_MASK(bit); +} + +static inline void +iio_channels_mask_clear_bit(struct iio_channels_mask *mask, unsigned int bit) +{ + mask->mask[BIT_WORD(bit)] &= ~BIT_MASK(bit); +} + struct iio_module * iio_open_module(const struct iio_context_params *params, const char *name); void iio_release_module(struct iio_module *module); diff --git a/mask.c b/mask.c new file mode 100644 index 000000000..4a0bf009b --- /dev/null +++ b/mask.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * libiio - Library for interfacing industrial I/O (IIO) devices + * + * Copyright (C) 2022 Analog Devices, Inc. + * Author: Paul Cercueil + */ + +#include "iio-private.h" + +#include +#include + +struct iio_channels_mask * iio_create_channels_mask(unsigned int nb_channels) +{ + struct iio_channels_mask *mask; + size_t nb_words = (nb_channels + 31) / 32; + + mask = zalloc(sizeof(*mask) + nb_words * sizeof(uint32_t)); + if (mask) + mask->words = nb_words; + + return mask; +} + +int iio_channels_mask_copy(struct iio_channels_mask *dst, + const struct iio_channels_mask *src) +{ + if (dst->words != src->words) + return -EINVAL; + + memcpy(dst->mask, src->mask, src->words * sizeof(uint32_t)); + + return 0; +}