]>
Commit | Line | Data |
---|---|---|
34e4813f BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 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 | |
8ff6afc9 | 29 | #define CHUNKSIZE (512 * 1024) |
13a12913 BV |
30 | #define DEFAULT_NUM_PROBES 8 |
31 | ||
c506a6a6 | 32 | struct context { |
0d1297a2 | 33 | uint64_t samplerate; |
c506a6a6 TŠ |
34 | }; |
35 | ||
757b8c62 | 36 | static int format_match(const char *filename) |
34e4813f | 37 | { |
13a12913 | 38 | /* suppress compiler warning */ |
719c5a93 | 39 | (void)filename; |
13a12913 BV |
40 | |
41 | /* this module will handle anything you throw at it */ | |
34e4813f BV |
42 | return TRUE; |
43 | } | |
44 | ||
f50f3f40 | 45 | static int init(struct sr_input *in) |
13a12913 | 46 | { |
5c3c1241 | 47 | struct sr_probe *probe; |
464d12c7 | 48 | int num_probes, i; |
c37d2b1b | 49 | char name[SR_MAX_PROBENAME_LEN + 1]; |
c506a6a6 TŠ |
50 | char *param; |
51 | struct context *ctx; | |
52 | ||
53 | if (!(ctx = g_try_malloc0(sizeof(*ctx)))) { | |
54 | sr_err("binary in: %s: ctx malloc failed", __func__); | |
55 | return SR_ERR_MALLOC; | |
56 | } | |
13a12913 | 57 | |
c506a6a6 TŠ |
58 | num_probes = DEFAULT_NUM_PROBES; |
59 | ctx->samplerate = 0; | |
60 | ||
61 | if(in->param) { | |
62 | param = g_hash_table_lookup(in->param, "numprobes"); | |
63 | if (param) { | |
64 | num_probes = strtoul(param, NULL, 10); | |
65 | if (num_probes < 1) | |
66 | return SR_ERR; | |
67 | } | |
68 | ||
69 | param = g_hash_table_lookup(in->param, "samplerate"); | |
70 | if (param) { | |
0d1297a2 | 71 | if (sr_parse_sizestring(param, &ctx->samplerate) != SR_OK) |
c506a6a6 | 72 | return SR_ERR; |
c506a6a6 | 73 | } |
c37d2b1b | 74 | } |
13a12913 | 75 | |
c37d2b1b | 76 | /* Create a virtual device. */ |
5c3c1241 | 77 | in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); |
c506a6a6 | 78 | in->internal = ctx; |
464d12c7 | 79 | |
c37d2b1b | 80 | for (i = 0; i < num_probes; i++) { |
464d12c7 | 81 | snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i); |
c37d2b1b | 82 | /* TODO: Check return value. */ |
5c3c1241 BV |
83 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name))) |
84 | return SR_ERR; | |
85 | in->sdi->probes = g_slist_append(in->sdi->probes, probe); | |
464d12c7 | 86 | } |
13a12913 | 87 | |
e46b8fb1 | 88 | return SR_OK; |
13a12913 BV |
89 | } |
90 | ||
f50f3f40 | 91 | static int loadfile(struct sr_input *in, const char *filename) |
34e4813f | 92 | { |
b9c735a2 UH |
93 | struct sr_datafeed_header header; |
94 | struct sr_datafeed_packet packet; | |
f366e86c | 95 | struct sr_datafeed_meta_logic meta; |
9c939c51 BV |
96 | struct sr_datafeed_logic logic; |
97 | unsigned char buffer[CHUNKSIZE]; | |
db91a1c3 | 98 | int fd, size, num_probes; |
c506a6a6 TŠ |
99 | struct context *ctx; |
100 | ||
101 | ctx = in->internal; | |
34e4813f | 102 | |
757b8c62 | 103 | if ((fd = open(filename, O_RDONLY)) == -1) |
e46b8fb1 | 104 | return SR_ERR; |
34e4813f | 105 | |
5c3c1241 | 106 | num_probes = g_slist_length(in->sdi->probes); |
db91a1c3 | 107 | |
13a12913 | 108 | /* send header */ |
34e4813f | 109 | header.feed_version = 1; |
34e4813f | 110 | gettimeofday(&header.starttime, NULL); |
5a2326a7 | 111 | packet.type = SR_DF_HEADER; |
34e4813f | 112 | packet.payload = &header; |
5c3c1241 | 113 | sr_session_send(in->sdi, &packet); |
34e4813f | 114 | |
f366e86c BV |
115 | /* Send metadata about the SR_DF_LOGIC packets to come. */ |
116 | packet.type = SR_DF_META_LOGIC; | |
117 | packet.payload = &meta; | |
c506a6a6 | 118 | meta.samplerate = ctx->samplerate; |
f366e86c | 119 | meta.num_probes = num_probes; |
5c3c1241 | 120 | sr_session_send(in->sdi, &packet); |
f366e86c | 121 | |
13a12913 | 122 | /* chop up the input file into chunks and feed it into the session bus */ |
5a2326a7 | 123 | packet.type = SR_DF_LOGIC; |
9c939c51 BV |
124 | packet.payload = &logic; |
125 | logic.unitsize = (num_probes + 7) / 8; | |
126 | logic.data = buffer; | |
757b8c62 | 127 | while ((size = read(fd, buffer, CHUNKSIZE)) > 0) { |
9c939c51 | 128 | logic.length = size; |
5c3c1241 | 129 | sr_session_send(in->sdi, &packet); |
34e4813f BV |
130 | } |
131 | close(fd); | |
132 | ||
13a12913 | 133 | /* end of stream */ |
5a2326a7 | 134 | packet.type = SR_DF_END; |
5c3c1241 | 135 | sr_session_send(in->sdi, &packet); |
34e4813f | 136 | |
c506a6a6 TŠ |
137 | g_free(ctx); |
138 | in->internal = NULL; | |
139 | ||
e46b8fb1 | 140 | return SR_OK; |
34e4813f BV |
141 | } |
142 | ||
7c1d391c | 143 | SR_PRIV struct sr_input_format input_binary = { |
cdb3573c | 144 | .id = "binary", |
d494a4aa UH |
145 | .description = "Raw binary", |
146 | .format_match = format_match, | |
147 | .init = init, | |
148 | .loadfile = loadfile, | |
34e4813f | 149 | }; |