]> sigrok.org Git - libsigrok.git/blob - output/text/text.c
Centralise duplicated logging helper defines.
[libsigrok.git] / output / text / text.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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>
25 #include "config.h" /* Needed for PACKAGE_STRING and others. */
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "text.h"
29
30 #define LOG_PREFIX "output/text"
31
32 SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf)
33 {
34         static int max_probename_len = 0;
35         int len, i;
36         GSList *l;
37         char *probe_name;
38
39         if (ctx->linebuf[0] == 0)
40                 return;
41
42         if (max_probename_len == 0) {
43                 /* First time through... */
44                 for (l = ctx->probenames; l; l = l->next) {
45                         probe_name = l->data;
46                         len = strlen(probe_name);
47                         if (len > max_probename_len)
48                                 max_probename_len = len;
49                 }
50         }
51
52         for (i = 0, l = ctx->probenames; l; l = l->next, i++) {
53                 probe_name = l->data;
54                 sprintf((char *)outbuf + strlen((const char *)outbuf),
55                         "%*s:%s\n", max_probename_len,
56                         probe_name, ctx->linebuf + i * ctx->linebuf_len);
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
67                 sprintf((char *)outbuf + strlen((const char *)outbuf),
68                         "T:%*s^\n", ctx->mark_trigger + space_offset, "");
69         }
70
71         memset(ctx->linebuf, 0, i * ctx->linebuf_len);
72 }
73
74 SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
75 {
76         struct context *ctx;
77         struct sr_probe *probe;
78         GSList *l;
79         GVariant *gvar;
80         uint64_t samplerate;
81         int num_probes, ret;
82         char *samplerate_s;
83
84         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
85                 sr_err("%s: ctx malloc failed", __func__);
86                 return SR_ERR_MALLOC;
87         }
88
89         o->internal = ctx;
90         ctx->num_enabled_probes = 0;
91         ctx->probenames = NULL;
92
93         for (l = o->sdi->probes; l; l = l->next) {
94                 probe = l->data;
95                 if (!probe->enabled)
96                         continue;
97                 ctx->probenames = g_slist_append(ctx->probenames, probe->name);
98                 ctx->num_enabled_probes++;
99         }
100
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
107         ret = SR_OK;
108         if (o->param && o->param[0]) {
109                 ctx->samples_per_line = strtoul(o->param, NULL, 10);
110                 if (ctx->samples_per_line < 1) {
111                         ret = SR_ERR;
112                         goto err;
113                 }
114         } else
115                 ctx->samples_per_line = default_spl;
116
117         if (!(ctx->header = g_try_malloc0(512))) {
118                 sr_err("%s: ctx->header malloc failed", __func__);
119                 ret = SR_ERR_MALLOC;
120                 goto err;
121         }
122
123         snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
124         num_probes = g_slist_length(o->sdi->probes);
125         if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
126                         &gvar) == SR_OK) {
127                 samplerate = g_variant_get_uint64(gvar);
128                 g_variant_unref(gvar);
129                 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
130                         ret = SR_ERR;
131                         goto err;
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);
137                 g_free(samplerate_s);
138         }
139
140         ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
141         if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
142                 sr_err("%s: ctx->linebuf malloc failed", __func__);
143                 ret = SR_ERR_MALLOC;
144                 goto err;
145         }
146
147         if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
148                 sr_err("%s: ctx->linevalues malloc failed", __func__);
149                 ret = SR_ERR_MALLOC;
150         }
151
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
158 err:
159         if (ret != SR_OK) {
160                 g_free(ctx->header);
161                 g_free(ctx);
162         }
163
164         return ret;
165 }
166
167 SR_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
192 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
193                   uint64_t *length_out)
194 {
195         struct context *ctx;
196         int outsize;
197         uint8_t *outbuf;
198
199         ctx = o->internal;
200         switch (event_type) {
201         case SR_DF_TRIGGER:
202                 ctx->mark_trigger = ctx->spl_cnt;
203                 *data_out = NULL;
204                 *length_out = 0;
205                 break;
206         case SR_DF_END:
207                 outsize = ctx->num_enabled_probes
208                                 * (ctx->samples_per_line + 20) + 512;
209                 if (!(outbuf = g_try_malloc0(outsize))) {
210                         sr_err("%s: outbuf malloc failed", __func__);
211                         return SR_ERR_MALLOC;
212                 }
213                 flush_linebufs(ctx, outbuf);
214                 *data_out = outbuf;
215                 *length_out = strlen((const char *)outbuf);
216                 break;
217         default:
218                 *data_out = NULL;
219                 *length_out = 0;
220                 break;
221         }
222
223         return SR_OK;
224 }