libsigrok
filter.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
00005  *
00006  * This program is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <stdlib.h>
00021 #include <stdint.h>
00022 #include <string.h>
00023 #include "sigrok.h"
00024 #include "sigrok-internal.h"
00025 
00026 /**
00027  * Remove unused probes from samples.
00028  *
00029  * Convert sample from maximum probes -- the way the hardware driver sent
00030  * it -- to a sample taking up only as much space as required, with
00031  * unused probes removed.
00032  *
00033  * The "unit size" is the number of bytes used to store probe values.
00034  * For example, a unit size of 1 means one byte is used (which can store
00035  * 8 probe values, each of them is 1 bit). A unit size of 2 means we can
00036  * store 16 probe values, 3 means we can store 24 probe values, and so on.
00037  *
00038  * If the data coming from the logic analyzer has a unit size of 4 for
00039  * example (as the device has 32 probes), but only 2 of them are actually
00040  * used in an acquisition, this function can convert the samples to only
00041  * use up 1 byte per sample (unit size = 1) instead of 4 bytes per sample.
00042  *
00043  * The output will contain the probe values in the order specified via the
00044  * probelist. For example, if in_unitsize = 4, probelist = [5, 16, 30], and
00045  * out_unitsize = 1, then the output samples (each of them one byte in size)
00046  * will have the following format: bit 0 = value of probe 5, bit 1 = value
00047  * of probe 16, bit 2 = value of probe 30. Unused bit(s) in the output byte(s)
00048  * are zero.
00049  *
00050  * The caller must make sure that length_in is not bigger than the memory
00051  * actually allocated for the input data (data_in), as this function does
00052  * not check that.
00053  *
00054  * @param in_unitsize The unit size (>= 1) of the input (data_in).
00055  * @param out_unitsize The unit size (>= 1) the output shall have (data_out).
00056  *                     The requested unit size must be big enough to hold as
00057  *                     much data as is specified by the number of enabled
00058  *                     probes in 'probelist'.
00059  * @param probelist Pointer to a list of integers (probe numbers). The probe
00060  *                  numbers in this list are 1-based, i.e. the first probe
00061  *                  is expected to be numbered 1 (not 0!). Must not be NULL.
00062  * @param data_in Pointer to the input data buffer. Must not be NULL.
00063  * @param length_in The input data length (>= 1), in number of bytes.
00064  * @param data_out Variable which will point to the newly allocated buffer
00065  *                 of output data. The caller is responsible for g_free()'ing
00066  *                 the buffer when it's no longer needed. Must not be NULL.
00067  * @param length_out Pointer to the variable which will contain the output
00068  *                   data length (in number of bytes) when the function
00069  *                   returns SR_OK. Must not be NULL.
00070  *
00071  * @return SR_OK upon success, SR_ERR_MALLOC upon memory allocation errors,
00072  *         or SR_ERR_ARG upon invalid arguments.
00073  *         If something other than SR_OK is returned, the values of
00074  *         out_unitsize, data_out, and length_out are undefined.
00075  */
00076 SR_API int sr_filter_probes(int in_unitsize, int out_unitsize,
00077                             const int *probelist, const uint8_t *data_in,
00078                             uint64_t length_in, uint8_t **data_out,
00079                             uint64_t *length_out)
00080 {
00081         unsigned int in_offset, out_offset;
00082         int num_enabled_probes, out_bit, i;
00083         uint64_t sample_in, sample_out;
00084 
00085         if (!probelist) {
00086                 sr_err("filter: %s: probelist was NULL", __func__);
00087                 return SR_ERR_ARG;
00088         }
00089 
00090         if (!data_in) {
00091                 sr_err("filter: %s: data_in was NULL", __func__);
00092                 return SR_ERR_ARG;
00093         }
00094 
00095         if (!data_out) {
00096                 sr_err("filter: %s: data_out was NULL", __func__);
00097                 return SR_ERR_ARG;
00098         }
00099 
00100         if (!length_out) {
00101                 sr_err("filter: %s: length_out was NULL", __func__);
00102                 return SR_ERR_ARG;
00103         }
00104 
00105         num_enabled_probes = 0;
00106         for (i = 0; probelist[i]; i++)
00107                 num_enabled_probes++;
00108 
00109         /* Are there more probes than the target unit size supports? */
00110         if (num_enabled_probes > out_unitsize * 8) {
00111                 sr_err("filter: %s: too many probes (%d) for the target unit "
00112                        "size (%d)", num_enabled_probes, out_unitsize, __func__);
00113                 return SR_ERR_ARG;
00114         }
00115 
00116         if (!(*data_out = g_try_malloc(length_in))) {
00117                 sr_err("filter: %s: data_out malloc failed", __func__);
00118                 return SR_ERR_MALLOC;
00119         }
00120 
00121         if (num_enabled_probes == in_unitsize * 8) {
00122                 /* All probes are used -- no need to compress anything. */
00123                 memcpy(*data_out, data_in, length_in);
00124                 *length_out = length_in;
00125                 return SR_OK;
00126         }
00127 
00128         /* If we reached this point, not all probes are used, so "compress". */
00129         in_offset = out_offset = 0;
00130         while (in_offset <= length_in - in_unitsize) {
00131                 memcpy(&sample_in, data_in + in_offset, in_unitsize);
00132                 sample_out = out_bit = 0;
00133                 for (i = 0; probelist[i]; i++) {
00134                         if (sample_in & (1 << (probelist[i] - 1)))
00135                                 sample_out |= (1 << out_bit);
00136                         out_bit++;
00137                 }
00138                 memcpy((*data_out) + out_offset, &sample_out, out_unitsize);
00139                 in_offset += in_unitsize;
00140                 out_offset += out_unitsize;
00141         }
00142         *length_out = out_offset;
00143 
00144         return SR_OK;
00145 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines