]> sigrok.org Git - libsigrok.git/blame - output/output_ols.c
LA8: Use the new SR_ERR_ARG macro.
[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/*
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>
31#include <sigrok.h>
32#include "config.h"
33
34struct context {
6e738600 35 GString *header;
6e738600 36 uint64_t num_samples;
ab224f7b 37 unsigned int unitsize;
ab224f7b
UH
38};
39
6e738600
BV
40static int init(struct sr_output *o)
41{
42 struct context *ctx;
1afe8989 43 struct sr_probe *probe;
6e738600 44 GSList *l;
305bde4d 45 uint64_t samplerate;
4487177c 46 int num_enabled_probes;
ab224f7b 47
6e738600
BV
48 if (!(ctx = g_malloc(sizeof(struct context))))
49 return SR_ERR_MALLOC;
6e738600 50 o->internal = ctx;
305bde4d 51
6e738600 52 ctx->num_samples = 0;
6e738600
BV
53 num_enabled_probes = 0;
54 for (l = o->device->probes; l; l = l->next) {
55 probe = l->data;
56 if (probe->enabled)
57 num_enabled_probes++;
ab224f7b 58 }
6e738600 59 ctx->unitsize = (num_enabled_probes + 7) / 8;
ab224f7b 60
305bde4d
BV
61 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE))
62 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
63 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
64 else
65 samplerate = 0;
66
67 ctx->header = g_string_sized_new(512);
68 g_string_append_printf(ctx->header, ";Rate: %"PRIu64"\n", samplerate);
69 g_string_append_printf(ctx->header, ";Channels: %d\n", num_enabled_probes);
70 g_string_append_printf(ctx->header, ";EnabledChannels: -1\n");
71 g_string_append_printf(ctx->header, ";Compressed: true\n");
72 g_string_append_printf(ctx->header, ";CursorEnabled: false\n");
305bde4d 73
6e738600 74 return SR_OK;
ab224f7b
UH
75}
76
f50f3f40 77static int event(struct sr_output *o, int event_type, char **data_out,
ab224f7b
UH
78 uint64_t *length_out)
79{
80 struct context *ctx;
81
82 ctx = o->internal;
6e738600 83
305bde4d
BV
84 if (ctx && event_type == SR_DF_END) {
85 if (ctx->header)
86 g_string_free(ctx->header, TRUE);
ab224f7b
UH
87 free(o->internal);
88 o->internal = NULL;
ab224f7b
UH
89 }
90
305bde4d
BV
91 *data_out = NULL;
92 *length_out = 0;
93
e46b8fb1 94 return SR_OK;
ab224f7b
UH
95}
96
54ac5277 97static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
ab224f7b
UH
98 char **data_out, uint64_t *length_out)
99{
305bde4d 100 GString *out;
ab224f7b 101 struct context *ctx;
ab224f7b 102 uint64_t sample;
6e738600 103 unsigned int i;
ab224f7b
UH
104
105 ctx = o->internal;
305bde4d
BV
106 if (ctx->header) {
107 /* first data packet */
108 out = ctx->header;
109 ctx->header = NULL;
110 } else
111 out = g_string_sized_new(512);
112
ab224f7b 113 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
6e738600 114 sample = 0;
ab224f7b 115 memcpy(&sample, data_in + i, ctx->unitsize);
305bde4d 116 g_string_append_printf(out, "%08x@%"PRIu64"\n",
6e738600 117 (uint32_t) sample, ctx->num_samples++);
ab224f7b 118 }
305bde4d
BV
119 *data_out = out->str;
120 *length_out = out->len;
121 g_string_free(out, FALSE);
ab224f7b 122
e46b8fb1 123 return SR_OK;
ab224f7b
UH
124}
125
f50f3f40 126struct sr_output_format output_ols = {
d494a4aa
UH
127 .extension = "ols",
128 .description = "OpenBench Logic Sniffer",
129 .df_type = SR_DF_LOGIC,
130 .init = init,
131 .data = data,
132 .event = event,
ab224f7b 133};