Exemple #1
0
static void init_device_list(void)
{
	unsigned int i, num = iio_context_get_devices_count(ctx);
	GtkTreeIter iter;

	gtk_list_store_clear(device_list_store);

	for (i = 0; i < num; i++) {
		struct iio_device *dev = iio_context_get_device(ctx, i);
		unsigned int j, nch = iio_device_get_channels_count(dev);
		const char *id;
		bool input = false;

		for (j = 0; !input && j < nch; j++) {
			struct iio_channel *chn =
				iio_device_get_channel(dev, j);
			input = !iio_channel_is_output(chn) &&
				iio_channel_find_attr(chn, "raw");
		}

		if (!input)
			continue;

		id = iio_device_get_name(dev);
		if (!id)
			id = iio_device_get_id(dev);

		gtk_list_store_append(device_list_store, &iter);
		gtk_list_store_set(device_list_store, &iter, 0, id,  1, 0, -1);
	}
	gtk_tree_sortable_set_sort_column_id(
		GTK_TREE_SORTABLE(GTK_TREE_MODEL(device_list_store)),
		0, GTK_SORT_ASCENDING);
}
Exemple #2
0
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;
}
Exemple #3
0
/*
* Check if a device has scan elements and if it is an output device (type = 0)
* or an input device (type = 1).
*/
static bool device_type_get(const struct iio_device *dev, int type)
{
	struct iio_channel *ch;
	int nb_channels, i;

	if (!dev)
		return false;

	nb_channels = iio_device_get_channels_count(dev);
	for (i = 0; i < nb_channels; i++) {
		ch = iio_device_get_channel(dev, i);
		if (iio_channel_is_scan_element(ch) &&
		        (type ? !iio_channel_is_output(ch) : iio_channel_is_output(ch)))
			return true;
	}

	return false;
}
Exemple #4
0
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;
}
Exemple #5
0
bool iio_device_is_tx(const struct iio_device *dev)
{
	unsigned int i;

	for (i = 0; i < dev->nb_channels; i++) {
		struct iio_channel *ch = dev->channels[i];
		if (iio_channel_is_output(ch) && iio_channel_is_enabled(ch))
			return true;
	}

	return false;
}
Exemple #6
0
struct iio_channel * iio_device_find_channel(const struct iio_device *dev,
		const char *name, bool output)
{
	unsigned int i;
	for (i = 0; i < dev->nb_channels; i++) {
		struct iio_channel *chn = dev->channels[i];
		if (iio_channel_is_output(chn) != output)
			continue;

		if (!strcmp(chn->id, name) ||
				(chn->name && !strcmp(chn->name, name)))
			return chn;
	}
	return NULL;
}
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;
}
Exemple #8
0
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;
}
Exemple #9
0
static bool dmm_identify(void)
{
	/* Use the OSC's IIO context just to detect the devices */
	struct iio_context *osc_ctx = get_context_from_osc();
	unsigned int i, num;
	bool ret = false;

	num = iio_context_get_devices_count(osc_ctx);
	for (i = 0; !ret && i < num; i++) {
		struct iio_device *dev = iio_context_get_device(osc_ctx, i);
		unsigned int j, nch = iio_device_get_channels_count(dev);

		for (j = 0; !ret && j < nch; j++) {
			struct iio_channel *chn =
				iio_device_get_channel(dev, j);
			if (!iio_channel_is_output(chn))
				ret = true;
		}
	}

	return ret;
}
int main(int argc, char **argv)
{
	unsigned int buffer_size = 1024 * 1024;
	int c, option_index = 0, arg_index = 0;
	unsigned int n_tx = 0, n_rx = 0;
	static struct iio_context *ctx;
	unsigned int i, nb_channels;
	struct iio_buffer *buffer;
	pthread_t monitor_thread;
	const char *device_name;
	struct iio_device *dev;
	char unit;
	int ret;

	while ((c = getopt_long(argc, argv, "+hs:",
					options, &option_index)) != -1) {
		switch (c) {
		case 'h':
			usage(argv);
			return EXIT_SUCCESS;
		case 's':
			arg_index += 2;
			ret = sscanf(argv[arg_index], "%u%c", &buffer_size, &unit);
			if (ret == 0)
				return EXIT_FAILURE;
			if (ret == 2) {
				if (unit == 'k')
					buffer_size *= 1024;
				else if (unit == 'M')
					buffer_size *= 1024 * 1024;
			}
			break;
		case '?':
			return EXIT_FAILURE;
		}
	}

	if (arg_index + 1 >= argc) {
		fprintf(stderr, "Incorrect number of arguments.\n\n");
		usage(argv);
		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);

	ctx = iio_create_default_context();
	if (!ctx) {
		fprintf(stderr, "Unable to create IIO context\n");
		return EXIT_FAILURE;
	}

	device_name = argv[arg_index + 1];

	dev = get_device(ctx, device_name);
	if (!dev) {
		iio_context_destroy(ctx);
		return EXIT_FAILURE;
	}

	nb_channels = iio_device_get_channels_count(dev);
	for (i = 0; i < nb_channels; i++) {
		struct iio_channel *ch = iio_device_get_channel(dev, i);
		if (!iio_channel_is_scan_element(ch))
			continue;
		iio_channel_enable(ch);
		if (iio_channel_is_output(ch))
			n_tx++;
		else
			n_rx++;
	}

	if (n_tx >= n_rx)
		device_is_tx = true;
	else
		device_is_tx = false;

	printf("Monitoring %s for underflows/overflows\n",
		iio_device_get_name(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;
	}

	ret = pthread_create(&monitor_thread, NULL, monitor_thread_fn,
		(void *)device_name);
	if (ret) {
		fprintf(stderr, "Failed to create monitor thread: %s\n",
				strerror(-ret));
	}

	while (app_running) {
		if (device_is_tx) {
			ret = iio_buffer_push(buffer);
			if (ret < 0) {
				fprintf(stderr, "Unable to push buffer: %s\n",
						strerror(-ret));
				app_running = false;
				break;
			}
		} else {
			ret = iio_buffer_refill(buffer);
			if (ret < 0) {
				fprintf(stderr, "Unable to refill buffer: %s\n",
						strerror(-ret));
				app_running = false;
				break;
			}
		}
	}

	iio_buffer_destroy(buffer);
	iio_context_destroy(ctx);

	pthread_join(monitor_thread, NULL);

	return 0;
}
Exemple #11
0
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;
}
Exemple #12
0
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;
				char buf[1024], *scale;

				/* Must be input */
				if (iio_channel_is_output(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;

				/* Must have 'scale', or be a temperature, which doesn't need scale */
				if (!strstr(name, "temp") &&
						iio_channel_attr_read(chn, "scale", buf, sizeof(buf)) < 0)
					continue;

				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;
				}

				scale = strdup(buf);

				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 */
						4, scale,	/* scale */
							-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);
}