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