iio_utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /* 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. */
  9. #ifndef _IIO_UTILS_H
  10. #define _IIO_UTILS_H
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include "iio_utils.h"
  19. const char *iio_dir = "/sys/bus/iio/devices/";
  20. static char * const iio_direction[] = {
  21. "in",
  22. "out",
  23. };
  24. /**
  25. * iioutils_break_up_name() - extract generic name from full channel name
  26. * @full_name: the full channel name
  27. * @generic_name: the output generic channel name
  28. **/
  29. int iioutils_break_up_name(const char *full_name,
  30. char **generic_name)
  31. {
  32. char *current;
  33. char *w, *r;
  34. char *working, *prefix = "";
  35. int i;
  36. for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
  37. if (!strncmp(full_name, iio_direction[i],
  38. strlen(iio_direction[i]))) {
  39. prefix = iio_direction[i];
  40. break;
  41. }
  42. current = strdup(full_name + strlen(prefix) + 1);
  43. working = strtok(current, "_\0");
  44. w = working;
  45. r = working;
  46. while (*r != '\0') {
  47. if (!isdigit(*r)) {
  48. *w = *r;
  49. w++;
  50. }
  51. r++;
  52. }
  53. *w = '\0';
  54. asprintf(generic_name, "%s_%s", prefix, working);
  55. free(current);
  56. return 0;
  57. }
  58. /**
  59. * iioutils_get_type() - find and process _type attribute data
  60. * @is_signed: output whether channel is signed
  61. * @bytes: output how many bytes the channel storage occupies
  62. * @mask: output a bit mask for the raw data
  63. * @be: big endian
  64. * @device_dir: the iio device directory
  65. * @name: the channel name
  66. * @generic_name: the channel type name
  67. **/
  68. int iioutils_get_type(unsigned *is_signed,
  69. unsigned *bytes,
  70. unsigned *bits_used,
  71. unsigned *shift,
  72. uint64_t *mask,
  73. unsigned *be,
  74. const char *device_dir,
  75. const char *name,
  76. const char *generic_name)
  77. {
  78. FILE *sysfsfp;
  79. int ret;
  80. DIR *dp;
  81. char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
  82. char signchar, endianchar;
  83. unsigned padint;
  84. const struct dirent *ent;
  85. ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
  86. if (ret < 0) {
  87. ret = -ENOMEM;
  88. goto error_ret;
  89. }
  90. ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
  91. if (ret < 0) {
  92. ret = -ENOMEM;
  93. goto error_free_scan_el_dir;
  94. }
  95. ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
  96. if (ret < 0) {
  97. ret = -ENOMEM;
  98. goto error_free_builtname;
  99. }
  100. dp = opendir(scan_el_dir);
  101. if (dp == NULL) {
  102. ret = -errno;
  103. goto error_free_builtname_generic;
  104. }
  105. while (ent = readdir(dp), ent != NULL)
  106. /*
  107. * Do we allow devices to override a generic name with
  108. * a specific one?
  109. */
  110. if ((strcmp(builtname, ent->d_name) == 0) ||
  111. (strcmp(builtname_generic, ent->d_name) == 0)) {
  112. ret = asprintf(&filename,
  113. "%s/%s", scan_el_dir, ent->d_name);
  114. if (ret < 0) {
  115. ret = -ENOMEM;
  116. goto error_closedir;
  117. }
  118. sysfsfp = fopen(filename, "r");
  119. if (sysfsfp == NULL) {
  120. ret = -errno;
  121. printf("failed to open %s\n", filename);
  122. goto error_free_filename;
  123. }
  124. ret = fscanf(sysfsfp,
  125. "%ce:%c%u/%u>>%u",
  126. &endianchar,
  127. &signchar,
  128. bits_used,
  129. &padint, shift);
  130. if (ret < 0) {
  131. ret = -errno;
  132. printf("failed to pass scan type description\n");
  133. goto error_close_sysfsfp;
  134. } else if (ret != 5) {
  135. ret = -EIO;
  136. printf("scan type description didn't match\n");
  137. goto error_close_sysfsfp;
  138. }
  139. *be = (endianchar == 'b');
  140. *bytes = padint / 8;
  141. if (*bits_used == 64)
  142. *mask = ~0;
  143. else
  144. *mask = (1 << *bits_used) - 1;
  145. if (signchar == 's')
  146. *is_signed = 1;
  147. else
  148. *is_signed = 0;
  149. fclose(sysfsfp);
  150. free(filename);
  151. filename = 0;
  152. sysfsfp = 0;
  153. }
  154. error_close_sysfsfp:
  155. if (sysfsfp)
  156. fclose(sysfsfp);
  157. error_free_filename:
  158. if (filename)
  159. free(filename);
  160. error_closedir:
  161. closedir(dp);
  162. error_free_builtname_generic:
  163. free(builtname_generic);
  164. error_free_builtname:
  165. free(builtname);
  166. error_free_scan_el_dir:
  167. free(scan_el_dir);
  168. error_ret:
  169. return ret;
  170. }
  171. int iioutils_get_param_float(float *output,
  172. const char *param_name,
  173. const char *device_dir,
  174. const char *name,
  175. const char *generic_name)
  176. {
  177. FILE *sysfsfp;
  178. int ret;
  179. DIR *dp;
  180. char *builtname, *builtname_generic;
  181. char *filename = NULL;
  182. const struct dirent *ent;
  183. ret = asprintf(&builtname, "%s_%s", name, param_name);
  184. if (ret < 0) {
  185. ret = -ENOMEM;
  186. goto error_ret;
  187. }
  188. ret = asprintf(&builtname_generic,
  189. "%s_%s", generic_name, param_name);
  190. if (ret < 0) {
  191. ret = -ENOMEM;
  192. goto error_free_builtname;
  193. }
  194. dp = opendir(device_dir);
  195. if (dp == NULL) {
  196. ret = -errno;
  197. goto error_free_builtname_generic;
  198. }
  199. while (ent = readdir(dp), ent != NULL)
  200. if ((strcmp(builtname, ent->d_name) == 0) ||
  201. (strcmp(builtname_generic, ent->d_name) == 0)) {
  202. ret = asprintf(&filename,
  203. "%s/%s", device_dir, ent->d_name);
  204. if (ret < 0) {
  205. ret = -ENOMEM;
  206. goto error_closedir;
  207. }
  208. sysfsfp = fopen(filename, "r");
  209. if (!sysfsfp) {
  210. ret = -errno;
  211. goto error_free_filename;
  212. }
  213. fscanf(sysfsfp, "%f", output);
  214. break;
  215. }
  216. error_free_filename:
  217. if (filename)
  218. free(filename);
  219. error_closedir:
  220. closedir(dp);
  221. error_free_builtname_generic:
  222. free(builtname_generic);
  223. error_free_builtname:
  224. free(builtname);
  225. error_ret:
  226. return ret;
  227. }
  228. /**
  229. * bsort_channel_array_by_index() - reorder so that the array is in index order
  230. *
  231. **/
  232. void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
  233. int cnt)
  234. {
  235. struct iio_channel_info temp;
  236. int x, y;
  237. for (x = 0; x < cnt; x++)
  238. for (y = 0; y < (cnt - 1); y++)
  239. if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
  240. temp = (*ci_array)[y + 1];
  241. (*ci_array)[y + 1] = (*ci_array)[y];
  242. (*ci_array)[y] = temp;
  243. }
  244. }
  245. /**
  246. * build_channel_array() - function to figure out what channels are present
  247. * @device_dir: the IIO device directory in sysfs
  248. * @
  249. **/
  250. int build_channel_array(const char *device_dir,
  251. struct iio_channel_info **ci_array,
  252. int *counter)
  253. {
  254. DIR *dp;
  255. FILE *sysfsfp;
  256. int count, i;
  257. struct iio_channel_info *current;
  258. int ret;
  259. const struct dirent *ent;
  260. char *scan_el_dir;
  261. char *filename;
  262. *counter = 0;
  263. ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
  264. if (ret < 0) {
  265. ret = -ENOMEM;
  266. goto error_ret;
  267. }
  268. dp = opendir(scan_el_dir);
  269. if (dp == NULL) {
  270. ret = -errno;
  271. goto error_free_name;
  272. }
  273. while (ent = readdir(dp), ent != NULL)
  274. if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
  275. "_en") == 0) {
  276. ret = asprintf(&filename,
  277. "%s/%s", scan_el_dir, ent->d_name);
  278. if (ret < 0) {
  279. ret = -ENOMEM;
  280. goto error_close_dir;
  281. }
  282. sysfsfp = fopen(filename, "r");
  283. if (sysfsfp == NULL) {
  284. ret = -errno;
  285. free(filename);
  286. goto error_close_dir;
  287. }
  288. fscanf(sysfsfp, "%i", &ret);
  289. if (ret == 1)
  290. (*counter)++;
  291. fclose(sysfsfp);
  292. free(filename);
  293. }
  294. *ci_array = malloc(sizeof(**ci_array) * (*counter));
  295. if (*ci_array == NULL) {
  296. ret = -ENOMEM;
  297. goto error_close_dir;
  298. }
  299. seekdir(dp, 0);
  300. count = 0;
  301. while (ent = readdir(dp), ent != NULL) {
  302. if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
  303. "_en") == 0) {
  304. int current_enabled = 0;
  305. current = &(*ci_array)[count++];
  306. ret = asprintf(&filename,
  307. "%s/%s", scan_el_dir, ent->d_name);
  308. if (ret < 0) {
  309. ret = -ENOMEM;
  310. /* decrement count to avoid freeing name */
  311. count--;
  312. goto error_cleanup_array;
  313. }
  314. sysfsfp = fopen(filename, "r");
  315. if (sysfsfp == NULL) {
  316. ret = -errno;
  317. free(filename);
  318. count--;
  319. goto error_cleanup_array;
  320. }
  321. fscanf(sysfsfp, "%i", &current_enabled);
  322. fclose(sysfsfp);
  323. if (!current_enabled) {
  324. free(filename);
  325. count--;
  326. continue;
  327. }
  328. current->scale = 1.0;
  329. current->offset = 0;
  330. current->name = strndup(ent->d_name,
  331. strlen(ent->d_name) -
  332. strlen("_en"));
  333. if (current->name == NULL) {
  334. free(filename);
  335. ret = -ENOMEM;
  336. count--;
  337. goto error_cleanup_array;
  338. }
  339. /* Get the generic and specific name elements */
  340. ret = iioutils_break_up_name(current->name,
  341. &current->generic_name);
  342. if (ret) {
  343. free(filename);
  344. free(current->name);
  345. count--;
  346. goto error_cleanup_array;
  347. }
  348. ret = asprintf(&filename,
  349. "%s/%s_index",
  350. scan_el_dir,
  351. current->name);
  352. if (ret < 0) {
  353. free(filename);
  354. ret = -ENOMEM;
  355. goto error_cleanup_array;
  356. }
  357. sysfsfp = fopen(filename, "r");
  358. fscanf(sysfsfp, "%u", &current->index);
  359. fclose(sysfsfp);
  360. free(filename);
  361. /* Find the scale */
  362. ret = iioutils_get_param_float(&current->scale,
  363. "scale",
  364. device_dir,
  365. current->name,
  366. current->generic_name);
  367. if (ret < 0)
  368. goto error_cleanup_array;
  369. ret = iioutils_get_param_float(&current->offset,
  370. "offset",
  371. device_dir,
  372. current->name,
  373. current->generic_name);
  374. if (ret < 0)
  375. goto error_cleanup_array;
  376. ret = iioutils_get_type(&current->is_signed,
  377. &current->bytes,
  378. &current->bits_used,
  379. &current->shift,
  380. &current->mask,
  381. &current->be,
  382. device_dir,
  383. current->name,
  384. current->generic_name);
  385. }
  386. }
  387. closedir(dp);
  388. free(scan_el_dir);
  389. /* reorder so that the array is in index order */
  390. bsort_channel_array_by_index(ci_array, *counter);
  391. return 0;
  392. error_cleanup_array:
  393. for (i = count - 1; i >= 0; i--) {
  394. free((*ci_array)[i].name);
  395. free((*ci_array)[i].generic_name);
  396. }
  397. free(*ci_array);
  398. error_close_dir:
  399. closedir(dp);
  400. error_free_name:
  401. free(scan_el_dir);
  402. error_ret:
  403. return ret;
  404. }
  405. int calc_digits(int num)
  406. {
  407. int count = 0;
  408. while (num != 0) {
  409. num /= 10;
  410. count++;
  411. }
  412. return count;
  413. }
  414. /**
  415. * find_type_by_name() - function to match top level types by name
  416. * @name: top level type instance name
  417. * @type: the type of top level instance being sort
  418. *
  419. * Typical types this is used for are device and trigger.
  420. **/
  421. int find_type_by_name(const char *name, const char *type)
  422. {
  423. const struct dirent *ent;
  424. int number, numstrlen, ret;
  425. FILE *nameFile;
  426. DIR *dp;
  427. char thisname[IIO_MAX_NAME_LENGTH];
  428. char *filename;
  429. dp = opendir(iio_dir);
  430. if (dp == NULL) {
  431. printf("No industrialio devices available\n");
  432. return -ENODEV;
  433. }
  434. while (ent = readdir(dp), ent != NULL) {
  435. if (strcmp(ent->d_name, ".") != 0 &&
  436. strcmp(ent->d_name, "..") != 0 &&
  437. strlen(ent->d_name) > strlen(type) &&
  438. strncmp(ent->d_name, type, strlen(type)) == 0) {
  439. errno = 0;
  440. ret = sscanf(ent->d_name + strlen(type), "%d", &number);
  441. if (ret < 0) {
  442. ret = -errno;
  443. printf("failed to read element number\n");
  444. goto error_close_dir;
  445. } else if (ret != 1) {
  446. ret = -EIO;
  447. printf("failed to match element number\n");
  448. goto error_close_dir;
  449. }
  450. numstrlen = calc_digits(number);
  451. /* verify the next character is not a colon */
  452. if (strncmp(ent->d_name + strlen(type) + numstrlen,
  453. ":",
  454. 1) != 0) {
  455. filename = malloc(strlen(iio_dir)
  456. + strlen(type)
  457. + numstrlen
  458. + 6);
  459. if (filename == NULL) {
  460. closedir(dp);
  461. return -ENOMEM;
  462. }
  463. sprintf(filename, "%s%s%d/name",
  464. iio_dir,
  465. type,
  466. number);
  467. nameFile = fopen(filename, "r");
  468. if (!nameFile) {
  469. free(filename);
  470. continue;
  471. }
  472. free(filename);
  473. fscanf(nameFile, "%s", thisname);
  474. fclose(nameFile);
  475. if (strcmp(name, thisname) == 0) {
  476. closedir(dp);
  477. return number;
  478. }
  479. }
  480. }
  481. }
  482. closedir(dp);
  483. return -ENODEV;
  484. error_close_dir:
  485. if (closedir(dp) == -1)
  486. perror("find_type_by_name(): Failed to close directory");
  487. return ret;
  488. }
  489. static int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
  490. {
  491. int ret = 0;
  492. FILE *sysfsfp;
  493. int test;
  494. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  495. if (temp == NULL)
  496. return -ENOMEM;
  497. sprintf(temp, "%s/%s", basedir, filename);
  498. sysfsfp = fopen(temp, "w");
  499. if (sysfsfp == NULL) {
  500. ret = -errno;
  501. printf("failed to open %s\n", temp);
  502. goto error_free;
  503. }
  504. fprintf(sysfsfp, "%d", val);
  505. fclose(sysfsfp);
  506. if (verify) {
  507. sysfsfp = fopen(temp, "r");
  508. if (sysfsfp == NULL) {
  509. ret = -errno;
  510. printf("failed to open %s\n", temp);
  511. goto error_free;
  512. }
  513. fscanf(sysfsfp, "%d", &test);
  514. fclose(sysfsfp);
  515. if (test != val) {
  516. printf("Possible failure in int write %d to %s%s\n",
  517. val,
  518. basedir,
  519. filename);
  520. ret = -1;
  521. }
  522. }
  523. error_free:
  524. free(temp);
  525. return ret;
  526. }
  527. int write_sysfs_int(char *filename, char *basedir, int val)
  528. {
  529. return _write_sysfs_int(filename, basedir, val, 0);
  530. }
  531. int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
  532. {
  533. return _write_sysfs_int(filename, basedir, val, 1);
  534. }
  535. static int _write_sysfs_string(char *filename, char *basedir, char *val,
  536. int verify)
  537. {
  538. int ret = 0;
  539. FILE *sysfsfp;
  540. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  541. if (temp == NULL) {
  542. printf("Memory allocation failed\n");
  543. return -ENOMEM;
  544. }
  545. sprintf(temp, "%s/%s", basedir, filename);
  546. sysfsfp = fopen(temp, "w");
  547. if (sysfsfp == NULL) {
  548. ret = -errno;
  549. printf("Could not open %s\n", temp);
  550. goto error_free;
  551. }
  552. fprintf(sysfsfp, "%s", val);
  553. fclose(sysfsfp);
  554. if (verify) {
  555. sysfsfp = fopen(temp, "r");
  556. if (sysfsfp == NULL) {
  557. ret = -errno;
  558. printf("could not open file to verify\n");
  559. goto error_free;
  560. }
  561. fscanf(sysfsfp, "%s", temp);
  562. fclose(sysfsfp);
  563. if (strcmp(temp, val) != 0) {
  564. printf("Possible failure in string write of %s "
  565. "Should be %s "
  566. "written to %s\%s\n",
  567. temp,
  568. val,
  569. basedir,
  570. filename);
  571. ret = -1;
  572. }
  573. }
  574. error_free:
  575. free(temp);
  576. return ret;
  577. }
  578. /**
  579. * write_sysfs_string_and_verify() - string write, readback and verify
  580. * @filename: name of file to write to
  581. * @basedir: the sysfs directory in which the file is to be found
  582. * @val: the string to write
  583. **/
  584. int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
  585. {
  586. return _write_sysfs_string(filename, basedir, val, 1);
  587. }
  588. int write_sysfs_string(char *filename, char *basedir, char *val)
  589. {
  590. return _write_sysfs_string(filename, basedir, val, 0);
  591. }
  592. int read_sysfs_posint(char *filename, char *basedir)
  593. {
  594. int ret;
  595. FILE *sysfsfp;
  596. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  597. if (temp == NULL) {
  598. printf("Memory allocation failed");
  599. return -ENOMEM;
  600. }
  601. sprintf(temp, "%s/%s", basedir, filename);
  602. sysfsfp = fopen(temp, "r");
  603. if (sysfsfp == NULL) {
  604. ret = -errno;
  605. goto error_free;
  606. }
  607. fscanf(sysfsfp, "%d\n", &ret);
  608. fclose(sysfsfp);
  609. error_free:
  610. free(temp);
  611. return ret;
  612. }
  613. int read_sysfs_float(char *filename, char *basedir, float *val)
  614. {
  615. int ret = 0;
  616. FILE *sysfsfp;
  617. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  618. if (temp == NULL) {
  619. printf("Memory allocation failed");
  620. return -ENOMEM;
  621. }
  622. sprintf(temp, "%s/%s", basedir, filename);
  623. sysfsfp = fopen(temp, "r");
  624. if (sysfsfp == NULL) {
  625. ret = -errno;
  626. goto error_free;
  627. }
  628. fscanf(sysfsfp, "%f\n", val);
  629. fclose(sysfsfp);
  630. error_free:
  631. free(temp);
  632. return ret;
  633. }
  634. int read_sysfs_string(const char *filename, const char *basedir, char *str)
  635. {
  636. int ret = 0;
  637. FILE *sysfsfp;
  638. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  639. if (temp == NULL) {
  640. printf("Memory allocation failed");
  641. return -ENOMEM;
  642. }
  643. sprintf(temp, "%s/%s", basedir, filename);
  644. sysfsfp = fopen(temp, "r");
  645. if (sysfsfp == NULL) {
  646. ret = -errno;
  647. goto error_free;
  648. }
  649. fscanf(sysfsfp, "%s\n", str);
  650. fclose(sysfsfp);
  651. error_free:
  652. free(temp);
  653. return ret;
  654. }
  655. #endif /* _IIO_UTILS_H */