]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/protocol.c
scpi: Make helper functions from scpi-pps available library-wide.
[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  *
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 <string.h>
21 #include <strings.h>
22 #include <stdarg.h>
23 #include "scpi.h"
24 #include "protocol.h"
25
26 SR_PRIV int select_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch)
27 {
28         struct dev_context *devc;
29         struct pps_channel *cur_pch, *new_pch;
30         int ret;
31
32         if (g_slist_length(sdi->channels) == 1)
33                 return SR_OK;
34
35         devc = sdi->priv;
36         if (ch == devc->cur_channel)
37                 return SR_OK;
38
39         new_pch = ch->priv;
40         if (devc->cur_channel) {
41                 cur_pch = devc->cur_channel->priv;
42                 if (cur_pch->hw_output_idx == new_pch->hw_output_idx) {
43                         /* Same underlying output channel. */
44                         devc->cur_channel = ch;
45                         return SR_OK;
46                 }
47         }
48
49         if ((ret = scpi_cmd(sdi, devc->device->commands, SCPI_CMD_SELECT_CHANNEL,
50                         new_pch->hwname)) >= 0)
51                 devc->cur_channel = ch;
52
53         return ret;
54 }
55
56 SR_PRIV struct sr_channel *next_enabled_channel(const struct sr_dev_inst *sdi,
57                 struct sr_channel *cur_channel)
58 {
59         struct sr_channel *next_channel;
60         GSList *l;
61
62         next_channel = cur_channel;
63         do {
64                 l = g_slist_find(sdi->channels, next_channel);
65                 if (l && l->next)
66                         next_channel = l->next->data;
67                 else
68                         next_channel = sdi->channels->data;
69         } while (!next_channel->enabled);
70
71         return next_channel;
72 }
73
74 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
75 {
76         struct dev_context *devc;
77         struct sr_datafeed_packet packet;
78         struct sr_datafeed_analog analog;
79         const struct sr_dev_inst *sdi;
80         struct sr_channel *next_channel;
81         struct sr_scpi_dev_inst *scpi;
82         struct pps_channel *pch;
83         float f;
84         int cmd;
85
86         (void)fd;
87         (void)revents;
88
89         if (!(sdi = cb_data))
90                 return TRUE;
91
92         if (!(devc = sdi->priv))
93                 return TRUE;
94
95         scpi = sdi->conn;
96
97         /* Retrieve requested value for this state. */
98         if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
99                 pch = devc->cur_channel->priv;
100                 packet.type = SR_DF_ANALOG;
101                 packet.payload = &analog;
102                 analog.channels = g_slist_append(NULL, devc->cur_channel);
103                 analog.num_samples = 1;
104                 analog.mq = pch->mq;
105                 if (pch->mq == SR_MQ_VOLTAGE)
106                         analog.unit = SR_UNIT_VOLT;
107                 else if (pch->mq == SR_MQ_CURRENT)
108                         analog.unit = SR_UNIT_AMPERE;
109                 else if (pch->mq == SR_MQ_POWER)
110                         analog.unit = SR_UNIT_WATT;
111                 analog.mqflags = SR_MQFLAG_DC;
112                 analog.data = &f;
113                 sr_session_send(sdi, &packet);
114                 g_slist_free(analog.channels);
115         }
116
117         if (g_slist_length(sdi->channels) > 1) {
118                 next_channel = next_enabled_channel(sdi, devc->cur_channel);
119                 if (select_channel(sdi, next_channel) != SR_OK) {
120                         sr_err("Failed to select channel %s", next_channel->name);
121                         return FALSE;
122                 }
123         }
124
125         pch = devc->cur_channel->priv;
126         if (pch->mq == SR_MQ_VOLTAGE)
127                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
128         else if (pch->mq == SR_MQ_FREQUENCY)
129                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
130         else if (pch->mq == SR_MQ_CURRENT)
131                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
132         else if (pch->mq == SR_MQ_POWER)
133                 cmd = SCPI_CMD_GET_MEAS_POWER;
134         else
135                 return SR_ERR;
136         scpi_cmd(sdi, devc->device->commands, cmd);
137
138         return TRUE;
139 }