]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/protocol.c
scpi-pps: Use thread safe SCPI functions, return float not double.
[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>
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
6ec6c43b 20#include <config.h>
9e45cd41 21#include <string.h>
ba464a12 22#include <strings.h>
9e45cd41 23#include <stdarg.h>
91ef511d 24#include "scpi.h"
ca1a7cb5
BV
25#include "protocol.h"
26
60475cd7
BV
27SR_PRIV int select_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch)
28{
29 struct dev_context *devc;
624503ae 30 struct pps_channel *cur_pch, *new_pch;
60475cd7
BV
31 int ret;
32
33 if (g_slist_length(sdi->channels) == 1)
34 return SR_OK;
35
36 devc = sdi->priv;
37 if (ch == devc->cur_channel)
38 return SR_OK;
39
624503ae
BV
40 new_pch = ch->priv;
41 if (devc->cur_channel) {
42 cur_pch = devc->cur_channel->priv;
43 if (cur_pch->hw_output_idx == new_pch->hw_output_idx) {
44 /* Same underlying output channel. */
45 devc->cur_channel = ch;
46 return SR_OK;
47 }
48 }
49
fa2ce8c7 50 if ((ret = sr_scpi_cmd(sdi, devc->device->commands, SCPI_CMD_SELECT_CHANNEL,
91ef511d 51 new_pch->hwname)) >= 0)
60475cd7
BV
52 devc->cur_channel = ch;
53
54 return ret;
55}
56
ca1a7cb5
BV
57SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
58{
ca1a7cb5 59 struct dev_context *devc;
9e45cd41 60 struct sr_datafeed_packet packet;
0de290a5
UH
61 struct sr_datafeed_analog analog;
62 struct sr_analog_encoding encoding;
63 struct sr_analog_meaning meaning;
64 struct sr_analog_spec spec;
9e45cd41 65 const struct sr_dev_inst *sdi;
60475cd7 66 struct sr_channel *next_channel;
01b0257a 67 struct pps_channel *pch;
6ed709fe 68 const struct channel_spec *ch_spec;
fa2ce8c7
FS
69 int ret;
70 float f;
71 GVariant *gvdata;
72 const GVariantType *gvtype;
01b0257a 73 int cmd;
ca1a7cb5
BV
74
75 (void)fd;
9e45cd41 76 (void)revents;
ca1a7cb5
BV
77
78 if (!(sdi = cb_data))
79 return TRUE;
80
81 if (!(devc = sdi->priv))
82 return TRUE;
83
fa2ce8c7
FS
84 pch = devc->cur_channel->priv;
85 if (pch->mq == SR_MQ_VOLTAGE) {
86 gvtype = G_VARIANT_TYPE_DOUBLE;
87 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
88 } else if (pch->mq == SR_MQ_FREQUENCY) {
89 gvtype = G_VARIANT_TYPE_DOUBLE;
90 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
91 } else if (pch->mq == SR_MQ_CURRENT) {
92 gvtype = G_VARIANT_TYPE_DOUBLE;
93 cmd = SCPI_CMD_GET_MEAS_CURRENT;
94 } else if (pch->mq == SR_MQ_POWER) {
95 gvtype = G_VARIANT_TYPE_DOUBLE;
96 cmd = SCPI_CMD_GET_MEAS_POWER;
97 } else {
98 return SR_ERR;
9e45cd41
BV
99 }
100
fa2ce8c7
FS
101 //sr_scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname); <- api.c 1x called
102 //sr_scpi_cmd(sdi, devc->device->commands, cmd); <- protocol.c xx called
103 ret = sr_scpi_cmd_resp(sdi, devc->device->commands, &gvdata, gvtype, cmd);
104 if (ret != SR_OK)
105 return ret;
106
107 ch_spec = &devc->device->channels[pch->hw_output_idx];
108 packet.type = SR_DF_ANALOG;
109 packet.payload = &analog;
110 /* Note: digits/spec_digits will be overridden later. */
111 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
112 analog.meaning->channels = g_slist_append(NULL, devc->cur_channel);
113 analog.num_samples = 1;
114 analog.meaning->mq = pch->mq;
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 }
128 analog.meaning->mqflags = SR_MQFLAG_DC;
129 f = (float)g_variant_get_double(gvdata);
130 analog.data = &f;
131 sr_session_send(sdi, &packet);
132 g_slist_free(analog.meaning->channels);
133
60475cd7 134 if (g_slist_length(sdi->channels) > 1) {
9c24d16a 135 next_channel = sr_next_enabled_channel(sdi, devc->cur_channel);
624503ae 136 if (select_channel(sdi, next_channel) != SR_OK) {
60475cd7
BV
137 sr_err("Failed to select channel %s", next_channel->name);
138 return FALSE;
139 }
140 }
01b0257a 141
ca1a7cb5
BV
142 return TRUE;
143}