]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / scpi-pps / protocol.c
CommitLineData
ca1a7cb5
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
7e66bf05 5 * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
ca1a7cb5
BV
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
6ec6c43b 21#include <config.h>
9e45cd41 22#include <string.h>
ba464a12 23#include <strings.h>
9e45cd41 24#include <stdarg.h>
91ef511d 25#include "scpi.h"
ca1a7cb5
BV
26#include "protocol.h"
27
28SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
29{
ca1a7cb5 30 struct dev_context *devc;
7e66bf05 31 const struct scpi_pps *device;
9e45cd41 32 struct sr_datafeed_packet packet;
0de290a5
UH
33 struct sr_datafeed_analog analog;
34 struct sr_analog_encoding encoding;
35 struct sr_analog_meaning meaning;
36 struct sr_analog_spec spec;
88e4daa9 37 struct sr_dev_inst *sdi;
17a82e83 38 int channel_group_cmd;
3d118722 39 const char *channel_group_name;
01b0257a 40 struct pps_channel *pch;
6ed709fe 41 const struct channel_spec *ch_spec;
fa2ce8c7
FS
42 int ret;
43 float f;
44 GVariant *gvdata;
45 const GVariantType *gvtype;
01b0257a 46 int cmd;
ca1a7cb5
BV
47
48 (void)fd;
9e45cd41 49 (void)revents;
ca1a7cb5
BV
50
51 if (!(sdi = cb_data))
52 return TRUE;
53
54 if (!(devc = sdi->priv))
55 return TRUE;
56
7e66bf05
FS
57 if (!(device = devc->device))
58 return TRUE;
59
3d118722
ML
60 pch = devc->cur_acquisition_channel->priv;
61
17a82e83
FS
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;
3d118722 66 channel_group_name = pch->hwname;
17a82e83
FS
67 }
68
7e66bf05
FS
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
fa2ce8c7
FS
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;
9e45cd41
BV
92 }
93
17a82e83
FS
94 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
95 channel_group_cmd, channel_group_name, &gvdata, gvtype, cmd);
17a82e83 96
fa2ce8c7
FS
97 if (ret != SR_OK)
98 return ret;
99
c54b3726
MA
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 }
fa2ce8c7
FS
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);
17a82e83 111 analog.meaning->channels = g_slist_append(NULL, devc->cur_acquisition_channel);
fa2ce8c7
FS
112 analog.num_samples = 1;
113 analog.meaning->mq = pch->mq;
f2bbcc33 114 analog.meaning->mqflags = pch->mqflags;
fa2ce8c7
FS
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];
969671a5
FS
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];
fa2ce8c7 131 }
fa2ce8c7 132 f = (float)g_variant_get_double(gvdata);
3d118722 133 g_variant_unref(gvdata);
fa2ce8c7
FS
134 analog.data = &f;
135 sr_session_send(sdi, &packet);
136 g_slist_free(analog.meaning->channels);
137
17a82e83 138 /* Next channel. */
60475cd7 139 if (g_slist_length(sdi->channels) > 1) {
17a82e83
FS
140 devc->cur_acquisition_channel =
141 sr_next_enabled_channel(sdi, devc->cur_acquisition_channel);
60475cd7 142 }
01b0257a 143
88e4daa9
ML
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
ca1a7cb5
BV
152 return TRUE;
153}