]> sigrok.org Git - libsigrok.git/blob - input/binary.c
Use new SR_DF_META packet.
[libsigrok.git] / input / binary.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/time.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28
29 /* Message logging helpers with driver-specific prefix string. */
30 #define DRIVER_LOG_DOMAIN "input/binary: "
31 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
36 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
37
38 #define CHUNKSIZE             (512 * 1024)
39 #define DEFAULT_NUM_PROBES    8
40
41 struct context {
42         uint64_t samplerate;
43 };
44
45 static int format_match(const char *filename)
46 {
47         (void)filename;
48
49         /* This module will handle anything you throw at it. */
50         return TRUE;
51 }
52
53 static int init(struct sr_input *in)
54 {
55         struct sr_probe *probe;
56         int num_probes, i;
57         char name[SR_MAX_PROBENAME_LEN + 1];
58         char *param;
59         struct context *ctx;
60
61         if (!(ctx = g_try_malloc0(sizeof(*ctx)))) {
62                 sr_err("Input format context malloc failed.");
63                 return SR_ERR_MALLOC;
64         }
65
66         num_probes = DEFAULT_NUM_PROBES;
67         ctx->samplerate = 0;
68
69         if (in->param) {
70                 param = g_hash_table_lookup(in->param, "numprobes");
71                 if (param) {
72                         num_probes = strtoul(param, NULL, 10);
73                         if (num_probes < 1)
74                                 return SR_ERR;
75                 }
76
77                 param = g_hash_table_lookup(in->param, "samplerate");
78                 if (param) {
79                         if (sr_parse_sizestring(param, &ctx->samplerate) != SR_OK)
80                                 return SR_ERR;
81                 }
82         }
83
84         /* Create a virtual device. */
85         in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
86         in->internal = ctx;
87
88         for (i = 0; i < num_probes; i++) {
89                 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
90                 /* TODO: Check return value. */
91                 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
92                         return SR_ERR;
93                 in->sdi->probes = g_slist_append(in->sdi->probes, probe);
94         }
95
96         return SR_OK;
97 }
98
99 static int loadfile(struct sr_input *in, const char *filename)
100 {
101         struct sr_datafeed_header header;
102         struct sr_datafeed_packet packet;
103         struct sr_datafeed_meta meta;
104         struct sr_datafeed_logic logic;
105         struct sr_config *src;
106         unsigned char buffer[CHUNKSIZE];
107         int fd, size, num_probes;
108         struct context *ctx;
109
110         ctx = in->internal;
111
112         if ((fd = open(filename, O_RDONLY)) == -1)
113                 return SR_ERR;
114
115         num_probes = g_slist_length(in->sdi->probes);
116
117         /* Send header packet to the session bus. */
118         header.feed_version = 1;
119         gettimeofday(&header.starttime, NULL);
120         packet.type = SR_DF_HEADER;
121         packet.payload = &header;
122         sr_session_send(in->sdi, &packet);
123
124         if (ctx->samplerate) {
125                 packet.type = SR_DF_META;
126                 packet.payload = &meta;
127                 src = sr_config_make(SR_HWCAP_SAMPLERATE, (const void *)&ctx->samplerate);
128                 meta.config = g_slist_append(NULL, src);
129                 sr_session_send(in->sdi, &packet);
130         }
131
132         /* Chop up the input file into chunks & send it to the session bus. */
133         packet.type = SR_DF_LOGIC;
134         packet.payload = &logic;
135         logic.unitsize = (num_probes + 7) / 8;
136         logic.data = buffer;
137         while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
138                 logic.length = size;
139                 sr_session_send(in->sdi, &packet);
140         }
141         close(fd);
142
143         /* Send end packet to the session bus. */
144         packet.type = SR_DF_END;
145         sr_session_send(in->sdi, &packet);
146
147         g_free(ctx);
148         in->internal = NULL;
149
150         return SR_OK;
151 }
152
153 SR_PRIV struct sr_input_format input_binary = {
154         .id = "binary",
155         .description = "Raw binary",
156         .format_match = format_match,
157         .init = init,
158         .loadfile = loadfile,
159 };