]>
Commit | Line | Data |
---|---|---|
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> | |
24 | #include "libsigrok.h" | |
25 | #include "libsigrok-internal.h" | |
26 | ||
27 | /** | |
28 | * Convert a numeric value value to its "natural" string representation. | |
29 | * in SI units | |
30 | * | |
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". | |
33 | * | |
34 | * @param x The value to convert. | |
35 | * @param unit The unit to append to the string, or NULL if the string | |
36 | * has no units. | |
37 | * | |
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 | |
40 | * memory. | |
41 | */ | |
42 | SR_API char *sr_si_string_u64(uint64_t x, const char *unit) | |
43 | { | |
44 | if(unit == NULL) | |
45 | unit = ""; | |
46 | ||
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", | |
54 | x / SR_MHZ(1), unit); | |
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", | |
60 | x / SR_KHZ(1), unit); | |
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); | |
64 | } else { | |
65 | return g_strdup_printf("%" PRIu64 " %s", x, unit); | |
66 | } | |
67 | ||
68 | sr_err("strutil: %s: Error creating SI units string.", | |
69 | __func__); | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | /** | |
74 | * Convert a numeric samplerate value to its "natural" string representation. | |
75 | * | |
76 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", | |
77 | * 31500 would become "31.5 kHz". | |
78 | * | |
79 | * @param samplerate The samplerate in Hz. | |
80 | * | |
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 | |
83 | * memory. | |
84 | */ | |
85 | SR_API char *sr_samplerate_string(uint64_t samplerate) | |
86 | { | |
87 | return sr_si_string_u64(samplerate, "Hz"); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Convert a numeric frequency value to the "natural" string representation | |
92 | * of its period. | |
93 | * | |
94 | * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". | |
95 | * | |
96 | * @param frequency The frequency in Hz. | |
97 | * | |
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 | |
100 | * memory. | |
101 | */ | |
102 | SR_API char *sr_period_string(uint64_t frequency) | |
103 | { | |
104 | char *o; | |
105 | int r; | |
106 | ||
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__); | |
110 | return NULL; | |
111 | } | |
112 | ||
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); | |
119 | else | |
120 | r = snprintf(o, 30, "%" PRIu64 " s", frequency); | |
121 | ||
122 | if (r < 0) { | |
123 | /* Something went wrong... */ | |
124 | g_free(o); | |
125 | return NULL; | |
126 | } | |
127 | ||
128 | return o; | |
129 | } | |
130 | ||
131 | /** | |
132 | * Convert a numeric frequency value to the "natural" string representation | |
133 | * of its voltage value. | |
134 | * | |
135 | * E.g. a value of 300000 would be converted to "300mV", 2 to "2V". | |
136 | * | |
137 | * @param voltage The voltage represented as a rational number, with the | |
138 | * denominator a divisor of 1V. | |
139 | * | |
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 | |
142 | * memory. | |
143 | */ | |
144 | SR_API char *sr_voltage_string(struct sr_rational *voltage) | |
145 | { | |
146 | char *o; | |
147 | int r; | |
148 | ||
149 | if (!(o = g_try_malloc0(30 + 1))) { | |
150 | sr_err("strutil: %s: o malloc failed", __func__); | |
151 | return NULL; | |
152 | } | |
153 | ||
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); | |
158 | else | |
159 | r = -1; | |
160 | ||
161 | if (r < 0) { | |
162 | /* Something went wrong... */ | |
163 | g_free(o); | |
164 | return NULL; | |
165 | } | |
166 | ||
167 | return o; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Parse a trigger specification string. | |
172 | * | |
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" | |
182 | * | |
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). | |
190 | */ | |
191 | SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi, | |
192 | const char *triggerstring) | |
193 | { | |
194 | GSList *l; | |
195 | struct sr_probe *probe; | |
196 | int max_probes, probenum, i; | |
197 | char **tokens, **triggerlist, *trigger, *tc; | |
198 | const char *trigger_types; | |
199 | gboolean error; | |
200 | ||
201 | max_probes = g_slist_length(sdi->probes); | |
202 | error = FALSE; | |
203 | ||
204 | if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) { | |
205 | sr_err("strutil: %s: triggerlist malloc failed", __func__); | |
206 | return NULL; | |
207 | } | |
208 | ||
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__); | |
212 | return NULL; | |
213 | } | |
214 | ||
215 | tokens = g_strsplit(triggerstring, ",", max_probes); | |
216 | for (i = 0; tokens[i]; i++) { | |
217 | if (tokens[i][0] < '0' || tokens[i][0] > '9') { | |
218 | /* Named probe */ | |
219 | probenum = 0; | |
220 | for (l = sdi->probes; l; l = l->next) { | |
221 | probe = (struct sr_probe *)l->data; | |
222 | if (probe->enabled | |
223 | && !strncmp(probe->name, tokens[i], | |
224 | strlen(probe->name))) { | |
225 | probenum = probe->index; | |
226 | break; | |
227 | } | |
228 | } | |
229 | } else { | |
230 | probenum = strtol(tokens[i], NULL, 10); | |
231 | } | |
232 | ||
233 | if (probenum < 0 || probenum >= max_probes) { | |
234 | sr_err("strutil: Invalid probe (%d).", probenum); | |
235 | error = TRUE; | |
236 | break; | |
237 | } | |
238 | ||
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 " | |
243 | "type '%c'.", *tc); | |
244 | error = TRUE; | |
245 | break; | |
246 | } | |
247 | } | |
248 | if (!error) | |
249 | triggerlist[probenum] = g_strdup(trigger); | |
250 | } | |
251 | } | |
252 | g_strfreev(tokens); | |
253 | ||
254 | if (error) { | |
255 | for (i = 0; i < max_probes; i++) | |
256 | g_free(triggerlist[i]); | |
257 | g_free(triggerlist); | |
258 | triggerlist = NULL; | |
259 | } | |
260 | ||
261 | return triggerlist; | |
262 | } | |
263 | ||
264 | /** | |
265 | * Convert a "natural" string representation of a size value to uint64_t. | |
266 | * | |
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. | |
269 | * | |
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. | |
273 | * | |
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. | |
276 | * | |
277 | * @return SR_OK upon success, SR_ERR upon errors. | |
278 | */ | |
279 | SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) | |
280 | { | |
281 | int multiplier, done; | |
282 | char *s; | |
283 | ||
284 | *size = strtoull(sizestring, &s, 10); | |
285 | multiplier = 0; | |
286 | done = FALSE; | |
287 | while (s && *s && multiplier == 0 && !done) { | |
288 | switch (*s) { | |
289 | case ' ': | |
290 | break; | |
291 | case 'k': | |
292 | case 'K': | |
293 | multiplier = SR_KHZ(1); | |
294 | break; | |
295 | case 'm': | |
296 | case 'M': | |
297 | multiplier = SR_MHZ(1); | |
298 | break; | |
299 | case 'g': | |
300 | case 'G': | |
301 | multiplier = SR_GHZ(1); | |
302 | break; | |
303 | default: | |
304 | done = TRUE; | |
305 | s--; | |
306 | } | |
307 | s++; | |
308 | } | |
309 | if (multiplier > 0) | |
310 | *size *= multiplier; | |
311 | ||
312 | if (*s && strcasecmp(s, "Hz")) | |
313 | return SR_ERR; | |
314 | ||
315 | return SR_OK; | |
316 | } | |
317 | ||
318 | /** | |
319 | * Convert a "natural" string representation of a time value to an | |
320 | * uint64_t value in milliseconds. | |
321 | * | |
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. | |
324 | * | |
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. | |
328 | * | |
329 | * @param timestring A string containing a (decimal) time value. | |
330 | * @return The string's time value as uint64_t, in milliseconds. | |
331 | * | |
332 | * TODO: Error handling. | |
333 | * TODO: Add support for "m" (minutes) and others. | |
334 | * TODO: picoseconds? | |
335 | * TODO: Allow both lower-case and upper-case. | |
336 | */ | |
337 | SR_API uint64_t sr_parse_timestring(const char *timestring) | |
338 | { | |
339 | uint64_t time_msec; | |
340 | char *s; | |
341 | ||
342 | time_msec = strtoull(timestring, &s, 10); | |
343 | if (time_msec == 0 && s == timestring) | |
344 | return 0; | |
345 | ||
346 | if (s && *s) { | |
347 | while (*s == ' ') | |
348 | s++; | |
349 | if (!strcmp(s, "s")) | |
350 | time_msec *= 1000; | |
351 | else if (!strcmp(s, "ms")) | |
352 | ; /* redundant */ | |
353 | else | |
354 | return 0; | |
355 | } | |
356 | ||
357 | return time_msec; | |
358 | } | |
359 | ||
360 | SR_API gboolean sr_parse_boolstring(const char *boolstr) | |
361 | { | |
362 | if (!boolstr) | |
363 | return FALSE; | |
364 | ||
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)) | |
369 | return TRUE; | |
370 | ||
371 | return FALSE; | |
372 | } | |
373 | ||
374 | SR_API int sr_parse_period(const char *periodstr, struct sr_rational *r) | |
375 | { | |
376 | char *s; | |
377 | ||
378 | r->p = strtoull(periodstr, &s, 10); | |
379 | if (r->p == 0 && s == periodstr) | |
380 | /* No digits found. */ | |
381 | return SR_ERR_ARG; | |
382 | ||
383 | if (s && *s) { | |
384 | while (*s == ' ') | |
385 | s++; | |
386 | if (!strcmp(s, "ns")) | |
387 | r->q = 1000000000L; | |
388 | else if (!strcmp(s, "us")) | |
389 | r->q = 1000000; | |
390 | else if (!strcmp(s, "ms")) | |
391 | r->q = 1000; | |
392 | else if (!strcmp(s, "s")) | |
393 | r->q = 1; | |
394 | else | |
395 | /* Must have a time suffix. */ | |
396 | return SR_ERR_ARG; | |
397 | } | |
398 | ||
399 | return SR_OK; | |
400 | } | |
401 | ||
402 | ||
403 | SR_API int sr_parse_voltage(const char *voltstr, struct sr_rational *r) | |
404 | { | |
405 | char *s; | |
406 | ||
407 | r->p = strtoull(voltstr, &s, 10); | |
408 | if (r->p == 0 && s == voltstr) | |
409 | /* No digits found. */ | |
410 | return SR_ERR_ARG; | |
411 | ||
412 | if (s && *s) { | |
413 | while (*s == ' ') | |
414 | s++; | |
415 | if (!strcasecmp(s, "mv")) | |
416 | r->q = 1000L; | |
417 | else if (!strcasecmp(s, "v")) | |
418 | r->q = 1; | |
419 | else | |
420 | /* Must have a base suffix. */ | |
421 | return SR_ERR_ARG; | |
422 | } | |
423 | ||
424 | return SR_OK; | |
425 | } | |
426 | ||
427 |