Skip to content

Commit

Permalink
Use new session API.
Browse files Browse the repository at this point in the history
  • Loading branch information
biot committed Jul 17, 2014
1 parent 41ce2cb commit 4bf77ec
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 61 deletions.
12 changes: 7 additions & 5 deletions anykey.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ static struct termios term_orig;

static int received_anykey(int fd, int revents, void *cb_data)
{
struct sr_session *session;
(void)fd;
(void)revents;
(void)cb_data;

sr_session_source_remove(STDIN_FILENO);
sr_session_stop();
session = cb_data;
sr_session_source_remove(session, STDIN_FILENO);
sr_session_stop(session);

return TRUE;
}

/* Turn off buffering on stdin. */
void add_anykey(void)
void add_anykey(struct sr_session *session)
{
#ifdef _WIN32
stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
Expand All @@ -67,7 +68,8 @@ void add_anykey(void)
tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
#endif

sr_session_source_add(STDIN_FILENO, G_IO_IN, -1, received_anykey, NULL);
sr_session_source_add(session, STDIN_FILENO, G_IO_IN, -1,
received_anykey, session);

g_message("Press any key to stop acquisition.");
}
Expand Down
34 changes: 18 additions & 16 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static void load_input_file_format(void)
{
GHashTable *fmtargs = NULL;
struct stat st;
struct sr_session *session;
struct sr_input *in;
struct sr_input_format *input_format;
char *fmtspec = NULL;
Expand Down Expand Up @@ -129,45 +130,46 @@ static void load_input_file_format(void)
if (select_channels(in->sdi) != SR_OK)
return;

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

input_format->loadfile(in, opt_input_file);

sr_session_destroy();
sr_session_destroy(session);

if (fmtargs)
g_hash_table_destroy(fmtargs);
}

void load_input_file(void)
{
GSList *sessions;
GSList *devices;
struct sr_session *session;
struct sr_dev_inst *sdi;
int ret;

if (sr_session_load(opt_input_file) == SR_OK) {
if (sr_session_load(opt_input_file, &session) == SR_OK) {
/* sigrok session file */
ret = sr_session_dev_list(&sessions);
if (ret != SR_OK || !sessions->data) {
ret = sr_session_dev_list(session, &devices);
if (ret != SR_OK || !devices->data) {
g_critical("Failed to access session device.");
sr_session_destroy();
sr_session_destroy(session);
return;
}
sdi = sessions->data;
sdi = devices->data;
if (select_channels(sdi) != SR_OK) {
sr_session_destroy();
sr_session_destroy(session);
return;
}
sr_session_datafeed_callback_add(datafeed_in, NULL);
sr_session_start();
sr_session_run();
sr_session_stop();
sr_session_datafeed_callback_add(session, datafeed_in, NULL);
sr_session_start(session);
sr_session_run(session);
sr_session_stop(session);
}
else {
/* fall back on input modules */
Expand Down
14 changes: 6 additions & 8 deletions parsers.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ int parse_trigger_match(char c)
return match;
}

int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s)
int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
struct sr_trigger **trigger)
{
struct sr_channel *ch;
struct sr_trigger *trigger;
struct sr_trigger_stage *stage;
GVariant *gvar;
GSList *l;
Expand All @@ -199,7 +199,7 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s)
}
matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));

trigger = sr_trigger_new(NULL);
*trigger = sr_trigger_new(NULL);
error = FALSE;
tokens = g_strsplit(s, ",", -1);
for (i = 0; tokens[i]; i++) {
Expand Down Expand Up @@ -241,8 +241,8 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s)
}
/* Make sure this ends up in the right stage, creating
* them as needed. */
while (!(stage = g_slist_nth_data(trigger->stages, t)))
sr_trigger_stage_add(trigger);
while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
sr_trigger_stage_add(*trigger);
if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
error = TRUE;
break;
Expand All @@ -253,9 +253,7 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s)
g_variant_unref(gvar);

if (error)
sr_trigger_free(trigger);
else
error = sr_session_trigger_set(trigger) != SR_OK;
sr_trigger_free(*trigger);

return !error;
}
Expand Down
65 changes: 36 additions & 29 deletions session.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void datafeed_in(const struct sr_dev_inst *sdi,
const struct sr_datafeed_meta *meta;
const struct sr_datafeed_logic *logic;
const struct sr_datafeed_analog *analog;
struct sr_session *session;
struct sr_config *src;
struct sr_channel *ch;
static struct sr_output *o = NULL;
Expand All @@ -137,12 +138,11 @@ void datafeed_in(const struct sr_dev_inst *sdi,
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;

session = cb_data;
switch (packet->type) {
case SR_DF_HEADER:
g_debug("cli: Received SR_DF_HEADER.");
Expand Down Expand Up @@ -249,17 +249,17 @@ void datafeed_in(const struct sr_dev_inst *sdi,
channels[i++] = ch->name;
}
channels[i] = NULL;
sr_session_save_init(opt_output_file, samplerate,
channels);
sr_session_save_init(session, opt_output_file,
samplerate, channels);
g_free(channels);
}
save_chunk_logic(logic->data, input_len, logic->unitsize);
save_chunk_logic(session, 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();
sr_session_stop(session);
#endif
}
}
Expand Down Expand Up @@ -313,7 +313,7 @@ void datafeed_in(const struct sr_dev_inst *sdi,

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

if (limit_samples) {
if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
Expand Down Expand Up @@ -445,6 +445,8 @@ void run_session(void)
GSList *devices;
GHashTable *devargs;
GVariant *gvar;
struct sr_session *session;
struct sr_trigger *trigger;
struct sr_dev_inst *sdi;
uint64_t min_samples, max_samples;

Expand All @@ -459,17 +461,17 @@ void run_session(void)
}
sdi = devices->data;

sr_session_new();
sr_session_datafeed_callback_add(datafeed_in, NULL);
sr_session_new(&session);
sr_session_datafeed_callback_add(session, 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) {
if (sr_session_dev_add(session, sdi) != SR_OK) {
g_critical("Failed to add device to session.");
sr_session_destroy();
sr_session_destroy(session);
return;
}

Expand All @@ -483,36 +485,40 @@ void run_session(void)

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

if (opt_triggers) {
if (!parse_triggerstring(sdi, opt_triggers)) {
sr_session_destroy();
if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
sr_session_destroy(session);
return;
}
if (sr_session_trigger_set(session, trigger) != SR_OK) {
sr_session_destroy(session);
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();
sr_session_destroy(session);
return;
}
}

if (opt_time) {
if (set_limit_time(sdi) != SR_OK) {
sr_session_destroy();
sr_session_destroy(session);
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();
sr_session_destroy(session);
return;
}
if (sr_config_list(sdi->driver, sdi, NULL,
Expand All @@ -533,46 +539,47 @@ void run_session(void)
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();
sr_session_destroy(session);
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();
sr_session_destroy(session);
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();
sr_session_destroy(session);
return;
}
}

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

if (opt_continuous)
add_anykey();
add_anykey(session);

sr_session_run();
sr_session_run(session);

if (opt_continuous)
clear_anykey();

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

}

void save_chunk_logic(uint8_t *data, uint64_t data_len, int unitsize)
void save_chunk_logic(struct sr_session *session, uint8_t *data,
uint64_t data_len, int unitsize)
{
static uint8_t *buf = NULL;
static int buf_len = 0;
Expand All @@ -585,13 +592,13 @@ void save_chunk_logic(uint8_t *data, uint64_t data_len, int unitsize)
if (buf_len + data_len > SAVE_CHUNK_SIZE) {
max = (SAVE_CHUNK_SIZE - buf_len) / unitsize * unitsize;
memcpy(buf + buf_len, data, max);
sr_session_append(opt_output_file, buf, unitsize,
sr_session_append(session, opt_output_file, buf, unitsize,
(buf_len + max) / unitsize);
memcpy(buf, data + max, data_len - max);
buf_len = data_len - max;
} else if (data_len == 0 && last_unitsize != 0) {
/* End of data, flush the buffer out. */
sr_session_append(opt_output_file, buf, last_unitsize,
sr_session_append(session, opt_output_file, buf, last_unitsize,
buf_len / last_unitsize);
} else {
/* Buffer chunk. */
Expand Down
8 changes: 5 additions & 3 deletions sigrok-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ void datafeed_in(const struct sr_dev_inst *sdi,
int opt_to_gvar(char *key, char *value, struct sr_config *src);
int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args);
void run_session(void);
void save_chunk_logic(uint8_t *data, uint64_t data_len, int unitsize);
void save_chunk_logic(struct sr_session *session, uint8_t *data,
uint64_t data_len, int unitsize);

/* input.c */
void load_input_file(void);
Expand All @@ -70,12 +71,13 @@ void map_pd_channels(struct sr_dev_inst *sdi);
/* parsers.c */
struct sr_channel *find_channel(GSList *channellist, const char *channelname);
GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring);
int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s);
int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
struct sr_trigger **trigger);
GHashTable *parse_generic_arg(const char *arg, gboolean sep_first);
int canon_cmp(const char *str1, const char *str2);

/* anykey.c */
void add_anykey(void);
void add_anykey(struct sr_session *session);
void clear_anykey(void);

/* options.c */
Expand Down

0 comments on commit 4bf77ec

Please sign in to comment.