]> sigrok.org Git - libsigrok.git/blame - input/binary.c
sr: chronovu-la8: fix segfault on discovery
[libsigrok.git] / input / binary.c
CommitLineData
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
757b8c62 32static int format_match(const char *filename)
34e4813f 33{
13a12913 34 /* suppress compiler warning */
719c5a93 35 (void)filename;
13a12913
BV
36
37 /* this module will handle anything you throw at it */
34e4813f
BV
38 return TRUE;
39}
40
f50f3f40 41static int init(struct sr_input *in)
13a12913 42{
464d12c7 43 int num_probes, i;
c37d2b1b 44 char name[SR_MAX_PROBENAME_LEN + 1];
13a12913
BV
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;
c37d2b1b 50 } else {
13a12913 51 num_probes = DEFAULT_NUM_PROBES;
c37d2b1b 52 }
13a12913 53
c37d2b1b 54 /* Create a virtual device. */
bb7ef793 55 in->vdev = sr_dev_new(NULL, 0);
464d12c7 56
c37d2b1b 57 for (i = 0; i < num_probes; i++) {
464d12c7 58 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
c37d2b1b 59 /* TODO: Check return value. */
bb7ef793 60 sr_dev_probe_add(in->vdev, name);
464d12c7 61 }
13a12913 62
e46b8fb1 63 return SR_OK;
13a12913
BV
64}
65
f50f3f40 66static int loadfile(struct sr_input *in, const char *filename)
34e4813f 67{
b9c735a2
UH
68 struct sr_datafeed_header header;
69 struct sr_datafeed_packet packet;
f366e86c 70 struct sr_datafeed_meta_logic meta;
9c939c51
BV
71 struct sr_datafeed_logic logic;
72 unsigned char buffer[CHUNKSIZE];
db91a1c3 73 int fd, size, num_probes;
34e4813f 74
757b8c62 75 if ((fd = open(filename, O_RDONLY)) == -1)
e46b8fb1 76 return SR_ERR;
34e4813f 77
bb7ef793 78 num_probes = g_slist_length(in->vdev->probes);
db91a1c3 79
13a12913 80 /* send header */
34e4813f 81 header.feed_version = 1;
34e4813f 82 gettimeofday(&header.starttime, NULL);
5a2326a7 83 packet.type = SR_DF_HEADER;
34e4813f 84 packet.payload = &header;
31ccebc4 85 sr_session_send(in->vdev, &packet);
34e4813f 86
f366e86c
BV
87 /* Send metadata about the SR_DF_LOGIC packets to come. */
88 packet.type = SR_DF_META_LOGIC;
89 packet.payload = &meta;
90 meta.samplerate = 0;
91 meta.num_probes = num_probes;
92 sr_session_send(in->vdev, &packet);
93
13a12913 94 /* chop up the input file into chunks and feed it into the session bus */
5a2326a7 95 packet.type = SR_DF_LOGIC;
9c939c51
BV
96 packet.payload = &logic;
97 logic.unitsize = (num_probes + 7) / 8;
98 logic.data = buffer;
757b8c62 99 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
9c939c51 100 logic.length = size;
31ccebc4 101 sr_session_send(in->vdev, &packet);
34e4813f
BV
102 }
103 close(fd);
104
13a12913 105 /* end of stream */
5a2326a7 106 packet.type = SR_DF_END;
31ccebc4 107 sr_session_send(in->vdev, &packet);
34e4813f 108
e46b8fb1 109 return SR_OK;
34e4813f
BV
110}
111
7c1d391c 112SR_PRIV struct sr_input_format input_binary = {
cdb3573c 113 .id = "binary",
d494a4aa
UH
114 .description = "Raw binary",
115 .format_match = format_match,
116 .init = init,
117 .loadfile = loadfile,
34e4813f 118};