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