]> sigrok.org Git - sigrok-cli.git/blame_incremental - main.c
Properly handle saving logic data packets of any size.
[sigrok-cli.git] / main.c
... / ...
CommitLineData
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 "sigrok-cli.h"
21#include <stdlib.h>
22#include <glib.h>
23
24struct sr_context *sr_ctx = NULL;
25#ifdef HAVE_SRD
26struct srd_session *srd_sess = NULL;
27#endif
28
29static 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
50int select_channels(struct sr_dev_inst *sdi)
51{
52 struct sr_channel *ch;
53 GSList *selected_channels, *l;
54
55 if (opt_channels) {
56 if (!(selected_channels = parse_channelstring(sdi, opt_channels)))
57 return SR_ERR;
58
59 for (l = sdi->channels; l; l = l->next) {
60 ch = l->data;
61 if (g_slist_find(selected_channels, ch))
62 ch->enabled = TRUE;
63 else
64 ch->enabled = FALSE;
65 }
66 g_slist_free(selected_channels);
67 }
68#ifdef HAVE_SRD
69 map_pd_channels(sdi);
70#endif
71 return SR_OK;
72}
73
74static void set_options(void)
75{
76 struct sr_dev_inst *sdi;
77 GSList *devices;
78 GHashTable *devargs;
79
80 if (!opt_config) {
81 g_critical("No setting specified.");
82 return;
83 }
84
85 if (!(devargs = parse_generic_arg(opt_config, FALSE)))
86 return;
87
88 if (!(devices = device_scan())) {
89 g_critical("No devices found.");
90 return;
91 }
92 sdi = devices->data;
93
94 if (sr_dev_open(sdi) != SR_OK) {
95 g_critical("Failed to open device.");
96 return;
97 }
98
99 set_dev_options(sdi, devargs);
100
101 sr_dev_close(sdi);
102 g_slist_free(devices);
103 g_hash_table_destroy(devargs);
104
105}
106
107int main(int argc, char **argv)
108{
109 g_log_set_default_handler(logger, NULL);
110
111 if (parse_options(argc, argv)) {
112 return 1;
113 }
114
115 /* Set the loglevel (amount of messages to output) for libsigrok. */
116 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
117 goto done;
118
119 if (sr_init(&sr_ctx) != SR_OK)
120 goto done;
121
122#ifdef HAVE_SRD
123 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
124 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
125 goto done;
126
127 if (opt_pds) {
128 if (srd_init(NULL) != SRD_OK)
129 goto done;
130 if (srd_session_new(&srd_sess) != SRD_OK) {
131 g_critical("Failed to create new decode session.");
132 goto done;
133 }
134 if (register_pds(opt_pds, opt_pd_annotations) != 0)
135 goto done;
136 if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0)
137 goto done;
138
139 /* Only one output type is ever shown. */
140 if (opt_pd_binary) {
141 if (setup_pd_binary(opt_pd_binary) != 0)
142 goto done;
143 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY,
144 show_pd_binary, NULL) != SRD_OK)
145 goto done;
146 } else if (opt_pd_meta) {
147 if (setup_pd_meta(opt_pd_meta) != 0)
148 goto done;
149 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META,
150 show_pd_meta, NULL) != SRD_OK)
151 goto done;
152 } else {
153 if (opt_pd_annotations)
154 if (setup_pd_annotations(opt_pd_annotations) != 0)
155 goto done;
156 if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN,
157 show_pd_annotations, NULL) != SRD_OK)
158 goto done;
159 }
160 }
161#endif
162
163 if (opt_version)
164 show_version();
165 else if (opt_scan_devs)
166 show_dev_list();
167#ifdef HAVE_SRD
168 else if (opt_pds && opt_show)
169 show_pd_detail();
170#endif
171 else if (opt_show)
172 show_dev_detail();
173 else if (opt_input_file)
174 load_input_file();
175 else if (opt_set)
176 set_options();
177 else if (opt_samples || opt_time || opt_frames || opt_continuous)
178 run_session();
179 else
180 show_help();
181
182#ifdef HAVE_SRD
183 if (opt_pds)
184 srd_exit();
185#endif
186
187done:
188 if (sr_ctx)
189 sr_exit(sr_ctx);
190
191 return 0;
192}