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
25 #include "sigrok-internal.h"
28 * Convert a numeric samplerate value to its "natural" string representation.
30 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz".
32 * @param samplerate The samplerate in Hz.
33 * @return A malloc()ed string representation of the samplerate value,
34 * or NULL upon errors. The caller is responsible to free() the memory.
36 char *sr_samplerate_string(uint64_t samplerate)
41 o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */
45 if (samplerate >= SR_GHZ(1))
46 r = snprintf(o, 30, "%" PRIu64 " GHz", samplerate / 1000000000);
47 else if (samplerate >= SR_MHZ(1))
48 r = snprintf(o, 30, "%" PRIu64 " MHz", samplerate / 1000000);
49 else if (samplerate >= SR_KHZ(1))
50 r = snprintf(o, 30, "%" PRIu64 " kHz", samplerate / 1000);
52 r = snprintf(o, 30, "%" PRIu64 " Hz", samplerate);
55 /* Something went wrong... */
64 * Convert a numeric frequency value to the "natural" string representation
67 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
69 * @param frequency The frequency in Hz.
70 * @return A malloc()ed string representation of the frequency value,
71 * or NULL upon errors. The caller is responsible to free() the memory.
73 char *sr_period_string(uint64_t frequency)
78 o = malloc(30 + 1); /* Enough for a uint64_t as string + " ms". */
82 if (frequency >= SR_GHZ(1))
83 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
84 else if (frequency >= SR_MHZ(1))
85 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
86 else if (frequency >= SR_KHZ(1))
87 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
89 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
92 /* Something went wrong... */
104 * @param triggerstring TODO
107 char **sr_parse_triggerstring(struct sr_device *device,
108 const char *triggerstring)
111 struct sr_probe *probe;
112 int max_probes, probenum, i;
113 char **tokens, **triggerlist, *trigger, *tc, *trigger_types;
116 max_probes = g_slist_length(device->probes);
119 if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
120 sr_err("session file: %s: metafile malloc failed", __func__);
124 tokens = g_strsplit(triggerstring, ",", max_probes);
125 trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES);
126 if (trigger_types == NULL)
129 for (i = 0; tokens[i]; i++) {
130 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
133 for (l = device->probes; l; l = l->next) {
134 probe = (struct sr_probe *)l->data;
136 && !strncmp(probe->name, tokens[i],
137 strlen(probe->name))) {
138 probenum = probe->index;
143 probenum = strtol(tokens[i], NULL, 10);
146 if (probenum < 1 || probenum > max_probes) {
147 sr_err("Invalid probe.\n");
152 if ((trigger = strchr(tokens[i], '='))) {
153 for (tc = ++trigger; *tc; tc++) {
154 if (strchr(trigger_types, *tc) == NULL) {
155 sr_err("Unsupported trigger type "
162 triggerlist[probenum - 1] = g_strdup(trigger);
168 for (i = 0; i < max_probes; i++)
170 g_free(triggerlist[i]);
179 * Convert a "natural" string representation of a size value to uint64_t.
181 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
182 * of "15M" would be converted to 15000000.
184 * Value representations other than decimal (such as hex or octal) are not
185 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
186 * Spaces (but not other whitespace) between value and suffix are allowed.
188 * @param sizestring A string containing a (decimal) size value.
189 * @param size Pointer to uint64_t which will contain the string's size value.
190 * @return SR_OK or error code
193 int sr_parse_sizestring(const char *sizestring, uint64_t *size)
195 int multiplier, done;
198 *size = strtoull(sizestring, &s, 10);
201 while (s && *s && multiplier == 0 && !done) {
207 multiplier = SR_KHZ(1);
211 multiplier = SR_MHZ(1);
215 multiplier = SR_GHZ(1);
226 if (*s && strcasecmp(s, "Hz"))
233 * Convert a "natural" string representation of a time value to an
234 * uint64_t value in milliseconds.
236 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
237 * of "15ms" would be converted to 15.
239 * Value representations other than decimal (such as hex or octal) are not
240 * supported. Only lower-case "s" and "ms" time suffixes are supported.
241 * Spaces (but not other whitespace) between value and suffix are allowed.
243 * @param timestring A string containing a (decimal) time value.
244 * @return The string's time value as uint64_t, in milliseconds.
246 * TODO: Error handling.
247 * TODO: Add support for "m" (minutes) and others.
249 * TODO: Allow both lower-case and upper-case.
251 uint64_t sr_parse_timestring(const char *timestring)
256 time_msec = strtoull(timestring, &s, 10);
257 if (time_msec == 0 && s == timestring)
265 else if (!strcmp(s, "ms"))
274 gboolean sr_parse_boolstring(const char *boolstr)
279 if (!g_strcasecmp(boolstr, "true") ||
280 !g_strcasecmp(boolstr, "yes") ||
281 !g_strcasecmp(boolstr, "on") ||
282 !g_strcasecmp(boolstr, "1"))