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);
233 #ifdef HAVE_LIBSERIALPORT
236 * Standard serial driver dev_open() callback API helper.
238 * This function can be used to implement the dev_open() driver API
239 * callback in drivers that use a serial port. The port is opened
240 * with the SERIAL_RDWR flag.
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 Serial port open failed.
248 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
250 struct sr_serial_dev_inst *serial;
253 sr_err("%s: Invalid argument.", __func__);
259 return serial_open(serial, SERIAL_RDWR);
263 * Standard serial driver dev_close() callback API helper.
265 * This function can be used to implement the dev_close() driver API
266 * callback in drivers that use a serial port.
268 * @param[in] sdi The device instance to use. Must not be NULL.
270 * @retval SR_OK Success.
271 * @retval SR_ERR_ARG Invalid argument.
272 * @retval other Serial port close failed.
274 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
276 struct sr_serial_dev_inst *serial;
279 sr_err("%s: Invalid argument.", __func__);
285 return serial_close(serial);
289 * Standard serial driver dev_acquisition_stop() callback API helper.
291 * This function can be used to simplify most (serial port based) drivers'
292 * dev_acquisition_stop() API callback.
294 * @param[in] sdi The device instance for which acquisition should stop.
297 * @retval SR_OK Success.
298 * @retval SR_ERR_ARG Invalid argument.
299 * @retval other Other error.
301 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
303 struct sr_serial_dev_inst *serial;
308 sr_err("%s: Invalid argument.", __func__);
313 prefix = sdi->driver->name;
315 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
316 sr_err("%s: Failed to remove source: %d.", prefix, ret);
320 if ((ret = sr_dev_close(sdi)) < 0) {
321 sr_err("%s: Failed to close device: %d.", prefix, ret);
325 return std_session_send_df_end(sdi);
331 * Standard driver dev_clear() callback API helper.
333 * Clear driver, this means, close all instances.
335 * This function can be used to implement the dev_clear() driver API
336 * callback. dev_close() is called before every sr_dev_inst is cleared.
338 * The only limitation is driver-specific device contexts (sdi->priv / devc).
339 * These are freed, but any dynamic allocation within structs stored
340 * there cannot be freed.
342 * @param[in] driver The driver which will have its instances released.
344 * @param[in] clear_private If not NULL, this points to a function called
345 * with sdi->priv (devc) as argument. The function can then clear
346 * any device instance-specific resources kept there.
347 * It must NOT clear the struct pointed to by sdi->priv (devc),
348 * since this function will always free it after clear_private()
351 * @retval SR_OK Success.
352 * @retval SR_ERR_ARG Invalid argument.
353 * @retval SR_ERR_BUG Implementation bug.
354 * @retval other Other error.
356 SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
357 std_dev_clear_callback clear_private)
359 struct drv_context *drvc;
360 struct sr_dev_inst *sdi;
365 sr_err("%s: Invalid argument.", __func__);
369 drvc = driver->context; /* Caller checked for context != NULL. */
372 for (l = drvc->instances; l; l = l->next) {
373 if (!(sdi = l->data)) {
374 sr_err("%s: Invalid device instance.", __func__);
378 if (driver->dev_close)
379 driver->dev_close(sdi);
382 #ifdef HAVE_LIBSERIALPORT
383 if (sdi->inst_type == SR_INST_SERIAL)
384 sr_serial_dev_inst_free(sdi->conn);
386 #ifdef HAVE_LIBUSB_1_0
387 if (sdi->inst_type == SR_INST_USB)
388 sr_usb_dev_inst_free(sdi->conn);
390 if (sdi->inst_type == SR_INST_SCPI)
391 sr_scpi_free(sdi->conn);
392 if (sdi->inst_type == SR_INST_MODBUS)
393 sr_modbus_free(sdi->conn);
396 /* Clear driver-specific stuff, if any. */
398 clear_private(sdi->priv);
400 /* Clear sdi->priv (devc). */
403 sr_dev_inst_free(sdi);
406 g_slist_free(drvc->instances);
407 drvc->instances = NULL;
412 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
414 return std_dev_clear_with_callback(driver, NULL);
418 * Standard driver dev_list() callback API helper.
420 * This function can be used as the dev_list() callback by most drivers.
422 * Return the devices contained in the driver context instances list.
424 * @param[in] di The driver instance to use. Must not be NULL.
426 * @retval NULL Error, or the list is empty.
427 * @retval other The list of device instances of this driver.
429 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
431 struct drv_context *drvc;
434 sr_err("%s: Invalid argument.", __func__);
440 return drvc->instances;
444 * Standard driver scan() callback API helper.
446 * This function can be used to perform common tasks required by a driver's
447 * scan() callback. It will initialize the driver for each device on the list
448 * and add the devices on the list to the driver's device instance list.
449 * Usually it should be used as the last step in the scan() callback, right
452 * Note: This function can only be used if std_init() has been called
453 * previously by the driver.
457 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
459 * struct GSList *device;
460 * struct sr_dev_inst *sdi;
462 * sdi = g_new0(sr_dev_inst, 1);
465 * devices = g_slist_append(devices, sdi);
467 * return std_scan_complete(di, devices);
471 * @param[in] di The driver instance to use. Must not be NULL.
472 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
475 * @return The @p devices list.
477 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
479 struct drv_context *drvc;
483 sr_err("Invalid driver instance (di), cannot complete scan.");
489 for (l = devices; l; l = l->next) {
490 struct sr_dev_inst *sdi = l->data;
492 sr_err("Invalid device instance, cannot complete scan.");
498 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
503 SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
504 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
505 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
506 size_t drvsize, const uint32_t devopts[], size_t devsize)
509 case SR_CONF_SCAN_OPTIONS:
510 /* Always return scanopts, regardless of sdi or cg. */
513 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
514 scanopts, scansize, sizeof(uint32_t));
516 case SR_CONF_DEVICE_OPTIONS:
518 /* sdi == NULL: return drvopts. */
521 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
522 drvopts, drvsize, sizeof(uint32_t));
523 } else if (sdi && !cg) {
524 /* sdi != NULL, cg == NULL: return devopts. */
527 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
528 devopts, devsize, sizeof(uint32_t));
531 * Note: sdi != NULL, cg != NULL is not handled by
532 * this function since it's very driver-specific.
534 sr_err("%s: %s: sdi/cg != NULL: not handling.",
535 sdi->driver->name, __func__);
546 SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
549 GVariant *rational[2];
552 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
554 for (i = 0; i < n; i++) {
555 rational[0] = g_variant_new_uint64(a[i][0]);
556 rational[1] = g_variant_new_uint64(a[i][1]);
558 /* FIXME: Valgrind reports a memory leak here. */
559 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
562 return g_variant_builder_end(&gvb);
565 SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
568 GVariant *rational[2];
571 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
573 for (i = 0; i < n; i++) {
574 rational[0] = g_variant_new_uint64(r[i].p);
575 rational[1] = g_variant_new_uint64(r[i].q);
577 /* FIXME: Valgrind reports a memory leak here. */
578 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
581 return g_variant_builder_end(&gvb);
584 static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
589 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
590 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
591 n, sizeof(uint64_t));
592 g_variant_builder_add(&gvb, "{sv}", str, gvar);
594 return g_variant_builder_end(&gvb);
597 SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
599 return samplerate_helper(samplerates, n, "samplerates");
602 SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
604 return samplerate_helper(samplerates, n, "samplerate-steps");
607 SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
611 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
613 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
614 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
615 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
617 return g_variant_builder_end(&gvb);
620 SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
625 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
627 for (i = 0; i < 3; i++)
628 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
630 return g_variant_builder_end(&gvb);
633 SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
636 GVariant *gvar, *range[2];
639 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
641 for (d = min; d <= max; d += step) {
642 range[0] = g_variant_new_double(d);
643 range[1] = g_variant_new_double(d);
645 gvar = g_variant_new_tuple(range, 2);
646 g_variant_builder_add_value(&gvb, gvar);
649 return g_variant_builder_end(&gvb);
652 SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
656 range[0] = g_variant_new_uint64(low);
657 range[1] = g_variant_new_uint64(high);
659 return g_variant_new_tuple(range, 2);
662 SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
666 range[0] = g_variant_new_double(low);
667 range[1] = g_variant_new_double(high);
669 return g_variant_new_tuple(range, 2);
672 SR_PRIV GVariant *std_gvar_array_i32(const int32_t *a, unsigned int n)
674 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
675 a, n, sizeof(int32_t));
678 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t *a, unsigned int n)
680 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
681 a, n, sizeof(uint32_t));
684 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t *a, unsigned int n)
686 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
687 a, n, sizeof(uint64_t));
690 SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
693 GVariant *gvar, *range[2];
696 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
698 for (i = 0; i < n; i++) {
699 range[0] = g_variant_new_double(a[i][0]);
700 range[1] = g_variant_new_double(a[i][1]);
701 gvar = g_variant_new_tuple(range, 2);
702 g_variant_builder_add_value(&gvb, gvar);
705 return g_variant_builder_end(&gvb);
708 /* Return the index of 'data' in the array 'arr' (or -1). */
709 static int find_in_array(GVariant *data, const GVariantType *type,
710 const void *arr, unsigned int n)
712 const char * const *sarr;
714 const uint64_t *u64arr;
715 const uint8_t *u8arr;
720 if (!g_variant_is_of_type(data, type))
723 switch (g_variant_classify(data)) {
724 case G_VARIANT_CLASS_STRING:
725 s = g_variant_get_string(data, NULL);
728 for (i = 0; i < n; i++)
729 if (!strcmp(s, sarr[i]))
732 case G_VARIANT_CLASS_UINT64:
733 u64 = g_variant_get_uint64(data);
736 for (i = 0; i < n; i++)
737 if (u64 == u64arr[i])
740 case G_VARIANT_CLASS_BYTE:
741 u8 = g_variant_get_byte(data);
744 for (i = 0; i < n; i++)
754 SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
756 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
759 SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
761 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
764 SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
766 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
769 SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
774 data = g_variant_new_string(s);
775 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
776 g_variant_unref(data);
781 SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
786 data = g_variant_new_byte(b);
787 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
788 g_variant_unref(data);
793 SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
798 g_variant_get(data, "(tt)", &low, &high);
800 for (i = 0; i < n; i++)
801 if (a[i][0] == low && a[i][1] == high)
807 SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
812 g_variant_get(data, "(dd)", &low, &high);
814 for (i = 0; i < n; i++)
815 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
821 SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
825 for (i = 0; i < n; i++)
832 SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
836 for (i = 0; i < n; i++)