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