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