2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <sys/types.h>
29 #include "sigrok-cli.h"
31 #define CHUNK_SIZE (4 * 1024 * 1024)
33 static void load_input_file_module(struct df_arg_desc
*df_arg
)
35 struct sr_session
*session
;
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
;
47 gboolean push_scan_data
;
50 g_critical("No supported input formats available.");
54 if (opt_input_format
) {
55 mod_args
= parse_generic_arg(opt_input_format
, TRUE
, NULL
);
56 mod_id
= g_hash_table_lookup(mod_args
, "sigrok_key");
59 is_stdin
= strcmp(opt_input_file
, "-") == 0;
60 push_scan_data
= FALSE
;
62 buf
= g_string_sized_new(CHUNK_SIZE
);
64 /* User specified an input module to use. */
65 if (!(imod
= sr_input_find(mod_id
)))
66 g_critical("Error: unknown input module '%s'.", mod_id
);
67 g_hash_table_remove(mod_args
, "sigrok_key");
68 if ((options
= sr_input_options_get(imod
))) {
69 mod_opts
= generic_arg_to_opt(options
, mod_args
);
70 (void)warn_unknown_keys(options
, mod_args
, NULL
);
71 sr_input_options_free(options
);
75 if (!(in
= sr_input_new(imod
, mod_opts
)))
76 g_critical("Error: failed to initialize input module.");
78 g_hash_table_destroy(mod_opts
);
80 g_hash_table_destroy(mod_args
);
81 if (!is_stdin
&& (fd
= open(opt_input_file
, O_RDONLY
)) < 0)
82 g_critical("Failed to load %s: %s.", opt_input_file
,
87 * An actual filename: let the input modules try to
90 if (sr_input_scan_file(opt_input_file
, &in
) == SR_OK
) {
91 /* That worked, reopen the file for reading. */
92 fd
= open(opt_input_file
, O_RDONLY
);
96 * Taking input from a pipe: let the input modules try
97 * to identify the stream content.
103 fd
= open(opt_input_file
, O_RDONLY
);
105 g_critical("Failed to load %s: %s.", opt_input_file
,
108 if ((len
= read(fd
, buf
->str
, buf
->allocated_len
)) < 1)
109 g_critical("Failed to read %s: %s.", opt_input_file
,
112 sr_input_scan_buffer(buf
, &in
);
113 push_scan_data
= TRUE
;
116 g_critical("Error: no input module found for this file.");
118 sr_session_new(sr_ctx
, &session
);
119 df_arg
->session
= session
;
120 sr_session_datafeed_callback_add(session
, datafeed_in
, df_arg
);
123 * Implementation detail: The combination of reading from stdin
124 * and automatic file format detection may have pushed the first
125 * chunk of input data into the input module's data accumulator,
126 * _bypassing_ the .receive() callback. It is essential to call
127 * .receive() before calling .end() for files of size smaller than
128 * CHUNK_SIZE (which is a typical case). So that sdi becomes ready.
129 * Fortunately all input modules accept .receive() calls with
130 * a zero length, and inspect whatever was accumulated so far.
132 * After that optional initial push of data which was queued
133 * above during format detection, continue reading remaining
134 * chunks from the input file until EOF is seen.
138 g_string_truncate(buf
, 0);
142 len
= read(fd
, buf
->str
, buf
->allocated_len
);
144 g_critical("Read failed: %s", g_strerror(errno
));
145 if (len
== 0 && !push_scan_data
)
146 /* End of file or stream. */
148 push_scan_data
= FALSE
;
150 if (sr_input_send(in
, buf
) != SR_OK
)
153 sdi
= sr_input_dev_inst_get(in
);
154 if (!got_sdi
&& sdi
) {
155 /* First time we got a valid sdi. */
156 if (select_channels(sdi
) != SR_OK
)
158 if (sr_session_dev_add(session
, sdi
) != SR_OK
) {
159 g_critical("Failed to use device.");
160 sr_session_destroy(session
);
168 g_string_free(buf
, TRUE
);
171 df_arg
->session
= NULL
;
172 sr_session_destroy(session
);
175 void load_input_file(gboolean do_props
)
177 struct df_arg_desc df_arg
;
178 struct sr_session
*session
;
179 struct sr_dev_inst
*sdi
;
181 GMainLoop
*main_loop
;
184 memset(&df_arg
, 0, sizeof(df_arg
));
185 df_arg
.do_props
= do_props
;
187 if (!strcmp(opt_input_file
, "-")) {
188 /* Input from stdin is never a session file. */
189 load_input_file_module(&df_arg
);
191 if ((ret
= sr_session_load(sr_ctx
, opt_input_file
,
192 &session
)) == SR_OK
) {
193 /* sigrok session file */
194 ret
= sr_session_dev_list(session
, &devices
);
195 if (ret
!= SR_OK
|| !devices
|| !devices
->data
) {
196 g_critical("Failed to access session device.");
197 g_slist_free(devices
);
198 sr_session_destroy(session
);
202 g_slist_free(devices
);
203 if (select_channels(sdi
) != SR_OK
) {
204 sr_session_destroy(session
);
207 main_loop
= g_main_loop_new(NULL
, FALSE
);
209 df_arg
.session
= session
;
210 sr_session_datafeed_callback_add(session
,
211 datafeed_in
, &df_arg
);
212 sr_session_stopped_callback_set(session
,
213 (sr_session_stopped_callback
)g_main_loop_quit
,
215 if (sr_session_start(session
) == SR_OK
)
216 g_main_loop_run(main_loop
);
218 g_main_loop_unref(main_loop
);
219 df_arg
.session
= NULL
;
220 sr_session_destroy(session
);
221 } else if (ret
!= SR_ERR
) {
222 /* It's a session file, but it didn't work out somehow. */
223 g_critical("Failed to load session file.");
225 /* Fall back on input modules. */
226 load_input_file_module(&df_arg
);