-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for IIO events #1081
Changes from all commits
be3a5db
5080ab0
ba393ca
600735c
7ec1936
81cf38f
5be9c99
a55c644
29efc2a
7d8851c
8bc719f
8270801
9916f46
29165c7
94fc363
b574558
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
/* | ||
* libiio - Library for interfacing industrial I/O (IIO) devices | ||
* | ||
* Copyright (C) 2023 Analog Devices, Inc. | ||
* Author: Paul Cercueil <[email protected]> | ||
*/ | ||
|
||
#include "iio-private.h" | ||
|
||
#include <ctype.h> | ||
#include <errno.h> | ||
#include <iio/iio-debug.h> | ||
|
||
struct iio_event_stream_pdata; | ||
|
||
struct iio_event_stream { | ||
const struct iio_device *dev; | ||
struct iio_event_stream_pdata *pdata; | ||
}; | ||
|
||
/* Corresponds to IIO_EVENT_CODE_EXTRACT_CHAN() and | ||
* IIO_EVENT_CODE_EXTRACT_CHAN2() macros of <linux/iio/events.h> */ | ||
static inline int16_t | ||
iio_event_get_channel_id(const struct iio_event *event, unsigned int channel) | ||
{ | ||
return (int16_t)(event->id >> (channel << 4)); | ||
} | ||
|
||
/* Corresponds to IIO_EVENT_CODE_EXTRACT_DIFF() of <linux/iio/events.h> */ | ||
static inline bool | ||
iio_event_is_differential(const struct iio_event *event) | ||
{ | ||
return event->id & BIT(55); | ||
} | ||
|
||
/* Corresponds to IIO_EVENT_CODE_EXTRACT_MODIFIER() of <linux/iio/events.h> */ | ||
static inline enum iio_modifier | ||
iio_event_get_modifier(const struct iio_event *event) | ||
{ | ||
return (enum iio_modifier)((event->id >> 40) & 0xff); | ||
} | ||
|
||
/* Corresponds to IIO_EVENT_CODE_EXTRACT_CHAN_TYPE() of <linux/iio/events.h> */ | ||
static inline enum iio_chan_type | ||
iio_event_get_chan_type(const struct iio_event *event) | ||
{ | ||
return (enum iio_chan_type)((event->id >> 32) & 0xff); | ||
} | ||
|
||
const struct iio_channel * | ||
iio_event_get_channel(const struct iio_event *event, | ||
const struct iio_device *dev, bool diff) | ||
{ | ||
const struct iio_channel *chn = NULL; | ||
const char *ptr; | ||
unsigned int i; | ||
int16_t chid; | ||
|
||
if (diff && !iio_event_is_differential(event)) | ||
return NULL; | ||
|
||
chid = iio_event_get_channel_id(event, diff); | ||
if (chid < 0) | ||
return NULL; | ||
|
||
if ((unsigned int)chid >= dev->nb_channels) { | ||
dev_warn(dev, "Unexpected IIO event channel ID\n"); | ||
return NULL; | ||
} | ||
|
||
for (i = 0; i < dev->nb_channels; i++) { | ||
chn = dev->channels[i]; | ||
|
||
if (chn->type != iio_event_get_chan_type(event) | ||
|| chn->modifier != iio_event_get_modifier(event)) { | ||
continue; | ||
} | ||
|
||
for (ptr = chn->id; *ptr && isalpha((unsigned char)*ptr); ) | ||
ptr++; | ||
|
||
if (!*ptr && chid <= 0) | ||
break; | ||
|
||
if ((uint16_t)chid == strtoul(ptr, NULL, 10)) | ||
break; | ||
} | ||
|
||
if (chn) { | ||
chn_dbg(chn, "Found channel %s for event\n", | ||
iio_channel_get_id(chn)); | ||
} else { | ||
dev_dbg(dev, "Unable to find channel for event\n"); | ||
} | ||
|
||
return chn; | ||
} | ||
|
||
struct iio_event_stream * | ||
iio_device_create_event_stream(const struct iio_device *dev) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have always found it helpful to have the same namespace prefix for all functions in a file. So perhaps this could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That wouldn't be consistent with what we already have, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough |
||
{ | ||
struct iio_event_stream *stream; | ||
int err; | ||
|
||
if (!dev->ctx->ops->open_ev) | ||
return iio_ptr(-ENOSYS); | ||
|
||
stream = zalloc(sizeof(*stream)); | ||
if (!stream) | ||
return iio_ptr(-ENOMEM); | ||
|
||
stream->dev = dev; | ||
|
||
stream->pdata = dev->ctx->ops->open_ev(dev); | ||
err = iio_err(stream->pdata); | ||
if (err) { | ||
free(stream); | ||
return iio_ptr(err); | ||
} | ||
|
||
return stream; | ||
} | ||
|
||
void iio_event_stream_destroy(struct iio_event_stream *stream) | ||
{ | ||
if (stream->dev->ctx->ops->close_ev) | ||
stream->dev->ctx->ops->close_ev(stream->pdata); | ||
|
||
free(stream); | ||
} | ||
|
||
int iio_event_stream_read(struct iio_event_stream *stream, | ||
struct iio_event *out_event, | ||
bool nonblock) | ||
{ | ||
if (!stream->dev->ctx->ops->read_ev) | ||
return -ENOSYS; | ||
|
||
if (!out_event) | ||
return -EINVAL; | ||
|
||
return stream->dev->ctx->ops->read_ev(stream->pdata, out_event, nonblock); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
#define is_little_endian() (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) | ||
#endif | ||
|
||
#define BIT(x) (1 << (x)) | ||
#define BIT(x) (1ull << (x)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems odd to make this 64-bit but not the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only make the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reviewing btw. |
||
#define BIT_MASK(bit) BIT((bit) % 32) | ||
#define BIT_WORD(bit) ((bit) / 32) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to be more explicit for the exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to
OSError
.