]> sigrok.org Git - libsigrok.git/blame - input/binary.c
sr: extra checks for properly handling driverless devices
[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
c506a6a6
32struct context {
33 int samplerate;
34};
35
757b8c62 36static int format_match(const char *filename)
34e4813f 37{
13a12913 38 /* suppress compiler warning */
719c5a93 39 (void)filename;
13a12913
BV
40
41 /* this module will handle anything you throw at it */
34e4813f
BV
42 return TRUE;
43}
44
f50f3f40 45static int init(struct sr_input *in)
13a12913 46{
464d12c7 47 int num_probes, i;
c37d2b1b 48 char name[SR_MAX_PROBENAME_LEN + 1];
c506a6a6
49 char *param;
50 struct context *ctx;
51
52 if (!(ctx = g_try_malloc0(sizeof(*ctx)))) {
53 sr_err("binary in: %s: ctx malloc failed", __func__);
54 return SR_ERR_MALLOC;
55 }
13a12913 56
c506a6a6
57 num_probes = DEFAULT_NUM_PROBES;
58 ctx->samplerate = 0;
59
60 if(in->param) {
61 param = g_hash_table_lookup(in->param, "numprobes");
62 if (param) {
63 num_probes = strtoul(param, NULL, 10);
64 if (num_probes < 1)
65 return SR_ERR;
66 }
67
68 param = g_hash_table_lookup(in->param, "samplerate");
69 if (param) {
70 ctx->samplerate = strtoul(param, NULL, 10);
71 if (ctx->samplerate < 1) {
72 return SR_ERR;
73 }
74 }
c37d2b1b 75 }
13a12913 76
c37d2b1b 77 /* Create a virtual device. */
bb7ef793 78 in->vdev = sr_dev_new(NULL, 0);
c506a6a6 79 in->internal = ctx;
464d12c7 80
c37d2b1b 81 for (i = 0; i < num_probes; i++) {
464d12c7 82 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
c37d2b1b 83 /* TODO: Check return value. */
bb7ef793 84 sr_dev_probe_add(in->vdev, name);
464d12c7 85 }
13a12913 86
e46b8fb1 87 return SR_OK;
13a12913
BV
88}
89
f50f3f40 90static int loadfile(struct sr_input *in, const char *filename)
34e4813f 91{
b9c735a2
UH
92 struct sr_datafeed_header header;
93 struct sr_datafeed_packet packet;
f366e86c 94 struct sr_datafeed_meta_logic meta;
9c939c51
BV
95 struct sr_datafeed_logic logic;
96 unsigned char buffer[CHUNKSIZE];
db91a1c3 97 int fd, size, num_probes;
c506a6a6
98 struct context *ctx;
99
100 ctx = in->internal;
34e4813f 101
757b8c62 102 if ((fd = open(filename, O_RDONLY)) == -1)
e46b8fb1 103 return SR_ERR;
34e4813f 104
bb7ef793 105 num_probes = g_slist_length(in->vdev->probes);
db91a1c3 106
13a12913 107 /* send header */
34e4813f 108 header.feed_version = 1;
34e4813f 109 gettimeofday(&header.starttime, NULL);
5a2326a7 110 packet.type = SR_DF_HEADER;
34e4813f 111 packet.payload = &header;
31ccebc4 112 sr_session_send(in->vdev, &packet);
34e4813f 113
f366e86c
BV
114 /* Send metadata about the SR_DF_LOGIC packets to come. */
115 packet.type = SR_DF_META_LOGIC;
116 packet.payload = &meta;
c506a6a6 117 meta.samplerate = ctx->samplerate;
f366e86c
BV
118 meta.num_probes = num_probes;
119 sr_session_send(in->vdev, &packet);
120
13a12913 121 /* chop up the input file into chunks and feed it into the session bus */
5a2326a7 122 packet.type = SR_DF_LOGIC;
9c939c51
BV
123 packet.payload = &logic;
124 logic.unitsize = (num_probes + 7) / 8;
125 logic.data = buffer;
757b8c62 126 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
9c939c51 127 logic.length = size;
31ccebc4 128 sr_session_send(in->vdev, &packet);
34e4813f
BV
129 }
130 close(fd);
131
13a12913 132 /* end of stream */
5a2326a7 133 packet.type = SR_DF_END;
31ccebc4 134 sr_session_send(in->vdev, &packet);
34e4813f 135
c506a6a6
136 g_free(ctx);
137 in->internal = NULL;
138
e46b8fb1 139 return SR_OK;
34e4813f
BV
140}
141
7c1d391c 142SR_PRIV struct sr_input_format input_binary = {
cdb3573c 143 .id = "binary",
d494a4aa
UH
144 .description = "Raw binary",
145 .format_match = format_match,
146 .init = init,
147 .loadfile = loadfile,
34e4813f 148};