]> sigrok.org Git - libsigrok.git/blame - strutil.c
sr: session_driver: Remove source on completion
[libsigrok.git] / strutil.c
CommitLineData
25e7d9b1
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
45c59c8b
BV
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
25e7d9b1
UH
26
27/**
28 * Convert a numeric samplerate value to its "natural" string representation.
29 *
c69e35a7
UH
30 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
31 * 31500 would become "31.5 kHz".
25e7d9b1
UH
32 *
33 * @param samplerate The samplerate in Hz.
44dae539 34 *
133a37bf
UH
35 * @return A g_try_malloc()ed string representation of the samplerate value,
36 * or NULL upon errors. The caller is responsible to g_free() the
37 * memory.
25e7d9b1 38 */
1a081ca6 39SR_API char *sr_samplerate_string(uint64_t samplerate)
25e7d9b1
UH
40{
41 char *o;
c69e35a7 42 uint64_t s = samplerate;
25e7d9b1 43
c69e35a7
UH
44 if ((s >= SR_GHZ(1)) && (s % SR_GHZ(1) == 0)) {
45 o = g_strdup_printf("%" PRIu64 " GHz", s / SR_GHZ(1));
46 } else if ((s >= SR_GHZ(1)) && (s % SR_GHZ(1) != 0)) {
47 o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " GHz",
48 s / SR_GHZ(1), s % SR_GHZ(1));
49 } else if ((s >= SR_MHZ(1)) && (s % SR_MHZ(1) == 0)) {
50 o = g_strdup_printf("%" PRIu64 " MHz", s / SR_MHZ(1));
51 } else if ((s >= SR_MHZ(1)) && (s % SR_MHZ(1) != 0)) {
52 o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " MHz",
53 s / SR_MHZ(1), s % SR_MHZ(1));
54 } else if ((s >= SR_KHZ(1)) && (s % SR_KHZ(1) == 0)) {
55 o = g_strdup_printf("%" PRIu64 " kHz", s / SR_KHZ(1));
56 } else if ((s >= SR_KHZ(1)) && (s % SR_KHZ(1) != 0)) {
57 o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " kHz",
58 s / SR_KHZ(1), s % SR_KHZ(1));
59 } else {
60 o = g_strdup_printf("%" PRIu64 " Hz", s);
133a37bf 61 }
25e7d9b1 62
c69e35a7
UH
63 if (!o) {
64 sr_err("strutil: %s: Error creating samplerate string.",
65 __func__);
25e7d9b1
UH
66 return NULL;
67 }
68
69 return o;
70}
2a3f9541 71
2a3f9541 72/**
dfcc0bf9 73 * Convert a numeric frequency value to the "natural" string representation
2a3f9541
BV
74 * of its period.
75 *
2507648e 76 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
2a3f9541
BV
77 *
78 * @param frequency The frequency in Hz.
44dae539 79 *
133a37bf
UH
80 * @return A g_try_malloc()ed string representation of the frequency value,
81 * or NULL upon errors. The caller is responsible to g_free() the
82 * memory.
2a3f9541 83 */
1a081ca6 84SR_API char *sr_period_string(uint64_t frequency)
2a3f9541
BV
85{
86 char *o;
87 int r;
88
133a37bf
UH
89 /* Allocate enough for a uint64_t as string + " ms". */
90 if (!(o = g_try_malloc0(30 + 1))) {
91 sr_err("strutil: %s: o malloc failed", __func__);
2a3f9541 92 return NULL;
133a37bf 93 }
2a3f9541 94
59df0c77 95 if (frequency >= SR_GHZ(1))
2a3f9541 96 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
59df0c77 97 else if (frequency >= SR_MHZ(1))
2507648e 98 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
59df0c77 99 else if (frequency >= SR_KHZ(1))
2a3f9541
BV
100 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
101 else
102 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
103
104 if (r < 0) {
105 /* Something went wrong... */
133a37bf 106 g_free(o);
2a3f9541
BV
107 return NULL;
108 }
109
110 return o;
111}
40f5ddac 112
79afc8ca
BV
113/**
114 * Convert a numeric frequency value to the "natural" string representation
115 * of its voltage value.
116 *
117 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
118 *
119 * @param voltage The voltage represented as a rational number, with the
120 * denominator a divisor of 1V.
121 *
122 * @return A g_try_malloc()ed string representation of the voltage value,
123 * or NULL upon errors. The caller is responsible to g_free() the
124 * memory.
125 */
126SR_API char *sr_voltage_string(struct sr_rational *voltage)
127{
128 char *o;
129 int r;
130
131 if (!(o = g_try_malloc0(30 + 1))) {
132 sr_err("strutil: %s: o malloc failed", __func__);
133 return NULL;
134 }
135
136 if (voltage->q == 1000)
137 r = snprintf(o, 30, "%" PRIu64 "mV", voltage->p);
138 else if (voltage->q == 1)
139 r = snprintf(o, 30, "%" PRIu64 "V", voltage->p);
140 else
141 r = -1;
142
143 if (r < 0) {
144 /* Something went wrong... */
145 g_free(o);
146 return NULL;
147 }
148
149 return o;
150}
151
dfcc0bf9 152/**
bf978d35 153 * Parse a trigger specification string.
dfcc0bf9 154 *
bf978d35
UH
155 * @param dev The device for which the trigger specification is intended.
156 * @param triggerstring The string containing the trigger specification for
157 * one or more probes of this device. Entries for multiple probes are
158 * comma-separated. Triggers are specified in the form key=value,
159 * where the key is a probe number (or probe name) and the value is
160 * the requested trigger type. Valid trigger types currently
161 * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value
162 * change), '0' (low value), or '1' (high value).
163 * Example: "1=r,sck=f,miso=0,7=c"
44dae539 164 *
bf978d35
UH
165 * @return Pointer to a list of trigger types (strings), or NULL upon errors.
166 * The pointer list (if non-NULL) has as many entries as the
167 * respective device has probes (all physically available probes,
168 * not just enabled ones). Entries of the list which don't have
169 * a trigger value set in 'triggerstring' are NULL, the other entries
170 * contain the respective trigger type which is requested for the
171 * respective probe (e.g. "r", "c", and so on).
dfcc0bf9 172 */
92ae7984 173SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi,
1a081ca6 174 const char *triggerstring)
40f5ddac
BV
175{
176 GSList *l;
1afe8989 177 struct sr_probe *probe;
40f5ddac 178 int max_probes, probenum, i;
b7f578be
JH
179 char **tokens, **triggerlist, *trigger, *tc;
180 const char *trigger_types;
40f5ddac
BV
181 gboolean error;
182
92ae7984 183 max_probes = g_slist_length(sdi->probes);
40f5ddac 184 error = FALSE;
b53738ba
UH
185
186 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
bf978d35 187 sr_err("strutil: %s: triggerlist malloc failed", __func__);
b53738ba
UH
188 return NULL;
189 }
190
92ae7984
BV
191 if (sdi->driver->info_get(SR_DI_TRIGGER_TYPES,
192 (const void **)&trigger_types, sdi) != SR_OK) {
193 sr_err("strutil: %s: Device doesn't support any triggers.", __func__);
40f5ddac 194 return NULL;
bf978d35 195 }
40f5ddac 196
31fc1fbc 197 tokens = g_strsplit(triggerstring, ",", max_probes);
40f5ddac
BV
198 for (i = 0; tokens[i]; i++) {
199 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
200 /* Named probe */
201 probenum = 0;
92ae7984 202 for (l = sdi->probes; l; l = l->next) {
1afe8989 203 probe = (struct sr_probe *)l->data;
40f5ddac
BV
204 if (probe->enabled
205 && !strncmp(probe->name, tokens[i],
206 strlen(probe->name))) {
207 probenum = probe->index;
208 break;
209 }
210 }
211 } else {
212 probenum = strtol(tokens[i], NULL, 10);
213 }
214
31fc1fbc 215 if (probenum < 0 || probenum >= max_probes) {
0aeb0ccd 216 sr_err("strutil: Invalid probe (%d).", probenum);
40f5ddac
BV
217 error = TRUE;
218 break;
219 }
220
221 if ((trigger = strchr(tokens[i], '='))) {
222 for (tc = ++trigger; *tc; tc++) {
223 if (strchr(trigger_types, *tc) == NULL) {
bf978d35 224 sr_err("strutil: Unsupported trigger "
0aeb0ccd 225 "type '%c'.", *tc);
40f5ddac
BV
226 error = TRUE;
227 break;
228 }
229 }
230 if (!error)
31fc1fbc 231 triggerlist[probenum] = g_strdup(trigger);
40f5ddac
BV
232 }
233 }
234 g_strfreev(tokens);
235
236 if (error) {
237 for (i = 0; i < max_probes; i++)
66410a86 238 g_free(triggerlist[i]);
40f5ddac
BV
239 g_free(triggerlist);
240 triggerlist = NULL;
241 }
242
243 return triggerlist;
244}
245
dfcc0bf9
UH
246/**
247 * Convert a "natural" string representation of a size value to uint64_t.
248 *
249 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
250 * of "15M" would be converted to 15000000.
251 *
252 * Value representations other than decimal (such as hex or octal) are not
253 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
254 * Spaces (but not other whitespace) between value and suffix are allowed.
255 *
256 * @param sizestring A string containing a (decimal) size value.
f64c1414 257 * @param size Pointer to uint64_t which will contain the string's size value.
dfcc0bf9 258 *
44dae539 259 * @return SR_OK upon success, SR_ERR upon errors.
dfcc0bf9 260 */
1a081ca6 261SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
40f5ddac 262{
f64c1414 263 int multiplier, done;
40f5ddac
BV
264 char *s;
265
f64c1414 266 *size = strtoull(sizestring, &s, 10);
40f5ddac 267 multiplier = 0;
f64c1414
BV
268 done = FALSE;
269 while (s && *s && multiplier == 0 && !done) {
40f5ddac
BV
270 switch (*s) {
271 case ' ':
272 break;
273 case 'k':
274 case 'K':
59df0c77 275 multiplier = SR_KHZ(1);
40f5ddac
BV
276 break;
277 case 'm':
278 case 'M':
59df0c77 279 multiplier = SR_MHZ(1);
40f5ddac
BV
280 break;
281 case 'g':
282 case 'G':
59df0c77 283 multiplier = SR_GHZ(1);
40f5ddac
BV
284 break;
285 default:
f64c1414
BV
286 done = TRUE;
287 s--;
40f5ddac
BV
288 }
289 s++;
290 }
291 if (multiplier > 0)
f64c1414 292 *size *= multiplier;
40f5ddac 293
f64c1414
BV
294 if (*s && strcasecmp(s, "Hz"))
295 return SR_ERR;
296
297 return SR_OK;
40f5ddac
BV
298}
299
dfcc0bf9
UH
300/**
301 * Convert a "natural" string representation of a time value to an
302 * uint64_t value in milliseconds.
303 *
304 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
305 * of "15ms" would be converted to 15.
306 *
307 * Value representations other than decimal (such as hex or octal) are not
308 * supported. Only lower-case "s" and "ms" time suffixes are supported.
309 * Spaces (but not other whitespace) between value and suffix are allowed.
310 *
311 * @param timestring A string containing a (decimal) time value.
312 * @return The string's time value as uint64_t, in milliseconds.
313 *
314 * TODO: Error handling.
315 * TODO: Add support for "m" (minutes) and others.
316 * TODO: picoseconds?
317 * TODO: Allow both lower-case and upper-case.
318 */
1a081ca6 319SR_API uint64_t sr_parse_timestring(const char *timestring)
40f5ddac
BV
320{
321 uint64_t time_msec;
322 char *s;
323
324 time_msec = strtoull(timestring, &s, 10);
325 if (time_msec == 0 && s == timestring)
326 return 0;
327
328 if (s && *s) {
329 while (*s == ' ')
330 s++;
331 if (!strcmp(s, "s"))
332 time_msec *= 1000;
333 else if (!strcmp(s, "ms"))
334 ; /* redundant */
335 else
336 return 0;
337 }
338
339 return time_msec;
340}
4d436e71 341
1a081ca6 342SR_API gboolean sr_parse_boolstring(const char *boolstr)
4d436e71
GM
343{
344 if (!boolstr)
345 return FALSE;
346
993526f8
BV
347 if (!g_ascii_strncasecmp(boolstr, "true", 4) ||
348 !g_ascii_strncasecmp(boolstr, "yes", 3) ||
349 !g_ascii_strncasecmp(boolstr, "on", 2) ||
350 !g_ascii_strncasecmp(boolstr, "1", 1))
4d436e71
GM
351 return TRUE;
352
353 return FALSE;
354}
76f4c610
BV
355
356SR_API int sr_parse_period(const char *periodstr, struct sr_rational *r)
357{
358 char *s;
359
360 r->p = strtoull(periodstr, &s, 10);
361 if (r->p == 0 && s == periodstr)
362 /* No digits found. */
363 return SR_ERR_ARG;
364
365 if (s && *s) {
366 while (*s == ' ')
367 s++;
368 if (!strcmp(s, "ns"))
369 r->q = 1000000000L;
370 else if (!strcmp(s, "us"))
371 r->q = 1000000;
372 else if (!strcmp(s, "ms"))
373 r->q = 1000;
374 else if (!strcmp(s, "s"))
375 r->q = 1;
376 else
377 /* Must have a time suffix. */
378 return SR_ERR_ARG;
379 }
380
381 return SR_OK;
382}
383
384
79afc8ca
BV
385SR_API int sr_parse_voltage(const char *voltstr, struct sr_rational *r)
386{
387 char *s;
388
389 r->p = strtoull(voltstr, &s, 10);
390 if (r->p == 0 && s == voltstr)
391 /* No digits found. */
392 return SR_ERR_ARG;
393
394 if (s && *s) {
395 while (*s == ' ')
396 s++;
397 if (!strcasecmp(s, "mv"))
398 r->q = 1000L;
399 else if (!strcasecmp(s, "v"))
400 r->q = 1;
401 else
402 /* Must have a base suffix. */
403 return SR_ERR_ARG;
404 }
405
406 return SR_OK;
407}
408
409