]> sigrok.org Git - libsigrok.git/blobdiff - session.c
better cleanup of device/plugin resources
[libsigrok.git] / session.c
index e7d3c7008cf87fae7e2e99208a4dee8abee16e68..632ac9467763a5d3dbe042a00fa1a579a225c1d9 100644 (file)
--- a/session.c
+++ b/session.c
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <zip.h>
+#include <glib.h>
 #include <sigrok.h>
+#include <sigrok-internal.h>
 
-/* There can only be one session at a time. */
-struct session *session;
+/* demo.c */
+extern GIOChannel channels[2];
 
-struct session *session_load(const char *filename)
-{
-       struct session *session;
+struct source {
+       int fd;
+       int events;
+       int timeout;
+       sr_receive_data_callback cb;
+       void *user_data;
+};
 
-       /* Avoid compiler warnings. */
-       filename = filename;
+/* There can only be one session at a time. */
+struct sr_session *session;
+int num_sources = 0;
 
-       /* TODO: Implement. */
-       session = NULL;
+struct source *sources = NULL;
+int source_timeout = -1;
 
-       return session;
-}
 
-struct session *session_new(void)
+struct sr_session *sr_session_new(void)
 {
-       session = calloc(1, sizeof(struct session));
+       session = calloc(1, sizeof(struct sr_session));
 
        return session;
 }
 
-void session_destroy(void)
+void sr_session_destroy(void)
 {
+
        g_slist_free(session->devices);
 
-       /* TODO: Loop over protocols and free them. */
+       /* TODO: Loop over protocol decoders and free them. */
 
        g_free(session);
 }
 
-void session_device_clear(void)
+void sr_session_device_clear(void)
 {
        g_slist_free(session->devices);
        session->devices = NULL;
 }
 
-int session_device_add(struct device *device)
+int sr_session_device_add(struct sr_device *device)
 {
        int ret;
 
-       ret = device->plugin->open(device->plugin_index);
-       if (ret == SIGROK_OK)
-               session->devices = g_slist_append(session->devices, device);
+       if (device->plugin && device->plugin->opendev) {
+               ret = device->plugin->opendev(device->plugin_index);
+               if (ret != SR_OK)
+                       return ret;
+       }
 
-       return ret;
+       session->devices = g_slist_append(session->devices, device);
+
+       return SR_OK;
 }
 
-void session_pa_clear(void)
+#if 0
+void sr_session_pa_clear(void)
 {
        /*
         * The protocols are pointers to the global set of PA plugins,
@@ -83,59 +94,130 @@ void session_pa_clear(void)
        session->analyzers = NULL;
 }
 
-void session_pa_add(struct analyzer *an)
+void sr_session_pa_add(struct sr_analyzer *an)
 {
        session->analyzers = g_slist_append(session->analyzers, an);
 }
+#endif
 
-void session_datafeed_callback_clear(void)
+void sr_session_datafeed_callback_clear(void)
 {
        g_slist_free(session->datafeed_callbacks);
        session->datafeed_callbacks = NULL;
 }
 
-void session_datafeed_callback_add(datafeed_callback callback)
+void sr_session_datafeed_callback_add(sr_datafeed_callback callback)
 {
        session->datafeed_callbacks =
            g_slist_append(session->datafeed_callbacks, callback);
 }
 
-int session_start(void)
+static void sr_session_run_poll()
 {
-       struct device *device;
+       GPollFD *fds, my_gpollfd;
+       int ret, i;
+
+       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);
+
+}
+
+int sr_session_start(void)
+{
+       struct sr_device *device;
        GSList *l;
        int ret;
 
-       g_message("starting acquisition");
+       sr_info("session: starting");
        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_stop(void)
+void sr_session_run(void)
+{
+
+       sr_info("session: running");
+       session->running = TRUE;
+
+       /* do we have real sources? */
+       if (num_sources == 1 && sources[0].fd == -1)
+               /* dummy source, freewheel over it */
+               while (session->running)
+                       sources[0].cb(-1, 0, sources[0].user_data);
+       else
+               /* real sources, use g_poll() main loop */
+               sr_session_run_poll();
+
+}
+
+void sr_session_halt(void)
+{
+
+       sr_info("session: halting");
+       session->running = FALSE;
+
+}
+
+void sr_session_stop(void)
 {
-       struct device *device;
+       struct sr_device *device;
        GSList *l;
 
-       g_message("stopping acquisition");
+       sr_info("session: stopping");
+       session->running = FALSE;
        for (l = session->devices; l; l = l->next) {
                device = l->data;
-               device->plugin->stop_acquisition(device->plugin_index, device);
+               if (device->plugin && device->plugin->stop_acquisition)
+                       device->plugin->stop_acquisition(device->plugin_index, device);
        }
+
 }
 
-void session_bus(struct device *device, struct datafeed_packet *packet)
+void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
 {
        GSList *l;
-       datafeed_callback cb;
+       sr_datafeed_callback cb;
 
        /*
-        * TODO: Send packet through PA pipe, and send the output of that to
+        * TODO: Send packet through PD pipe, and send the output of that to
         * the callbacks as well.
         */
        for (l = session->datafeed_callbacks; l; l = l->next) {
@@ -144,114 +226,53 @@ void session_bus(struct device *device, struct datafeed_packet *packet)
        }
 }
 
-void make_metadata(char *filename)
+void sr_session_source_add(int fd, int events, int timeout,
+               sr_receive_data_callback callback, void *user_data)
 {
-       GSList *l, *p;
-       struct device *device;
-       struct probe *probe;
-       FILE *f;
-       int devcnt;
-
-       f = fopen(filename, "wb");
+       struct source *new_sources, *s;
 
-       /* General */
+       new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
 
-       /* Devices */
-       devcnt = 1;
-       for (l = session->devices; l; l = l->next) {
-               device = l->data;
-               fprintf(f, "[device]\n");
-               fprintf(f, "driver = %s\n", device->plugin->name);
-
-               if (device->datastore)
-                       fprintf(f, "capturefile = raw-%d\n", devcnt);
-
-               for (p = device->probes; p; p = p->next) {
-                       probe = p->data;
-                       if (probe->enabled) {
-                               fprintf(f, "probe %d", probe->index);
-                               if (probe->name)
-                                       fprintf(f, " name \"%s\"", probe->name);
-                               if (probe->trigger)
-                                       fprintf(f, " trigger \"%s\"",
-                                               probe->trigger);
-                               fprintf(f, "\n");
-                       }
-               }
-               devcnt++;
+       if (sources) {
+               memcpy(new_sources, sources,
+                      sizeof(struct source) * num_sources);
+               free(sources);
        }
 
-       /* TODO: Protocol analyzers */
-
-       fclose(f);
+       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;
 }
 
-int session_save(char *filename)
+void sr_session_source_remove(int fd)
 {
-       GSList *l, *d;
-       struct device *device;
-       struct datastore *ds;
-       struct zip *zipfile;
-       struct zip_source *src;
-       int bufcnt, devcnt, tmpfile, ret, error;
-       char version[1], rawname[16], metafile[32], *buf;
-
-       /* Quietly delete it first, libzip wants replace ops otherwise. */
-       unlink(filename);
-
-       if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
-               return SIGROK_ERR;
-
-       /* Version */
-       version[0] = '1';
-       if (!(src = zip_source_buffer(zipfile, version, 1, 0)))
-               return SIGROK_ERR;
-       if (zip_add(zipfile, "version", src) == -1) {
-               g_message("error saving version into zipfile: %s",
-                         zip_strerror(zipfile));
-               return SIGROK_ERR;
+       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);
        }
-
-       /* Metadata */
-       strcpy(metafile, "sigrok-meta-XXXXXX");
-       if ((tmpfile = g_mkstemp(metafile)) == -1)
-               return SIGROK_ERR;
-       close(tmpfile);
-       make_metadata(metafile);
-       if (!(src = zip_source_file(zipfile, metafile, 0, -1)))
-               return SIGROK_ERR;
-       if (zip_add(zipfile, "metadata", src) == -1)
-               return SIGROK_ERR;
-       unlink(metafile);
-
-       /* Raw */
-       devcnt = 1;
-       for (l = session->devices; l; l = l->next) {
-               device = l->data;
-               ds = device->datastore;
-               if (ds) {
-                       buf = malloc(ds->num_units * ds->ds_unitsize +
-                                  DATASTORE_CHUNKSIZE);
-                       bufcnt = 0;
-                       for (d = ds->chunklist; d; d = d->next) {
-                               memcpy(buf + bufcnt, d->data,
-                                      DATASTORE_CHUNKSIZE);
-                               bufcnt += DATASTORE_CHUNKSIZE;
-                       }
-                       if (!(src = zip_source_buffer(zipfile, buf,
-                                      ds->num_units * ds->ds_unitsize, TRUE)))
-                               return SIGROK_ERR;
-                       snprintf(rawname, 15, "raw-%d", devcnt);
-                       if (zip_add(zipfile, rawname, src) == -1)
-                               return SIGROK_ERR;
-               }
-               devcnt++;
-       }
-
-       if ((ret = zip_close(zipfile)) == -1) {
-               g_message("error saving zipfile: %s", zip_strerror(zipfile));
-               return SIGROK_ERR;
-       }
-
-       return SIGROK_OK;
 }
+