]> sigrok.org Git - libsigrok.git/blame_incremental - input/binary.c
sr: fx2lafw: Add basic support for USBee DX and clones
[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 "libsigrok.h"
27#include "libsigrok-internal.h"
28
29#define CHUNKSIZE (512 * 1024)
30#define DEFAULT_NUM_PROBES 8
31
32struct context {
33 uint64_t samplerate;
34};
35
36static int format_match(const char *filename)
37{
38 /* suppress compiler warning */
39 (void)filename;
40
41 /* this module will handle anything you throw at it */
42 return TRUE;
43}
44
45static int init(struct sr_input *in)
46{
47 int num_probes, i;
48 char name[SR_MAX_PROBENAME_LEN + 1];
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 }
56
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 if (sr_parse_sizestring(param, &ctx->samplerate) != SR_OK)
71 return SR_ERR;
72 }
73 }
74
75 /* Create a virtual device. */
76 in->vdev = sr_dev_new(NULL, 0);
77 in->internal = ctx;
78
79 for (i = 0; i < num_probes; i++) {
80 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
81 /* TODO: Check return value. */
82 sr_dev_probe_add(in->vdev, name);
83 }
84
85 return SR_OK;
86}
87
88static int loadfile(struct sr_input *in, const char *filename)
89{
90 struct sr_datafeed_header header;
91 struct sr_datafeed_packet packet;
92 struct sr_datafeed_meta_logic meta;
93 struct sr_datafeed_logic logic;
94 unsigned char buffer[CHUNKSIZE];
95 int fd, size, num_probes;
96 struct context *ctx;
97
98 ctx = in->internal;
99
100 if ((fd = open(filename, O_RDONLY)) == -1)
101 return SR_ERR;
102
103 num_probes = g_slist_length(in->vdev->probes);
104
105 /* send header */
106 header.feed_version = 1;
107 gettimeofday(&header.starttime, NULL);
108 packet.type = SR_DF_HEADER;
109 packet.payload = &header;
110 sr_session_send(in->vdev, &packet);
111
112 /* Send metadata about the SR_DF_LOGIC packets to come. */
113 packet.type = SR_DF_META_LOGIC;
114 packet.payload = &meta;
115 meta.samplerate = ctx->samplerate;
116 meta.num_probes = num_probes;
117 sr_session_send(in->vdev, &packet);
118
119 /* chop up the input file into chunks and feed it into the session bus */
120 packet.type = SR_DF_LOGIC;
121 packet.payload = &logic;
122 logic.unitsize = (num_probes + 7) / 8;
123 logic.data = buffer;
124 while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
125 logic.length = size;
126 sr_session_send(in->vdev, &packet);
127 }
128 close(fd);
129
130 /* end of stream */
131 packet.type = SR_DF_END;
132 sr_session_send(in->vdev, &packet);
133
134 g_free(ctx);
135 in->internal = NULL;
136
137 return SR_OK;
138}
139
140SR_PRIV struct sr_input_format input_binary = {
141 .id = "binary",
142 .description = "Raw binary",
143 .format_match = format_match,
144 .init = init,
145 .loadfile = loadfile,
146};