]> sigrok.org Git - libsigrok.git/blob - src/binary_helpers.c
input/stf: introduce support for Asix' Sigma Test File format
[libsigrok.git] / src / binary_helpers.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2020 Andreas Sandberg <andreas@sandberg.pp.se>
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 <config.h>
21 #include <glib.h>
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24
25 SR_PRIV int bv_get_value(float *out, const struct binary_value_spec *spec, const void *data, size_t length)
26 {
27         float value;
28
29         if (!out || !spec || !data)
30                 return SR_ERR_ARG;
31
32 #define VALUE_TYPE(T, R, L)                             \
33         case T:                                         \
34                 if (spec->offset + (L) > length)        \
35                         return SR_ERR_DATA;             \
36                 value = R(data + spec->offset);         \
37                 break
38
39         switch (spec->type) {
40                 VALUE_TYPE(BVT_UINT8, R8, 1);
41
42                 VALUE_TYPE(BVT_BE_UINT16, RB16, 2);
43                 VALUE_TYPE(BVT_BE_UINT32, RB32, 4);
44                 VALUE_TYPE(BVT_BE_UINT64, RB64, 8);
45                 VALUE_TYPE(BVT_BE_FLOAT, RBFL, 4);
46
47                 VALUE_TYPE(BVT_LE_UINT16, RL16, 2);
48                 VALUE_TYPE(BVT_LE_UINT32, RL32, 4);
49                 VALUE_TYPE(BVT_LE_UINT64, RL64, 8);
50                 VALUE_TYPE(BVT_LE_FLOAT, RLFL, 4);
51
52         default:
53                 return SR_ERR_ARG;
54         }
55
56 #undef VALUE_TYPE
57
58         *out = value * spec->scale;
59         return SR_OK;
60 }
61
62 SR_PRIV int bv_send_analog_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch,
63                                    const struct binary_analog_channel *bac, const void *data, size_t length)
64 {
65         int err;
66         struct sr_analog_encoding encoding;
67         struct sr_analog_meaning meaning;
68         struct sr_analog_spec spec;
69         struct sr_datafeed_analog analog;
70         struct sr_datafeed_packet packet = {
71                 .type = SR_DF_ANALOG,
72                 .payload = &analog,
73         };
74         float value;
75
76         err = bv_get_value(&value, &bac->spec, data, length);
77         if (err != SR_OK)
78                 goto err_out;
79
80         err = sr_analog_init(&analog, &encoding, &meaning, &spec, bac->digits);
81         if (err != SR_OK)
82                 goto err_out;
83
84         meaning.mq = bac->mq;
85         meaning.unit = bac->unit;
86         meaning.mqflags = 0;
87         meaning.channels = g_slist_append(NULL, ch);
88
89         spec.spec_digits = bac->digits;
90
91         analog.data = &value;
92         analog.num_samples = 1;
93
94         err = sr_session_send(sdi, &packet);
95         if (err != SR_OK)
96                 goto err_free;
97
98         return SR_OK;
99
100 err_free:
101         g_slist_free(meaning.channels);
102
103 err_out:
104         return err;
105 }