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.
26 /* Needed for gettimeofday(), at least on FreeBSD. */
27 #define _XOPEN_SOURCE 700
34 #include <libsigrok/libsigrok.h>
35 #include "libsigrok-internal.h"
38 #define LOG_PREFIX "std"
40 SR_PRIV const uint32_t NO_OPTS[1] = {};
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 packet.type = SR_DF_HEADER;
186 packet.payload = (uint8_t *)&header;
187 header.feed_version = 1;
188 gettimeofday(&header.starttime, NULL);
190 if ((ret = sr_session_send(sdi, &packet)) < 0) {
191 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
199 * Standard API helper for sending an SR_DF_END packet.
201 * This function can be used to simplify most drivers'
202 * dev_acquisition_stop() API callback.
204 * @param[in] sdi The device instance to use. Must not be NULL.
206 * @retval SR_OK Success.
207 * @retval SR_ERR_ARG Invalid argument.
208 * @retval other Other error.
210 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
214 struct sr_datafeed_packet packet;
217 sr_err("%s: Invalid argument.", __func__);
221 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
223 packet.type = SR_DF_END;
224 packet.payload = NULL;
226 if ((ret = sr_session_send(sdi, &packet)) < 0) {
227 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
235 * Standard API helper for sending an SR_DF_TRIGGER packet.
237 * This function can be used to simplify most drivers' trigger handling.
239 * @param[in] sdi The device instance to use. Must not be NULL.
241 * @retval SR_OK Success.
242 * @retval SR_ERR_ARG Invalid argument.
243 * @retval other Other error.
245 SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi)
249 struct sr_datafeed_packet packet;
252 sr_err("%s: Invalid argument.", __func__);
256 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
258 packet.type = SR_DF_TRIGGER;
259 packet.payload = NULL;
261 if ((ret = sr_session_send(sdi, &packet)) < 0) {
262 sr_err("%s: Failed to send SR_DF_TRIGGER packet: %d.", prefix, ret);
270 * Standard API helper for sending an SR_DF_FRAME_BEGIN packet.
272 * This function can be used to simplify most drivers'
275 * @param[in] sdi The device instance to use. Must not be NULL.
277 * @retval SR_OK Success.
278 * @retval SR_ERR_ARG Invalid argument.
279 * @retval other Other error.
281 SR_PRIV int std_session_send_frame_begin(const struct sr_dev_inst *sdi)
285 struct sr_datafeed_packet packet;
288 sr_err("%s: Invalid argument.", __func__);
292 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
294 packet.type = SR_DF_FRAME_BEGIN;
295 packet.payload = NULL;
297 if ((ret = sr_session_send(sdi, &packet)) < 0) {
298 sr_err("%s: Failed to send SR_DF_FRAME_BEGIN packet: %d.", prefix, ret);
306 * Standard API helper for sending an SR_DF_FRAME_END packet.
308 * This function can be used to simplify most drivers'
311 * @param[in] sdi The device instance to use. Must not be NULL.
313 * @retval SR_OK Success.
314 * @retval SR_ERR_ARG Invalid argument.
315 * @retval other Other error.
317 SR_PRIV int std_session_send_frame_end(const struct sr_dev_inst *sdi)
321 struct sr_datafeed_packet packet;
324 sr_err("%s: Invalid argument.", __func__);
328 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
330 packet.type = SR_DF_FRAME_END;
331 packet.payload = NULL;
333 if ((ret = sr_session_send(sdi, &packet)) < 0) {
334 sr_err("%s: Failed to send SR_DF_FRAME_END packet: %d.", prefix, ret);
341 #ifdef HAVE_SERIAL_COMM
344 * Standard serial driver dev_open() callback API helper.
346 * This function can be used to implement the dev_open() driver API
347 * callback in drivers that use a serial port. The port is opened
348 * with the SERIAL_RDWR flag.
350 * @param[in] sdi The device instance to use. Must not be NULL.
352 * @retval SR_OK Success.
353 * @retval SR_ERR_ARG Invalid argument.
354 * @retval other Serial port open failed.
356 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
358 struct sr_serial_dev_inst *serial;
361 sr_err("%s: Invalid argument.", __func__);
367 return serial_open(serial, SERIAL_RDWR);
371 * Standard serial driver dev_close() callback API helper.
373 * This function can be used to implement the dev_close() driver API
374 * callback in drivers that use a serial port.
376 * @param[in] sdi The device instance to use. Must not be NULL.
378 * @retval SR_OK Success.
379 * @retval SR_ERR_ARG Invalid argument.
380 * @retval other Serial port close failed.
382 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
384 struct sr_serial_dev_inst *serial;
387 sr_err("%s: Invalid argument.", __func__);
393 return serial_close(serial);
397 * Standard serial driver dev_acquisition_stop() callback API helper.
399 * This function can be used to simplify most (serial port based) drivers'
400 * dev_acquisition_stop() API callback.
402 * @param[in] sdi The device instance for which acquisition should stop.
405 * @retval SR_OK Success.
406 * @retval SR_ERR_ARG Invalid argument.
407 * @retval other Other error.
409 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
411 struct sr_serial_dev_inst *serial;
416 sr_err("%s: Invalid argument.", __func__);
421 prefix = sdi->driver->name;
423 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
424 sr_err("%s: Failed to remove source: %d.", prefix, ret);
428 return std_session_send_df_end(sdi);
434 * Standard driver dev_clear() callback API helper.
436 * Clear driver, this means, close all instances.
438 * This function can be used to implement the dev_clear() driver API
439 * callback. dev_close() is called before every sr_dev_inst is cleared.
441 * The only limitation is driver-specific device contexts (sdi->priv / devc).
442 * These are freed, but any dynamic allocation within structs stored
443 * there cannot be freed.
445 * @param[in] driver The driver which will have its instances released.
447 * @param[in] clear_private If not NULL, this points to a function called
448 * with sdi->priv (devc) as argument. The function can then clear
449 * any device instance-specific resources kept there.
450 * It must NOT clear the struct pointed to by sdi->priv (devc),
451 * since this function will always free it after clear_private()
454 * @retval SR_OK Success.
455 * @retval SR_ERR_ARG Invalid argument.
456 * @retval SR_ERR_BUG Implementation bug.
457 * @retval other Other error.
459 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
460 std_dev_clear_callback clear_private)
462 struct drv_context *drvc;
463 struct sr_dev_inst *sdi;
468 sr_err("%s: Invalid argument.", __func__);
472 drvc = driver->context; /* Caller checked for context != NULL. */
475 for (l = drvc->instances; l; l = l->next) {
476 if (!(sdi = l->data)) {
477 sr_err("%s: Invalid device instance.", __func__);
481 if (driver->dev_close)
482 driver->dev_close(sdi);
485 #ifdef HAVE_SERIAL_COMM
486 if (sdi->inst_type == SR_INST_SERIAL)
487 sr_serial_dev_inst_free(sdi->conn);
489 #ifdef HAVE_LIBUSB_1_0
490 if (sdi->inst_type == SR_INST_USB)
491 sr_usb_dev_inst_free(sdi->conn);
493 if (sdi->inst_type == SR_INST_SCPI)
494 sr_scpi_free(sdi->conn);
495 if (sdi->inst_type == SR_INST_MODBUS)
496 sr_modbus_free(sdi->conn);
499 /* Clear driver-specific stuff, if any. */
501 clear_private(sdi->priv);
503 /* Clear sdi->priv (devc). */
506 sr_dev_inst_free(sdi);
509 g_slist_free(drvc->instances);
510 drvc->instances = NULL;
515 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
517 return std_dev_clear_with_callback(driver, NULL);
521 * Standard driver dev_list() callback API helper.
523 * This function can be used as the dev_list() callback by most drivers.
525 * Return the devices contained in the driver context instances list.
527 * @param[in] di The driver instance to use. Must not be NULL.
529 * @retval NULL Error, or the list is empty.
530 * @retval other The list of device instances of this driver.
532 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
534 struct drv_context *drvc;
537 sr_err("%s: Invalid argument.", __func__);
543 return drvc->instances;
547 * Standard driver scan() callback API helper.
549 * This function can be used to perform common tasks required by a driver's
550 * scan() callback. It will initialize the driver for each device on the list
551 * and add the devices on the list to the driver's device instance list.
552 * Usually it should be used as the last step in the scan() callback, right
555 * Note: This function can only be used if std_init() has been called
556 * previously by the driver.
560 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
562 * struct GSList *device;
563 * struct sr_dev_inst *sdi;
565 * sdi = g_new0(sr_dev_inst, 1);
568 * devices = g_slist_append(devices, sdi);
570 * return std_scan_complete(di, devices);
574 * @param[in] di The driver instance to use. Must not be NULL.
575 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
578 * @return The @p devices list.
580 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
582 struct drv_context *drvc;
586 sr_err("Invalid driver instance (di), cannot complete scan.");
592 for (l = devices; l; l = l->next) {
593 struct sr_dev_inst *sdi = l->data;
595 sr_err("Invalid device instance, cannot complete scan.");
601 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
606 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
607 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
608 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
609 size_t drvsize, const uint32_t devopts[], size_t devsize)
612 case SR_CONF_SCAN_OPTIONS:
613 /* Always return scanopts, regardless of sdi or cg. */
614 if (!scanopts || scanopts == NO_OPTS)
616 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
617 scanopts, scansize, sizeof(uint32_t));
619 case SR_CONF_DEVICE_OPTIONS:
621 /* sdi == NULL: return drvopts. */
622 if (!drvopts || drvopts == NO_OPTS)
624 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
625 drvopts, drvsize, sizeof(uint32_t));
626 } else if (sdi && !cg) {
627 /* sdi != NULL, cg == NULL: return devopts. */
628 if (!devopts || devopts == NO_OPTS)
630 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
631 devopts, devsize, sizeof(uint32_t));
634 * Note: sdi != NULL, cg != NULL is not handled by
635 * this function since it's very driver-specific.
637 sr_err("%s: %s: sdi/cg != NULL: not handling.",
638 sdi->driver->name, __func__);
649 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
652 GVariant *rational[2];
655 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
657 for (i = 0; i < n; i++) {
658 rational[0] = g_variant_new_uint64(a[i][0]);
659 rational[1] = g_variant_new_uint64(a[i][1]);
661 /* FIXME: Valgrind reports a memory leak here. */
662 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
665 return g_variant_builder_end(&gvb);
668 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
671 GVariant *rational[2];
674 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
676 for (i = 0; i < n; i++) {
677 rational[0] = g_variant_new_uint64(r[i].p);
678 rational[1] = g_variant_new_uint64(r[i].q);
680 /* FIXME: Valgrind reports a memory leak here. */
681 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
684 return g_variant_builder_end(&gvb);
687 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
692 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
693 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
694 n, sizeof(uint64_t));
695 g_variant_builder_add(&gvb, "{sv}", str, gvar);
697 return g_variant_builder_end(&gvb);
700 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
702 return samplerate_helper(samplerates, n, "samplerates");
705 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
707 return samplerate_helper(samplerates, n, "samplerate-steps");
710 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
714 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
716 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
717 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
718 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
720 return g_variant_builder_end(&gvb);
723 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
728 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
730 for (i = 0; i < 3; i++)
731 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
733 return g_variant_builder_end(&gvb);
736 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
739 GVariant *gvar, *range[2];
742 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
744 for (d = min; d <= max; d += step) {
746 * We will never see exactly 0.0 because of the error we're
747 * accumulating, so catch the "zero" value and force it to be 0.
749 v = ((d > (-step / 2)) && (d < (step / 2))) ? 0 : d;
751 range[0] = g_variant_new_double(v);
752 range[1] = g_variant_new_double(v);
754 gvar = g_variant_new_tuple(range, 2);
755 g_variant_builder_add_value(&gvb, gvar);
758 return g_variant_builder_end(&gvb);
761 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
765 range[0] = g_variant_new_uint64(low);
766 range[1] = g_variant_new_uint64(high);
768 return g_variant_new_tuple(range, 2);
771 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
775 range[0] = g_variant_new_double(low);
776 range[1] = g_variant_new_double(high);
778 return g_variant_new_tuple(range, 2);
781 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n)
783 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
784 a, n, sizeof(int32_t));
787 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n)
789 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
790 a, n, sizeof(uint32_t));
793 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n)
795 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
796 a, n, sizeof(uint64_t));
799 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n)
802 GVariantBuilder *builder;
805 builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
807 for (i = 0; i < n; i++)
808 g_variant_builder_add(builder, "s", a[i]);
810 gvar = g_variant_new("as", builder);
811 g_variant_builder_unref(builder);
816 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
819 GVariant *gvar, *range[2];
822 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
824 for (i = 0; i < n; i++) {
825 range[0] = g_variant_new_double(a[i][0]);
826 range[1] = g_variant_new_double(a[i][1]);
827 gvar = g_variant_new_tuple(range, 2);
828 g_variant_builder_add_value(&gvb, gvar);
831 return g_variant_builder_end(&gvb);
834 /* Return the index of 'data' in the array 'arr' (or -1). */
835 static int find_in_array(GVariant *data, const GVariantType *type,
836 const void *arr, unsigned int n)
838 const char * const *sarr;
840 const uint64_t *u64arr;
841 const uint8_t *u8arr;
846 if (!g_variant_is_of_type(data, type))
849 switch (g_variant_classify(data)) {
850 case G_VARIANT_CLASS_STRING:
851 s = g_variant_get_string(data, NULL);
854 for (i = 0; i < n; i++)
855 if (!strcmp(s, sarr[i]))
858 case G_VARIANT_CLASS_UINT64:
859 u64 = g_variant_get_uint64(data);
862 for (i = 0; i < n; i++)
863 if (u64 == u64arr[i])
866 case G_VARIANT_CLASS_BYTE:
867 u8 = g_variant_get_byte(data);
870 for (i = 0; i < n; i++)
880 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
882 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
885 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
887 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
890 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
892 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
895 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
900 data = g_variant_new_string(s);
901 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
902 g_variant_unref(data);
907 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
912 data = g_variant_new_byte(b);
913 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
914 g_variant_unref(data);
919 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
924 g_variant_get(data, "(tt)", &low, &high);
926 for (i = 0; i < n; i++)
927 if (a[i][0] == low && a[i][1] == high)
933 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
938 g_variant_get(data, "(dd)", &low, &high);
940 for (i = 0; i < n; i++)
941 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
947 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
951 for (i = 0; i < n; i++)
958 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
962 for (i = 0; i < n; i++)
969 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
970 int baudrate, int bits, int parity, int stopbits,
971 int flowcontrol, int rts, int dtr)