]> sigrok.org Git - libsigrok.git/blame - output/ols.c
GPL headers: Use correct project name.
[libsigrok.git] / output / ols.c
CommitLineData
ab224f7b 1/*
50985c20 2 * This file is part of the libsigrok project.
ab224f7b
UH
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
13d8e03c 5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
ab224f7b
UH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/*
305bde4d
BV
23 * This implements version 1.3 of the output format for the OpenBench Logic
24 * Sniffer "Alternative" Java client. Details:
25 * https://github.com/jawi/ols/wiki/OLS-data-file-format
ab224f7b
UH
26 */
27
28#include <stdlib.h>
29#include <string.h>
30#include <glib.h>
45c59c8b
BV
31#include "libsigrok.h"
32#include "libsigrok-internal.h"
ab224f7b 33
a944a84b
UH
34/* Message logging helpers with driver-specific prefix string. */
35#define DRIVER_LOG_DOMAIN "output/ols: "
36#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
37#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
38#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
39#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
40#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
41#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
42
ab224f7b 43struct context {
6e738600 44 GString *header;
6e738600 45 uint64_t num_samples;
ab224f7b 46 unsigned int unitsize;
ab224f7b
UH
47};
48
6e738600
BV
49static int init(struct sr_output *o)
50{
51 struct context *ctx;
1afe8989 52 struct sr_probe *probe;
6e738600 53 GSList *l;
ec4063b8
BV
54 GVariant *gvar;
55 uint64_t samplerate;
4487177c 56 int num_enabled_probes;
ab224f7b 57
b53738ba 58 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
a944a84b 59 sr_err("%s: ctx malloc failed", __func__);
6e738600 60 return SR_ERR_MALLOC;
b53738ba 61 }
6e738600 62 o->internal = ctx;
305bde4d 63
6e738600 64 ctx->num_samples = 0;
6e738600 65 num_enabled_probes = 0;
5c3c1241 66 for (l = o->sdi->probes; l; l = l->next) {
6e738600
BV
67 probe = l->data;
68 if (probe->enabled)
69 num_enabled_probes++;
ab224f7b 70 }
6e738600 71 ctx->unitsize = (num_enabled_probes + 7) / 8;
ab224f7b 72
ec4063b8
BV
73 if (o->sdi->driver && sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
74 o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi);
75 samplerate = g_variant_get_uint64(gvar);
76 g_variant_unref(gvar);
77 } else {
78 samplerate = 0;
5c3c1241 79 }
305bde4d
BV
80
81 ctx->header = g_string_sized_new(512);
ec4063b8 82 g_string_append_printf(ctx->header, ";Rate: %"PRIu64"\n", samplerate);
305bde4d
BV
83 g_string_append_printf(ctx->header, ";Channels: %d\n", num_enabled_probes);
84 g_string_append_printf(ctx->header, ";EnabledChannels: -1\n");
85 g_string_append_printf(ctx->header, ";Compressed: true\n");
86 g_string_append_printf(ctx->header, ";CursorEnabled: false\n");
305bde4d 87
6e738600 88 return SR_OK;
ab224f7b
UH
89}
90
054e6709 91static int event(struct sr_output *o, int event_type, uint8_t **data_out,
ab224f7b
UH
92 uint64_t *length_out)
93{
94 struct context *ctx;
95
96 ctx = o->internal;
6e738600 97
305bde4d 98 if (ctx && event_type == SR_DF_END) {
66410a86 99 g_string_free(ctx->header, TRUE);
133a37bf 100 g_free(o->internal);
ab224f7b 101 o->internal = NULL;
ab224f7b
UH
102 }
103
305bde4d
BV
104 *data_out = NULL;
105 *length_out = 0;
106
e46b8fb1 107 return SR_OK;
ab224f7b
UH
108}
109
054e6709
UH
110static int data(struct sr_output *o, const uint8_t *data_in,
111 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
ab224f7b 112{
305bde4d 113 GString *out;
ab224f7b 114 struct context *ctx;
ab224f7b 115 uint64_t sample;
6e738600 116 unsigned int i;
ab224f7b
UH
117
118 ctx = o->internal;
305bde4d
BV
119 if (ctx->header) {
120 /* first data packet */
121 out = ctx->header;
122 ctx->header = NULL;
123 } else
124 out = g_string_sized_new(512);
125
ab224f7b 126 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
6e738600 127 sample = 0;
ab224f7b 128 memcpy(&sample, data_in + i, ctx->unitsize);
305bde4d 129 g_string_append_printf(out, "%08x@%"PRIu64"\n",
6e738600 130 (uint32_t) sample, ctx->num_samples++);
ab224f7b 131 }
054e6709 132 *data_out = (uint8_t *)out->str;
305bde4d
BV
133 *length_out = out->len;
134 g_string_free(out, FALSE);
ab224f7b 135
e46b8fb1 136 return SR_OK;
ab224f7b
UH
137}
138
7c1d391c 139SR_PRIV struct sr_output_format output_ols = {
cdb3573c 140 .id = "ols",
d494a4aa
UH
141 .description = "OpenBench Logic Sniffer",
142 .df_type = SR_DF_LOGIC,
143 .init = init,
144 .data = data,
145 .event = event,
ab224f7b 146};