]> sigrok.org Git - libsigrok.git/blame - input/binary.c
Bump copyright year
[libsigrok.git] / input / binary.c
CommitLineData
34e4813f
BV
1/*
2 * This file is part of the sigrok project.
3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
34e4813f
BV
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
13a12913 20#include <stdlib.h>
34e4813f
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <sys/time.h>
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
34e4813f 28
8e7f1cfd
UH
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
8ff6afc9 38#define CHUNKSIZE (512 * 1024)
13a12913
BV
39#define DEFAULT_NUM_PROBES 8
40
c506a6a6 41struct context {
0d1297a2 42 uint64_t samplerate;
c506a6a6
43};
44
757b8c62 45static int format_match(const char *filename)
34e4813f 46{
719c5a93 47 (void)filename;
13a12913 48
8e7f1cfd 49 /* This module will handle anything you throw at it. */
34e4813f
BV
50 return TRUE;
51}
52
543d45c5 53static int init(struct sr_input *in, const char *filename)
13a12913 54{
5c3c1241 55 struct sr_probe *probe;
464d12c7 56 int num_probes, i;
c37d2b1b 57 char name[SR_MAX_PROBENAME_LEN + 1];
c506a6a6
58 char *param;
59 struct context *ctx;
60
543d45c5
BV
61 (void)filename;
62
c506a6a6 63 if (!(ctx = g_try_malloc0(sizeof(*ctx)))) {
8e7f1cfd 64 sr_err("Input format context malloc failed.");
c506a6a6
65 return SR_ERR_MALLOC;
66 }
13a12913 67
c506a6a6
68 num_probes = DEFAULT_NUM_PROBES;
69 ctx->samplerate = 0;
70
8e7f1cfd 71 if (in->param) {
c506a6a6
72 param = g_hash_table_lookup(in->param, "numprobes");
73 if (param) {
74 num_probes = strtoul(param, NULL, 10);
75 if (num_probes < 1)
76 return SR_ERR;
77 }
78
79 param = g_hash_table_lookup(in->param, "samplerate");
80 if (param) {
0d1297a2 81 if (sr_parse_sizestring(param, &ctx->samplerate) != SR_OK)
c506a6a6 82 return SR_ERR;
c506a6a6 83 }
c37d2b1b 84 }
13a12913 85
c37d2b1b 86 /* Create a virtual device. */
5c3c1241 87 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
c506a6a6 88 in->internal = ctx;
464d12c7 89
c37d2b1b 90 for (i = 0; i < num_probes; i++) {
464d12c7 91 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
c37d2b1b 92 /* TODO: Check return value. */
5c3c1241
BV
93 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
94 return SR_ERR;
95 in->sdi->probes = g_slist_append(in->sdi->probes, probe);
464d12c7 96 }
13a12913 97
e46b8fb1 98 return SR_OK;
13a12913
BV
99}
100
f50f3f40 101static int loadfile(struct sr_input *in, const char *filename)
34e4813f 102{
b9c735a2 103 struct sr_datafeed_packet packet;
2df1e819 104 struct sr_datafeed_meta meta;
9c939c51 105 struct sr_datafeed_logic logic;
2df1e819 106 struct sr_config *src;
9c939c51 107 unsigned char buffer[CHUNKSIZE];
db91a1c3 108 int fd, size, num_probes;
c506a6a6
109 struct context *ctx;
110
111 ctx = in->internal;
34e4813f 112
757b8c62 113 if ((fd = open(filename, O_RDONLY)) == -1)
e46b8fb1 114 return SR_ERR;
34e4813f 115
5c3c1241 116 num_probes = g_slist_length(in->sdi->probes);
db91a1c3 117
8e7f1cfd 118 /* Send header packet to the session bus. */
4afdfd46 119 std_session_send_df_header(in->sdi, DRIVER_LOG_DOMAIN);
34e4813f 120
2df1e819
BV
121 if (ctx->samplerate) {
122 packet.type = SR_DF_META;
123 packet.payload = &meta;
3ebce226 124 src = sr_config_new(SR_CONF_SAMPLERATE, (const void *)&ctx->samplerate);
2df1e819
BV
125 meta.config = g_slist_append(NULL, src);
126 sr_session_send(in->sdi, &packet);
127 }
f366e86c 128
8e7f1cfd 129 /* Chop up the input file into chunks & send it to the session bus. */
5a2326a7 130 packet.type = SR_DF_LOGIC;
9c939c51
BV
131 packet.payload = &logic;
132 logic.unitsize = (num_probes + 7) / 8;
133 logic.data = buffer;
757b8c62 134 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
9c939c51 135 logic.length = size;
5c3c1241 136 sr_session_send(in->sdi, &packet);
34e4813f
BV
137 }
138 close(fd);
139
8e7f1cfd 140 /* Send end packet to the session bus. */
5a2326a7 141 packet.type = SR_DF_END;
5c3c1241 142 sr_session_send(in->sdi, &packet);
34e4813f 143
c506a6a6
144 g_free(ctx);
145 in->internal = NULL;
146
e46b8fb1 147 return SR_OK;
34e4813f
BV
148}
149
7c1d391c 150SR_PRIV struct sr_input_format input_binary = {
cdb3573c 151 .id = "binary",
d494a4aa
UH
152 .description = "Raw binary",
153 .format_match = format_match,
154 .init = init,
155 .loadfile = loadfile,
34e4813f 156};