blob: eb73d05d197b217b1cd717f878c1c4941685d376 [file] [log] [blame]
Bert Vermeulen2be182e2013-11-17 13:15:38 +01001/*
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 Hermann20fb52e2013-11-19 11:48:06 +010020#include "sigrok-cli.h"
Bert Vermeulen2be182e2013-11-17 13:15:38 +010021#include "config.h"
Bert Vermeulena8b40412014-08-10 16:58:36 +020022#include <sys/types.h>
Dan Horák8d52f782014-01-25 14:15:24 +010023#include <sys/stat.h>
Bert Vermeulena8b40412014-08-10 16:58:36 +020024#include <fcntl.h>
25#include <unistd.h>
Dan Horák8d52f782014-01-25 14:15:24 +010026#include <errno.h>
27#include <stdlib.h>
28#include <string.h>
Bert Vermeulen2be182e2013-11-17 13:15:38 +010029#include <glib.h>
Bert Vermeulen2be182e2013-11-17 13:15:38 +010030
Bert Vermeulena8b40412014-08-10 16:58:36 +020031#define BUFSIZE 16384
32
33static void load_input_file_module(void)
Bert Vermeulen2be182e2013-11-17 13:15:38 +010034{
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +020035 struct sr_session *session;
Bert Vermeulena8b40412014-08-10 16:58:36 +020036 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 int fd;
43 ssize_t len;
44 char *mod_id;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010045
Bert Vermeulena8b40412014-08-10 16:58:36 +020046 if (!sr_input_list())
47 g_critical("No supported input formats available.");
48
49 mod_id = NULL;
50 mod_args = NULL;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010051 if (opt_input_format) {
Bert Vermeulena8b40412014-08-10 16:58:36 +020052 mod_args = parse_generic_arg(opt_input_format, TRUE);
53 mod_id = g_hash_table_lookup(mod_args, "sigrok_key");
Bert Vermeulen2be182e2013-11-17 13:15:38 +010054 }
55
Bert Vermeulena8b40412014-08-10 16:58:36 +020056 buf = g_string_sized_new(BUFSIZE);
57 if (mod_id) {
58 /* User specified an input module to use. */
59 if (!(imod = sr_input_find(mod_id)))
60 g_critical("Error: unknown input module '%s'.", mod_id);
61 g_hash_table_remove(mod_args, "sigrok_key");
62 if ((options = sr_input_options_get(imod))) {
63 mod_opts = generic_arg_to_opt(options, mod_args);
64 sr_output_options_free(options);
65 } else
66 mod_opts = NULL;
67 if (!(in = sr_input_new(imod, mod_opts)))
68 g_critical("Error: failed to initialize input module.");
69 if (mod_opts)
70 g_hash_table_destroy(mod_opts);
71 if (mod_args)
72 g_hash_table_destroy(mod_args);
73 if ((fd = open(opt_input_file, O_RDONLY)) == -1)
74 g_critical("Failed to load %s: %s.", opt_input_file,
75 strerror(errno));
76 } else {
Bert Vermeulen47f97412014-08-20 01:15:36 +020077 if (strcmp(opt_input_file, "-")) {
78 /*
79 * An actual filename: let the input modules try to
80 * identify the file.
81 */
Bert Vermeulenb66260b2014-08-28 01:50:49 +020082 if (sr_input_scan_file(opt_input_file, &in) == SR_OK) {
83 /* That worked, reopen the file for reading. */
84 fd = open(opt_input_file, O_RDONLY);
85 }
Bert Vermeulen47f97412014-08-20 01:15:36 +020086 } else {
87 /*
88 * Taking input from a pipe: let the input modules try
89 * to identify the stream content.
90 */
91 if (!strcmp(opt_input_file, "-")) {
92 /* stdin */
93 fd = 0;
94 } else {
95 if ((fd = open(opt_input_file, O_RDONLY)) == -1)
96 g_critical("Failed to load %s: %s.", opt_input_file,
97 strerror(errno));
98 }
99 if ((len = read(fd, buf->str, BUFSIZE)) < 1)
100 g_critical("Failed to read %s: %s.", opt_input_file,
101 strerror(errno));
102 buf->len = len;
Bert Vermeulenb66260b2014-08-28 01:50:49 +0200103 sr_input_scan_buffer(buf, &in);
Bert Vermeulen47f97412014-08-20 01:15:36 +0200104 }
105 if (!in)
Bert Vermeulena8b40412014-08-10 16:58:36 +0200106 g_critical("Error: no input module found for this file.");
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100107 }
Bert Vermeulena8b40412014-08-10 16:58:36 +0200108 sdi = sr_input_dev_inst_get(in);
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100109
Bert Vermeulena8b40412014-08-10 16:58:36 +0200110 if (select_channels(sdi) != SR_OK)
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100111 return;
112
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200113 sr_session_new(&session);
114 sr_session_datafeed_callback_add(session, &datafeed_in, NULL);
Bert Vermeulena8b40412014-08-10 16:58:36 +0200115 if (sr_session_dev_add(session, sdi) != SR_OK) {
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100116 g_critical("Failed to use device.");
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200117 sr_session_destroy(session);
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100118 return;
119 }
120
Bert Vermeulena8b40412014-08-10 16:58:36 +0200121 while(TRUE) {
122 g_string_truncate(buf, 0);
123 len = read(fd, buf->str, BUFSIZE);
124 if (len < 0)
125 g_critical("Read failed: %s", strerror(errno));
126 buf->len = len;
127 if (sr_input_send(in, buf) != SR_OK)
128 break;
129 if (len < BUFSIZE)
130 break;
131 }
132 sr_input_free(in);
133 g_string_free(buf, TRUE);
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100134
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200135 sr_session_destroy(session);
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100136
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100137}
138
139void load_input_file(void)
140{
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200141 struct sr_session *session;
Daniel Elstnere1ec80f2014-02-21 00:34:12 +0100142 struct sr_dev_inst *sdi;
Bert Vermeulena8b40412014-08-10 16:58:36 +0200143 GSList *devices;
Daniel Elstnere1ec80f2014-02-21 00:34:12 +0100144 int ret;
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100145
Bert Vermeulen11c747b2014-08-29 23:02:19 +0200146 if (strcmp(opt_input_file, "-") && sr_session_load(opt_input_file, &session) == SR_OK) {
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100147 /* sigrok session file */
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200148 ret = sr_session_dev_list(session, &devices);
149 if (ret != SR_OK || !devices->data) {
Daniel Elstnere1ec80f2014-02-21 00:34:12 +0100150 g_critical("Failed to access session device.");
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200151 sr_session_destroy(session);
Daniel Elstnere1ec80f2014-02-21 00:34:12 +0100152 return;
153 }
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200154 sdi = devices->data;
Uwe Hermann029d73f2014-03-24 22:32:47 +0100155 if (select_channels(sdi) != SR_OK) {
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200156 sr_session_destroy(session);
Daniel Elstnere1ec80f2014-02-21 00:34:12 +0100157 return;
158 }
Bert Vermeulen4bf77ec2014-07-17 22:49:20 +0200159 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
160 sr_session_start(session);
161 sr_session_run(session);
162 sr_session_stop(session);
Bert Vermeulen11c747b2014-08-29 23:02:19 +0200163 } else {
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100164 /* fall back on input modules */
Bert Vermeulena8b40412014-08-10 16:58:36 +0200165 load_input_file_module();
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100166 }
167}