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/>.
20 #include "sigrok-cli.h"
22 #include <sys/types.h>
33 static void load_input_file_module(void)
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 g_critical("No supported input formats available.");
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");
56 buf = g_string_sized_new(BUFSIZE);
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);
67 if (!(in = sr_input_new(imod, mod_opts)))
68 g_critical("Error: failed to initialize input module.");
70 g_hash_table_destroy(mod_opts);
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,
77 /* Let the input modules try to identify the file. */
78 if ((fd = open(opt_input_file, O_RDONLY)) == -1)
79 g_critical("Failed to load %s: %s.", opt_input_file,
81 if ((len = read(fd, buf->str, BUFSIZE)) < 1)
82 g_critical("Failed to read %s: %s.", opt_input_file,
85 if (!(in = sr_input_scan_buffer(buf)))
86 g_critical("Error: no input module found for this file.");
88 sdi = sr_input_dev_inst_get(in);
90 if (select_channels(sdi) != SR_OK)
93 sr_session_new(&session);
94 sr_session_datafeed_callback_add(session, &datafeed_in, NULL);
95 if (sr_session_dev_add(session, sdi) != SR_OK) {
96 g_critical("Failed to use device.");
97 sr_session_destroy(session);
102 g_string_truncate(buf, 0);
103 len = read(fd, buf->str, BUFSIZE);
105 g_critical("Read failed: %s", strerror(errno));
107 if (sr_input_send(in, buf) != SR_OK)
113 g_string_free(buf, TRUE);
115 sr_session_destroy(session);
119 void load_input_file(void)
121 struct sr_session *session;
122 struct sr_dev_inst *sdi;
127 if (stat(opt_input_file, &st) == -1)
128 g_critical("Failed to load %s: %s.", opt_input_file, strerror(errno));
130 if (sr_session_load(opt_input_file, &session) == SR_OK) {
131 /* sigrok session file */
132 ret = sr_session_dev_list(session, &devices);
133 if (ret != SR_OK || !devices->data) {
134 g_critical("Failed to access session device.");
135 sr_session_destroy(session);
139 if (select_channels(sdi) != SR_OK) {
140 sr_session_destroy(session);
143 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
144 sr_session_start(session);
145 sr_session_run(session);
146 sr_session_stop(session);
149 /* fall back on input modules */
150 load_input_file_module();