scpi-pps: Data capture dynamically-probed channels
[libsigrok.git] / src / hardware / scpi-pps / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <stdarg.h>
25 #include "scpi.h"
26 #include "protocol.h"
27
28 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
29 {
30         struct dev_context *devc;
31         const struct scpi_pps *device;
32         struct sr_datafeed_packet packet;
33         struct sr_datafeed_analog analog;
34         struct sr_analog_encoding encoding;
35         struct sr_analog_meaning meaning;
36         struct sr_analog_spec spec;
37         struct sr_dev_inst *sdi;
38         int channel_group_cmd;
39         const char *channel_group_name;
40         struct pps_channel *pch;
41         const struct channel_spec *ch_spec;
42         int ret;
43         float f;
44         GVariant *gvdata;
45         const GVariantType *gvtype;
46         int cmd;
47
48         (void)fd;
49         (void)revents;
50
51         if (!(sdi = cb_data))
52                 return TRUE;
53
54         if (!(devc = sdi->priv))
55                 return TRUE;
56
57         if (!(device = devc->device))
58                 return TRUE;
59
60         pch = devc->cur_acquisition_channel->priv;
61
62         channel_group_cmd = 0;
63         channel_group_name = NULL;
64         if (g_slist_length(sdi->channel_groups) > 1) {
65                 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
66                 channel_group_name = pch->hwname;
67         }
68
69         /*
70          * When the current channel is the first in the array, perform the device
71          * specific status update first.
72          */
73         if (devc->cur_acquisition_channel == sr_next_enabled_channel(sdi, NULL) &&
74                 device->update_status) {
75                 device->update_status(sdi);
76         }
77
78         if (pch->mq == SR_MQ_VOLTAGE) {
79                 gvtype = G_VARIANT_TYPE_DOUBLE;
80                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
81         } else if (pch->mq == SR_MQ_FREQUENCY) {
82                 gvtype = G_VARIANT_TYPE_DOUBLE;
83                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
84         } else if (pch->mq == SR_MQ_CURRENT) {
85                 gvtype = G_VARIANT_TYPE_DOUBLE;
86                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
87         } else if (pch->mq == SR_MQ_POWER) {
88                 gvtype = G_VARIANT_TYPE_DOUBLE;
89                 cmd = SCPI_CMD_GET_MEAS_POWER;
90         } else {
91                 return SR_ERR;
92         }
93
94         ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
95                 channel_group_cmd, channel_group_name, &gvdata, gvtype, cmd);
96
97         if (ret != SR_OK)
98                 return ret;
99
100         if (devc->channels) {
101                 /* Dynamically-probed devices. */
102                 ch_spec = &devc->channels[pch->hw_output_idx];
103         } else {
104                 /* Statically-configured devices. */
105                 ch_spec = &devc->device->channels[pch->hw_output_idx];
106         }
107         packet.type = SR_DF_ANALOG;
108         packet.payload = &analog;
109         /* Note: digits/spec_digits will be overridden later. */
110         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
111         analog.meaning->channels = g_slist_append(NULL, devc->cur_acquisition_channel);
112         analog.num_samples = 1;
113         analog.meaning->mq = pch->mq;
114         analog.meaning->mqflags = pch->mqflags;
115         if (pch->mq == SR_MQ_VOLTAGE) {
116                 analog.meaning->unit = SR_UNIT_VOLT;
117                 analog.encoding->digits = ch_spec->voltage[4];
118                 analog.spec->spec_digits = ch_spec->voltage[3];
119         } else if (pch->mq == SR_MQ_CURRENT) {
120                 analog.meaning->unit = SR_UNIT_AMPERE;
121                 analog.encoding->digits = ch_spec->current[4];
122                 analog.spec->spec_digits = ch_spec->current[3];
123         } else if (pch->mq == SR_MQ_POWER) {
124                 analog.meaning->unit = SR_UNIT_WATT;
125                 analog.encoding->digits = ch_spec->power[4];
126                 analog.spec->spec_digits = ch_spec->power[3];
127         } else if (pch->mq == SR_MQ_FREQUENCY) {
128                 analog.meaning->unit = SR_UNIT_HERTZ;
129                 analog.encoding->digits = ch_spec->frequency[4];
130                 analog.spec->spec_digits = ch_spec->frequency[3];
131         }
132         f = (float)g_variant_get_double(gvdata);
133         g_variant_unref(gvdata);
134         analog.data = &f;
135         sr_session_send(sdi, &packet);
136         g_slist_free(analog.meaning->channels);
137
138         /* Next channel. */
139         if (g_slist_length(sdi->channels) > 1) {
140                 devc->cur_acquisition_channel =
141                         sr_next_enabled_channel(sdi, devc->cur_acquisition_channel);
142         }
143
144         if (devc->cur_acquisition_channel == sr_next_enabled_channel(sdi, NULL))
145                 /* First enabled channel, so each channel has been sampled */
146                 sr_sw_limits_update_samples_read(&devc->limits, 1);
147
148         /* Stop if limits have been hit. */
149         if (sr_sw_limits_check(&devc->limits))
150                 sr_dev_acquisition_stop(sdi);
151
152         return TRUE;
153 }