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);
198 static int send_df_without_payload(const struct sr_dev_inst *sdi, uint16_t packet_type)
202 struct sr_datafeed_packet packet;
205 sr_err("%s: Invalid argument.", __func__);
209 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
211 packet.type = packet_type;
212 packet.payload = NULL;
214 if ((ret = sr_session_send(sdi, &packet)) < 0) {
215 sr_err("%s: Failed to send packet of type %d: %d.", prefix, packet_type, ret);
223 * Standard API helper for sending an SR_DF_END packet.
225 * This function can be used to simplify most drivers'
226 * dev_acquisition_stop() API callback.
228 * @param[in] sdi The device instance to use. Must not be NULL.
230 * @retval SR_OK Success.
231 * @retval SR_ERR_ARG Invalid argument.
232 * @retval other Other error.
234 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
236 return send_df_without_payload(sdi, SR_DF_END);
240 * Standard API helper for sending an SR_DF_TRIGGER packet.
242 * This function can be used to simplify most drivers' trigger handling.
244 * @param[in] sdi The device instance to use. Must not be NULL.
246 * @retval SR_OK Success.
247 * @retval SR_ERR_ARG Invalid argument.
248 * @retval other Other error.
250 SR_PRIV int std_session_send_df_trigger(const struct sr_dev_inst *sdi)
252 return send_df_without_payload(sdi, SR_DF_TRIGGER);
256 * Standard API helper for sending an SR_DF_FRAME_BEGIN packet.
258 * This function can be used to simplify most drivers' frame handling.
260 * @param[in] sdi The device instance to use. Must not be NULL.
262 * @retval SR_OK Success.
263 * @retval SR_ERR_ARG Invalid argument.
264 * @retval other Other error.
266 SR_PRIV int std_session_send_df_frame_begin(const struct sr_dev_inst *sdi)
268 return send_df_without_payload(sdi, SR_DF_FRAME_BEGIN);
272 * Standard API helper for sending an SR_DF_FRAME_END packet.
274 * This function can be used to simplify most drivers' frame handling.
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_df_frame_end(const struct sr_dev_inst *sdi)
284 return send_df_without_payload(sdi, SR_DF_FRAME_END);
287 #ifdef HAVE_SERIAL_COMM
290 * Standard serial driver dev_open() callback API helper.
292 * This function can be used to implement the dev_open() driver API
293 * callback in drivers that use a serial port. The port is opened
294 * with the SERIAL_RDWR flag.
296 * @param[in] sdi The device instance to use. Must not be NULL.
298 * @retval SR_OK Success.
299 * @retval SR_ERR_ARG Invalid argument.
300 * @retval other Serial port open failed.
302 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
304 struct sr_serial_dev_inst *serial;
307 sr_err("%s: Invalid argument.", __func__);
313 return serial_open(serial, SERIAL_RDWR);
317 * Standard serial driver dev_close() callback API helper.
319 * This function can be used to implement the dev_close() driver API
320 * callback in drivers that use a serial port.
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 close failed.
328 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
330 struct sr_serial_dev_inst *serial;
333 sr_err("%s: Invalid argument.", __func__);
339 return serial_close(serial);
343 * Standard serial driver dev_acquisition_stop() callback API helper.
345 * This function can be used to simplify most (serial port based) drivers'
346 * dev_acquisition_stop() API callback.
348 * @param[in] sdi The device instance for which acquisition should stop.
351 * @retval SR_OK Success.
352 * @retval SR_ERR_ARG Invalid argument.
353 * @retval other Other error.
355 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
357 struct sr_serial_dev_inst *serial;
362 sr_err("%s: Invalid argument.", __func__);
367 prefix = sdi->driver->name;
369 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
370 sr_err("%s: Failed to remove source: %d.", prefix, ret);
374 return std_session_send_df_end(sdi);
380 * Standard driver dev_clear() callback API helper.
382 * Clear driver, this means, close all instances.
384 * This function can be used to implement the dev_clear() driver API
385 * callback. dev_close() is called before every sr_dev_inst is cleared.
387 * The only limitation is driver-specific device contexts (sdi->priv / devc).
388 * These are freed, but any dynamic allocation within structs stored
389 * there cannot be freed.
391 * @param[in] driver The driver which will have its instances released.
393 * @param[in] clear_private If not NULL, this points to a function called
394 * with sdi->priv (devc) as argument. The function can then clear
395 * any device instance-specific resources kept there.
396 * It must NOT clear the struct pointed to by sdi->priv (devc),
397 * since this function will always free it after clear_private()
400 * @retval SR_OK Success.
401 * @retval SR_ERR_ARG Invalid argument.
402 * @retval SR_ERR_BUG Implementation bug.
403 * @retval other Other error.
405 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
406 std_dev_clear_callback clear_private)
408 struct drv_context *drvc;
409 struct sr_dev_inst *sdi;
414 sr_err("%s: Invalid argument.", __func__);
418 drvc = driver->context; /* Caller checked for context != NULL. */
421 for (l = drvc->instances; l; l = l->next) {
422 if (!(sdi = l->data)) {
423 sr_err("%s: Invalid device instance.", __func__);
427 if (driver->dev_close && sdi->status == SR_ST_ACTIVE)
428 driver->dev_close(sdi);
431 #ifdef HAVE_SERIAL_COMM
432 if (sdi->inst_type == SR_INST_SERIAL)
433 sr_serial_dev_inst_free(sdi->conn);
435 #ifdef HAVE_LIBUSB_1_0
436 if (sdi->inst_type == SR_INST_USB)
437 sr_usb_dev_inst_free(sdi->conn);
439 if (sdi->inst_type == SR_INST_SCPI)
440 sr_scpi_free(sdi->conn);
441 if (sdi->inst_type == SR_INST_MODBUS)
442 sr_modbus_free(sdi->conn);
445 /* Clear driver-specific stuff, if any. */
447 clear_private(sdi->priv);
449 /* Clear sdi->priv (devc). */
452 sr_dev_inst_free(sdi);
455 g_slist_free(drvc->instances);
456 drvc->instances = NULL;
461 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
463 return std_dev_clear_with_callback(driver, NULL);
467 * Standard driver dev_list() callback API helper.
469 * This function can be used as the dev_list() callback by most drivers.
471 * Return the devices contained in the driver context instances list.
473 * @param[in] di The driver instance to use. Must not be NULL.
475 * @retval NULL Error, or the list is empty.
476 * @retval other The list of device instances of this driver.
478 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
480 struct drv_context *drvc;
483 sr_err("%s: Invalid argument.", __func__);
489 return drvc->instances;
493 * Standard driver scan() callback API helper.
495 * This function can be used to perform common tasks required by a driver's
496 * scan() callback. It will initialize the driver for each device on the list
497 * and add the devices on the list to the driver's device instance list.
498 * Usually it should be used as the last step in the scan() callback, right
501 * Note: This function can only be used if std_init() has been called
502 * previously by the driver.
506 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
508 * struct GSList *device;
509 * struct sr_dev_inst *sdi;
511 * sdi = g_new0(sr_dev_inst, 1);
514 * devices = g_slist_append(devices, sdi);
516 * return std_scan_complete(di, devices);
520 * @param[in] di The driver instance to use. Must not be NULL.
521 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
524 * @return The @p devices list.
526 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
528 struct drv_context *drvc;
532 sr_err("Invalid driver instance (di), cannot complete scan.");
538 for (l = devices; l; l = l->next) {
539 struct sr_dev_inst *sdi = l->data;
541 sr_err("Invalid device instance, cannot complete scan.");
547 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
552 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
553 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
554 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
555 size_t drvsize, const uint32_t devopts[], size_t devsize)
558 case SR_CONF_SCAN_OPTIONS:
559 /* Always return scanopts, regardless of sdi or cg. */
560 if (!scanopts || scanopts == NO_OPTS)
562 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
563 scanopts, scansize, sizeof(uint32_t));
565 case SR_CONF_DEVICE_OPTIONS:
567 /* sdi == NULL: return drvopts. */
568 if (!drvopts || drvopts == NO_OPTS)
570 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
571 drvopts, drvsize, sizeof(uint32_t));
572 } else if (sdi && !cg) {
573 /* sdi != NULL, cg == NULL: return devopts. */
574 if (!devopts || devopts == NO_OPTS)
576 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
577 devopts, devsize, sizeof(uint32_t));
580 * Note: sdi != NULL, cg != NULL is not handled by
581 * this function since it's very driver-specific.
583 sr_err("%s: %s: sdi/cg != NULL: not handling.",
584 sdi->driver->name, __func__);
595 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
598 GVariant *rational[2];
601 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
603 for (i = 0; i < n; i++) {
604 rational[0] = g_variant_new_uint64(a[i][0]);
605 rational[1] = g_variant_new_uint64(a[i][1]);
607 /* FIXME: Valgrind reports a memory leak here. */
608 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
611 return g_variant_builder_end(&gvb);
614 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, 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(r[i].p);
624 rational[1] = g_variant_new_uint64(r[i].q);
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 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
638 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
639 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
640 n, sizeof(uint64_t));
641 g_variant_builder_add(&gvb, "{sv}", str, gvar);
643 return g_variant_builder_end(&gvb);
646 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
648 return samplerate_helper(samplerates, n, "samplerates");
651 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
653 return samplerate_helper(samplerates, n, "samplerate-steps");
656 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
660 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
662 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
663 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
664 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
666 return g_variant_builder_end(&gvb);
669 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
674 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
676 for (i = 0; i < 3; i++)
677 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
679 return g_variant_builder_end(&gvb);
682 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
685 GVariant *gvar, *range[2];
688 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
690 for (d = min; d <= max; d += step) {
692 * We will never see exactly 0.0 because of the error we're
693 * accumulating, so catch the "zero" value and force it to be 0.
695 v = ((d > (-step / 2)) && (d < (step / 2))) ? 0 : d;
697 range[0] = g_variant_new_double(v);
698 range[1] = g_variant_new_double(v);
700 gvar = g_variant_new_tuple(range, 2);
701 g_variant_builder_add_value(&gvb, gvar);
704 return g_variant_builder_end(&gvb);
707 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
711 range[0] = g_variant_new_uint64(low);
712 range[1] = g_variant_new_uint64(high);
714 return g_variant_new_tuple(range, 2);
717 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
721 range[0] = g_variant_new_double(low);
722 range[1] = g_variant_new_double(high);
724 return g_variant_new_tuple(range, 2);
727 SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n)
729 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
730 a, n, sizeof(int32_t));
733 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n)
735 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
736 a, n, sizeof(uint32_t));
739 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n)
741 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
742 a, n, sizeof(uint64_t));
745 SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n)
748 GVariantBuilder *builder;
751 builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
753 for (i = 0; i < n; i++)
754 g_variant_builder_add(builder, "s", a[i]);
756 gvar = g_variant_new("as", builder);
757 g_variant_builder_unref(builder);
762 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
765 GVariant *gvar, *range[2];
768 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
770 for (i = 0; i < n; i++) {
771 range[0] = g_variant_new_double(a[i][0]);
772 range[1] = g_variant_new_double(a[i][1]);
773 gvar = g_variant_new_tuple(range, 2);
774 g_variant_builder_add_value(&gvb, gvar);
777 return g_variant_builder_end(&gvb);
780 /* Return the index of 'data' in the array 'arr' (or -1). */
781 static int find_in_array(GVariant *data, const GVariantType *type,
782 const void *arr, unsigned int n)
784 const char * const *sarr;
786 const uint64_t *u64arr;
787 const uint8_t *u8arr;
792 if (!g_variant_is_of_type(data, type))
795 switch (g_variant_classify(data)) {
796 case G_VARIANT_CLASS_STRING:
797 s = g_variant_get_string(data, NULL);
800 for (i = 0; i < n; i++)
801 if (!strcmp(s, sarr[i]))
804 case G_VARIANT_CLASS_UINT64:
805 u64 = g_variant_get_uint64(data);
808 for (i = 0; i < n; i++)
809 if (u64 == u64arr[i])
812 case G_VARIANT_CLASS_BYTE:
813 u8 = g_variant_get_byte(data);
816 for (i = 0; i < n; i++)
826 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
828 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
831 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
833 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
836 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
838 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
841 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
846 data = g_variant_new_string(s);
847 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
848 g_variant_unref(data);
853 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
858 data = g_variant_new_byte(b);
859 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
860 g_variant_unref(data);
865 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
870 g_variant_get(data, "(tt)", &low, &high);
872 for (i = 0; i < n; i++)
873 if (a[i][0] == low && a[i][1] == high)
879 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
884 g_variant_get(data, "(dd)", &low, &high);
886 for (i = 0; i < n; i++)
887 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
893 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
897 for (i = 0; i < n; i++)
904 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
908 for (i = 0; i < n; i++)
915 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
916 int baudrate, int bits, int parity, int stopbits,
917 int flowcontrol, int rts, int dtr)
931 SR_PRIV int std_dummy_set_handshake(struct sr_serial_dev_inst *serial,