2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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.
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.
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/>.
22 static const int32_t hwopts[] = {
27 static const int32_t hwcaps[] = {
29 SR_CONF_LIMIT_SAMPLES,
36 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
38 SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
39 SR_PRIV struct sr_dev_driver siemens_b102x_driver_info;
41 static const char* get_brandstr(struct sr_dev_driver* drv)
43 if (drv == &norma_dmm_driver_info)
49 static const char* get_typestr(int type, struct sr_dev_driver* drv)
51 static const char* nameref[5][2] = {
58 if ((type < 1) || (type > 5))
59 return "Unknown type!";
61 return nameref[type-1][(drv == &siemens_b102x_driver_info)];
64 static int init_norma_dmm(struct sr_context *sr_ctx)
66 return std_init(sr_ctx, &norma_dmm_driver_info, LOG_PREFIX);
69 static int init_siemens_b102x(struct sr_context *sr_ctx)
71 return std_init(sr_ctx, &siemens_b102x_driver_info, LOG_PREFIX);
74 static GSList *do_scan(struct sr_dev_driver* drv, GSList *options)
76 struct sr_dev_inst *sdi;
77 struct drv_context *drvc;
78 struct dev_context *devc;
79 struct sr_config *src;
80 struct sr_channel *ch;
81 struct sr_serial_dev_inst *serial;
84 const char *conn, *serialcomm;
91 drvc->instances = NULL;
92 conn = serialcomm = NULL;
94 for (l = options; l; l = l->next) {
98 conn = g_variant_get_string(src->data, NULL);
100 case SR_CONF_SERIALCOMM:
101 serialcomm = g_variant_get_string(src->data, NULL);
108 serialcomm = SERIALCOMM;
110 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
113 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
116 serial_flush(serial);
118 if (!(buf = g_try_malloc(BUF_MAX))) {
119 sr_err("Serial buffer malloc failed.");
123 snprintf(req, sizeof(req), "%s\r\n",
124 nmadmm_requests[NMADMM_REQ_IDN].req_str);
125 for (cnt = 0; cnt < 7; cnt++) {
126 if (serial_write(serial, req, strlen(req)) == -1) {
127 sr_err("Unable to send identification request: %d %s.",
128 errno, strerror(errno));
132 serial_readline(serial, &buf, &len, 1500);
135 buf[BUF_MAX - 1] = '\0';
137 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
138 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
139 auxtype = xgittoint(buf[7]);
140 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
142 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
143 get_brandstr(drv), get_typestr(auxtype, drv), buf + 9)))
145 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
146 sr_err("Device context malloc failed.");
149 devc->type = auxtype;
150 devc->version = g_strdup(&buf[9]);
151 devc->elapsed_msec = g_timer_new();
156 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE,
159 sdi->channels = g_slist_append(sdi->channels, ch);
160 drvc->instances = g_slist_append(drvc->instances, sdi);
161 devices = g_slist_append(devices, sdi);
166 * The interface of the DM9x0 contains a cap that needs to
167 * charge for up to 10s before the interface works, if not
168 * powered externally. Therefore wait a little to improve
172 sr_info("Waiting 5s to allow interface to settle.");
173 g_usleep(5 * 1000 * 1000);
179 serial_close(serial);
181 sr_serial_dev_inst_free(serial);
186 static GSList *scan_norma_dmm(GSList *options)
188 return do_scan(&norma_dmm_driver_info, options);
191 static GSList *scan_siemens_b102x(GSList *options)
193 return do_scan(&siemens_b102x_driver_info, options);
196 static GSList *dev_list_norma_dmm(void)
198 return ((struct drv_context *)(norma_dmm_driver_info.priv))->instances;
201 static GSList *dev_list_siemens_b102x(void)
203 return ((struct drv_context *)(siemens_b102x_driver_info.priv))->instances;
206 static int dev_close(struct sr_dev_inst *sdi)
208 struct dev_context *devc;
210 std_serial_dev_close(sdi);
212 /* Free dynamically allocated resources. */
213 if ((devc = sdi->priv) && devc->version) {
214 g_free(devc->version);
215 devc->version = NULL;
216 g_timer_destroy(devc->elapsed_msec);
222 static int cleanup_norma_dmm(void)
224 return std_dev_clear(&norma_dmm_driver_info, NULL);
227 static int cleanup_siemens_b102x(void)
229 return std_dev_clear(&siemens_b102x_driver_info, NULL);
232 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
233 const struct sr_channel_group *cg)
235 struct dev_context *devc;
239 if (sdi->status != SR_ST_ACTIVE)
240 return SR_ERR_DEV_CLOSED;
242 if (!(devc = sdi->priv)) {
243 sr_err("sdi->priv was NULL.");
248 case SR_CONF_LIMIT_MSEC:
249 if (g_variant_get_uint64(data) == 0) {
250 sr_err("LIMIT_MSEC can't be 0.");
253 devc->limit_msec = g_variant_get_uint64(data);
254 sr_dbg("Setting time limit to %" PRIu64 "ms.",
257 case SR_CONF_LIMIT_SAMPLES:
258 devc->limit_samples = g_variant_get_uint64(data);
259 sr_dbg("Setting sample limit to %" PRIu64 ".",
260 devc->limit_samples);
269 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
270 const struct sr_channel_group *cg)
276 case SR_CONF_SCAN_OPTIONS:
277 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
278 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
280 case SR_CONF_DEVICE_OPTIONS:
281 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
282 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
291 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
294 struct dev_context *devc;
295 struct sr_serial_dev_inst *serial;
297 if (!sdi || !cb_data || !(devc = sdi->priv))
300 if (sdi->status != SR_ST_ACTIVE)
301 return SR_ERR_DEV_CLOSED;
303 devc->cb_data = cb_data;
305 /* Send header packet to the session bus. */
306 std_session_send_df_header(cb_data, LOG_PREFIX);
308 /* Start timer, if required. */
309 if (devc->limit_msec)
310 g_timer_start(devc->elapsed_msec);
312 /* Poll every 100ms, or whenever some data comes in. */
314 serial_source_add(serial, G_IO_IN, 100, norma_dmm_receive_data,
320 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
322 struct dev_context *devc;
324 /* Stop timer, if required. */
325 if (sdi && (devc = sdi->priv) && devc->limit_msec)
326 g_timer_stop(devc->elapsed_msec);
328 return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
329 sdi->conn, LOG_PREFIX);
332 SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
334 .longname = "Norma DM9x0 DMMs",
336 .init = init_norma_dmm,
337 .cleanup = cleanup_norma_dmm,
338 .scan = scan_norma_dmm,
339 .dev_list = dev_list_norma_dmm,
342 .config_set = config_set,
343 .config_list = config_list,
344 .dev_open = std_serial_dev_open,
345 .dev_close = dev_close,
346 .dev_acquisition_start = dev_acquisition_start,
347 .dev_acquisition_stop = dev_acquisition_stop,
352 SR_PRIV struct sr_dev_driver siemens_b102x_driver_info = {
353 .name = "siemens-b102x",
354 .longname = "Siemens B102x DMMs",
356 .init = init_siemens_b102x,
357 .cleanup = cleanup_siemens_b102x,
358 .scan = scan_siemens_b102x,
359 .dev_list = dev_list_siemens_b102x,
362 .config_set = config_set,
363 .config_list = config_list,
364 .dev_open = std_serial_dev_open,
365 .dev_close = dev_close,
366 .dev_acquisition_start = dev_acquisition_start,
367 .dev_acquisition_stop = dev_acquisition_stop,