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