2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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/>.
23 * Standard API helper functions.
30 #include <libsigrok/libsigrok.h>
31 #include "libsigrok-internal.h"
34 #define LOG_PREFIX "std"
37 * Standard driver init() callback API helper.
39 * This function can be used to simplify most driver's init() API callback.
41 * Create a new 'struct drv_context' (drvc), assign sr_ctx to it, and
42 * then assign 'drvc' to the 'struct sr_dev_driver' (di) that is passed.
44 * @param[in] di The driver instance to use. Must not be NULL.
45 * @param[in] sr_ctx The libsigrok context to assign. May be NULL.
47 * @retval SR_OK Success.
48 * @retval SR_ERR_ARG Invalid argument.
50 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
52 struct drv_context *drvc;
55 sr_err("%s: Invalid argument.", __func__);
59 drvc = g_malloc0(sizeof(struct drv_context));
60 drvc->sr_ctx = sr_ctx;
61 drvc->instances = NULL;
68 * Standard driver cleanup() callback API helper.
70 * This function can be used to simplify most driver's cleanup() API callback.
72 * Free all device instances by calling sr_dev_clear() and then release any
73 * resources allocated by std_init().
75 * @param[in] di The driver instance to use. Must not be NULL.
77 * @retval SR_OK Success.
78 * @retval SR_ERR_ARG Invalid argument.
79 * @retval other Other error.
81 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
86 sr_err("%s: Invalid argument.", __func__);
90 ret = sr_dev_clear(di);
97 * Dummmy driver dev_open() callback API helper.
99 * @param[in] sdi The device instance to use. May be NULL (unused).
101 * @retval SR_OK Success.
103 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi)
111 * Dummmy driver dev_close() callback API helper.
113 * @param[in] sdi The device instance to use. May be NULL (unused).
115 * @retval SR_OK Success.
117 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi)
125 * Dummmy driver dev_acquisition_start() callback API helper.
127 * @param[in] sdi The device instance to use. May be NULL (unused).
129 * @retval SR_OK Success.
131 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi)
139 * Dummmy driver dev_acquisition_stop() callback API helper.
141 * @param[in] sdi The device instance to use. May be NULL (unused).
143 * @retval SR_OK Success.
145 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi)
153 * Standard API helper for sending an SR_DF_HEADER packet.
155 * This function can be used to simplify most drivers'
156 * dev_acquisition_start() API callback.
158 * @param[in] sdi The device instance to use. Must not be NULL.
160 * @retval SR_OK Success.
161 * @retval SR_ERR_ARG Invalid argument.
162 * @retval other Other error.
164 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
168 struct sr_datafeed_packet packet;
169 struct sr_datafeed_header header;
172 sr_err("%s: Invalid argument.", __func__);
176 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
178 /* Send header packet to the session bus. */
179 sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
180 packet.type = SR_DF_HEADER;
181 packet.payload = (uint8_t *)&header;
182 header.feed_version = 1;
183 gettimeofday(&header.starttime, NULL);
185 if ((ret = sr_session_send(sdi, &packet)) < 0) {
186 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
194 * Standard API helper for sending an SR_DF_END packet.
196 * This function can be used to simplify most drivers'
197 * dev_acquisition_stop() API callback.
199 * @param[in] sdi The device instance to use. Must not be NULL.
201 * @retval SR_OK Success.
202 * @retval SR_ERR_ARG Invalid argument.
203 * @retval other Other error.
205 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
209 struct sr_datafeed_packet packet;
212 sr_err("%s: Invalid argument.", __func__);
216 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
218 sr_dbg("%s: Sending SR_DF_END packet.", prefix);
220 packet.type = SR_DF_END;
221 packet.payload = NULL;
223 if ((ret = sr_session_send(sdi, &packet)) < 0) {
224 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
231 #ifdef HAVE_LIBSERIALPORT
234 * Standard serial driver dev_open() callback API helper.
236 * This function can be used to implement the dev_open() driver API
237 * callback in drivers that use a serial port. The port is opened
238 * with the SERIAL_RDWR flag.
240 * @param[in] sdi The device instance to use. Must not be NULL.
242 * @retval SR_OK Success.
243 * @retval SR_ERR_ARG Invalid argument.
244 * @retval other Serial port open failed.
246 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
248 struct sr_serial_dev_inst *serial;
251 sr_err("%s: Invalid argument.", __func__);
257 return serial_open(serial, SERIAL_RDWR);
261 * Standard serial driver dev_close() callback API helper.
263 * This function can be used to implement the dev_close() driver API
264 * callback in drivers that use a serial port.
266 * @param[in] sdi The device instance to use. Must not be NULL.
268 * @retval SR_OK Success.
269 * @retval SR_ERR_ARG Invalid argument.
270 * @retval other Serial port close failed.
272 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
274 struct sr_serial_dev_inst *serial;
277 sr_err("%s: Invalid argument.", __func__);
283 return serial_close(serial);
287 * Standard serial driver dev_acquisition_stop() callback API helper.
289 * This function can be used to simplify most (serial port based) drivers'
290 * dev_acquisition_stop() API callback.
292 * @param[in] sdi The device instance for which acquisition should stop.
295 * @retval SR_OK Success.
296 * @retval SR_ERR_ARG Invalid argument.
297 * @retval other Other error.
299 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
301 struct sr_serial_dev_inst *serial;
306 sr_err("%s: Invalid argument.", __func__);
311 prefix = sdi->driver->name;
313 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
314 sr_err("%s: Failed to remove source: %d.", prefix, ret);
318 if ((ret = sr_dev_close(sdi)) < 0) {
319 sr_err("%s: Failed to close device: %d.", prefix, ret);
323 return std_session_send_df_end(sdi);
329 * Standard driver dev_clear() callback API helper.
331 * Clear driver, this means, close all instances.
333 * This function can be used to implement the dev_clear() driver API
334 * callback. dev_close() is called before every sr_dev_inst is cleared.
336 * The only limitation is driver-specific device contexts (sdi->priv / devc).
337 * These are freed, but any dynamic allocation within structs stored
338 * there cannot be freed.
340 * @param[in] driver The driver which will have its instances released.
342 * @param[in] clear_private If not NULL, this points to a function called
343 * with sdi->priv (devc) as argument. The function can then clear
344 * any device instance-specific resources kept there.
345 * It must NOT clear the struct pointed to by sdi->priv (devc),
346 * since this function will always free it after clear_private()
349 * @retval SR_OK Success.
350 * @retval SR_ERR_ARG Invalid argument.
351 * @retval SR_ERR_BUG Implementation bug.
352 * @retval other Other error.
354 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
355 std_dev_clear_callback clear_private)
357 struct drv_context *drvc;
358 struct sr_dev_inst *sdi;
363 sr_err("%s: Invalid argument.", __func__);
367 drvc = driver->context; /* Caller checked for context != NULL. */
370 for (l = drvc->instances; l; l = l->next) {
371 if (!(sdi = l->data)) {
372 sr_err("%s: Invalid device instance.", __func__);
376 if (driver->dev_close)
377 driver->dev_close(sdi);
380 #ifdef HAVE_LIBSERIALPORT
381 if (sdi->inst_type == SR_INST_SERIAL)
382 sr_serial_dev_inst_free(sdi->conn);
384 #ifdef HAVE_LIBUSB_1_0
385 if (sdi->inst_type == SR_INST_USB)
386 sr_usb_dev_inst_free(sdi->conn);
388 if (sdi->inst_type == SR_INST_SCPI)
389 sr_scpi_free(sdi->conn);
390 if (sdi->inst_type == SR_INST_MODBUS)
391 sr_modbus_free(sdi->conn);
394 /* Clear driver-specific stuff, if any. */
396 clear_private(sdi->priv);
398 /* Clear sdi->priv (devc). */
401 sr_dev_inst_free(sdi);
404 g_slist_free(drvc->instances);
405 drvc->instances = NULL;
410 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
412 return std_dev_clear_with_callback(driver, NULL);
416 * Standard driver dev_list() callback API helper.
418 * This function can be used as the dev_list() callback by most drivers.
420 * Return the devices contained in the driver context instances list.
422 * @param[in] di The driver instance to use. Must not be NULL.
424 * @retval NULL Error, or the list is empty.
425 * @retval other The list of device instances of this driver.
427 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
429 struct drv_context *drvc;
432 sr_err("%s: Invalid argument.", __func__);
438 return drvc->instances;
442 * Standard driver scan() callback API helper.
444 * This function can be used to perform common tasks required by a driver's
445 * scan() callback. It will initialize the driver for each device on the list
446 * and add the devices on the list to the driver's device instance list.
447 * Usually it should be used as the last step in the scan() callback, right
450 * Note: This function can only be used if std_init() has been called
451 * previously by the driver.
455 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
457 * struct GSList *device;
458 * struct sr_dev_inst *sdi;
460 * sdi = g_new0(sr_dev_inst, 1);
463 * devices = g_slist_append(devices, sdi);
465 * return std_scan_complete(di, devices);
469 * @param[in] di The driver instance to use. Must not be NULL.
470 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
473 * @return The @p devices list.
475 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
477 struct drv_context *drvc;
481 sr_err("Invalid driver instance (di), cannot complete scan.");
487 for (l = devices; l; l = l->next) {
488 struct sr_dev_inst *sdi = l->data;
490 sr_err("Invalid device instance, cannot complete scan.");
496 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
501 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
502 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
503 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
504 size_t drvsize, const uint32_t devopts[], size_t devsize)
507 case SR_CONF_SCAN_OPTIONS:
508 /* Always return scanopts, regardless of sdi or cg. */
511 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512 scanopts, scansize, sizeof(uint32_t));
514 case SR_CONF_DEVICE_OPTIONS:
516 /* sdi == NULL: return drvopts. */
519 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
520 drvopts, drvsize, sizeof(uint32_t));
521 } else if (sdi && !cg) {
522 /* sdi != NULL, cg == NULL: return devopts. */
525 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
526 devopts, devsize, sizeof(uint32_t));
529 * Note: sdi != NULL, cg != NULL is not handled by
530 * this function since it's very driver-specific.
532 sr_err("%s: %s: sdi/cg != NULL: not handling.",
533 sdi->driver->name, __func__);
544 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t (*a)[][2], unsigned int n)
547 GVariant *rational[2];
550 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
552 for (i = 0; i < n; i++) {
553 rational[0] = g_variant_new_uint64((*a)[i][0]);
554 rational[1] = g_variant_new_uint64((*a)[i][1]);
556 /* FIXME: Valgrind reports a memory leak here. */
557 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
560 return g_variant_builder_end(&gvb);
563 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
566 GVariant *rational[2];
569 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
571 for (i = 0; i < n; i++) {
572 rational[0] = g_variant_new_uint64(r[i].p);
573 rational[1] = g_variant_new_uint64(r[i].q);
575 /* FIXME: Valgrind reports a memory leak here. */
576 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
579 return g_variant_builder_end(&gvb);
582 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
587 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
588 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
589 n, sizeof(uint64_t));
590 g_variant_builder_add(&gvb, "{sv}", str, gvar);
592 return g_variant_builder_end(&gvb);
595 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
597 return samplerate_helper(samplerates, n, "samplerates");
600 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
602 return samplerate_helper(samplerates, n, "samplerate-steps");
605 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
609 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
611 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
612 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
613 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
615 return g_variant_builder_end(&gvb);
618 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
623 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
625 for (i = 0; i < 3; i++)
626 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
628 return g_variant_builder_end(&gvb);