-
Notifications
You must be signed in to change notification settings - Fork 318
/
attr.c
316 lines (249 loc) · 6.26 KB
/
attr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// 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 "sort.h"
#include <inttypes.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
static inline unsigned int attr_index(const struct iio_attr_list *list,
const struct iio_attr *attr)
{
uintptr_t diff = (uintptr_t)attr - (uintptr_t)list->attrs;
return (unsigned int)diff / sizeof(*attr);
}
ssize_t iio_attr_read_raw(const struct iio_attr *attr, char *dst, size_t len)
{
const struct iio_device *dev = iio_attr_get_device(attr);
unsigned int idx;
if (attr->type == IIO_ATTR_TYPE_CONTEXT) {
idx = attr_index(&attr->iio.ctx->attrlist, attr);
return (ssize_t) iio_strlcpy(dst, attr->iio.ctx->values[idx], len);
}
if (dev->ctx->ops->read_attr)
return dev->ctx->ops->read_attr(attr, dst, len);
return -ENOSYS;
}
int iio_attr_read_longlong(const struct iio_attr *attr, long long *val)
{
char *end, buf[1024];
long long value;
ssize_t ret;
ret = iio_attr_read_raw(attr, buf, sizeof(buf));
if (ret < 0)
return (int) ret;
errno = 0;
value = strtoll(buf, &end, 0);
if (end == buf || errno == ERANGE)
return -EINVAL;
*val = value;
return 0;
}
int iio_attr_read_bool(const struct iio_attr *attr, bool *val)
{
long long value;
int ret;
ret = iio_attr_read_longlong(attr, &value);
if (ret < 0)
return ret;
*val = !!value;
return 0;
}
int iio_attr_read_double(const struct iio_attr *attr, double *val)
{
char buf[1024];
ssize_t ret;
ret = iio_attr_read_raw(attr, buf, sizeof(buf));
if (ret < 0)
return (int) ret;
return read_double(buf, val);
}
ssize_t iio_attr_write_raw(const struct iio_attr *attr,
const void *src, size_t len)
{
const struct iio_device *dev = iio_attr_get_device(attr);
if (attr->type == IIO_ATTR_TYPE_CONTEXT)
return -EPERM;
if (dev->ctx->ops->write_attr)
return dev->ctx->ops->write_attr(attr, src, len);
return -ENOSYS;
}
ssize_t iio_attr_write_string(const struct iio_attr *attr, const char *src)
{
return iio_attr_write_raw(attr, src, strlen(src) + 1); /* Flawfinder: ignore */
}
int iio_attr_write_longlong(const struct iio_attr *attr, long long val)
{
size_t len;
ssize_t ret;
char buf[1024];
len = iio_snprintf(buf, sizeof(buf), "%lld", val);
ret = iio_attr_write_raw(attr, buf, len + 1);
return (int) (ret < 0 ? ret : 0);
}
int iio_attr_write_double(const struct iio_attr *attr, double val)
{
ssize_t ret;
char buf[1024];
ret = (ssize_t) write_double(buf, sizeof(buf), val);
if (!ret)
ret = iio_attr_write_string(attr, buf);
return (int) (ret < 0 ? ret : 0);
}
int iio_attr_write_bool(const struct iio_attr *attr, bool val)
{
ssize_t ret;
if (val)
ret = iio_attr_write_raw(attr, "1", 2);
else
ret = iio_attr_write_raw(attr, "0", 2);
return (int) (ret < 0 ? ret : 0);
}
const struct iio_attr *
iio_attr_get(const struct iio_attr_list *attrs, unsigned int idx)
{
if (idx >= attrs->num)
return NULL;
return &attrs->attrs[idx];
}
const struct iio_attr *
iio_attr_find(const struct iio_attr_list *attrs, const char *name)
{
unsigned int i;
for (i = 0; i < attrs->num; i++)
if (!strcmp(attrs->attrs[i].name, name))
return iio_attr_get(attrs, i);
return NULL;
}
const char *
iio_attr_get_name(const struct iio_attr *attr)
{
return attr->name;
}
const char *
iio_attr_get_filename(const struct iio_attr *attr)
{
return attr->filename;
}
const char *
iio_attr_get_static_value(const struct iio_attr *attr)
{
unsigned int idx;
switch (attr->type) {
case IIO_ATTR_TYPE_CONTEXT:
idx = attr_index(&attr->iio.ctx->attrlist, attr);
return attr->iio.ctx->values[idx];
default:
return NULL;
}
}
int iio_add_attr(union iio_pointer p, struct iio_attr_list *attrs,
const char *name, const char *filename,
enum iio_attr_type type)
{
struct iio_attr *attr;
attr = realloc(attrs->attrs, (1 + attrs->num) * sizeof(*attrs->attrs));
if (!attr)
return -ENOMEM;
attrs->attrs = attr;
attr[attrs->num] = (struct iio_attr){
.iio = p,
.type = type,
.name = iio_strdup(name),
.filename = NULL,
};
if (!attr[attrs->num].name)
return -ENOMEM;
if (filename) {
attr[attrs->num].filename = iio_strdup(filename);
if (!attr[attrs->num].filename) {
free((char *) attr[attrs->num].name);
return -ENOMEM;
}
} else {
attr[attrs->num].filename = attr[attrs->num].name;
}
attrs->num++;
iio_sort_attrs(attrs);
return 0;
}
static const char * const attr_type_string[] = {
"",
" debug",
" buffer",
};
int iio_device_add_attr(struct iio_device *dev,
const char *name, enum iio_attr_type type)
{
union iio_pointer p = { .dev = dev, };
int ret;
ret = iio_add_attr(p, &dev->attrlist[type], name, NULL, type);
if (ret < 0)
return ret;
dev_dbg(dev, "Added%s attr \'%s\'\n", attr_type_string[type], name);
return 0;
}
int iio_channel_add_attr(struct iio_channel *chn,
const char *name, const char *filename)
{
union iio_pointer p = { .chn = chn, };
int ret;
ret = iio_add_attr(p, &chn->attrlist, name, filename,
IIO_ATTR_TYPE_CHANNEL);
if (ret < 0)
return ret;
chn_dbg(chn, "Added attr \'%s\' (\'%s\')\n", name, filename);
return 0;
}
int iio_context_add_attr(struct iio_context *ctx,
const char *key, const char *value)
{
char **values, *new_val;
union iio_pointer p = { .ctx = ctx, };
unsigned int i;
int ret;
new_val = iio_strdup(value);
if (!new_val)
return -ENOMEM;
for (i = 0; i < ctx->attrlist.num; i++) {
if(!strcmp(ctx->attrlist.attrs[i].name, key)) {
free(ctx->values[i]);
ctx->values[i] = new_val;
return 0;
}
}
values = realloc(ctx->values,
(ctx->attrlist.num + 1) * sizeof(*ctx->values));
if (!values) {
free(new_val);
return -ENOMEM;
}
values[ctx->attrlist.num] = new_val;
ctx->values = values;
ret = iio_add_attr(p, &ctx->attrlist, key, NULL, IIO_ATTR_TYPE_CONTEXT);
if (ret) {
free(new_val);
return ret;
}
return 0;
}
void iio_free_attr_data(struct iio_attr *attr)
{
if (attr->filename != attr->name)
free((char *) attr->filename);
free((char *) attr->name);
attr->filename = NULL;
attr->name = NULL;
}
void iio_free_attrs(const struct iio_attr_list *attrs)
{
unsigned int i;
for (i = 0; i < attrs->num; i++)
iio_free_attr_data(&attrs->attrs[i]);
free(attrs->attrs);
}