]> sigrok.org Git - sigrok-cli.git/blame - input.c
Use sr_input_scan_*() API changes.
[sigrok-cli.git] / input.c
CommitLineData
2be182e6
BV
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 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
20fb52e0 20#include "sigrok-cli.h"
2be182e6 21#include "config.h"
a8b4041a 22#include <sys/types.h>
8d52f788 23#include <sys/stat.h>
a8b4041a
BV
24#include <fcntl.h>
25#include <unistd.h>
8d52f788
DH
26#include <errno.h>
27#include <stdlib.h>
28#include <string.h>
2be182e6 29#include <glib.h>
2be182e6 30
a8b4041a 31#define BUFSIZE 16384
2be182e6 32
a8b4041a 33static void load_input_file_module(void)
2be182e6 34{
4bf77ec6 35 struct sr_session *session;
a8b4041a
BV
36 const struct sr_input *in;
37 const struct sr_input_module *imod;
38 const struct sr_option **options;
39 struct sr_dev_inst *sdi;
40 GHashTable *mod_args, *mod_opts;
41 GString *buf;
42 int fd;
43 ssize_t len;
44 char *mod_id;
2be182e6 45
a8b4041a
BV
46 if (!sr_input_list())
47 g_critical("No supported input formats available.");
2be182e6 48
a8b4041a
BV
49 mod_id = NULL;
50 mod_args = NULL;
51 if (opt_input_format) {
52 mod_args = parse_generic_arg(opt_input_format, TRUE);
53 mod_id = g_hash_table_lookup(mod_args, "sigrok_key");
2be182e6
BV
54 }
55
a8b4041a
BV
56 buf = g_string_sized_new(BUFSIZE);
57 if (mod_id) {
58 /* User specified an input module to use. */
59 if (!(imod = sr_input_find(mod_id)))
60 g_critical("Error: unknown input module '%s'.", mod_id);
61 g_hash_table_remove(mod_args, "sigrok_key");
62 if ((options = sr_input_options_get(imod))) {
63 mod_opts = generic_arg_to_opt(options, mod_args);
64 sr_output_options_free(options);
65 } else
66 mod_opts = NULL;
67 if (!(in = sr_input_new(imod, mod_opts)))
68 g_critical("Error: failed to initialize input module.");
69 if (mod_opts)
70 g_hash_table_destroy(mod_opts);
71 if (mod_args)
72 g_hash_table_destroy(mod_args);
73 if ((fd = open(opt_input_file, O_RDONLY)) == -1)
74 g_critical("Failed to load %s: %s.", opt_input_file,
75 strerror(errno));
76 } else {
47f97410
BV
77 if (strcmp(opt_input_file, "-")) {
78 /*
79 * An actual filename: let the input modules try to
80 * identify the file.
81 */
b66260b9
BV
82 if (sr_input_scan_file(opt_input_file, &in) == SR_OK) {
83 /* That worked, reopen the file for reading. */
84 fd = open(opt_input_file, O_RDONLY);
85 }
47f97410
BV
86 } else {
87 /*
88 * Taking input from a pipe: let the input modules try
89 * to identify the stream content.
90 */
91 if (!strcmp(opt_input_file, "-")) {
92 /* stdin */
93 fd = 0;
94 } else {
95 if ((fd = open(opt_input_file, O_RDONLY)) == -1)
96 g_critical("Failed to load %s: %s.", opt_input_file,
97 strerror(errno));
98 }
99 if ((len = read(fd, buf->str, BUFSIZE)) < 1)
100 g_critical("Failed to read %s: %s.", opt_input_file,
101 strerror(errno));
102 buf->len = len;
b66260b9 103 sr_input_scan_buffer(buf, &in);
47f97410
BV
104 }
105 if (!in)
a8b4041a 106 g_critical("Error: no input module found for this file.");
2be182e6 107 }
a8b4041a 108 sdi = sr_input_dev_inst_get(in);
2be182e6 109
a8b4041a 110 if (select_channels(sdi) != SR_OK)
2be182e6
BV
111 return;
112
4bf77ec6
BV
113 sr_session_new(&session);
114 sr_session_datafeed_callback_add(session, &datafeed_in, NULL);
a8b4041a 115 if (sr_session_dev_add(session, sdi) != SR_OK) {
2be182e6 116 g_critical("Failed to use device.");
4bf77ec6 117 sr_session_destroy(session);
2be182e6
BV
118 return;
119 }
120
a8b4041a
BV
121 while(TRUE) {
122 g_string_truncate(buf, 0);
123 len = read(fd, buf->str, BUFSIZE);
124 if (len < 0)
125 g_critical("Read failed: %s", strerror(errno));
126 buf->len = len;
127 if (sr_input_send(in, buf) != SR_OK)
128 break;
129 if (len < BUFSIZE)
130 break;
131 }
132 sr_input_free(in);
133 g_string_free(buf, TRUE);
2be182e6 134
4bf77ec6 135 sr_session_destroy(session);
2be182e6 136
2be182e6
BV
137}
138
139void load_input_file(void)
140{
4bf77ec6 141 struct sr_session *session;
e1ec80fa 142 struct sr_dev_inst *sdi;
a8b4041a 143 GSList *devices;
e1ec80fa 144 int ret;
2be182e6 145
a48ddcc1 146 if (strcmp(opt_input_file, "-") || sr_session_load(opt_input_file, &session) == SR_OK) {
2be182e6 147 /* sigrok session file */
4bf77ec6
BV
148 ret = sr_session_dev_list(session, &devices);
149 if (ret != SR_OK || !devices->data) {
e1ec80fa 150 g_critical("Failed to access session device.");
4bf77ec6 151 sr_session_destroy(session);
e1ec80fa
DE
152 return;
153 }
4bf77ec6 154 sdi = devices->data;
029d73fe 155 if (select_channels(sdi) != SR_OK) {
4bf77ec6 156 sr_session_destroy(session);
e1ec80fa
DE
157 return;
158 }
4bf77ec6
BV
159 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
160 sr_session_start(session);
161 sr_session_run(session);
162 sr_session_stop(session);
2be182e6
BV
163 }
164 else {
165 /* fall back on input modules */
a8b4041a 166 load_input_file_module();
2be182e6
BV
167 }
168}