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