static int write_each_attr(struct iio_device *dev, bool is_debug, ssize_t (*cb)(struct iio_device *dev, const char *attr, void *buf, size_t len, void *d), void *data) { char *buf, *ptr; unsigned int i, count; size_t len = 0x100000; int ret; /* We need a big buffer here; 1 MiB should be enough */ buf = malloc(len); if (!buf) return -ENOMEM; ptr = buf; if (is_debug) count = iio_device_get_debug_attrs_count(dev); else count = iio_device_get_attrs_count(dev); for (i = 0; i < count; i++) { const char *attr; if (is_debug) attr = iio_device_get_debug_attr(dev, i); else attr = iio_device_get_attr(dev, i); ret = (int) cb(dev, attr, ptr + 4, len - 4, data); if (ret < 0) goto err_free_buf; *(int32_t *) ptr = (int32_t) htonl((uint32_t) ret); ptr += 4; len -= 4; if (ret > 0) { if (ret & 0x3) ret = ((ret >> 2) + 1) << 2; ptr += ret; len -= ret; } } if (is_debug) ret = (int) iio_device_debug_attr_write_raw(dev, NULL, buf, ptr - buf); else ret = (int) iio_device_attr_write_raw(dev, NULL, buf, ptr - buf); err_free_buf: free(buf); return ret < 0 ? ret : 0; }
struct iio_device *iioc_dev_open(struct extra_ctx_info *ctx_info, const char *dev_name) { unsigned int j, k; //Ñ°ÕÒÉ豸 struct iio_device *dev = iio_context_find_device(ctx_info->ctx, dev_name); if (!dev) { IIOC_DBG("No such device(%s).\n", dev_name); return NULL; } IIOC_DBG("Open device - %s.\n", dev_name); //·ÖÅädev_info¿Õ¼ä struct extra_dev_info *dev_info = (struct extra_dev_info *)calloc(1, sizeof(*dev_info)); if (!dev_info) { IIOC_DBG("Can not calloc memory for struct extra_dev_info.\n"); return NULL; } unsigned int nb_channels = iio_device_get_channels_count(dev); unsigned int nb_attrs = iio_device_get_attrs_count(dev); dev_info->input_device = is_input_device(dev); dev_info->nb_channels = nb_channels; dev_info->nb_attrs = nb_attrs; dev_info->ctx_info = ctx_info; iio_device_set_data(dev, dev_info); for (j = 0; j < nb_channels; j++) { struct iio_channel *ch = iio_device_get_channel(dev, j); #ifdef _DEBUG const char *ch_id = iio_channel_get_id(ch); const char *ch_name = iio_channel_get_name(ch); IIOC_DBG("\tCH%d: %s - %s.\n", j, ch_id, ch_name); #endif struct extra_chn_info *chn_info = (struct extra_chn_info *)calloc(1, sizeof(*chn_info)); if (!chn_info) { goto error_calloc_chn_info; } chn_info->dev = dev; iio_channel_set_data(ch, chn_info); } #ifdef _DEBUG for (k = 0; k < nb_attrs; k++) { const char *attr_name = iio_device_get_attr(dev, k); IIOC_DBG("\t>> Attr%d: %s.\n", k, attr_name); } #endif return dev; error_calloc_chn_info: for (k = 0; k < nb_channels; k++) { struct iio_channel *ch = iio_device_get_channel(dev, k); struct extra_chn_info *chn_info = iio_channel_get_data(ch); if (chn_info) free(chn_info); } IIOC_DBG("Can not calloc memory for struct extra_chn_info.\n"); return NULL; }
static int read_each_attr(struct iio_device *dev, bool is_debug, int (*cb)(struct iio_device *dev, const char *attr, const char *val, size_t len, void *d), void *data) { int ret; char *buf, *ptr; unsigned int i, count; /* We need a big buffer here; 1 MiB should be enough */ buf = malloc(0x100000); if (!buf) return -ENOMEM; if (is_debug) { count = iio_device_get_debug_attrs_count(dev); ret = (int) iio_device_debug_attr_read(dev, NULL, buf, 0x100000); } else { count = iio_device_get_attrs_count(dev); ret = (int) iio_device_attr_read(dev, NULL, buf, 0x100000); } if (ret < 0) goto err_free_buf; ptr = buf; for (i = 0; i < count; i++) { const char *attr; int32_t len = (int32_t) ntohl(*(uint32_t *) ptr); if (is_debug) attr = iio_device_get_debug_attr(dev, i); else attr = iio_device_get_attr(dev, i); ptr += 4; if (len > 0) { ret = cb(dev, attr, ptr, (size_t) len, data); if (ret < 0) goto err_free_buf; if (len & 0x3) len = ((len >> 2) + 1) << 2; ptr += len; } }
int main(int argc, char **argv) { struct iio_context *ctx; int c, option_index = 0, arg_index = 0; enum backend backend = LOCAL; unsigned int major, minor; char git_tag[8]; int ret; while ((c = getopt_long(argc, argv, "+hn:x:", options, &option_index)) != -1) { switch (c) { case 'h': usage(); return EXIT_SUCCESS; case 'n': if (backend != LOCAL) { ERROR("-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = NETWORK; arg_index += 2; break; case 'x': if (backend != LOCAL) { ERROR("-x and -n are mutually exclusive\n"); return EXIT_FAILURE; } backend = XML; arg_index += 2; break; case '?': return EXIT_FAILURE; } } if (arg_index >= argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } iio_library_get_version(&major, &minor, git_tag); INFO("Library version: %u.%u (git tag: %s)\n", major, minor, git_tag); if (backend == XML) ctx = iio_create_xml_context(argv[arg_index]); else if (backend == NETWORK) ctx = iio_create_network_context(argv[arg_index]); else ctx = iio_create_local_context(); if (!ctx) { ERROR("Unable to create IIO context\n"); return EXIT_FAILURE; } INFO("IIO context created with %s backend.\n", iio_context_get_name(ctx)); ret = iio_context_get_version(ctx, &major, &minor, git_tag); if (!ret) INFO("Backend version: %u.%u (git tag: %s)\n", major, minor, git_tag); else ERROR("Unable to get backend version: %i\n", ret); unsigned int nb_devices = iio_context_get_devices_count(ctx); INFO("IIO context has %u devices:\n", nb_devices); unsigned int i; for (i = 0; i < nb_devices; i++) { const struct iio_device *dev = iio_context_get_device(ctx, i); const char *name = iio_device_get_name(dev); INFO("\t%s: %s\n", iio_device_get_id(dev), name ? name : "" ); unsigned int nb_channels = iio_device_get_channels_count(dev); INFO("\t\t%u channels found:\n", nb_channels); unsigned int j; for (j = 0; j < nb_channels; j++) { struct iio_channel *ch = iio_device_get_channel(dev, j); const char *type_name; if (iio_channel_is_output(ch)) type_name = "output"; else type_name = "input"; name = iio_channel_get_name(ch); INFO("\t\t\t%s: %s (%s)\n", iio_channel_get_id(ch), name ? name : "", type_name); unsigned int nb_attrs = iio_channel_get_attrs_count(ch); if (!nb_attrs) continue; INFO("\t\t\t%u channel-specific attributes found:\n", nb_attrs); unsigned int k; for (k = 0; k < nb_attrs; k++) { const char *attr = iio_channel_get_attr(ch, k); char buf[1024]; ret = (int) iio_channel_attr_read(ch, attr, buf, 1024); if (ret > 0) INFO("\t\t\t\tattr %u: %s" " value: %s\n", k, attr, buf); else if (ret == -ENOSYS) INFO("\t\t\t\tattr %u: %s\n", k, attr); else ERROR("Unable to read attribute %s\n", attr); } } unsigned int nb_attrs = iio_device_get_attrs_count(dev); if (!nb_attrs) continue; INFO("\t\t%u device-specific attributes found:\n", nb_attrs); for (j = 0; j < nb_attrs; j++) { const char *attr = iio_device_get_attr(dev, j); char buf[1024]; ret = (int) iio_device_attr_read(dev, attr, buf, 1024); if (ret > 0) INFO("\t\t\t\tattr %u: %s value: %s\n", j, attr, buf); else if (ret == -ENOSYS) INFO("\t\t\t\tattr %u: %s\n", j, attr); else ERROR("Unable to read attribute: %s\n", attr); } } iio_context_destroy(ctx); return EXIT_SUCCESS; }