blob: 812153ff18c310af6f1744447107ac07e3574f1b [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
32 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020033int iioutils_break_up_name(const char *full_name,
Jonathan Camerone58537c2010-10-08 12:14:14 +010034 char **generic_name)
35{
36 char *current;
37 char *w, *r;
Irina Tirdead9d7b992015-03-27 13:53:00 +020038 char *working, *prefix = "";
39 int i;
Melike Yurtoglu79bdd482014-10-03 23:35:54 +030040
Irina Tirdead9d7b992015-03-27 13:53:00 +020041 for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
42 if (!strncmp(full_name, iio_direction[i],
43 strlen(iio_direction[i]))) {
44 prefix = iio_direction[i];
45 break;
46 }
47
48 current = strdup(full_name + strlen(prefix) + 1);
Jonathan Camerone58537c2010-10-08 12:14:14 +010049 working = strtok(current, "_\0");
Irina Tirdead9d7b992015-03-27 13:53:00 +020050
Jonathan Camerone58537c2010-10-08 12:14:14 +010051 w = working;
52 r = working;
53
Michael Hennerich8b68bb22011-03-08 08:55:48 +010054 while (*r != '\0') {
Jonathan Camerone58537c2010-10-08 12:14:14 +010055 if (!isdigit(*r)) {
56 *w = *r;
57 w++;
58 }
59 r++;
60 }
61 *w = '\0';
Irina Tirdead9d7b992015-03-27 13:53:00 +020062 asprintf(generic_name, "%s_%s", prefix, working);
Jonathan Camerone58537c2010-10-08 12:14:14 +010063 free(current);
64
65 return 0;
66}
67
68/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010069 * iioutils_get_type() - find and process _type attribute data
70 * @is_signed: output whether channel is signed
71 * @bytes: output how many bytes the channel storage occupies
72 * @mask: output a bit mask for the raw data
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000073 * @be: big endian
Jonathan Camerone58537c2010-10-08 12:14:14 +010074 * @device_dir: the iio device directory
75 * @name: the channel name
76 * @generic_name: the channel type name
77 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020078int iioutils_get_type(unsigned *is_signed,
Jonathan Camerone58537c2010-10-08 12:14:14 +010079 unsigned *bytes,
80 unsigned *bits_used,
Jonathan Cameron52615d42011-05-18 14:41:19 +010081 unsigned *shift,
Jonathan Camerone58537c2010-10-08 12:14:14 +010082 uint64_t *mask,
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000083 unsigned *be,
Jonathan Camerone58537c2010-10-08 12:14:14 +010084 const char *device_dir,
85 const char *name,
86 const char *generic_name)
87{
88 FILE *sysfsfp;
89 int ret;
90 DIR *dp;
91 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000092 char signchar, endianchar;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +010093 unsigned padint;
Jonathan Camerone58537c2010-10-08 12:14:14 +010094 const struct dirent *ent;
95
96 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
97 if (ret < 0) {
98 ret = -ENOMEM;
99 goto error_ret;
100 }
101 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
102 if (ret < 0) {
103 ret = -ENOMEM;
104 goto error_free_scan_el_dir;
105 }
106 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
107 if (ret < 0) {
108 ret = -ENOMEM;
109 goto error_free_builtname;
110 }
111
112 dp = opendir(scan_el_dir);
113 if (dp == NULL) {
114 ret = -errno;
115 goto error_free_builtname_generic;
116 }
117 while (ent = readdir(dp), ent != NULL)
118 /*
119 * Do we allow devices to override a generic name with
120 * a specific one?
121 */
122 if ((strcmp(builtname, ent->d_name) == 0) ||
123 (strcmp(builtname_generic, ent->d_name) == 0)) {
124 ret = asprintf(&filename,
125 "%s/%s", scan_el_dir, ent->d_name);
126 if (ret < 0) {
127 ret = -ENOMEM;
128 goto error_closedir;
129 }
130 sysfsfp = fopen(filename, "r");
131 if (sysfsfp == NULL) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100132 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200133 printf("failed to open %s\n", filename);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100134 goto error_free_filename;
135 }
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000136
137 ret = fscanf(sysfsfp,
138 "%ce:%c%u/%u>>%u",
139 &endianchar,
140 &signchar,
141 bits_used,
142 &padint, shift);
143 if (ret < 0) {
Peter Meerwald578f7372012-06-25 23:13:24 +0200144 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200145 printf("failed to pass scan type description\n");
Peter Meerwald578f7372012-06-25 23:13:24 +0200146 goto error_close_sysfsfp;
Hartmut Knaackdc8b5d62015-05-31 14:39:59 +0200147 } else if (ret != 5) {
148 ret = -EIO;
149 printf("scan type description didn't match\n");
150 goto error_close_sysfsfp;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000151 }
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000152 *be = (endianchar == 'b');
Jonathan Camerone58537c2010-10-08 12:14:14 +0100153 *bytes = padint / 8;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +0100154 if (*bits_used == 64)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100155 *mask = ~0;
156 else
157 *mask = (1 << *bits_used) - 1;
158 if (signchar == 's')
159 *is_signed = 1;
160 else
161 *is_signed = 0;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000162 fclose(sysfsfp);
163 free(filename);
164
165 filename = 0;
Peter Meerwald578f7372012-06-25 23:13:24 +0200166 sysfsfp = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100167 }
Peter Meerwald578f7372012-06-25 23:13:24 +0200168error_close_sysfsfp:
169 if (sysfsfp)
170 fclose(sysfsfp);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100171error_free_filename:
172 if (filename)
173 free(filename);
174error_closedir:
175 closedir(dp);
176error_free_builtname_generic:
177 free(builtname_generic);
178error_free_builtname:
179 free(builtname);
180error_free_scan_el_dir:
181 free(scan_el_dir);
182error_ret:
183 return ret;
184}
185
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200186int iioutils_get_param_float(float *output,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100187 const char *param_name,
188 const char *device_dir,
189 const char *name,
190 const char *generic_name)
191{
192 FILE *sysfsfp;
193 int ret;
194 DIR *dp;
195 char *builtname, *builtname_generic;
196 char *filename = NULL;
197 const struct dirent *ent;
198
199 ret = asprintf(&builtname, "%s_%s", name, param_name);
200 if (ret < 0) {
201 ret = -ENOMEM;
202 goto error_ret;
203 }
204 ret = asprintf(&builtname_generic,
205 "%s_%s", generic_name, param_name);
206 if (ret < 0) {
207 ret = -ENOMEM;
208 goto error_free_builtname;
209 }
210 dp = opendir(device_dir);
211 if (dp == NULL) {
212 ret = -errno;
213 goto error_free_builtname_generic;
214 }
215 while (ent = readdir(dp), ent != NULL)
216 if ((strcmp(builtname, ent->d_name) == 0) ||
217 (strcmp(builtname_generic, ent->d_name) == 0)) {
218 ret = asprintf(&filename,
219 "%s/%s", device_dir, ent->d_name);
220 if (ret < 0) {
221 ret = -ENOMEM;
222 goto error_closedir;
223 }
224 sysfsfp = fopen(filename, "r");
225 if (!sysfsfp) {
226 ret = -errno;
227 goto error_free_filename;
228 }
229 fscanf(sysfsfp, "%f", output);
230 break;
231 }
232error_free_filename:
233 if (filename)
234 free(filename);
235error_closedir:
236 closedir(dp);
237error_free_builtname_generic:
238 free(builtname_generic);
239error_free_builtname:
240 free(builtname);
241error_ret:
242 return ret;
243}
244
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100245/**
246 * bsort_channel_array_by_index() - reorder so that the array is in index order
247 *
248 **/
249
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200250void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100251 int cnt)
252{
253
254 struct iio_channel_info temp;
255 int x, y;
256
257 for (x = 0; x < cnt; x++)
258 for (y = 0; y < (cnt - 1); y++)
259 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
260 temp = (*ci_array)[y + 1];
261 (*ci_array)[y + 1] = (*ci_array)[y];
262 (*ci_array)[y] = temp;
263 }
264}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100265
266/**
267 * build_channel_array() - function to figure out what channels are present
268 * @device_dir: the IIO device directory in sysfs
269 * @
270 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200271int build_channel_array(const char *device_dir,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100272 struct iio_channel_info **ci_array,
273 int *counter)
274{
275 DIR *dp;
276 FILE *sysfsfp;
Peter Meerwald10937922012-06-25 23:12:12 +0200277 int count, i;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100278 struct iio_channel_info *current;
279 int ret;
280 const struct dirent *ent;
281 char *scan_el_dir;
282 char *filename;
283
284 *counter = 0;
285 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
286 if (ret < 0) {
287 ret = -ENOMEM;
288 goto error_ret;
289 }
290 dp = opendir(scan_el_dir);
291 if (dp == NULL) {
292 ret = -errno;
293 goto error_free_name;
294 }
295 while (ent = readdir(dp), ent != NULL)
296 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
297 "_en") == 0) {
298 ret = asprintf(&filename,
299 "%s/%s", scan_el_dir, ent->d_name);
300 if (ret < 0) {
301 ret = -ENOMEM;
302 goto error_close_dir;
303 }
304 sysfsfp = fopen(filename, "r");
305 if (sysfsfp == NULL) {
306 ret = -errno;
307 free(filename);
308 goto error_close_dir;
309 }
Toralf Förster34683a52014-05-24 14:25:00 +0100310 fscanf(sysfsfp, "%i", &ret);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100311 if (ret == 1)
312 (*counter)++;
313 fclose(sysfsfp);
314 free(filename);
315 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100316 *ci_array = malloc(sizeof(**ci_array) * (*counter));
Jonathan Camerone58537c2010-10-08 12:14:14 +0100317 if (*ci_array == NULL) {
318 ret = -ENOMEM;
319 goto error_close_dir;
320 }
321 seekdir(dp, 0);
Michael Hennerich7ccd4502011-02-24 16:34:53 +0100322 count = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100323 while (ent = readdir(dp), ent != NULL) {
324 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
325 "_en") == 0) {
Craig Markwardt66c65d92014-01-01 15:38:52 +0000326 int current_enabled = 0;
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300327
Jonathan Camerone58537c2010-10-08 12:14:14 +0100328 current = &(*ci_array)[count++];
329 ret = asprintf(&filename,
330 "%s/%s", scan_el_dir, ent->d_name);
331 if (ret < 0) {
332 ret = -ENOMEM;
333 /* decrement count to avoid freeing name */
334 count--;
335 goto error_cleanup_array;
336 }
337 sysfsfp = fopen(filename, "r");
338 if (sysfsfp == NULL) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100339 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200340 free(filename);
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200341 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100342 goto error_cleanup_array;
343 }
Toralf Förster34683a52014-05-24 14:25:00 +0100344 fscanf(sysfsfp, "%i", &current_enabled);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100345 fclose(sysfsfp);
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100346
Craig Markwardt66c65d92014-01-01 15:38:52 +0000347 if (!current_enabled) {
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100348 free(filename);
349 count--;
350 continue;
351 }
352
Jonathan Camerone58537c2010-10-08 12:14:14 +0100353 current->scale = 1.0;
354 current->offset = 0;
355 current->name = strndup(ent->d_name,
356 strlen(ent->d_name) -
357 strlen("_en"));
358 if (current->name == NULL) {
359 free(filename);
360 ret = -ENOMEM;
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200361 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100362 goto error_cleanup_array;
363 }
364 /* Get the generic and specific name elements */
365 ret = iioutils_break_up_name(current->name,
366 &current->generic_name);
367 if (ret) {
368 free(filename);
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200369 free(current->name);
370 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100371 goto error_cleanup_array;
372 }
373 ret = asprintf(&filename,
374 "%s/%s_index",
375 scan_el_dir,
376 current->name);
377 if (ret < 0) {
378 free(filename);
379 ret = -ENOMEM;
380 goto error_cleanup_array;
381 }
382 sysfsfp = fopen(filename, "r");
383 fscanf(sysfsfp, "%u", &current->index);
384 fclose(sysfsfp);
385 free(filename);
386 /* Find the scale */
387 ret = iioutils_get_param_float(&current->scale,
388 "scale",
389 device_dir,
390 current->name,
391 current->generic_name);
392 if (ret < 0)
393 goto error_cleanup_array;
394 ret = iioutils_get_param_float(&current->offset,
395 "offset",
396 device_dir,
397 current->name,
398 current->generic_name);
399 if (ret < 0)
400 goto error_cleanup_array;
401 ret = iioutils_get_type(&current->is_signed,
402 &current->bytes,
403 &current->bits_used,
Jonathan Cameron52615d42011-05-18 14:41:19 +0100404 &current->shift,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100405 &current->mask,
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000406 &current->be,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100407 device_dir,
408 current->name,
409 current->generic_name);
410 }
411 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100412
Jonathan Camerone58537c2010-10-08 12:14:14 +0100413 closedir(dp);
Hartmut Knaack66dd08f2015-05-31 14:39:43 +0200414 free(scan_el_dir);
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100415 /* reorder so that the array is in index order */
416 bsort_channel_array_by_index(ci_array, *counter);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100417
418 return 0;
419
420error_cleanup_array:
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200421 for (i = count - 1; i >= 0; i--) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100422 free((*ci_array)[i].name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200423 free((*ci_array)[i].generic_name);
424 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100425 free(*ci_array);
426error_close_dir:
427 closedir(dp);
428error_free_name:
429 free(scan_el_dir);
430error_ret:
431 return ret;
432}
433
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200434int calc_digits(int num)
435{
436 int count = 0;
437
438 while (num != 0) {
439 num /= 10;
440 count++;
441 }
442
443 return count;
444}
445
Jonathan Camerone58537c2010-10-08 12:14:14 +0100446/**
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100447 * find_type_by_name() - function to match top level types by name
448 * @name: top level type instance name
449 * @type: the type of top level instance being sort
450 *
451 * Typical types this is used for are device and trigger.
452 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200453int find_type_by_name(const char *name, const char *type)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100454{
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100455 const struct dirent *ent;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200456 int number, numstrlen, ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100457
458 FILE *nameFile;
459 DIR *dp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100460 char thisname[IIO_MAX_NAME_LENGTH];
461 char *filename;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100462
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100463 dp = opendir(iio_dir);
464 if (dp == NULL) {
Peter Meerwaldc866ffc2012-07-01 00:47:41 +0200465 printf("No industrialio devices available\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100466 return -ENODEV;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100467 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100468
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100469 while (ent = readdir(dp), ent != NULL) {
470 if (strcmp(ent->d_name, ".") != 0 &&
471 strcmp(ent->d_name, "..") != 0 &&
472 strlen(ent->d_name) > strlen(type) &&
473 strncmp(ent->d_name, type, strlen(type)) == 0) {
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200474 errno = 0;
475 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
476 if (ret < 0) {
477 ret = -errno;
478 printf("failed to read element number\n");
479 goto error_close_dir;
480 } else if (ret != 1) {
481 ret = -EIO;
482 printf("failed to match element number\n");
483 goto error_close_dir;
484 }
485
486 numstrlen = calc_digits(number);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100487 /* verify the next character is not a colon */
488 if (strncmp(ent->d_name + strlen(type) + numstrlen,
489 ":",
490 1) != 0) {
491 filename = malloc(strlen(iio_dir)
492 + strlen(type)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100493 + numstrlen
Barry Songb6ee30a2010-05-25 17:40:04 +0800494 + 6);
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200495 if (filename == NULL) {
496 closedir(dp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100497 return -ENOMEM;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200498 }
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100499 sprintf(filename, "%s%s%d/name",
500 iio_dir,
501 type,
502 number);
503 nameFile = fopen(filename, "r");
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200504 if (!nameFile) {
505 free(filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100506 continue;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200507 }
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100508 free(filename);
509 fscanf(nameFile, "%s", thisname);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100510 fclose(nameFile);
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200511 if (strcmp(name, thisname) == 0) {
512 closedir(dp);
513 return number;
514 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100515 }
516 }
517 }
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200518 closedir(dp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100519 return -ENODEV;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200520
521error_close_dir:
522 if (closedir(dp) == -1)
523 perror("find_type_by_name(): Failed to close directory");
524 return ret;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100525}
526
Hartmut Knaack2156b172015-05-31 14:40:01 +0200527static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100528{
Sebastian Andrzej Siewior11cb4542013-10-07 13:42:00 +0100529 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100530 FILE *sysfsfp;
531 int test;
532 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300533
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100534 if (temp == NULL)
535 return -ENOMEM;
536 sprintf(temp, "%s/%s", basedir, filename);
537 sysfsfp = fopen(temp, "w");
538 if (sysfsfp == NULL) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100539 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200540 printf("failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100541 goto error_free;
542 }
543 fprintf(sysfsfp, "%d", val);
544 fclose(sysfsfp);
545 if (verify) {
546 sysfsfp = fopen(temp, "r");
547 if (sysfsfp == NULL) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100548 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200549 printf("failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100550 goto error_free;
551 }
552 fscanf(sysfsfp, "%d", &test);
Peter Meerwaldce7b04c2012-06-25 23:13:26 +0200553 fclose(sysfsfp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100554 if (test != val) {
555 printf("Possible failure in int write %d to %s%s\n",
556 val,
557 basedir,
558 filename);
559 ret = -1;
560 }
561 }
562error_free:
563 free(temp);
564 return ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100565}
566
567int write_sysfs_int(char *filename, char *basedir, int val)
568{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100569 return _write_sysfs_int(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100570}
571
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100572int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
573{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100574 return _write_sysfs_int(filename, basedir, val, 1);
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100575}
576
Hartmut Knaack2156b172015-05-31 14:40:01 +0200577static int _write_sysfs_string(char *filename, char *basedir, char *val,
578 int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100579{
Jonathan Camerone58537c2010-10-08 12:14:14 +0100580 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100581 FILE *sysfsfp;
582 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300583
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100584 if (temp == NULL) {
585 printf("Memory allocation failed\n");
586 return -ENOMEM;
587 }
588 sprintf(temp, "%s/%s", basedir, filename);
589 sysfsfp = fopen(temp, "w");
590 if (sysfsfp == NULL) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100591 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200592 printf("Could not open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100593 goto error_free;
594 }
595 fprintf(sysfsfp, "%s", val);
596 fclose(sysfsfp);
597 if (verify) {
598 sysfsfp = fopen(temp, "r");
599 if (sysfsfp == NULL) {
600 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200601 printf("could not open file to verify\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100602 goto error_free;
603 }
604 fscanf(sysfsfp, "%s", temp);
Peter Meerwaldce7b04c2012-06-25 23:13:26 +0200605 fclose(sysfsfp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100606 if (strcmp(temp, val) != 0) {
607 printf("Possible failure in string write of %s "
608 "Should be %s "
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300609 "written to %s\%s\n",
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100610 temp,
611 val,
612 basedir,
613 filename);
614 ret = -1;
615 }
616 }
617error_free:
618 free(temp);
619
620 return ret;
621}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100622
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100623/**
624 * write_sysfs_string_and_verify() - string write, readback and verify
625 * @filename: name of file to write to
626 * @basedir: the sysfs directory in which the file is to be found
627 * @val: the string to write
628 **/
629int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
630{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100631 return _write_sysfs_string(filename, basedir, val, 1);
632}
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100633
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100634int write_sysfs_string(char *filename, char *basedir, char *val)
635{
636 return _write_sysfs_string(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100637}
638
639int read_sysfs_posint(char *filename, char *basedir)
640{
641 int ret;
642 FILE *sysfsfp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100643 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300644
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100645 if (temp == NULL) {
646 printf("Memory allocation failed");
647 return -ENOMEM;
648 }
649 sprintf(temp, "%s/%s", basedir, filename);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100650 sysfsfp = fopen(temp, "r");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100651 if (sysfsfp == NULL) {
652 ret = -errno;
653 goto error_free;
654 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100655 fscanf(sysfsfp, "%d\n", &ret);
656 fclose(sysfsfp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100657error_free:
658 free(temp);
659 return ret;
660}
661
662int read_sysfs_float(char *filename, char *basedir, float *val)
663{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000664 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100665 FILE *sysfsfp;
666 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300667
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100668 if (temp == NULL) {
669 printf("Memory allocation failed");
670 return -ENOMEM;
671 }
672 sprintf(temp, "%s/%s", basedir, filename);
673 sysfsfp = fopen(temp, "r");
674 if (sysfsfp == NULL) {
675 ret = -errno;
676 goto error_free;
677 }
678 fscanf(sysfsfp, "%f\n", val);
679 fclose(sysfsfp);
680error_free:
681 free(temp);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100682 return ret;
683}
Manuel Stahl49d916e2014-05-02 13:23:00 +0100684
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000685int read_sysfs_string(const char *filename, const char *basedir, char *str)
Manuel Stahl49d916e2014-05-02 13:23:00 +0100686{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000687 int ret = 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100688 FILE *sysfsfp;
689 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300690
Manuel Stahl49d916e2014-05-02 13:23:00 +0100691 if (temp == NULL) {
692 printf("Memory allocation failed");
693 return -ENOMEM;
694 }
695 sprintf(temp, "%s/%s", basedir, filename);
696 sysfsfp = fopen(temp, "r");
697 if (sysfsfp == NULL) {
698 ret = -errno;
699 goto error_free;
700 }
701 fscanf(sysfsfp, "%s\n", str);
702 fclose(sysfsfp);
703error_free:
704 free(temp);
705 return ret;
706}
Cristina Opriceana37e3be92015-03-04 02:37:15 +0200707
708#endif /* _IIO_UTILS_H */