]> sigrok.org Git - libsigrok.git/blob - src/hardware/gwinstek-gds-800/api.c
Introduce standard implementation of the dev_list() callback
[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 int dev_open(struct sr_dev_inst *sdi)
97 {
98         int ret;
99         struct sr_scpi_dev_inst *scpi = sdi->conn;
100
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         }
105
106         sdi->status = SR_ST_ACTIVE;
107
108         return SR_OK;
109 }
110
111 static int dev_close(struct sr_dev_inst *sdi)
112 {
113         struct sr_scpi_dev_inst *scpi;
114
115         if (sdi->status != SR_ST_ACTIVE)
116                 return SR_ERR_DEV_CLOSED;
117
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         }
124
125         return SR_OK;
126 }
127
128 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
129                 const struct sr_channel_group *cg)
130 {
131         struct dev_context *devc;
132
133         (void)cg;
134
135         if (!sdi || !(devc = sdi->priv))
136                 return SR_ERR_ARG;
137
138         switch (key) {
139         case SR_CONF_SAMPLERATE:
140                 *data = g_variant_new_uint64(devc->sample_rate);
141                 break;
142         default:
143                 return SR_ERR_NA;
144         }
145
146         return SR_OK;
147 }
148
149 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
150                 const struct sr_channel_group *cg)
151 {
152         struct dev_context *devc;
153
154         (void)cg;
155
156         if (!sdi || !(devc = sdi->priv))
157                 return SR_ERR_ARG;
158
159         if (sdi->status != SR_ST_ACTIVE)
160                 return SR_ERR_DEV_CLOSED;
161
162         switch (key) {
163         case SR_CONF_LIMIT_FRAMES:
164                 devc->frame_limit = g_variant_get_uint64(data);
165                 break;
166         default:
167                 return SR_ERR_NA;
168         }
169
170         return SR_OK;
171 }
172
173 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
174                 const struct sr_channel_group *cg)
175 {
176         (void)sdi;
177         (void)cg;
178
179         switch (key) {
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;
188         default:
189                 return SR_ERR_NA;
190         }
191
192         return SR_OK;
193 }
194
195 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
196 {
197         struct sr_scpi_dev_inst *scpi;
198         struct dev_context *devc;
199
200         scpi = sdi->conn;
201         devc = sdi->priv;
202
203         if (sdi->status != SR_ST_ACTIVE)
204                 return SR_ERR_DEV_CLOSED;
205
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);
211
212         return SR_OK;
213 }
214
215 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
216 {
217         struct sr_scpi_dev_inst *scpi;
218         struct dev_context *devc;
219         struct sr_datafeed_packet packet;
220
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
233                 std_session_send_df_end(sdi, LOG_PREFIX);
234
235                 devc->df_started = FALSE;
236         }
237
238         sr_scpi_source_remove(sdi->session, scpi);
239
240         return SR_OK;
241 }
242
243 SR_PRIV struct sr_dev_driver gwinstek_gds_800_driver_info = {
244         .name = "gwinstek-gds-800",
245         .longname = "GW Instek GDS-800 series",
246         .api_version = 1,
247         .init = init,
248         .cleanup = std_cleanup,
249         .scan = scan,
250         .dev_list = std_dev_list,
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 };