libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
ols.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
00005  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00020  */
00021 
00022 /*
00023  * This implements version 1.3 of the output format for the OpenBench Logic
00024  * Sniffer "Alternative" Java client. Details:
00025  * https://github.com/jawi/ols/wiki/OLS-data-file-format
00026  */
00027 
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <glib.h>
00031 #include "sigrok.h"
00032 #include "sigrok-internal.h"
00033 
00034 struct context {
00035         GString *header;
00036         uint64_t num_samples;
00037         unsigned int unitsize;
00038 };
00039 
00040 static int init(struct sr_output *o)
00041 {
00042         struct context *ctx;
00043         struct sr_probe *probe;
00044         GSList *l;
00045         uint64_t samplerate;
00046         int num_enabled_probes;
00047 
00048         if (!(ctx = g_try_malloc(sizeof(struct context)))) {
00049                 sr_err("ols out: %s: ctx malloc failed", __func__);
00050                 return SR_ERR_MALLOC;
00051         }
00052         o->internal = ctx;
00053 
00054         ctx->num_samples = 0;
00055         num_enabled_probes = 0;
00056         for (l = o->dev->probes; l; l = l->next) {
00057                 probe = l->data;
00058                 if (probe->enabled)
00059                         num_enabled_probes++;
00060         }
00061         ctx->unitsize = (num_enabled_probes + 7) / 8;
00062 
00063         if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE))
00064                 samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
00065                                 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
00066         else
00067                 samplerate = 0;
00068 
00069         ctx->header = g_string_sized_new(512);
00070         g_string_append_printf(ctx->header, ";Rate: %"PRIu64"\n", samplerate);
00071         g_string_append_printf(ctx->header, ";Channels: %d\n", num_enabled_probes);
00072         g_string_append_printf(ctx->header, ";EnabledChannels: -1\n");
00073         g_string_append_printf(ctx->header, ";Compressed: true\n");
00074         g_string_append_printf(ctx->header, ";CursorEnabled: false\n");
00075 
00076         return SR_OK;
00077 }
00078 
00079 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
00080                  uint64_t *length_out)
00081 {
00082         struct context *ctx;
00083 
00084         ctx = o->internal;
00085 
00086         if (ctx && event_type == SR_DF_END) {
00087                 g_string_free(ctx->header, TRUE);
00088                 g_free(o->internal);
00089                 o->internal = NULL;
00090         }
00091 
00092         *data_out = NULL;
00093         *length_out = 0;
00094 
00095         return SR_OK;
00096 }
00097 
00098 static int data(struct sr_output *o, const uint8_t *data_in,
00099                 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
00100 {
00101         GString *out;
00102         struct context *ctx;
00103         uint64_t sample;
00104         unsigned int i;
00105 
00106         ctx = o->internal;
00107         if (ctx->header) {
00108                 /* first data packet */
00109                 out = ctx->header;
00110                 ctx->header = NULL;
00111         } else
00112                 out = g_string_sized_new(512);
00113 
00114         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
00115                 sample = 0;
00116                 memcpy(&sample, data_in + i, ctx->unitsize);
00117                 g_string_append_printf(out, "%08x@%"PRIu64"\n",
00118                                 (uint32_t) sample, ctx->num_samples++);
00119         }
00120         *data_out = (uint8_t *)out->str;
00121         *length_out = out->len;
00122         g_string_free(out, FALSE);
00123 
00124         return SR_OK;
00125 }
00126 
00127 SR_PRIV struct sr_output_format output_ols = {
00128         .id = "ols",
00129         .description = "OpenBench Logic Sniffer",
00130         .df_type = SR_DF_LOGIC,
00131         .init = init,
00132         .data = data,
00133         .event = event,
00134 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines