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