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