]> sigrok.org Git - libsigrok.git/blob - src/hardware/gwinstek-gds-800/api.c
Factor out std_session_send_df_end() helper.
[libsigrok.git] / src / hardware / gwinstek-gds-800 / api.c
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
20 #include <string.h>
21 #include "protocol.h"
22
23 static const uint32_t scanopts[] = {
24         SR_CONF_CONN,
25         SR_CONF_SERIALCOMM,
26 };
27
28 static 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
34 SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info;
35
36 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
37 {
38         return std_init(sr_ctx, di, LOG_PREFIX);
39 }
40
41 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
42 {
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         }
52
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         }
58
59         sdi = g_malloc0(sizeof(struct sr_dev_inst));
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 }
90
91 static GSList *scan(struct sr_dev_driver *di, GSList *options)
92 {
93         return sr_scpi_scan(di->context, options, probe_device);
94 }
95
96 static GSList *dev_list(const struct sr_dev_driver *di)
97 {
98         return ((struct drv_context *)(di->context))->instances;
99 }
100
101 static int dev_clear(const struct sr_dev_driver *di)
102 {
103         return std_dev_clear(di, NULL);
104 }
105
106 static int dev_open(struct sr_dev_inst *sdi)
107 {
108         int ret;
109         struct sr_scpi_dev_inst *scpi = sdi->conn;
110
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         }
115
116         sdi->status = SR_ST_ACTIVE;
117
118         return SR_OK;
119 }
120
121 static int dev_close(struct sr_dev_inst *sdi)
122 {
123         struct sr_scpi_dev_inst *scpi;
124
125         if (sdi->status != SR_ST_ACTIVE)
126                 return SR_ERR_DEV_CLOSED;
127
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         }
134
135         return SR_OK;
136 }
137
138 static int cleanup(const struct sr_dev_driver *di)
139 {
140         dev_clear(di);
141
142         return SR_OK;
143 }
144
145 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
146                 const struct sr_channel_group *cg)
147 {
148         struct dev_context *devc;
149
150         (void)cg;
151
152         if (!sdi || !(devc = sdi->priv))
153                 return SR_ERR_ARG;
154
155         switch (key) {
156         case SR_CONF_SAMPLERATE:
157                 *data = g_variant_new_uint64(devc->sample_rate);
158                 break;
159         default:
160                 return SR_ERR_NA;
161         }
162
163         return SR_OK;
164 }
165
166 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
167                 const struct sr_channel_group *cg)
168 {
169         struct dev_context *devc;
170
171         (void)cg;
172
173         if (!sdi || !(devc = sdi->priv))
174                 return SR_ERR_ARG;
175
176         if (sdi->status != SR_ST_ACTIVE)
177                 return SR_ERR_DEV_CLOSED;
178
179         switch (key) {
180         case SR_CONF_LIMIT_FRAMES:
181                 devc->frame_limit = g_variant_get_uint64(data);
182                 break;
183         default:
184                 return SR_ERR_NA;
185         }
186
187         return SR_OK;
188 }
189
190 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
191                 const struct sr_channel_group *cg)
192 {
193         (void)sdi;
194         (void)cg;
195
196         switch (key) {
197         case SR_CONF_SCAN_OPTIONS:
198                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
199                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
200                 return SR_OK;
201         case SR_CONF_DEVICE_OPTIONS:
202                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
203                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
204                 return SR_OK;
205         default:
206                 return SR_ERR_NA;
207         }
208
209         return SR_OK;
210 }
211
212 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
213 {
214         struct sr_scpi_dev_inst *scpi;
215         struct dev_context *devc;
216
217         (void)cb_data;
218
219         scpi = sdi->conn;
220         devc = sdi->priv;
221
222         if (sdi->status != SR_ST_ACTIVE)
223                 return SR_ERR_DEV_CLOSED;
224
225         devc->state = START_ACQUISITION;
226         devc->cur_acq_frame = 0;
227
228         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
229                         gwinstek_gds_800_receive_data, (void *)sdi);
230
231         return SR_OK;
232 }
233
234 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
235 {
236         struct sr_scpi_dev_inst *scpi;
237         struct dev_context *devc;
238         struct sr_datafeed_packet packet;
239
240         (void)cb_data;
241
242         scpi = sdi->conn;
243         devc = sdi->priv;
244
245         if (sdi->status != SR_ST_ACTIVE) {
246                 sr_err("Device inactive, can't stop acquisition.");
247                 return SR_ERR;
248         }
249
250         if (devc->df_started) {
251                 packet.type = SR_DF_FRAME_END;
252                 sr_session_send(sdi, &packet);
253
254                 std_session_send_df_end(sdi, LOG_PREFIX);
255
256                 devc->df_started = FALSE;
257         }
258
259         sr_scpi_source_remove(sdi->session, scpi);
260
261         return SR_OK;
262 }
263
264 SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info = {
265         .name = "gwinstek-gds-800",
266         .longname = "GW Instek GDS-800 series",
267         .api_version = 1,
268         .init = init,
269         .cleanup = cleanup,
270         .scan = scan,
271         .dev_list = dev_list,
272         .dev_clear = dev_clear,
273         .config_get = config_get,
274         .config_set = config_set,
275         .config_list = config_list,
276         .dev_open = dev_open,
277         .dev_close = dev_close,
278         .dev_acquisition_start = dev_acquisition_start,
279         .dev_acquisition_stop = dev_acquisition_stop,
280         .context = NULL,
281 };