]> sigrok.org Git - libsigrok.git/blame_incremental - output/ols.c
Centralise duplicated logging helper defines.
[libsigrok.git] / output / ols.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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 * 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
26 */
27
28#include <stdlib.h>
29#include <string.h>
30#include <glib.h>
31#include "libsigrok.h"
32#include "libsigrok-internal.h"
33
34#define LOG_PREFIX "output/ols"
35
36struct context {
37 uint64_t samplerate;
38 uint64_t num_samples;
39};
40
41static int init(struct sr_output *o)
42{
43 struct context *ctx;
44
45 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
46 sr_err("%s: ctx malloc failed", __func__);
47 return SR_ERR_MALLOC;
48 }
49 o->internal = ctx;
50
51 ctx->samplerate = 0;
52 ctx->num_samples = 0;
53
54 return SR_OK;
55}
56
57static GString *gen_header(const struct sr_dev_inst *sdi, struct context *ctx)
58{
59 struct sr_probe *probe;
60 GSList *l;
61 GString *s;
62 GVariant *gvar;
63 int num_enabled_probes;
64
65 if (!ctx->samplerate && sr_config_get(sdi->driver, sdi, NULL,
66 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
67 ctx->samplerate = g_variant_get_uint64(gvar);
68 g_variant_unref(gvar);
69 }
70
71 num_enabled_probes = 0;
72 for (l = sdi->probes; l; l = l->next) {
73 probe = l->data;
74 if (probe->enabled)
75 num_enabled_probes++;
76 }
77
78 s = g_string_sized_new(512);
79 g_string_append_printf(s, ";Rate: %"PRIu64"\n", ctx->samplerate);
80 g_string_append_printf(s, ";Channels: %d\n", num_enabled_probes);
81 g_string_append_printf(s, ";EnabledChannels: -1\n");
82 g_string_append_printf(s, ";Compressed: true\n");
83 g_string_append_printf(s, ";CursorEnabled: false\n");
84
85 return s;
86}
87
88static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
89 const struct sr_datafeed_packet *packet, GString **out)
90{
91 struct context *ctx;
92 const struct sr_datafeed_meta *meta;
93 const struct sr_datafeed_logic *logic;
94 const struct sr_config *src;
95 GSList *l;
96 unsigned int i, j;
97 uint8_t c;
98
99 *out = NULL;
100 if (!o || !o->sdi)
101 return SR_ERR_ARG;
102 ctx = o->internal;
103
104 switch (packet->type) {
105 case SR_DF_META:
106 meta = packet->payload;
107 for (l = meta->config; l; l = l->next) {
108 src = l->data;
109 if (src->key == SR_CONF_SAMPLERATE)
110 ctx->samplerate = g_variant_get_uint64(src->data);
111 }
112 break;
113 case SR_DF_LOGIC:
114 logic = packet->payload;
115 if (ctx->num_samples == 0) {
116 /* First logic packet in the feed. */
117 *out = gen_header(sdi, ctx);
118 } else
119 *out = g_string_sized_new(512);
120 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
121 for (j = 0; j < logic->unitsize; j++) {
122 /* The OLS format wants the samples presented MSB first. */
123 c = *((uint8_t *)logic->data + i + logic->unitsize - 1 - j);
124 g_string_append_printf(*out, "%02x", c);
125 }
126 g_string_append_printf(*out, "@%"PRIu64"\n", ctx->num_samples++);
127 }
128 break;
129 }
130
131 return SR_OK;
132}
133
134static int cleanup(struct sr_output *o)
135{
136 struct context *ctx;
137
138 if (!o || !o->sdi)
139 return SR_ERR_ARG;
140
141 ctx = o->internal;
142 g_free(ctx);
143 o->internal = NULL;
144
145 return SR_OK;
146}
147
148SR_PRIV struct sr_output_format output_ols = {
149 .id = "ols",
150 .description = "OpenBench Logic Sniffer",
151 .df_type = SR_DF_LOGIC,
152 .init = init,
153 .receive = receive,
154 .cleanup = cleanup
155};