]> sigrok.org Git - libsigrok.git/blobdiff - session.c
libsigrok: closedev() now has a return code.
[libsigrok.git] / session.c
index e08b39b2f6157fbfe9e899be779cfc05d3f5826d..b80ab7d59f489ff4bcd3e87918a2e3454b2ee806 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 <config.h>
+#include <sigrok-internal.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;
+/* demo.c */
+extern GIOChannel channels[2];
 
 struct source {
        int fd;
        int events;
        int timeout;
-       receive_data_callback cb;
+       sr_receive_data_callback cb;
        void *user_data;
 };
 
+/* There can only be one session at a time. */
+struct sr_session *session;
+int num_sources = 0;
+
 struct source *sources = NULL;
 int source_timeout = -1;
 
 
-
-
-struct session *session_load(const char *filename)
-{
-       struct session *session;
-
-       /* Avoid compiler warnings. */
-       filename = filename;
-
-       /* TODO: Implement. */
-       session = NULL;
-
-       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);
 
@@ -77,18 +61,18 @@ void session_destroy(void)
        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;
 
-       if (device->plugin && device->plugin->open) {
-               ret = device->plugin->open(device->plugin_index);
+       if (device->plugin && device->plugin->opendev) {
+               ret = device->plugin->opendev(device->plugin_index);
                if (ret != SR_OK)
                        return ret;
        }
@@ -98,7 +82,8 @@ int session_device_add(struct device *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,
@@ -108,47 +93,29 @@ 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)
-{
-       struct device *device;
-       GSList *l;
-       int ret;
-
-       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)) != SR_OK)
-                       break;
-       }
-
-       return ret;
-}
-
-void session_run(void)
+static void sr_session_run_poll()
 {
        GPollFD *fds, my_gpollfd;
        int ret, i;
 
-       g_message("running session");
-       session->running = TRUE;
        fds = NULL;
        while (session->running) {
                if (fds)
@@ -178,7 +145,7 @@ void session_run(void)
                                 * asked for that timeout.
                                 */
                                sources[i].cb(fds[i].fd, fds[i].revents,
-                                             sources[i].user_data);
+                                                 sources[i].user_data);
                        }
                }
        }
@@ -186,33 +153,67 @@ void session_run(void)
 
 }
 
-void session_halt(void)
+int sr_session_start(void)
+{
+       struct sr_device *device;
+       GSList *l;
+       int ret;
+
+       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)) != SR_OK)
+                       break;
+       }
+
+       return ret;
+}
+
+void sr_session_run(void)
 {
 
-       g_message("halting session");
+       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 session_stop(void)
+void sr_session_stop(void)
 {
-       struct device *device;
+       struct sr_device *device;
        GSList *l;
 
-       g_message("stopping session");
+       sr_info("session: stopping");
        session->running = FALSE;
        for (l = session->devices; l; l = l->next) {
                device = l->data;
-               if (device->plugin)
+               if (device->plugin && device->plugin->stop_acquisition)
                        device->plugin->stop_acquisition(device->plugin_index, device);
        }
 
 }
 
-void session_bus(struct device *device, struct sr_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 PD pipe, and send the output of that to
@@ -224,111 +225,8 @@ void session_bus(struct device *device, struct sr_datafeed_packet *packet)
        }
 }
 
-int session_save(char *filename)
-{
-       GSList *l, *p, *d;
-       FILE *meta;
-       struct device *device;
-       struct probe *probe;
-       struct datastore *ds;
-       struct zip *zipfile;
-       struct zip_source *versrc, *metasrc, *logicsrc;
-       int bufcnt, devcnt, tmpfile, ret, error;
-       char version[1], rawname[16], metafile[32], *newfn, *buf;
-
-       newfn = g_malloc(strlen(filename) + 10);
-       strcpy(newfn, filename);
-       if (strstr(filename, ".sigrok") != filename+strlen(filename)-7)
-               strcat(newfn, ".sigrok");
-
-       /* Quietly delete it first, libzip wants replace ops otherwise. */
-       unlink(newfn);
-       if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error)))
-               return SR_ERR;
-       g_free(newfn);
-
-       /* "version" */
-       version[0] = '1';
-       if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
-               return SR_ERR;
-       if (zip_add(zipfile, "version", versrc) == -1) {
-               g_message("error saving version into zipfile: %s",
-                         zip_strerror(zipfile));
-               return SR_ERR;
-       }
-
-       /* init "metadata" */
-       strcpy(metafile, "sigrok-meta-XXXXXX");
-       if ((tmpfile = g_mkstemp(metafile)) == -1)
-               return SR_ERR;
-       close(tmpfile);
-       meta = fopen(metafile, "wb");
-       fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
-       /* TODO: save protocol decoders used */
-
-       /* all datastores in all devices */
-       devcnt = 1;
-       for (l = session->devices; l; l = l->next) {
-               device = l->data;
-               /* metadata */
-               fprintf(meta, "[device]\n");
-               if (device->plugin)
-                       fprintf(meta, "driver = %s\n", device->plugin->name);
-
-               ds = device->datastore;
-               if (ds) {
-                       /* metadata */
-                       fprintf(meta, "capturefile = logic-%d\n", devcnt);
-                       for (p = device->probes; p; p = p->next) {
-                               probe = p->data;
-                               if (probe->enabled) {
-                                       fprintf(meta, "probe %d", probe->index);
-                                       if (probe->name)
-                                               fprintf(meta, " name \"%s\"", probe->name);
-                                       if (probe->trigger)
-                                               fprintf(meta, " trigger \"%s\"",
-                                                       probe->trigger);
-                                       fprintf(meta, "\n");
-                               }
-                       }
-
-                       /* dump datastore into logic-n */
-                       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 (!(logicsrc = zip_source_buffer(zipfile, buf,
-                                      ds->num_units * ds->ds_unitsize, TRUE)))
-                               return SR_ERR;
-                       snprintf(rawname, 15, "logic-%d", devcnt);
-                       if (zip_add(zipfile, rawname, logicsrc) == -1)
-                               return SR_ERR;
-               }
-               devcnt++;
-       }
-       fclose(meta);
-
-       if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
-               return SR_ERR;
-       if (zip_add(zipfile, "metadata", metasrc) == -1)
-               return SR_ERR;
-
-       if ((ret = zip_close(zipfile)) == -1) {
-               g_message("error saving zipfile: %s", zip_strerror(zipfile));
-               return SR_ERR;
-       }
-
-       unlink(metafile);
-
-       return SR_OK;
-}
-
-void session_source_add(int fd, int events, int timeout,
-               receive_data_callback callback, void *user_data)
+void sr_session_source_add(int fd, int events, int timeout,
+               sr_receive_data_callback callback, void *user_data)
 {
        struct source *new_sources, *s;
 
@@ -353,7 +251,7 @@ void session_source_add(int fd, int events, int timeout,
                source_timeout = timeout;
 }
 
-void session_source_remove(int fd)
+void sr_session_source_remove(int fd)
 {
        struct source *new_sources;
        int old, new;