]>
Commit | Line | Data |
---|---|---|
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 | 32 | SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf) |
97554432 | 33 | { |
ba7dd8bb | 34 | static int max_channelname_len = 0; |
97554432 | 35 | int len, i; |
d53e4e8d | 36 | GSList *l; |
ba7dd8bb | 37 | char *channel_name; |
97554432 BV |
38 | |
39 | if (ctx->linebuf[0] == 0) | |
40 | return; | |
41 | ||
ba7dd8bb | 42 | if (max_channelname_len == 0) { |
97554432 | 43 | /* First time through... */ |
ba7dd8bb UH |
44 | for (l = ctx->channelnames; l; l = l->next) { |
45 | channel_name = l->data; | |
46 | len = strlen(channel_name); | |
47 | if (len > max_channelname_len) | |
48 | max_channelname_len = len; | |
97554432 BV |
49 | } |
50 | } | |
51 | ||
ba7dd8bb UH |
52 | for (i = 0, l = ctx->channelnames; l; l = l->next, i++) { |
53 | channel_name = l->data; | |
054e6709 | 54 | sprintf((char *)outbuf + strlen((const char *)outbuf), |
ba7dd8bb UH |
55 | "%*s:%s\n", max_channelname_len, |
56 | channel_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 | 74 | SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode) |
97554432 BV |
75 | { |
76 | struct context *ctx; | |
ba7dd8bb | 77 | struct sr_channel *ch; |
97554432 | 78 | GSList *l; |
ec4063b8 BV |
79 | GVariant *gvar; |
80 | uint64_t samplerate; | |
ba7dd8bb | 81 | int num_channels, 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; | |
ba7dd8bb UH |
90 | ctx->num_enabled_channels = 0; |
91 | ctx->channelnames = NULL; | |
97554432 | 92 | |
ba7dd8bb UH |
93 | for (l = o->sdi->channels; l; l = l->next) { |
94 | ch = l->data; | |
3f239f08 | 95 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 96 | continue; |
ba7dd8bb | 97 | if (!ch->enabled) |
97554432 | 98 | continue; |
ba7dd8bb UH |
99 | ctx->channelnames = g_slist_append(ctx->channelnames, ch->name); |
100 | ctx->num_enabled_channels++; | |
97554432 BV |
101 | } |
102 | ||
ba7dd8bb | 103 | ctx->unitsize = (ctx->num_enabled_channels + 7) / 8; |
97554432 BV |
104 | ctx->line_offset = 0; |
105 | ctx->spl_cnt = 0; | |
106 | ctx->mark_trigger = -1; | |
107 | ctx->mode = mode; | |
108 | ||
aee878fa | 109 | ret = SR_OK; |
97554432 BV |
110 | if (o->param && o->param[0]) { |
111 | ctx->samples_per_line = strtoul(o->param, NULL, 10); | |
5c3c1241 BV |
112 | if (ctx->samples_per_line < 1) { |
113 | ret = SR_ERR; | |
114 | goto err; | |
115 | } | |
97554432 BV |
116 | } else |
117 | ctx->samples_per_line = default_spl; | |
118 | ||
133a37bf | 119 | if (!(ctx->header = g_try_malloc0(512))) { |
a944a84b | 120 | sr_err("%s: ctx->header malloc failed", __func__); |
5c3c1241 BV |
121 | ret = SR_ERR_MALLOC; |
122 | goto err; | |
97554432 BV |
123 | } |
124 | ||
125 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); | |
ba7dd8bb | 126 | num_channels = g_slist_length(o->sdi->channels); |
d3c74a6f BV |
127 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, |
128 | &gvar) == SR_OK) { | |
ec4063b8 | 129 | samplerate = g_variant_get_uint64(gvar); |
af51a771 | 130 | g_variant_unref(gvar); |
ec4063b8 | 131 | if (!(samplerate_s = sr_samplerate_string(samplerate))) { |
5c3c1241 BV |
132 | ret = SR_ERR; |
133 | goto err; | |
97554432 BV |
134 | } |
135 | snprintf(ctx->header + strlen(ctx->header), | |
136 | 511 - strlen(ctx->header), | |
ba7dd8bb UH |
137 | "Acquisition with %d/%d channels at %s\n", |
138 | ctx->num_enabled_channels, num_channels, samplerate_s); | |
133a37bf | 139 | g_free(samplerate_s); |
97554432 BV |
140 | } |
141 | ||
142 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; | |
ba7dd8bb | 143 | if (!(ctx->linebuf = g_try_malloc0(num_channels * ctx->linebuf_len))) { |
a944a84b | 144 | sr_err("%s: ctx->linebuf malloc failed", __func__); |
5c3c1241 BV |
145 | ret = SR_ERR_MALLOC; |
146 | goto err; | |
97554432 | 147 | } |
5c3c1241 | 148 | |
ba7dd8bb | 149 | if (!(ctx->linevalues = g_try_malloc0(num_channels))) { |
a944a84b | 150 | sr_err("%s: ctx->linevalues malloc failed", __func__); |
5c3c1241 BV |
151 | ret = SR_ERR_MALLOC; |
152 | } | |
153 | ||
3a581560 | 154 | if (mode == MODE_ASCII && |
ba7dd8bb | 155 | !(ctx->prevsample = g_try_malloc0(num_channels / 8))) { |
3a581560 ML |
156 | sr_err("%s: ctx->prevsample malloc failed", __func__); |
157 | ret = SR_ERR_MALLOC; | |
158 | } | |
159 | ||
5c3c1241 BV |
160 | err: |
161 | if (ret != SR_OK) { | |
133a37bf UH |
162 | g_free(ctx->header); |
163 | g_free(ctx); | |
97554432 BV |
164 | } |
165 | ||
5c3c1241 | 166 | return ret; |
97554432 BV |
167 | } |
168 | ||
8c273ac5 DJ |
169 | SR_PRIV int text_cleanup(struct sr_output *o) |
170 | { | |
171 | struct context *ctx; | |
172 | ||
173 | if (!o) | |
174 | return SR_ERR_ARG; | |
175 | ||
176 | ctx = o->internal; | |
177 | ||
178 | g_free(ctx->header); | |
179 | g_free(ctx->linebuf); | |
180 | g_free(ctx->linevalues); | |
181 | ||
182 | if (ctx->prevsample) | |
183 | g_free(ctx->prevsample); | |
184 | ||
ba7dd8bb | 185 | g_slist_free(ctx->channelnames); |
8c273ac5 DJ |
186 | |
187 | g_free(ctx); | |
188 | ||
189 | o->internal = NULL; | |
190 | ||
191 | return SR_OK; | |
192 | } | |
193 | ||
054e6709 | 194 | SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out, |
7c1d391c | 195 | uint64_t *length_out) |
97554432 BV |
196 | { |
197 | struct context *ctx; | |
198 | int outsize; | |
054e6709 | 199 | uint8_t *outbuf; |
97554432 BV |
200 | |
201 | ctx = o->internal; | |
202 | switch (event_type) { | |
5a2326a7 | 203 | case SR_DF_TRIGGER: |
97554432 BV |
204 | ctx->mark_trigger = ctx->spl_cnt; |
205 | *data_out = NULL; | |
206 | *length_out = 0; | |
207 | break; | |
5a2326a7 | 208 | case SR_DF_END: |
ba7dd8bb | 209 | outsize = ctx->num_enabled_channels |
97554432 | 210 | * (ctx->samples_per_line + 20) + 512; |
133a37bf | 211 | if (!(outbuf = g_try_malloc0(outsize))) { |
a944a84b | 212 | sr_err("%s: outbuf malloc failed", __func__); |
e46b8fb1 | 213 | return SR_ERR_MALLOC; |
133a37bf | 214 | } |
97554432 BV |
215 | flush_linebufs(ctx, outbuf); |
216 | *data_out = outbuf; | |
054e6709 | 217 | *length_out = strlen((const char *)outbuf); |
97554432 BV |
218 | break; |
219 | default: | |
220 | *data_out = NULL; | |
221 | *length_out = 0; | |
222 | break; | |
223 | } | |
224 | ||
e46b8fb1 | 225 | return SR_OK; |
97554432 | 226 | } |