]> sigrok.org Git - libsigrok.git/blame - output/text/text.c
Centralise duplicated logging helper defines.
[libsigrok.git] / output / text / text.c
CommitLineData
97554432 1/*
50985c20 2 * This file is part of the libsigrok project.
97554432 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
97554432
BV
5 * Copyright (C) 2011 Håvard Espeland <gus@ping.uio.no>
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <glib.h>
545f9786 25#include "config.h" /* Needed for PACKAGE_STRING and others. */
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
97554432
BV
28#include "text.h"
29
3544f848 30#define LOG_PREFIX "output/text"
a944a84b 31
054e6709 32SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf)
97554432
BV
33{
34 static int max_probename_len = 0;
35 int len, i;
d53e4e8d
ML
36 GSList *l;
37 char *probe_name;
97554432
BV
38
39 if (ctx->linebuf[0] == 0)
40 return;
41
42 if (max_probename_len == 0) {
43 /* First time through... */
d53e4e8d
ML
44 for (l = ctx->probenames; l; l = l->next) {
45 probe_name = l->data;
46 len = strlen(probe_name);
97554432
BV
47 if (len > max_probename_len)
48 max_probename_len = len;
49 }
50 }
51
d53e4e8d
ML
52 for (i = 0, l = ctx->probenames; l; l = l->next, i++) {
53 probe_name = l->data;
054e6709
UH
54 sprintf((char *)outbuf + strlen((const char *)outbuf),
55 "%*s:%s\n", max_probename_len,
d53e4e8d 56 probe_name, ctx->linebuf + i * ctx->linebuf_len);
97554432
BV
57 }
58
59 /* Mark trigger with a ^ character. */
60 if (ctx->mark_trigger != -1)
61 {
62 int space_offset = ctx->mark_trigger / 8;
63
64 if (ctx->mode == MODE_ASCII)
65 space_offset = 0;
66
054e6709
UH
67 sprintf((char *)outbuf + strlen((const char *)outbuf),
68 "T:%*s^\n", ctx->mark_trigger + space_offset, "");
97554432
BV
69 }
70
71 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
72}
73
7c1d391c 74SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
97554432
BV
75{
76 struct context *ctx;
1afe8989 77 struct sr_probe *probe;
97554432 78 GSList *l;
ec4063b8
BV
79 GVariant *gvar;
80 uint64_t samplerate;
5c3c1241 81 int num_probes, ret;
97554432
BV
82 char *samplerate_s;
83
133a37bf 84 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 85 sr_err("%s: ctx malloc failed", __func__);
e46b8fb1 86 return SR_ERR_MALLOC;
133a37bf 87 }
97554432
BV
88
89 o->internal = ctx;
90 ctx->num_enabled_probes = 0;
d53e4e8d 91 ctx->probenames = NULL;
97554432 92
5c3c1241 93 for (l = o->sdi->probes; l; l = l->next) {
97554432
BV
94 probe = l->data;
95 if (!probe->enabled)
96 continue;
d53e4e8d
ML
97 ctx->probenames = g_slist_append(ctx->probenames, probe->name);
98 ctx->num_enabled_probes++;
97554432
BV
99 }
100
97554432
BV
101 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
102 ctx->line_offset = 0;
103 ctx->spl_cnt = 0;
104 ctx->mark_trigger = -1;
105 ctx->mode = mode;
106
aee878fa 107 ret = SR_OK;
97554432
BV
108 if (o->param && o->param[0]) {
109 ctx->samples_per_line = strtoul(o->param, NULL, 10);
5c3c1241
BV
110 if (ctx->samples_per_line < 1) {
111 ret = SR_ERR;
112 goto err;
113 }
97554432
BV
114 } else
115 ctx->samples_per_line = default_spl;
116
133a37bf 117 if (!(ctx->header = g_try_malloc0(512))) {
a944a84b 118 sr_err("%s: ctx->header malloc failed", __func__);
5c3c1241
BV
119 ret = SR_ERR_MALLOC;
120 goto err;
97554432
BV
121 }
122
123 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
5c3c1241 124 num_probes = g_slist_length(o->sdi->probes);
d3c74a6f
BV
125 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
126 &gvar) == SR_OK) {
ec4063b8 127 samplerate = g_variant_get_uint64(gvar);
af51a771 128 g_variant_unref(gvar);
ec4063b8 129 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
5c3c1241
BV
130 ret = SR_ERR;
131 goto err;
97554432
BV
132 }
133 snprintf(ctx->header + strlen(ctx->header),
134 511 - strlen(ctx->header),
135 "Acquisition with %d/%d probes at %s\n",
136 ctx->num_enabled_probes, num_probes, samplerate_s);
133a37bf 137 g_free(samplerate_s);
97554432
BV
138 }
139
140 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
133a37bf 141 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
a944a84b 142 sr_err("%s: ctx->linebuf malloc failed", __func__);
5c3c1241
BV
143 ret = SR_ERR_MALLOC;
144 goto err;
97554432 145 }
5c3c1241 146
133a37bf 147 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
a944a84b 148 sr_err("%s: ctx->linevalues malloc failed", __func__);
5c3c1241
BV
149 ret = SR_ERR_MALLOC;
150 }
151
3a581560
ML
152 if (mode == MODE_ASCII &&
153 !(ctx->prevsample = g_try_malloc0(num_probes / 8))) {
154 sr_err("%s: ctx->prevsample malloc failed", __func__);
155 ret = SR_ERR_MALLOC;
156 }
157
5c3c1241
BV
158err:
159 if (ret != SR_OK) {
133a37bf
UH
160 g_free(ctx->header);
161 g_free(ctx);
97554432
BV
162 }
163
5c3c1241 164 return ret;
97554432
BV
165}
166
8c273ac5
DJ
167SR_PRIV int text_cleanup(struct sr_output *o)
168{
169 struct context *ctx;
170
171 if (!o)
172 return SR_ERR_ARG;
173
174 ctx = o->internal;
175
176 g_free(ctx->header);
177 g_free(ctx->linebuf);
178 g_free(ctx->linevalues);
179
180 if (ctx->prevsample)
181 g_free(ctx->prevsample);
182
183 g_slist_free(ctx->probenames);
184
185 g_free(ctx);
186
187 o->internal = NULL;
188
189 return SR_OK;
190}
191
054e6709 192SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
7c1d391c 193 uint64_t *length_out)
97554432
BV
194{
195 struct context *ctx;
196 int outsize;
054e6709 197 uint8_t *outbuf;
97554432
BV
198
199 ctx = o->internal;
200 switch (event_type) {
5a2326a7 201 case SR_DF_TRIGGER:
97554432
BV
202 ctx->mark_trigger = ctx->spl_cnt;
203 *data_out = NULL;
204 *length_out = 0;
205 break;
5a2326a7 206 case SR_DF_END:
97554432
BV
207 outsize = ctx->num_enabled_probes
208 * (ctx->samples_per_line + 20) + 512;
133a37bf 209 if (!(outbuf = g_try_malloc0(outsize))) {
a944a84b 210 sr_err("%s: outbuf malloc failed", __func__);
e46b8fb1 211 return SR_ERR_MALLOC;
133a37bf 212 }
97554432
BV
213 flush_linebufs(ctx, outbuf);
214 *data_out = outbuf;
054e6709 215 *length_out = strlen((const char *)outbuf);
97554432
BV
216 break;
217 default:
218 *data_out = NULL;
219 *length_out = 0;
220 break;
221 }
222
e46b8fb1 223 return SR_OK;
97554432 224}