2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 * Convert a numeric value value to its "natural" string representation.
31 * E.g. a value of 3000000, with units set to "W", would be converted
32 * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW".
34 * @param x The value to convert.
35 * @param unit The unit to append to the string, or NULL if the string
38 * @return A g_try_malloc()ed string representation of the samplerate value,
39 * or NULL upon errors. The caller is responsible to g_free() the
42 SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
47 if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) == 0)) {
48 return g_strdup_printf("%" PRIu64 " G%s", x / SR_GHZ(1), unit);
49 } else if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) != 0)) {
50 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " G%s",
51 x / SR_GHZ(1), x % SR_GHZ(1), unit);
52 } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) == 0)) {
53 return g_strdup_printf("%" PRIu64 " M%s",
55 } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) != 0)) {
56 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " M%s",
57 x / SR_MHZ(1), x % SR_MHZ(1), unit);
58 } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) == 0)) {
59 return g_strdup_printf("%" PRIu64 " k%s",
61 } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) != 0)) {
62 return g_strdup_printf("%" PRIu64 ".%" PRIu64 " k%s",
63 x / SR_KHZ(1), x % SR_KHZ(1), unit);
65 return g_strdup_printf("%" PRIu64 " %s", x, unit);
68 sr_err("strutil: %s: Error creating SI units string.",
74 * Convert a numeric samplerate value to its "natural" string representation.
76 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz",
77 * 31500 would become "31.5 kHz".
79 * @param samplerate The samplerate in Hz.
81 * @return A g_try_malloc()ed string representation of the samplerate value,
82 * or NULL upon errors. The caller is responsible to g_free() the
85 SR_API char *sr_samplerate_string(uint64_t samplerate)
87 return sr_si_string_u64(samplerate, "Hz");
91 * Convert a numeric frequency value to the "natural" string representation
94 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
96 * @param frequency The frequency in Hz.
98 * @return A g_try_malloc()ed string representation of the frequency value,
99 * or NULL upon errors. The caller is responsible to g_free() the
102 SR_API char *sr_period_string(uint64_t frequency)
107 /* Allocate enough for a uint64_t as string + " ms". */
108 if (!(o = g_try_malloc0(30 + 1))) {
109 sr_err("strutil: %s: o malloc failed", __func__);
113 if (frequency >= SR_GHZ(1))
114 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
115 else if (frequency >= SR_MHZ(1))
116 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
117 else if (frequency >= SR_KHZ(1))
118 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
120 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
123 /* Something went wrong... */
132 * Convert a numeric frequency value to the "natural" string representation
133 * of its voltage value.
135 * E.g. a value of 300000 would be converted to "300mV", 2 to "2V".
137 * @param voltage The voltage represented as a rational number, with the
138 * denominator a divisor of 1V.
140 * @return A g_try_malloc()ed string representation of the voltage value,
141 * or NULL upon errors. The caller is responsible to g_free() the
144 SR_API char *sr_voltage_string(struct sr_rational *voltage)
149 if (!(o = g_try_malloc0(30 + 1))) {
150 sr_err("strutil: %s: o malloc failed", __func__);
154 if (voltage->q == 1000)
155 r = snprintf(o, 30, "%" PRIu64 "mV", voltage->p);
156 else if (voltage->q == 1)
157 r = snprintf(o, 30, "%" PRIu64 "V", voltage->p);
162 /* Something went wrong... */
171 * Parse a trigger specification string.
173 * @param dev The device for which the trigger specification is intended.
174 * @param triggerstring The string containing the trigger specification for
175 * one or more probes of this device. Entries for multiple probes are
176 * comma-separated. Triggers are specified in the form key=value,
177 * where the key is a probe number (or probe name) and the value is
178 * the requested trigger type. Valid trigger types currently
179 * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value
180 * change), '0' (low value), or '1' (high value).
181 * Example: "1=r,sck=f,miso=0,7=c"
183 * @return Pointer to a list of trigger types (strings), or NULL upon errors.
184 * The pointer list (if non-NULL) has as many entries as the
185 * respective device has probes (all physically available probes,
186 * not just enabled ones). Entries of the list which don't have
187 * a trigger value set in 'triggerstring' are NULL, the other entries
188 * contain the respective trigger type which is requested for the
189 * respective probe (e.g. "r", "c", and so on).
191 SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi,
192 const char *triggerstring)
195 struct sr_probe *probe;
196 int max_probes, probenum, i;
197 char **tokens, **triggerlist, *trigger, *tc;
198 const char *trigger_types;
201 max_probes = g_slist_length(sdi->probes);
204 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
205 sr_err("strutil: %s: triggerlist malloc failed", __func__);
209 if (sdi->driver->info_get(SR_DI_TRIGGER_TYPES,
210 (const void **)&trigger_types, sdi) != SR_OK) {
211 sr_err("strutil: %s: Device doesn't support any triggers.", __func__);
215 tokens = g_strsplit(triggerstring, ",", max_probes);
216 for (i = 0; tokens[i]; i++) {
217 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
220 for (l = sdi->probes; l; l = l->next) {
221 probe = (struct sr_probe *)l->data;
223 && !strncmp(probe->name, tokens[i],
224 strlen(probe->name))) {
225 probenum = probe->index;
230 probenum = strtol(tokens[i], NULL, 10);
233 if (probenum < 0 || probenum >= max_probes) {
234 sr_err("strutil: Invalid probe (%d).", probenum);
239 if ((trigger = strchr(tokens[i], '='))) {
240 for (tc = ++trigger; *tc; tc++) {
241 if (strchr(trigger_types, *tc) == NULL) {
242 sr_err("strutil: Unsupported trigger "
249 triggerlist[probenum] = g_strdup(trigger);
255 for (i = 0; i < max_probes; i++)
256 g_free(triggerlist[i]);
265 * Convert a "natural" string representation of a size value to uint64_t.
267 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
268 * of "15M" would be converted to 15000000.
270 * Value representations other than decimal (such as hex or octal) are not
271 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
272 * Spaces (but not other whitespace) between value and suffix are allowed.
274 * @param sizestring A string containing a (decimal) size value.
275 * @param size Pointer to uint64_t which will contain the string's size value.
277 * @return SR_OK upon success, SR_ERR upon errors.
279 SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
281 int multiplier, done;
284 *size = strtoull(sizestring, &s, 10);
287 while (s && *s && multiplier == 0 && !done) {
293 multiplier = SR_KHZ(1);
297 multiplier = SR_MHZ(1);
301 multiplier = SR_GHZ(1);
312 if (*s && strcasecmp(s, "Hz"))
319 * Convert a "natural" string representation of a time value to an
320 * uint64_t value in milliseconds.
322 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
323 * of "15ms" would be converted to 15.
325 * Value representations other than decimal (such as hex or octal) are not
326 * supported. Only lower-case "s" and "ms" time suffixes are supported.
327 * Spaces (but not other whitespace) between value and suffix are allowed.
329 * @param timestring A string containing a (decimal) time value.
330 * @return The string's time value as uint64_t, in milliseconds.
332 * TODO: Error handling.
333 * TODO: Add support for "m" (minutes) and others.
335 * TODO: Allow both lower-case and upper-case.
337 SR_API uint64_t sr_parse_timestring(const char *timestring)
342 time_msec = strtoull(timestring, &s, 10);
343 if (time_msec == 0 && s == timestring)
351 else if (!strcmp(s, "ms"))
360 SR_API gboolean sr_parse_boolstring(const char *boolstr)
365 if (!g_ascii_strncasecmp(boolstr, "true", 4) ||
366 !g_ascii_strncasecmp(boolstr, "yes", 3) ||
367 !g_ascii_strncasecmp(boolstr, "on", 2) ||
368 !g_ascii_strncasecmp(boolstr, "1", 1))
374 SR_API int sr_parse_period(const char *periodstr, struct sr_rational *r)
378 r->p = strtoull(periodstr, &s, 10);
379 if (r->p == 0 && s == periodstr)
380 /* No digits found. */
386 if (!strcmp(s, "ns"))
388 else if (!strcmp(s, "us"))
390 else if (!strcmp(s, "ms"))
392 else if (!strcmp(s, "s"))
395 /* Must have a time suffix. */
403 SR_API int sr_parse_voltage(const char *voltstr, struct sr_rational *r)
407 r->p = strtoull(voltstr, &s, 10);
408 if (r->p == 0 && s == voltstr)
409 /* No digits found. */
415 if (!strcasecmp(s, "mv"))
417 else if (!strcasecmp(s, "v"))
420 /* Must have a base suffix. */