blob: 8b393c90c740faa69bf4500622e86c14898d0c45 [file] [log] [blame]
Paul Cercueilbb4401d2014-02-28 16:10:49 +01001/*
2 * libiio - Library for interfacing industrial I/O (IIO) devices
3 *
4 * Copyright (C) 2014 Analog Devices, Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * */
18
Paul Cercueil167d3112014-02-18 12:23:53 +010019#include "debug.h"
Paul Cercueil0b2ce712014-02-17 15:04:18 +010020#include "iio-private.h"
21
Paul Cercueil167d3112014-02-18 12:23:53 +010022#include <errno.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010023#include <stdio.h>
Paul Cercueil167d3112014-02-18 12:23:53 +010024#include <string.h>
Paul Cercueil0b2ce712014-02-17 15:04:18 +010025
Paul Cercueil42090d12014-02-24 12:32:23 +010026static char *get_attr_xml(const char *attr, size_t *length)
27{
28 size_t len = sizeof("<attribute name=\"\" />") + strlen(attr);
29 char *str = malloc(len);
30 if (!str) {
31 ERROR("Unable to allocate memory\n");
32 return NULL;
33 }
34
35 *length = len - 1; /* Skip the \0 */
36 sprintf(str, "<attribute name=\"%s\" />", attr);
37 return str;
38}
39
40/* Returns a string containing the XML representation of this channel */
41char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length)
42{
43 size_t len = sizeof("<channel id=\"\" name=\"\" "
44 "type=\"output\" ></channel>");
45 char *ptr, *str, *attrs[chn->nb_attrs];
46 size_t attrs_len[chn->nb_attrs];
47 unsigned int i;
48
49 for (i = 0; i < chn->nb_attrs; i++) {
50 char *xml = get_attr_xml(chn->attrs[i], &attrs_len[i]);
51 if (!xml)
52 goto err_free_attrs;
53 attrs[i] = xml;
54 len += attrs_len[i];
55 }
56
57 len += strlen(chn->id);
58 if (chn->name)
59 len += strlen(chn->name);
60
61 str = malloc(len);
62 if (!str)
63 goto err_free_attrs;
64
65 sprintf(str, "<channel id=\"%s\"", chn->id);
66 ptr = strrchr(str, '\0');
67
68 if (chn->name) {
69 sprintf(ptr, " name=\"%s\"", chn->name);
70 ptr = strrchr(ptr, '\0');
71 }
72
73 sprintf(ptr, " type=\"%s\" >", chn->is_output ? "output" : "input");
74 ptr = strrchr(ptr, '\0');
75
76 for (i = 0; i < chn->nb_attrs; i++) {
77 strcpy(ptr, attrs[i]);
78 ptr += attrs_len[i];
79 free(attrs[i]);
80 }
81
82 strcpy(ptr, "</channel>");
83 *length = ptr - str + sizeof("</channel>") - 1;
84 return str;
85
86err_free_attrs:
87 while (i--)
88 free(attrs[i]);
89 return NULL;
90}
91
Paul Cercueil0b2ce712014-02-17 15:04:18 +010092const char * iio_channel_get_id(const struct iio_channel *chn)
93{
94 return chn->id;
95}
96
97const char * iio_channel_get_name(const struct iio_channel *chn)
98{
99 return chn->name;
100}
101
Paul Cercueil35a01312014-02-20 10:56:57 +0100102bool iio_channel_is_output(const struct iio_channel *chn)
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100103{
Paul Cercueil35a01312014-02-20 10:56:57 +0100104 return chn->is_output;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100105}
106
107unsigned int iio_channel_get_attrs_count(const struct iio_channel *chn)
108{
109 return chn->nb_attrs;
110}
111
112const char * iio_channel_get_attr(const struct iio_channel *chn,
113 unsigned int index)
114{
115 if (index >= chn->nb_attrs)
116 return NULL;
117 else
118 return chn->attrs[index];
119}
120
121ssize_t iio_channel_attr_read(const struct iio_channel *chn,
122 const char *attr, char *dst, size_t len)
123{
Paul Cercueil6a013602014-02-19 12:37:39 +0100124 if (chn->dev->ctx->ops->read_channel_attr)
125 return chn->dev->ctx->ops->read_channel_attr(chn,
126 attr, dst, len);
127 else
128 return -ENOSYS;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100129}
130
131ssize_t iio_channel_attr_write(const struct iio_channel *chn,
132 const char *attr, const char *src)
133{
Paul Cercueil6a013602014-02-19 12:37:39 +0100134 if (chn->dev->ctx->ops->write_channel_attr)
135 return chn->dev->ctx->ops->write_channel_attr(chn, attr, src);
136 else
137 return -ENOSYS;
Paul Cercueil0b2ce712014-02-17 15:04:18 +0100138}
Paul Cercueil00236242014-02-18 15:09:06 +0100139
140void free_channel(struct iio_channel *chn)
141{
142 unsigned int i;
143 for (i = 0; i < chn->nb_attrs; i++)
144 free((char *) chn->attrs[i]);
145 if (chn->nb_attrs)
146 free(chn->attrs);
147 if (chn->name)
148 free((char *) chn->name);
149 if (chn->id)
150 free((char *) chn->id);
151 free(chn);
152}