]> sigrok.org Git - libsigrok.git/blobdiff - session.c
Make more variables/functions static and non-global.
[libsigrok.git] / session.c
index 65f7a8df2785bf03f53d03960010e62ed8ca47aa..8f8edcce0eddd1c91f178b46f42e2ec7b63c9948 100644 (file)
--- a/session.c
+++ b/session.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <glib.h>
 #include <sigrok.h>
+#include <sigrok-internal.h>
 
 /* demo.c */
 extern GIOChannel channels[2];
@@ -37,12 +38,12 @@ struct source {
 };
 
 /* There can only be one session at a time. */
+/* 'session' is not static, it's used elsewhere (via 'extern'). */
 struct sr_session *session;
-int num_sources = 0;
-
-struct source *sources = NULL;
-int source_timeout = -1;
+static int num_sources = 0;
 
+static struct source *sources = NULL;
+static int source_timeout = -1;
 
 struct sr_session *sr_session_new(void)
 {
@@ -70,8 +71,8 @@ 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;
        }
@@ -81,6 +82,7 @@ int sr_session_device_add(struct sr_device *device)
        return SR_OK;
 }
 
+#if 0
 void sr_session_pa_clear(void)
 {
        /*
@@ -91,10 +93,11 @@ void sr_session_pa_clear(void)
        session->analyzers = NULL;
 }
 
-void sr_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 sr_session_datafeed_callback_clear(void)
 {
@@ -141,13 +144,13 @@ static void sr_session_run_poll()
                                 * 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);
+                               if (!sources[i].cb(fds[i].fd, fds[i].revents,
+                                                 sources[i].user_data))
+                                       sr_session_source_remove(sources[i].fd);
                        }
                }
        }
        free(fds);
-
 }
 
 int sr_session_start(void)
@@ -156,7 +159,7 @@ int sr_session_start(void)
        GSList *l;
        int ret;
 
-       g_message("session: starting");
+       sr_info("session: starting");
        for (l = session->devices; l; l = l->next) {
                device = l->data;
                if ((ret = device->plugin->start_acquisition(
@@ -169,8 +172,7 @@ int sr_session_start(void)
 
 void sr_session_run(void)
 {
-
-       g_message("session: running");
+       sr_info("session: running");
        session->running = TRUE;
 
        /* do we have real sources? */
@@ -181,15 +183,12 @@ void sr_session_run(void)
        else
                /* real sources, use g_poll() main loop */
                sr_session_run_poll();
-
 }
 
 void sr_session_halt(void)
 {
-
-       g_message("session: halting");
+       sr_info("session: halting");
        session->running = FALSE;
-
 }
 
 void sr_session_stop(void)
@@ -197,14 +196,39 @@ void sr_session_stop(void)
        struct sr_device *device;
        GSList *l;
 
-       g_message("session: stopping");
+       sr_info("session: stopping");
        session->running = FALSE;
        for (l = session->devices; l; l = l->next) {
                device = l->data;
                if (device->plugin && device->plugin->stop_acquisition)
                        device->plugin->stop_acquisition(device->plugin_index, device);
        }
+}
 
+static void datafeed_dump(struct sr_datafeed_packet *packet)
+{
+       struct sr_datafeed_logic *logic;
+
+       switch (packet->type) {
+       case SR_DF_HEADER:
+               sr_dbg("bus: received SR_DF_HEADER");
+               break;
+       case SR_DF_TRIGGER:
+               sr_dbg("bus: received SR_DF_TRIGGER at %lu ms",
+                               packet->timeoffset / 1000000);
+               break;
+       case SR_DF_LOGIC:
+               logic = packet->payload;
+               sr_dbg("bus: received SR_DF_LOGIC at %f ms duration %f ms, %"PRIu64" bytes",
+                               packet->timeoffset / 1000000.0, packet->duration / 1000000.0,
+                               logic->length);
+               break;
+       case SR_DF_END:
+               sr_dbg("bus: received SR_DF_END");
+               break;
+       default:
+               sr_dbg("bus: received unknown packet type %d", packet->type);
+       }
 }
 
 void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
@@ -218,6 +242,7 @@ void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
         */
        for (l = session->datafeed_callbacks; l; l = l->next) {
                cb = l->data;
+               datafeed_dump(packet);
                cb(device, packet);
        }
 }
@@ -257,7 +282,7 @@ void sr_session_source_remove(int fd)
                return;
 
        new_sources = calloc(1, sizeof(struct source) * num_sources);
-       for (old = 0; old < num_sources; old++)
+       for (old = 0, new = 0; old < num_sources; old++)
                if (sources[old].fd != fd)
                        memcpy(&new_sources[new++], &sources[old],
                               sizeof(struct source));
@@ -271,4 +296,3 @@ void sr_session_source_remove(int fd)
                free(new_sources);
        }
 }
-