X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=session.c;h=e08b39b2f6157fbfe9e899be779cfc05d3f5826d;hb=b9c735a275512ce82da7f66275c7cb62eaf66b60;hp=604d4b74d04ea5b9bc26006b7a18aa4f136321c6;hpb=a1bb33afbde769156ad4bef7a60579da64aebbb7;p=libsigrok.git diff --git a/session.c b/session.c index 604d4b74..e08b39b2 100644 --- a/session.c +++ b/session.c @@ -22,263 +22,358 @@ #include #include #include -#include "sigrok.h" +#include +#include +#include -/* there can only be one session at a time */ + +/* 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(char *filename) + + +struct session *session_load(const char *filename) { struct session *session; - /* TODO: implement */ + /* Avoid compiler warnings. */ + filename = filename; + + /* TODO: Implement. */ session = NULL; return session; } - struct session *session_new(void) { - session = calloc(1, sizeof(struct session)); return session; } - void 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) { - g_slist_free(session->devices); session->devices = NULL; - } - int session_device_add(struct 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->open) { + ret = device->plugin->open(device->plugin_index); + if (ret != SR_OK) + return ret; + } + + session->devices = g_slist_append(session->devices, device); - return ret; + return SR_OK; } - void session_pa_clear(void) { - - /* the protocols are pointers to the global set of PA plugins, so don't free them */ + /* + * The protocols are pointers to the global set of PA plugins, + * so don't free them. + */ g_slist_free(session->analyzers); session->analyzers = NULL; - } - void session_pa_add(struct analyzer *an) { - session->analyzers = g_slist_append(session->analyzers, an); - } - void session_datafeed_callback_clear(void) { - g_slist_free(session->datafeed_callbacks); session->datafeed_callbacks = NULL; - } - void session_datafeed_callback_add(datafeed_callback callback) { - - session->datafeed_callbacks = g_slist_append(session->datafeed_callbacks, 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 acquisition"); - for(l = session->devices; l; l = l->next) - { + 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) + if ((ret = device->plugin->start_acquisition( + device->plugin_index, device)) != SR_OK) break; } return ret; } - -void session_stop(void) +void session_run(void) { - struct device *device; - GSList *l; + 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 + } - g_message("stopping acquisition"); - for(l = session->devices; l; l = l->next) - { - device = l->data; - device->plugin->stop_acquisition(device->plugin_index, device); + 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_bus(struct device *device, struct datafeed_packet *packet) +void session_halt(void) { - GSList *l; - datafeed_callback cb; - - /* TODO: send packet through PA pipe, and send the output of that to - * the callbacks as well - */ - for(l = session->datafeed_callbacks; l; l = l->next) - { - cb = l->data; - cb(device, packet); - } + g_message("halting session"); + session->running = FALSE; } - -void make_metadata(char *filename) +void session_stop(void) { - GSList *l, *p; struct device *device; - struct probe *probe; - FILE *f; - int devcnt; - - f = fopen(filename, "wb"); - - /* general */ + GSList *l; - /* devices */ - devcnt = 1; - for(l = session->devices; l; l = l->next) { + g_message("stopping session"); + session->running = FALSE; + 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 (device->plugin) + device->plugin->stop_acquisition(device->plugin_index, device); } - /* TODO: protocol analyzers */ +} - fclose(f); +void session_bus(struct device *device, struct sr_datafeed_packet *packet) +{ + GSList *l; + datafeed_callback cb; + /* + * 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) { + cb = l->data; + cb(device, packet); + } } - int session_save(char *filename) { - GSList *l, *d; + GSList *l, *p, *d; + FILE *meta; struct device *device; + struct probe *probe; struct datastore *ds; struct zip *zipfile; - struct zip_source *src; + struct zip_source *versrc, *metasrc, *logicsrc; int bufcnt, devcnt, tmpfile, ret, error; - char version[1], rawname[16], metafile[32], *buf; + char version[1], rawname[16], metafile[32], *newfn, *buf; - /* quietly delete it first, libzip wants replace ops otherwise */ - unlink(filename); + newfn = g_malloc(strlen(filename) + 10); + strcpy(newfn, filename); + if (strstr(filename, ".sigrok") != filename+strlen(filename)-7) + strcat(newfn, ".sigrok"); - if( !(zipfile = zip_open(filename, ZIP_CREATE, &error)) ) - return SIGROK_NOK; + /* 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" */ version[0] = '1'; - if( !(src = zip_source_buffer(zipfile, version, 1, 0)) ) - return SIGROK_NOK; - if(zip_add(zipfile, "version", src) == -1) { - g_message("error saving version into zipfile: %s", zip_strerror(zipfile)); - return SIGROK_NOK; + 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; } - /* metadata */ + /* init "metadata" */ strcpy(metafile, "sigrok-meta-XXXXXX"); - if( (tmpfile = g_mkstemp(metafile)) == -1) - return SIGROK_NOK; + if ((tmpfile = g_mkstemp(metafile)) == -1) + return SR_ERR; close(tmpfile); - make_metadata(metafile); - if( !(src = zip_source_file(zipfile, metafile, 0, -1)) ) - return SIGROK_NOK; - if(zip_add(zipfile, "metadata", src) == -1) - return SIGROK_NOK; - unlink(metafile); + meta = fopen(metafile, "wb"); + fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION); + /* TODO: save protocol decoders used */ - /* raw */ + /* all datastores in all devices */ devcnt = 1; - for(l = session->devices; l; l = l->next) { + 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) { - buf = malloc(ds->num_units * ds->ds_unitsize + DATASTORE_CHUNKSIZE); + 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); + 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_NOK; - snprintf(rawname, 15, "raw-%d", devcnt); - if(zip_add(zipfile, rawname, src) == -1) - return SIGROK_NOK; + 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) { + if ((ret = zip_close(zipfile)) == -1) { g_message("error saving zipfile: %s", zip_strerror(zipfile)); - return SIGROK_NOK; + return SR_ERR; } - return SIGROK_OK; -} - + unlink(metafile); + 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); + } +}