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