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