]> sigrok.org Git - libsigrok.git/blame - src/hardware/serial-lcr/api.c
serial-lcr: move device driver code from src/lcr/ to src/hardware/
[libsigrok.git] / src / hardware / serial-lcr / api.c
CommitLineData
b5089195
JH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
bf5c4d46 5 * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
b5089195
JH
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
6ec6c43b 21#include <config.h>
b5089195 22#include <glib.h>
c1aae900 23#include <libsigrok/libsigrok.h>
b5089195 24#include "libsigrok-internal.h"
bf5c4d46
GS
25#include <math.h>
26#include "protocol.h"
27#include <stdint.h>
28#include <string.h>
b5089195 29
bf5c4d46
GS
30static const uint32_t scanopts[] = {
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
33};
34
35static const uint32_t drvopts[] = {
36 SR_CONF_LCRMETER,
37};
beedfa06 38
bf5c4d46
GS
39static const uint32_t devopts[] = {
40 SR_CONF_CONTINUOUS,
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,
beedfa06 45};
b5089195 46
bf5c4d46
GS
47static struct sr_dev_inst *scan_packet_check_devinst;
48
49static void scan_packet_check_setup(struct sr_dev_inst *sdi)
50{
51 scan_packet_check_devinst = sdi;
52}
53
54static gboolean scan_packet_check_func(const uint8_t *buf)
b5089195 55{
bf5c4d46
GS
56 struct sr_dev_inst *sdi;
57 struct dev_context *devc;
58 const struct lcr_info *lcr;
59 struct lcr_parse_info *info;
60
61 /* Get a reference to the LCR model that is getting checked. */
62 sdi = scan_packet_check_devinst;
63 if (!sdi)
64 return FALSE;
65 devc = sdi->priv;
66 if (!devc)
67 return FALSE;
68 lcr = devc->lcr_info;
69 if (!lcr)
70 return FALSE;
71
72 /* Synchronize to the stream of LCR packets. */
73 if (!lcr->packet_valid(buf))
74 return FALSE;
75
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;
83 }
84
85 return TRUE;
b5089195
JH
86}
87
4f840ce9 88static GSList *scan(struct sr_dev_driver *di, GSList *options)
b5089195 89{
bf5c4d46
GS
90 struct lcr_info *lcr;
91 struct sr_config *src;
92 GSList *l, *devices;
93 const char *conn, *serialcomm;
94 struct sr_serial_dev_inst *serial;
95 uint8_t buf[128];
96 size_t len, dropped;
97 int ret;
b5089195 98 struct sr_dev_inst *sdi;
bf5c4d46
GS
99 struct dev_context *devc;
100 size_t ch_idx;
101 char ch_name[8];
b5089195 102
bf5c4d46 103 lcr = (struct lcr_info *)di;
beedfa06 104
bf5c4d46
GS
105 /* Get serial port name and communication parameters. */
106 conn = NULL;
107 serialcomm = lcr->comm;
108 for (l = options; l; l = l->next) {
109 src = l->data;
110 switch (src->key) {
111 case SR_CONF_CONN:
112 conn = g_variant_get_string(src->data, NULL);
113 break;
114 case SR_CONF_SERIALCOMM:
115 serialcomm = g_variant_get_string(src->data, NULL);
116 break;
117 }
118 }
119 if (!conn)
b5089195
JH
120 return NULL;
121
bf5c4d46
GS
122 /* Open the serial port. */
123 serial = sr_serial_dev_inst_new(conn, serialcomm);
124 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
125 return NULL;
126 sr_info("Probing serial port %s.", conn);
127
128 /*
129 * See if we can detect a device of specified type.
130 *
131 * No supported device provides a means to "identify" yet. No
132 * supported device requires "packet request" yet. They all just
133 * send data periodically. So we check if the packets match the
134 * probed device's expected format.
135 */
136 serial_flush(serial);
137 devices = NULL;
138 len = sizeof(buf);
139 ret = serial_stream_detect(serial, buf, &len,
140 lcr->packet_size, lcr->packet_valid, 3000);
141 if (ret != SR_OK)
142 goto scan_cleanup;
143
144 /*
145 * If the packets were found to match after more than two packets
146 * got dropped, something is wrong. This is worth warning about,
147 * but isn't fatal. The dropped bytes might be due to nonstandard
148 * cables that ship with some devices.
149 */
150 dropped = len - lcr->packet_size;
151 if (dropped > 2 * lcr->packet_size)
152 sr_warn("Had to drop unexpected amounts of data.");
153
154 /* Create a device instance for the found device. */
155 sr_info("Found %s %s device on port %s.", lcr->vendor, lcr->model, conn);
156 sdi = g_malloc0(sizeof(*sdi));
157 sdi->status = SR_ST_INACTIVE;
158 sdi->vendor = g_strdup(lcr->vendor);
159 sdi->model = g_strdup(lcr->model);
160 sdi->inst_type = SR_INST_SERIAL;
161 sdi->conn = serial;
162 devc = g_malloc0(sizeof(*devc));
163 sdi->priv = devc;
164 devc->lcr_info = lcr;
165 sr_sw_limits_init(&devc->limits);
166 for (ch_idx = 0; ch_idx < lcr->channel_count; ch_idx++) {
167 snprintf(ch_name, sizeof(ch_name), "P%zu", ch_idx + 1);
168 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, ch_name);
169 }
170 devices = g_slist_append(devices, sdi);
171
172 /*
173 * Receive a few more packets (and process them!) to have the
174 * current output frequency and circuit model parameter values
175 * detected. The above "stream detect" phase only synchronized
176 * to the packets by checking their validity, but it cannot
177 * provide details. This phase here runs a modified "checker"
178 * routine which also extracts details from LCR packets after
179 * the device got detected and parameter storage was prepared.
180 */
181 sr_info("Retrieving current acquisition parameters.");
182 len = sizeof(buf);
183 scan_packet_check_setup(sdi);
184 ret = serial_stream_detect(serial, buf, &len,
185 lcr->packet_size, scan_packet_check_func, 1000);
186 scan_packet_check_setup(NULL);
187
188scan_cleanup:
189 serial_close(serial);
190
191 return std_scan_complete(di, devices);
192}
193
194static int config_get(uint32_t key, GVariant **data,
195 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
196{
197 struct dev_context *devc;
198 const struct lcr_info *lcr;
199
200 if (!sdi)
201 return SR_ERR_ARG;
202 devc = sdi->priv;
203
204 switch (key) {
205 case SR_CONF_LIMIT_FRAMES:
206 case SR_CONF_LIMIT_MSEC:
207 return sr_sw_limits_config_get(&devc->limits, key, data);
208 case SR_CONF_OUTPUT_FREQUENCY:
209 *data = g_variant_new_double(devc->output_freq);
210 return SR_OK;
211 case SR_CONF_EQUIV_CIRCUIT_MODEL:
212 if (!devc->circuit_model)
213 return SR_ERR_NA;
214 *data = g_variant_new_string(devc->circuit_model);
215 return SR_OK;
216 default:
217 lcr = devc->lcr_info;
218 if (!lcr)
219 return SR_ERR_NA;
220 if (!lcr->config_get)
221 return SR_ERR_NA;
222 return lcr->config_get(key, data, sdi, cg);
223 }
224 /* UNREACH */
225}
226
227static int config_set(uint32_t key, GVariant *data,
228 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
229{
230 struct dev_context *devc;
231 const struct lcr_info *lcr;
232
233 if (!sdi)
234 return SR_ERR_ARG;
235 devc = sdi->priv;
236
237 switch (key) {
238 case SR_CONF_LIMIT_FRAMES:
239 case SR_CONF_LIMIT_MSEC:
240 return sr_sw_limits_config_set(&devc->limits, key, data);
241 default:
242 lcr = devc->lcr_info;
243 if (!lcr)
244 return SR_ERR_NA;
245 if (!lcr->config_set)
246 return SR_ERR_NA;
247 return lcr->config_set(key, data, sdi, cg);
248 }
249 /* UNREACH */
250}
251
252static int config_list(uint32_t key, GVariant **data,
253 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
254{
255 struct dev_context *devc;
256 const struct lcr_info *lcr;
257
258 switch (key) {
259 case SR_CONF_SCAN_OPTIONS:
260 case SR_CONF_DEVICE_OPTIONS:
261 return STD_CONFIG_LIST(key, data, sdi, cg,
262 scanopts, drvopts, devopts);
263 default:
264 break;
265 }
266
267 if (!sdi)
268 return SR_ERR_ARG;
269 devc = sdi->priv;
270 switch (key) {
271 default:
272 lcr = devc->lcr_info;
273 if (!lcr || !lcr->config_list)
274 return SR_ERR_NA;
275 return lcr->config_list(key, data, sdi, cg);
276 }
277 /* UNREACH */
278}
279
280static int dev_acquisition_start(const struct sr_dev_inst *sdi)
281{
282 struct dev_context *devc;
283 struct sr_serial_dev_inst *serial;
284
285 devc = sdi->priv;
286
287 /*
288 * Clear values that were gathered during scan or in a previous
289 * acquisition, so that this acquisition's data feed immediately
290 * starts with meta packets before first measurement values, and
291 * also communicates subsequent parameter changes.
292 */
293 devc->output_freq = 0;
294 devc->circuit_model = NULL;
295
296 sr_sw_limits_acquisition_start(&devc->limits);
297 std_session_send_df_header(sdi);
298
299 serial = sdi->conn;
300 serial_source_add(sdi->session, serial, G_IO_IN, 50,
301 lcr_receive_data, (void *)sdi);
302
303 return SR_OK;
b5089195
JH
304}
305
beedfa06 306#define LCR_ES51919(id, vendor, model) \
bf5c4d46 307 &((struct lcr_info) { \
beedfa06
GS
308 { \
309 .name = id, \
310 .longname = vendor " " model, \
311 .api_version = 1, \
312 .init = std_init, \
313 .cleanup = std_cleanup, \
314 .scan = scan, \
315 .dev_list = std_dev_list, \
bf5c4d46
GS
316 .dev_clear = std_dev_clear, \
317 .config_get = config_get, \
318 .config_set = config_set, \
319 .config_list = config_list, \
beedfa06
GS
320 .dev_open = std_serial_dev_open, \
321 .dev_close = std_serial_dev_close, \
bf5c4d46 322 .dev_acquisition_start = dev_acquisition_start, \
beedfa06
GS
323 .dev_acquisition_stop = std_serial_dev_acquisition_stop, \
324 .context = NULL, \
325 }, \
bf5c4d46
GS
326 vendor, model, ES51919_CHANNEL_COUNT, \
327 ES51919_COMM_PARAM, ES51919_PACKET_SIZE, \
328 es51919_packet_valid, es51919_packet_parse, \
329 NULL, NULL, es51919_config_list, \
beedfa06
GS
330 }).di
331
332SR_REGISTER_DEV_DRIVER_LIST(lcr_es51919_drivers,
333 LCR_ES51919("deree-de5000", "DER EE", "DE-5000"),
ca9f8961 334 LCR_ES51919("mastech-ms5308", "MASTECH", "MS5308"),
6c62c605 335 LCR_ES51919("peaktech-2170", "PeakTech", "2170"),
6e407e98 336 LCR_ES51919("uni-t-ut612", "UNI-T", "UT612"),
beedfa06 337);