]> sigrok.org Git - libsigrok.git/blame_incremental - input/binary.c
sr: SR_DF_ANALOG type, and meta types for analog+logic
[libsigrok.git] / input / binary.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-2012 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#include "sigrok-internal.h"
28
29#define CHUNKSIZE (512 * 1024)
30#define DEFAULT_NUM_PROBES 8
31
32static int format_match(const char *filename)
33{
34 /* suppress compiler warning */
35 (void)filename;
36
37 /* this module will handle anything you throw at it */
38 return TRUE;
39}
40
41static int init(struct sr_input *in)
42{
43 int num_probes, i;
44 char name[SR_MAX_PROBENAME_LEN + 1];
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
54 /* Create a virtual device. */
55 in->vdev = sr_dev_new(NULL, 0);
56
57 for (i = 0; i < num_probes; i++) {
58 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
59 /* TODO: Check return value. */
60 sr_dev_probe_add(in->vdev, name);
61 }
62
63 return SR_OK;
64}
65
66static int loadfile(struct sr_input *in, const char *filename)
67{
68 struct sr_datafeed_header header;
69 struct sr_datafeed_packet packet;
70 struct sr_datafeed_logic logic;
71 unsigned char buffer[CHUNKSIZE];
72 int fd, size, num_probes;
73
74 if ((fd = open(filename, O_RDONLY)) == -1)
75 return SR_ERR;
76
77 num_probes = g_slist_length(in->vdev->probes);
78
79 /* send header */
80 header.feed_version = 1;
81 header.num_logic_probes = num_probes;
82 header.samplerate = 0;
83 gettimeofday(&header.starttime, NULL);
84 packet.type = SR_DF_HEADER;
85 packet.payload = &header;
86 sr_session_send(in->vdev, &packet);
87
88 /* chop up the input file into chunks and feed it into the session bus */
89 packet.type = SR_DF_LOGIC;
90 packet.payload = &logic;
91 logic.unitsize = (num_probes + 7) / 8;
92 logic.data = buffer;
93 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
94 logic.length = size;
95 sr_session_send(in->vdev, &packet);
96 }
97 close(fd);
98
99 /* end of stream */
100 packet.type = SR_DF_END;
101 sr_session_send(in->vdev, &packet);
102
103 return SR_OK;
104}
105
106SR_PRIV struct sr_input_format input_binary = {
107 .id = "binary",
108 .description = "Raw binary",
109 .format_match = format_match,
110 .init = init,
111 .loadfile = loadfile,
112};