]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <stdint.h> | |
22 | #include <string.h> | |
45c59c8b BV |
23 | #include "libsigrok.h" |
24 | #include "libsigrok-internal.h" | |
a1bb33af | 25 | |
a885ce3e UH |
26 | /* Message logging helpers with driver-specific prefix string. */ |
27 | #define DRIVER_LOG_DOMAIN "filter: " | |
28 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
29 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
30 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
31 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
32 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
33 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
34 | ||
393fb9cb UH |
35 | /** |
36 | * @file | |
37 | * | |
38 | * Helper functions to filter out unused probes from samples. | |
39 | */ | |
40 | ||
7b870c38 UH |
41 | /** |
42 | * @defgroup grp_filter Probe filter | |
43 | * | |
44 | * Helper functions to filter out unused probes from samples. | |
45 | * | |
46 | * @{ | |
47 | */ | |
48 | ||
25b4fb85 UH |
49 | /** |
50 | * Remove unused probes from samples. | |
51 | * | |
62c82025 | 52 | * Convert sample from maximum probes -- the way the hardware driver sent |
a1bb33af UH |
53 | * it -- to a sample taking up only as much space as required, with |
54 | * unused probes removed. | |
25b4fb85 | 55 | * |
488a13b1 UH |
56 | * The "unit size" is the number of bytes used to store probe values. |
57 | * For example, a unit size of 1 means one byte is used (which can store | |
58 | * 8 probe values, each of them is 1 bit). A unit size of 2 means we can | |
59 | * store 16 probe values, 3 means we can store 24 probe values, and so on. | |
60 | * | |
61 | * If the data coming from the logic analyzer has a unit size of 4 for | |
62 | * example (as the device has 32 probes), but only 2 of them are actually | |
63 | * used in an acquisition, this function can convert the samples to only | |
64 | * use up 1 byte per sample (unit size = 1) instead of 4 bytes per sample. | |
65 | * | |
66 | * The output will contain the probe values in the order specified via the | |
67 | * probelist. For example, if in_unitsize = 4, probelist = [5, 16, 30], and | |
68 | * out_unitsize = 1, then the output samples (each of them one byte in size) | |
69 | * will have the following format: bit 0 = value of probe 5, bit 1 = value | |
70 | * of probe 16, bit 2 = value of probe 30. Unused bit(s) in the output byte(s) | |
71 | * are zero. | |
72 | * | |
73 | * The caller must make sure that length_in is not bigger than the memory | |
74 | * actually allocated for the input data (data_in), as this function does | |
75 | * not check that. | |
76 | * | |
77 | * @param in_unitsize The unit size (>= 1) of the input (data_in). | |
78 | * @param out_unitsize The unit size (>= 1) the output shall have (data_out). | |
7a6ec0c3 UH |
79 | * The requested unit size must be big enough to hold as |
80 | * much data as is specified by the number of enabled | |
81 | * probes in 'probelist'. | |
1907d2c9 BV |
82 | * @param probelist Pointer to a list of probe numbers, numbered starting |
83 | * from 0. The list is terminated with -1. | |
488a13b1 UH |
84 | * @param data_in Pointer to the input data buffer. Must not be NULL. |
85 | * @param length_in The input data length (>= 1), in number of bytes. | |
86 | * @param data_out Variable which will point to the newly allocated buffer | |
87 | * of output data. The caller is responsible for g_free()'ing | |
88 | * the buffer when it's no longer needed. Must not be NULL. | |
89 | * @param length_out Pointer to the variable which will contain the output | |
90 | * data length (in number of bytes) when the function | |
91 | * returns SR_OK. Must not be NULL. | |
92 | * | |
93 | * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors, | |
94 | * or SR_ERR_ARG upon invalid arguments. | |
95 | * If something other than SR_OK is returned, the values of | |
96 | * out_unitsize, data_out, and length_out are undefined. | |
a1bb33af | 97 | */ |
1a081ca6 | 98 | SR_API int sr_filter_probes(int in_unitsize, int out_unitsize, |
054e6709 UH |
99 | const int *probelist, const uint8_t *data_in, |
100 | uint64_t length_in, uint8_t **data_out, | |
1a081ca6 | 101 | uint64_t *length_out) |
a1bb33af | 102 | { |
afc8e4de UH |
103 | unsigned int in_offset, out_offset; |
104 | int num_enabled_probes, out_bit, i; | |
a1bb33af UH |
105 | uint64_t sample_in, sample_out; |
106 | ||
488a13b1 | 107 | if (!probelist) { |
a885ce3e | 108 | sr_err("%s: probelist was NULL", __func__); |
488a13b1 UH |
109 | return SR_ERR_ARG; |
110 | } | |
111 | ||
112 | if (!data_in) { | |
a885ce3e | 113 | sr_err("%s: data_in was NULL", __func__); |
488a13b1 UH |
114 | return SR_ERR_ARG; |
115 | } | |
116 | ||
117 | if (!data_out) { | |
a885ce3e | 118 | sr_err("%s: data_out was NULL", __func__); |
488a13b1 UH |
119 | return SR_ERR_ARG; |
120 | } | |
121 | ||
122 | if (!length_out) { | |
a885ce3e | 123 | sr_err("%s: length_out was NULL", __func__); |
488a13b1 UH |
124 | return SR_ERR_ARG; |
125 | } | |
126 | ||
a1bb33af | 127 | num_enabled_probes = 0; |
1907d2c9 | 128 | for (i = 0; probelist[i] != -1; i++) |
a1bb33af UH |
129 | num_enabled_probes++; |
130 | ||
7a6ec0c3 UH |
131 | /* Are there more probes than the target unit size supports? */ |
132 | if (num_enabled_probes > out_unitsize * 8) { | |
a885ce3e | 133 | sr_err("%s: too many probes (%d) for the target unit " |
1907d2c9 | 134 | "size (%d)", __func__, num_enabled_probes, out_unitsize); |
7a6ec0c3 UH |
135 | return SR_ERR_ARG; |
136 | } | |
137 | ||
138 | if (!(*data_out = g_try_malloc(length_in))) { | |
a885ce3e | 139 | sr_err("%s: data_out malloc failed", __func__); |
7a6ec0c3 UH |
140 | return SR_ERR_MALLOC; |
141 | } | |
142 | ||
fdebec21 | 143 | if (num_enabled_probes == in_unitsize * 8) { |
62c82025 | 144 | /* All probes are used -- no need to compress anything. */ |
a1bb33af UH |
145 | memcpy(*data_out, data_in, length_in); |
146 | *length_out = length_in; | |
e46b8fb1 | 147 | return SR_OK; |
fdebec21 UH |
148 | } |
149 | ||
150 | /* If we reached this point, not all probes are used, so "compress". */ | |
151 | in_offset = out_offset = 0; | |
152 | while (in_offset <= length_in - in_unitsize) { | |
153 | memcpy(&sample_in, data_in + in_offset, in_unitsize); | |
154 | sample_out = out_bit = 0; | |
1907d2c9 BV |
155 | for (i = 0; probelist[i] != -1; i++) { |
156 | if (sample_in & (1 << (probelist[i]))) | |
fdebec21 UH |
157 | sample_out |= (1 << out_bit); |
158 | out_bit++; | |
159 | } | |
160 | memcpy((*data_out) + out_offset, &sample_out, out_unitsize); | |
161 | in_offset += in_unitsize; | |
162 | out_offset += out_unitsize; | |
a1bb33af | 163 | } |
fdebec21 | 164 | *length_out = out_offset; |
a1bb33af | 165 | |
e46b8fb1 | 166 | return SR_OK; |
a1bb33af | 167 | } |
7b870c38 UH |
168 | |
169 | /** @} */ |