コード例 #1
0
/**
 * Save a session to the specified file.
 *
 * @param session The session to save to the specified file. Must not be NULL.
 * @param filename The name of the filename to save the session as.
 *                 Must not be NULL.
 * @param sdi The device instance from which the data was captured.
 * @param buf The data to be saved.
 * @param unitsize The number of bytes per sample.
 * @param units The number of samples.
 *
 * @retval SR_OK Success
 * @retval SR_ERR_ARG Invalid arguments
 * @retval SR_ERR Other errors
 *
 * @since 0.2.0
 */
SR_API int sr_session_save(struct sr_session *session, const char *filename,
		const struct sr_dev_inst *sdi, unsigned char *buf, int unitsize,
		int units)
{
	struct sr_channel *ch;
	GSList *l;
	GVariant *gvar;
	uint64_t samplerate;
	int cnt, ret;
	char **channel_names;

	samplerate = 0;
	if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
		if (sr_config_get(sdi->driver, sdi, NULL,
					SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
			samplerate = g_variant_get_uint64(gvar);
			g_variant_unref(gvar);
		}
	}

	channel_names = g_malloc0(sizeof(char *) * (g_slist_length(sdi->channels) + 1));
	cnt = 0;
	for (l = sdi->channels; l; l = l->next) {
		ch = l->data;
		if (ch->type != SR_CHANNEL_LOGIC)
			continue;
		if (ch->enabled != TRUE)
			continue;
		if (!ch->name)
			continue;
		/* Just borrowing the ptr. */
		channel_names[cnt++] = ch->name;
	}

	if ((ret = sr_session_save_init(session, filename, samplerate,
			channel_names)) != SR_OK)
		return ret;

	ret = sr_session_append(session, filename, buf, unitsize, units);

	return ret;
}
コード例 #2
0
ファイル: session.c プロジェクト: JenSte/sigrok-cli
void datafeed_in(const struct sr_dev_inst *sdi,
		const struct sr_datafeed_packet *packet, void *cb_data)
{
	const struct sr_datafeed_meta *meta;
	const struct sr_datafeed_logic *logic;
	const struct sr_datafeed_analog *analog;
	struct sr_config *src;
	struct sr_channel *ch;
	static struct sr_output *o = NULL;
	static uint64_t rcvd_samples_logic = 0;
	static uint64_t rcvd_samples_analog = 0;
	static uint64_t samplerate = 0;
	static int triggered = 0;
	static FILE *outfile = NULL;
	GSList *l;
	GString *out;
	GVariant *gvar;
	uint64_t end_sample;
	uint64_t input_len;
	int i;
	char **channels;

	(void) cb_data;

	/* If the first packet to come in isn't a header, don't even try. */
	if (packet->type != SR_DF_HEADER && o == NULL)
		return;

	switch (packet->type) {
	case SR_DF_HEADER:
		g_debug("cli: Received SR_DF_HEADER.");
		o = setup_output_format(sdi);

		/* Prepare non-stdout output. */
		outfile = stdout;
		if (opt_output_file) {
			if (default_output_format) {
				outfile = NULL;
			} else {
				/* saving to a file in whatever format was set
				 * with --format, so all we need is a filehandle */
				outfile = g_fopen(opt_output_file, "wb");
			}
		}
		rcvd_samples_logic = rcvd_samples_analog = 0;

		if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
				&gvar) == SR_OK) {
			samplerate = g_variant_get_uint64(gvar);
			g_variant_unref(gvar);
		}

#ifdef HAVE_SRD
		if (opt_pds) {
			if (samplerate) {
				if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
						g_variant_new_uint64(samplerate)) != SRD_OK) {
					g_critical("Failed to configure decode session.");
					break;
				}
			}
			if (srd_session_start(srd_sess) != SRD_OK) {
				g_critical("Failed to start decode session.");
				break;
			}
		}
#endif
		break;

	case SR_DF_META:
		g_debug("cli: Received SR_DF_META.");
		meta = packet->payload;
		for (l = meta->config; l; l = l->next) {
			src = l->data;
			switch (src->key) {
			case SR_CONF_SAMPLERATE:
				samplerate = g_variant_get_uint64(src->data);
				g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
#ifdef HAVE_SRD
				if (opt_pds) {
					if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
							g_variant_new_uint64(samplerate)) != SRD_OK) {
						g_critical("Failed to pass samplerate to decoder.");
					}
				}
#endif
				break;
			case SR_CONF_SAMPLE_INTERVAL:
				samplerate = g_variant_get_uint64(src->data);
				g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
				break;
			default:
				/* Unknown metadata is not an error. */
				break;
			}
		}
		break;

	case SR_DF_TRIGGER:
		g_debug("cli: Received SR_DF_TRIGGER.");
		triggered = 1;
		break;

	case SR_DF_LOGIC:
		logic = packet->payload;
		g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
				logic->length, logic->unitsize);
		if (logic->length == 0)
			break;

		/* Don't store any samples until triggered. */
		if (opt_wait_trigger && !triggered)
			break;

		if (limit_samples && rcvd_samples_logic >= limit_samples)
			break;

		end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
		/* Cut off last packet according to the sample limit. */
		if (limit_samples && end_sample > limit_samples)
			end_sample = limit_samples;
		input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;

		if (opt_output_file && default_output_format) {
			/* Saving to a session file. */
			if (rcvd_samples_logic == 0) {
				/* First packet with logic data, init session file. */
				channels = g_malloc(sizeof(char *) * g_slist_length(sdi->channels));
				for (i = 0, l = sdi->channels; l; l = l->next) {
					ch = l->data;
					if (ch->enabled && ch->type == SR_CHANNEL_LOGIC)
						channels[i++] = ch->name;
				}
				channels[i] = NULL;
				sr_session_save_init(opt_output_file, samplerate,
						channels);
				g_free(channels);
			}
			save_chunk_logic(logic->data, input_len, logic->unitsize);
		} else {
			if (opt_pds) {
#ifdef HAVE_SRD
				if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
						logic->data, input_len) != SRD_OK)
					sr_session_stop();
#endif
			}
		}

		rcvd_samples_logic = end_sample;
		break;

	case SR_DF_ANALOG:
		analog = packet->payload;
		g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
		if (analog->num_samples == 0)
			break;

		if (limit_samples && rcvd_samples_analog >= limit_samples)
			break;

		rcvd_samples_analog += analog->num_samples;
		break;

	case SR_DF_FRAME_BEGIN:
		g_debug("cli: Received SR_DF_FRAME_BEGIN.");
		break;

	case SR_DF_FRAME_END:
		g_debug("cli: Received SR_DF_FRAME_END.");
		break;

	default:
		break;
	}

	if (o && outfile && !opt_pds) {
		if (sr_output_send(o, packet, &out) == SR_OK && out) {
			fwrite(out->str, 1, out->len, outfile);
			fflush(outfile);
			g_string_free(out, TRUE);
		}
	}

	/* SR_DF_END needs to be handled after the output module's receive()
	 * is called, so it can properly clean up that module. */
	if (packet->type == SR_DF_END) {
		g_debug("cli: Received SR_DF_END.");

		if (o)
			sr_output_free(o);
		o = NULL;

		if (outfile && outfile != stdout)
			fclose(outfile);

		if (opt_output_file && default_output_format)
			/* Flush whatever is left out to the session file. */
			save_chunk_logic(NULL, 0, 0);

		if (limit_samples) {
			if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
				g_warning("Device only sent %" PRIu64 " samples.",
					   rcvd_samples_logic);
			else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
				g_warning("Device only sent %" PRIu64 " samples.",
					   rcvd_samples_analog);
		}
	}

}
コード例 #3
0
ファイル: storesession.cpp プロジェクト: plaes/pulseview
bool StoreSession::start()
{
	set< shared_ptr<data::SignalData> > data_set =
		_session.get_data();
	const vector< shared_ptr<view::Signal> > sigs =
		_session.get_signals();

	// Check we have logic data
	if (data_set.empty() || sigs.empty()) {
		_error = tr("No data to save.");
		return false;
	}

	if (data_set.size() > 1) {
		_error = tr("PulseView currently only has support for "
			"storing a single data stream.");
		return false;
	}

	// Get the logic data
	//shared_ptr<data::SignalData
	shared_ptr<data::Logic> data;
	if (!(data = dynamic_pointer_cast<data::Logic>(*data_set.begin()))) {
		_error = tr("PulseView currently only has support for "
			"storing a logic data.");
		return false;
	}

	// Get the snapshot
	const deque< shared_ptr<data::LogicSnapshot> > &snapshots =
		data->get_snapshots();

	if (snapshots.empty()) {
		_error = tr("No snapshots to save.");
		return false;
	}

	const shared_ptr<data::LogicSnapshot> snapshot(snapshots.front());
	assert(snapshot);

	// Make a list of probes
	char **const probes = new char*[sigs.size() + 1];
	for (size_t i = 0; i < sigs.size(); i++) {
		shared_ptr<view::Signal> sig(sigs[i]);
		assert(sig);
		probes[i] = strdup(sig->get_name().toUtf8().constData());
	}
	probes[sigs.size()] = NULL;

	// Begin storing
	if (sr_session_save_init(_file_name.c_str(),
		data->samplerate(), probes) != SR_OK) {
		_error = tr("Error while saving.");
		return false;
	}

	// Delete the probes array
	for (size_t i = 0; i <= sigs.size(); i++)
		free(probes[i]);
	delete[] probes;

	_thread = boost::thread(&StoreSession::store_proc, this, snapshot);
	return true;
}