]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/gmc-mh-1x-2x/api.c
Add std_serial_dev_close() function.
[libsigrok.git] / hardware / gmc-mh-1x-2x / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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
22#include "protocol.h"
23
24/* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */
25#define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */
26#define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0"
27#define VENDOR_GMC "Gossen Metrawatt"
28
29SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info;
30
31static const int32_t hwopts[] = {
32 SR_CONF_CONN,
33 SR_CONF_SERIALCOMM,
34};
35
36static const int32_t hwcaps[] = {
37 SR_CONF_MULTIMETER,
38 SR_CONF_LIMIT_SAMPLES,
39 SR_CONF_LIMIT_MSEC,
40 SR_CONF_CONTINUOUS,
41};
42
43/** Driver init function */
44static int init_1x_2x_rs232(struct sr_context *sr_ctx)
45{
46 return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX);
47}
48
49
50/** Read single byte from serial port.
51 * \retval -1 Timeout or error.
52 * \retval other Byte.
53 */
54static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
55{
56 guint8 result = 0;
57 int rc = 0;
58
59 for (;;) {
60 rc = serial_read(serial, &result, 1);
61 if (rc == 1) {
62 sr_spew("read: 0x%02x/%d", result, result);
63 return result;
64 }
65 if (g_get_monotonic_time() > timeout)
66 return -1;
67 g_usleep(2000);
68 }
69}
70
71/** Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
72 * \param serial Configured, open serial port.
73 *
74 * \retval NULL Detection failed.
75 * \retval other Model code.
76 */
77static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
78{
79 int byte, bytecnt, cnt;
80 enum model model;
81 gint64 timeout_us;
82
83 model = SR_METRAHIT_NONE;
84 timeout_us = g_get_monotonic_time() + 1*1000*1000;
85
86 /* Try to find message consisting of device code and several
87 * (at least 4) data bytes. */
88 for (bytecnt = 0; bytecnt < 100; bytecnt++) {
89 byte = read_byte(serial, timeout_us);
90 if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
91 break;
92 if ((byte & MSGID_MASK) == MSGID_INF) {
93 if (!(model = sr_gmc_decode_model_sm(byte & MSGC_MASK)))
94 break;
95 /* Now expect (at least) 4 data bytes */
96 for (cnt = 0; cnt < 4; cnt++) {
97 byte = read_byte(serial, timeout_us);
98 if ((byte == -1) ||
99 ((byte & MSGID_MASK) != MSGID_DATA))
100 {
101 model = SR_METRAHIT_NONE;
102 bytecnt = 100;
103 break;
104 }
105 }
106 break;
107 }
108 }
109
110 return model;
111}
112
113/** Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
114 * 'RS232' interface. The older 1x models use 8192 and the newer 2x 9600 baud.
115 * The DMM usually sends up to about 20 messages per second. However, depending
116 * on configuration and measurement mode the intervals can be much larger and
117 * then the detection might not work.
118 */
119static GSList *scan_1x_2x_rs232(GSList *options)
120{
121 struct sr_dev_inst *sdi;
122 struct drv_context *drvc;
123 struct dev_context *devc;
124 struct sr_config *src;
125 struct sr_probe *probe;
126 struct sr_serial_dev_inst *serial;
127 GSList *l, *devices;
128 const char *conn, *serialcomm;
129 enum model model;
130 gboolean serialcomm_given;
131 int cnt;
132
133 devices = NULL;
134 drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv;
135 drvc->instances = NULL;
136 conn = serialcomm = NULL;
137 model = SR_METRAHIT_NONE;
138 serialcomm_given = FALSE;
139
140 sr_spew("scan_1x_2x_rs232() called!");
141
142 for (l = options; l; l = l->next) {
143 src = l->data;
144 switch (src->key) {
145 case SR_CONF_CONN:
146 conn = g_variant_get_string(src->data, NULL);
147 break;
148 case SR_CONF_SERIALCOMM:
149 serialcomm = g_variant_get_string(src->data, NULL);
150 serialcomm_given = TRUE;
151 break;
152 }
153 }
154 if (!conn)
155 return NULL;
156 if (!serialcomm)
157 serialcomm = SERIALCOMM_2X_RS232;
158
159 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
160 return NULL;
161
162 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) {
163 sr_serial_dev_inst_free(serial);
164 return NULL;
165 }
166
167 serial_flush(serial);
168
169 model = scan_model_sm(serial);
170
171 /* If detection failed and no user-supplied parameters,
172 * try second baud rate. */
173 if ((model == SR_METRAHIT_NONE) && !serialcomm_given) {
174 serialcomm = SERIALCOMM_1X_RS232;
175 g_free(serial->serialcomm);
176 serial->serialcomm = g_strdup(serialcomm);
177 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
178 serial_flush(serial);
179 model = scan_model_sm(serial);
180 }
181 }
182
183 if (model != SR_METRAHIT_NONE) {
184 sr_spew("%s %s detected!", VENDOR_GMC, sr_gmc_model_str(model));
185 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC,
186 sr_gmc_model_str(model), "")))
187 return NULL;
188 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
189 sr_err("Device context malloc failed.");
190 return NULL;
191 }
192 devc->model = model;
193 devc->limit_samples = 0;
194 devc->limit_msec = 0;
195 devc->num_samples = 0;
196 devc->elapsed_msec = g_timer_new();
197 devc->settings_ok = FALSE;
198
199 sdi->conn = serial;
200 sdi->priv = devc;
201 sdi->driver = &gmc_mh_1x_2x_rs232_driver_info;
202 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
203 return NULL;
204 sdi->probes = g_slist_append(sdi->probes, probe);
205 drvc->instances = g_slist_append(drvc->instances, sdi);
206 devices = g_slist_append(devices, sdi);
207 }
208
209 return devices;
210}
211
212/** Driver device list function */
213static GSList *dev_list_1x_2x_rs232(void)
214{
215 return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv))
216 ->instances;
217}
218
219static int dev_clear_1x_2x_rs232(void)
220{
221 return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL);
222}
223
224static int dev_open(struct sr_dev_inst *sdi)
225{
226 struct sr_serial_dev_inst *serial = sdi->conn;
227 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
228 return SR_ERR;
229
230 sdi->status = SR_ST_ACTIVE;
231
232 return SR_OK;
233}
234
235static int dev_close(struct sr_dev_inst *sdi)
236{
237 struct sr_serial_dev_inst *serial;
238 struct dev_context *devc;
239
240 serial = sdi->conn;
241 if (serial && serial->fd != -1) {
242 serial_close(serial);
243 sdi->status = SR_ST_INACTIVE;
244 }
245
246 sdi->status = SR_ST_INACTIVE;
247
248 /* Free dynamically allocated resources. */
249 if ((devc = sdi->priv) && devc->elapsed_msec) {
250 g_timer_destroy(devc->elapsed_msec);
251 devc->elapsed_msec = NULL;
252 devc->model = SR_METRAHIT_NONE;
253 }
254
255 return SR_OK;
256}
257
258static int cleanup_sm_rs232(void)
259{
260 return dev_clear_1x_2x_rs232();
261}
262
263/** TODO */
264static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
265 const struct sr_probe_group *probe_group)
266{
267 int ret;
268
269 (void)sdi;
270 (void)data;
271 (void)probe_group;
272
273 ret = SR_OK;
274 switch (key) {
275 /* TODO */
276 default:
277 return SR_ERR_NA;
278 }
279
280 return ret;
281}
282
283static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
284 const struct sr_probe_group *probe_group)
285{
286 struct dev_context *devc;
287
288 (void)probe_group;
289
290 if (sdi->status != SR_ST_ACTIVE)
291 return SR_ERR_DEV_CLOSED;
292
293 if (!(devc = sdi->priv)) {
294 sr_err("sdi->priv was NULL.");
295 return SR_ERR_BUG;
296 }
297
298 switch (key) {
299 case SR_CONF_LIMIT_MSEC:
300 if (g_variant_get_uint64(data) == 0) {
301 sr_err("LIMIT_MSEC can't be 0.");
302 return SR_ERR;
303 }
304 devc->limit_msec = g_variant_get_uint64(data);
305 sr_dbg("Setting time limit to %" PRIu64 "ms.",
306 devc->limit_msec);
307 break;
308 case SR_CONF_LIMIT_SAMPLES:
309 devc->limit_samples = g_variant_get_uint64(data);
310 sr_dbg("Setting sample limit to %" PRIu64 ".",
311 devc->limit_samples);
312 break;
313 default:
314 return SR_ERR_NA;
315 }
316
317 return SR_OK;
318}
319
320static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
321 const struct sr_probe_group *probe_group)
322{
323 (void)sdi;
324
325 switch (key) {
326 case SR_CONF_SCAN_OPTIONS:
327 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
328 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
329 break;
330 case SR_CONF_DEVICE_OPTIONS:
331 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
332 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
333 break;
334 default:
335 return SR_ERR_NA;
336 }
337
338 return SR_OK;
339}
340
341static int dev_acq_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
342 void *cb_data)
343{
344 struct dev_context *devc;
345 struct sr_serial_dev_inst *serial;
346
347 if (!sdi || !cb_data || !(devc = sdi->priv))
348 return SR_ERR_BUG;
349
350 if (sdi->status != SR_ST_ACTIVE)
351 return SR_ERR_DEV_CLOSED;
352
353 devc->cb_data = cb_data;
354 devc->settings_ok = FALSE;
355 devc->buflen = 0;
356
357 /* Send header packet to the session bus. */
358 std_session_send_df_header(cb_data, LOG_PREFIX);
359
360 /* Start timer, if required. */
361 if (devc->limit_msec)
362 g_timer_start(devc->elapsed_msec);
363
364 /* Poll every 40ms, or whenever some data comes in. */
365 serial = sdi->conn;
366 serial_source_add(serial, G_IO_IN, 40, gmc_mh_1x_2x_receive_data,
367 (void *)sdi);
368
369 return SR_OK;
370}
371
372static int dev_acq_stop(struct sr_dev_inst *sdi, void *cb_data)
373{
374 struct dev_context *devc;
375
376 /* Stop timer, if required. */
377 if (sdi && (devc = sdi->priv) && devc->limit_msec)
378 g_timer_stop(devc->elapsed_msec);
379
380 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
381 sdi->conn, LOG_PREFIX);
382}
383
384SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
385 .name = "gmc-mh-1x-2x-rs232",
386 .longname =
387 "Gossen Metrawatt Metrahit 1x/2x DMMs, 'RS232' Interface",
388 .api_version = 1,
389 .init = init_1x_2x_rs232,
390 .cleanup = cleanup_sm_rs232,
391 .scan = scan_1x_2x_rs232,
392 .dev_list = dev_list_1x_2x_rs232,
393 .dev_clear = dev_clear_1x_2x_rs232,
394 .config_get = config_get,
395 .config_set = config_set,
396 .config_list = config_list,
397 .dev_open = dev_open,
398 .dev_close = dev_close,
399 .dev_acquisition_start = dev_acq_start_1x_2x_rs232,
400 .dev_acquisition_stop = dev_acq_stop,
401 .priv = NULL,
402};