2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
5 * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
30 static const uint32_t scanopts[] = {
35 static const uint32_t drvopts[] = {
39 static const uint32_t devopts[] = {
41 SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
42 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
43 SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST,
44 SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST,
47 static struct sr_dev_inst *scan_packet_check_devinst;
49 static void scan_packet_check_setup(struct sr_dev_inst *sdi)
51 scan_packet_check_devinst = sdi;
54 static gboolean scan_packet_check_func(const uint8_t *buf)
56 struct sr_dev_inst *sdi;
57 struct dev_context *devc;
58 const struct lcr_info *lcr;
59 struct lcr_parse_info *info;
61 /* Get a reference to the LCR model that is getting checked. */
62 sdi = scan_packet_check_devinst;
72 /* Synchronize to the stream of LCR packets. */
73 if (!lcr->packet_valid(buf))
76 /* Have LCR packets _processed_, gather current configuration. */
77 info = &devc->parse_info;
78 memset(info, 0, sizeof(*info));
79 if (lcr->packet_parse(buf, NULL, NULL, info) == SR_OK) {
80 devc->output_freq = info->output_freq;
81 if (info->circuit_model)
82 devc->circuit_model = info->circuit_model;
88 static int scan_lcr_port(const struct lcr_info *lcr,
89 const char *conn, struct sr_serial_dev_inst *serial)
96 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
98 sr_info("Probing serial port %s.", conn);
101 * See if we can detect a device of specified type.
103 * No supported device provides a means to "identify" yet. No
104 * supported device requires "packet request" yet. They all just
105 * send data periodically. So we check if the packets match the
106 * probed device's expected format.
108 serial_flush(serial);
109 if (lcr->packet_request) {
110 ret = lcr->packet_request(serial);
112 sr_err("Failed to request packet: %d.", ret);
113 goto scan_port_cleanup;
117 ret = serial_stream_detect(serial, buf, &len,
118 lcr->packet_size, lcr->packet_valid, 3000);
120 goto scan_port_cleanup;
123 * If the packets were found to match after more than two packets
124 * got dropped, something is wrong. This is worth warning about,
125 * but isn't fatal. The dropped bytes might be due to nonstandard
126 * cables that ship with some devices.
128 dropped = len - lcr->packet_size;
129 if (dropped > 2 * lcr->packet_size)
130 sr_warn("Had to drop unexpected amounts of data.");
132 /* Create a device instance for the found device. */
133 sr_info("Found %s %s device on port %s.", lcr->vendor, lcr->model, conn);
136 /* Keep serial port open if probe succeeded. */
138 serial_close(serial);
143 static struct sr_dev_inst *create_lcr_sdi(struct lcr_info *lcr,
144 struct sr_serial_dev_inst *serial)
146 struct sr_dev_inst *sdi;
147 struct dev_context *devc;
149 const char **ch_fmts;
153 sdi = g_malloc0(sizeof(*sdi));
154 sdi->status = SR_ST_INACTIVE;
155 sdi->vendor = g_strdup(lcr->vendor);
156 sdi->model = g_strdup(lcr->model);
157 sdi->inst_type = SR_INST_SERIAL;
159 devc = g_malloc0(sizeof(*devc));
161 devc->lcr_info = lcr;
162 sr_sw_limits_init(&devc->limits);
163 ch_fmts = lcr->channel_formats;
164 for (ch_idx = 0; ch_idx < lcr->channel_count; ch_idx++) {
165 fmt = (ch_fmts && ch_fmts[ch_idx]) ? ch_fmts[ch_idx] : "P%zu";
166 snprintf(ch_name, sizeof(ch_name), fmt, ch_idx + 1);
167 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, ch_name);
173 static int read_lcr_port(struct sr_dev_inst *sdi,
174 const struct lcr_info *lcr, struct sr_serial_dev_inst *serial)
180 serial_flush(serial);
181 if (lcr->packet_request) {
182 ret = lcr->packet_request(serial);
184 sr_err("Failed to request packet: %d.", ret);
190 * Receive a few more packets (and process them!) to have the
191 * current output frequency and circuit model parameter values
192 * detected. The above "stream detect" phase only synchronized
193 * to the packets by checking their validity, but it cannot
194 * provide details. This phase here runs a modified "checker"
195 * routine which also extracts details from LCR packets after
196 * the device got detected and parameter storage was prepared.
198 sr_info("Retrieving current acquisition parameters.");
200 scan_packet_check_setup(sdi);
201 ret = serial_stream_detect(serial, buf, &len,
202 lcr->packet_size, scan_packet_check_func, 1500);
203 scan_packet_check_setup(NULL);
208 static GSList *scan(struct sr_dev_driver *di, GSList *options)
210 struct lcr_info *lcr;
211 struct sr_config *src;
213 const char *conn, *serialcomm;
214 struct sr_serial_dev_inst *serial;
216 struct sr_dev_inst *sdi;
218 lcr = (struct lcr_info *)di;
220 /* Get serial port name and communication parameters. */
222 serialcomm = lcr->comm;
223 for (l = options; l; l = l->next) {
227 conn = g_variant_get_string(src->data, NULL);
229 case SR_CONF_SERIALCOMM:
230 serialcomm = g_variant_get_string(src->data, NULL);
238 /* TODO Handle ambiguous conn= specs, see serial-dmm. */
240 /* Open the serial port, check data packets. */
241 serial = sr_serial_dev_inst_new(conn, serialcomm);
242 ret = scan_lcr_port(lcr, conn, serial);
244 /* Probe failed, release 'serial'. */
245 sr_serial_dev_inst_free(serial);
247 /* Create and return device instance, keep 'serial' alive. */
248 sdi = create_lcr_sdi(lcr, serial);
249 devices = g_slist_append(devices, sdi);
250 (void)read_lcr_port(sdi, lcr, serial);
251 serial_close(serial);
254 return std_scan_complete(di, devices);
257 static int config_get(uint32_t key, GVariant **data,
258 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
260 struct dev_context *devc;
261 const struct lcr_info *lcr;
268 case SR_CONF_LIMIT_FRAMES:
269 case SR_CONF_LIMIT_MSEC:
270 return sr_sw_limits_config_get(&devc->limits, key, data);
271 case SR_CONF_OUTPUT_FREQUENCY:
272 *data = g_variant_new_double(devc->output_freq);
274 case SR_CONF_EQUIV_CIRCUIT_MODEL:
275 if (!devc->circuit_model)
277 *data = g_variant_new_string(devc->circuit_model);
280 lcr = devc->lcr_info;
283 if (!lcr->config_get)
285 return lcr->config_get(key, data, sdi, cg);
290 static int config_set(uint32_t key, GVariant *data,
291 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
293 struct dev_context *devc;
294 const struct lcr_info *lcr;
301 case SR_CONF_LIMIT_FRAMES:
302 case SR_CONF_LIMIT_MSEC:
303 return sr_sw_limits_config_set(&devc->limits, key, data);
305 lcr = devc->lcr_info;
308 if (!lcr->config_set)
310 return lcr->config_set(key, data, sdi, cg);
315 static int config_list(uint32_t key, GVariant **data,
316 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
318 struct dev_context *devc;
319 const struct lcr_info *lcr;
322 case SR_CONF_SCAN_OPTIONS:
323 case SR_CONF_DEVICE_OPTIONS:
324 return STD_CONFIG_LIST(key, data, sdi, cg,
325 scanopts, drvopts, devopts);
335 lcr = devc->lcr_info;
336 if (!lcr || !lcr->config_list)
338 return lcr->config_list(key, data, sdi, cg);
343 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
345 struct dev_context *devc;
346 struct sr_serial_dev_inst *serial;
351 * Clear values that were gathered during scan or in a previous
352 * acquisition, so that this acquisition's data feed immediately
353 * starts with meta packets before first measurement values, and
354 * also communicates subsequent parameter changes.
356 devc->output_freq = 0;
357 devc->circuit_model = NULL;
359 sr_sw_limits_acquisition_start(&devc->limits);
360 std_session_send_df_header(sdi);
363 serial_source_add(sdi->session, serial, G_IO_IN, 50,
364 lcr_receive_data, (void *)sdi);
369 #define LCR_ES51919(id, vendor, model) \
370 &((struct lcr_info) { \
373 .longname = vendor " " model, \
376 .cleanup = std_cleanup, \
378 .dev_list = std_dev_list, \
379 .dev_clear = std_dev_clear, \
380 .config_get = config_get, \
381 .config_set = config_set, \
382 .config_list = config_list, \
383 .dev_open = std_serial_dev_open, \
384 .dev_close = std_serial_dev_close, \
385 .dev_acquisition_start = dev_acquisition_start, \
386 .dev_acquisition_stop = std_serial_dev_acquisition_stop, \
389 vendor, model, ES51919_CHANNEL_COUNT, NULL, \
390 ES51919_COMM_PARAM, ES51919_PACKET_SIZE, \
392 es51919_packet_valid, es51919_packet_parse, \
393 NULL, NULL, es51919_config_list, \
396 SR_REGISTER_DEV_DRIVER_LIST(lcr_es51919_drivers,
397 LCR_ES51919("deree-de5000", "DER EE", "DE-5000"),
398 LCR_ES51919("mastech-ms5308", "MASTECH", "MS5308"),
399 LCR_ES51919("peaktech-2170", "PeakTech", "2170"),
400 LCR_ES51919("uni-t-ut612", "UNI-T", "UT612"),
403 #define LCR_VC4080(id, vendor, model) \
404 &((struct lcr_info) { \
407 .longname = vendor " " model, \
410 .cleanup = std_cleanup, \
412 .dev_list = std_dev_list, \
413 .dev_clear = std_dev_clear, \
414 .config_get = config_get, \
415 .config_set = config_set, \
416 .config_list = config_list, \
417 .dev_open = std_serial_dev_open, \
418 .dev_close = std_serial_dev_close, \
419 .dev_acquisition_start = dev_acquisition_start, \
420 .dev_acquisition_stop = std_serial_dev_acquisition_stop, \
424 VC4080_CHANNEL_COUNT, vc4080_channel_formats, \
425 VC4080_COMM_PARAM, VC4080_PACKET_SIZE, \
426 500, vc4080_packet_request, \
427 vc4080_packet_valid, vc4080_packet_parse, \
428 NULL, NULL, vc4080_config_list, \
431 SR_REGISTER_DEV_DRIVER_LIST(lcr_vc4080_drivers,
432 LCR_VC4080("peaktech-2165", "PeakTech", "2165"),
433 LCR_VC4080("voltcraft-4080", "Voltcraft", "4080"),