]> sigrok.org Git - libsigrok.git/blame - output/output_ols.c
demo: forgot second part of samplerate support
[libsigrok.git] / output / output_ols.c
CommitLineData
ab224f7b
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
6e738600 5 * Copyright (C) 2011 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/*
23 * Output format for the OpenBench Logic Sniffer "Alternative" Java client.
24 * Details: https://github.com/jawi/ols/wiki/OLS-data-file-format
25 */
26
27#include <stdlib.h>
28#include <string.h>
29#include <glib.h>
30#include <sigrok.h>
31#include "config.h"
32
33struct context {
6e738600
BV
34 GString *header;
35 GString *body;
36 uint64_t num_samples;
37 gboolean got_trigger;
38 uint64_t trigger_pos;
ab224f7b 39 unsigned int unitsize;
ab224f7b
UH
40};
41
ab224f7b 42
6e738600 43static void make_header(struct sr_output *o)
ab224f7b
UH
44{
45 struct context *ctx;
ab224f7b 46 uint64_t samplerate;
6e738600 47 int i;
ab224f7b 48
6e738600 49 ctx = o->internal;
ab224f7b 50
2bf4aca6 51 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE))
ab224f7b 52 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
5a2326a7 53 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
6e738600
BV
54 else
55 samplerate = 0;
56
57 g_string_append_printf(ctx->header, ";Size: %"PRIu64"\n", ctx->num_samples);
58 g_string_append_printf(ctx->header, ";Rate: %"PRIu64"\n", samplerate);
59 g_string_append_printf(ctx->header, ";Channels: %d\n", ctx->unitsize*8);
60 g_string_append_printf(ctx->header, ";EnabledChannels: -1\n");
61 if (ctx->got_trigger)
62 g_string_append_printf(ctx->header, ";TriggerPosition: %"PRIu64"\n", ctx->trigger_pos);
63 g_string_append_printf(ctx->header, ";Compressed: true\n");
64 g_string_append_printf(ctx->header, ";AbsoluteLength: %"PRIu64"\n", ctx->num_samples);
65 g_string_append_printf(ctx->header, ";CursorEnabled: false\n");
66 for (i = 0; i < 10; i++)
67 g_string_append_printf(ctx->header, ";Cursor%d: 0\n", i);
ab224f7b 68
6e738600 69}
ab224f7b 70
6e738600
BV
71static int init(struct sr_output *o)
72{
73 struct context *ctx;
1afe8989 74 struct sr_probe *probe;
6e738600
BV
75 GSList *l;
76 int num_enabled_probes;
ab224f7b 77
6e738600
BV
78 if (!(ctx = g_malloc(sizeof(struct context))))
79 return SR_ERR_MALLOC;
ab224f7b 80
6e738600
BV
81 o->internal = ctx;
82 ctx->header = g_string_sized_new(512);
83 ctx->body = g_string_sized_new(512);
84 ctx->num_samples = 0;
85 ctx->got_trigger = FALSE;
86 ctx->trigger_pos = 0;
87 num_enabled_probes = 0;
88 for (l = o->device->probes; l; l = l->next) {
89 probe = l->data;
90 if (probe->enabled)
91 num_enabled_probes++;
ab224f7b 92 }
6e738600 93 ctx->unitsize = (num_enabled_probes + 7) / 8;
ab224f7b 94
6e738600 95 return SR_OK;
ab224f7b
UH
96}
97
f50f3f40 98static int event(struct sr_output *o, int event_type, char **data_out,
ab224f7b
UH
99 uint64_t *length_out)
100{
101 struct context *ctx;
102
103 ctx = o->internal;
6e738600
BV
104 *data_out = NULL;
105 *length_out = 0;
106
ab224f7b 107 switch (event_type) {
5a2326a7 108 case SR_DF_TRIGGER:
6e738600
BV
109 ctx->got_trigger = TRUE;
110 ctx->trigger_pos = ctx->num_samples;
ab224f7b 111 break;
5a2326a7 112 case SR_DF_END:
6e738600
BV
113 make_header(o);
114 *length_out = ctx->header->len + ctx->body->len;
115 *data_out = g_malloc(*length_out);
116 memcpy(*data_out, ctx->header->str, ctx->header->len);
117 memcpy(*data_out + ctx->header->len, ctx->body->str, ctx->body->len);
118 g_string_free(ctx->header, TRUE);
119 g_string_free(ctx->body, TRUE);
ab224f7b
UH
120 free(o->internal);
121 o->internal = NULL;
122 break;
123 }
124
e46b8fb1 125 return SR_OK;
ab224f7b
UH
126}
127
f50f3f40 128static int data(struct sr_output *o, char *data_in, uint64_t length_in,
ab224f7b
UH
129 char **data_out, uint64_t *length_out)
130{
131 struct context *ctx;
ab224f7b 132 uint64_t sample;
6e738600 133 unsigned int i;
ab224f7b
UH
134
135 ctx = o->internal;
ab224f7b 136 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
6e738600 137 sample = 0;
ab224f7b 138 memcpy(&sample, data_in + i, ctx->unitsize);
6e738600
BV
139 g_string_append_printf(ctx->body, "%08x@%"PRIu64"\n",
140 (uint32_t) sample, ctx->num_samples++);
ab224f7b
UH
141 }
142
6e738600
BV
143 *data_out = NULL;
144 *length_out = 0;
ab224f7b 145
e46b8fb1 146 return SR_OK;
ab224f7b
UH
147}
148
f50f3f40 149struct sr_output_format output_ols = {
ab224f7b
UH
150 "ols",
151 "OpenBench Logic Sniffer",
5a2326a7 152 SR_DF_LOGIC,
ab224f7b
UH
153 init,
154 data,
155 event,
156};