blob: a5749529e82859c00f38746a8d4d1cea9afaf597 [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"
Dan HorĂ¡k8d52f782014-01-25 14:15:24 +010021#include <stdlib.h>
Bert Vermeulen2be182e2013-11-17 13:15:38 +010022#include <glib.h>
Bert Vermeulen2be182e2013-11-17 13:15:38 +010023
24struct sr_context *sr_ctx = NULL;
25#ifdef HAVE_SRD
26struct srd_session *srd_sess = NULL;
27#endif
28
Bert Vermeulen2be182e2013-11-17 13:15:38 +010029static void logger(const gchar *log_domain, GLogLevelFlags log_level,
30 const gchar *message, gpointer cb_data)
31{
32 (void)log_domain;
33 (void)cb_data;
34
35 /*
36 * All messages, warnings, errors etc. go to stderr (not stdout) in
37 * order to not mess up the CLI tool data output, e.g. VCD output.
38 */
39 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
40 || opt_loglevel > SR_LOG_WARN) {
41 fprintf(stderr, "%s\n", message);
42 fflush(stderr);
43 }
44
45 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL))
46 exit(1);
47
48}
49
Uwe Hermann029d73f2014-03-24 22:32:47 +010050int select_channels(struct sr_dev_inst *sdi)
Bert Vermeulen2be182e2013-11-17 13:15:38 +010051{
Uwe Hermann029d73f2014-03-24 22:32:47 +010052 struct sr_channel *ch;
53 GSList *selected_channels, *l;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010054
Uwe Hermann3dfbfbc2014-04-12 13:42:30 +020055 if (opt_channels) {
56 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
Daniel Elstner39aedc32014-02-21 20:10:09 +010057 return SR_ERR;
Bert Vermeulen2be182e2013-11-17 13:15:38 +010058
Uwe Hermann029d73f2014-03-24 22:32:47 +010059 for (l = sdi->channels; l; l = l->next) {
60 ch = l->data;
61 if (g_slist_find(selected_channels, ch))
62 ch->enabled = TRUE;
Daniel Elstner39aedc32014-02-21 20:10:09 +010063 else
Uwe Hermann029d73f2014-03-24 22:32:47 +010064 ch->enabled = FALSE;
Daniel Elstner39aedc32014-02-21 20:10:09 +010065 }
Uwe Hermann029d73f2014-03-24 22:32:47 +010066 g_slist_free(selected_channels);
Bert Vermeulen2be182e2013-11-17 13:15:38 +010067 }
Daniel Elstner39aedc32014-02-21 20:10:09 +010068#ifdef HAVE_SRD
Uwe Hermann3dfbfbc2014-04-12 13:42:30 +020069 map_pd_channels(sdi);
Daniel Elstner39aedc32014-02-21 20:10:09 +010070#endif
Bert Vermeulen2be182e2013-11-17 13:15:38 +010071 return SR_OK;
72}
73
Bert Vermeulen62a64762014-09-10 17:57:01 +020074static void get_option(void)
75{
76 struct sr_dev_inst *sdi;
77 struct sr_channel_group *cg;
78 const struct sr_config_info *ci;
79 GSList *devices;
80 GVariant *gvar;
81 int ret;
82 char *s;
83
84 if (!(devices = device_scan())) {
85 g_critical("No devices found.");
86 return;
87 }
88 sdi = devices->data;
89 g_slist_free(devices);
90
91 if (sr_dev_open(sdi) != SR_OK) {
92 g_critical("Failed to open device.");
93 return;
94 }
95
96 cg = select_channel_group(sdi);
97 if (!(ci = sr_config_info_name_get(opt_get)))
98 g_critical("Unknown option '%s'", opt_get);
99
100 if ((ret = sr_config_get(sdi->driver, sdi, cg, ci->key, &gvar)) != SR_OK)
101 g_critical("Failed to get '%s': %s", opt_get, sr_strerror(ret));
102 s = g_variant_print(gvar, FALSE);
103 printf("%s\n", s);
104 g_free(s);
105
106 g_variant_unref(gvar);
107 sr_dev_close(sdi);
108}
109
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100110static void set_options(void)
111{
112 struct sr_dev_inst *sdi;
113 GSList *devices;
114 GHashTable *devargs;
115
116 if (!opt_config) {
117 g_critical("No setting specified.");
118 return;
119 }
120
121 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
122 return;
123
124 if (!(devices = device_scan())) {
125 g_critical("No devices found.");
126 return;
127 }
128 sdi = devices->data;
Bert Vermeulenb4eece72014-07-24 20:31:55 +0200129 g_slist_free(devices);
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100130
131 if (sr_dev_open(sdi) != SR_OK) {
132 g_critical("Failed to open device.");
133 return;
134 }
135
136 set_dev_options(sdi, devargs);
137
138 sr_dev_close(sdi);
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100139 g_hash_table_destroy(devargs);
140
141}
142
143int main(int argc, char **argv)
144{
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100145 g_log_set_default_handler(logger, NULL);
146
Jens Steinhausercd62e022014-07-15 17:14:03 +0200147 if (parse_options(argc, argv)) {
148 return 1;
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100149 }
150
151 /* Set the loglevel (amount of messages to output) for libsigrok. */
152 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
153 goto done;
154
155 if (sr_init(&sr_ctx) != SR_OK)
156 goto done;
157
158#ifdef HAVE_SRD
159 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
160 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
161 goto done;
162
163 if (opt_pds) {
164 if (srd_init(NULL) != SRD_OK)
165 goto done;
166 if (srd_session_new(&srd_sess) != SRD_OK) {
167 g_critical("Failed to create new decode session.");
168 goto done;
169 }
Bert Vermeulen26c63752013-11-17 18:51:15 +0100170 if (register_pds(opt_pds, opt_pd_annotations) != 0)
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100171 goto done;
Bert Vermeulen26c63752013-11-17 18:51:15 +0100172 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100173 goto done;
174
175 /* Only one output type is ever shown. */
176 if (opt_pd_binary) {
Bert Vermeulen26c63752013-11-17 18:51:15 +0100177 if (setup_pd_binary(opt_pd_binary) != 0)
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100178 goto done;
179 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
180 show_pd_binary, NULL) != SRD_OK)
181 goto done;
182 } else if (opt_pd_meta) {
Bert Vermeulen26c63752013-11-17 18:51:15 +0100183 if (setup_pd_meta(opt_pd_meta) != 0)
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100184 goto done;
185 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
186 show_pd_meta, NULL) != SRD_OK)
187 goto done;
188 } else {
189 if (opt_pd_annotations)
Bert Vermeulen26c63752013-11-17 18:51:15 +0100190 if (setup_pd_annotations(opt_pd_annotations) != 0)
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100191 goto done;
192 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
193 show_pd_annotations, NULL) != SRD_OK)
194 goto done;
195 }
196 }
197#endif
198
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100199 if (opt_version)
200 show_version();
Bert Vermeulena8b40412014-08-10 16:58:36 +0200201 else if (opt_input_format && opt_show)
202 show_input();
Bert Vermeulenad6520c2014-07-25 22:39:48 +0200203 else if (opt_output_format && opt_show)
204 show_output();
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100205 else if (opt_scan_devs)
206 show_dev_list();
207#ifdef HAVE_SRD
208 else if (opt_pds && opt_show)
209 show_pd_detail();
210#endif
211 else if (opt_show)
212 show_dev_detail();
213 else if (opt_input_file)
214 load_input_file();
Bert Vermeulen62a64762014-09-10 17:57:01 +0200215 else if (opt_get)
216 get_option();
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100217 else if (opt_set)
218 set_options();
219 else if (opt_samples || opt_time || opt_frames || opt_continuous)
220 run_session();
Jens Steinhausercd62e022014-07-15 17:14:03 +0200221 else
222 show_help();
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100223
224#ifdef HAVE_SRD
225 if (opt_pds)
226 srd_exit();
227#endif
228
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100229done:
230 if (sr_ctx)
231 sr_exit(sr_ctx);
232
Jens Steinhausercd62e022014-07-15 17:14:03 +0200233 return 0;
Bert Vermeulen2be182e2013-11-17 13:15:38 +0100234}