]> sigrok.org Git - libsigrok.git/blob - src/hardware/gmc-mh-1x-2x/api.c
Don't check sr_channel_new() return value (always succeeds).
[libsigrok.git] / src / hardware / gmc-mh-1x-2x / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013, 2014 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 /** @file
21  *  Gossen Metrawatt Metrahit 1x/2x drivers
22  *  @internal
23  */
24
25 #include <string.h>
26 #include "protocol.h"
27
28 /* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */
29 #define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */
30 #define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0"
31 #define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0"
32 #define VENDOR_GMC "Gossen Metrawatt"
33
34 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info;
35 SR_PRIV struct sr_dev_driver gmc_mh_2x_bd232_driver_info;
36
37 static const uint32_t scanopts[] = {
38         SR_CONF_CONN,
39         SR_CONF_SERIALCOMM,
40 };
41
42 /** Hardware capabilities for Metrahit 1x/2x devices in send mode. */
43 static const uint32_t devopts_sm[] = {
44         SR_CONF_MULTIMETER,
45         SR_CONF_THERMOMETER,    /**< All GMC 1x/2x multimeters seem to support this */
46         SR_CONF_CONTINUOUS,
47         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
48         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
49 };
50
51 /** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */
52 static const uint32_t devopts_bd[] = {
53         SR_CONF_MULTIMETER,
54         SR_CONF_THERMOMETER,    /**< All GMC 1x/2x multimeters seem to support this */
55         SR_CONF_CONTINUOUS,
56         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
57         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
58         SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
59 };
60
61
62 /* TODO:
63  * - For the 29S SR_CONF_ENERGYMETER, too.
64  * - SR_CONF_PATTERN_MODE for some 2x devices
65  * - SR_CONF_DATALOG for 22M, 26M, 29S and storage adaptors.
66  * Need to implement device-specific lists.
67  */
68
69 /** Init driver gmc_mh_1x_2x_rs232. */
70 static int init_1x_2x_rs232(struct sr_context *sr_ctx)
71 {
72         return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX);
73 }
74
75 /** Init driver gmc_mh_2x_bd232. */
76 static int init_2x_bd232(struct sr_context *sr_ctx)
77 {
78         return std_init(sr_ctx, &gmc_mh_2x_bd232_driver_info, LOG_PREFIX);
79 }
80
81 /**
82  * Read single byte from serial port.
83  *
84  * @retval -1 Timeout or error.
85  * @retval other Byte.
86  */
87 static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout)
88 {
89         uint8_t result = 0;
90         int rc = 0;
91
92         for (;;) {
93                 rc = serial_read_nonblocking(serial, &result, 1);
94                 if (rc == 1) {
95                         sr_spew("read: 0x%02x/%d", result, result);
96                         return result;
97                 }
98                 if (g_get_monotonic_time() > timeout)
99                         return -1;
100                 g_usleep(2000);
101         }
102 }
103
104 /**
105  * Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second.
106  *
107  * @param serial Configured, open serial port.
108  *
109  * @retval NULL Detection failed.
110  * @retval other Model code.
111  */
112 static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
113 {
114         int byte, bytecnt, cnt;
115         enum model model;
116         gint64 timeout_us;
117
118         model = METRAHIT_NONE;
119         timeout_us = g_get_monotonic_time() + 1 * 1000 * 1000;
120
121         /*
122          * Try to find message consisting of device code and several
123          * (at least 4) data bytes.
124          */
125         for (bytecnt = 0; bytecnt < 100; bytecnt++) {
126                 byte = read_byte(serial, timeout_us);
127                 if ((byte == -1) || (timeout_us < g_get_monotonic_time()))
128                         break;
129                 if ((byte & MSGID_MASK) == MSGID_INF) {
130                         if (!(model = gmc_decode_model_sm(byte & MSGC_MASK)))
131                                 break;
132                         /* Now expect (at least) 4 data bytes. */
133                         for (cnt = 0; cnt < 4; cnt++) {
134                                 byte = read_byte(serial, timeout_us);
135                                 if ((byte == -1) ||
136                                                 ((byte & MSGID_MASK) != MSGID_DATA))
137                                 {
138                                         model = METRAHIT_NONE;
139                                         bytecnt = 100;
140                                         break;
141                                 }
142                         }
143                         break;
144                 }
145         }
146
147         return model;
148 }
149
150 /**
151  * Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt
152  * 'RS232' interface.
153  *
154  * The older 1x models use 8192 baud and the newer 2x 9600 baud.
155  * The DMM usually sends up to about 20 messages per second. However, depending
156  * on configuration and measurement mode the intervals can be much larger and
157  * then the detection might not work.
158  */
159 static GSList *scan_1x_2x_rs232(GSList *options)
160 {
161         struct sr_dev_inst *sdi;
162         struct drv_context *drvc;
163         struct dev_context *devc;
164         struct sr_config *src;
165         struct sr_channel *ch;
166         struct sr_serial_dev_inst *serial;
167         GSList *l, *devices;
168         const char *conn, *serialcomm;
169         enum model model;
170         gboolean serialcomm_given;
171
172         devices = NULL;
173         drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv;
174         drvc->instances = NULL;
175         conn = serialcomm = NULL;
176         model = METRAHIT_NONE;
177         serialcomm_given = FALSE;
178
179         sr_spew("scan_1x_2x_rs232() called!");
180
181         for (l = options; l; l = l->next) {
182                 src = l->data;
183                 switch (src->key) {
184                 case SR_CONF_CONN:
185                         conn = g_variant_get_string(src->data, NULL);
186                         break;
187                 case SR_CONF_SERIALCOMM:
188                         serialcomm = g_variant_get_string(src->data, NULL);
189                         serialcomm_given = TRUE;
190                         break;
191                 }
192         }
193         if (!conn)
194                 return NULL;
195         if (!serialcomm)
196                 serialcomm = SERIALCOMM_2X_RS232;
197
198         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
199                 return NULL;
200
201         if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
202                 sr_serial_dev_inst_free(serial);
203                 return NULL;
204         }
205
206         serial_flush(serial);
207
208         model = scan_model_sm(serial);
209
210         /*
211          * If detection failed and no user-supplied parameters,
212          * try second baud rate.
213          */
214         if ((model == METRAHIT_NONE) && !serialcomm_given) {
215                 serialcomm = SERIALCOMM_1X_RS232;
216                 g_free(serial->serialcomm);
217                 serial->serialcomm = g_strdup(serialcomm);
218                 if (serial_set_paramstr(serial, serialcomm) == SR_OK) {
219                         serial_flush(serial);
220                         model = scan_model_sm(serial);
221                 }
222         }
223
224         if (model != METRAHIT_NONE) {
225                 sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
226                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
227                 sdi->status = SR_ST_INACTIVE;
228                 sdi->vendor = g_strdup(VENDOR_GMC);
229                 sdi->model = g_strdup(gmc_model_str(model));
230                 devc = g_malloc0(sizeof(struct dev_context));
231                 devc->model = model;
232                 devc->limit_samples = 0;
233                 devc->limit_msec = 0;
234                 devc->num_samples = 0;
235                 devc->elapsed_msec = g_timer_new();
236                 devc->settings_ok = FALSE;
237                 sdi->conn = serial;
238                 sdi->priv = devc;
239                 sdi->driver = &gmc_mh_1x_2x_rs232_driver_info;
240                 ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1");
241                 sdi->channels = g_slist_append(sdi->channels, ch);
242                 drvc->instances = g_slist_append(drvc->instances, sdi);
243                 devices = g_slist_append(devices, sdi);
244         }
245
246         return devices;
247 }
248
249 /** Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt 'BD 232' interface.
250  *
251  */
252 static GSList *scan_2x_bd232(GSList *options)
253 {
254         struct sr_dev_inst *sdi;
255         struct drv_context *drvc;
256         struct dev_context *devc;
257         struct sr_config *src;
258         struct sr_channel *ch;
259         struct sr_serial_dev_inst *serial;
260         GSList *l, *devices;
261         const char *conn, *serialcomm;
262         int cnt, byte;
263         gint64 timeout_us;
264
265         sdi = NULL;
266         devc = NULL;
267         conn = serialcomm = NULL;
268         devices = NULL;
269
270         drvc = (&gmc_mh_2x_bd232_driver_info)->priv;
271         drvc->instances = NULL;
272
273         sr_spew("scan_2x_bd232() called!");
274
275         for (l = options; l; l = l->next) {
276                 src = l->data;
277                 switch (src->key) {
278                 case SR_CONF_CONN:
279                         conn = g_variant_get_string(src->data, NULL);
280                         break;
281                 case SR_CONF_SERIALCOMM:
282                         serialcomm = g_variant_get_string(src->data, NULL);
283                         break;
284                 }
285         }
286         if (!conn)
287                 return NULL;
288         if (!serialcomm)
289                 serialcomm = SERIALCOMM_2X;
290
291         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
292                 return NULL;
293
294         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
295                 goto exit_err;
296
297         devc = g_malloc0(sizeof(struct dev_context));
298
299         sdi = g_malloc0(sizeof(struct sr_dev_inst));
300         sdi->status = SR_ST_INACTIVE;
301         sdi->vendor = g_strdup(VENDOR_GMC);
302         sdi->priv = devc;
303
304         /* Send message 03 "Query multimeter version and status" */
305         sdi->conn = serial;
306         sdi->priv = devc;
307         if (req_stat14(sdi, TRUE) != SR_OK)
308                 goto exit_err;
309
310         /* Wait for reply from device(s) for up to 2s. */
311         timeout_us = g_get_monotonic_time() + 2*1000*1000;
312
313         while (timeout_us > g_get_monotonic_time()) {
314                 /* Receive reply (14 bytes) */
315                 devc->buflen = 0;
316                 for (cnt = 0; cnt < 14; cnt++) {
317                         byte = read_byte(serial, timeout_us);
318                         if (byte != -1)
319                                 devc->buf[devc->buflen++] = (byte & MASK_6BITS);
320                 }
321
322                 if (devc->buflen != 14)
323                         continue;
324
325                 devc->addr = devc->buf[0];
326                 process_msg14(sdi);
327                 devc->buflen = 0;
328
329                 if (devc->model != METRAHIT_NONE) {
330                         sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
331                         devc->elapsed_msec = g_timer_new();
332                         sdi->model = g_strdup(gmc_model_str(devc->model));
333                         sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
334                         sdi->conn = serial;
335                         sdi->priv = devc;
336                         sdi->driver = &gmc_mh_2x_bd232_driver_info;
337                         ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1");
338                         sdi->channels = g_slist_append(sdi->channels, ch);
339                         drvc->instances = g_slist_append(drvc->instances, sdi);
340                         devices = g_slist_append(devices, sdi);
341                         devc = g_malloc0(sizeof(struct dev_context));
342                         sdi = g_malloc0(sizeof(struct sr_dev_inst));
343                         sdi->status = SR_ST_INACTIVE;
344                         sdi->vendor = g_strdup(VENDOR_GMC);
345                 }
346         };
347
348         /* Free last alloc if no device found */
349         if (devc->model == METRAHIT_NONE) {
350                 g_free(devc);
351                 sr_dev_inst_free(sdi);
352         }
353
354         return devices;
355
356 exit_err:
357         sr_info("scan_2x_bd232(): Error!");
358
359         if (serial)
360                 sr_serial_dev_inst_free(serial);
361         if (devc)
362                 g_free(devc);
363         if (sdi)
364                 sr_dev_inst_free(sdi);
365
366         return NULL;
367 }
368
369 /** Driver device list function */
370 static GSList *dev_list_1x_2x_rs232(void)
371 {
372         return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv))->instances;
373 }
374
375 /** Driver device list function */
376 static GSList *dev_list_2x_bd232(void)
377 {
378         return ((struct drv_context *)(gmc_mh_2x_bd232_driver_info.priv))
379                         ->instances;
380 }
381
382 static int dev_close(struct sr_dev_inst *sdi)
383 {
384         struct dev_context *devc;
385
386         std_serial_dev_close(sdi);
387
388         sdi->status = SR_ST_INACTIVE;
389
390         /* Free dynamically allocated resources. */
391         if ((devc = sdi->priv) && devc->elapsed_msec) {
392                 g_timer_destroy(devc->elapsed_msec);
393                 devc->elapsed_msec = NULL;
394                 devc->model = METRAHIT_NONE;
395         }
396
397         return SR_OK;
398 }
399
400 static int cleanup_sm_rs232(void)
401 {
402         return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL);
403 }
404
405 static int cleanup_2x_bd232(void)
406 {
407         return std_dev_clear(&gmc_mh_2x_bd232_driver_info, NULL);
408 }
409
410 /** Get value of configuration item */
411 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
412                 const struct sr_channel_group *cg)
413 {
414         int ret;
415         struct dev_context *devc;
416
417         (void)cg;
418
419         ret = SR_OK;
420
421         if (!sdi || !(devc = sdi->priv))
422                 return SR_ERR_ARG;
423
424         ret = SR_OK;
425         switch (key) {
426         case SR_CONF_LIMIT_SAMPLES:
427                 *data = g_variant_new_uint64(devc->limit_samples);
428                 break;
429         case SR_CONF_LIMIT_MSEC:
430                 *data = g_variant_new_uint64(devc->limit_msec);
431                 break;
432         case SR_CONF_POWER_OFF:
433                 *data = g_variant_new_boolean(FALSE);
434                 break;
435         default:
436                 return SR_ERR_NA;
437         }
438
439         return ret;
440 }
441
442 /** Implementation of config_list, auxiliary function for common parts, */
443 static int config_list_common(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
444                 const struct sr_channel_group *cg)
445 {
446         (void)sdi;
447         (void)cg;
448
449         switch (key) {
450         case SR_CONF_SCAN_OPTIONS:
451                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
452                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
453                 break;
454         default:
455                 return SR_ERR_NA;
456         }
457
458         return SR_OK;
459 }
460
461 /** Implementation of config_list for Metrahit 1x/2x send mode */
462 static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
463                           const struct sr_channel_group *cg)
464 {
465         switch (key) {
466         case SR_CONF_DEVICE_OPTIONS:
467                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
468                                 devopts_sm, ARRAY_SIZE(devopts_sm), sizeof(uint32_t));
469                 break;
470         default:
471                 return config_list_common(key, data, sdi, cg);
472         }
473
474         return SR_OK;
475 }
476
477 /** Implementation of config_list for Metrahit 2x bidirectional mode */
478 static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
479                           const struct sr_channel_group *cg)
480 {
481         switch (key) {
482         case SR_CONF_DEVICE_OPTIONS:
483                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
484                                 devopts_bd, ARRAY_SIZE(devopts_bd), sizeof(uint32_t));
485                 break;
486         default:
487                 return config_list_common(key, data, sdi, cg);
488         }
489
490         return SR_OK;
491 }
492
493 static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
494                                              void *cb_data)
495 {
496         struct dev_context *devc;
497         struct sr_serial_dev_inst *serial;
498
499         if (!sdi || !cb_data || !(devc = sdi->priv))
500                 return SR_ERR_BUG;
501
502         if (sdi->status != SR_ST_ACTIVE)
503                 return SR_ERR_DEV_CLOSED;
504
505         devc->cb_data = cb_data;
506         devc->settings_ok = FALSE;
507         devc->buflen = 0;
508
509         /* Send header packet to the session bus. */
510         std_session_send_df_header(cb_data, LOG_PREFIX);
511
512         /* Start timer, if required. */
513         if (devc->limit_msec)
514                 g_timer_start(devc->elapsed_msec);
515
516         /* Poll every 40ms, or whenever some data comes in. */
517         serial = sdi->conn;
518         serial_source_add(sdi->session, serial, G_IO_IN, 40,
519                         gmc_mh_1x_2x_receive_data, (void *)sdi);
520
521         return SR_OK;
522 }
523
524 static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi,
525                                           void *cb_data)
526 {
527         struct dev_context *devc;
528         struct sr_serial_dev_inst *serial;
529
530         if (!sdi || !cb_data || !(devc = sdi->priv))
531                 return SR_ERR_BUG;
532
533         if (sdi->status != SR_ST_ACTIVE)
534                 return SR_ERR_DEV_CLOSED;
535
536         devc->cb_data = cb_data;
537         devc->settings_ok = FALSE;
538         devc->buflen = 0;
539
540         /* Send header packet to the session bus. */
541         std_session_send_df_header(cb_data, LOG_PREFIX);
542
543         /* Start timer, if required. */
544         if (devc->limit_msec)
545                 g_timer_start(devc->elapsed_msec);
546
547         /* Poll every 40ms, or whenever some data comes in. */
548         serial = sdi->conn;
549         serial_source_add(sdi->session, serial, G_IO_IN, 40,
550                         gmc_mh_2x_receive_data, (void *)sdi);
551
552         /* Send start message */
553         return req_meas14(sdi);
554 }
555
556 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
557 {
558         struct dev_context *devc;
559
560         /* Stop timer, if required. */
561         if (sdi && (devc = sdi->priv) && devc->limit_msec)
562                 g_timer_stop(devc->elapsed_msec);
563
564         return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
565                         sdi->conn, LOG_PREFIX);
566 }
567
568 SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
569         .name = "gmc-mh-1x-2x-rs232",
570         .longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
571         .api_version = 1,
572         .init = init_1x_2x_rs232,
573         .cleanup = cleanup_sm_rs232,
574         .scan = scan_1x_2x_rs232,
575         .dev_list = dev_list_1x_2x_rs232,
576         .dev_clear = NULL,
577         .config_get = config_get,
578         .config_set = config_set,
579         .config_list = config_list_sm,
580         .dev_open = std_serial_dev_open,
581         .dev_close = dev_close,
582         .dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
583         .dev_acquisition_stop = dev_acquisition_stop,
584         .priv = NULL,
585 };
586
587 SR_PRIV struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
588         .name = "gmc-mh-2x-bd232",
589         .longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
590         .api_version = 1,
591         .init = init_2x_bd232,
592         .cleanup = cleanup_2x_bd232,
593         .scan = scan_2x_bd232,
594         .dev_list = dev_list_2x_bd232,
595         .dev_clear = NULL,
596         .config_get = config_get,
597         .config_set = config_set,
598         .config_list = config_list_bd,
599         .dev_open = std_serial_dev_open,
600         .dev_close = dev_close,
601         .dev_acquisition_start = dev_acquisition_start_2x_bd232,
602         .dev_acquisition_stop = dev_acquisition_stop,
603         .priv = NULL,
604 };