]> sigrok.org Git - libsigrok.git/blame - src/hardware/gwinstek-gds-800/api.c
Introduce standard implementation of the dev_list() callback
[libsigrok.git] / src / hardware / gwinstek-gds-800 / api.c
CommitLineData
7c198f96
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Martin Lederhilger <martin.lederhilger@gmx.at>
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
b11afbb1 20#include <string.h>
7c198f96
ML
21#include "protocol.h"
22
b11afbb1
ML
23static const uint32_t scanopts[] = {
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26};
27
28static const uint32_t devopts[] = {
29 SR_CONF_OSCILLOSCOPE,
30 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
31 SR_CONF_SAMPLERATE | SR_CONF_GET,
32};
33
7c198f96
ML
34SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info;
35
36static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
37{
38 return std_init(sr_ctx, di, LOG_PREFIX);
39}
40
b11afbb1 41static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
7c198f96 42{
b11afbb1
ML
43 struct dev_context *devc;
44 struct sr_dev_inst *sdi;
45 struct sr_scpi_hw_info *hw_info;
46 struct sr_channel_group *cg;
47
48 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
49 sr_info("Couldn't get IDN response.");
50 return NULL;
51 }
7c198f96 52
b11afbb1
ML
53 if (strcmp(hw_info->manufacturer, "GW") != 0 ||
54 strncmp(hw_info->model, "GDS-8", 5) != 0) {
55 sr_scpi_hw_info_free(hw_info);
56 return NULL;
57 }
7c198f96 58
b11afbb1 59 sdi = g_malloc0(sizeof(struct sr_dev_inst));
b11afbb1
ML
60 sdi->vendor = g_strdup(hw_info->manufacturer);
61 sdi->model = g_strdup(hw_info->model);
62 sdi->version = g_strdup(hw_info->firmware_version);
63 sdi->conn = scpi;
64 sdi->driver = &gwinstek_gds_800_driver_info;
65 sdi->inst_type = SR_INST_SCPI;
66 sdi->serial_num = g_strdup(hw_info->serial_number);
67 sdi->channels = NULL;
68 sdi->channel_groups = NULL;
69
70 sr_scpi_hw_info_free(hw_info);
71
72 devc = g_malloc0(sizeof(struct dev_context));
73 devc->frame_limit = 1;
74 devc->sample_rate = 0.;
75 devc->df_started = FALSE;
76 sdi->priv = devc;
77
78 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1");
79 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "CH2");
80
81 cg = g_malloc0(sizeof(struct sr_channel_group));
82 cg->name = g_strdup("");
83 cg->channels = g_slist_append(cg->channels, g_slist_nth_data(sdi->channels, 0));
84 cg->channels = g_slist_append(cg->channels, g_slist_nth_data(sdi->channels, 1));
85 cg->priv = NULL;
86 sdi->channel_groups = g_slist_append(NULL, cg);
87
88 return sdi;
89}
7c198f96 90
b11afbb1
ML
91static GSList *scan(struct sr_dev_driver *di, GSList *options)
92{
93 return sr_scpi_scan(di->context, options, probe_device);
7c198f96
ML
94}
95
7c198f96
ML
96static int dev_open(struct sr_dev_inst *sdi)
97{
b11afbb1
ML
98 int ret;
99 struct sr_scpi_dev_inst *scpi = sdi->conn;
7c198f96 100
b11afbb1
ML
101 if ((ret = sr_scpi_open(scpi)) < 0) {
102 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
103 return SR_ERR;
104 }
7c198f96
ML
105
106 sdi->status = SR_ST_ACTIVE;
107
108 return SR_OK;
109}
110
111static int dev_close(struct sr_dev_inst *sdi)
112{
b11afbb1 113 struct sr_scpi_dev_inst *scpi;
7c198f96 114
b11afbb1
ML
115 if (sdi->status != SR_ST_ACTIVE)
116 return SR_ERR_DEV_CLOSED;
7c198f96 117
b11afbb1
ML
118 scpi = sdi->conn;
119 if (scpi) {
120 if (sr_scpi_close(scpi) < 0)
121 return SR_ERR;
122 sdi->status = SR_ST_INACTIVE;
123 }
7c198f96
ML
124
125 return SR_OK;
126}
127
7c198f96
ML
128static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
129 const struct sr_channel_group *cg)
130{
b11afbb1 131 struct dev_context *devc;
7c198f96 132
7c198f96
ML
133 (void)cg;
134
b11afbb1
ML
135 if (!sdi || !(devc = sdi->priv))
136 return SR_ERR_ARG;
137
7c198f96 138 switch (key) {
b11afbb1
ML
139 case SR_CONF_SAMPLERATE:
140 *data = g_variant_new_uint64(devc->sample_rate);
141 break;
7c198f96
ML
142 default:
143 return SR_ERR_NA;
144 }
145
b11afbb1 146 return SR_OK;
7c198f96
ML
147}
148
149static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
150 const struct sr_channel_group *cg)
151{
b11afbb1 152 struct dev_context *devc;
7c198f96 153
7c198f96
ML
154 (void)cg;
155
b11afbb1
ML
156 if (!sdi || !(devc = sdi->priv))
157 return SR_ERR_ARG;
158
7c198f96
ML
159 if (sdi->status != SR_ST_ACTIVE)
160 return SR_ERR_DEV_CLOSED;
161
7c198f96 162 switch (key) {
b11afbb1
ML
163 case SR_CONF_LIMIT_FRAMES:
164 devc->frame_limit = g_variant_get_uint64(data);
165 break;
7c198f96 166 default:
b11afbb1 167 return SR_ERR_NA;
7c198f96
ML
168 }
169
b11afbb1 170 return SR_OK;
7c198f96
ML
171}
172
173static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
174 const struct sr_channel_group *cg)
175{
7c198f96 176 (void)sdi;
7c198f96
ML
177 (void)cg;
178
7c198f96 179 switch (key) {
b11afbb1
ML
180 case SR_CONF_SCAN_OPTIONS:
181 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
182 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
183 return SR_OK;
184 case SR_CONF_DEVICE_OPTIONS:
185 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
186 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
187 return SR_OK;
7c198f96
ML
188 default:
189 return SR_ERR_NA;
190 }
191
b11afbb1 192 return SR_OK;
7c198f96
ML
193}
194
695dc859 195static int dev_acquisition_start(const struct sr_dev_inst *sdi)
7c198f96 196{
b11afbb1
ML
197 struct sr_scpi_dev_inst *scpi;
198 struct dev_context *devc;
199
b11afbb1
ML
200 scpi = sdi->conn;
201 devc = sdi->priv;
202
7c198f96
ML
203 if (sdi->status != SR_ST_ACTIVE)
204 return SR_ERR_DEV_CLOSED;
205
b11afbb1
ML
206 devc->state = START_ACQUISITION;
207 devc->cur_acq_frame = 0;
208
209 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
210 gwinstek_gds_800_receive_data, (void *)sdi);
7c198f96
ML
211
212 return SR_OK;
213}
214
695dc859 215static int dev_acquisition_stop(struct sr_dev_inst *sdi)
7c198f96 216{
b11afbb1
ML
217 struct sr_scpi_dev_inst *scpi;
218 struct dev_context *devc;
219 struct sr_datafeed_packet packet;
220
b11afbb1
ML
221 scpi = sdi->conn;
222 devc = sdi->priv;
223
224 if (sdi->status != SR_ST_ACTIVE) {
225 sr_err("Device inactive, can't stop acquisition.");
226 return SR_ERR;
227 }
228
229 if (devc->df_started) {
230 packet.type = SR_DF_FRAME_END;
231 sr_session_send(sdi, &packet);
232
3be42bc2 233 std_session_send_df_end(sdi, LOG_PREFIX);
b11afbb1
ML
234
235 devc->df_started = FALSE;
236 }
7c198f96 237
b11afbb1 238 sr_scpi_source_remove(sdi->session, scpi);
7c198f96
ML
239
240 return SR_OK;
241}
242
243SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info = {
244 .name = "gwinstek-gds-800",
b11afbb1 245 .longname = "GW Instek GDS-800 series",
7c198f96
ML
246 .api_version = 1,
247 .init = init,
700d6b64 248 .cleanup = std_cleanup,
7c198f96 249 .scan = scan,
c01bf34c 250 .dev_list = std_dev_list,
7c198f96
ML
251 .config_get = config_get,
252 .config_set = config_set,
253 .config_list = config_list,
254 .dev_open = dev_open,
255 .dev_close = dev_close,
256 .dev_acquisition_start = dev_acquisition_start,
257 .dev_acquisition_stop = dev_acquisition_stop,
258 .context = NULL,
259};