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_FRAME_BEGIN packet.
237 * This function can be used to simplify most drivers'
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 Other error.
246 SR_PRIV int std_session_send_frame_begin(const struct sr_dev_inst *sdi)
250 struct sr_datafeed_packet packet;
253 sr_err("%s: Invalid argument.", __func__);
257 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
259 packet.type = SR_DF_FRAME_BEGIN;
260 packet.payload = NULL;
262 if ((ret = sr_session_send(sdi, &packet)) < 0) {
263 sr_err("%s: Failed to send SR_DF_FRAME_BEGIN packet: %d.", prefix, ret);
271 * Standard API helper for sending an SR_DF_FRAME_END packet.
273 * This function can be used to simplify most drivers'
276 * @param[in] sdi The device instance to use. Must not be NULL.
278 * @retval SR_OK Success.
279 * @retval SR_ERR_ARG Invalid argument.
280 * @retval other Other error.
282 SR_PRIV int std_session_send_frame_end(const struct sr_dev_inst *sdi)
286 struct sr_datafeed_packet packet;
289 sr_err("%s: Invalid argument.", __func__);
293 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
295 packet.type = SR_DF_FRAME_END;
296 packet.payload = NULL;
298 if ((ret = sr_session_send(sdi, &packet)) < 0) {
299 sr_err("%s: Failed to send SR_DF_FRAME_END packet: %d.", prefix, ret);
306 #ifdef HAVE_SERIAL_COMM
309 * Standard serial driver dev_open() callback API helper.
311 * This function can be used to implement the dev_open() driver API
312 * callback in drivers that use a serial port. The port is opened
313 * with the SERIAL_RDWR flag.
315 * @param[in] sdi The device instance to use. Must not be NULL.
317 * @retval SR_OK Success.
318 * @retval SR_ERR_ARG Invalid argument.
319 * @retval other Serial port open failed.
321 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
323 struct sr_serial_dev_inst *serial;
326 sr_err("%s: Invalid argument.", __func__);
332 return serial_open(serial, SERIAL_RDWR);
336 * Standard serial driver dev_close() callback API helper.
338 * This function can be used to implement the dev_close() driver API
339 * callback in drivers that use a serial port.
341 * @param[in] sdi The device instance to use. Must not be NULL.
343 * @retval SR_OK Success.
344 * @retval SR_ERR_ARG Invalid argument.
345 * @retval other Serial port close failed.
347 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
349 struct sr_serial_dev_inst *serial;
352 sr_err("%s: Invalid argument.", __func__);
358 return serial_close(serial);
362 * Standard serial driver dev_acquisition_stop() callback API helper.
364 * This function can be used to simplify most (serial port based) drivers'
365 * dev_acquisition_stop() API callback.
367 * @param[in] sdi The device instance for which acquisition should stop.
370 * @retval SR_OK Success.
371 * @retval SR_ERR_ARG Invalid argument.
372 * @retval other Other error.
374 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
376 struct sr_serial_dev_inst *serial;
381 sr_err("%s: Invalid argument.", __func__);
386 prefix = sdi->driver->name;
388 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
389 sr_err("%s: Failed to remove source: %d.", prefix, ret);
393 return std_session_send_df_end(sdi);
399 * Standard driver dev_clear() callback API helper.
401 * Clear driver, this means, close all instances.
403 * This function can be used to implement the dev_clear() driver API
404 * callback. dev_close() is called before every sr_dev_inst is cleared.
406 * The only limitation is driver-specific device contexts (sdi->priv / devc).
407 * These are freed, but any dynamic allocation within structs stored
408 * there cannot be freed.
410 * @param[in] driver The driver which will have its instances released.
412 * @param[in] clear_private If not NULL, this points to a function called
413 * with sdi->priv (devc) as argument. The function can then clear
414 * any device instance-specific resources kept there.
415 * It must NOT clear the struct pointed to by sdi->priv (devc),
416 * since this function will always free it after clear_private()
419 * @retval SR_OK Success.
420 * @retval SR_ERR_ARG Invalid argument.
421 * @retval SR_ERR_BUG Implementation bug.
422 * @retval other Other error.
424 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
425 std_dev_clear_callback clear_private)
427 struct drv_context *drvc;
428 struct sr_dev_inst *sdi;
433 sr_err("%s: Invalid argument.", __func__);
437 drvc = driver->context; /* Caller checked for context != NULL. */
440 for (l = drvc->instances; l; l = l->next) {
441 if (!(sdi = l->data)) {
442 sr_err("%s: Invalid device instance.", __func__);
446 if (driver->dev_close)
447 driver->dev_close(sdi);
450 #ifdef HAVE_SERIAL_COMM
451 if (sdi->inst_type == SR_INST_SERIAL)
452 sr_serial_dev_inst_free(sdi->conn);
454 #ifdef HAVE_LIBUSB_1_0
455 if (sdi->inst_type == SR_INST_USB)
456 sr_usb_dev_inst_free(sdi->conn);
458 if (sdi->inst_type == SR_INST_SCPI)
459 sr_scpi_free(sdi->conn);
460 if (sdi->inst_type == SR_INST_MODBUS)
461 sr_modbus_free(sdi->conn);
464 /* Clear driver-specific stuff, if any. */
466 clear_private(sdi->priv);
468 /* Clear sdi->priv (devc). */
471 sr_dev_inst_free(sdi);
474 g_slist_free(drvc->instances);
475 drvc->instances = NULL;
480 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
482 return std_dev_clear_with_callback(driver, NULL);
486 * Standard driver dev_list() callback API helper.
488 * This function can be used as the dev_list() callback by most drivers.
490 * Return the devices contained in the driver context instances list.
492 * @param[in] di The driver instance to use. Must not be NULL.
494 * @retval NULL Error, or the list is empty.
495 * @retval other The list of device instances of this driver.
497 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
499 struct drv_context *drvc;
502 sr_err("%s: Invalid argument.", __func__);
508 return drvc->instances;
512 * Standard driver scan() callback API helper.
514 * This function can be used to perform common tasks required by a driver's
515 * scan() callback. It will initialize the driver for each device on the list
516 * and add the devices on the list to the driver's device instance list.
517 * Usually it should be used as the last step in the scan() callback, right
520 * Note: This function can only be used if std_init() has been called
521 * previously by the driver.
525 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
527 * struct GSList *device;
528 * struct sr_dev_inst *sdi;
530 * sdi = g_new0(sr_dev_inst, 1);
533 * devices = g_slist_append(devices, sdi);
535 * return std_scan_complete(di, devices);
539 * @param[in] di The driver instance to use. Must not be NULL.
540 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
543 * @return The @p devices list.
545 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
547 struct drv_context *drvc;
551 sr_err("Invalid driver instance (di), cannot complete scan.");
557 for (l = devices; l; l = l->next) {
558 struct sr_dev_inst *sdi = l->data;
560 sr_err("Invalid device instance, cannot complete scan.");
566 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
571 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
572 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
573 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
574 size_t drvsize, const uint32_t devopts[], size_t devsize)
577 case SR_CONF_SCAN_OPTIONS:
578 /* Always return scanopts, regardless of sdi or cg. */
579 if (!scanopts || scanopts == NO_OPTS)
581 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
582 scanopts, scansize, sizeof(uint32_t));
584 case SR_CONF_DEVICE_OPTIONS:
586 /* sdi == NULL: return drvopts. */
587 if (!drvopts || drvopts == NO_OPTS)
589 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
590 drvopts, drvsize, sizeof(uint32_t));
591 } else if (sdi && !cg) {
592 /* sdi != NULL, cg == NULL: return devopts. */
593 if (!devopts || devopts == NO_OPTS)
595 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
596 devopts, devsize, sizeof(uint32_t));
599 * Note: sdi != NULL, cg != NULL is not handled by
600 * this function since it's very driver-specific.
602 sr_err("%s: %s: sdi/cg != NULL: not handling.",
603 sdi->driver->name, __func__);
614 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
617 GVariant *rational[2];
620 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
622 for (i = 0; i < n; i++) {
623 rational[0] = g_variant_new_uint64(a[i][0]);
624 rational[1] = g_variant_new_uint64(a[i][1]);
626 /* FIXME: Valgrind reports a memory leak here. */
627 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
630 return g_variant_builder_end(&gvb);
633 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
636 GVariant *rational[2];
639 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
641 for (i = 0; i < n; i++) {
642 rational[0] = g_variant_new_uint64(r[i].p);
643 rational[1] = g_variant_new_uint64(r[i].q);
645 /* FIXME: Valgrind reports a memory leak here. */
646 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
649 return g_variant_builder_end(&gvb);
652 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
657 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
658 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
659 n, sizeof(uint64_t));
660 g_variant_builder_add(&gvb, "{sv}", str, gvar);
662 return g_variant_builder_end(&gvb);
665 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
667 return samplerate_helper(samplerates, n, "samplerates");
670 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
672 return samplerate_helper(samplerates, n, "samplerate-steps");
675 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
679 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
681 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
682 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
683 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
685 return g_variant_builder_end(&gvb);
688 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
693 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
695 for (i = 0; i < 3; i++)
696 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
698 return g_variant_builder_end(&gvb);
701 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
704 GVariant *gvar, *range[2];
707 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
709 for (d = min; d <= max; d += step) {
711 * We will never see exactly 0.0 because of the error we're
712 * accumulating, so catch the "zero" value and force it to be 0.
714 v = ((d > (-step / 2)) && (d < (step / 2))) ? 0 : d;
716 range[0] = g_variant_new_double(v);
717 range[1] = g_variant_new_double(v);
719 gvar = g_variant_new_tuple(range, 2);
720 g_variant_builder_add_value(&gvb, gvar);
723 return g_variant_builder_end(&gvb);
726 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
730 range[0] = g_variant_new_uint64(low);
731 range[1] = g_variant_new_uint64(high);
733 return g_variant_new_tuple(range, 2);
736 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
740 range[0] = g_variant_new_double(low);
741 range[1] = g_variant_new_double(high);
743 return g_variant_new_tuple(range, 2);
746 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n)
748 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
749 a, n, sizeof(int32_t));
752 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n)
754 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
755 a, n, sizeof(uint32_t));
758 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n)
760 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
761 a, n, sizeof(uint64_t));
764 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n)
767 GVariantBuilder *builder;
770 builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
772 for (i = 0; i < n; i++)
773 g_variant_builder_add(builder, "s", a[i]);
775 gvar = g_variant_new("as", builder);
776 g_variant_builder_unref(builder);
781 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
784 GVariant *gvar, *range[2];
787 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
789 for (i = 0; i < n; i++) {
790 range[0] = g_variant_new_double(a[i][0]);
791 range[1] = g_variant_new_double(a[i][1]);
792 gvar = g_variant_new_tuple(range, 2);
793 g_variant_builder_add_value(&gvb, gvar);
796 return g_variant_builder_end(&gvb);
799 /* Return the index of 'data' in the array 'arr' (or -1). */
800 static int find_in_array(GVariant *data, const GVariantType *type,
801 const void *arr, unsigned int n)
803 const char * const *sarr;
805 const uint64_t *u64arr;
806 const uint8_t *u8arr;
811 if (!g_variant_is_of_type(data, type))
814 switch (g_variant_classify(data)) {
815 case G_VARIANT_CLASS_STRING:
816 s = g_variant_get_string(data, NULL);
819 for (i = 0; i < n; i++)
820 if (!strcmp(s, sarr[i]))
823 case G_VARIANT_CLASS_UINT64:
824 u64 = g_variant_get_uint64(data);
827 for (i = 0; i < n; i++)
828 if (u64 == u64arr[i])
831 case G_VARIANT_CLASS_BYTE:
832 u8 = g_variant_get_byte(data);
835 for (i = 0; i < n; i++)
845 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
847 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
850 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
852 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
855 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
857 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
860 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
865 data = g_variant_new_string(s);
866 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
867 g_variant_unref(data);
872 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
877 data = g_variant_new_byte(b);
878 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
879 g_variant_unref(data);
884 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
889 g_variant_get(data, "(tt)", &low, &high);
891 for (i = 0; i < n; i++)
892 if (a[i][0] == low && a[i][1] == high)
898 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
903 g_variant_get(data, "(dd)", &low, &high);
905 for (i = 0; i < n; i++)
906 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
912 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
916 for (i = 0; i < n; i++)
923 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
927 for (i = 0; i < n; i++)
934 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
935 int baudrate, int bits, int parity, int stopbits,
936 int flowcontrol, int rts, int dtr)