9609936a23ae0604df899b9ddc23482c12111715
[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         ch_spec = &devc->device->channels[pch->hw_output_idx];
101         packet.type = SR_DF_ANALOG;
102         packet.payload = &analog;
103         /* Note: digits/spec_digits will be overridden later. */
104         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
105         analog.meaning->channels = g_slist_append(NULL, devc->cur_acquisition_channel);
106         analog.num_samples = 1;
107         analog.meaning->mq = pch->mq;
108         analog.meaning->mqflags = pch->mqflags;
109         if (pch->mq == SR_MQ_VOLTAGE) {
110                 analog.meaning->unit = SR_UNIT_VOLT;
111                 analog.encoding->digits = ch_spec->voltage[4];
112                 analog.spec->spec_digits = ch_spec->voltage[3];
113         } else if (pch->mq == SR_MQ_CURRENT) {
114                 analog.meaning->unit = SR_UNIT_AMPERE;
115                 analog.encoding->digits = ch_spec->current[4];
116                 analog.spec->spec_digits = ch_spec->current[3];
117         } else if (pch->mq == SR_MQ_POWER) {
118                 analog.meaning->unit = SR_UNIT_WATT;
119                 analog.encoding->digits = ch_spec->power[4];
120                 analog.spec->spec_digits = ch_spec->power[3];
121         } else if (pch->mq == SR_MQ_FREQUENCY) {
122                 analog.meaning->unit = SR_UNIT_HERTZ;
123                 analog.encoding->digits = ch_spec->frequency[4];
124                 analog.spec->spec_digits = ch_spec->frequency[3];
125         }
126         f = (float)g_variant_get_double(gvdata);
127         g_variant_unref(gvdata);
128         analog.data = &f;
129         sr_session_send(sdi, &packet);
130         g_slist_free(analog.meaning->channels);
131
132         /* Next channel. */
133         if (g_slist_length(sdi->channels) > 1) {
134                 devc->cur_acquisition_channel =
135                         sr_next_enabled_channel(sdi, devc->cur_acquisition_channel);
136         }
137
138         if (devc->cur_acquisition_channel == sr_next_enabled_channel(sdi, NULL))
139                 /* First enabled channel, so each channel has been sampled */
140                 sr_sw_limits_update_samples_read(&devc->limits, 1);
141
142         /* Stop if limits have been hit. */
143         if (sr_sw_limits_check(&devc->limits))
144                 sr_dev_acquisition_stop(sdi);
145
146         return TRUE;
147 }