Skip to content
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

Better backend API #1138

Merged
merged 10 commits into from
Mar 11, 2024
3 changes: 3 additions & 0 deletions attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "iio-private.h"
#include "sort.h"

#include <inttypes.h>
#include <errno.h>
Expand Down Expand Up @@ -214,6 +215,8 @@ int iio_add_attr(union iio_pointer p, struct iio_attr_list *attrs,

attrs->num++;

iio_sort_attrs(attrs);

return 0;
}

Expand Down
7 changes: 0 additions & 7 deletions attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,4 @@ int iio_add_attr(union iio_pointer p, struct iio_attr_list *attrs,
const char *name, const char *filename,
enum iio_attr_type type);

int iio_context_add_attr(struct iio_context *ctx,
const char *key, const char *value);
int iio_device_add_attr(struct iio_device *dev,
const char *name, enum iio_attr_type type);
int iio_channel_add_attr(struct iio_channel *chn,
const char *name, const char *filename);

#endif /* __IIO_ATTR_H__ */
81 changes: 67 additions & 14 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,15 @@ char * iio_context_get_xml(const struct iio_context *ctx)
return str;
}

struct iio_context * iio_context_create_from_backend(
const struct iio_backend *backend,
const char *description)
struct iio_context *
iio_context_create_from_backend(const struct iio_context_params *params,
const struct iio_backend *backend,
const char *description,
unsigned int major, unsigned int minor,
const char *git_tag)
{
struct iio_context *ctx;
int ret;
int ret = -ENOMEM;

if (!backend)
return iio_ptr(-EINVAL);
Expand All @@ -218,7 +221,6 @@ struct iio_context * iio_context_create_from_backend(
if (!ctx)
return iio_ptr(-ENOMEM);

ret = -ENOMEM;
if (description) {
ctx->description = iio_strdup(description);
if (!ctx->description)
Expand All @@ -227,9 +229,21 @@ struct iio_context * iio_context_create_from_backend(

ctx->name = backend->name;
ctx->ops = backend->ops;
ctx->params = *params;

ctx->major = major;
ctx->minor = minor;

if (git_tag) {
ctx->git_tag = iio_strdup(git_tag);
if (!ctx->git_tag)
goto err_free_description;
}

return ctx;

err_free_description:
free(ctx->description);
err_free_ctx:
free(ctx);
return iio_ptr(ret);
Expand Down Expand Up @@ -511,22 +525,61 @@ iio_context_find_attr(const struct iio_context *ctx, const char *name)
return iio_attr_find(&ctx->attrlist, name);
}

int iio_context_add_device(struct iio_context *ctx, struct iio_device *dev)
struct iio_device * iio_context_add_device(struct iio_context *ctx,
const char *id, const char *name,
const char *label)
{
struct iio_device **devices = realloc(ctx->devices,
(ctx->nb_devices + 1) * sizeof(struct iio_device *));
struct iio_device *dev, **devs;
char *new_id, *new_name = NULL, *new_label = NULL;

dev = zalloc(sizeof(*dev));
if (!dev)
return NULL;

new_id = iio_strdup(id);
if (!new_id)
goto err_free_dev;

if (!devices) {
ctx_err(ctx, "Unable to allocate memory\n");
return -ENOMEM;
if (name) {
new_name = iio_strdup(name);
if (!new_name)
goto err_free_id;
}

devices[ctx->nb_devices++] = dev;
ctx->devices = devices;
if (label) {
new_label = iio_strdup(label);
if (!new_label)
goto err_free_name;
}

dev->id = new_id;
dev->ctx = ctx;
dev->name = new_name;
dev->label = new_label;

devs = realloc(ctx->devices, (ctx->nb_devices + 1) * sizeof(*dev));
if (!devs)
goto err_free_label;

devs[ctx->nb_devices++] = dev;
ctx->devices = devs;

ctx_dbg(ctx, "Added device \'%s\' to context \'%s\'\n",
dev->id, ctx->name);
return 0;

iio_sort_devices(ctx);

return dev;

err_free_label:
free(new_label);
err_free_name:
free(new_name);
err_free_id:
free(new_id);
err_free_dev:
free(dev);
return NULL;
}

struct iio_context *
Expand Down
64 changes: 64 additions & 0 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "attr.h"
#include "iio-private.h"
#include "sort.h"

#include <iio/iio-debug.h>
#include <inttypes.h>
Expand Down Expand Up @@ -351,3 +352,66 @@ void iio_device_set_pdata(struct iio_device *dev, struct iio_device_pdata *d)
{
dev->pdata = d;
}

struct iio_channel * iio_device_add_channel(struct iio_device *dev, long index,
const char *id, const char *name,
bool output, bool scan_element,
const struct iio_data_format *fmt)
{
struct iio_channel *chn, **chns;
char *new_id, *new_name = NULL;
unsigned int i;

chn = zalloc(sizeof(*chn));
if (!chn)
return NULL;

new_id = iio_strdup(id);
if (!new_id)
goto err_free_chn;

if (name) {
new_name = iio_strdup(name);
if (!new_name)
goto err_free_id;
}

chn->id = new_id;
chn->name = new_name;
chn->dev = dev;
chn->is_output = output;
chn->is_scan_element = scan_element;
chn->index = index;

memcpy(&chn->format, fmt, sizeof(*fmt));

if (!chn->format.repeat)
chn->format.repeat = 1;

chns = realloc(dev->channels, (dev->nb_channels + 1) * sizeof(*chn));
if (!chns)
goto err_free_name;

chns[dev->nb_channels++] = chn;
dev->channels = chns;

dev_dbg(dev, "Added channel \'%s\'\n", id);

iio_channel_init_finalize(chn);

/* Reorder channels by index */
iio_sort_channels(dev);

for (i = 0; i < dev->nb_channels; i++)
dev->channels[i]->number = i;

return chn;

err_free_name:
free(new_name);
err_free_id:
free(new_id);
err_free_chn:
free(chn);
return NULL;
}
2 changes: 0 additions & 2 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ char *iio_strtok_r(char *str, const char *delim, char **saveptr);
char * iio_getenv (char * envvar);
uint64_t iio_read_counter_us(void);

int iio_context_add_device(struct iio_context *ctx, struct iio_device *dev);

__cnst const struct iio_context_params *get_default_params(void);

extern const struct iio_backend iio_ip_backend;
Expand Down
27 changes: 24 additions & 3 deletions include/iio/iio-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,30 @@ struct iio_backend {
*/
extern const struct iio_backend iio_external_backend;

struct iio_context * iio_context_create_from_backend(
const struct iio_backend *backend,
const char *description);
__api struct iio_context *
iio_context_create_from_backend(const struct iio_context_params *params,
const struct iio_backend *backend,
const char *description, unsigned int minor,
unsigned int major, const char *git_tag);

__api struct iio_device *
iio_context_add_device(struct iio_context *ctx,
const char *id, const char *name, const char *label);

__api struct iio_channel *
iio_device_add_channel(struct iio_device *dev, long index,
const char *id, const char *name, bool output,
bool scan_element, const struct iio_data_format *fmt);

__api int
iio_context_add_attr(struct iio_context *ctx,
const char *key, const char *value);
__api int
iio_device_add_attr(struct iio_device *dev,
const char *name, enum iio_attr_type type);
__api int
iio_channel_add_attr(struct iio_channel *chn,
const char *name, const char *filename);

__api struct iio_context_pdata *
iio_context_get_pdata(const struct iio_context *ctx);
Expand Down
Loading
Loading