iio_utils.c 21.3 KB
Newer Older
1 2 3 4 5 6 7 8
/* IIO - useful set of util functionality
 *
 * Copyright (c) 2008 Jonathan Cameron
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */
9 10
#ifndef _IIO_UTILS_H
#define _IIO_UTILS_H
11

12 13
#include <string.h>
#include <stdlib.h>
14 15
#include <stdio.h>
#include <stdint.h>
16
#include <dirent.h>
17
#include <errno.h>
18 19
#include <ctype.h>
#include "iio_utils.h"
20

21 22
const char *iio_dir = "/sys/bus/iio/devices/";

23 24 25 26 27
static char * const iio_direction[] = {
	"in",
	"out",
};

28 29 30 31
/**
 * iioutils_break_up_name() - extract generic name from full channel name
 * @full_name: the full channel name
 * @generic_name: the output generic channel name
32 33
 *
 * Returns 0 on success, or a negative error code if string extraction failed.
34
 **/
35
int iioutils_break_up_name(const char *full_name,
36 37 38 39
				  char **generic_name)
{
	char *current;
	char *w, *r;
40
	char *working, *prefix = "";
41
	int i, ret;
42 43 44 45 46 47 48

	for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
		if (!strncmp(full_name, iio_direction[i],
			     strlen(iio_direction[i]))) {
			prefix = iio_direction[i];
			break;
		}
49

50
	current = strdup(full_name + strlen(prefix) + 1);
51 52 53
	if (!current)
		return -ENOMEM;

54
	working = strtok(current, "_\0");
55 56 57 58
	if (!working) {
		free(current);
		return -EINVAL;
	}
59

60 61 62
	w = working;
	r = working;

63
	while (*r != '\0') {
64 65 66 67 68 69 70
		if (!isdigit(*r)) {
			*w = *r;
			w++;
		}
		r++;
	}
	*w = '\0';
71
	ret = asprintf(generic_name, "%s_%s", prefix, working);
72 73
	free(current);

74
	return (ret == -1) ? -ENOMEM : 0;
75 76 77 78 79 80
}

/**
 * iioutils_get_type() - find and process _type attribute data
 * @is_signed: output whether channel is signed
 * @bytes: output how many bytes the channel storage occupies
81 82
 * @bits_used: output number of valid bits of data
 * @shift: output amount of bits to shift right data before applying bit mask
83
 * @mask: output a bit mask for the raw data
84 85
 * @be: output if data in big endian
 * @device_dir: the IIO device directory
86 87
 * @name: the channel name
 * @generic_name: the channel type name
88 89
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
90
 **/
91
int iioutils_get_type(unsigned *is_signed,
92 93
			     unsigned *bytes,
			     unsigned *bits_used,
94
			     unsigned *shift,
95
			     uint64_t *mask,
96
			     unsigned *be,
97 98 99 100 101 102 103 104
			     const char *device_dir,
			     const char *name,
			     const char *generic_name)
{
	FILE *sysfsfp;
	int ret;
	DIR *dp;
	char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
105
	char signchar, endianchar;
106
	unsigned padint;
107 108 109
	const struct dirent *ent;

	ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
110 111 112
	if (ret < 0)
		return -ENOMEM;

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
	if (ret < 0) {
		ret = -ENOMEM;
		goto error_free_scan_el_dir;
	}
	ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
	if (ret < 0) {
		ret = -ENOMEM;
		goto error_free_builtname;
	}

	dp = opendir(scan_el_dir);
	if (dp == NULL) {
		ret = -errno;
		goto error_free_builtname_generic;
	}
129
	ret = -ENOENT;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	while (ent = readdir(dp), ent != NULL)
		/*
		 * Do we allow devices to override a generic name with
		 * a specific one?
		 */
		if ((strcmp(builtname, ent->d_name) == 0) ||
		    (strcmp(builtname_generic, ent->d_name) == 0)) {
			ret = asprintf(&filename,
				       "%s/%s", scan_el_dir, ent->d_name);
			if (ret < 0) {
				ret = -ENOMEM;
				goto error_closedir;
			}
			sysfsfp = fopen(filename, "r");
			if (sysfsfp == NULL) {
				ret = -errno;
H
Hartmut Knaack 已提交
146
				printf("failed to open %s\n", filename);
147 148
				goto error_free_filename;
			}
149 150 151 152 153 154 155 156

			ret = fscanf(sysfsfp,
				     "%ce:%c%u/%u>>%u",
				     &endianchar,
				     &signchar,
				     bits_used,
				     &padint, shift);
			if (ret < 0) {
157
				ret = -errno;
H
Hartmut Knaack 已提交
158
				printf("failed to pass scan type description\n");
159
				goto error_close_sysfsfp;
160 161 162 163
			} else if (ret != 5) {
				ret = -EIO;
				printf("scan type description didn't match\n");
				goto error_close_sysfsfp;
164
			}
165
			*be = (endianchar == 'b');
166
			*bytes = padint / 8;
167
			if (*bits_used == 64)
168 169 170
				*mask = ~0;
			else
				*mask = (1 << *bits_used) - 1;
171
			*is_signed = (signchar == 's');
172 173 174 175 176 177
			if (fclose(sysfsfp)) {
				ret = -errno;
				printf("Failed to close %s\n", filename);
				goto error_free_filename;
			}

178 179 180
			free(filename);

			filename = 0;
181
			sysfsfp = 0;
182
		}
183 184
error_close_sysfsfp:
	if (sysfsfp)
185 186 187
		if (fclose(sysfsfp))
			perror("iioutils_get_type(): Failed to close file");

188 189 190 191
error_free_filename:
	if (filename)
		free(filename);
error_closedir:
192 193 194
	if (closedir(dp) == -1)
		perror("iioutils_get_type(): Failed to close directory");

195 196 197 198 199 200
error_free_builtname_generic:
	free(builtname_generic);
error_free_builtname:
	free(builtname);
error_free_scan_el_dir:
	free(scan_el_dir);
201

202 203 204
	return ret;
}

205 206 207 208 209 210 211 212 213 214
/**
 * iioutils_get_param_float() - read a float value from a channel parameter
 * @output: output the float value
 * @param_name: the parameter name to read
 * @device_dir: the IIO device directory in sysfs
 * @name: the channel name
 * @generic_name: the channel type name
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
 **/
215
int iioutils_get_param_float(float *output,
216 217 218 219 220 221 222 223 224 225 226 227 228
				    const char *param_name,
				    const char *device_dir,
				    const char *name,
				    const char *generic_name)
{
	FILE *sysfsfp;
	int ret;
	DIR *dp;
	char *builtname, *builtname_generic;
	char *filename = NULL;
	const struct dirent *ent;

	ret = asprintf(&builtname, "%s_%s", name, param_name);
229 230 231
	if (ret < 0)
		return -ENOMEM;

232 233 234 235 236 237 238 239 240 241 242
	ret = asprintf(&builtname_generic,
		       "%s_%s", generic_name, param_name);
	if (ret < 0) {
		ret = -ENOMEM;
		goto error_free_builtname;
	}
	dp = opendir(device_dir);
	if (dp == NULL) {
		ret = -errno;
		goto error_free_builtname_generic;
	}
243
	ret = -ENOENT;
244 245 246 247 248 249 250 251 252 253 254 255 256 257
	while (ent = readdir(dp), ent != NULL)
		if ((strcmp(builtname, ent->d_name) == 0) ||
		    (strcmp(builtname_generic, ent->d_name) == 0)) {
			ret = asprintf(&filename,
				       "%s/%s", device_dir, ent->d_name);
			if (ret < 0) {
				ret = -ENOMEM;
				goto error_closedir;
			}
			sysfsfp = fopen(filename, "r");
			if (!sysfsfp) {
				ret = -errno;
				goto error_free_filename;
			}
258 259 260 261
			errno = 0;
			if (fscanf(sysfsfp, "%f", output) != 1)
				ret = errno ? -errno : -ENODATA;

262 263 264 265 266 267
			break;
		}
error_free_filename:
	if (filename)
		free(filename);
error_closedir:
268 269 270
	if (closedir(dp) == -1)
		perror("iioutils_get_param_float(): Failed to close directory");

271 272 273 274
error_free_builtname_generic:
	free(builtname_generic);
error_free_builtname:
	free(builtname);
275

276 277 278
	return ret;
}

279
/**
280 281 282
 * bsort_channel_array_by_index() - sort the array in index order
 * @ci_array: the iio_channel_info array to be sorted
 * @cnt: the amount of array elements
283 284
 **/

285
void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
286 287 288 289 290 291 292 293 294 295 296 297 298 299
					 int cnt)
{

	struct iio_channel_info temp;
	int x, y;

	for (x = 0; x < cnt; x++)
		for (y = 0; y < (cnt - 1); y++)
			if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
				temp = (*ci_array)[y + 1];
				(*ci_array)[y + 1] = (*ci_array)[y];
				(*ci_array)[y] = temp;
			}
}
300 301 302 303

/**
 * build_channel_array() - function to figure out what channels are present
 * @device_dir: the IIO device directory in sysfs
304 305 306 307
 * @ci_array: output the resulting array of iio_channel_info
 * @counter: output the amount of array elements
 *
 * Returns 0 on success, otherwise a negative error code.
308
 **/
309
int build_channel_array(const char *device_dir,
310 311 312 313 314
			      struct iio_channel_info **ci_array,
			      int *counter)
{
	DIR *dp;
	FILE *sysfsfp;
315
	int count, i;
316 317 318 319 320 321 322 323
	struct iio_channel_info *current;
	int ret;
	const struct dirent *ent;
	char *scan_el_dir;
	char *filename;

	*counter = 0;
	ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
324 325 326
	if (ret < 0)
		return -ENOMEM;

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	dp = opendir(scan_el_dir);
	if (dp == NULL) {
		ret = -errno;
		goto error_free_name;
	}
	while (ent = readdir(dp), ent != NULL)
		if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
			   "_en") == 0) {
			ret = asprintf(&filename,
				       "%s/%s", scan_el_dir, ent->d_name);
			if (ret < 0) {
				ret = -ENOMEM;
				goto error_close_dir;
			}
			sysfsfp = fopen(filename, "r");
			if (sysfsfp == NULL) {
				ret = -errno;
				free(filename);
				goto error_close_dir;
			}
347 348 349 350 351 352 353 354 355 356
			errno = 0;
			if (fscanf(sysfsfp, "%i", &ret) != 1) {
				ret = errno ? -errno : -ENODATA;
				if (fclose(sysfsfp))
					perror("build_channel_array(): Failed to close file");

				free(filename);
				goto error_close_dir;
			}

357 358
			if (ret == 1)
				(*counter)++;
359 360 361 362 363 364
			if (fclose(sysfsfp)) {
				ret = -errno;
				free(filename);
				goto error_close_dir;
			}

365 366
			free(filename);
		}
367
	*ci_array = malloc(sizeof(**ci_array) * (*counter));
368 369 370 371 372
	if (*ci_array == NULL) {
		ret = -ENOMEM;
		goto error_close_dir;
	}
	seekdir(dp, 0);
373
	count = 0;
374 375 376
	while (ent = readdir(dp), ent != NULL) {
		if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
			   "_en") == 0) {
377
			int current_enabled = 0;
378

379 380 381 382 383 384 385 386 387 388 389 390
			current = &(*ci_array)[count++];
			ret = asprintf(&filename,
				       "%s/%s", scan_el_dir, ent->d_name);
			if (ret < 0) {
				ret = -ENOMEM;
				/* decrement count to avoid freeing name */
				count--;
				goto error_cleanup_array;
			}
			sysfsfp = fopen(filename, "r");
			if (sysfsfp == NULL) {
				ret = -errno;
H
Hartmut Knaack 已提交
391
				free(filename);
392
				count--;
393 394
				goto error_cleanup_array;
			}
395 396 397 398 399 400 401 402 403 404 405 406 407 408
			errno = 0;
			if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
				ret = errno ? -errno : -ENODATA;
				free(filename);
				count--;
				goto error_cleanup_array;
			}

			if (fclose(sysfsfp)) {
				ret = -errno;
				free(filename);
				count--;
				goto error_cleanup_array;
			}
409

410
			if (!current_enabled) {
411 412 413 414 415
				free(filename);
				count--;
				continue;
			}

416 417 418 419 420 421 422 423
			current->scale = 1.0;
			current->offset = 0;
			current->name = strndup(ent->d_name,
						strlen(ent->d_name) -
						strlen("_en"));
			if (current->name == NULL) {
				free(filename);
				ret = -ENOMEM;
424
				count--;
425 426 427 428 429 430 431
				goto error_cleanup_array;
			}
			/* Get the generic and specific name elements */
			ret = iioutils_break_up_name(current->name,
						     &current->generic_name);
			if (ret) {
				free(filename);
432 433
				free(current->name);
				count--;
434 435 436 437 438 439 440 441 442 443 444 445
				goto error_cleanup_array;
			}
			ret = asprintf(&filename,
				       "%s/%s_index",
				       scan_el_dir,
				       current->name);
			if (ret < 0) {
				free(filename);
				ret = -ENOMEM;
				goto error_cleanup_array;
			}
			sysfsfp = fopen(filename, "r");
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
			if (sysfsfp == NULL) {
				ret = -errno;
				printf("failed to open %s\n", filename);
				free(filename);
				goto error_cleanup_array;
			}

			errno = 0;
			if (fscanf(sysfsfp, "%u", &current->index) != 1) {
				ret = errno ? -errno : -ENODATA;
				if (fclose(sysfsfp))
					perror("build_channel_array(): Failed to close file");

				free(filename);
				goto error_cleanup_array;
			}

			if (fclose(sysfsfp)) {
				ret = -errno;
				free(filename);
				goto error_cleanup_array;
			}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
			free(filename);
			/* Find the scale */
			ret = iioutils_get_param_float(&current->scale,
						       "scale",
						       device_dir,
						       current->name,
						       current->generic_name);
			if (ret < 0)
				goto error_cleanup_array;
			ret = iioutils_get_param_float(&current->offset,
						       "offset",
						       device_dir,
						       current->name,
						       current->generic_name);
			if (ret < 0)
				goto error_cleanup_array;
			ret = iioutils_get_type(&current->is_signed,
						&current->bytes,
						&current->bits_used,
488
						&current->shift,
489
						&current->mask,
490
						&current->be,
491 492 493
						device_dir,
						current->name,
						current->generic_name);
494 495
			if (ret < 0)
				goto error_cleanup_array;
496 497
		}
	}
498

499 500 501 502 503
	if (closedir(dp) == -1) {
		ret = -errno;
		goto error_cleanup_array;
	}

504
	free(scan_el_dir);
505 506
	/* reorder so that the array is in index order */
	bsort_channel_array_by_index(ci_array, *counter);
507 508 509 510

	return 0;

error_cleanup_array:
511
	for (i = count - 1;  i >= 0; i--) {
512
		free((*ci_array)[i].name);
513 514
		free((*ci_array)[i].generic_name);
	}
515 516
	free(*ci_array);
error_close_dir:
517 518 519 520
	if (dp)
		if (closedir(dp) == -1)
			perror("build_channel_array(): Failed to close dir");

521 522
error_free_name:
	free(scan_el_dir);
523

524 525 526
	return ret;
}

527 528 529 530 531 532 533 534 535 536 537 538
int calc_digits(int num)
{
	int count = 0;

	while (num != 0) {
		num /= 10;
		count++;
	}

	return count;
}

539 540 541
/**
 * find_type_by_name() - function to match top level types by name
 * @name: top level type instance name
542
 * @type: the type of top level instance being searched
543
 *
544 545
 * Returns the device number of a matched IIO device on success, otherwise a
 * negative error code.
546 547
 * Typical types this is used for are device and trigger.
 **/
548
int find_type_by_name(const char *name, const char *type)
549 550
{
	const struct dirent *ent;
551
	int number, numstrlen, ret;
552 553 554

	FILE *nameFile;
	DIR *dp;
555 556 557
	char thisname[IIO_MAX_NAME_LENGTH];
	char *filename;

558 559
	dp = opendir(iio_dir);
	if (dp == NULL) {
560
		printf("No industrialio devices available\n");
561
		return -ENODEV;
562
	}
563

564 565
	while (ent = readdir(dp), ent != NULL) {
		if (strcmp(ent->d_name, ".") != 0 &&
566 567 568
			strcmp(ent->d_name, "..") != 0 &&
			strlen(ent->d_name) > strlen(type) &&
			strncmp(ent->d_name, type, strlen(type)) == 0) {
569 570 571 572 573 574 575 576 577 578 579 580 581
			errno = 0;
			ret = sscanf(ent->d_name + strlen(type), "%d", &number);
			if (ret < 0) {
				ret = -errno;
				printf("failed to read element number\n");
				goto error_close_dir;
			} else if (ret != 1) {
				ret = -EIO;
				printf("failed to match element number\n");
				goto error_close_dir;
			}

			numstrlen = calc_digits(number);
582 583 584 585 586 587 588
			/* verify the next character is not a colon */
			if (strncmp(ent->d_name + strlen(type) + numstrlen,
					":",
					1) != 0) {
				filename = malloc(strlen(iio_dir)
						+ strlen(type)
						+ numstrlen
589
						+ 6);
590
				if (filename == NULL) {
591 592 593 594 595 596 597 598 599
					ret = -ENOMEM;
					goto error_close_dir;
				}

				ret = sprintf(filename, "%s%s%d/name", iio_dir,
					      type, number);
				if (ret < 0) {
					free(filename);
					goto error_close_dir;
600
				}
601

602
				nameFile = fopen(filename, "r");
603 604
				if (!nameFile) {
					free(filename);
605
					continue;
606
				}
607
				free(filename);
608 609 610 611 612 613 614 615 616 617 618
				errno = 0;
				if (fscanf(nameFile, "%s", thisname) != 1) {
					ret = errno ? -errno : -ENODATA;
					goto error_close_dir;
				}

				if (fclose(nameFile)) {
					ret = -errno;
					goto error_close_dir;
				}

619
				if (strcmp(name, thisname) == 0) {
620 621
					if (closedir(dp) == -1)
						return -errno;
622 623
					return number;
				}
624 625 626
			}
		}
	}
627 628 629
	if (closedir(dp) == -1)
		return -errno;

630
	return -ENODEV;
631 632 633 634 635

error_close_dir:
	if (closedir(dp) == -1)
		perror("find_type_by_name(): Failed to close directory");
	return ret;
636 637
}

638
static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
639
{
640
	int ret = 0;
641 642 643
	FILE *sysfsfp;
	int test;
	char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
644

645 646
	if (temp == NULL)
		return -ENOMEM;
647 648 649 650
	ret = sprintf(temp, "%s/%s", basedir, filename);
	if (ret < 0)
		goto error_free;

651
	sysfsfp = fopen(temp, "w");
652 653
	if (sysfsfp == NULL) {
		ret = -errno;
H
Hartmut Knaack 已提交
654
		printf("failed to open %s\n", temp);
655 656
		goto error_free;
	}
657 658 659 660 661 662 663 664 665 666 667 668 669
	ret = fprintf(sysfsfp, "%d", val);
	if (ret < 0) {
		if (fclose(sysfsfp))
			perror("_write_sysfs_int(): Failed to close dir");

		goto error_free;
	}

	if (fclose(sysfsfp)) {
		ret = -errno;
		goto error_free;
	}

670 671 672 673
	if (verify) {
		sysfsfp = fopen(temp, "r");
		if (sysfsfp == NULL) {
			ret = -errno;
H
Hartmut Knaack 已提交
674
			printf("failed to open %s\n", temp);
675 676
			goto error_free;
		}
677 678 679 680 681 682 683 684 685 686 687 688 689
		if (fscanf(sysfsfp, "%d", &test) != 1) {
			ret = errno ? -errno : -ENODATA;
			if (fclose(sysfsfp))
				perror("_write_sysfs_int(): Failed to close dir");

			goto error_free;
		}

		if (fclose(sysfsfp)) {
			ret = -errno;
			goto error_free;
		}

690 691 692 693 694 695 696 697 698 699 700 701 702
		if (test != val) {
			printf("Possible failure in int write %d to %s%s\n",
				val,
				basedir,
				filename);
			ret = -1;
		}
	}
error_free:
	free(temp);
	return ret;
}

703 704 705 706 707 708 709 710
/**
 * write_sysfs_int() - write an integer value to a sysfs file
 * @filename: name of the file to write to
 * @basedir: the sysfs directory in which the file is to be found
 * @val: integer value to write to file
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
 **/
711 712 713
int write_sysfs_int(char *filename, char *basedir, int val)
{
	return _write_sysfs_int(filename, basedir, val, 0);
714 715
}

716 717 718 719 720 721 722 723 724
/**
 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
 *				  and verify
 * @filename: name of the file to write to
 * @basedir: the sysfs directory in which the file is to be found
 * @val: integer value to write to file
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
 **/
725
int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
726 727 728 729
{
	return _write_sysfs_int(filename, basedir, val, 1);
}

730 731
static int _write_sysfs_string(char *filename, char *basedir, char *val,
			       int verify)
732
{
733
	int ret = 0;
734
	FILE  *sysfsfp;
735
	char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
736

737 738 739 740
	if (temp == NULL) {
		printf("Memory allocation failed\n");
		return -ENOMEM;
	}
741 742 743 744
	ret = sprintf(temp, "%s/%s", basedir, filename);
	if (ret < 0)
		goto error_free;

745
	sysfsfp = fopen(temp, "w");
746 747
	if (sysfsfp == NULL) {
		ret = -errno;
H
Hartmut Knaack 已提交
748
		printf("Could not open %s\n", temp);
749 750
		goto error_free;
	}
751 752 753 754 755 756 757 758 759 760 761 762 763
	ret = fprintf(sysfsfp, "%s", val);
	if (ret < 0) {
		if (fclose(sysfsfp))
			perror("_write_sysfs_string(): Failed to close dir");

		goto error_free;
	}

	if (fclose(sysfsfp)) {
		ret = -errno;
		goto error_free;
	}

764 765 766 767
	if (verify) {
		sysfsfp = fopen(temp, "r");
		if (sysfsfp == NULL) {
			ret = -errno;
H
Hartmut Knaack 已提交
768
			printf("could not open file to verify\n");
769 770
			goto error_free;
		}
771 772 773 774 775 776 777 778 779 780 781 782 783
		if (fscanf(sysfsfp, "%s", temp) != 1) {
			ret = errno ? -errno : -ENODATA;
			if (fclose(sysfsfp))
				perror("_write_sysfs_string(): Failed to close dir");

			goto error_free;
		}

		if (fclose(sysfsfp)) {
			ret = -errno;
			goto error_free;
		}

784 785 786
		if (strcmp(temp, val) != 0) {
			printf("Possible failure in string write of %s "
				"Should be %s "
L
Lucas De Marchi 已提交
787
				"written to %s\%s\n",
788 789 790 791 792 793
				temp,
				val,
				basedir,
				filename);
			ret = -1;
		}
794
	}
795 796
error_free:
	free(temp);
797

798
	return ret;
799
}
800

801 802 803 804 805
/**
 * write_sysfs_string_and_verify() - string write, readback and verify
 * @filename: name of file to write to
 * @basedir: the sysfs directory in which the file is to be found
 * @val: the string to write
806 807
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
808 809 810
 **/
int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
{
811 812
	return _write_sysfs_string(filename, basedir, val, 1);
}
813

814 815 816 817 818 819 820 821
/**
 * write_sysfs_string() - write string to a sysfs file
 * @filename: name of file to write to
 * @basedir: the sysfs directory in which the file is to be found
 * @val: the string to write
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
 **/
822 823 824
int write_sysfs_string(char *filename, char *basedir, char *val)
{
	return _write_sysfs_string(filename, basedir, val, 0);
825 826
}

827 828 829 830 831 832 833 834
/**
 * read_sysfs_posint() - read an integer value from file
 * @filename: name of file to read from
 * @basedir: the sysfs directory in which the file is to be found
 *
 * Returns the read integer value >= 0 on success, otherwise a negative error
 * code.
 **/
835 836 837 838
int read_sysfs_posint(char *filename, char *basedir)
{
	int ret;
	FILE  *sysfsfp;
839
	char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
840

841 842 843 844
	if (temp == NULL) {
		printf("Memory allocation failed");
		return -ENOMEM;
	}
845 846 847 848
	ret = sprintf(temp, "%s/%s", basedir, filename);
	if (ret < 0)
		goto error_free;

849
	sysfsfp = fopen(temp, "r");
850 851 852 853
	if (sysfsfp == NULL) {
		ret = -errno;
		goto error_free;
	}
854 855 856 857 858 859 860 861 862 863 864 865
	errno = 0;
	if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
		ret = errno ? -errno : -ENODATA;
		if (fclose(sysfsfp))
			perror("read_sysfs_posint(): Failed to close dir");

		goto error_free;
	}

	if (fclose(sysfsfp))
		ret = -errno;

866 867 868 869 870
error_free:
	free(temp);
	return ret;
}

871 872 873 874 875 876 877 878
/**
 * read_sysfs_float() - read a float value from file
 * @filename: name of file to read from
 * @basedir: the sysfs directory in which the file is to be found
 * @val: output the read float value
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
 **/
879 880
int read_sysfs_float(char *filename, char *basedir, float *val)
{
881
	int ret = 0;
882 883
	FILE  *sysfsfp;
	char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
884

885 886 887 888
	if (temp == NULL) {
		printf("Memory allocation failed");
		return -ENOMEM;
	}
889 890 891 892
	ret = sprintf(temp, "%s/%s", basedir, filename);
	if (ret < 0)
		goto error_free;

893 894 895 896 897
	sysfsfp = fopen(temp, "r");
	if (sysfsfp == NULL) {
		ret = -errno;
		goto error_free;
	}
898 899 900 901 902 903 904 905 906 907 908 909
	errno = 0;
	if (fscanf(sysfsfp, "%f\n", val) != 1) {
		ret = errno ? -errno : -ENODATA;
		if (fclose(sysfsfp))
			perror("read_sysfs_float(): Failed to close dir");

		goto error_free;
	}

	if (fclose(sysfsfp))
		ret = -errno;

910 911
error_free:
	free(temp);
912 913
	return ret;
}
914

915 916 917 918 919 920 921 922
/**
 * read_sysfs_string() - read a string from file
 * @filename: name of file to read from
 * @basedir: the sysfs directory in which the file is to be found
 * @str: output the read string
 *
 * Returns a value >= 0 on success, otherwise a negative error code.
 **/
923
int read_sysfs_string(const char *filename, const char *basedir, char *str)
924
{
925
	int ret = 0;
926 927
	FILE  *sysfsfp;
	char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
928

929 930 931 932
	if (temp == NULL) {
		printf("Memory allocation failed");
		return -ENOMEM;
	}
933 934 935 936
	ret = sprintf(temp, "%s/%s", basedir, filename);
	if (ret < 0)
		goto error_free;

937 938 939 940 941
	sysfsfp = fopen(temp, "r");
	if (sysfsfp == NULL) {
		ret = -errno;
		goto error_free;
	}
942 943 944 945 946 947 948 949 950 951 952 953
	errno = 0;
	if (fscanf(sysfsfp, "%s\n", str) != 1) {
		ret = errno ? -errno : -ENODATA;
		if (fclose(sysfsfp))
			perror("read_sysfs_string(): Failed to close dir");

		goto error_free;
	}

	if (fclose(sysfsfp))
		ret = -errno;

954 955 956 957
error_free:
	free(temp);
	return ret;
}
958 959

#endif /* _IIO_UTILS_H */