]>
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 "sigrok.h" | |
25 | #include "sigrok-internal.h" | |
26 | ||
27 | /** | |
28 | * Convert a numeric samplerate value to its "natural" string representation. | |
29 | * | |
30 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", | |
31 | * 31500 would become "31.5 kHz". | |
32 | * | |
33 | * @param samplerate The samplerate in Hz. | |
34 | * | |
35 | * @return A g_try_malloc()ed string representation of the samplerate value, | |
36 | * or NULL upon errors. The caller is responsible to g_free() the | |
37 | * memory. | |
38 | */ | |
39 | SR_API char *sr_samplerate_string(uint64_t samplerate) | |
40 | { | |
41 | char *o; | |
42 | uint64_t s = samplerate; | |
43 | ||
44 | if ((s >= SR_GHZ(1)) && (s % SR_GHZ(1) == 0)) { | |
45 | o = g_strdup_printf("%" PRIu64 " GHz", s / SR_GHZ(1)); | |
46 | } else if ((s >= SR_GHZ(1)) && (s % SR_GHZ(1) != 0)) { | |
47 | o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " GHz", | |
48 | s / SR_GHZ(1), s % SR_GHZ(1)); | |
49 | } else if ((s >= SR_MHZ(1)) && (s % SR_MHZ(1) == 0)) { | |
50 | o = g_strdup_printf("%" PRIu64 " MHz", s / SR_MHZ(1)); | |
51 | } else if ((s >= SR_MHZ(1)) && (s % SR_MHZ(1) != 0)) { | |
52 | o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " MHz", | |
53 | s / SR_MHZ(1), s % SR_MHZ(1)); | |
54 | } else if ((s >= SR_KHZ(1)) && (s % SR_KHZ(1) == 0)) { | |
55 | o = g_strdup_printf("%" PRIu64 " kHz", s / SR_KHZ(1)); | |
56 | } else if ((s >= SR_KHZ(1)) && (s % SR_KHZ(1) != 0)) { | |
57 | o = g_strdup_printf("%" PRIu64 ".%" PRIu64 " kHz", | |
58 | s / SR_KHZ(1), s % SR_KHZ(1)); | |
59 | } else { | |
60 | o = g_strdup_printf("%" PRIu64 " Hz", s); | |
61 | } | |
62 | ||
63 | if (!o) { | |
64 | sr_err("strutil: %s: Error creating samplerate string.", | |
65 | __func__); | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | return o; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Convert a numeric frequency value to the "natural" string representation | |
74 | * of its period. | |
75 | * | |
76 | * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". | |
77 | * | |
78 | * @param frequency The frequency in Hz. | |
79 | * | |
80 | * @return A g_try_malloc()ed string representation of the frequency value, | |
81 | * or NULL upon errors. The caller is responsible to g_free() the | |
82 | * memory. | |
83 | */ | |
84 | SR_API char *sr_period_string(uint64_t frequency) | |
85 | { | |
86 | char *o; | |
87 | int r; | |
88 | ||
89 | /* Allocate enough for a uint64_t as string + " ms". */ | |
90 | if (!(o = g_try_malloc0(30 + 1))) { | |
91 | sr_err("strutil: %s: o malloc failed", __func__); | |
92 | return NULL; | |
93 | } | |
94 | ||
95 | if (frequency >= SR_GHZ(1)) | |
96 | r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); | |
97 | else if (frequency >= SR_MHZ(1)) | |
98 | r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000); | |
99 | else if (frequency >= SR_KHZ(1)) | |
100 | r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000); | |
101 | else | |
102 | r = snprintf(o, 30, "%" PRIu64 " s", frequency); | |
103 | ||
104 | if (r < 0) { | |
105 | /* Something went wrong... */ | |
106 | g_free(o); | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | return o; | |
111 | } | |
112 | ||
113 | /** | |
114 | * Parse a trigger specification string. | |
115 | * | |
116 | * @param dev The device for which the trigger specification is intended. | |
117 | * @param triggerstring The string containing the trigger specification for | |
118 | * one or more probes of this device. Entries for multiple probes are | |
119 | * comma-separated. Triggers are specified in the form key=value, | |
120 | * where the key is a probe number (or probe name) and the value is | |
121 | * the requested trigger type. Valid trigger types currently | |
122 | * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value | |
123 | * change), '0' (low value), or '1' (high value). | |
124 | * Example: "1=r,sck=f,miso=0,7=c" | |
125 | * | |
126 | * @return Pointer to a list of trigger types (strings), or NULL upon errors. | |
127 | * The pointer list (if non-NULL) has as many entries as the | |
128 | * respective device has probes (all physically available probes, | |
129 | * not just enabled ones). Entries of the list which don't have | |
130 | * a trigger value set in 'triggerstring' are NULL, the other entries | |
131 | * contain the respective trigger type which is requested for the | |
132 | * respective probe (e.g. "r", "c", and so on). | |
133 | */ | |
134 | SR_API char **sr_parse_triggerstring(struct sr_dev *dev, | |
135 | const char *triggerstring) | |
136 | { | |
137 | GSList *l; | |
138 | struct sr_probe *probe; | |
139 | int max_probes, probenum, i; | |
140 | char **tokens, **triggerlist, *trigger, *tc, *trigger_types; | |
141 | gboolean error; | |
142 | ||
143 | max_probes = g_slist_length(dev->probes); | |
144 | error = FALSE; | |
145 | ||
146 | if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) { | |
147 | sr_err("strutil: %s: triggerlist malloc failed", __func__); | |
148 | return NULL; | |
149 | } | |
150 | ||
151 | tokens = g_strsplit(triggerstring, ",", max_probes); | |
152 | ||
153 | trigger_types = dev->driver->dev_info_get(0, SR_DI_TRIGGER_TYPES); | |
154 | if (!trigger_types) { | |
155 | sr_err("strutil: %s: Device doesn't support any triggers.", | |
156 | __func__); | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | for (i = 0; tokens[i]; i++) { | |
161 | if (tokens[i][0] < '0' || tokens[i][0] > '9') { | |
162 | /* Named probe */ | |
163 | probenum = 0; | |
164 | for (l = dev->probes; l; l = l->next) { | |
165 | probe = (struct sr_probe *)l->data; | |
166 | if (probe->enabled | |
167 | && !strncmp(probe->name, tokens[i], | |
168 | strlen(probe->name))) { | |
169 | probenum = probe->index; | |
170 | break; | |
171 | } | |
172 | } | |
173 | } else { | |
174 | probenum = strtol(tokens[i], NULL, 10); | |
175 | } | |
176 | ||
177 | if (probenum < 1 || probenum > max_probes) { | |
178 | sr_err("strutil: Invalid probe (%d).", probenum); | |
179 | error = TRUE; | |
180 | break; | |
181 | } | |
182 | ||
183 | if ((trigger = strchr(tokens[i], '='))) { | |
184 | for (tc = ++trigger; *tc; tc++) { | |
185 | if (strchr(trigger_types, *tc) == NULL) { | |
186 | sr_err("strutil: Unsupported trigger " | |
187 | "type '%c'.", *tc); | |
188 | error = TRUE; | |
189 | break; | |
190 | } | |
191 | } | |
192 | if (!error) | |
193 | triggerlist[probenum - 1] = g_strdup(trigger); | |
194 | } | |
195 | } | |
196 | g_strfreev(tokens); | |
197 | ||
198 | if (error) { | |
199 | for (i = 0; i < max_probes; i++) | |
200 | g_free(triggerlist[i]); | |
201 | g_free(triggerlist); | |
202 | triggerlist = NULL; | |
203 | } | |
204 | ||
205 | return triggerlist; | |
206 | } | |
207 | ||
208 | /** | |
209 | * Convert a "natural" string representation of a size value to uint64_t. | |
210 | * | |
211 | * E.g. a value of "3k" or "3 K" would be converted to 3000, a value | |
212 | * of "15M" would be converted to 15000000. | |
213 | * | |
214 | * Value representations other than decimal (such as hex or octal) are not | |
215 | * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported. | |
216 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
217 | * | |
218 | * @param sizestring A string containing a (decimal) size value. | |
219 | * @param size Pointer to uint64_t which will contain the string's size value. | |
220 | * | |
221 | * @return SR_OK upon success, SR_ERR upon errors. | |
222 | */ | |
223 | SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) | |
224 | { | |
225 | int multiplier, done; | |
226 | char *s; | |
227 | ||
228 | *size = strtoull(sizestring, &s, 10); | |
229 | multiplier = 0; | |
230 | done = FALSE; | |
231 | while (s && *s && multiplier == 0 && !done) { | |
232 | switch (*s) { | |
233 | case ' ': | |
234 | break; | |
235 | case 'k': | |
236 | case 'K': | |
237 | multiplier = SR_KHZ(1); | |
238 | break; | |
239 | case 'm': | |
240 | case 'M': | |
241 | multiplier = SR_MHZ(1); | |
242 | break; | |
243 | case 'g': | |
244 | case 'G': | |
245 | multiplier = SR_GHZ(1); | |
246 | break; | |
247 | default: | |
248 | done = TRUE; | |
249 | s--; | |
250 | } | |
251 | s++; | |
252 | } | |
253 | if (multiplier > 0) | |
254 | *size *= multiplier; | |
255 | ||
256 | if (*s && strcasecmp(s, "Hz")) | |
257 | return SR_ERR; | |
258 | ||
259 | return SR_OK; | |
260 | } | |
261 | ||
262 | /** | |
263 | * Convert a "natural" string representation of a time value to an | |
264 | * uint64_t value in milliseconds. | |
265 | * | |
266 | * E.g. a value of "3s" or "3 s" would be converted to 3000, a value | |
267 | * of "15ms" would be converted to 15. | |
268 | * | |
269 | * Value representations other than decimal (such as hex or octal) are not | |
270 | * supported. Only lower-case "s" and "ms" time suffixes are supported. | |
271 | * Spaces (but not other whitespace) between value and suffix are allowed. | |
272 | * | |
273 | * @param timestring A string containing a (decimal) time value. | |
274 | * @return The string's time value as uint64_t, in milliseconds. | |
275 | * | |
276 | * TODO: Error handling. | |
277 | * TODO: Add support for "m" (minutes) and others. | |
278 | * TODO: picoseconds? | |
279 | * TODO: Allow both lower-case and upper-case. | |
280 | */ | |
281 | SR_API uint64_t sr_parse_timestring(const char *timestring) | |
282 | { | |
283 | uint64_t time_msec; | |
284 | char *s; | |
285 | ||
286 | time_msec = strtoull(timestring, &s, 10); | |
287 | if (time_msec == 0 && s == timestring) | |
288 | return 0; | |
289 | ||
290 | if (s && *s) { | |
291 | while (*s == ' ') | |
292 | s++; | |
293 | if (!strcmp(s, "s")) | |
294 | time_msec *= 1000; | |
295 | else if (!strcmp(s, "ms")) | |
296 | ; /* redundant */ | |
297 | else | |
298 | return 0; | |
299 | } | |
300 | ||
301 | return time_msec; | |
302 | } | |
303 | ||
304 | SR_API gboolean sr_parse_boolstring(const char *boolstr) | |
305 | { | |
306 | if (!boolstr) | |
307 | return FALSE; | |
308 | ||
309 | if (!g_ascii_strncasecmp(boolstr, "true", 4) || | |
310 | !g_ascii_strncasecmp(boolstr, "yes", 3) || | |
311 | !g_ascii_strncasecmp(boolstr, "on", 2) || | |
312 | !g_ascii_strncasecmp(boolstr, "1", 1)) | |
313 | return TRUE; | |
314 | ||
315 | return FALSE; | |
316 | } |