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.
28 /* Needed for gettimeofday(), at least on FreeBSD. */
29 #define _XOPEN_SOURCE 700
36 #include <libsigrok/libsigrok.h>
37 #include "libsigrok-internal.h"
40 #define LOG_PREFIX "std"
43 * Standard driver init() callback API helper.
45 * This function can be used to simplify most driver's init() API callback.
47 * Create a new 'struct drv_context' (drvc), assign sr_ctx to it, and
48 * then assign 'drvc' to the 'struct sr_dev_driver' (di) that is passed.
50 * @param[in] di The driver instance to use. Must not be NULL.
51 * @param[in] sr_ctx The libsigrok context to assign. May be NULL.
53 * @retval SR_OK Success.
54 * @retval SR_ERR_ARG Invalid argument.
56 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
58 struct drv_context *drvc;
61 sr_err("%s: Invalid argument.", __func__);
65 drvc = g_malloc0(sizeof(struct drv_context));
66 drvc->sr_ctx = sr_ctx;
67 drvc->instances = NULL;
74 * Standard driver cleanup() callback API helper.
76 * This function can be used to simplify most driver's cleanup() API callback.
78 * Free all device instances by calling sr_dev_clear() and then release any
79 * resources allocated by std_init().
81 * @param[in] di The driver instance to use. Must not be NULL.
83 * @retval SR_OK Success.
84 * @retval SR_ERR_ARG Invalid argument.
85 * @retval other Other error.
87 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
92 sr_err("%s: Invalid argument.", __func__);
96 ret = sr_dev_clear(di);
103 * Dummmy driver dev_open() callback API helper.
105 * @param[in] sdi The device instance to use. May be NULL (unused).
107 * @retval SR_OK Success.
109 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi)
117 * Dummmy driver dev_close() callback API helper.
119 * @param[in] sdi The device instance to use. May be NULL (unused).
121 * @retval SR_OK Success.
123 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi)
131 * Dummmy driver dev_acquisition_start() callback API helper.
133 * @param[in] sdi The device instance to use. May be NULL (unused).
135 * @retval SR_OK Success.
137 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi)
145 * Dummmy driver dev_acquisition_stop() callback API helper.
147 * @param[in] sdi The device instance to use. May be NULL (unused).
149 * @retval SR_OK Success.
151 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi)
159 * Standard API helper for sending an SR_DF_HEADER packet.
161 * This function can be used to simplify most drivers'
162 * dev_acquisition_start() API callback.
164 * @param[in] sdi The device instance to use. Must not be NULL.
166 * @retval SR_OK Success.
167 * @retval SR_ERR_ARG Invalid argument.
168 * @retval other Other error.
170 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
174 struct sr_datafeed_packet packet;
175 struct sr_datafeed_header header;
178 sr_err("%s: Invalid argument.", __func__);
182 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
184 /* Send header packet to the session bus. */
185 sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
186 packet.type = SR_DF_HEADER;
187 packet.payload = (uint8_t *)&header;
188 header.feed_version = 1;
189 gettimeofday(&header.starttime, NULL);
191 if ((ret = sr_session_send(sdi, &packet)) < 0) {
192 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
200 * Standard API helper for sending an SR_DF_END packet.
202 * This function can be used to simplify most drivers'
203 * dev_acquisition_stop() API callback.
205 * @param[in] sdi The device instance to use. Must not be NULL.
207 * @retval SR_OK Success.
208 * @retval SR_ERR_ARG Invalid argument.
209 * @retval other Other error.
211 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
215 struct sr_datafeed_packet packet;
218 sr_err("%s: Invalid argument.", __func__);
222 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
224 sr_dbg("%s: Sending SR_DF_END packet.", prefix);
226 packet.type = SR_DF_END;
227 packet.payload = NULL;
229 if ((ret = sr_session_send(sdi, &packet)) < 0) {
230 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
238 * Standard API helper for sending an SR_DF_FRAME_BEGIN packet.
240 * This function can be used to simplify most drivers'
243 * @param[in] sdi The device instance to use. Must not be NULL.
245 * @retval SR_OK Success.
246 * @retval SR_ERR_ARG Invalid argument.
247 * @retval other Other error.
249 SR_PRIV int std_session_send_frame_begin(const struct sr_dev_inst *sdi)
253 struct sr_datafeed_packet packet;
256 sr_err("%s: Invalid argument.", __func__);
260 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
262 sr_dbg("%s: Sending SR_DF_FRAME_BEGIN packet.", prefix);
264 packet.type = SR_DF_FRAME_BEGIN;
265 packet.payload = NULL;
267 if ((ret = sr_session_send(sdi, &packet)) < 0) {
268 sr_err("%s: Failed to send SR_DF_FRAME_BEGIN packet: %d.", prefix, ret);
276 * Standard API helper for sending an SR_DF_FRAME_END packet.
278 * This function can be used to simplify most drivers'
281 * @param[in] sdi The device instance to use. Must not be NULL.
283 * @retval SR_OK Success.
284 * @retval SR_ERR_ARG Invalid argument.
285 * @retval other Other error.
287 SR_PRIV int std_session_send_frame_end(const struct sr_dev_inst *sdi)
291 struct sr_datafeed_packet packet;
294 sr_err("%s: Invalid argument.", __func__);
298 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
300 sr_dbg("%s: Sending SR_DF_FRAME_END packet.", prefix);
302 packet.type = SR_DF_FRAME_END;
303 packet.payload = NULL;
305 if ((ret = sr_session_send(sdi, &packet)) < 0) {
306 sr_err("%s: Failed to send SR_DF_FRAME_END packet: %d.", prefix, ret);
313 #ifdef HAVE_LIBSERIALPORT
316 * Standard serial driver dev_open() callback API helper.
318 * This function can be used to implement the dev_open() driver API
319 * callback in drivers that use a serial port. The port is opened
320 * with the SERIAL_RDWR flag.
322 * @param[in] sdi The device instance to use. Must not be NULL.
324 * @retval SR_OK Success.
325 * @retval SR_ERR_ARG Invalid argument.
326 * @retval other Serial port open failed.
328 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
330 struct sr_serial_dev_inst *serial;
333 sr_err("%s: Invalid argument.", __func__);
339 return serial_open(serial, SERIAL_RDWR);
343 * Standard serial driver dev_close() callback API helper.
345 * This function can be used to implement the dev_close() driver API
346 * callback in drivers that use a serial port.
348 * @param[in] sdi The device instance to use. Must not be NULL.
350 * @retval SR_OK Success.
351 * @retval SR_ERR_ARG Invalid argument.
352 * @retval other Serial port close failed.
354 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
356 struct sr_serial_dev_inst *serial;
359 sr_err("%s: Invalid argument.", __func__);
365 return serial_close(serial);
369 * Standard serial driver dev_acquisition_stop() callback API helper.
371 * This function can be used to simplify most (serial port based) drivers'
372 * dev_acquisition_stop() API callback.
374 * @param[in] sdi The device instance for which acquisition should stop.
377 * @retval SR_OK Success.
378 * @retval SR_ERR_ARG Invalid argument.
379 * @retval other Other error.
381 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
383 struct sr_serial_dev_inst *serial;
388 sr_err("%s: Invalid argument.", __func__);
393 prefix = sdi->driver->name;
395 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
396 sr_err("%s: Failed to remove source: %d.", prefix, ret);
400 if ((ret = sr_dev_close(sdi)) < 0) {
401 sr_err("%s: Failed to close device: %d.", prefix, ret);
405 return std_session_send_df_end(sdi);
411 * Standard driver dev_clear() callback API helper.
413 * Clear driver, this means, close all instances.
415 * This function can be used to implement the dev_clear() driver API
416 * callback. dev_close() is called before every sr_dev_inst is cleared.
418 * The only limitation is driver-specific device contexts (sdi->priv / devc).
419 * These are freed, but any dynamic allocation within structs stored
420 * there cannot be freed.
422 * @param[in] driver The driver which will have its instances released.
424 * @param[in] clear_private If not NULL, this points to a function called
425 * with sdi->priv (devc) as argument. The function can then clear
426 * any device instance-specific resources kept there.
427 * It must NOT clear the struct pointed to by sdi->priv (devc),
428 * since this function will always free it after clear_private()
431 * @retval SR_OK Success.
432 * @retval SR_ERR_ARG Invalid argument.
433 * @retval SR_ERR_BUG Implementation bug.
434 * @retval other Other error.
436 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
437 std_dev_clear_callback clear_private)
439 struct drv_context *drvc;
440 struct sr_dev_inst *sdi;
445 sr_err("%s: Invalid argument.", __func__);
449 drvc = driver->context; /* Caller checked for context != NULL. */
452 for (l = drvc->instances; l; l = l->next) {
453 if (!(sdi = l->data)) {
454 sr_err("%s: Invalid device instance.", __func__);
458 if (driver->dev_close)
459 driver->dev_close(sdi);
462 #ifdef HAVE_LIBSERIALPORT
463 if (sdi->inst_type == SR_INST_SERIAL)
464 sr_serial_dev_inst_free(sdi->conn);
466 #ifdef HAVE_LIBUSB_1_0
467 if (sdi->inst_type == SR_INST_USB)
468 sr_usb_dev_inst_free(sdi->conn);
470 if (sdi->inst_type == SR_INST_SCPI)
471 sr_scpi_free(sdi->conn);
472 if (sdi->inst_type == SR_INST_MODBUS)
473 sr_modbus_free(sdi->conn);
476 /* Clear driver-specific stuff, if any. */
478 clear_private(sdi->priv);
480 /* Clear sdi->priv (devc). */
483 sr_dev_inst_free(sdi);
486 g_slist_free(drvc->instances);
487 drvc->instances = NULL;
492 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
494 return std_dev_clear_with_callback(driver, NULL);
498 * Standard driver dev_list() callback API helper.
500 * This function can be used as the dev_list() callback by most drivers.
502 * Return the devices contained in the driver context instances list.
504 * @param[in] di The driver instance to use. Must not be NULL.
506 * @retval NULL Error, or the list is empty.
507 * @retval other The list of device instances of this driver.
509 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
511 struct drv_context *drvc;
514 sr_err("%s: Invalid argument.", __func__);
520 return drvc->instances;
524 * Standard driver scan() callback API helper.
526 * This function can be used to perform common tasks required by a driver's
527 * scan() callback. It will initialize the driver for each device on the list
528 * and add the devices on the list to the driver's device instance list.
529 * Usually it should be used as the last step in the scan() callback, right
532 * Note: This function can only be used if std_init() has been called
533 * previously by the driver.
537 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
539 * struct GSList *device;
540 * struct sr_dev_inst *sdi;
542 * sdi = g_new0(sr_dev_inst, 1);
545 * devices = g_slist_append(devices, sdi);
547 * return std_scan_complete(di, devices);
551 * @param[in] di The driver instance to use. Must not be NULL.
552 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
555 * @return The @p devices list.
557 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
559 struct drv_context *drvc;
563 sr_err("Invalid driver instance (di), cannot complete scan.");
569 for (l = devices; l; l = l->next) {
570 struct sr_dev_inst *sdi = l->data;
572 sr_err("Invalid device instance, cannot complete scan.");
578 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
583 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
584 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
585 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
586 size_t drvsize, const uint32_t devopts[], size_t devsize)
589 case SR_CONF_SCAN_OPTIONS:
590 /* Always return scanopts, regardless of sdi or cg. */
593 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
594 scanopts, scansize, sizeof(uint32_t));
596 case SR_CONF_DEVICE_OPTIONS:
598 /* sdi == NULL: return drvopts. */
601 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
602 drvopts, drvsize, sizeof(uint32_t));
603 } else if (sdi && !cg) {
604 /* sdi != NULL, cg == NULL: return devopts. */
607 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
608 devopts, devsize, sizeof(uint32_t));
611 * Note: sdi != NULL, cg != NULL is not handled by
612 * this function since it's very driver-specific.
614 sr_err("%s: %s: sdi/cg != NULL: not handling.",
615 sdi->driver->name, __func__);
626 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
629 GVariant *rational[2];
632 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
634 for (i = 0; i < n; i++) {
635 rational[0] = g_variant_new_uint64(a[i][0]);
636 rational[1] = g_variant_new_uint64(a[i][1]);
638 /* FIXME: Valgrind reports a memory leak here. */
639 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
642 return g_variant_builder_end(&gvb);
645 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
648 GVariant *rational[2];
651 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
653 for (i = 0; i < n; i++) {
654 rational[0] = g_variant_new_uint64(r[i].p);
655 rational[1] = g_variant_new_uint64(r[i].q);
657 /* FIXME: Valgrind reports a memory leak here. */
658 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
661 return g_variant_builder_end(&gvb);
664 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
669 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
670 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
671 n, sizeof(uint64_t));
672 g_variant_builder_add(&gvb, "{sv}", str, gvar);
674 return g_variant_builder_end(&gvb);
677 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
679 return samplerate_helper(samplerates, n, "samplerates");
682 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
684 return samplerate_helper(samplerates, n, "samplerate-steps");
687 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
691 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
693 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
694 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
695 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
697 return g_variant_builder_end(&gvb);
700 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
705 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
707 for (i = 0; i < 3; i++)
708 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
710 return g_variant_builder_end(&gvb);
713 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
716 GVariant *gvar, *range[2];
719 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
721 for (d = min; d <= max; d += step) {
722 range[0] = g_variant_new_double(d);
723 range[1] = g_variant_new_double(d);
725 gvar = g_variant_new_tuple(range, 2);
726 g_variant_builder_add_value(&gvb, gvar);
729 return g_variant_builder_end(&gvb);
732 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
736 range[0] = g_variant_new_uint64(low);
737 range[1] = g_variant_new_uint64(high);
739 return g_variant_new_tuple(range, 2);
742 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
746 range[0] = g_variant_new_double(low);
747 range[1] = g_variant_new_double(high);
749 return g_variant_new_tuple(range, 2);
752 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n)
754 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
755 a, n, sizeof(int32_t));
758 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n)
760 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
761 a, n, sizeof(uint32_t));
764 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n)
766 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
767 a, n, sizeof(uint64_t));
770 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n)
773 GVariantBuilder *builder;
776 builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
778 for (i = 0; i < n; i++)
779 g_variant_builder_add(builder, "s", a[i]);
781 gvar = g_variant_new("as", builder);
782 g_variant_builder_unref(builder);
787 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
790 GVariant *gvar, *range[2];
793 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
795 for (i = 0; i < n; i++) {
796 range[0] = g_variant_new_double(a[i][0]);
797 range[1] = g_variant_new_double(a[i][1]);
798 gvar = g_variant_new_tuple(range, 2);
799 g_variant_builder_add_value(&gvb, gvar);
802 return g_variant_builder_end(&gvb);
805 /* Return the index of 'data' in the array 'arr' (or -1). */
806 static int find_in_array(GVariant *data, const GVariantType *type,
807 const void *arr, unsigned int n)
809 const char * const *sarr;
811 const uint64_t *u64arr;
812 const uint8_t *u8arr;
817 if (!g_variant_is_of_type(data, type))
820 switch (g_variant_classify(data)) {
821 case G_VARIANT_CLASS_STRING:
822 s = g_variant_get_string(data, NULL);
825 for (i = 0; i < n; i++)
826 if (!strcmp(s, sarr[i]))
829 case G_VARIANT_CLASS_UINT64:
830 u64 = g_variant_get_uint64(data);
833 for (i = 0; i < n; i++)
834 if (u64 == u64arr[i])
837 case G_VARIANT_CLASS_BYTE:
838 u8 = g_variant_get_byte(data);
841 for (i = 0; i < n; i++)
851 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
853 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
856 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
858 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
861 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
863 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
866 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
871 data = g_variant_new_string(s);
872 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
873 g_variant_unref(data);
878 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
883 data = g_variant_new_byte(b);
884 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
885 g_variant_unref(data);
890 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
895 g_variant_get(data, "(tt)", &low, &high);
897 for (i = 0; i < n; i++)
898 if (a[i][0] == low && a[i][1] == high)
904 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
909 g_variant_get(data, "(dd)", &low, &high);
911 for (i = 0; i < n; i++)
912 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
918 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
922 for (i = 0; i < n; i++)
929 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
933 for (i = 0; i < n; i++)