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" |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 22 | #include <sys/types.h> |
Dan Horák | 8d52f78 | 2014-01-25 14:15:24 +0100 | [diff] [blame] | 23 | #include <sys/stat.h> |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
Dan Horák | 8d52f78 | 2014-01-25 14:15:24 +0100 | [diff] [blame] | 26 | #include <errno.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 29 | #include <glib.h> |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 30 | |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 31 | #define BUFSIZE 16384 |
| 32 | |
| 33 | static void load_input_file_module(void) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 34 | { |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 35 | struct sr_session *session; |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 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; |
Bert Vermeulen | 64f9f5b | 2014-09-22 15:28:40 +0200 | [diff] [blame] | 42 | gboolean got_sdi; |
| 43 | int fd; |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 44 | ssize_t len; |
| 45 | char *mod_id; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 46 | |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 47 | if (!sr_input_list()) |
| 48 | g_critical("No supported input formats available."); |
| 49 | |
| 50 | mod_id = NULL; |
| 51 | mod_args = NULL; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 52 | if (opt_input_format) { |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 53 | mod_args = parse_generic_arg(opt_input_format, TRUE); |
| 54 | mod_id = g_hash_table_lookup(mod_args, "sigrok_key"); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Bert Vermeulen | 64f9f5b | 2014-09-22 15:28:40 +0200 | [diff] [blame] | 57 | fd = 0; |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 58 | buf = g_string_sized_new(BUFSIZE); |
| 59 | if (mod_id) { |
| 60 | /* User specified an input module to use. */ |
| 61 | if (!(imod = sr_input_find(mod_id))) |
| 62 | g_critical("Error: unknown input module '%s'.", mod_id); |
| 63 | g_hash_table_remove(mod_args, "sigrok_key"); |
| 64 | if ((options = sr_input_options_get(imod))) { |
| 65 | mod_opts = generic_arg_to_opt(options, mod_args); |
| 66 | sr_output_options_free(options); |
| 67 | } else |
| 68 | mod_opts = NULL; |
| 69 | if (!(in = sr_input_new(imod, mod_opts))) |
| 70 | g_critical("Error: failed to initialize input module."); |
| 71 | if (mod_opts) |
| 72 | g_hash_table_destroy(mod_opts); |
| 73 | if (mod_args) |
| 74 | g_hash_table_destroy(mod_args); |
| 75 | if ((fd = open(opt_input_file, O_RDONLY)) == -1) |
| 76 | g_critical("Failed to load %s: %s.", opt_input_file, |
| 77 | strerror(errno)); |
| 78 | } else { |
Bert Vermeulen | 47f9741 | 2014-08-20 01:15:36 +0200 | [diff] [blame] | 79 | if (strcmp(opt_input_file, "-")) { |
| 80 | /* |
| 81 | * An actual filename: let the input modules try to |
| 82 | * identify the file. |
| 83 | */ |
Bert Vermeulen | b66260b | 2014-08-28 01:50:49 +0200 | [diff] [blame] | 84 | if (sr_input_scan_file(opt_input_file, &in) == SR_OK) { |
| 85 | /* That worked, reopen the file for reading. */ |
| 86 | fd = open(opt_input_file, O_RDONLY); |
| 87 | } |
Bert Vermeulen | 47f9741 | 2014-08-20 01:15:36 +0200 | [diff] [blame] | 88 | } else { |
| 89 | /* |
| 90 | * Taking input from a pipe: let the input modules try |
| 91 | * to identify the stream content. |
| 92 | */ |
| 93 | if (!strcmp(opt_input_file, "-")) { |
| 94 | /* stdin */ |
| 95 | fd = 0; |
| 96 | } else { |
| 97 | if ((fd = open(opt_input_file, O_RDONLY)) == -1) |
| 98 | g_critical("Failed to load %s: %s.", opt_input_file, |
| 99 | strerror(errno)); |
| 100 | } |
| 101 | if ((len = read(fd, buf->str, BUFSIZE)) < 1) |
| 102 | g_critical("Failed to read %s: %s.", opt_input_file, |
| 103 | strerror(errno)); |
| 104 | buf->len = len; |
Bert Vermeulen | b66260b | 2014-08-28 01:50:49 +0200 | [diff] [blame] | 105 | sr_input_scan_buffer(buf, &in); |
Bert Vermeulen | 47f9741 | 2014-08-20 01:15:36 +0200 | [diff] [blame] | 106 | } |
| 107 | if (!in) |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 108 | g_critical("Error: no input module found for this file."); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 109 | } |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 110 | sr_session_new(&session); |
| 111 | sr_session_datafeed_callback_add(session, &datafeed_in, NULL); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 112 | |
Bert Vermeulen | 64f9f5b | 2014-09-22 15:28:40 +0200 | [diff] [blame] | 113 | got_sdi = FALSE; |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 114 | while(TRUE) { |
| 115 | g_string_truncate(buf, 0); |
| 116 | len = read(fd, buf->str, BUFSIZE); |
| 117 | if (len < 0) |
| 118 | g_critical("Read failed: %s", strerror(errno)); |
Bert Vermeulen | 64f9f5b | 2014-09-22 15:28:40 +0200 | [diff] [blame] | 119 | if (len == 0) |
| 120 | /* End of file or stream. */ |
| 121 | break; |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 122 | buf->len = len; |
| 123 | if (sr_input_send(in, buf) != SR_OK) |
| 124 | break; |
Bert Vermeulen | 64f9f5b | 2014-09-22 15:28:40 +0200 | [diff] [blame] | 125 | |
| 126 | sdi = sr_input_dev_inst_get(in); |
| 127 | if(!got_sdi && sdi) { |
| 128 | /* First time we got a valid sdi. */ |
| 129 | if (select_channels(sdi) != SR_OK) |
| 130 | return; |
| 131 | if (sr_session_dev_add(session, sdi) != SR_OK) { |
| 132 | g_critical("Failed to use device."); |
| 133 | sr_session_destroy(session); |
| 134 | return; |
| 135 | } |
| 136 | got_sdi = TRUE; |
| 137 | } |
| 138 | |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 139 | } |
Bert Vermeulen | 67a0074 | 2014-09-23 12:16:50 +0200 | [diff] [blame] | 140 | sr_input_end(in); |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 141 | sr_input_free(in); |
| 142 | g_string_free(buf, TRUE); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 143 | |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 144 | sr_session_destroy(session); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 145 | |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void load_input_file(void) |
| 149 | { |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 150 | struct sr_session *session; |
Daniel Elstner | e1ec80f | 2014-02-21 00:34:12 +0100 | [diff] [blame] | 151 | struct sr_dev_inst *sdi; |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 152 | GSList *devices; |
Daniel Elstner | e1ec80f | 2014-02-21 00:34:12 +0100 | [diff] [blame] | 153 | int ret; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 154 | |
Bert Vermeulen | f553109 | 2014-10-02 11:35:07 +0200 | [diff] [blame] | 155 | if (!strcmp(opt_input_file, "-")) { |
| 156 | /* Input from stdin is never a session file. */ |
Bert Vermeulen | a8b4041 | 2014-08-10 16:58:36 +0200 | [diff] [blame] | 157 | load_input_file_module(); |
Bert Vermeulen | f553109 | 2014-10-02 11:35:07 +0200 | [diff] [blame] | 158 | } else { |
| 159 | if ((ret = sr_session_load(opt_input_file, &session)) == SR_OK) { |
| 160 | /* sigrok session file */ |
| 161 | ret = sr_session_dev_list(session, &devices); |
| 162 | if (ret != SR_OK || !devices || !devices->data) { |
| 163 | g_critical("Failed to access session device."); |
| 164 | sr_session_destroy(session); |
| 165 | return; |
| 166 | } |
| 167 | sdi = devices->data; |
| 168 | if (select_channels(sdi) != SR_OK) { |
| 169 | sr_session_destroy(session); |
| 170 | return; |
| 171 | } |
| 172 | sr_session_datafeed_callback_add(session, datafeed_in, NULL); |
| 173 | sr_session_start(session); |
| 174 | sr_session_run(session); |
| 175 | sr_session_stop(session); |
| 176 | } else if (ret != SR_ERR) { |
| 177 | /* It's a session file, but it didn't work out somehow. */ |
| 178 | g_critical("Failed to load session file."); |
| 179 | } else { |
| 180 | /* Fall back on input modules. */ |
| 181 | load_input_file_module(); |
| 182 | } |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 183 | } |
| 184 | } |