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