]> sigrok.org Git - libsigrok.git/blame - filter.c
Improve sr_filter_probes error handling and docs.
[libsigrok.git] / filter.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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>
62c82025 23#include <sigrok.h>
488a13b1 24#include "sigrok-internal.h"
a1bb33af 25
25b4fb85
UH
26/**
27 * Remove unused probes from samples.
28 *
62c82025 29 * Convert sample from maximum probes -- the way the hardware driver sent
a1bb33af
UH
30 * it -- to a sample taking up only as much space as required, with
31 * unused probes removed.
25b4fb85 32 *
488a13b1
UH
33 * The "unit size" is the number of bytes used to store probe values.
34 * For example, a unit size of 1 means one byte is used (which can store
35 * 8 probe values, each of them is 1 bit). A unit size of 2 means we can
36 * store 16 probe values, 3 means we can store 24 probe values, and so on.
37 *
38 * If the data coming from the logic analyzer has a unit size of 4 for
39 * example (as the device has 32 probes), but only 2 of them are actually
40 * used in an acquisition, this function can convert the samples to only
41 * use up 1 byte per sample (unit size = 1) instead of 4 bytes per sample.
42 *
43 * The output will contain the probe values in the order specified via the
44 * probelist. For example, if in_unitsize = 4, probelist = [5, 16, 30], and
45 * out_unitsize = 1, then the output samples (each of them one byte in size)
46 * will have the following format: bit 0 = value of probe 5, bit 1 = value
47 * of probe 16, bit 2 = value of probe 30. Unused bit(s) in the output byte(s)
48 * are zero.
49 *
50 * The caller must make sure that length_in is not bigger than the memory
51 * actually allocated for the input data (data_in), as this function does
52 * not check that.
53 *
54 * @param in_unitsize The unit size (>= 1) of the input (data_in).
55 * @param out_unitsize The unit size (>= 1) the output shall have (data_out).
56 * @param probelist Pointer to a list of integers (probe numbers). The probe
57 * numbers in this list are 1-based, i.e. the first probe
58 * is expected to be numbered 1 (not 0!). Must not be NULL.
59 * @param data_in Pointer to the input data buffer. Must not be NULL.
60 * @param length_in The input data length (>= 1), in number of bytes.
61 * @param data_out Variable which will point to the newly allocated buffer
62 * of output data. The caller is responsible for g_free()'ing
63 * the buffer when it's no longer needed. Must not be NULL.
64 * @param length_out Pointer to the variable which will contain the output
65 * data length (in number of bytes) when the function
66 * returns SR_OK. Must not be NULL.
67 *
68 * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
69 * or SR_ERR_ARG upon invalid arguments.
70 * If something other than SR_OK is returned, the values of
71 * out_unitsize, data_out, and length_out are undefined.
a1bb33af 72 */
488a13b1 73int sr_filter_probes(int in_unitsize, int out_unitsize, const int *probelist,
60eb1eb5
BV
74 const unsigned char *data_in, uint64_t length_in,
75 char **data_out, uint64_t *length_out)
a1bb33af 76{
afc8e4de
UH
77 unsigned int in_offset, out_offset;
78 int num_enabled_probes, out_bit, i;
a1bb33af
UH
79 uint64_t sample_in, sample_out;
80
488a13b1
UH
81 if (!probelist) {
82 sr_err("filter: %s: probelist was NULL", __func__);
83 return SR_ERR_ARG;
84 }
85
86 if (!data_in) {
87 sr_err("filter: %s: data_in was NULL", __func__);
88 return SR_ERR_ARG;
89 }
90
91 if (!data_out) {
92 sr_err("filter: %s: data_out was NULL", __func__);
93 return SR_ERR_ARG;
94 }
95
96 if (!length_out) {
97 sr_err("filter: %s: length_out was NULL", __func__);
98 return SR_ERR_ARG;
99 }
100
101 if (!(*data_out = g_try_malloc(length_in))) {
102 sr_err("filter: %s: data_out malloc failed", __func__);
e46b8fb1 103 return SR_ERR_MALLOC;
488a13b1 104 }
fdebec21 105
a1bb33af 106 num_enabled_probes = 0;
62c82025 107 for (i = 0; probelist[i]; i++)
a1bb33af
UH
108 num_enabled_probes++;
109
fdebec21 110 if (num_enabled_probes == in_unitsize * 8) {
62c82025 111 /* All probes are used -- no need to compress anything. */
a1bb33af
UH
112 memcpy(*data_out, data_in, length_in);
113 *length_out = length_in;
e46b8fb1 114 return SR_OK;
fdebec21
UH
115 }
116
117 /* If we reached this point, not all probes are used, so "compress". */
118 in_offset = out_offset = 0;
119 while (in_offset <= length_in - in_unitsize) {
120 memcpy(&sample_in, data_in + in_offset, in_unitsize);
121 sample_out = out_bit = 0;
122 for (i = 0; probelist[i]; i++) {
123 if (sample_in & (1 << (probelist[i] - 1)))
124 sample_out |= (1 << out_bit);
125 out_bit++;
126 }
127 memcpy((*data_out) + out_offset, &sample_out, out_unitsize);
128 in_offset += in_unitsize;
129 out_offset += out_unitsize;
a1bb33af 130 }
fdebec21 131 *length_out = out_offset;
a1bb33af 132
e46b8fb1 133 return SR_OK;
a1bb33af 134}