]> sigrok.org Git - libsigrok.git/blame - input/input_binary.c
get_sr_device_instance() -> sr_get_device_instance().
[libsigrok.git] / input / input_binary.c
CommitLineData
34e4813f
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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
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>
34e4813f
BV
26#include <sigrok.h>
27
13a12913
BV
28#define CHUNKSIZE 4096
29#define DEFAULT_NUM_PROBES 8
30
34e4813f 31
757b8c62 32static int format_match(const char *filename)
34e4813f 33{
13a12913
BV
34
35 /* suppress compiler warning */
34e4813f 36 filename = NULL;
13a12913
BV
37
38 /* this module will handle anything you throw at it */
34e4813f
BV
39 return TRUE;
40}
41
f50f3f40 42static int init(struct sr_input *in)
13a12913
BV
43{
44 int num_probes;
45
46 if (in->param && in->param[0]) {
47 num_probes = strtoul(in->param, NULL, 10);
48 if (num_probes < 1)
e46b8fb1 49 return SR_ERR;
13a12913
BV
50 } else
51 num_probes = DEFAULT_NUM_PROBES;
52
53 /* create a virtual device */
6ea7e235 54 in->vdevice = device_new(NULL, 0, num_probes);
13a12913 55
e46b8fb1 56 return SR_OK;
13a12913
BV
57}
58
f50f3f40 59static int loadfile(struct sr_input *in, const char *filename)
34e4813f
BV
60{
61 struct datafeed_header header;
62 struct datafeed_packet packet;
63 char buffer[CHUNKSIZE];
db91a1c3 64 int fd, size, num_probes;
34e4813f 65
757b8c62 66 if ((fd = open(filename, O_RDONLY)) == -1)
e46b8fb1 67 return SR_ERR;
34e4813f 68
13a12913 69 num_probes = g_slist_length(in->vdevice->probes);
db91a1c3 70
13a12913 71 /* send header */
34e4813f 72 header.feed_version = 1;
c2616fb9 73 header.num_logic_probes = num_probes;
13a12913 74 header.num_analog_probes = 0;
34e4813f
BV
75 header.protocol_id = PROTO_RAW;
76 header.samplerate = 0;
77 gettimeofday(&header.starttime, NULL);
78 packet.type = DF_HEADER;
79 packet.length = sizeof(struct datafeed_header);
80 packet.payload = &header;
13a12913 81 session_bus(in->vdevice, &packet);
34e4813f 82
13a12913 83 /* chop up the input file into chunks and feed it into the session bus */
4c046c6b 84 packet.type = DF_LOGIC;
13a12913 85 packet.unitsize = (num_probes + 7) / 8;
34e4813f 86 packet.payload = buffer;
757b8c62 87 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
34e4813f 88 packet.length = size;
13a12913 89 session_bus(in->vdevice, &packet);
34e4813f
BV
90 }
91 close(fd);
92
13a12913 93 /* end of stream */
34e4813f 94 packet.type = DF_END;
db91a1c3 95 packet.length = 0;
13a12913 96 session_bus(in->vdevice, &packet);
34e4813f 97
e46b8fb1 98 return SR_OK;
34e4813f
BV
99}
100
f50f3f40 101struct sr_input_format input_binary = {
757b8c62
UH
102 "binary",
103 "Raw binary",
104 format_match,
13a12913
BV
105 init,
106 loadfile,
34e4813f 107};