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.
32 #include <libsigrok/libsigrok.h>
33 #include "libsigrok-internal.h"
36 #define LOG_PREFIX "std"
39 * Standard driver init() callback API helper.
41 * This function can be used to simplify most driver's init() API callback.
43 * Create a new 'struct drv_context' (drvc), assign sr_ctx to it, and
44 * then assign 'drvc' to the 'struct sr_dev_driver' (di) that is passed.
46 * @param[in] di The driver instance to use. Must not be NULL.
47 * @param[in] sr_ctx The libsigrok context to assign. May be NULL.
49 * @retval SR_OK Success.
50 * @retval SR_ERR_ARG Invalid argument.
52 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
54 struct drv_context *drvc;
57 sr_err("%s: Invalid argument.", __func__);
61 drvc = g_malloc0(sizeof(struct drv_context));
62 drvc->sr_ctx = sr_ctx;
63 drvc->instances = NULL;
70 * Standard driver cleanup() callback API helper.
72 * This function can be used to simplify most driver's cleanup() API callback.
74 * Free all device instances by calling sr_dev_clear() and then release any
75 * resources allocated by std_init().
77 * @param[in] di The driver instance to use. Must not be NULL.
79 * @retval SR_OK Success.
80 * @retval SR_ERR_ARG Invalid argument.
81 * @retval other Other error.
83 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
88 sr_err("%s: Invalid argument.", __func__);
92 ret = sr_dev_clear(di);
99 * Dummmy driver dev_open() callback API helper.
101 * @param[in] sdi The device instance to use. May be NULL (unused).
103 * @retval SR_OK Success.
105 SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi)
113 * Dummmy driver dev_close() callback API helper.
115 * @param[in] sdi The device instance to use. May be NULL (unused).
117 * @retval SR_OK Success.
119 SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi)
127 * Dummmy driver dev_acquisition_start() callback API helper.
129 * @param[in] sdi The device instance to use. May be NULL (unused).
131 * @retval SR_OK Success.
133 SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi)
141 * Dummmy driver dev_acquisition_stop() callback API helper.
143 * @param[in] sdi The device instance to use. May be NULL (unused).
145 * @retval SR_OK Success.
147 SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi)
155 * Standard API helper for sending an SR_DF_HEADER packet.
157 * This function can be used to simplify most drivers'
158 * dev_acquisition_start() API callback.
160 * @param[in] sdi The device instance to use. Must not be NULL.
162 * @retval SR_OK Success.
163 * @retval SR_ERR_ARG Invalid argument.
164 * @retval other Other error.
166 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
170 struct sr_datafeed_packet packet;
171 struct sr_datafeed_header header;
174 sr_err("%s: Invalid argument.", __func__);
178 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
180 /* Send header packet to the session bus. */
181 sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
182 packet.type = SR_DF_HEADER;
183 packet.payload = (uint8_t *)&header;
184 header.feed_version = 1;
185 gettimeofday(&header.starttime, NULL);
187 if ((ret = sr_session_send(sdi, &packet)) < 0) {
188 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
196 * Standard API helper for sending an SR_DF_END packet.
198 * This function can be used to simplify most drivers'
199 * dev_acquisition_stop() API callback.
201 * @param[in] sdi The device instance to use. Must not be NULL.
203 * @retval SR_OK Success.
204 * @retval SR_ERR_ARG Invalid argument.
205 * @retval other Other error.
207 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
211 struct sr_datafeed_packet packet;
214 sr_err("%s: Invalid argument.", __func__);
218 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
220 sr_dbg("%s: Sending SR_DF_END packet.", prefix);
222 packet.type = SR_DF_END;
223 packet.payload = NULL;
225 if ((ret = sr_session_send(sdi, &packet)) < 0) {
226 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
234 * Standard API helper for sending an SR_DF_FRAME_BEGIN packet.
236 * This function can be used to simplify most drivers'
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_frame_begin(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 sr_dbg("%s: Sending SR_DF_FRAME_BEGIN packet.", prefix);
260 packet.type = SR_DF_FRAME_BEGIN;
261 packet.payload = NULL;
263 if ((ret = sr_session_send(sdi, &packet)) < 0) {
264 sr_err("%s: Failed to send SR_DF_FRAME_BEGIN packet: %d.", prefix, ret);
272 * Standard API helper for sending an SR_DF_FRAME_END packet.
274 * This function can be used to simplify most drivers'
277 * @param[in] sdi The device instance to use. Must not be NULL.
279 * @retval SR_OK Success.
280 * @retval SR_ERR_ARG Invalid argument.
281 * @retval other Other error.
283 SR_PRIV int std_session_send_frame_end(const struct sr_dev_inst *sdi)
287 struct sr_datafeed_packet packet;
290 sr_err("%s: Invalid argument.", __func__);
294 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
296 sr_dbg("%s: Sending SR_DF_FRAME_END packet.", prefix);
298 packet.type = SR_DF_FRAME_END;
299 packet.payload = NULL;
301 if ((ret = sr_session_send(sdi, &packet)) < 0) {
302 sr_err("%s: Failed to send SR_DF_FRAME_END packet: %d.", prefix, ret);
309 #ifdef HAVE_LIBSERIALPORT
312 * Standard serial driver dev_open() callback API helper.
314 * This function can be used to implement the dev_open() driver API
315 * callback in drivers that use a serial port. The port is opened
316 * with the SERIAL_RDWR flag.
318 * @param[in] sdi The device instance to use. Must not be NULL.
320 * @retval SR_OK Success.
321 * @retval SR_ERR_ARG Invalid argument.
322 * @retval other Serial port open failed.
324 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
326 struct sr_serial_dev_inst *serial;
329 sr_err("%s: Invalid argument.", __func__);
335 return serial_open(serial, SERIAL_RDWR);
339 * Standard serial driver dev_close() callback API helper.
341 * This function can be used to implement the dev_close() driver API
342 * callback in drivers that use a serial port.
344 * @param[in] sdi The device instance to use. Must not be NULL.
346 * @retval SR_OK Success.
347 * @retval SR_ERR_ARG Invalid argument.
348 * @retval other Serial port close failed.
350 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
352 struct sr_serial_dev_inst *serial;
355 sr_err("%s: Invalid argument.", __func__);
361 return serial_close(serial);
365 * Standard serial driver dev_acquisition_stop() callback API helper.
367 * This function can be used to simplify most (serial port based) drivers'
368 * dev_acquisition_stop() API callback.
370 * @param[in] sdi The device instance for which acquisition should stop.
373 * @retval SR_OK Success.
374 * @retval SR_ERR_ARG Invalid argument.
375 * @retval other Other error.
377 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
379 struct sr_serial_dev_inst *serial;
384 sr_err("%s: Invalid argument.", __func__);
389 prefix = sdi->driver->name;
391 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
392 sr_err("%s: Failed to remove source: %d.", prefix, ret);
396 if ((ret = sr_dev_close(sdi)) < 0) {
397 sr_err("%s: Failed to close device: %d.", prefix, ret);
401 return std_session_send_df_end(sdi);
407 * Standard driver dev_clear() callback API helper.
409 * Clear driver, this means, close all instances.
411 * This function can be used to implement the dev_clear() driver API
412 * callback. dev_close() is called before every sr_dev_inst is cleared.
414 * The only limitation is driver-specific device contexts (sdi->priv / devc).
415 * These are freed, but any dynamic allocation within structs stored
416 * there cannot be freed.
418 * @param[in] driver The driver which will have its instances released.
420 * @param[in] clear_private If not NULL, this points to a function called
421 * with sdi->priv (devc) as argument. The function can then clear
422 * any device instance-specific resources kept there.
423 * It must NOT clear the struct pointed to by sdi->priv (devc),
424 * since this function will always free it after clear_private()
427 * @retval SR_OK Success.
428 * @retval SR_ERR_ARG Invalid argument.
429 * @retval SR_ERR_BUG Implementation bug.
430 * @retval other Other error.
432 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
433 std_dev_clear_callback clear_private)
435 struct drv_context *drvc;
436 struct sr_dev_inst *sdi;
441 sr_err("%s: Invalid argument.", __func__);
445 drvc = driver->context; /* Caller checked for context != NULL. */
448 for (l = drvc->instances; l; l = l->next) {
449 if (!(sdi = l->data)) {
450 sr_err("%s: Invalid device instance.", __func__);
454 if (driver->dev_close)
455 driver->dev_close(sdi);
458 #ifdef HAVE_LIBSERIALPORT
459 if (sdi->inst_type == SR_INST_SERIAL)
460 sr_serial_dev_inst_free(sdi->conn);
462 #ifdef HAVE_LIBUSB_1_0
463 if (sdi->inst_type == SR_INST_USB)
464 sr_usb_dev_inst_free(sdi->conn);
466 if (sdi->inst_type == SR_INST_SCPI)
467 sr_scpi_free(sdi->conn);
468 if (sdi->inst_type == SR_INST_MODBUS)
469 sr_modbus_free(sdi->conn);
472 /* Clear driver-specific stuff, if any. */
474 clear_private(sdi->priv);
476 /* Clear sdi->priv (devc). */
479 sr_dev_inst_free(sdi);
482 g_slist_free(drvc->instances);
483 drvc->instances = NULL;
488 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
490 return std_dev_clear_with_callback(driver, NULL);
494 * Standard driver dev_list() callback API helper.
496 * This function can be used as the dev_list() callback by most drivers.
498 * Return the devices contained in the driver context instances list.
500 * @param[in] di The driver instance to use. Must not be NULL.
502 * @retval NULL Error, or the list is empty.
503 * @retval other The list of device instances of this driver.
505 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
507 struct drv_context *drvc;
510 sr_err("%s: Invalid argument.", __func__);
516 return drvc->instances;
520 * Standard driver scan() callback API helper.
522 * This function can be used to perform common tasks required by a driver's
523 * scan() callback. It will initialize the driver for each device on the list
524 * and add the devices on the list to the driver's device instance list.
525 * Usually it should be used as the last step in the scan() callback, right
528 * Note: This function can only be used if std_init() has been called
529 * previously by the driver.
533 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
535 * struct GSList *device;
536 * struct sr_dev_inst *sdi;
538 * sdi = g_new0(sr_dev_inst, 1);
541 * devices = g_slist_append(devices, sdi);
543 * return std_scan_complete(di, devices);
547 * @param[in] di The driver instance to use. Must not be NULL.
548 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
551 * @return The @p devices list.
553 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
555 struct drv_context *drvc;
559 sr_err("Invalid driver instance (di), cannot complete scan.");
565 for (l = devices; l; l = l->next) {
566 struct sr_dev_inst *sdi = l->data;
568 sr_err("Invalid device instance, cannot complete scan.");
574 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
579 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
580 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
581 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
582 size_t drvsize, const uint32_t devopts[], size_t devsize)
585 case SR_CONF_SCAN_OPTIONS:
586 /* Always return scanopts, regardless of sdi or cg. */
589 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
590 scanopts, scansize, sizeof(uint32_t));
592 case SR_CONF_DEVICE_OPTIONS:
594 /* sdi == NULL: return drvopts. */
597 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
598 drvopts, drvsize, sizeof(uint32_t));
599 } else if (sdi && !cg) {
600 /* sdi != NULL, cg == NULL: return devopts. */
603 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
604 devopts, devsize, sizeof(uint32_t));
607 * Note: sdi != NULL, cg != NULL is not handled by
608 * this function since it's very driver-specific.
610 sr_err("%s: %s: sdi/cg != NULL: not handling.",
611 sdi->driver->name, __func__);
622 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
625 GVariant *rational[2];
628 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
630 for (i = 0; i < n; i++) {
631 rational[0] = g_variant_new_uint64(a[i][0]);
632 rational[1] = g_variant_new_uint64(a[i][1]);
634 /* FIXME: Valgrind reports a memory leak here. */
635 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
638 return g_variant_builder_end(&gvb);
641 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
644 GVariant *rational[2];
647 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
649 for (i = 0; i < n; i++) {
650 rational[0] = g_variant_new_uint64(r[i].p);
651 rational[1] = g_variant_new_uint64(r[i].q);
653 /* FIXME: Valgrind reports a memory leak here. */
654 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
657 return g_variant_builder_end(&gvb);
660 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
665 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
666 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
667 n, sizeof(uint64_t));
668 g_variant_builder_add(&gvb, "{sv}", str, gvar);
670 return g_variant_builder_end(&gvb);
673 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
675 return samplerate_helper(samplerates, n, "samplerates");
678 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
680 return samplerate_helper(samplerates, n, "samplerate-steps");
683 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
687 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
689 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
690 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
691 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
693 return g_variant_builder_end(&gvb);
696 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
701 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
703 for (i = 0; i < 3; i++)
704 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
706 return g_variant_builder_end(&gvb);
709 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
712 GVariant *gvar, *range[2];
715 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
717 for (d = min; d <= max; d += step) {
718 range[0] = g_variant_new_double(d);
719 range[1] = g_variant_new_double(d);
721 gvar = g_variant_new_tuple(range, 2);
722 g_variant_builder_add_value(&gvb, gvar);
725 return g_variant_builder_end(&gvb);
728 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
732 range[0] = g_variant_new_uint64(low);
733 range[1] = g_variant_new_uint64(high);
735 return g_variant_new_tuple(range, 2);
738 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
742 range[0] = g_variant_new_double(low);
743 range[1] = g_variant_new_double(high);
745 return g_variant_new_tuple(range, 2);
748 SR_PRIV GVariant *std_gvar_array_i32(const int32_t *a, unsigned int n)
750 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
751 a, n, sizeof(int32_t));
754 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t *a, unsigned int n)
756 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
757 a, n, sizeof(uint32_t));
760 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t *a, unsigned int n)
762 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
763 a, n, sizeof(uint64_t));
766 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
769 GVariant *gvar, *range[2];
772 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
774 for (i = 0; i < n; i++) {
775 range[0] = g_variant_new_double(a[i][0]);
776 range[1] = g_variant_new_double(a[i][1]);
777 gvar = g_variant_new_tuple(range, 2);
778 g_variant_builder_add_value(&gvb, gvar);
781 return g_variant_builder_end(&gvb);
784 /* Return the index of 'data' in the array 'arr' (or -1). */
785 static int find_in_array(GVariant *data, const GVariantType *type,
786 const void *arr, unsigned int n)
788 const char * const *sarr;
790 const uint64_t *u64arr;
791 const uint8_t *u8arr;
796 if (!g_variant_is_of_type(data, type))
799 switch (g_variant_classify(data)) {
800 case G_VARIANT_CLASS_STRING:
801 s = g_variant_get_string(data, NULL);
804 for (i = 0; i < n; i++)
805 if (!strcmp(s, sarr[i]))
808 case G_VARIANT_CLASS_UINT64:
809 u64 = g_variant_get_uint64(data);
812 for (i = 0; i < n; i++)
813 if (u64 == u64arr[i])
816 case G_VARIANT_CLASS_BYTE:
817 u8 = g_variant_get_byte(data);
820 for (i = 0; i < n; i++)
830 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
832 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
835 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
837 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
840 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
842 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
845 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
850 data = g_variant_new_string(s);
851 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
852 g_variant_unref(data);
857 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
862 data = g_variant_new_byte(b);
863 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
864 g_variant_unref(data);
869 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
874 g_variant_get(data, "(tt)", &low, &high);
876 for (i = 0; i < n; i++)
877 if (a[i][0] == low && a[i][1] == high)
883 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
888 g_variant_get(data, "(dd)", &low, &high);
890 for (i = 0; i < n; i++)
891 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
897 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
901 for (i = 0; i < n; i++)
908 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
912 for (i = 0; i < n; i++)