Use new session API.
authorBert Vermeulen <bert@biot.com>
Thu, 17 Jul 2014 20:49:20 +0000 (22:49 +0200)
committerBert Vermeulen <bert@biot.com>
Thu, 17 Jul 2014 21:10:25 +0000 (23:10 +0200)
anykey.c
input.c
parsers.c
session.c
sigrok-cli.h

index f11b0d7b031a4a3f94deefad037c861f828cdfc7..4126518e8a0801dc3700bdc3c0e86582668b47fd 100644 (file)
--- a/anykey.c
+++ b/anykey.c
@@ -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);
@@ -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.");
 }
diff --git a/input.c b/input.c
index d2adde6998b7d4d396e314bf42dd0b5f67bc87f7..c62c96406917a74e424a4de7e054625bee5894c3 100644 (file)
--- a/input.c
+++ b/input.c
@@ -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;
@@ -129,17 +130,17 @@ 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);
@@ -147,27 +148,28 @@ static void load_input_file_format(void)
 
 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 */
index a87177b99871c0efc9c59b45a410c21cd069572f..7f0d227a61b89eb97ff8b941735a0762cdefa02b 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -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;
@@ -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++) {
@@ -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;
@@ -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;
 }
index 07144aa4ee70fb02eeefe68de64d140095382c38..7c66659614d7c7673b41b54af44c8f2c115a2e2d 100644 (file)
--- a/session.c
+++ b/session.c
@@ -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;
@@ -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.");
@@ -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
                        }
                }
@@ -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)
@@ -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;
 
@@ -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;
        }
 
@@ -483,13 +485,17 @@ 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;
                }
        }
@@ -497,14 +503,14 @@ void run_session(void)
        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;
                }
        }
@@ -512,7 +518,7 @@ void run_session(void)
        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,
@@ -533,7 +539,7 @@ 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;
                }
        }
@@ -541,38 +547,39 @@ void run_session(void)
        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;
@@ -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. */
index a3289c86edbcd0f196295ca4313dda8fa8127824..b158e00223726cc7b1f97c4b195c5132ee283eab 100644 (file)
@@ -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);
@@ -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 */