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