2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
26 #include "sigrok-cli.h"
28 char **parse_probestring(int max_probes
, const char *probestring
)
31 char **tokens
, **range
, **probelist
, *name
, str
[8];
36 probelist
= g_malloc0(max_probes
* sizeof(char *));
37 tokens
= g_strsplit(probestring
, ",", max_probes
);
39 for (i
= 0; tokens
[i
]; i
++) {
40 if (strchr(tokens
[i
], '-')) {
41 /* A range of probes in the form 1-5. */
42 range
= g_strsplit(tokens
[i
], "-", 2);
43 if (!range
[0] || !range
[1] || range
[2]) {
44 /* Need exactly two arguments. */
45 printf("Invalid probe syntax '%s'.\n",
51 b
= strtol(range
[0], NULL
, 10);
52 e
= strtol(range
[1], NULL
, 10);
53 if (b
< 1 || e
> max_probes
|| b
>= e
) {
54 printf("Invalid probe range '%s'.\n",
61 snprintf(str
, 7, "%d", b
);
62 probelist
[b
- 1] = g_strdup(str
);
66 tmp
= strtol(tokens
[i
], NULL
, 10);
67 if (tmp
< 1 || tmp
> max_probes
) {
68 printf("Invalid probe %d.\n", tmp
);
73 if ((name
= strchr(tokens
[i
], '='))) {
74 probelist
[tmp
- 1] = g_strdup(++name
);
75 if (strlen(probelist
[tmp
- 1]) > SR_MAX_PROBENAME_LEN
)
76 probelist
[tmp
- 1][SR_MAX_PROBENAME_LEN
] = 0;
78 snprintf(str
, 7, "%d", tmp
);
79 probelist
[tmp
- 1] = g_strdup(str
);
85 for (i
= 0; i
< max_probes
; i
++)
99 GHashTable
*parse_generic_arg(const char *arg
)
108 hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
109 elements
= g_strsplit(arg
, ":", 0);
110 g_hash_table_insert(hash
, g_strdup("sigrok_key"), g_strdup(elements
[0]));
111 for (i
= 1; elements
[i
]; i
++) {
112 e
= strchr(elements
[i
], '=');
114 g_hash_table_insert(hash
, g_strdup(elements
[i
]), NULL
);
117 g_hash_table_insert(hash
, g_strdup(elements
[i
]), g_strdup(e
));
120 g_strfreev(elements
);
125 struct sr_device
*parse_devicestring(const char *devicestring
)
127 struct sr_device
*device
, *d
;
128 struct sr_device_plugin
*plugin
;
129 GSList
*devices
, *plugins
, *l
, *p
;
130 int num_devices
, device_num
, device_cnt
;
137 device_num
= strtol(devicestring
, &tmp
, 10);
138 if (tmp
!= devicestring
) {
139 /* argument is numeric, meaning a device ID. Make all driver
140 * plugins scan for devices.
142 num_devices
= num_real_devices();
143 if (device_num
< 0 || device_num
>= num_devices
)
147 devices
= sr_device_list();
148 for (l
= devices
; l
; l
= l
->next
) {
150 if (strstr(d
->plugin
->name
, "demo"))
152 if (device_cnt
== device_num
) {
153 if (device_num
== device_cnt
) {
161 /* select device by driver -- only initialize that driver,
162 * no need to let them all scan
165 plugins
= sr_list_hwplugins();
166 for (p
= plugins
; p
; p
= p
->next
) {
168 if (strcmp(plugin
->name
, devicestring
))
170 num_devices
= sr_init_hwplugins(plugin
);
171 if (num_devices
== 1) {
172 devices
= sr_device_list();
173 device
= devices
->data
;
174 } else if (num_devices
> 1) {
175 printf("driver '%s' found %d devices, select by ID instead.\n",
176 devicestring
, num_devices
);
178 /* fall through: selected driver found no devices */