Esempio n. 1
0
static void dev_selected(GtkComboBox *dev, GObject *parent)
{
	GtkTreeModel *devlist = gtk_combo_box_get_model(dev);
	GtkTreeIter iter;
	const gchar *name;
	GtkCheckMenuItem *menuitem;
	struct sr_device *device;

	if (!gtk_combo_box_get_active_iter(dev, &iter)) {
		g_object_set_data(parent, "device", NULL);
		return;
	}
	gtk_tree_model_get(devlist, &iter, 0, &name, 1, &device,
				2, &menuitem, -1);

	gtk_check_menu_item_set_active(menuitem, TRUE);

	sr_session_device_clear();
	if (sr_session_device_add(device) != SR_OK) {
		g_critical("Failed to use device.");
		sr_session_destroy();
		device = NULL;
	}
	g_object_set_data(parent, "device", device);
}
Esempio n. 2
0
void Device::release()
{
	if (_owner) {
		DevInst::release();
		sr_session_destroy();
	}

	sr_dev_close(_sdi);
}
Esempio n. 3
0
void SessionFile::release()
{
	if (!_owner)
		return;

	assert(_sdi);
	File::release();
	sr_dev_close(_sdi);
	sr_dev_clear(_sdi->driver);
	sr_session_destroy();
	_sdi = NULL;
}
Esempio n. 4
0
END_TEST

/*
 * Check whether multiple sr_session_new() calls work.
 * If any call returns != SR_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_new_multiple)
{
	int ret;
	struct sr_session *sess1, *sess2, *sess3;

	sess1 = sess2 = sess3 = NULL;

	/* Multiple sr_session_new() calls must work. */
	ret = sr_session_new(srtest_ctx, &sess1);
	fail_unless(ret == SR_OK, "sr_session_new() 1 failed: %d.", ret);
	ret = sr_session_new(srtest_ctx, &sess2);
	fail_unless(ret == SR_OK, "sr_session_new() 2 failed: %d.", ret);
	ret = sr_session_new(srtest_ctx, &sess3);
	fail_unless(ret == SR_OK, "sr_session_new() 3 failed: %d.", ret);

	/* The returned session pointers must all be non-NULL. */
	fail_unless(sess1 != NULL);
	fail_unless(sess2 != NULL);
	fail_unless(sess3 != NULL);

	/* The returned session pointers must not be the same. */
	fail_unless(sess1 != sess2);
	fail_unless(sess1 != sess3);
	fail_unless(sess2 != sess3);

	/* Destroying any of the sessions must work. */
	ret = sr_session_destroy(sess1);
	fail_unless(ret == SR_OK, "sr_session_destroy() 1 failed: %d.", ret);
	ret = sr_session_destroy(sess2);
	fail_unless(ret == SR_OK, "sr_session_destroy() 2 failed: %d.", ret);
	ret = sr_session_destroy(sess3);
	fail_unless(ret == SR_OK, "sr_session_destroy() 3 failed: %d.", ret);
}
Esempio n. 5
0
END_TEST

/*
 * Check whether sr_session_destroy() fails for bogus sessions.
 * If it returns SR_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_destroy_bogus)
{
	int ret;

	ret = sr_session_destroy(NULL);
	fail_unless(ret != SR_OK, "sr_session_destroy() worked.");
}
Esempio n. 6
0
END_TEST

/*
 * Check whether sr_session_destroy() works.
 * If it returns != SR_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_destroy)
{
	int ret;
	struct sr_session *sess;

	sr_session_new(srtest_ctx, &sess);
	ret = sr_session_destroy(sess);
	fail_unless(ret == SR_OK, "sr_session_destroy() failed: %d.", ret);
}
Esempio n. 7
0
END_TEST

START_TEST(test_session_trigger_set_get_null)
{
	int ret;
	struct sr_session *sess;
	struct sr_trigger *t;

	sr_session_new(srtest_ctx, &sess);

	/* Adding a NULL trigger is allowed. */
	ret = sr_session_trigger_set(sess, NULL);
	fail_unless(ret == SR_OK);
	t = sr_session_trigger_get(sess);
	fail_unless(t == NULL);

	sr_session_destroy(sess);
}
Esempio n. 8
0
File* File::create(QString name)
{
    if (sr_session_load(name.toLocal8Bit().data()) == SR_OK) {
		GSList *devlist = NULL;
		sr_session_dev_list(&devlist);
		sr_session_destroy();

		if (devlist) {
			sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
			g_slist_free(devlist);
			if (sdi) {
				sr_dev_close(sdi);
				sr_dev_clear(sdi->driver);
				return new SessionFile(name);
			}
		}
	}

	return new InputFile(name);
}
Esempio n. 9
0
File* File::create(const string &name)
{
	struct sr_session *temp_session;
	if (sr_session_load(name.c_str(), &temp_session) == SR_OK) {
		GSList *devlist = NULL;
		sr_session_dev_list(temp_session, &devlist);
		sr_session_destroy(temp_session);

		if (devlist) {
			sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
			g_slist_free(devlist);
			if (sdi) {
				sr_dev_close(sdi);
				sr_dev_clear(sdi->driver);
				return new SessionFile(name);
			}
		}
	}

	return NULL;
}
Esempio n. 10
0
END_TEST

START_TEST(test_session_trigger_set_get)
{
	int ret;
	struct sr_session *sess;
	struct sr_trigger *t1, *t2;

	sr_session_new(srtest_ctx, &sess);
	t1 = sr_trigger_new("T1");

	/* Set a trigger and see if getting it works OK. */
	ret = sr_session_trigger_set(sess, t1);
	fail_unless(ret == SR_OK);
	t2 = sr_session_trigger_get(sess);
	fail_unless(t2 != NULL);
	fail_unless(t1 == t2);
	fail_unless(g_slist_length(t1->stages) == g_slist_length(t2->stages));
	fail_unless(!strcmp(t1->name, t2->name));

	sr_session_destroy(sess);
}
Esempio n. 11
0
static void dev_selected(GtkComboBox *devbox, GObject *parent)
{
	GtkTreeModel *devlist = gtk_combo_box_get_model(devbox);
	GtkEntry *timesamples = g_object_get_data(parent, "timesamples");
	GtkComboBox *timeunit = g_object_get_data(parent, "timeunit");
	GtkTreeIter iter;
	const gchar *name;
	GtkCheckMenuItem *menuitem;
	struct sr_dev *dev;

	if (!gtk_combo_box_get_active_iter(devbox, &iter)) {
		g_object_set_data(parent, "dev", NULL);
		return;
	}
	gtk_tree_model_get(devlist, &iter, 0, &name, 1, &dev,
				2, &menuitem, -1);

	gtk_check_menu_item_set_active(menuitem, TRUE);

	sr_session_dev_remove_all();
	if (sr_session_dev_add(dev) != SR_OK) {
		g_critical("Failed to use device.");
		sr_session_destroy();
		dev = NULL;
	}
	g_object_set_data(parent, "dev", dev);

	/*
	 * Grey out the time unless the device is valid,
	 * and it supports sample limiting
	 */
	const gboolean limit_samples = dev &&
		sr_driver_hwcap_exists(dev->driver,
		SR_HWCAP_LIMIT_SAMPLES);
	gtk_widget_set_sensitive((GtkWidget*)timesamples, limit_samples);
	gtk_widget_set_sensitive((GtkWidget*)timeunit, limit_samples);
}
Esempio n. 12
0
static void capture_run(GtkAction *action, GObject *parent)
{
	(void)action;

	struct sr_dev *dev = g_object_get_data(G_OBJECT(parent), "dev");
	GtkEntry *timesamples = g_object_get_data(parent, "timesamples");
	GtkComboBox *timeunit = g_object_get_data(parent, "timeunit");
	gint i = gtk_combo_box_get_active(timeunit);
	guint64 time_msec = 0;
	guint64 limit_samples = 0;
	
	switch (i) {
	case 0: /* Samples */
		sr_parse_sizestring(gtk_entry_get_text(timesamples), 
				&limit_samples);
		break;
	case 1: /* Milliseconds */
		time_msec = strtoull(gtk_entry_get_text(timesamples), NULL, 10);
		break;
	case 2: /* Seconds */
		time_msec = strtoull(gtk_entry_get_text(timesamples), NULL, 10)
				* 1000;
		break;
	}

	if (time_msec) {
		if (sr_driver_hwcap_exists(dev->driver, SR_HWCAP_LIMIT_MSEC)) {
			if (dev->driver->dev_config_set(dev->driver_index,
							SR_HWCAP_LIMIT_MSEC,
							&time_msec) != SR_OK) {
				g_critical("Failed to configure time limit.");
				sr_session_destroy();
				return;
			}
		} else {
			/* time limit set, but device doesn't support this...
			 * convert to samples based on the samplerate.
			 */
			limit_samples = 0;
			if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) {
				guint64 tmp_u64;
				tmp_u64 = *((uint64_t *)dev->driver->dev_info_get(
							dev->driver_index,
							SR_DI_CUR_SAMPLERATE));
				limit_samples = tmp_u64 * time_msec / (uint64_t) 1000;
			}
			if (limit_samples == 0) {
				g_critical("Not enough time at this samplerate.");
				return;
			}

			if (dev->driver->dev_config_set(dev->driver_index,
						SR_HWCAP_LIMIT_SAMPLES,
						&limit_samples) != SR_OK) {
				g_critical("Failed to configure time-based sample limit.");
				return;
			}
		}
	}
	if (limit_samples) {
		if (dev->driver->dev_config_set(dev->driver_index,
						SR_HWCAP_LIMIT_SAMPLES,
						&limit_samples) != SR_OK) {
			g_critical("Failed to configure sample limit.");
			return;
		}
	}

	if (dev->driver->dev_config_set(dev->driver_index,
	    SR_HWCAP_PROBECONFIG, (char *)dev->probes) != SR_OK) {
		printf("Failed to configure probes.\n");
		sr_session_destroy();
		return;
	}

	if (sr_session_start() != SR_OK) {
		g_critical("Failed to start session.");
		return;
	}

	sr_session_run();
}
Esempio n. 13
0
void run_session(void)
{
	GSList *devices;
	GHashTable *devargs;
	GVariant *gvar;
	struct sr_dev_inst *sdi;
	uint64_t min_samples, max_samples;

	devices = device_scan();
	if (!devices) {
		g_critical("No devices found.");
		return;
	}
	if (g_slist_length(devices) > 1) {
		g_critical("sigrok-cli only supports one device for capturing.");
		return;
	}
	sdi = devices->data;

	sr_session_new();
	sr_session_datafeed_callback_add(datafeed_in, NULL);

	if (sr_dev_open(sdi) != SR_OK) {
		g_critical("Failed to open device.");
		return;
	}

	if (sr_session_dev_add(sdi) != SR_OK) {
		g_critical("Failed to add device to session.");
		sr_session_destroy();
		return;
	}

	if (opt_config) {
		if ((devargs = parse_generic_arg(opt_config, FALSE))) {
			if (set_dev_options(sdi, devargs) != SR_OK)
				return;
			g_hash_table_destroy(devargs);
		}
	}

	if (select_channels(sdi) != SR_OK) {
		g_critical("Failed to set channels.");
		sr_session_destroy();
		return;
	}

	if (opt_triggers) {
		if (!parse_triggerstring(sdi, opt_triggers)) {
			sr_session_destroy();
			return;
		}
	}

	if (opt_continuous) {
		if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
			g_critical("This device does not support continuous sampling.");
			sr_session_destroy();
			return;
		}
	}

	if (opt_time) {
		if (set_limit_time(sdi) != SR_OK) {
			sr_session_destroy();
			return;
		}
	}

	if (opt_samples) {
		if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
			g_critical("Invalid sample limit '%s'.", opt_samples);
			sr_session_destroy();
			return;
		}
		if (sr_config_list(sdi->driver, sdi, NULL,
				SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
			/* The device has no compression, or compression is turned
			 * off, and publishes its sample memory size. */
			g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
			g_variant_unref(gvar);
			if (limit_samples < min_samples) {
				g_critical("The device stores at least %"PRIu64
						" samples with the current settings.", min_samples);
			}
			if (limit_samples > max_samples) {
				g_critical("The device can store only %"PRIu64
						" samples with the current settings.", max_samples);
			}
		}
		gvar = g_variant_new_uint64(limit_samples);
		if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
			g_critical("Failed to configure sample limit.");
			sr_session_destroy();
			return;
		}
	}

	if (opt_frames) {
		if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
			g_critical("Invalid sample limit '%s'.", opt_samples);
			sr_session_destroy();
			return;
		}
		gvar = g_variant_new_uint64(limit_frames);
		if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
			g_critical("Failed to configure frame limit.");
			sr_session_destroy();
			return;
		}
	}

	if (sr_session_start() != SR_OK) {
		g_critical("Failed to start session.");
		sr_session_destroy();
		return;
	}

	if (opt_continuous)
		add_anykey();

	sr_session_run();

	if (opt_continuous)
		clear_anykey();

	sr_session_datafeed_callback_remove_all();
	sr_session_destroy();
	g_slist_free(devices);

}
Esempio n. 14
0
void MainWindow::on_action_Get_samples_triggered()
{
	uint64_t samplerate;
	QString s;
	GSList *devs = NULL;
	int opt_dev;
	struct sr_dev *dev;
	QComboBox *n = ui->comboBoxNumSamples;

	opt_dev = 0; /* FIXME */

	/*
	 * The number of samples to get is a drop-down list, but you can also
	 * manually enter a value. If the latter, we have to get the value from
	 * the lineEdit object, otherwise via itemData() and the list index.
	 */
	if (n->lineEdit() != NULL) {
		limit_samples = n->lineEdit()->text().toLongLong();
	} else {
		limit_samples = n->itemData(n->currentIndex()).toLongLong();
	}

	samplerate = ui->comboBoxSampleRate->itemData(
		ui->comboBoxSampleRate->currentIndex()).toLongLong();

	/* TODO: Sanity checks. */

	/* TODO: Assumes unitsize == 1. */
	if (!(sample_buffer = (uint8_t *)malloc(limit_samples))) {
		/* TODO: Error handling. */
		return;
	}

	sr_session_new();
	sr_session_datafeed_callback_add(datafeed_in);

	devs = sr_dev_list();

	dev = (struct sr_dev *)g_slist_nth_data(devs, opt_dev);

	/* Set the number of samples we want to get from the device. */
	if (dev->driver->dev_config_set(dev->driver_index,
	    SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK) {
		qDebug("Failed to set sample limit.");
		sr_session_destroy();
		return;
	}

	if (sr_session_dev_add(dev) != SR_OK) {
		qDebug("Failed to use device.");
		sr_session_destroy();
		return;
	}

	/* Set the samplerate. */
	if (dev->driver->dev_config_set(dev->driver_index,
	    SR_HWCAP_SAMPLERATE, &samplerate) != SR_OK) {
		qDebug("Failed to set samplerate.");
		sr_session_destroy();
		return;
	};

	if (dev->driver->dev_config_set(dev->driver_index,
	    SR_HWCAP_PROBECONFIG, (char *)dev->probes) != SR_OK) {
		qDebug("Failed to configure probes.");
		sr_session_destroy();
		return;
	}

	if (sr_session_start() != SR_OK) {
		qDebug("Failed to start session.");
		sr_session_destroy();
		return;
	}

	progress = new QProgressDialog("Getting samples from logic analyzer...",
				       "Abort", 0, limit_samples, this);
	progress->setWindowModality(Qt::WindowModal);
	progress->setMinimumDuration(100);

	sr_session_run();

	sr_session_stop();
	sr_session_destroy();

	for (int i = 0; i < getNumChannels(); ++i) {
		channelForms[i]->setNumSamples(limit_samples);
		// channelForms[i]->setSampleStart(0);
		// channelForms[i]->setSampleEnd(limit_samples);

		/* If any of the scale factors change, update all of them.. */
		connect(channelForms[i], SIGNAL(scaleFactorChanged(float)),
		        w, SLOT(updateScaleFactors(float)));

		channelForms[i]->generatePainterPath();
		// channelForms[i]->update();
	}

	setNumSamples(limit_samples);
	
	/* Enable the relevant labels/buttons. */
	ui->labelSampleStart->setEnabled(true);
	ui->labelSampleEnd->setEnabled(true);
	ui->labelScaleFactor->setEnabled(true);
	ui->action_Save_as->setEnabled(true);

	// sr_hw_get_samples_shutdown(&ctx, 1000);
}
Esempio n. 15
0
static void run_session(void)
{
	struct sr_dev *dev;
	GHashTable *devargs;
	int num_devs, max_probes, i;
	uint64_t time_msec;
	char **probelist, *devspec;

	devargs = NULL;
	if (opt_dev) {
		devargs = parse_generic_arg(opt_dev);
		devspec = g_hash_table_lookup(devargs, "sigrok_key");
		dev = parse_devstring(devspec);
		if (!dev) {
			g_critical("Device not found.");
			return;
		}
		g_hash_table_remove(devargs, "sigrok_key");
	} else {
		num_devs = num_real_devs();
		if (num_devs == 1) {
			/* No device specified, but there is only one. */
			devargs = NULL;
			dev = parse_devstring("0");
		} else if (num_devs == 0) {
			g_critical("No devices found.");
			return;
		} else {
			g_critical("%d devices found, please select one.", num_devs);
			return;
		}
	}

	sr_session_new();
	sr_session_datafeed_callback_add(datafeed_in);

	if (sr_session_dev_add(dev) != SR_OK) {
		g_critical("Failed to use device.");
		sr_session_destroy();
		return;
	}

	if (devargs) {
		if (set_dev_options(dev, devargs) != SR_OK) {
			sr_session_destroy();
			return;
		}
		g_hash_table_destroy(devargs);
	}

	if (select_probes(dev) != SR_OK)
            return;

	if (opt_continuous) {
		if (!sr_driver_hwcap_exists(dev->driver, SR_HWCAP_CONTINUOUS)) {
			g_critical("This device does not support continuous sampling.");
			sr_session_destroy();
			return;
		}
	}

	if (opt_triggers) {
		probelist = sr_parse_triggerstring(dev, opt_triggers);
		if (!probelist) {
			sr_session_destroy();
			return;
		}

		max_probes = g_slist_length(dev->probes);
		for (i = 0; i < max_probes; i++) {
			if (probelist[i]) {
				sr_dev_trigger_set(dev, i + 1, probelist[i]);
				g_free(probelist[i]);
			}
		}
		g_free(probelist);
	}

	if (opt_time) {
		time_msec = sr_parse_timestring(opt_time);
		if (time_msec == 0) {
			g_critical("Invalid time '%s'", opt_time);
			sr_session_destroy();
			return;
		}

		if (sr_driver_hwcap_exists(dev->driver, SR_HWCAP_LIMIT_MSEC)) {
			if (dev->driver->dev_config_set(dev->driver_index,
			    SR_HWCAP_LIMIT_MSEC, &time_msec) != SR_OK) {
				g_critical("Failed to configure time limit.");
				sr_session_destroy();
				return;
			}
		}
		else {
			/* time limit set, but device doesn't support this...
			 * convert to samples based on the samplerate.
			 */
			limit_samples = 0;
			if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) {
				const uint64_t *samplerate;

				sr_dev_info_get(dev, SR_DI_CUR_SAMPLERATE,
						(const void **)&samplerate);
				limit_samples = (*samplerate) * time_msec / (uint64_t)1000;
			}
			if (limit_samples == 0) {
				g_critical("Not enough time at this samplerate.");
				sr_session_destroy();
				return;
			}

			if (dev->driver->dev_config_set(dev->driver_index,
			    SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK) {
				g_critical("Failed to configure time-based sample limit.");
				sr_session_destroy();
				return;
			}
		}
	}

	if (opt_samples) {
		if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)
			|| (dev->driver->dev_config_set(dev->driver_index,
			    SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK)) {
			g_critical("Failed to configure sample limit.");
			sr_session_destroy();
			return;
		}
	}

	if (opt_frames) {
		if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)
			|| (dev->driver->dev_config_set(dev->driver_index,
			    SR_HWCAP_LIMIT_FRAMES, &limit_frames) != SR_OK)) {
			printf("Failed to configure frame limit.\n");
			sr_session_destroy();
			return;
		}
	}

	if (dev->driver->dev_config_set(dev->driver_index,
		  SR_HWCAP_PROBECONFIG, (char *)dev->probes) != SR_OK) {
		g_critical("Failed to configure probes.");
		sr_session_destroy();
		return;
	}

	if (sr_session_start() != SR_OK) {
		g_critical("Failed to start session.");
		sr_session_destroy();
		return;
	}

	if (opt_continuous)
		add_anykey();

	sr_session_run();

	if (opt_continuous)
		clear_anykey();

	if (opt_output_file && default_output_format) {
		if (sr_session_save(opt_output_file) != SR_OK)
			g_critical("Failed to save session.");
	}
	sr_session_destroy();
}
Esempio n. 16
0
static void load_input_file_format(void)
{
	GHashTable *fmtargs = NULL;
	struct stat st;
	struct sr_input *in;
	struct sr_input_format *input_format;
	char *fmtspec = NULL;

	if (opt_input_format) {
		fmtargs = parse_generic_arg(opt_input_format);
		fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
	}

	if (!(input_format = determine_input_file_format(opt_input_file,
						   fmtspec))) {
		/* The exact cause was already logged. */
		return;
	}
;
	if (fmtargs)
		g_hash_table_remove(fmtargs, "sigrok_key");

	if (stat(opt_input_file, &st) == -1) {
		g_critical("Failed to load %s: %s", opt_input_file,
			strerror(errno));
		exit(1);
	}

	/* Initialize the input module. */
	if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
		g_critical("Failed to allocate input module.");
		exit(1);
	}
	in->format = input_format;
	in->param = fmtargs;
	if (in->format->init) {
		if (in->format->init(in) != SR_OK) {
			g_critical("Input format init failed.");
			exit(1);
		}
	}

	if (select_probes(in->vdev) > 0)
            return;

	sr_session_new();
	sr_session_datafeed_callback_add(datafeed_in);
	if (sr_session_dev_add(in->vdev) != SR_OK) {
		g_critical("Failed to use device.");
		sr_session_destroy();
		return;
	}

	input_format->loadfile(in, opt_input_file);
	if (opt_output_file && default_output_format) {
		if (sr_session_save(opt_output_file) != SR_OK)
			g_critical("Failed to save session.");
	}
	sr_session_destroy();

	if (fmtargs)
		g_hash_table_destroy(fmtargs);
}