]> sigrok.org Git - libsigrok.git/blame - hardware/manson-hcs-3xxx/api.c
manson-hcs-3xxx: Fixed build without libserialport.
[libsigrok.git] / hardware / manson-hcs-3xxx / api.c
CommitLineData
8f4e922f
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "protocol.h"
22
b5e92647
UH
23static const int32_t hwopts[] = {
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26};
27
28static const int32_t devopts[] = {
29 SR_CONF_POWER_SUPPLY,
30 SR_CONF_LIMIT_SAMPLES,
31 SR_CONF_LIMIT_MSEC,
32 SR_CONF_CONTINUOUS,
33 SR_CONF_OUTPUT_VOLTAGE,
34 SR_CONF_OUTPUT_CURRENT,
35};
36
37/* Note: All models have one power supply output only. */
38static struct hcs_model models[] = {
39 { MANSON_HCS_3200, "HCS-3200", { 1, 18, 0.1 }, { 0, 20, 0.01 } },
40 { MANSON_HCS_3202, "HCS-3202", { 1, 36, 0.1 }, { 0, 10, 0.01 } },
41 { MANSON_HCS_3204, "HCS-3204", { 1, 60, 0.1 }, { 0, 5, 0.01 } },
42 { 0, NULL, { 0, 0, 0 }, { 0, 0, 0 }, },
43};
44
8f4e922f
UH
45SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info;
46static struct sr_dev_driver *di = &manson_hcs_3xxx_driver_info;
47
b5e92647
UH
48static int dev_clear(void)
49{
50 return std_dev_clear(di, NULL);
51}
52
8f4e922f
UH
53static int init(struct sr_context *sr_ctx)
54{
55 return std_init(sr_ctx, di, LOG_PREFIX);
56}
57
58static GSList *scan(GSList *options)
59{
b5e92647 60 int i, model_id;
8f4e922f 61 struct drv_context *drvc;
b5e92647
UH
62 struct dev_context *devc;
63 struct sr_dev_inst *sdi;
64 struct sr_config *src;
65 struct sr_channel *ch;
66 GSList *devices, *l;
67 const char *conn, *serialcomm;
68 struct sr_serial_dev_inst *serial;
69 char reply[50], **tokens;
8f4e922f 70
8f4e922f
UH
71 drvc = di->priv;
72 drvc->instances = NULL;
b5e92647
UH
73 devices = NULL;
74 conn = NULL;
75 serialcomm = NULL;
76
77 for (l = options; l; l = l->next) {
78 src = l->data;
79 switch (src->key) {
80 case SR_CONF_CONN:
81 conn = g_variant_get_string(src->data, NULL);
82 break;
83 case SR_CONF_SERIALCOMM:
84 serialcomm = g_variant_get_string(src->data, NULL);
85 break;
86 default:
87 sr_err("Unknown option %d, skipping.", src->key);
88 break;
89 }
90 }
8f4e922f 91
b5e92647
UH
92 if (!conn)
93 return NULL;
94 if (!serialcomm)
95 serialcomm = "9600/8n1";
8f4e922f 96
b5e92647
UH
97 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
98 return NULL;
8f4e922f 99
b5e92647
UH
100 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
101 return NULL;
8f4e922f 102
b5e92647 103 serial_flush(serial);
8f4e922f 104
b5e92647 105 sr_info("Probing serial port %s.", conn);
8f4e922f 106
b5e92647
UH
107 /* Get the device model. */
108 memset(&reply, 0, sizeof(reply));
109 serial_write_blocking(serial, "GMOD\r", 5);
110 serial_read_blocking(serial, &reply, 8);
111 tokens = g_strsplit((const gchar *)&reply, "\r", 2);
8f4e922f 112
b5e92647
UH
113 model_id = -1;
114 for (i = 0; models[i].name != NULL; i++) {
115 if (!strcmp(models[i].name + 4, tokens[0]))
116 model_id = i;
117 }
118 if (model_id < 0) {
119 sr_err("Unknown model '%s' detected, aborting.", tokens[0]);
120 return NULL;
121 }
122
123 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Manson",
124 models[model_id].name, NULL))) {
125 sr_err("Failed to create device instance.");
126 return NULL;
127 }
8f4e922f 128
b5e92647 129 g_strfreev(tokens);
8f4e922f 130
b5e92647
UH
131 sdi->inst_type = SR_INST_SERIAL;
132 sdi->conn = serial;
133 sdi->driver = di;
134
135 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "CH1"))) {
136 sr_err("Failed to create channel.");
137 return NULL;
138 }
139 sdi->channels = g_slist_append(sdi->channels, ch);
140
141 devc = g_malloc0(sizeof(struct dev_context));
142 devc->model = &models[model_id];
143
144 sdi->priv = devc;
145
146 drvc->instances = g_slist_append(drvc->instances, sdi);
147 devices = g_slist_append(devices, sdi);
148
149 serial_close(serial);
150 if (!devices)
151 sr_serial_dev_inst_free(serial);
152
153 return devices;
154}
155
156static GSList *dev_list(void)
157{
158 return ((struct drv_context *)(di->priv))->instances;
8f4e922f
UH
159}
160
161static int cleanup(void)
162{
163 return dev_clear();
164}
165
166static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
167 const struct sr_channel_group *cg)
168{
b5e92647 169 struct dev_context *devc;
8f4e922f 170
8f4e922f
UH
171 (void)cg;
172
b5e92647
UH
173 if (!sdi)
174 return SR_ERR_ARG;
175
176 devc = sdi->priv;
177
8f4e922f 178 switch (key) {
b5e92647
UH
179 case SR_CONF_LIMIT_SAMPLES:
180 *data = g_variant_new_uint64(devc->limit_samples);
181 break;
182 case SR_CONF_LIMIT_MSEC:
183 *data = g_variant_new_uint64(devc->limit_msec);
184 break;
185 case SR_CONF_OUTPUT_VOLTAGE:
186 *data = g_variant_new_double(devc->voltage);
187 break;
188 case SR_CONF_OUTPUT_CURRENT:
189 *data = g_variant_new_double(devc->current);
190 break;
8f4e922f
UH
191 default:
192 return SR_ERR_NA;
193 }
194
b5e92647 195 return SR_OK;
8f4e922f
UH
196}
197
198static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
199 const struct sr_channel_group *cg)
200{
b5e92647 201 struct dev_context *devc;
8f4e922f 202
8f4e922f
UH
203 (void)cg;
204
205 if (sdi->status != SR_ST_ACTIVE)
206 return SR_ERR_DEV_CLOSED;
207
b5e92647
UH
208 devc = sdi->priv;
209
8f4e922f 210 switch (key) {
b5e92647
UH
211 case SR_CONF_LIMIT_MSEC:
212 if (g_variant_get_uint64(data) == 0)
213 return SR_ERR_ARG;
214 devc->limit_msec = g_variant_get_uint64(data);
215 break;
216 case SR_CONF_LIMIT_SAMPLES:
217 if (g_variant_get_uint64(data) == 0)
218 return SR_ERR_ARG;
219 devc->limit_samples = g_variant_get_uint64(data);
220 break;
8f4e922f 221 default:
b5e92647 222 return SR_ERR_NA;
8f4e922f
UH
223 }
224
b5e92647 225 return SR_OK;
8f4e922f
UH
226}
227
228static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
229 const struct sr_channel_group *cg)
230{
8f4e922f 231 (void)sdi;
8f4e922f
UH
232 (void)cg;
233
8f4e922f 234 switch (key) {
b5e92647
UH
235 case SR_CONF_SCAN_OPTIONS:
236 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
237 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
238 break;
239 case SR_CONF_DEVICE_OPTIONS:
240 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
241 devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
242 break;
8f4e922f
UH
243 default:
244 return SR_ERR_NA;
245 }
246
b5e92647 247 return SR_OK;
8f4e922f
UH
248}
249
b5e92647 250static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
8f4e922f 251{
b5e92647
UH
252 struct dev_context *devc;
253 struct sr_serial_dev_inst *serial;
8f4e922f
UH
254
255 if (sdi->status != SR_ST_ACTIVE)
256 return SR_ERR_DEV_CLOSED;
257
b5e92647
UH
258 devc = sdi->priv;
259 devc->cb_data = cb_data;
260
261 /* Send header packet to the session bus. */
262 std_session_send_df_header(cb_data, LOG_PREFIX);
263
264 devc->starttime = g_get_monotonic_time();
265 devc->num_samples = 0;
266 devc->reply_pending = FALSE;
267 devc->req_sent_at = 0;
268
269 /* Poll every 10ms, or whenever some data comes in. */
270 serial = sdi->conn;
271 serial_source_add(serial, G_IO_IN, 10, hcs_receive_data, (void *)sdi);
272
8f4e922f
UH
273 return SR_OK;
274}
275
276static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
277{
b5e92647
UH
278 return std_serial_dev_acquisition_stop(sdi, cb_data,
279 std_serial_dev_close, sdi->conn, LOG_PREFIX);
8f4e922f
UH
280}
281
282SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info = {
283 .name = "manson-hcs-3xxx",
284 .longname = "Manson HCS-3xxx",
285 .api_version = 1,
286 .init = init,
287 .cleanup = cleanup,
288 .scan = scan,
289 .dev_list = dev_list,
290 .dev_clear = dev_clear,
291 .config_get = config_get,
292 .config_set = config_set,
293 .config_list = config_list,
b5e92647
UH
294 .dev_open = std_serial_dev_open,
295 .dev_close = std_serial_dev_close,
8f4e922f
UH
296 .dev_acquisition_start = dev_acquisition_start,
297 .dev_acquisition_stop = dev_acquisition_stop,
298 .priv = NULL,
299};