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"
42 SR_PRIV const uint32_t NO_OPTS[1] = {};
45 * Standard driver init() callback API helper.
47 * This function can be used to simplify most driver's init() API callback.
49 * Create a new 'struct drv_context' (drvc), assign sr_ctx to it, and
50 * then assign 'drvc' to the 'struct sr_dev_driver' (di) that is passed.
52 * @param[in] di The driver instance to use. Must not be NULL.
53 * @param[in] sr_ctx The libsigrok context to assign. May be NULL.
55 * @retval SR_OK Success.
56 * @retval SR_ERR_ARG Invalid argument.
58 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
60 struct drv_context *drvc;
63 sr_err("%s: Invalid argument.", __func__);
67 drvc = g_malloc0(sizeof(struct drv_context));
68 drvc->sr_ctx = sr_ctx;
69 drvc->instances = NULL;
76 * Standard driver cleanup() callback API helper.
78 * This function can be used to simplify most driver's cleanup() API callback.
80 * Free all device instances by calling sr_dev_clear() and then release any
81 * resources allocated by std_init().
83 * @param[in] di The driver instance to use. Must not be NULL.
85 * @retval SR_OK Success.
86 * @retval SR_ERR_ARG Invalid argument.
87 * @retval other Other error.
89 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
94 sr_err("%s: Invalid argument.", __func__);
98 ret = sr_dev_clear(di);
105 * Dummmy driver dev_open() callback API helper.
107 * @param[in] sdi The device instance to use. May be NULL (unused).
109 * @retval SR_OK Success.
111 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi)
119 * Dummmy driver dev_close() callback API helper.
121 * @param[in] sdi The device instance to use. May be NULL (unused).
123 * @retval SR_OK Success.
125 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi)
133 * Dummmy driver dev_acquisition_start() callback API helper.
135 * @param[in] sdi The device instance to use. May be NULL (unused).
137 * @retval SR_OK Success.
139 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi)
147 * Dummmy driver dev_acquisition_stop() callback API helper.
149 * @param[in] sdi The device instance to use. May be NULL (unused).
151 * @retval SR_OK Success.
153 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi)
161 * Standard API helper for sending an SR_DF_HEADER packet.
163 * This function can be used to simplify most drivers'
164 * dev_acquisition_start() API callback.
166 * @param[in] sdi The device instance to use. Must not be NULL.
168 * @retval SR_OK Success.
169 * @retval SR_ERR_ARG Invalid argument.
170 * @retval other Other error.
172 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
176 struct sr_datafeed_packet packet;
177 struct sr_datafeed_header header;
180 sr_err("%s: Invalid argument.", __func__);
184 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
186 /* Send header packet to the session bus. */
187 packet.type = SR_DF_HEADER;
188 packet.payload = (uint8_t *)&header;
189 header.feed_version = 1;
190 gettimeofday(&header.starttime, NULL);
192 if ((ret = sr_session_send(sdi, &packet)) < 0) {
193 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
201 * Standard API helper for sending an SR_DF_END packet.
203 * This function can be used to simplify most drivers'
204 * dev_acquisition_stop() API callback.
206 * @param[in] sdi The device instance to use. Must not be NULL.
208 * @retval SR_OK Success.
209 * @retval SR_ERR_ARG Invalid argument.
210 * @retval other Other error.
212 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
216 struct sr_datafeed_packet packet;
219 sr_err("%s: Invalid argument.", __func__);
223 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
225 packet.type = SR_DF_END;
226 packet.payload = NULL;
228 if ((ret = sr_session_send(sdi, &packet)) < 0) {
229 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
237 * Standard API helper for sending an SR_DF_FRAME_BEGIN packet.
239 * This function can be used to simplify most drivers'
242 * @param[in] sdi The device instance to use. Must not be NULL.
244 * @retval SR_OK Success.
245 * @retval SR_ERR_ARG Invalid argument.
246 * @retval other Other error.
248 SR_PRIV int std_session_send_frame_begin(const struct sr_dev_inst *sdi)
252 struct sr_datafeed_packet packet;
255 sr_err("%s: Invalid argument.", __func__);
259 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
261 packet.type = SR_DF_FRAME_BEGIN;
262 packet.payload = NULL;
264 if ((ret = sr_session_send(sdi, &packet)) < 0) {
265 sr_err("%s: Failed to send SR_DF_FRAME_BEGIN packet: %d.", prefix, ret);
273 * Standard API helper for sending an SR_DF_FRAME_END packet.
275 * This function can be used to simplify most drivers'
278 * @param[in] sdi The device instance to use. Must not be NULL.
280 * @retval SR_OK Success.
281 * @retval SR_ERR_ARG Invalid argument.
282 * @retval other Other error.
284 SR_PRIV int std_session_send_frame_end(const struct sr_dev_inst *sdi)
288 struct sr_datafeed_packet packet;
291 sr_err("%s: Invalid argument.", __func__);
295 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
297 packet.type = SR_DF_FRAME_END;
298 packet.payload = NULL;
300 if ((ret = sr_session_send(sdi, &packet)) < 0) {
301 sr_err("%s: Failed to send SR_DF_FRAME_END packet: %d.", prefix, ret);
308 #ifdef HAVE_SERIAL_COMM
311 * Standard serial driver dev_open() callback API helper.
313 * This function can be used to implement the dev_open() driver API
314 * callback in drivers that use a serial port. The port is opened
315 * with the SERIAL_RDWR flag.
317 * @param[in] sdi The device instance to use. Must not be NULL.
319 * @retval SR_OK Success.
320 * @retval SR_ERR_ARG Invalid argument.
321 * @retval other Serial port open failed.
323 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
325 struct sr_serial_dev_inst *serial;
328 sr_err("%s: Invalid argument.", __func__);
334 return serial_open(serial, SERIAL_RDWR);
338 * Standard serial driver dev_close() callback API helper.
340 * This function can be used to implement the dev_close() driver API
341 * callback in drivers that use a serial port.
343 * @param[in] sdi The device instance to use. Must not be NULL.
345 * @retval SR_OK Success.
346 * @retval SR_ERR_ARG Invalid argument.
347 * @retval other Serial port close failed.
349 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
351 struct sr_serial_dev_inst *serial;
354 sr_err("%s: Invalid argument.", __func__);
360 return serial_close(serial);
364 * Standard serial driver dev_acquisition_stop() callback API helper.
366 * This function can be used to simplify most (serial port based) drivers'
367 * dev_acquisition_stop() API callback.
369 * @param[in] sdi The device instance for which acquisition should stop.
372 * @retval SR_OK Success.
373 * @retval SR_ERR_ARG Invalid argument.
374 * @retval other Other error.
376 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
378 struct sr_serial_dev_inst *serial;
383 sr_err("%s: Invalid argument.", __func__);
388 prefix = sdi->driver->name;
390 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
391 sr_err("%s: Failed to remove source: %d.", prefix, ret);
395 if ((ret = sr_dev_close(sdi)) < 0) {
396 sr_err("%s: Failed to close device: %d.", prefix, ret);
400 return std_session_send_df_end(sdi);
406 * Standard driver dev_clear() callback API helper.
408 * Clear driver, this means, close all instances.
410 * This function can be used to implement the dev_clear() driver API
411 * callback. dev_close() is called before every sr_dev_inst is cleared.
413 * The only limitation is driver-specific device contexts (sdi->priv / devc).
414 * These are freed, but any dynamic allocation within structs stored
415 * there cannot be freed.
417 * @param[in] driver The driver which will have its instances released.
419 * @param[in] clear_private If not NULL, this points to a function called
420 * with sdi->priv (devc) as argument. The function can then clear
421 * any device instance-specific resources kept there.
422 * It must NOT clear the struct pointed to by sdi->priv (devc),
423 * since this function will always free it after clear_private()
426 * @retval SR_OK Success.
427 * @retval SR_ERR_ARG Invalid argument.
428 * @retval SR_ERR_BUG Implementation bug.
429 * @retval other Other error.
431 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
432 std_dev_clear_callback clear_private)
434 struct drv_context *drvc;
435 struct sr_dev_inst *sdi;
440 sr_err("%s: Invalid argument.", __func__);
444 drvc = driver->context; /* Caller checked for context != NULL. */
447 for (l = drvc->instances; l; l = l->next) {
448 if (!(sdi = l->data)) {
449 sr_err("%s: Invalid device instance.", __func__);
453 if (driver->dev_close)
454 driver->dev_close(sdi);
457 #ifdef HAVE_SERIAL_COMM
458 if (sdi->inst_type == SR_INST_SERIAL)
459 sr_serial_dev_inst_free(sdi->conn);
461 #ifdef HAVE_LIBUSB_1_0
462 if (sdi->inst_type == SR_INST_USB)
463 sr_usb_dev_inst_free(sdi->conn);
465 if (sdi->inst_type == SR_INST_SCPI)
466 sr_scpi_free(sdi->conn);
467 if (sdi->inst_type == SR_INST_MODBUS)
468 sr_modbus_free(sdi->conn);
471 /* Clear driver-specific stuff, if any. */
473 clear_private(sdi->priv);
475 /* Clear sdi->priv (devc). */
478 sr_dev_inst_free(sdi);
481 g_slist_free(drvc->instances);
482 drvc->instances = NULL;
487 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
489 return std_dev_clear_with_callback(driver, NULL);
493 * Standard driver dev_list() callback API helper.
495 * This function can be used as the dev_list() callback by most drivers.
497 * Return the devices contained in the driver context instances list.
499 * @param[in] di The driver instance to use. Must not be NULL.
501 * @retval NULL Error, or the list is empty.
502 * @retval other The list of device instances of this driver.
504 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
506 struct drv_context *drvc;
509 sr_err("%s: Invalid argument.", __func__);
515 return drvc->instances;
519 * Standard driver scan() callback API helper.
521 * This function can be used to perform common tasks required by a driver's
522 * scan() callback. It will initialize the driver for each device on the list
523 * and add the devices on the list to the driver's device instance list.
524 * Usually it should be used as the last step in the scan() callback, right
527 * Note: This function can only be used if std_init() has been called
528 * previously by the driver.
532 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
534 * struct GSList *device;
535 * struct sr_dev_inst *sdi;
537 * sdi = g_new0(sr_dev_inst, 1);
540 * devices = g_slist_append(devices, sdi);
542 * return std_scan_complete(di, devices);
546 * @param[in] di The driver instance to use. Must not be NULL.
547 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
550 * @return The @p devices list.
552 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
554 struct drv_context *drvc;
558 sr_err("Invalid driver instance (di), cannot complete scan.");
564 for (l = devices; l; l = l->next) {
565 struct sr_dev_inst *sdi = l->data;
567 sr_err("Invalid device instance, cannot complete scan.");
573 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
578 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
579 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
580 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
581 size_t drvsize, const uint32_t devopts[], size_t devsize)
584 case SR_CONF_SCAN_OPTIONS:
585 /* Always return scanopts, regardless of sdi or cg. */
586 if (!scanopts || scanopts == NO_OPTS)
588 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
589 scanopts, scansize, sizeof(uint32_t));
591 case SR_CONF_DEVICE_OPTIONS:
593 /* sdi == NULL: return drvopts. */
594 if (!drvopts || drvopts == NO_OPTS)
596 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
597 drvopts, drvsize, sizeof(uint32_t));
598 } else if (sdi && !cg) {
599 /* sdi != NULL, cg == NULL: return devopts. */
600 if (!devopts || devopts == NO_OPTS)
602 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
603 devopts, devsize, sizeof(uint32_t));
606 * Note: sdi != NULL, cg != NULL is not handled by
607 * this function since it's very driver-specific.
609 sr_err("%s: %s: sdi/cg != NULL: not handling.",
610 sdi->driver->name, __func__);
621 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
624 GVariant *rational[2];
627 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
629 for (i = 0; i < n; i++) {
630 rational[0] = g_variant_new_uint64(a[i][0]);
631 rational[1] = g_variant_new_uint64(a[i][1]);
633 /* FIXME: Valgrind reports a memory leak here. */
634 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
637 return g_variant_builder_end(&gvb);
640 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
643 GVariant *rational[2];
646 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
648 for (i = 0; i < n; i++) {
649 rational[0] = g_variant_new_uint64(r[i].p);
650 rational[1] = g_variant_new_uint64(r[i].q);
652 /* FIXME: Valgrind reports a memory leak here. */
653 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
656 return g_variant_builder_end(&gvb);
659 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
664 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
665 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
666 n, sizeof(uint64_t));
667 g_variant_builder_add(&gvb, "{sv}", str, gvar);
669 return g_variant_builder_end(&gvb);
672 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
674 return samplerate_helper(samplerates, n, "samplerates");
677 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
679 return samplerate_helper(samplerates, n, "samplerate-steps");
682 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
686 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
688 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
689 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
690 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
692 return g_variant_builder_end(&gvb);
695 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
700 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
702 for (i = 0; i < 3; i++)
703 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
705 return g_variant_builder_end(&gvb);
708 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
711 GVariant *gvar, *range[2];
714 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
716 for (d = min; d <= max; d += step) {
718 * We will never see exactly 0.0 because of the error we're
719 * accumulating, so catch the "zero" value and force it to be 0.
721 v = ((d > (-step / 2)) && (d < (step / 2))) ? 0 : d;
723 range[0] = g_variant_new_double(v);
724 range[1] = g_variant_new_double(v);
726 gvar = g_variant_new_tuple(range, 2);
727 g_variant_builder_add_value(&gvb, gvar);
730 return g_variant_builder_end(&gvb);
733 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
737 range[0] = g_variant_new_uint64(low);
738 range[1] = g_variant_new_uint64(high);
740 return g_variant_new_tuple(range, 2);
743 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
747 range[0] = g_variant_new_double(low);
748 range[1] = g_variant_new_double(high);
750 return g_variant_new_tuple(range, 2);
753 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n)
755 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
756 a, n, sizeof(int32_t));
759 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n)
761 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
762 a, n, sizeof(uint32_t));
765 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n)
767 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
768 a, n, sizeof(uint64_t));
771 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n)
774 GVariantBuilder *builder;
777 builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
779 for (i = 0; i < n; i++)
780 g_variant_builder_add(builder, "s", a[i]);
782 gvar = g_variant_new("as", builder);
783 g_variant_builder_unref(builder);
788 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
791 GVariant *gvar, *range[2];
794 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
796 for (i = 0; i < n; i++) {
797 range[0] = g_variant_new_double(a[i][0]);
798 range[1] = g_variant_new_double(a[i][1]);
799 gvar = g_variant_new_tuple(range, 2);
800 g_variant_builder_add_value(&gvb, gvar);
803 return g_variant_builder_end(&gvb);
806 /* Return the index of 'data' in the array 'arr' (or -1). */
807 static int find_in_array(GVariant *data, const GVariantType *type,
808 const void *arr, unsigned int n)
810 const char * const *sarr;
812 const uint64_t *u64arr;
813 const uint8_t *u8arr;
818 if (!g_variant_is_of_type(data, type))
821 switch (g_variant_classify(data)) {
822 case G_VARIANT_CLASS_STRING:
823 s = g_variant_get_string(data, NULL);
826 for (i = 0; i < n; i++)
827 if (!strcmp(s, sarr[i]))
830 case G_VARIANT_CLASS_UINT64:
831 u64 = g_variant_get_uint64(data);
834 for (i = 0; i < n; i++)
835 if (u64 == u64arr[i])
838 case G_VARIANT_CLASS_BYTE:
839 u8 = g_variant_get_byte(data);
842 for (i = 0; i < n; i++)
852 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
854 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
857 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
859 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
862 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
864 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
867 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
872 data = g_variant_new_string(s);
873 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
874 g_variant_unref(data);
879 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
884 data = g_variant_new_byte(b);
885 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
886 g_variant_unref(data);
891 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
896 g_variant_get(data, "(tt)", &low, &high);
898 for (i = 0; i < n; i++)
899 if (a[i][0] == low && a[i][1] == high)
905 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
910 g_variant_get(data, "(dd)", &low, &high);
912 for (i = 0; i < n; i++)
913 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
919 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
923 for (i = 0; i < n; i++)
930 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
934 for (i = 0; i < n; i++)
941 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
942 int baudrate, int bits, int parity, int stopbits,
943 int flowcontrol, int rts, int dtr)