]> sigrok.org Git - libsigrok.git/blob - input/input_binary.c
d9a53cb66c3f63a7876c1e02c68d846623a69f1f
[libsigrok.git] / input / input_binary.c
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
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 <sigrok.h>
27
28 #define CHUNKSIZE          4096
29 #define DEFAULT_NUM_PROBES    8
30
31
32 static int format_match(const char *filename)
33 {
34
35         /* suppress compiler warning */
36         filename = NULL;
37
38         /* this module will handle anything you throw at it */
39         return TRUE;
40 }
41
42 static int init(struct sr_input *in)
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)
49                         return SR_ERR;
50         } else
51                 num_probes = DEFAULT_NUM_PROBES;
52
53         /* create a virtual device */
54         in->vdevice = sr_device_new(NULL, 0, num_probes);
55
56         return SR_OK;
57 }
58
59 static int loadfile(struct sr_input *in, const char *filename)
60 {
61         struct sr_datafeed_header header;
62         struct sr_datafeed_packet packet;
63         struct sr_datafeed_logic logic;
64         unsigned char buffer[CHUNKSIZE];
65         int fd, size, num_probes;
66
67         if ((fd = open(filename, O_RDONLY)) == -1)
68                 return SR_ERR;
69
70         num_probes = g_slist_length(in->vdevice->probes);
71
72         /* send header */
73         header.feed_version = 1;
74         header.num_logic_probes = num_probes;
75         header.num_analog_probes = 0;
76         header.samplerate = 0;
77         gettimeofday(&header.starttime, NULL);
78         packet.type = SR_DF_HEADER;
79         packet.payload = &header;
80         sr_session_bus(in->vdevice, &packet);
81
82         /* chop up the input file into chunks and feed it into the session bus */
83         packet.type = SR_DF_LOGIC;
84         packet.payload = &logic;
85         logic.unitsize = (num_probes + 7) / 8;
86         logic.data = buffer;
87         while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
88                 logic.length = size;
89                 sr_session_bus(in->vdevice, &packet);
90         }
91         close(fd);
92
93         /* end of stream */
94         packet.type = SR_DF_END;
95         sr_session_bus(in->vdevice, &packet);
96
97         return SR_OK;
98 }
99
100 struct sr_input_format input_binary = {
101         .id = "binary",
102         .description = "Raw binary",
103         .format_match = format_match,
104         .init = init,
105         .loadfile = loadfile,
106 };