configure.ac: Bump package version to 0.7.1.
[sigrok-cli.git] / input.c
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
20 #include <config.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib.h>
29 #include "sigrok-cli.h"
30
31 #define CHUNK_SIZE (4 * 1024 * 1024)
32
33 static void load_input_file_module(void)
34 {
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;
41         GString *buf;
42         gboolean got_sdi;
43         int fd;
44         ssize_t len;
45         char *mod_id;
46         gboolean is_stdin;
47
48         if (!sr_input_list())
49                 g_critical("No supported input formats available.");
50
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");
56         }
57
58         is_stdin = strcmp(opt_input_file, "-") == 0;
59         fd = 0;
60         buf = g_string_sized_new(CHUNK_SIZE);
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);
77                 if (!is_stdin && (fd = open(opt_input_file, O_RDONLY)) < 0)
78                         g_critical("Failed to load %s: %s.", opt_input_file,
79                                         g_strerror(errno));
80         } else {
81                 if (!is_stdin) {
82                         /*
83                          * An actual filename: let the input modules try to
84                          * identify the file.
85                          */
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                         }
90                 } else {
91                         /*
92                          * Taking input from a pipe: let the input modules try
93                          * to identify the stream content.
94                          */
95                         if (is_stdin) {
96                                 /* stdin */
97                                 fd = 0;
98                         } else {
99                                 fd = open(opt_input_file, O_RDONLY);
100                                 if (fd == -1)
101                                         g_critical("Failed to load %s: %s.", opt_input_file,
102                                                         g_strerror(errno));
103                         }
104                         if ((len = read(fd, buf->str, CHUNK_SIZE)) < 1)
105                                 g_critical("Failed to read %s: %s.", opt_input_file,
106                                                 g_strerror(errno));
107                         buf->len = len;
108                         sr_input_scan_buffer(buf, &in);
109                 }
110                 if (!in)
111                         g_critical("Error: no input module found for this file.");
112         }
113         sr_session_new(sr_ctx, &session);
114         sr_session_datafeed_callback_add(session, &datafeed_in, session);
115
116         got_sdi = FALSE;
117         while (TRUE) {
118                 g_string_truncate(buf, 0);
119                 len = read(fd, buf->str, CHUNK_SIZE);
120                 if (len < 0)
121                         g_critical("Read failed: %s", g_strerror(errno));
122                 if (len == 0)
123                         /* End of file or stream. */
124                         break;
125                 buf->len = len;
126                 if (sr_input_send(in, buf) != SR_OK)
127                         break;
128
129                 sdi = sr_input_dev_inst_get(in);
130                 if (!got_sdi && sdi) {
131                         /* First time we got a valid sdi. */
132                         if (select_channels(sdi) != SR_OK)
133                                 return;
134                         if (sr_session_dev_add(session, sdi) != SR_OK) {
135                                 g_critical("Failed to use device.");
136                                 sr_session_destroy(session);
137                                 return;
138                         }
139                         got_sdi = TRUE;
140                 }
141         }
142         sr_input_end(in);
143         sr_input_free(in);
144         g_string_free(buf, TRUE);
145
146         sr_session_destroy(session);
147
148 }
149
150 void load_input_file(void)
151 {
152         struct sr_session *session;
153         struct sr_dev_inst *sdi;
154         GSList *devices;
155         GMainLoop *main_loop;
156         int ret;
157
158         if (!strcmp(opt_input_file, "-")) {
159                 /* Input from stdin is never a session file. */
160                 load_input_file_module();
161         } else {
162                 if ((ret = sr_session_load(sr_ctx, opt_input_file,
163                                 &session)) == SR_OK) {
164                         /* sigrok session file */
165                         ret = sr_session_dev_list(session, &devices);
166                         if (ret != SR_OK || !devices || !devices->data) {
167                                 g_critical("Failed to access session device.");
168                                 g_slist_free(devices);
169                                 sr_session_destroy(session);
170                                 return;
171                         }
172                         sdi = devices->data;
173                         g_slist_free(devices);
174                         if (select_channels(sdi) != SR_OK) {
175                                 sr_session_destroy(session);
176                                 return;
177                         }
178                         main_loop = g_main_loop_new(NULL, FALSE);
179
180                         sr_session_datafeed_callback_add(session, datafeed_in, session);
181                         sr_session_stopped_callback_set(session,
182                                 (sr_session_stopped_callback)g_main_loop_quit,
183                                 main_loop);
184                         if (sr_session_start(session) == SR_OK)
185                                 g_main_loop_run(main_loop);
186
187                         g_main_loop_unref(main_loop);
188                         sr_session_destroy(session);
189                 } else if (ret != SR_ERR) {
190                         /* It's a session file, but it didn't work out somehow. */
191                         g_critical("Failed to load session file.");
192                 } else {
193                         /* Fall back on input modules. */
194                         load_input_file_module();
195                 }
196         }
197 }