]> sigrok.org Git - libsigrok.git/blame - src/hardware/gwinstek-gds-800/api.c
Introduce standard cleanup helper
[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
96static GSList *dev_list(const struct sr_dev_driver *di)
97{
98 return ((struct drv_context *)(di->context))->instances;
99}
100
101static int dev_clear(const struct sr_dev_driver *di)
102{
103 return std_dev_clear(di, NULL);
104}
105
106static int dev_open(struct sr_dev_inst *sdi)
107{
b11afbb1
ML
108 int ret;
109 struct sr_scpi_dev_inst *scpi = sdi->conn;
7c198f96 110
b11afbb1
ML
111 if ((ret = sr_scpi_open(scpi)) < 0) {
112 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
113 return SR_ERR;
114 }
7c198f96
ML
115
116 sdi->status = SR_ST_ACTIVE;
117
118 return SR_OK;
119}
120
121static int dev_close(struct sr_dev_inst *sdi)
122{
b11afbb1 123 struct sr_scpi_dev_inst *scpi;
7c198f96 124
b11afbb1
ML
125 if (sdi->status != SR_ST_ACTIVE)
126 return SR_ERR_DEV_CLOSED;
7c198f96 127
b11afbb1
ML
128 scpi = sdi->conn;
129 if (scpi) {
130 if (sr_scpi_close(scpi) < 0)
131 return SR_ERR;
132 sdi->status = SR_ST_INACTIVE;
133 }
7c198f96
ML
134
135 return SR_OK;
136}
137
7c198f96
ML
138static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
139 const struct sr_channel_group *cg)
140{
b11afbb1 141 struct dev_context *devc;
7c198f96 142
7c198f96
ML
143 (void)cg;
144
b11afbb1
ML
145 if (!sdi || !(devc = sdi->priv))
146 return SR_ERR_ARG;
147
7c198f96 148 switch (key) {
b11afbb1
ML
149 case SR_CONF_SAMPLERATE:
150 *data = g_variant_new_uint64(devc->sample_rate);
151 break;
7c198f96
ML
152 default:
153 return SR_ERR_NA;
154 }
155
b11afbb1 156 return SR_OK;
7c198f96
ML
157}
158
159static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
160 const struct sr_channel_group *cg)
161{
b11afbb1 162 struct dev_context *devc;
7c198f96 163
7c198f96
ML
164 (void)cg;
165
b11afbb1
ML
166 if (!sdi || !(devc = sdi->priv))
167 return SR_ERR_ARG;
168
7c198f96
ML
169 if (sdi->status != SR_ST_ACTIVE)
170 return SR_ERR_DEV_CLOSED;
171
7c198f96 172 switch (key) {
b11afbb1
ML
173 case SR_CONF_LIMIT_FRAMES:
174 devc->frame_limit = g_variant_get_uint64(data);
175 break;
7c198f96 176 default:
b11afbb1 177 return SR_ERR_NA;
7c198f96
ML
178 }
179
b11afbb1 180 return SR_OK;
7c198f96
ML
181}
182
183static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
184 const struct sr_channel_group *cg)
185{
7c198f96 186 (void)sdi;
7c198f96
ML
187 (void)cg;
188
7c198f96 189 switch (key) {
b11afbb1
ML
190 case SR_CONF_SCAN_OPTIONS:
191 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
192 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
193 return SR_OK;
194 case SR_CONF_DEVICE_OPTIONS:
195 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
196 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
197 return SR_OK;
7c198f96
ML
198 default:
199 return SR_ERR_NA;
200 }
201
b11afbb1 202 return SR_OK;
7c198f96
ML
203}
204
695dc859 205static int dev_acquisition_start(const struct sr_dev_inst *sdi)
7c198f96 206{
b11afbb1
ML
207 struct sr_scpi_dev_inst *scpi;
208 struct dev_context *devc;
209
b11afbb1
ML
210 scpi = sdi->conn;
211 devc = sdi->priv;
212
7c198f96
ML
213 if (sdi->status != SR_ST_ACTIVE)
214 return SR_ERR_DEV_CLOSED;
215
b11afbb1
ML
216 devc->state = START_ACQUISITION;
217 devc->cur_acq_frame = 0;
218
219 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
220 gwinstek_gds_800_receive_data, (void *)sdi);
7c198f96
ML
221
222 return SR_OK;
223}
224
695dc859 225static int dev_acquisition_stop(struct sr_dev_inst *sdi)
7c198f96 226{
b11afbb1
ML
227 struct sr_scpi_dev_inst *scpi;
228 struct dev_context *devc;
229 struct sr_datafeed_packet packet;
230
b11afbb1
ML
231 scpi = sdi->conn;
232 devc = sdi->priv;
233
234 if (sdi->status != SR_ST_ACTIVE) {
235 sr_err("Device inactive, can't stop acquisition.");
236 return SR_ERR;
237 }
238
239 if (devc->df_started) {
240 packet.type = SR_DF_FRAME_END;
241 sr_session_send(sdi, &packet);
242
3be42bc2 243 std_session_send_df_end(sdi, LOG_PREFIX);
b11afbb1
ML
244
245 devc->df_started = FALSE;
246 }
7c198f96 247
b11afbb1 248 sr_scpi_source_remove(sdi->session, scpi);
7c198f96
ML
249
250 return SR_OK;
251}
252
253SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info = {
254 .name = "gwinstek-gds-800",
b11afbb1 255 .longname = "GW Instek GDS-800 series",
7c198f96
ML
256 .api_version = 1,
257 .init = init,
700d6b64 258 .cleanup = std_cleanup,
7c198f96
ML
259 .scan = scan,
260 .dev_list = dev_list,
261 .dev_clear = dev_clear,
262 .config_get = config_get,
263 .config_set = config_set,
264 .config_list = config_list,
265 .dev_open = dev_open,
266 .dev_close = dev_close,
267 .dev_acquisition_start = dev_acquisition_start,
268 .dev_acquisition_stop = dev_acquisition_stop,
269 .context = NULL,
270};