]>
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 | ||
20fb52e0 | 20 | #include "sigrok-cli.h" |
2be182e6 | 21 | #include "config.h" |
8d52f788 DH |
22 | #include <sys/stat.h> |
23 | #include <errno.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
2be182e6 | 26 | #include <glib.h> |
2be182e6 BV |
27 | |
28 | extern gchar *opt_input_file; | |
29 | extern gchar *opt_input_format; | |
30 | extern gchar *opt_probes; | |
31 | ||
32 | ||
33 | /** | |
34 | * Return the input file format which the CLI tool should use. | |
35 | * | |
36 | * If the user specified -I / --input-format, use that one. Otherwise, try to | |
37 | * autodetect the format as good as possible. Failing that, return NULL. | |
38 | * | |
39 | * @param filename The filename of the input file. Must not be NULL. | |
40 | * @param opt The -I / --input-file option the user specified (or NULL). | |
41 | * | |
42 | * @return A pointer to the 'struct sr_input_format' that should be used, | |
43 | * or NULL if no input format was selected or auto-detected. | |
44 | */ | |
45 | static struct sr_input_format *determine_input_file_format( | |
46 | const char *filename, const char *opt) | |
47 | { | |
48 | int i; | |
49 | struct sr_input_format **inputs; | |
50 | ||
51 | /* If there are no input formats, return NULL right away. */ | |
52 | inputs = sr_input_list(); | |
53 | if (!inputs) { | |
54 | g_critical("No supported input formats available."); | |
55 | return NULL; | |
56 | } | |
57 | ||
58 | /* If the user specified -I / --input-format, use that one. */ | |
59 | if (opt) { | |
60 | for (i = 0; inputs[i]; i++) { | |
61 | if (strcasecmp(inputs[i]->id, opt)) | |
62 | continue; | |
63 | g_debug("Using user-specified input file format '%s'.", | |
64 | inputs[i]->id); | |
65 | return inputs[i]; | |
66 | } | |
67 | ||
68 | /* The user specified an unknown input format, return NULL. */ | |
69 | g_critical("Error: specified input file format '%s' is " | |
70 | "unknown.", opt); | |
71 | return NULL; | |
72 | } | |
73 | ||
74 | /* Otherwise, try to find an input module that can handle this file. */ | |
75 | for (i = 0; inputs[i]; i++) { | |
76 | if (inputs[i]->format_match(filename)) | |
77 | break; | |
78 | } | |
79 | ||
80 | /* Return NULL if no input module wanted to touch this. */ | |
81 | if (!inputs[i]) { | |
82 | g_critical("Error: no matching input module found."); | |
83 | return NULL; | |
84 | } | |
85 | ||
86 | g_debug("cli: Autodetected '%s' input format for file '%s'.", | |
87 | inputs[i]->id, filename); | |
88 | ||
89 | return inputs[i]; | |
90 | } | |
91 | ||
92 | static void load_input_file_format(void) | |
93 | { | |
94 | GHashTable *fmtargs = NULL; | |
95 | struct stat st; | |
96 | struct sr_input *in; | |
97 | struct sr_input_format *input_format; | |
98 | char *fmtspec = NULL; | |
99 | ||
100 | if (opt_input_format) { | |
101 | fmtargs = parse_generic_arg(opt_input_format, TRUE); | |
102 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
103 | } | |
104 | ||
105 | if (!(input_format = determine_input_file_format(opt_input_file, | |
106 | fmtspec))) { | |
107 | /* The exact cause was already logged. */ | |
108 | return; | |
109 | } | |
110 | ||
111 | if (fmtargs) | |
112 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
113 | ||
114 | if (stat(opt_input_file, &st) == -1) { | |
115 | g_critical("Failed to load %s: %s", opt_input_file, | |
116 | strerror(errno)); | |
117 | exit(1); | |
118 | } | |
119 | ||
120 | /* Initialize the input module. */ | |
121 | if (!(in = g_try_malloc(sizeof(struct sr_input)))) { | |
122 | g_critical("Failed to allocate input module."); | |
123 | exit(1); | |
124 | } | |
125 | in->format = input_format; | |
126 | in->param = fmtargs; | |
127 | if (in->format->init) { | |
128 | if (in->format->init(in, opt_input_file) != SR_OK) { | |
129 | g_critical("Input format init failed."); | |
130 | exit(1); | |
131 | } | |
132 | } | |
133 | ||
029d73fe | 134 | if (select_channels(in->sdi) != SR_OK) |
2be182e6 BV |
135 | return; |
136 | ||
137 | sr_session_new(); | |
138 | sr_session_datafeed_callback_add(datafeed_in, NULL); | |
139 | if (sr_session_dev_add(in->sdi) != SR_OK) { | |
140 | g_critical("Failed to use device."); | |
141 | sr_session_destroy(); | |
142 | return; | |
143 | } | |
144 | ||
145 | input_format->loadfile(in, opt_input_file); | |
146 | ||
147 | sr_session_destroy(); | |
148 | ||
149 | if (fmtargs) | |
150 | g_hash_table_destroy(fmtargs); | |
151 | } | |
152 | ||
153 | void load_input_file(void) | |
154 | { | |
e1ec80fa DE |
155 | GSList *sessions; |
156 | struct sr_dev_inst *sdi; | |
157 | int ret; | |
2be182e6 BV |
158 | |
159 | if (sr_session_load(opt_input_file) == SR_OK) { | |
160 | /* sigrok session file */ | |
e1ec80fa DE |
161 | ret = sr_session_dev_list(&sessions); |
162 | if (ret != SR_OK || !sessions->data) { | |
163 | g_critical("Failed to access session device."); | |
164 | sr_session_destroy(); | |
165 | return; | |
166 | } | |
167 | sdi = sessions->data; | |
029d73fe | 168 | if (select_channels(sdi) != SR_OK) { |
e1ec80fa DE |
169 | sr_session_destroy(); |
170 | return; | |
171 | } | |
2be182e6 BV |
172 | sr_session_datafeed_callback_add(datafeed_in, NULL); |
173 | sr_session_start(); | |
174 | sr_session_run(); | |
175 | sr_session_stop(); | |
176 | } | |
177 | else { | |
178 | /* fall back on input modules */ | |
179 | load_input_file_format(); | |
180 | } | |
181 | } |