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