blob: dfee1a3f3f636a846fff85fefbfe69beae26d5f8 [file] [log] [blame]
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +01001/* IIO - useful set of util functionality
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
Cristina Opriceana37e3be92015-03-04 02:37:15 +02009#ifndef _IIO_UTILS_H
10#define _IIO_UTILS_H
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +010011
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010012#include <string.h>
13#include <stdlib.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010014#include <stdio.h>
15#include <stdint.h>
Lars-Peter Clausenbc9f35d2011-10-26 17:27:44 +010016#include <dirent.h>
Peter Meerwaldbb233782012-06-25 23:12:17 +020017#include <errno.h>
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020018#include <ctype.h>
19#include "iio_utils.h"
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +010020
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010021const char *iio_dir = "/sys/bus/iio/devices/";
22
Irina Tirdead9d7b992015-03-27 13:53:00 +020023static char * const iio_direction[] = {
24 "in",
25 "out",
26};
27
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010028/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010029 * iioutils_break_up_name() - extract generic name from full channel name
30 * @full_name: the full channel name
31 * @generic_name: the output generic channel name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020032 *
33 * Returns 0 on success, or a negative error code if string extraction failed.
Jonathan Camerone58537c2010-10-08 12:14:14 +010034 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020035int iioutils_break_up_name(const char *full_name,
Jonathan Camerone58537c2010-10-08 12:14:14 +010036 char **generic_name)
37{
38 char *current;
39 char *w, *r;
Irina Tirdead9d7b992015-03-27 13:53:00 +020040 char *working, *prefix = "";
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020041 int i, ret;
Melike Yurtoglu79bdd482014-10-03 23:35:54 +030042
Irina Tirdead9d7b992015-03-27 13:53:00 +020043 for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
44 if (!strncmp(full_name, iio_direction[i],
45 strlen(iio_direction[i]))) {
46 prefix = iio_direction[i];
47 break;
48 }
49
50 current = strdup(full_name + strlen(prefix) + 1);
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020051 if (!current)
52 return -ENOMEM;
53
Jonathan Camerone58537c2010-10-08 12:14:14 +010054 working = strtok(current, "_\0");
Hartmut Knaack53118552015-05-31 14:40:14 +020055 if (!working) {
56 free(current);
57 return -EINVAL;
58 }
Irina Tirdead9d7b992015-03-27 13:53:00 +020059
Jonathan Camerone58537c2010-10-08 12:14:14 +010060 w = working;
61 r = working;
62
Michael Hennerich8b68bb22011-03-08 08:55:48 +010063 while (*r != '\0') {
Jonathan Camerone58537c2010-10-08 12:14:14 +010064 if (!isdigit(*r)) {
65 *w = *r;
66 w++;
67 }
68 r++;
69 }
70 *w = '\0';
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020071 ret = asprintf(generic_name, "%s_%s", prefix, working);
Jonathan Camerone58537c2010-10-08 12:14:14 +010072 free(current);
73
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020074 return (ret == -1) ? -ENOMEM : 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +010075}
76
77/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010078 * iioutils_get_type() - find and process _type attribute data
79 * @is_signed: output whether channel is signed
80 * @bytes: output how many bytes the channel storage occupies
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020081 * @bits_used: output number of valid bits of data
82 * @shift: output amount of bits to shift right data before applying bit mask
Jonathan Camerone58537c2010-10-08 12:14:14 +010083 * @mask: output a bit mask for the raw data
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020084 * @be: output if data in big endian
85 * @device_dir: the IIO device directory
Jonathan Camerone58537c2010-10-08 12:14:14 +010086 * @name: the channel name
87 * @generic_name: the channel type name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020088 *
89 * Returns a value >= 0 on success, otherwise a negative error code.
Jonathan Camerone58537c2010-10-08 12:14:14 +010090 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020091int iioutils_get_type(unsigned *is_signed,
Jonathan Camerone58537c2010-10-08 12:14:14 +010092 unsigned *bytes,
93 unsigned *bits_used,
Jonathan Cameron52615d42011-05-18 14:41:19 +010094 unsigned *shift,
Jonathan Camerone58537c2010-10-08 12:14:14 +010095 uint64_t *mask,
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000096 unsigned *be,
Jonathan Camerone58537c2010-10-08 12:14:14 +010097 const char *device_dir,
98 const char *name,
99 const char *generic_name)
100{
101 FILE *sysfsfp;
102 int ret;
103 DIR *dp;
104 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000105 char signchar, endianchar;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +0100106 unsigned padint;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100107 const struct dirent *ent;
108
109 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200110 if (ret < 0)
111 return -ENOMEM;
112
Jonathan Camerone58537c2010-10-08 12:14:14 +0100113 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
114 if (ret < 0) {
115 ret = -ENOMEM;
116 goto error_free_scan_el_dir;
117 }
118 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
119 if (ret < 0) {
120 ret = -ENOMEM;
121 goto error_free_builtname;
122 }
123
124 dp = opendir(scan_el_dir);
125 if (dp == NULL) {
126 ret = -errno;
127 goto error_free_builtname_generic;
128 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200129 ret = -ENOENT;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100130 while (ent = readdir(dp), ent != NULL)
131 /*
132 * Do we allow devices to override a generic name with
133 * a specific one?
134 */
135 if ((strcmp(builtname, ent->d_name) == 0) ||
136 (strcmp(builtname_generic, ent->d_name) == 0)) {
137 ret = asprintf(&filename,
138 "%s/%s", scan_el_dir, ent->d_name);
139 if (ret < 0) {
140 ret = -ENOMEM;
141 goto error_closedir;
142 }
143 sysfsfp = fopen(filename, "r");
144 if (sysfsfp == NULL) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100145 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200146 printf("failed to open %s\n", filename);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100147 goto error_free_filename;
148 }
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000149
150 ret = fscanf(sysfsfp,
151 "%ce:%c%u/%u>>%u",
152 &endianchar,
153 &signchar,
154 bits_used,
155 &padint, shift);
156 if (ret < 0) {
Peter Meerwald578f7372012-06-25 23:13:24 +0200157 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200158 printf("failed to pass scan type description\n");
Peter Meerwald578f7372012-06-25 23:13:24 +0200159 goto error_close_sysfsfp;
Hartmut Knaackdc8b5d62015-05-31 14:39:59 +0200160 } else if (ret != 5) {
161 ret = -EIO;
162 printf("scan type description didn't match\n");
163 goto error_close_sysfsfp;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000164 }
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000165 *be = (endianchar == 'b');
Jonathan Camerone58537c2010-10-08 12:14:14 +0100166 *bytes = padint / 8;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +0100167 if (*bits_used == 64)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100168 *mask = ~0;
169 else
170 *mask = (1 << *bits_used) - 1;
Hartmut Knaack33ebcb22015-05-31 14:40:19 +0200171 *is_signed = (signchar == 's');
Hartmut Knaack53118552015-05-31 14:40:14 +0200172 if (fclose(sysfsfp)) {
173 ret = -errno;
174 printf("Failed to close %s\n", filename);
175 goto error_free_filename;
176 }
177
Hartmut Knaackace76e42015-05-31 14:40:20 +0200178 sysfsfp = 0;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000179 free(filename);
180
181 filename = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100182 }
Peter Meerwald578f7372012-06-25 23:13:24 +0200183error_close_sysfsfp:
184 if (sysfsfp)
Hartmut Knaack53118552015-05-31 14:40:14 +0200185 if (fclose(sysfsfp))
186 perror("iioutils_get_type(): Failed to close file");
187
Jonathan Camerone58537c2010-10-08 12:14:14 +0100188error_free_filename:
189 if (filename)
190 free(filename);
191error_closedir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200192 if (closedir(dp) == -1)
193 perror("iioutils_get_type(): Failed to close directory");
194
Jonathan Camerone58537c2010-10-08 12:14:14 +0100195error_free_builtname_generic:
196 free(builtname_generic);
197error_free_builtname:
198 free(builtname);
199error_free_scan_el_dir:
200 free(scan_el_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200201
Jonathan Camerone58537c2010-10-08 12:14:14 +0100202 return ret;
203}
204
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200205/**
206 * iioutils_get_param_float() - read a float value from a channel parameter
207 * @output: output the float value
208 * @param_name: the parameter name to read
209 * @device_dir: the IIO device directory in sysfs
210 * @name: the channel name
211 * @generic_name: the channel type name
212 *
213 * Returns a value >= 0 on success, otherwise a negative error code.
214 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200215int iioutils_get_param_float(float *output,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100216 const char *param_name,
217 const char *device_dir,
218 const char *name,
219 const char *generic_name)
220{
221 FILE *sysfsfp;
222 int ret;
223 DIR *dp;
224 char *builtname, *builtname_generic;
225 char *filename = NULL;
226 const struct dirent *ent;
227
228 ret = asprintf(&builtname, "%s_%s", name, param_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200229 if (ret < 0)
230 return -ENOMEM;
231
Jonathan Camerone58537c2010-10-08 12:14:14 +0100232 ret = asprintf(&builtname_generic,
233 "%s_%s", generic_name, param_name);
234 if (ret < 0) {
235 ret = -ENOMEM;
236 goto error_free_builtname;
237 }
238 dp = opendir(device_dir);
239 if (dp == NULL) {
240 ret = -errno;
241 goto error_free_builtname_generic;
242 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200243 ret = -ENOENT;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100244 while (ent = readdir(dp), ent != NULL)
245 if ((strcmp(builtname, ent->d_name) == 0) ||
246 (strcmp(builtname_generic, ent->d_name) == 0)) {
247 ret = asprintf(&filename,
248 "%s/%s", device_dir, ent->d_name);
249 if (ret < 0) {
250 ret = -ENOMEM;
251 goto error_closedir;
252 }
253 sysfsfp = fopen(filename, "r");
254 if (!sysfsfp) {
255 ret = -errno;
256 goto error_free_filename;
257 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200258 errno = 0;
259 if (fscanf(sysfsfp, "%f", output) != 1)
260 ret = errno ? -errno : -ENODATA;
261
Jonathan Camerone58537c2010-10-08 12:14:14 +0100262 break;
263 }
264error_free_filename:
265 if (filename)
266 free(filename);
267error_closedir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200268 if (closedir(dp) == -1)
269 perror("iioutils_get_param_float(): Failed to close directory");
270
Jonathan Camerone58537c2010-10-08 12:14:14 +0100271error_free_builtname_generic:
272 free(builtname_generic);
273error_free_builtname:
274 free(builtname);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200275
Jonathan Camerone58537c2010-10-08 12:14:14 +0100276 return ret;
277}
278
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100279/**
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200280 * bsort_channel_array_by_index() - sort the array in index order
281 * @ci_array: the iio_channel_info array to be sorted
282 * @cnt: the amount of array elements
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100283 **/
284
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200285void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100286 int cnt)
287{
288
289 struct iio_channel_info temp;
290 int x, y;
291
292 for (x = 0; x < cnt; x++)
293 for (y = 0; y < (cnt - 1); y++)
294 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
295 temp = (*ci_array)[y + 1];
296 (*ci_array)[y + 1] = (*ci_array)[y];
297 (*ci_array)[y] = temp;
298 }
299}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100300
301/**
302 * build_channel_array() - function to figure out what channels are present
303 * @device_dir: the IIO device directory in sysfs
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200304 * @ci_array: output the resulting array of iio_channel_info
305 * @counter: output the amount of array elements
306 *
307 * Returns 0 on success, otherwise a negative error code.
Jonathan Camerone58537c2010-10-08 12:14:14 +0100308 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200309int build_channel_array(const char *device_dir,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100310 struct iio_channel_info **ci_array,
311 int *counter)
312{
313 DIR *dp;
314 FILE *sysfsfp;
Hartmut Knaack1e7c3472015-05-31 14:40:21 +0200315 int count = 0, i;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100316 struct iio_channel_info *current;
317 int ret;
318 const struct dirent *ent;
319 char *scan_el_dir;
320 char *filename;
321
322 *counter = 0;
323 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200324 if (ret < 0)
325 return -ENOMEM;
326
Jonathan Camerone58537c2010-10-08 12:14:14 +0100327 dp = opendir(scan_el_dir);
328 if (dp == NULL) {
329 ret = -errno;
330 goto error_free_name;
331 }
332 while (ent = readdir(dp), ent != NULL)
333 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
334 "_en") == 0) {
335 ret = asprintf(&filename,
336 "%s/%s", scan_el_dir, ent->d_name);
337 if (ret < 0) {
338 ret = -ENOMEM;
339 goto error_close_dir;
340 }
341 sysfsfp = fopen(filename, "r");
342 if (sysfsfp == NULL) {
343 ret = -errno;
344 free(filename);
345 goto error_close_dir;
346 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200347 errno = 0;
348 if (fscanf(sysfsfp, "%i", &ret) != 1) {
349 ret = errno ? -errno : -ENODATA;
350 if (fclose(sysfsfp))
351 perror("build_channel_array(): Failed to close file");
352
353 free(filename);
354 goto error_close_dir;
355 }
356
Jonathan Camerone58537c2010-10-08 12:14:14 +0100357 if (ret == 1)
358 (*counter)++;
Hartmut Knaack53118552015-05-31 14:40:14 +0200359 if (fclose(sysfsfp)) {
360 ret = -errno;
361 free(filename);
362 goto error_close_dir;
363 }
364
Jonathan Camerone58537c2010-10-08 12:14:14 +0100365 free(filename);
366 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100367 *ci_array = malloc(sizeof(**ci_array) * (*counter));
Jonathan Camerone58537c2010-10-08 12:14:14 +0100368 if (*ci_array == NULL) {
369 ret = -ENOMEM;
370 goto error_close_dir;
371 }
372 seekdir(dp, 0);
373 while (ent = readdir(dp), ent != NULL) {
374 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
375 "_en") == 0) {
Craig Markwardt66c65d92014-01-01 15:38:52 +0000376 int current_enabled = 0;
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300377
Jonathan Camerone58537c2010-10-08 12:14:14 +0100378 current = &(*ci_array)[count++];
379 ret = asprintf(&filename,
380 "%s/%s", scan_el_dir, ent->d_name);
381 if (ret < 0) {
382 ret = -ENOMEM;
383 /* decrement count to avoid freeing name */
384 count--;
385 goto error_cleanup_array;
386 }
387 sysfsfp = fopen(filename, "r");
388 if (sysfsfp == NULL) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100389 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200390 free(filename);
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200391 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100392 goto error_cleanup_array;
393 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200394 errno = 0;
395 if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
396 ret = errno ? -errno : -ENODATA;
397 free(filename);
398 count--;
399 goto error_cleanup_array;
400 }
401
402 if (fclose(sysfsfp)) {
403 ret = -errno;
404 free(filename);
405 count--;
406 goto error_cleanup_array;
407 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100408
Craig Markwardt66c65d92014-01-01 15:38:52 +0000409 if (!current_enabled) {
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100410 free(filename);
411 count--;
412 continue;
413 }
414
Jonathan Camerone58537c2010-10-08 12:14:14 +0100415 current->scale = 1.0;
416 current->offset = 0;
417 current->name = strndup(ent->d_name,
418 strlen(ent->d_name) -
419 strlen("_en"));
420 if (current->name == NULL) {
421 free(filename);
422 ret = -ENOMEM;
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200423 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100424 goto error_cleanup_array;
425 }
426 /* Get the generic and specific name elements */
427 ret = iioutils_break_up_name(current->name,
428 &current->generic_name);
429 if (ret) {
430 free(filename);
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200431 free(current->name);
432 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100433 goto error_cleanup_array;
434 }
435 ret = asprintf(&filename,
436 "%s/%s_index",
437 scan_el_dir,
438 current->name);
439 if (ret < 0) {
440 free(filename);
441 ret = -ENOMEM;
442 goto error_cleanup_array;
443 }
444 sysfsfp = fopen(filename, "r");
Hartmut Knaack53118552015-05-31 14:40:14 +0200445 if (sysfsfp == NULL) {
446 ret = -errno;
447 printf("failed to open %s\n", filename);
448 free(filename);
449 goto error_cleanup_array;
450 }
451
452 errno = 0;
453 if (fscanf(sysfsfp, "%u", &current->index) != 1) {
454 ret = errno ? -errno : -ENODATA;
455 if (fclose(sysfsfp))
456 perror("build_channel_array(): Failed to close file");
457
458 free(filename);
459 goto error_cleanup_array;
460 }
461
462 if (fclose(sysfsfp)) {
463 ret = -errno;
464 free(filename);
465 goto error_cleanup_array;
466 }
467
Jonathan Camerone58537c2010-10-08 12:14:14 +0100468 free(filename);
469 /* Find the scale */
470 ret = iioutils_get_param_float(&current->scale,
471 "scale",
472 device_dir,
473 current->name,
474 current->generic_name);
475 if (ret < 0)
476 goto error_cleanup_array;
477 ret = iioutils_get_param_float(&current->offset,
478 "offset",
479 device_dir,
480 current->name,
481 current->generic_name);
482 if (ret < 0)
483 goto error_cleanup_array;
484 ret = iioutils_get_type(&current->is_signed,
485 &current->bytes,
486 &current->bits_used,
Jonathan Cameron52615d42011-05-18 14:41:19 +0100487 &current->shift,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100488 &current->mask,
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000489 &current->be,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100490 device_dir,
491 current->name,
492 current->generic_name);
Hartmut Knaack53118552015-05-31 14:40:14 +0200493 if (ret < 0)
494 goto error_cleanup_array;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100495 }
496 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100497
Hartmut Knaack53118552015-05-31 14:40:14 +0200498 if (closedir(dp) == -1) {
499 ret = -errno;
500 goto error_cleanup_array;
501 }
502
Hartmut Knaack66dd08f2015-05-31 14:39:43 +0200503 free(scan_el_dir);
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100504 /* reorder so that the array is in index order */
505 bsort_channel_array_by_index(ci_array, *counter);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100506
507 return 0;
508
509error_cleanup_array:
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200510 for (i = count - 1; i >= 0; i--) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100511 free((*ci_array)[i].name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200512 free((*ci_array)[i].generic_name);
513 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100514 free(*ci_array);
515error_close_dir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200516 if (dp)
517 if (closedir(dp) == -1)
518 perror("build_channel_array(): Failed to close dir");
519
Jonathan Camerone58537c2010-10-08 12:14:14 +0100520error_free_name:
521 free(scan_el_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200522
Jonathan Camerone58537c2010-10-08 12:14:14 +0100523 return ret;
524}
525
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200526int calc_digits(int num)
527{
528 int count = 0;
529
530 while (num != 0) {
531 num /= 10;
532 count++;
533 }
534
535 return count;
536}
537
Jonathan Camerone58537c2010-10-08 12:14:14 +0100538/**
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100539 * find_type_by_name() - function to match top level types by name
540 * @name: top level type instance name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200541 * @type: the type of top level instance being searched
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100542 *
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200543 * Returns the device number of a matched IIO device on success, otherwise a
544 * negative error code.
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100545 * Typical types this is used for are device and trigger.
546 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200547int find_type_by_name(const char *name, const char *type)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100548{
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100549 const struct dirent *ent;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200550 int number, numstrlen, ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100551
552 FILE *nameFile;
553 DIR *dp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100554 char thisname[IIO_MAX_NAME_LENGTH];
555 char *filename;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100556
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100557 dp = opendir(iio_dir);
558 if (dp == NULL) {
Peter Meerwaldc866ffc2012-07-01 00:47:41 +0200559 printf("No industrialio devices available\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100560 return -ENODEV;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100561 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100562
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100563 while (ent = readdir(dp), ent != NULL) {
564 if (strcmp(ent->d_name, ".") != 0 &&
565 strcmp(ent->d_name, "..") != 0 &&
566 strlen(ent->d_name) > strlen(type) &&
567 strncmp(ent->d_name, type, strlen(type)) == 0) {
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200568 errno = 0;
569 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
570 if (ret < 0) {
571 ret = -errno;
572 printf("failed to read element number\n");
573 goto error_close_dir;
574 } else if (ret != 1) {
575 ret = -EIO;
576 printf("failed to match element number\n");
577 goto error_close_dir;
578 }
579
580 numstrlen = calc_digits(number);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100581 /* verify the next character is not a colon */
582 if (strncmp(ent->d_name + strlen(type) + numstrlen,
583 ":",
584 1) != 0) {
585 filename = malloc(strlen(iio_dir)
586 + strlen(type)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100587 + numstrlen
Barry Songb6ee30a2010-05-25 17:40:04 +0800588 + 6);
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200589 if (filename == NULL) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200590 ret = -ENOMEM;
591 goto error_close_dir;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200592 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200593
594 ret = sprintf(filename, "%s%s%d/name", iio_dir,
595 type, number);
596 if (ret < 0) {
597 free(filename);
598 goto error_close_dir;
599 }
600
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100601 nameFile = fopen(filename, "r");
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200602 if (!nameFile) {
603 free(filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100604 continue;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200605 }
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100606 free(filename);
Hartmut Knaack53118552015-05-31 14:40:14 +0200607 errno = 0;
608 if (fscanf(nameFile, "%s", thisname) != 1) {
609 ret = errno ? -errno : -ENODATA;
610 goto error_close_dir;
611 }
612
613 if (fclose(nameFile)) {
614 ret = -errno;
615 goto error_close_dir;
616 }
617
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200618 if (strcmp(name, thisname) == 0) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200619 if (closedir(dp) == -1)
620 return -errno;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200621 return number;
622 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100623 }
624 }
625 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200626 if (closedir(dp) == -1)
627 return -errno;
628
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100629 return -ENODEV;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200630
631error_close_dir:
632 if (closedir(dp) == -1)
633 perror("find_type_by_name(): Failed to close directory");
634 return ret;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100635}
636
Hartmut Knaack2156b172015-05-31 14:40:01 +0200637static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100638{
Sebastian Andrzej Siewior11cb4542013-10-07 13:42:00 +0100639 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100640 FILE *sysfsfp;
641 int test;
642 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300643
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100644 if (temp == NULL)
645 return -ENOMEM;
Hartmut Knaack53118552015-05-31 14:40:14 +0200646 ret = sprintf(temp, "%s/%s", basedir, filename);
647 if (ret < 0)
648 goto error_free;
649
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100650 sysfsfp = fopen(temp, "w");
651 if (sysfsfp == NULL) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100652 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200653 printf("failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100654 goto error_free;
655 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200656 ret = fprintf(sysfsfp, "%d", val);
657 if (ret < 0) {
658 if (fclose(sysfsfp))
659 perror("_write_sysfs_int(): Failed to close dir");
660
661 goto error_free;
662 }
663
664 if (fclose(sysfsfp)) {
665 ret = -errno;
666 goto error_free;
667 }
668
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100669 if (verify) {
670 sysfsfp = fopen(temp, "r");
671 if (sysfsfp == NULL) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100672 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200673 printf("failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100674 goto error_free;
675 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200676 if (fscanf(sysfsfp, "%d", &test) != 1) {
677 ret = errno ? -errno : -ENODATA;
678 if (fclose(sysfsfp))
679 perror("_write_sysfs_int(): Failed to close dir");
680
681 goto error_free;
682 }
683
684 if (fclose(sysfsfp)) {
685 ret = -errno;
686 goto error_free;
687 }
688
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100689 if (test != val) {
690 printf("Possible failure in int write %d to %s%s\n",
691 val,
692 basedir,
693 filename);
694 ret = -1;
695 }
696 }
697error_free:
698 free(temp);
699 return ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100700}
701
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200702/**
703 * write_sysfs_int() - write an integer value to a sysfs file
704 * @filename: name of the file to write to
705 * @basedir: the sysfs directory in which the file is to be found
706 * @val: integer value to write to file
707 *
708 * Returns a value >= 0 on success, otherwise a negative error code.
709 **/
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100710int write_sysfs_int(char *filename, char *basedir, int val)
711{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100712 return _write_sysfs_int(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100713}
714
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200715/**
716 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
717 * and verify
718 * @filename: name of the file to write to
719 * @basedir: the sysfs directory in which the file is to be found
720 * @val: integer value to write to file
721 *
722 * Returns a value >= 0 on success, otherwise a negative error code.
723 **/
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100724int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
725{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100726 return _write_sysfs_int(filename, basedir, val, 1);
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100727}
728
Hartmut Knaack2156b172015-05-31 14:40:01 +0200729static int _write_sysfs_string(char *filename, char *basedir, char *val,
730 int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100731{
Jonathan Camerone58537c2010-10-08 12:14:14 +0100732 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100733 FILE *sysfsfp;
734 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300735
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100736 if (temp == NULL) {
737 printf("Memory allocation failed\n");
738 return -ENOMEM;
739 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200740 ret = sprintf(temp, "%s/%s", basedir, filename);
741 if (ret < 0)
742 goto error_free;
743
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100744 sysfsfp = fopen(temp, "w");
745 if (sysfsfp == NULL) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100746 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200747 printf("Could not open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100748 goto error_free;
749 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200750 ret = fprintf(sysfsfp, "%s", val);
751 if (ret < 0) {
752 if (fclose(sysfsfp))
753 perror("_write_sysfs_string(): Failed to close dir");
754
755 goto error_free;
756 }
757
758 if (fclose(sysfsfp)) {
759 ret = -errno;
760 goto error_free;
761 }
762
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100763 if (verify) {
764 sysfsfp = fopen(temp, "r");
765 if (sysfsfp == NULL) {
766 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200767 printf("could not open file to verify\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100768 goto error_free;
769 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200770 if (fscanf(sysfsfp, "%s", temp) != 1) {
771 ret = errno ? -errno : -ENODATA;
772 if (fclose(sysfsfp))
773 perror("_write_sysfs_string(): Failed to close dir");
774
775 goto error_free;
776 }
777
778 if (fclose(sysfsfp)) {
779 ret = -errno;
780 goto error_free;
781 }
782
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100783 if (strcmp(temp, val) != 0) {
784 printf("Possible failure in string write of %s "
785 "Should be %s "
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300786 "written to %s\%s\n",
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100787 temp,
788 val,
789 basedir,
790 filename);
791 ret = -1;
792 }
793 }
794error_free:
795 free(temp);
796
797 return ret;
798}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100799
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100800/**
801 * write_sysfs_string_and_verify() - string write, readback and verify
802 * @filename: name of file to write to
803 * @basedir: the sysfs directory in which the file is to be found
804 * @val: the string to write
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200805 *
806 * Returns a value >= 0 on success, otherwise a negative error code.
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100807 **/
808int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
809{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100810 return _write_sysfs_string(filename, basedir, val, 1);
811}
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100812
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200813/**
814 * write_sysfs_string() - write string to a sysfs file
815 * @filename: name of file to write to
816 * @basedir: the sysfs directory in which the file is to be found
817 * @val: the string to write
818 *
819 * Returns a value >= 0 on success, otherwise a negative error code.
820 **/
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100821int write_sysfs_string(char *filename, char *basedir, char *val)
822{
823 return _write_sysfs_string(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100824}
825
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200826/**
827 * read_sysfs_posint() - read an integer value from file
828 * @filename: name of file to read from
829 * @basedir: the sysfs directory in which the file is to be found
830 *
831 * Returns the read integer value >= 0 on success, otherwise a negative error
832 * code.
833 **/
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100834int read_sysfs_posint(char *filename, char *basedir)
835{
836 int ret;
837 FILE *sysfsfp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100838 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300839
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100840 if (temp == NULL) {
841 printf("Memory allocation failed");
842 return -ENOMEM;
843 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200844 ret = sprintf(temp, "%s/%s", basedir, filename);
845 if (ret < 0)
846 goto error_free;
847
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100848 sysfsfp = fopen(temp, "r");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100849 if (sysfsfp == NULL) {
850 ret = -errno;
851 goto error_free;
852 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200853 errno = 0;
854 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
855 ret = errno ? -errno : -ENODATA;
856 if (fclose(sysfsfp))
857 perror("read_sysfs_posint(): Failed to close dir");
858
859 goto error_free;
860 }
861
862 if (fclose(sysfsfp))
863 ret = -errno;
864
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100865error_free:
866 free(temp);
867 return ret;
868}
869
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200870/**
871 * read_sysfs_float() - read a float value from file
872 * @filename: name of file to read from
873 * @basedir: the sysfs directory in which the file is to be found
874 * @val: output the read float value
875 *
876 * Returns a value >= 0 on success, otherwise a negative error code.
877 **/
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100878int read_sysfs_float(char *filename, char *basedir, float *val)
879{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000880 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100881 FILE *sysfsfp;
882 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300883
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100884 if (temp == NULL) {
885 printf("Memory allocation failed");
886 return -ENOMEM;
887 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200888 ret = sprintf(temp, "%s/%s", basedir, filename);
889 if (ret < 0)
890 goto error_free;
891
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100892 sysfsfp = fopen(temp, "r");
893 if (sysfsfp == NULL) {
894 ret = -errno;
895 goto error_free;
896 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200897 errno = 0;
898 if (fscanf(sysfsfp, "%f\n", val) != 1) {
899 ret = errno ? -errno : -ENODATA;
900 if (fclose(sysfsfp))
901 perror("read_sysfs_float(): Failed to close dir");
902
903 goto error_free;
904 }
905
906 if (fclose(sysfsfp))
907 ret = -errno;
908
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100909error_free:
910 free(temp);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100911 return ret;
912}
Manuel Stahl49d916e2014-05-02 13:23:00 +0100913
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200914/**
915 * read_sysfs_string() - read a string from file
916 * @filename: name of file to read from
917 * @basedir: the sysfs directory in which the file is to be found
918 * @str: output the read string
919 *
920 * Returns a value >= 0 on success, otherwise a negative error code.
921 **/
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000922int read_sysfs_string(const char *filename, const char *basedir, char *str)
Manuel Stahl49d916e2014-05-02 13:23:00 +0100923{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000924 int ret = 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100925 FILE *sysfsfp;
926 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300927
Manuel Stahl49d916e2014-05-02 13:23:00 +0100928 if (temp == NULL) {
929 printf("Memory allocation failed");
930 return -ENOMEM;
931 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200932 ret = sprintf(temp, "%s/%s", basedir, filename);
933 if (ret < 0)
934 goto error_free;
935
Manuel Stahl49d916e2014-05-02 13:23:00 +0100936 sysfsfp = fopen(temp, "r");
937 if (sysfsfp == NULL) {
938 ret = -errno;
939 goto error_free;
940 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200941 errno = 0;
942 if (fscanf(sysfsfp, "%s\n", str) != 1) {
943 ret = errno ? -errno : -ENODATA;
944 if (fclose(sysfsfp))
945 perror("read_sysfs_string(): Failed to close dir");
946
947 goto error_free;
948 }
949
950 if (fclose(sysfsfp))
951 ret = -errno;
952
Manuel Stahl49d916e2014-05-02 13:23:00 +0100953error_free:
954 free(temp);
955 return ret;
956}
Cristina Opriceana37e3be92015-03-04 02:37:15 +0200957
958#endif /* _IIO_UTILS_H */