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