Skip to content

Commit

Permalink
Introduce iio_channels_mask private struct
Browse files Browse the repository at this point in the history
This struct will be used later to store a bitmask of enabled channels.

Add iio_channels_mask_set_bit(), iio_channels_mask_clear_bit(), and
iio_channels_mask_test_bit() inline functions to manipulate this new
object.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Feb 28, 2022
1 parent 46f760f commit 2f33d3b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
29 changes: 29 additions & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions mask.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#include "iio-private.h"

#include <errno.h>
#include <string.h>

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;
}

0 comments on commit 2f33d3b

Please sign in to comment.