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