]> sigrok.org Git - libsigrok.git/blob - src/hardware/rohde-schwarz-sme-0x/api.c
sr_dev_close(): Set status to SR_ST_INACTIVE.
[libsigrok.git] / src / hardware / rohde-schwarz-sme-0x / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2016 Vlad Ivanov <vlad.ivanov@lab-systems.ru>
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 <config.h>
21 #include <libserialport.h>
22 #include <scpi.h>
23 #include <string.h>
24
25 #include "protocol.h"
26
27 SR_PRIV struct sr_dev_driver rohde_schwarz_sme_0x_driver_info;
28
29 static const char *manufacturer = "Rohde&Schwarz";
30
31 static const struct rs_device_model device_models[] = {
32         {
33                 .model_str = "SME02",
34                 .freq_max = SR_GHZ(1.5),
35                 .freq_min = SR_KHZ(5),
36                 .power_max = 16,
37                 .power_min = -144,
38         },
39         {
40                 .model_str = "SME03E",
41                 .freq_max = SR_GHZ(2.2),
42                 .freq_min = SR_KHZ(5),
43                 .power_max = 16,
44                 .power_min = -144,
45         },
46         {
47                 .model_str = "SME03A",
48                 .freq_max = SR_GHZ(3),
49                 .freq_min = SR_KHZ(5),
50                 .power_max = 16,
51                 .power_min = -144,
52         },
53         {
54                 .model_str = "SME03",
55                 .freq_max = SR_GHZ(3),
56                 .freq_min = SR_KHZ(5),
57                 .power_max = 16,
58                 .power_min = -144,
59         },
60         {
61                 .model_str = "SME06",
62                 .freq_max = SR_GHZ(1.5),
63                 .freq_min = SR_KHZ(5),
64                 .power_max = 16,
65                 .power_min = -144,
66         }
67 };
68
69 static const uint32_t scanopts[] = {
70         SR_CONF_CONN,
71         SR_CONF_SERIALCOMM,
72 };
73
74 static const uint32_t drvopts[] = {
75         SR_CONF_SIGNAL_GENERATOR,
76 };
77
78 static const uint32_t devopts[] = {
79         SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
80         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
81 };
82
83 static int rs_init_device(struct sr_dev_inst *sdi)
84 {
85         struct dev_context *devc;
86         uint8_t model_found;
87
88         devc = sdi->priv;
89         model_found = 0;
90
91         for (size_t i = 0; i < ARRAY_SIZE(device_models); i++) {
92                 if (!strcmp(device_models[i].model_str, sdi->model)) {
93                         model_found = 1;
94                         devc->model_config = &device_models[i];
95                         break;
96                 }
97         }
98
99         if (!model_found) {
100                 sr_dbg("Device %s %s is not supported by this driver.",
101                         manufacturer, sdi->model);
102                 return SR_ERR_NA;
103         }
104
105         return SR_OK;
106 }
107
108 static struct sr_dev_inst *rs_probe_serial_device(struct sr_scpi_dev_inst *scpi)
109 {
110         struct sr_dev_inst *sdi;
111         struct dev_context *devc;
112         struct sr_scpi_hw_info *hw_info;
113
114         sdi = NULL;
115         devc = NULL;
116         hw_info = NULL;
117
118         rs_sme0x_mode_remote(scpi);
119
120         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK)
121                 goto fail;
122
123         if (strcmp(hw_info->manufacturer, manufacturer) != 0)
124                 goto fail;
125
126         sdi = g_malloc0(sizeof(struct sr_dev_inst));
127         sdi->vendor = g_strdup(hw_info->manufacturer);
128         sdi->model = g_strdup(hw_info->model);
129         sdi->version = g_strdup(hw_info->firmware_version);
130         sdi->serial_num = g_strdup(hw_info->serial_number);
131         sdi->driver = &rohde_schwarz_sme_0x_driver_info;
132         sdi->inst_type = SR_INST_SCPI;
133         sdi->conn = scpi;
134
135         sr_scpi_hw_info_free(hw_info);
136         hw_info = NULL;
137
138         devc = g_malloc0(sizeof(struct dev_context));
139         sdi->priv = devc;
140
141         if (rs_init_device(sdi) != SR_OK)
142                 goto fail;
143
144         return sdi;
145
146 fail:
147         sr_scpi_hw_info_free(hw_info);
148         sr_dev_inst_free(sdi);
149         g_free(devc);
150         return NULL;
151 }
152
153 static GSList *scan(struct sr_dev_driver *di, GSList *options)
154 {
155         return sr_scpi_scan(di->context, options, rs_probe_serial_device);
156 }
157
158 static int dev_clear(const struct sr_dev_driver *di)
159 {
160         return std_dev_clear(di, NULL);
161 }
162
163 static int dev_open(struct sr_dev_inst *sdi)
164 {
165         return sr_scpi_open(sdi->conn);
166 }
167
168 static int dev_close(struct sr_dev_inst *sdi)
169 {
170         return sr_scpi_close(sdi->conn);
171 }
172
173 static int config_get(uint32_t key, GVariant **data,
174         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
175 {
176         double value_f;
177
178         (void) cg;
179
180         switch (key) {
181         case SR_CONF_OUTPUT_FREQUENCY:
182                 rs_sme0x_get_freq(sdi, &value_f);
183                 *data = g_variant_new_double(value_f);
184                 break;
185         case SR_CONF_AMPLITUDE:
186                 rs_sme0x_get_power(sdi, &value_f);
187                 *data = g_variant_new_double(value_f);
188                 break;
189         default:
190                 return SR_ERR_NA;
191         }
192
193         return SR_OK;
194 }
195
196 static int config_set(uint32_t key, GVariant *data,
197         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
198 {
199         double value_f;
200
201         (void)cg;
202
203         if (!sdi)
204                 return SR_ERR_ARG;
205
206         switch (key) {
207         case SR_CONF_OUTPUT_FREQUENCY:
208                 value_f = g_variant_get_double(data);
209                 rs_sme0x_set_freq(sdi, value_f);
210                 break;
211         case SR_CONF_AMPLITUDE:
212                 value_f = g_variant_get_double(data);
213                 rs_sme0x_set_power(sdi, value_f);
214                 break;
215         default:
216                 return SR_ERR_NA;
217         }
218
219         return SR_OK;
220 }
221
222 static int config_list(uint32_t key, GVariant **data,
223         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
224 {
225         (void)sdi;
226         (void)cg;
227
228         /* Return drvopts without sdi (and devopts with sdi, see below). */
229         if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
230                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
231                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
232                 return SR_OK;
233         }
234
235         switch (key) {
236         case SR_CONF_SCAN_OPTIONS:
237                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
238                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
239                 break;
240         case SR_CONF_DEVICE_OPTIONS:
241                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
242                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
243                 break;
244         default:
245                 return SR_ERR_NA;
246         }
247
248         return SR_OK;
249 }
250
251 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
252 {
253         (void)sdi;
254
255         return SR_OK;
256 }
257
258 SR_PRIV struct sr_dev_driver rohde_schwarz_sme_0x_driver_info = {
259         .name = "rohde-schwarz-sme-0x",
260         .longname = "Rohde&Schwarz SME-0x",
261         .api_version = 1,
262         .init = std_init,
263         .cleanup = std_cleanup,
264         .scan = scan,
265         .dev_list = std_dev_list,
266         .dev_clear = dev_clear,
267         .config_get = config_get,
268         .config_set = config_set,
269         .config_list = config_list,
270         .dev_open = dev_open,
271         .dev_close = dev_close,
272         .dev_acquisition_start = dev_acquisition_start,
273         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
274         .context = NULL,
275 };
276
277 SR_REGISTER_DEV_DRIVER(rohde_schwarz_sme_0x_driver_info);