/* Check for a valid two channels combination (ch0->ch1, ch2->ch3, ...) * * struct iio_channel_info *chanels - list of channels of a device * int ch_count - number of channel in the list * char* ch_name - output parameter: stores references to the enabled * channels. * Return 1 if the channel combination is valid * Return 0 if the combination is not valid */ int channel_combination_check(struct iio_device *dev, const char **ch_names) { bool consecutive_ch = FALSE; unsigned int i, k, nb_channels = iio_device_get_channels_count(dev); for (i = 0, k = 0; i < nb_channels; i++) { struct iio_channel *ch = iio_device_get_channel(dev, i); struct extra_info *info = iio_channel_get_data(ch); if (info->may_be_enabled) { const char *name = iio_channel_get_name(ch) ?: iio_channel_get_id(ch); ch_names[k++] = name; if (i > 0) { struct extra_info *prev = iio_channel_get_data(iio_device_get_channel(dev, i - 1)); if (prev->may_be_enabled) { consecutive_ch = TRUE; break; } } } } if (!consecutive_ch) return 0; if (!(i & 0x1)) return 0; return 1; }
ssize_t iiod_client_read_attr(struct iiod_client *client, int desc, const struct iio_device *dev, const struct iio_channel *chn, const char *attr, char *dest, size_t len, bool is_debug) { const char *id = iio_device_get_id(dev); char buf[1024]; ssize_t ret; if (attr) { if (chn) { if (!iio_channel_find_attr(chn, attr)) return -ENOENT; } else if (is_debug) { if (!iio_device_find_debug_attr(dev, attr)) return -ENOENT; } else { if (!iio_device_find_attr(dev, attr)) return -ENOENT; } } if (chn) snprintf(buf, sizeof(buf), "READ %s %s %s %s\r\n", id, iio_channel_is_output(chn) ? "OUTPUT" : "INPUT", iio_channel_get_id(chn), attr ? attr : ""); else if (is_debug) snprintf(buf, sizeof(buf), "READ %s DEBUG %s\r\n", id, attr ? attr : ""); else snprintf(buf, sizeof(buf), "READ %s %s\r\n", id, attr ? attr : ""); iio_mutex_lock(client->lock); ret = (ssize_t) iiod_client_exec_command(client, desc, buf); if (ret < 0) goto out_unlock; if ((size_t) ret + 1 > len) { ret = -EIO; goto out_unlock; } /* +1: Also read the trailing \n */ ret = iiod_client_read_all(client, desc, dest, ret + 1); if (ret > 0) { /* Discard the trailing \n */ ret--; /* Replace it with a \0 just in case */ dest[ret] = '\0'; } out_unlock: iio_mutex_unlock(client->lock); return ret; }
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; }
ssize_t iiod_client_write_attr(struct iiod_client *client, int desc, const struct iio_device *dev, const struct iio_channel *chn, const char *attr, const char *src, size_t len, bool is_debug) { struct iio_context_pdata *pdata = client->pdata; const struct iiod_client_ops *ops = client->ops; const char *id = iio_device_get_id(dev); char buf[1024]; ssize_t ret; int resp; if (attr) { if (chn) { if (!iio_channel_find_attr(chn, attr)) return -ENOENT; } else if (is_debug) { if (!iio_device_find_debug_attr(dev, attr)) return -ENOENT; } else { if (!iio_device_find_attr(dev, attr)) return -ENOENT; } } if (chn) snprintf(buf, sizeof(buf), "WRITE %s %s %s %s %lu\r\n", id, iio_channel_is_output(chn) ? "OUTPUT" : "INPUT", iio_channel_get_id(chn), attr ? attr : "", (unsigned long) len); else if (is_debug) snprintf(buf, sizeof(buf), "WRITE %s DEBUG %s %lu\r\n", id, attr ? attr : "", (unsigned long) len); else snprintf(buf, sizeof(buf), "WRITE %s %s %lu\r\n", id, attr ? attr : "", (unsigned long) len); iio_mutex_lock(client->lock); ret = ops->write(pdata, desc, buf, strlen(buf)); if (ret < 0) goto out_unlock; ret = iiod_client_write_all(client, desc, src, len); if (ret < 0) goto out_unlock; ret = iiod_client_read_integer(client, desc, &resp); if (ret < 0) goto out_unlock; ret = (ssize_t) resp; out_unlock: iio_mutex_unlock(client->lock); return ret; }
static struct iio_channel * get_channel(const struct iio_device *dev, const char *id) { unsigned int i, nb = iio_device_get_channels_count(dev); for (i = 0; i < nb; i++) { struct iio_channel *chn = iio_device_get_channel(dev, i); const char *name = iio_channel_get_name(chn); if (iio_channel_is_output(chn)) continue; if (!strcmp(id, iio_channel_get_id(chn)) || (name && !strcmp(name, id))) return chn; } return NULL; }
static bool is_valid_dmm_channel(struct iio_channel *chn) { const char *id; if (iio_channel_is_output(chn)) return false; if (!iio_channel_find_attr(chn, "raw")) return false; /* find the name */ id = iio_channel_get_id(chn); /* Must have 'scale', or be a temperature, which doesn't need scale */ if (!strstr(id, "temp") && !iio_channel_find_attr(chn, "scale")) return false; return true; }
static void build_channel_list(void) { GtkTreeIter iter, iter2, iter3; unsigned int enabled; char *device, *device2; gboolean first = FALSE, iter3_valid = FALSE, loop, loop2, all = FALSE; char dev_ch[256]; loop = gtk_tree_model_get_iter_first(GTK_TREE_MODEL (device_list_store), &iter); gtk_list_store_clear(channel_list_store); while (loop) { gtk_tree_model_get(GTK_TREE_MODEL (device_list_store), &iter, 0, &device, 1, &enabled, -1); if (enabled) { struct iio_device *dev; unsigned int i, nb_channels; all = true; /* is it already in the list? */ loop2 = gtk_tree_model_get_iter_first(GTK_TREE_MODEL (channel_list_store), &iter2); if (loop2) { first = TRUE; iter3 = iter2; iter3_valid = TRUE; } while (loop2) { gtk_tree_model_get(GTK_TREE_MODEL (channel_list_store), &iter2, 2, &device2, -1); if (!strcmp(device, device2)) break; if (strcmp(device, device2) >= 0) { first = FALSE; iter3 = iter2; } g_free(device2); loop2 = gtk_tree_model_iter_next(GTK_TREE_MODEL (channel_list_store), &iter2); } /* it is, so skip the rest */ if (loop2) { loop = gtk_tree_model_iter_next(GTK_TREE_MODEL (device_list_store), &iter); continue; } dev = get_device(device); if (!dev) continue; nb_channels = iio_device_get_channels_count(dev); for (i = 0; i < nb_channels; i++) { struct iio_channel *chn = iio_device_get_channel(dev, i); const char *name, *id, *devid; /* Must be input */ if (!is_valid_dmm_channel(chn)) continue; /* find the name */ devid = iio_device_get_id(dev); name = iio_channel_get_name(chn); id = iio_channel_get_id(chn); if (!name) name = id; if (iter3_valid) { if (first) { gtk_list_store_insert_before(channel_list_store, &iter2, &iter3); first = FALSE; } else if(gtk_tree_model_iter_next(GTK_TREE_MODEL (channel_list_store), &iter3)) gtk_list_store_insert_before(channel_list_store, &iter2, &iter3); else gtk_list_store_append(channel_list_store, &iter2); } else { gtk_list_store_append(channel_list_store, &iter2); iter3_valid = TRUE; } snprintf(dev_ch, sizeof(dev_ch), "%s:%s", device, name); gtk_list_store_set(channel_list_store, &iter2, 0, dev_ch, /* device & channel name */ 1, 0, /* On/Off */ 2, devid, /* device ID */ 3, id, /* channel ID */ -1); iter3 = iter2; } } else { loop2 = gtk_tree_model_get_iter_first(GTK_TREE_MODEL (channel_list_store), &iter2); while (loop2) { gtk_tree_model_get(GTK_TREE_MODEL (channel_list_store), &iter2, 2, &device2, -1); if (!strcmp(device, device2)) { loop2 = gtk_list_store_remove(channel_list_store, &iter2); continue; } loop2 = gtk_tree_model_iter_next(GTK_TREE_MODEL (channel_list_store), &iter2); } } loop = gtk_tree_model_iter_next(GTK_TREE_MODEL (device_list_store), &iter); } gtk_tree_sortable_set_sort_column_id( GTK_TREE_SORTABLE(GTK_TREE_MODEL(channel_list_store)), 0, GTK_SORT_ASCENDING); if (all) gtk_widget_show(select_all_channels); else gtk_widget_hide(select_all_channels); }
int main(int argc, char **argv) { unsigned int i, nb_channels; unsigned int buffer_size = SAMPLES_PER_READ; int c, option_index = 0, arg_index = 0, ip_index = 0; struct iio_device *dev; size_t sample_size; while ((c = getopt_long(argc, argv, "+hn:t:b:s:", options, &option_index)) != -1) { switch (c) { case 'h': usage(); return EXIT_SUCCESS; case 'n': arg_index += 2; ip_index = arg_index; break; case 't': arg_index += 2; trigger_name = argv[arg_index]; break; case 'b': arg_index += 2; buffer_size = atoi(argv[arg_index]); break; case 's': arg_index += 2; num_samples = atoi(argv[arg_index]); break; case '?': return EXIT_FAILURE; } } if (arg_index + 1 >= argc) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } if (ip_index) ctx = iio_create_network_context(argv[ip_index]); else ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return EXIT_FAILURE; } #ifndef _WIN32 set_handler(SIGHUP, &quit_all); #endif set_handler(SIGINT, &quit_all); set_handler(SIGSEGV, &quit_all); set_handler(SIGTERM, &quit_all); dev = get_device(ctx, argv[arg_index + 1]); if (!dev) { iio_context_destroy(ctx); return EXIT_FAILURE; } if (trigger_name) { struct iio_device *trigger = get_device(ctx, trigger_name); if (!trigger) { iio_context_destroy(ctx); return EXIT_FAILURE; } if (!iio_device_is_trigger(trigger)) { fprintf(stderr, "Specified device is not a trigger\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } /* Fixed rate for now */ iio_device_attr_write_longlong(trigger, "frequency", 100); iio_device_set_trigger(dev, trigger); } nb_channels = iio_device_get_channels_count(dev); if (argc == arg_index + 2) { /* Enable all channels */ for (i = 0; i < nb_channels; i++) iio_channel_enable(iio_device_get_channel(dev, i)); } else { for (i = 0; i < nb_channels; i++) { unsigned int j; struct iio_channel *ch = iio_device_get_channel(dev, i); for (j = arg_index + 2; j < argc; j++) { const char *n = iio_channel_get_name(ch); if (!strcmp(argv[j], iio_channel_get_id(ch)) || (n && !strcmp(n, argv[j]))) iio_channel_enable(ch); } } } sample_size = iio_device_get_sample_size(dev); buffer = iio_device_create_buffer(dev, buffer_size, false); if (!buffer) { fprintf(stderr, "Unable to allocate buffer\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } while (app_running) { int ret = iio_buffer_refill(buffer); if (ret < 0) { fprintf(stderr, "Unable to refill buffer: %s\n", strerror(-ret)); break; } /* If there are only the samples we requested, we don't need to * demux */ if (iio_buffer_step(buffer) == sample_size) { void *start = iio_buffer_start(buffer); ptrdiff_t len = (intptr_t) iio_buffer_end(buffer) - (intptr_t) start; size_t read_len; if (num_samples && len > num_samples * sample_size) len = num_samples * sample_size; for (read_len = len; len; ) { ssize_t nb = fwrite(start, 1, len, stdout); if (nb < 0) { fprintf(stderr, "Unable to write data!\n"); goto err_destroy_buffer; } len -= nb; start = (void *)((intptr_t) start + nb); } if (num_samples) { num_samples -= read_len / sample_size; if (!num_samples) quit_all(EXIT_SUCCESS); } } else { iio_buffer_foreach_sample(buffer, print_sample, NULL); } } err_destroy_buffer: iio_buffer_destroy(buffer); iio_context_destroy(ctx); return exit_code; }
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; }
int main(int argc, char **argv) { unsigned int i, nb_channels; unsigned int buffer_size = SAMPLES_PER_READ; const char *arg_uri = NULL; const char *arg_ip = NULL; int c, option_index = 0; struct iio_device *dev; size_t sample_size; int timeout = -1; bool scan_for_context = false; while ((c = getopt_long(argc, argv, "+hn:u:t:b:s:T:a", options, &option_index)) != -1) { switch (c) { case 'h': usage(); return EXIT_SUCCESS; case 'n': arg_ip = optarg; break; case 'u': arg_uri = optarg; break; case 'a': scan_for_context = true; break; case 't': trigger_name = optarg; break; case 'b': buffer_size = atoi(optarg); break; case 's': num_samples = atoi(optarg); break; case 'T': timeout = atoi(optarg); break; case '?': return EXIT_FAILURE; } } if (argc == optind) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } setup_sig_handler(); if (scan_for_context) ctx = scan(); else if (arg_uri) ctx = iio_create_context_from_uri(arg_uri); else if (arg_ip) ctx = iio_create_network_context(arg_ip); else ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return EXIT_FAILURE; } if (timeout >= 0) iio_context_set_timeout(ctx, timeout); dev = iio_context_find_device(ctx, argv[optind]); if (!dev) { fprintf(stderr, "Device %s not found\n", argv[optind]); iio_context_destroy(ctx); return EXIT_FAILURE; } if (trigger_name) { struct iio_device *trigger = iio_context_find_device( ctx, trigger_name); if (!trigger) { fprintf(stderr, "Trigger %s not found\n", trigger_name); iio_context_destroy(ctx); return EXIT_FAILURE; } if (!iio_device_is_trigger(trigger)) { fprintf(stderr, "Specified device is not a trigger\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } /* * Fixed rate for now. Try new ABI first, * fail gracefully to remain compatible. */ if (iio_device_attr_write_longlong(trigger, "sampling_frequency", DEFAULT_FREQ_HZ) < 0) iio_device_attr_write_longlong(trigger, "frequency", DEFAULT_FREQ_HZ); iio_device_set_trigger(dev, trigger); } nb_channels = iio_device_get_channels_count(dev); if (argc == optind + 1) { /* Enable all channels */ for (i = 0; i < nb_channels; i++) iio_channel_enable(iio_device_get_channel(dev, i)); } else { for (i = 0; i < nb_channels; i++) { unsigned int j; struct iio_channel *ch = iio_device_get_channel(dev, i); for (j = optind + 1; j < (unsigned int) argc; j++) { const char *n = iio_channel_get_name(ch); if (!strcmp(argv[j], iio_channel_get_id(ch)) || (n && !strcmp(n, argv[j]))) iio_channel_enable(ch); } } } sample_size = iio_device_get_sample_size(dev); buffer = iio_device_create_buffer(dev, buffer_size, false); if (!buffer) { char buf[256]; iio_strerror(errno, buf, sizeof(buf)); fprintf(stderr, "Unable to allocate buffer: %s\n", buf); iio_context_destroy(ctx); return EXIT_FAILURE; } while (app_running) { int ret = iio_buffer_refill(buffer); if (ret < 0) { if (app_running) { char buf[256]; iio_strerror(-ret, buf, sizeof(buf)); fprintf(stderr, "Unable to refill buffer: %s\n", buf); } break; } /* If there are only the samples we requested, we don't need to * demux */ if (iio_buffer_step(buffer) == sample_size) { void *start = iio_buffer_start(buffer); size_t read_len, len = (intptr_t) iio_buffer_end(buffer) - (intptr_t) start; if (num_samples && len > num_samples * sample_size) len = num_samples * sample_size; for (read_len = len; len; ) { size_t nb = fwrite(start, 1, len, stdout); if (!nb) goto err_destroy_buffer; len -= nb; start = (void *)((intptr_t) start + nb); } if (num_samples) { num_samples -= read_len / sample_size; if (!num_samples) quit_all(EXIT_SUCCESS); } } else { iio_buffer_foreach_sample(buffer, print_sample, NULL); } } err_destroy_buffer: iio_buffer_destroy(buffer); iio_context_destroy(ctx); return exit_code; }