23 #include <sys/types.h>
28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "hwdriver"
71 "Serial communication", NULL},
73 "Modbus slave address", NULL},
79 "Pre-trigger capture ratio", NULL},
83 "Run length encoding", NULL},
85 "Trigger slope", NULL},
89 "Number of samples to average over", NULL},
91 "Trigger source", NULL},
93 "Horizontal trigger position", NULL},
105 "Trigger matches", NULL},
107 "Sample interval", NULL},
109 "Number of horizontal divisions", NULL},
111 "Number of vertical divisions", NULL},
113 "Sound pressure level frequency weighting", NULL},
115 "Sound pressure level time weighting", NULL},
117 "Sound pressure level measurement range", NULL},
123 "Voltage threshold", NULL },
125 "External clock mode", NULL},
127 "Swap channel order", NULL},
129 "Center frequency", NULL},
131 "Number of logic channels", NULL},
133 "Number of analog channels", NULL},
135 "Current voltage", NULL},
137 "Voltage target", NULL},
139 "Current current", NULL},
141 "Current limit", NULL},
143 "Channel enabled", NULL},
145 "Channel modes", NULL},
147 "Over-voltage protection enabled", NULL},
149 "Over-voltage protection active", NULL},
151 "Over-voltage protection threshold", NULL},
153 "Over-current protection enabled", NULL},
155 "Over-current protection active", NULL},
157 "Over-current protection threshold", NULL},
163 "Channel regulation", NULL},
165 "Over-temperature protection", NULL},
167 "Output frequency", NULL},
169 "Output frequency target", NULL},
171 "Measured quantity", NULL},
173 "Equivalent circuit model", NULL},
175 "Over-temperature protection active", NULL},
177 "Under-voltage condition", NULL},
179 "Under-voltage condition active", NULL},
181 "Under-voltage condition threshold", NULL},
183 "Trigger level", NULL},
185 "External clock source", NULL},
189 "Trigger pattern", NULL},
191 "High resolution", NULL},
193 "Peak detection", NULL},
195 "Logic threshold (predefined)", NULL},
197 "Logic threshold (custom)", NULL},
205 "Session file", NULL},
207 "Capture file", NULL},
209 "Capture unitsize", NULL},
213 "Data source", NULL},
215 "Probe factor", NULL},
217 "Number of ADC powerline cycles", NULL},
223 "Sample limit", NULL},
225 "Frame limit", NULL},
227 "Continuous sampling", NULL},
231 "Device mode", NULL},
288 "Frequency weighted (A)", NULL},
290 "Frequency weighted (C)", NULL},
292 "Frequency weighted (Z)", NULL},
294 "Frequency weighted (flat)", NULL},
296 "Time weighted (S)", NULL},
298 "Time weighted (F)", NULL},
301 "Percentage over alarm", NULL},
316 return G_VARIANT_TYPE_INT32;
318 return G_VARIANT_TYPE_UINT64;
320 return G_VARIANT_TYPE_STRING;
322 return G_VARIANT_TYPE_BOOLEAN;
324 return G_VARIANT_TYPE_DOUBLE;
329 return G_VARIANT_TYPE_TUPLE;
331 return G_VARIANT_TYPE_DICTIONARY;
333 return G_VARIANT_TYPE_TUPLE;
340 SR_PRIV int sr_variant_type_check(uint32_t
key, GVariant *value)
343 const GVariantType *type, *expected;
344 char *expected_string, *type_string;
350 expected = sr_variant_type_get(info->
datatype);
351 type = g_variant_get_type(value);
352 if (!g_variant_type_equal(type, expected)
353 && !g_variant_type_is_subtype_of(type, expected)) {
354 expected_string = g_variant_type_dup_string(expected);
355 type_string = g_variant_type_dup_string(type);
356 sr_err(
"Wrong variant type for key '%s': expected '%s', got '%s'",
357 info->
name, expected_string, type_string);
358 g_free(expected_string);
382 return ctx->driver_list;
409 sr_err(
"Invalid libsigrok context, can't initialize.");
414 sr_err(
"Invalid driver, can't initialize.");
420 if ((ret = driver->
init(driver, ctx)) < 0)
421 sr_err(
"Failed to initialize the driver: %d.", ret);
445 const uint32_t *opts;
452 opts = g_variant_get_fixed_array(gvar, &num_opts,
sizeof(uint32_t));
454 result = g_array_sized_new(FALSE, FALSE,
sizeof(uint32_t), num_opts);
456 g_array_insert_vals(result, 0, opts, num_opts);
458 g_variant_unref(gvar);
463 static int check_options(
struct sr_dev_driver *driver, GSList *options,
464 uint32_t optlist_key,
struct sr_dev_inst *sdi,
471 const uint32_t *opts;
481 opts = g_variant_get_fixed_array(gvar_opts, &num_opts,
sizeof(uint32_t));
482 for (l = options; l; l = l->next) {
484 for (i = 0; i < num_opts; i++) {
485 if (opts[i] == src->
key)
491 sr_err(
"Invalid option %d.", src->
key);
493 sr_err(
"Invalid option '%s'.", srci->
id);
497 if (sr_variant_type_check(src->
key, src->
data) !=
SR_OK) {
502 g_variant_unref(gvar_opts);
537 sr_err(
"Invalid driver, can't scan for devices.");
542 sr_err(
"Driver not initialized, can't scan for devices.");
547 if (check_options(driver, options, SR_CONF_SCAN_OPTIONS, NULL, NULL) !=
SR_OK)
551 l = driver->
scan(driver, options);
553 sr_spew(
"Scan found %d devices (%s).", g_slist_length(l), driver->
name);
573 sr_dbg(
"Cleaning up all drivers.");
576 for (i = 0; drivers[i]; i++) {
578 drivers[i]->
cleanup(drivers[i]);
600 src = g_malloc0(
sizeof(
struct sr_config));
602 src->
data = g_variant_ref_sink(data);
614 if (!src || !src->
data) {
615 sr_err(
"%s: invalid data!", __func__);
619 g_variant_unref(src->
data);
624 SR_PRIV int sr_dev_acquisition_start(
struct sr_dev_inst *sdi)
626 if (!sdi || !sdi->driver) {
627 sr_err(
"%s: Invalid arguments.", __func__);
632 sr_err(
"%s: Device instance not active, can't start.",
637 sr_dbg(
"%s: Starting acquisition.", sdi->driver->name);
639 return sdi->driver->dev_acquisition_start(sdi);
643 SR_PRIV int sr_dev_acquisition_stop(
struct sr_dev_inst *sdi)
645 if (!sdi || !sdi->driver) {
646 sr_err(
"%s: Invalid arguments.", __func__);
651 sr_err(
"%s: Device instance not active, can't stop.",
656 sr_dbg(
"%s: Stopping acquisition.", sdi->driver->name);
658 return sdi->driver->dev_acquisition_stop(sdi);
661 static void log_key(
const struct sr_dev_inst *sdi,
670 if (key == SR_CONF_DEVICE_OPTIONS)
676 tmp_str = g_variant_print(data, TRUE);
677 sr_spew(
"sr_config_%s(): key %d (%s) sdi %p cg %s -> %s", opstr, key,
678 srci ? srci->
id :
"NULL", sdi, cg ? cg->
name :
"NULL",
679 data ? tmp_str :
"NULL");
685 uint32_t key,
unsigned int op, GVariant *data)
690 const uint32_t *opts;
696 suffix =
" for this device instance and channel group";
698 suffix =
" for this device instance";
703 sr_err(
"Invalid key %d.", key);
715 if (g_variant_get_uint64(data) == 0) {
716 sr_err(
"Cannot set '%s' to 0.", srci->
id);
724 if (g_variant_get_uint64(data) > 100) {
725 sr_err(
"Capture ratio must be 0..100.");
733 sr_err(
"No options available%s.", suffix);
736 opts = g_variant_get_fixed_array(gvar_opts, &num_opts,
sizeof(uint32_t));
738 for (i = 0; i < num_opts; i++) {
739 if ((opts[i] & SR_CONF_MASK) == key) {
744 g_variant_unref(gvar_opts);
746 sr_err(
"Option '%s' not available%s.", srci->
id, suffix);
750 if (!(pub_opt & op)) {
751 sr_err(
"Option '%s' not available to %s%s.", srci->
id, opstr, suffix);
784 const struct sr_dev_inst *sdi,
786 uint32_t key, GVariant **data)
790 if (!driver || !data)
799 if (sdi && !sdi->priv) {
800 sr_err(
"Can't get config (sdi != NULL, sdi->priv == NULL).");
808 g_variant_ref_sink(*data);
812 sr_err(
"%s: No channel group specified.",
813 (sdi) ? sdi->driver->name :
"unknown");
840 uint32_t key, GVariant *data)
844 g_variant_ref_sink(data);
846 if (!sdi || !sdi->driver || !sdi->priv || !data)
848 else if (!sdi->driver->config_set)
851 sr_err(
"%s: Device instance not active, can't set config.",
854 }
else if (check_key(sdi->driver, sdi, cg, key,
SR_CONF_SET, data) !=
SR_OK)
856 else if ((ret = sr_variant_type_check(key, data)) ==
SR_OK) {
858 ret = sdi->driver->config_set(key, data, sdi, cg);
861 g_variant_unref(data);
864 sr_err(
"%s: No channel group specified.",
865 (sdi) ? sdi->driver->name :
"unknown");
883 if (!sdi || !sdi->driver)
885 else if (!sdi->driver->config_commit)
888 sr_err(
"%s: Device instance not active, can't commit config.",
892 ret = sdi->driver->config_commit(sdi);
927 const struct sr_dev_inst *sdi,
929 uint32_t key, GVariant **data)
933 if (!driver || !data)
939 if (key != SR_CONF_SCAN_OPTIONS && key != SR_CONF_DEVICE_OPTIONS) {
944 if (sdi && !sdi->priv) {
945 sr_err(
"Can't list config (sdi != NULL, sdi->priv == NULL).");
949 if (key != SR_CONF_SCAN_OPTIONS && key != SR_CONF_DEVICE_OPTIONS && !sdi) {
950 sr_err(
"Config keys other than SR_CONF_SCAN_OPTIONS and "
951 "SR_CONF_DEVICE_OPTIONS always need an sdi.");
955 if (cg && sdi && !sdi->channel_groups) {
956 sr_err(
"Can't list config for channel group, there are none.");
960 if (cg && sdi && !g_slist_find(sdi->channel_groups, cg)) {
961 sr_err(
"If a channel group is specified, it must be a valid one.");
966 sr_err(
"Need sdi when a channel group is specified.");
972 g_variant_ref_sink(*data);
976 sr_err(
"%s: No channel group specified.",
977 (sdi) ? sdi->driver->name :
"unknown");
982 static struct sr_key_info *get_keytable(
int keytype)
988 table = sr_key_info_config;
991 table = sr_key_info_mq;
994 table = sr_key_info_mqflag;
997 sr_err(
"Invalid keytype %d", keytype);
1020 if (!(table = get_keytable(keytype)))
1023 for (i = 0; table[i].
key; i++) {
1024 if (table[i].key == key)
1047 if (!(table = get_keytable(keytype)))
1050 for (i = 0; table[i].
key; i++) {
1053 if (!strcmp(table[i].
id, keyid))
Generic/unspecified error.
Output frequency target in Hz.
Time is duration (as opposed to epoch, ...).
Sound pressure level is not weighted in the frequency domain, albeit without standards-defined low an...
int sr_config_list(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, uint32_t key, GVariant **data)
List all possible values for a configuration key.
const char * name
Full capitalized name, e.g.
Sound pressure level is A-weighted in the frequency domain, according to IEC 61672:2003.
The device can measure temperature.
The device supports setting the number of logic channels.
The number of digits (e.g.
The device supports setting the number of analog channels.
The device supports setting a sample number limit (how many samples should be acquired).
The device supports run-length encoding (RLE).
Measurement is four wire (e.g.
The device can act as an oscilloscope.
int(* config_list)(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
List all possible values for a configuration key in a device instance.
This is a true RMS measurement.
The device can act as an LCR meter.
Under-voltage condition active.
Series capacitance (LCR meter model).
Equivalent circuit model.
Channel regulation get: "CV", "CC" or "UR", denoting constant voltage, constant current or unregulate...
Sound pressure level measurement is S-weighted (1s) in the time domain.
The device supports continuous sampling.
Number of vertical divisions, as related to SR_CONF_VDIV.
int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver)
Initialize a hardware driver.
Sound pressure level measurement is F-weighted (125ms) in the time domain.
Over-current protection (OCP) feature.
The device can measure power.
The device can act as a scale.
int(* config_get)(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
Query value of a configuration key in driver or given device instance.
The device supports setting a sample time limit (how long the sample acquisition should run...
Data source for acquisition.
GSList * sr_driver_scan(struct sr_dev_driver *driver, GSList *options)
Tell a hardware driver to scan for devices.
The public libsigrok header file to be used by frontends.
Over-current protection (OCP) threshold.
Logic low-high threshold range.
Sound pressure level is time-averaged (LAT), also known as Equivalent Continuous A-weighted Sound Lev...
Device is in autoranging mode.
Sound pressure level is C-weighted in the frequency domain, according to IEC 61672:2003.
const char * name
Driver name.
The device is a demo device.
Device is in "min" mode, only updating upon a new min value.
Electrical power, usually in W, or dBm.
Sound pressure level represented as a percentage of measurements that were over a preset alarm level...
GArray * sr_driver_scan_options_list(const struct sr_dev_driver *driver)
Enumerate scan options supported by this driver.
int datatype
Data type like SR_T_STRING, etc if applicable.
GSList *(* scan)(struct sr_dev_driver *driver, GSList *options)
Scan for devices.
The device supports specifying a capturefile to inject.
Voltage measurement is alternating current (AC).
The device supports setting a pre/post-trigger capture ratio.
Used for setting or getting value of a config item.
Horizontal trigger position.
The device supports specifying the capturefile unit size.
Specification on how to connect to a device.
The device can measure energy consumption.
The device supports setting its sample interval, in ms.
Device is in "hold" mode (repeating the last measurement).
The device has internal storage, into which data is logged.
Offset of a source without strictly-defined MQ.
Enabling/disabling channel.
Over-voltage protection (OVP) threshold.
A channel group must be specified.
Parallel inductance (LCR meter model).
Possible values can be enumerated.
Logic threshold: custom numerical value.
The device instance is actively in use in a session.
struct sr_dev_driver ** sr_driver_list(const struct sr_context *ctx)
Return the list of supported hardware drivers.
Number of powerline cycles for ADC integration time.
The device can act as a function generator.
Parallel resistance (LCR meter model).
Series resistance (LCR meter model).
GVariant * data
Key-specific data.
int sr_config_commit(const struct sr_dev_inst *sdi)
Apply configuration settings to the device hardware.
The device can measure humidity.
Which external clock source to use if the device supports multiple external clock channels...
int sr_config_set(const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, uint32_t key, GVariant *data)
Set value of a configuration key in a device instance.
The device can act as a multimeter.
The device supports using an external clock.
The device supports setting its samplerate, in Hz.
Structure for groups of channels that have common properties.
The device can act as logic analyzer.
The device can act as a signal demodulator.
Amplitude of a source without strictly-defined MQ.
Over-voltage protection (OVP) active.
The device supports averaging.
int(* init)(struct sr_dev_driver *driver, struct sr_context *sr_ctx)
Called when driver is loaded, e.g.
Modbus slave address specification.
Device is closed, but must be open.
The device supports swapping channels.
char * name
Name of the channel group.
Device is in "avg" mode, averaging upon each new value.
Sound pressure level time weighting.
Device mode for multi-function devices.
Voltage measurement is direct current (DC).
The device can act as a sound level meter.
const struct sr_key_info * sr_key_info_get(int keytype, uint32_t key)
Get information about a key, by key.
The device supports setting trigger slope.
Serial communication specification, in the form:
void * context
Device driver context, considered private.
uint32_t key
Config key like SR_CONF_CONN, MQ value like SR_MQ_VOLTAGE, etc.
Series inductance (LCR meter model).
const struct sr_key_info * sr_key_info_name_get(int keytype, const char *keyid)
Get information about a key, by name.
The device supports setting a pattern (pattern generator mode).
The measurement range of a DMM or the output range of a power supply.
Gain (a transistor's gain, or hFE, for example).
The device supports setting number of samples to be averaged over.
The device supports setting a probe factor.
The device can act as a programmable power supply.
Sound pressure level is Z-weighted (i.e.
The device supports setting a pattern for the logic trigger.
uint32_t key
Config key like SR_CONF_CONN, etc.
Over-temperature protection (OTP)
int sr_config_get(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, uint32_t key, GVariant **data)
Query value of a configuration key at the given driver or device instance.
Logic threshold: predefined levels (TTL, ECL, CMOS, etc).
Over-voltage protection (OVP) feature.
Difference from reference value.
Over-temperature protection (OTP) active.
int(* cleanup)(const struct sr_dev_driver *driver)
Called before driver is unloaded.
Value is voltage drop across a diode, or NAN.
Parallel capacitance (LCR meter model).
Sound pressure level measurement range.
const char * id
Short, lowercase ID string, e.g.
Device is in "max" mode, only updating upon a new max value.
Device is in relative mode.
The device supports setting a frame limit (how many frames should be acquired).
Logarithmic representation of sound pressure relative to a reference value.
The device can act as an electronic load.
Under-voltage condition threshold.
Sound pressure level frequency weighting.
Number of horizontal divisions, as related to SR_CONF_TIMEBASE.
Opaque structure representing a libsigrok context.
Over-current protection (OCP) active.
Choice of clock edge for external clock ("r" or "f").
Unstable value (hasn't settled yet).