Bert Vermeulen | ca1a7cb | 2014-08-31 17:20:39 +0200 | [diff] [blame] | 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 | |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <stdarg.h> |
Bert Vermeulen | ca1a7cb | 2014-08-31 17:20:39 +0200 | [diff] [blame] | 22 | #include "protocol.h" |
| 23 | |
Bert Vermeulen | 478c8d9 | 2014-09-08 00:02:03 +0200 | [diff] [blame] | 24 | SR_PRIV char *scpi_cmd_get(const struct sr_dev_inst *sdi, int command) |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 25 | { |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 26 | struct dev_context *devc; |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 27 | unsigned int i; |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 28 | char *cmd; |
| 29 | |
| 30 | devc = sdi->priv; |
| 31 | cmd = NULL; |
| 32 | for (i = 0; i < devc->device->num_commands; i++) { |
| 33 | if (devc->device->commands[i].command == command) { |
| 34 | cmd = devc->device->commands[i].string; |
| 35 | break; |
| 36 | } |
| 37 | } |
Bert Vermeulen | 478c8d9 | 2014-09-08 00:02:03 +0200 | [diff] [blame] | 38 | |
| 39 | return cmd; |
| 40 | } |
| 41 | |
| 42 | SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, int command, ...) |
| 43 | { |
| 44 | struct sr_scpi_dev_inst *scpi; |
| 45 | va_list args; |
| 46 | int ret; |
| 47 | char *cmd; |
| 48 | |
| 49 | if (!(cmd = scpi_cmd_get(sdi, command))) { |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 50 | /* Device does not implement this command, that's OK. */ |
| 51 | return SR_OK_CONTINUE; |
| 52 | } |
| 53 | |
| 54 | scpi = sdi->conn; |
| 55 | va_start(args, command); |
| 56 | ret = sr_scpi_send_variadic(scpi, cmd, args); |
| 57 | va_end(args); |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
Bert Vermeulen | 478c8d9 | 2014-09-08 00:02:03 +0200 | [diff] [blame] | 62 | SR_PRIV int scpi_cmd_resp(const struct sr_dev_inst *sdi, GVariant **gvar, |
| 63 | const GVariantType *gvtype, int command, ...) |
| 64 | { |
| 65 | struct sr_scpi_dev_inst *scpi; |
| 66 | va_list args; |
| 67 | double d; |
| 68 | int ret; |
| 69 | char *cmd, *s; |
| 70 | |
| 71 | if (!(cmd = scpi_cmd_get(sdi, command))) { |
| 72 | /* Device does not implement this command, that's OK. */ |
| 73 | return SR_OK_CONTINUE; |
| 74 | } |
| 75 | |
| 76 | scpi = sdi->conn; |
| 77 | va_start(args, command); |
| 78 | ret = sr_scpi_send_variadic(scpi, cmd, args); |
| 79 | va_end(args); |
| 80 | if (ret != SR_OK) |
| 81 | return ret; |
| 82 | |
| 83 | if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) { |
| 84 | if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK) |
| 85 | return ret; |
| 86 | if (!strcasecmp(s, "ON") || !strcasecmp(s, "1") || !strcasecmp(s, "YES")) |
| 87 | *gvar = g_variant_new_boolean(TRUE); |
| 88 | else if (!strcasecmp(s, "OFF") || !strcasecmp(s, "0") || !strcasecmp(s, "NO")) |
| 89 | *gvar = g_variant_new_boolean(FALSE); |
| 90 | else |
| 91 | ret = SR_ERR; |
| 92 | } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) { |
| 93 | if ((ret = sr_scpi_get_double(scpi, NULL, &d)) == SR_OK) |
| 94 | *gvar = g_variant_new_double(d); |
| 95 | } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) { |
| 96 | if ((ret = sr_scpi_get_string(scpi, NULL, &s)) == SR_OK) |
| 97 | *gvar = g_variant_new_string(s); |
| 98 | } |
| 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
Bert Vermeulen | ca1a7cb | 2014-08-31 17:20:39 +0200 | [diff] [blame] | 103 | SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data) |
| 104 | { |
Bert Vermeulen | ca1a7cb | 2014-08-31 17:20:39 +0200 | [diff] [blame] | 105 | struct dev_context *devc; |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 106 | struct sr_datafeed_packet packet; |
| 107 | struct sr_datafeed_analog analog; |
| 108 | const struct sr_dev_inst *sdi; |
| 109 | struct sr_scpi_dev_inst *scpi; |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 110 | struct pps_channel *pch; |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 111 | GSList *l; |
| 112 | float f; |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 113 | int cmd; |
Bert Vermeulen | ca1a7cb | 2014-08-31 17:20:39 +0200 | [diff] [blame] | 114 | |
| 115 | (void)fd; |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 116 | (void)revents; |
Bert Vermeulen | ca1a7cb | 2014-08-31 17:20:39 +0200 | [diff] [blame] | 117 | |
| 118 | if (!(sdi = cb_data)) |
| 119 | return TRUE; |
| 120 | |
| 121 | if (!(devc = sdi->priv)) |
| 122 | return TRUE; |
| 123 | |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 124 | scpi = sdi->conn; |
| 125 | |
| 126 | /* Retrieve requested value for this state. */ |
| 127 | if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) { |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 128 | pch = devc->cur_channel->priv; |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 129 | packet.type = SR_DF_ANALOG; |
| 130 | packet.payload = &analog; |
| 131 | analog.channels = g_slist_append(NULL, devc->cur_channel); |
| 132 | analog.num_samples = 1; |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 133 | analog.mq = pch->mq; |
| 134 | if (pch->mq == SR_MQ_VOLTAGE) |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 135 | analog.unit = SR_UNIT_VOLT; |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 136 | else if (pch->mq == SR_MQ_CURRENT) |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 137 | analog.unit = SR_UNIT_AMPERE; |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 138 | else if (pch->mq == SR_MQ_POWER) |
| 139 | analog.unit = SR_UNIT_WATT; |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 140 | analog.mqflags = SR_MQFLAG_DC; |
| 141 | analog.data = &f; |
| 142 | sr_session_send(sdi, &packet); |
| 143 | g_slist_free(analog.channels); |
| 144 | } |
| 145 | |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 146 | /* Find next enabled channel. */ |
| 147 | do { |
| 148 | l = g_slist_find(sdi->channels, devc->cur_channel); |
| 149 | if (l->next) |
| 150 | devc->cur_channel = l->next->data; |
| 151 | else |
| 152 | devc->cur_channel = sdi->channels->data; |
| 153 | } while (!devc->cur_channel->enabled); |
Bert Vermeulen | 9e45cd4 | 2014-09-05 03:20:32 +0200 | [diff] [blame] | 154 | |
Bert Vermeulen | 01b0257 | 2014-09-08 03:26:19 +0200 | [diff] [blame] | 155 | pch = devc->cur_channel->priv; |
| 156 | if (pch->mq == SR_MQ_VOLTAGE) |
| 157 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; |
| 158 | else if (pch->mq == SR_MQ_CURRENT) |
| 159 | cmd = SCPI_CMD_GET_MEAS_CURRENT; |
| 160 | else if (pch->mq == SR_MQ_POWER) |
| 161 | cmd = SCPI_CMD_GET_MEAS_POWER; |
| 162 | else |
| 163 | return SR_ERR; |
| 164 | scpi_cmd(sdi, cmd, pch->hwname); |
Bert Vermeulen | ca1a7cb | 2014-08-31 17:20:39 +0200 | [diff] [blame] | 165 | |
| 166 | return TRUE; |
| 167 | } |