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