]>
Commit | Line | Data |
---|---|---|
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 | ||
d486cbdd | 20 | #include <config.h> |
a8b4041a | 21 | #include <sys/types.h> |
8d52f788 | 22 | #include <sys/stat.h> |
a8b4041a BV |
23 | #include <fcntl.h> |
24 | #include <unistd.h> | |
8d52f788 DH |
25 | #include <errno.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
2be182e6 | 28 | #include <glib.h> |
662a1e27 | 29 | #include "sigrok-cli.h" |
2be182e6 | 30 | |
7ac79fb6 | 31 | #define CHUNK_SIZE (4 * 1024 * 1024) |
2be182e6 | 32 | |
1c950633 | 33 | static void load_input_file_module(struct df_arg_desc *df_arg) |
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; | |
64f9f5b2 BV |
42 | gboolean got_sdi; |
43 | int fd; | |
a8b4041a BV |
44 | ssize_t len; |
45 | char *mod_id; | |
a30c837a | 46 | gboolean is_stdin; |
2be182e6 | 47 | |
a8b4041a BV |
48 | if (!sr_input_list()) |
49 | g_critical("No supported input formats available."); | |
2be182e6 | 50 | |
a8b4041a BV |
51 | mod_id = NULL; |
52 | mod_args = NULL; | |
53 | if (opt_input_format) { | |
54 | mod_args = parse_generic_arg(opt_input_format, TRUE); | |
55 | mod_id = g_hash_table_lookup(mod_args, "sigrok_key"); | |
2be182e6 BV |
56 | } |
57 | ||
a30c837a | 58 | is_stdin = strcmp(opt_input_file, "-") == 0; |
64f9f5b2 | 59 | fd = 0; |
7ac79fb6 | 60 | buf = g_string_sized_new(CHUNK_SIZE); |
a8b4041a BV |
61 | if (mod_id) { |
62 | /* User specified an input module to use. */ | |
63 | if (!(imod = sr_input_find(mod_id))) | |
64 | g_critical("Error: unknown input module '%s'.", mod_id); | |
65 | g_hash_table_remove(mod_args, "sigrok_key"); | |
66 | if ((options = sr_input_options_get(imod))) { | |
67 | mod_opts = generic_arg_to_opt(options, mod_args); | |
68 | sr_output_options_free(options); | |
69 | } else | |
70 | mod_opts = NULL; | |
71 | if (!(in = sr_input_new(imod, mod_opts))) | |
72 | g_critical("Error: failed to initialize input module."); | |
73 | if (mod_opts) | |
74 | g_hash_table_destroy(mod_opts); | |
75 | if (mod_args) | |
76 | g_hash_table_destroy(mod_args); | |
a30c837a | 77 | if (!is_stdin && (fd = open(opt_input_file, O_RDONLY)) < 0) |
a8b4041a | 78 | g_critical("Failed to load %s: %s.", opt_input_file, |
19bdd3dd | 79 | g_strerror(errno)); |
a8b4041a | 80 | } else { |
a30c837a | 81 | if (!is_stdin) { |
47f97410 BV |
82 | /* |
83 | * An actual filename: let the input modules try to | |
84 | * identify the file. | |
85 | */ | |
b66260b9 BV |
86 | if (sr_input_scan_file(opt_input_file, &in) == SR_OK) { |
87 | /* That worked, reopen the file for reading. */ | |
88 | fd = open(opt_input_file, O_RDONLY); | |
89 | } | |
47f97410 BV |
90 | } else { |
91 | /* | |
92 | * Taking input from a pipe: let the input modules try | |
93 | * to identify the stream content. | |
94 | */ | |
a30c837a | 95 | if (is_stdin) { |
47f97410 BV |
96 | /* stdin */ |
97 | fd = 0; | |
98 | } else { | |
a30c837a GS |
99 | fd = open(opt_input_file, O_RDONLY); |
100 | if (fd == -1) | |
47f97410 | 101 | g_critical("Failed to load %s: %s.", opt_input_file, |
19bdd3dd | 102 | g_strerror(errno)); |
47f97410 | 103 | } |
7ac79fb6 | 104 | if ((len = read(fd, buf->str, CHUNK_SIZE)) < 1) |
47f97410 | 105 | g_critical("Failed to read %s: %s.", opt_input_file, |
19bdd3dd | 106 | g_strerror(errno)); |
47f97410 | 107 | buf->len = len; |
b66260b9 | 108 | sr_input_scan_buffer(buf, &in); |
47f97410 BV |
109 | } |
110 | if (!in) | |
a8b4041a | 111 | g_critical("Error: no input module found for this file."); |
2be182e6 | 112 | } |
3d24ca2d | 113 | sr_session_new(sr_ctx, &session); |
1c950633 GS |
114 | df_arg->session = session; |
115 | sr_session_datafeed_callback_add(session, datafeed_in, df_arg); | |
2be182e6 | 116 | |
64f9f5b2 | 117 | got_sdi = FALSE; |
f0f54487 | 118 | while (TRUE) { |
a8b4041a | 119 | g_string_truncate(buf, 0); |
7ac79fb6 | 120 | len = read(fd, buf->str, CHUNK_SIZE); |
a8b4041a | 121 | if (len < 0) |
19bdd3dd | 122 | g_critical("Read failed: %s", g_strerror(errno)); |
64f9f5b2 BV |
123 | if (len == 0) |
124 | /* End of file or stream. */ | |
125 | break; | |
a8b4041a BV |
126 | buf->len = len; |
127 | if (sr_input_send(in, buf) != SR_OK) | |
128 | break; | |
64f9f5b2 BV |
129 | |
130 | sdi = sr_input_dev_inst_get(in); | |
f0f54487 | 131 | if (!got_sdi && sdi) { |
64f9f5b2 BV |
132 | /* First time we got a valid sdi. */ |
133 | if (select_channels(sdi) != SR_OK) | |
134 | return; | |
135 | if (sr_session_dev_add(session, sdi) != SR_OK) { | |
136 | g_critical("Failed to use device."); | |
137 | sr_session_destroy(session); | |
138 | return; | |
139 | } | |
140 | got_sdi = TRUE; | |
141 | } | |
a8b4041a | 142 | } |
67a00747 | 143 | sr_input_end(in); |
a8b4041a BV |
144 | sr_input_free(in); |
145 | g_string_free(buf, TRUE); | |
2be182e6 | 146 | |
1c950633 | 147 | df_arg->session = NULL; |
4bf77ec6 | 148 | sr_session_destroy(session); |
2be182e6 | 149 | |
2be182e6 BV |
150 | } |
151 | ||
1c950633 | 152 | void load_input_file(gboolean do_props) |
2be182e6 | 153 | { |
1c950633 | 154 | struct df_arg_desc df_arg; |
4bf77ec6 | 155 | struct sr_session *session; |
e1ec80fa | 156 | struct sr_dev_inst *sdi; |
a8b4041a | 157 | GSList *devices; |
1ef118ab | 158 | GMainLoop *main_loop; |
e1ec80fa | 159 | int ret; |
2be182e6 | 160 | |
1c950633 GS |
161 | memset(&df_arg, 0, sizeof(df_arg)); |
162 | df_arg.do_props = do_props; | |
163 | ||
f5531096 BV |
164 | if (!strcmp(opt_input_file, "-")) { |
165 | /* Input from stdin is never a session file. */ | |
1c950633 | 166 | load_input_file_module(&df_arg); |
f5531096 | 167 | } else { |
3d24ca2d ML |
168 | if ((ret = sr_session_load(sr_ctx, opt_input_file, |
169 | &session)) == SR_OK) { | |
f5531096 BV |
170 | /* sigrok session file */ |
171 | ret = sr_session_dev_list(session, &devices); | |
172 | if (ret != SR_OK || !devices || !devices->data) { | |
173 | g_critical("Failed to access session device."); | |
1ef118ab | 174 | g_slist_free(devices); |
f5531096 BV |
175 | sr_session_destroy(session); |
176 | return; | |
177 | } | |
178 | sdi = devices->data; | |
1ef118ab | 179 | g_slist_free(devices); |
f5531096 BV |
180 | if (select_channels(sdi) != SR_OK) { |
181 | sr_session_destroy(session); | |
182 | return; | |
183 | } | |
1ef118ab DE |
184 | main_loop = g_main_loop_new(NULL, FALSE); |
185 | ||
1c950633 GS |
186 | df_arg.session = session; |
187 | sr_session_datafeed_callback_add(session, | |
188 | datafeed_in, &df_arg); | |
1ef118ab DE |
189 | sr_session_stopped_callback_set(session, |
190 | (sr_session_stopped_callback)g_main_loop_quit, | |
191 | main_loop); | |
192 | if (sr_session_start(session) == SR_OK) | |
193 | g_main_loop_run(main_loop); | |
194 | ||
195 | g_main_loop_unref(main_loop); | |
1c950633 | 196 | df_arg.session = NULL; |
1ef118ab | 197 | sr_session_destroy(session); |
f5531096 BV |
198 | } else if (ret != SR_ERR) { |
199 | /* It's a session file, but it didn't work out somehow. */ | |
200 | g_critical("Failed to load session file."); | |
201 | } else { | |
202 | /* Fall back on input modules. */ | |
1c950633 | 203 | load_input_file_module(&df_arg); |
f5531096 | 204 | } |
2be182e6 BV |
205 | } |
206 | } |