]> sigrok.org Git - libsigrok.git/blobdiff - session.c
Prefixes for *_device_instance.
[libsigrok.git] / session.c
index 930b313ea244458f10c718555d35224283669619..68b1913e323f3f3997ed8b3b67c8c4c26cabd55f 100644 (file)
--- a/session.c
+++ b/session.c
 #include <unistd.h>
 #include <string.h>
 #include <zip.h>
+#include <glib.h>
 #include <sigrok.h>
 #include <config.h>
 
 
 /* There can only be one session at a time. */
 struct session *session;
+int num_sources = 0;
+/* These live in hwplugin.c, for the frontend to override. */
+extern source_callback_add source_cb_add;
+extern source_callback_remove source_cb_remove;
+
+struct source {
+       int fd;
+       int events;
+       int timeout;
+       receive_data_callback cb;
+       void *user_data;
+};
+
+struct source *sources = NULL;
+int source_timeout = -1;
+
+
+
 
 struct session *session_load(const char *filename)
 {
@@ -64,19 +83,19 @@ void session_device_clear(void)
        session->devices = NULL;
 }
 
-int session_device_add(struct device *device)
+int session_device_add(struct sr_device *device)
 {
        int ret;
 
        if (device->plugin && device->plugin->open) {
                ret = device->plugin->open(device->plugin_index);
-               if (ret != SIGROK_OK)
+               if (ret != SR_OK)
                        return ret;
        }
 
        session->devices = g_slist_append(session->devices, device);
 
-       return SIGROK_OK;
+       return SR_OK;
 }
 
 void session_pa_clear(void)
@@ -108,35 +127,89 @@ void session_datafeed_callback_add(datafeed_callback callback)
 
 int session_start(void)
 {
-       struct device *device;
+       struct sr_device *device;
        GSList *l;
        int ret;
 
-       g_message("starting acquisition");
+       g_message("starting session");
        for (l = session->devices; l; l = l->next) {
                device = l->data;
                if ((ret = device->plugin->start_acquisition(
-                               device->plugin_index, device)) != SIGROK_OK)
+                               device->plugin_index, device)) != SR_OK)
                        break;
        }
 
        return ret;
 }
 
+void session_run(void)
+{
+       GPollFD *fds, my_gpollfd;
+       int ret, i;
+
+       g_message("running session");
+       session->running = TRUE;
+       fds = NULL;
+       while (session->running) {
+               if (fds)
+                       free(fds);
+
+               /* Construct g_poll()'s array. */
+               fds = malloc(sizeof(GPollFD) * num_sources);
+               for (i = 0; i < num_sources; i++) {
+#ifdef _WIN32
+                       g_io_channel_win32_make_pollfd(&channels[0],
+                                       sources[i].events, &my_gpollfd);
+#else
+                       my_gpollfd.fd = sources[i].fd;
+                       my_gpollfd.events = sources[i].events;
+                       fds[i] = my_gpollfd;
+#endif
+               }
+
+               ret = g_poll(fds, num_sources, source_timeout);
+
+               for (i = 0; i < num_sources; i++) {
+                       if (fds[i].revents > 0 || (ret == 0
+                               && source_timeout == sources[i].timeout)) {
+                               /*
+                                * Invoke the source's callback on an event,
+                                * or if the poll timeout out and this source
+                                * asked for that timeout.
+                                */
+                               sources[i].cb(fds[i].fd, fds[i].revents,
+                                             sources[i].user_data);
+                       }
+               }
+       }
+       free(fds);
+
+}
+
+void session_halt(void)
+{
+
+       g_message("halting session");
+       session->running = FALSE;
+
+}
+
 void session_stop(void)
 {
-       struct device *device;
+       struct sr_device *device;
        GSList *l;
 
-       g_message("stopping acquisition");
+       g_message("stopping session");
+       session->running = FALSE;
        for (l = session->devices; l; l = l->next) {
                device = l->data;
                if (device->plugin)
                        device->plugin->stop_acquisition(device->plugin_index, device);
        }
+
 }
 
-void session_bus(struct device *device, struct datafeed_packet *packet)
+void session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
 {
        GSList *l;
        datafeed_callback cb;
@@ -155,7 +228,7 @@ int session_save(char *filename)
 {
        GSList *l, *p, *d;
        FILE *meta;
-       struct device *device;
+       struct sr_device *device;
        struct probe *probe;
        struct datastore *ds;
        struct zip *zipfile;
@@ -171,23 +244,23 @@ int session_save(char *filename)
        /* Quietly delete it first, libzip wants replace ops otherwise. */
        unlink(newfn);
        if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error)))
-               return SIGROK_ERR;
+               return SR_ERR;
        g_free(newfn);
 
        /* "version" */
        version[0] = '1';
        if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
-               return SIGROK_ERR;
+               return SR_ERR;
        if (zip_add(zipfile, "version", versrc) == -1) {
                g_message("error saving version into zipfile: %s",
                          zip_strerror(zipfile));
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        /* init "metadata" */
        strcpy(metafile, "sigrok-meta-XXXXXX");
        if ((tmpfile = g_mkstemp(metafile)) == -1)
-               return SIGROK_ERR;
+               return SR_ERR;
        close(tmpfile);
        meta = fopen(metafile, "wb");
        fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
@@ -230,26 +303,77 @@ int session_save(char *filename)
                        }
                        if (!(logicsrc = zip_source_buffer(zipfile, buf,
                                       ds->num_units * ds->ds_unitsize, TRUE)))
-                               return SIGROK_ERR;
+                               return SR_ERR;
                        snprintf(rawname, 15, "logic-%d", devcnt);
                        if (zip_add(zipfile, rawname, logicsrc) == -1)
-                               return SIGROK_ERR;
+                               return SR_ERR;
                }
                devcnt++;
        }
        fclose(meta);
 
        if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
-               return SIGROK_ERR;
+               return SR_ERR;
        if (zip_add(zipfile, "metadata", metasrc) == -1)
-               return SIGROK_ERR;
+               return SR_ERR;
 
        if ((ret = zip_close(zipfile)) == -1) {
                g_message("error saving zipfile: %s", zip_strerror(zipfile));
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        unlink(metafile);
 
-       return SIGROK_OK;
+       return SR_OK;
+}
+
+void session_source_add(int fd, int events, int timeout,
+               receive_data_callback callback, void *user_data)
+{
+       struct source *new_sources, *s;
+
+       new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
+
+       if (sources) {
+               memcpy(new_sources, sources,
+                      sizeof(struct source) * num_sources);
+               free(sources);
+       }
+
+       s = &new_sources[num_sources++];
+       s->fd = fd;
+       s->events = events;
+       s->timeout = timeout;
+       s->cb = callback;
+       s->user_data = user_data;
+       sources = new_sources;
+
+       if (timeout != source_timeout && timeout > 0
+           && (source_timeout == -1 || timeout < source_timeout))
+               source_timeout = timeout;
+}
+
+void session_source_remove(int fd)
+{
+       struct source *new_sources;
+       int old, new;
+
+       if (!sources)
+               return;
+
+       new_sources = calloc(1, sizeof(struct source) * num_sources);
+       for (old = 0; old < num_sources; old++)
+               if (sources[old].fd != fd)
+                       memcpy(&new_sources[new++], &sources[old],
+                              sizeof(struct source));
+
+       if (old != new) {
+               free(sources);
+               sources = new_sources;
+               num_sources--;
+       } else {
+               /* Target fd was not found. */
+               free(new_sources);
+       }
 }
+