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