23 #include <sys/types.h> 28 #include "libsigrok-internal.h" 31 #define LOG_PREFIX "hwdriver" 72 "Serial communication", NULL},
74 "Modbus slave address", NULL},
76 "Forced detection", NULL},
78 "Names of device's probes", NULL},
84 "Pre-trigger capture ratio", NULL},
88 "Run length encoding", NULL},
90 "Trigger slope", NULL},
94 "Number of samples to average over", NULL},
96 "Trigger source", NULL},
98 "Horizontal trigger position", NULL},
100 "Buffer size", NULL},
110 "Trigger matches", NULL},
112 "Sample interval", NULL},
114 "Number of horizontal divisions", NULL},
116 "Number of vertical divisions", NULL},
118 "Sound pressure level frequency weighting", NULL},
120 "Sound pressure level time weighting", NULL},
122 "Sound pressure level measurement range", NULL},
128 "Voltage threshold", NULL },
130 "External clock mode", NULL},
132 "Swap channel order", NULL},
134 "Center frequency", NULL},
136 "Number of logic channels", NULL},
138 "Number of analog channels", NULL},
140 "Current voltage", NULL},
142 "Voltage target", NULL},
144 "Current current", NULL},
146 "Current limit", NULL},
148 "Channel enabled", NULL},
150 "Channel modes", NULL},
152 "Over-voltage protection enabled", NULL},
154 "Over-voltage protection active", NULL},
156 "Over-voltage protection threshold", NULL},
158 "Over-current protection enabled", NULL},
160 "Over-current protection active", NULL},
162 "Over-current protection threshold", NULL},
168 "Channel regulation", NULL},
170 "Over-temperature protection", NULL},
172 "Output frequency", NULL},
174 "Output frequency target", NULL},
176 "Measured quantity", NULL},
178 "Equivalent circuit model", NULL},
180 "Over-temperature protection active", NULL},
182 "Under-voltage condition", NULL},
184 "Under-voltage condition active", NULL},
186 "Under-voltage condition threshold", NULL},
188 "Trigger level", NULL},
190 "External clock source", NULL},
194 "Trigger pattern", NULL},
196 "High resolution", NULL},
198 "Peak detection", NULL},
200 "Logic threshold (predefined)", NULL},
202 "Logic threshold (custom)", NULL},
214 "Power Target", NULL},
216 "Resistance Target", NULL},
218 "Over-current protection delay", NULL},
222 "Session file", NULL},
224 "Capture file", NULL},
226 "Capture unitsize", NULL},
230 "Data source", NULL},
232 "Probe factor", NULL},
234 "Number of ADC powerline cycles", NULL},
240 "Sample limit", NULL},
242 "Frame limit", NULL},
244 "Continuous sampling", NULL},
248 "Device mode", NULL},
307 "Frequency weighted (A)", NULL},
309 "Frequency weighted (C)", NULL},
311 "Frequency weighted (Z)", NULL},
313 "Frequency weighted (flat)", NULL},
315 "Time weighted (S)", NULL},
317 "Time weighted (F)", NULL},
320 "Percentage over alarm", NULL},
335 return G_VARIANT_TYPE_INT32;
337 return G_VARIANT_TYPE_UINT32;
339 return G_VARIANT_TYPE_UINT64;
341 return G_VARIANT_TYPE_STRING;
343 return G_VARIANT_TYPE_BOOLEAN;
345 return G_VARIANT_TYPE_DOUBLE;
350 return G_VARIANT_TYPE_TUPLE;
352 return G_VARIANT_TYPE_DICTIONARY;
354 return G_VARIANT_TYPE_TUPLE;
361 SR_PRIV int sr_variant_type_check(uint32_t
key, GVariant *value)
364 const GVariantType *type, *expected;
365 char *expected_string, *type_string;
371 expected = sr_variant_type_get(info->
datatype);
372 type = g_variant_get_type(value);
373 if (!g_variant_type_equal(type, expected)
374 && !g_variant_type_is_subtype_of(type, expected)) {
375 expected_string = g_variant_type_dup_string(expected);
376 type_string = g_variant_type_dup_string(type);
377 sr_err(
"Wrong variant type for key '%s': expected '%s', got '%s'",
378 info->
name, expected_string, type_string);
379 g_free(expected_string);
403 return ctx->driver_list;
430 sr_err(
"Invalid libsigrok context, can't initialize.");
435 sr_err(
"Invalid driver, can't initialize.");
441 if ((ret = driver->
init(driver, ctx)) < 0)
442 sr_err(
"Failed to initialize the driver: %d.", ret);
466 const uint32_t *opts;
473 opts = g_variant_get_fixed_array(gvar, &num_opts,
sizeof(uint32_t));
475 result = g_array_sized_new(FALSE, FALSE,
sizeof(uint32_t), num_opts);
477 g_array_insert_vals(result, 0, opts, num_opts);
479 g_variant_unref(gvar);
484 static int check_options(
struct sr_dev_driver *driver, GSList *options,
485 uint32_t optlist_key,
struct sr_dev_inst *sdi,
492 const uint32_t *opts;
502 opts = g_variant_get_fixed_array(gvar_opts, &num_opts,
sizeof(uint32_t));
503 for (l = options; l; l = l->next) {
505 for (i = 0; i < num_opts; i++) {
506 if (opts[i] == src->
key)
512 sr_err(
"Invalid option %d.", src->
key);
514 sr_err(
"Invalid option '%s'.", srci->
id);
518 if (sr_variant_type_check(src->
key, src->
data) !=
SR_OK) {
523 g_variant_unref(gvar_opts);
558 sr_err(
"Invalid driver, can't scan for devices.");
563 sr_err(
"Driver not initialized, can't scan for devices.");
568 if (check_options(driver, options, SR_CONF_SCAN_OPTIONS, NULL, NULL) !=
SR_OK)
572 l = driver->
scan(driver, options);
574 sr_spew(
"Scan found %d devices (%s).", g_slist_length(l), driver->
name);
594 sr_dbg(
"Cleaning up all drivers.");
597 for (i = 0; drivers[i]; i++) {
599 drivers[i]->
cleanup(drivers[i]);
621 src = g_malloc0(
sizeof(
struct sr_config));
623 src->
data = g_variant_ref_sink(data);
635 if (!src || !src->
data) {
636 sr_err(
"%s: invalid data!", __func__);
640 g_variant_unref(src->
data);
645 SR_PRIV int sr_dev_acquisition_start(
struct sr_dev_inst *sdi)
647 if (!sdi || !sdi->driver) {
648 sr_err(
"%s: Invalid arguments.", __func__);
653 sr_err(
"%s: Device instance not active, can't start.",
658 sr_dbg(
"%s: Starting acquisition.", sdi->driver->name);
660 return sdi->driver->dev_acquisition_start(sdi);
664 SR_PRIV int sr_dev_acquisition_stop(
struct sr_dev_inst *sdi)
666 if (!sdi || !sdi->driver) {
667 sr_err(
"%s: Invalid arguments.", __func__);
672 sr_err(
"%s: Device instance not active, can't stop.",
677 sr_dbg(
"%s: Stopping acquisition.", sdi->driver->name);
679 return sdi->driver->dev_acquisition_stop(sdi);
682 static void log_key(
const struct sr_dev_inst *sdi,
691 if (key == SR_CONF_DEVICE_OPTIONS)
697 tmp_str = g_variant_print(data, TRUE);
698 sr_spew(
"sr_config_%s(): key %d (%s) sdi %p cg %s -> %s", opstr, key,
699 srci ? srci->
id :
"NULL", sdi, cg ? cg->
name :
"NULL",
700 data ? tmp_str :
"NULL");
706 uint32_t key,
unsigned int op, GVariant *data)
711 const uint32_t *opts;
717 suffix =
" for this device instance and channel group";
719 suffix =
" for this device instance";
724 sr_err(
"Invalid key %d.", key);
736 if (g_variant_get_uint64(data) == 0) {
737 sr_err(
"Cannot set '%s' to 0.", srci->
id);
745 if (g_variant_get_uint64(data) > 100) {
746 sr_err(
"Capture ratio must be 0..100.");
754 sr_err(
"No options available%s.", suffix);
757 opts = g_variant_get_fixed_array(gvar_opts, &num_opts,
sizeof(uint32_t));
759 for (i = 0; i < num_opts; i++) {
760 if ((opts[i] & SR_CONF_MASK) == key) {
765 g_variant_unref(gvar_opts);
767 sr_err(
"Option '%s' not available%s.", srci->
id, suffix);
771 if (!(pub_opt & op)) {
772 sr_err(
"Option '%s' not available to %s%s.", srci->
id, opstr, suffix);
805 const struct sr_dev_inst *sdi,
807 uint32_t key, GVariant **data)
811 if (!driver || !data)
820 if (sdi && !sdi->priv) {
821 sr_err(
"Can't get config (sdi != NULL, sdi->priv == NULL).");
829 g_variant_ref_sink(*data);
833 sr_err(
"%s: No channel group specified.",
834 (sdi) ? sdi->driver->name :
"unknown");
861 uint32_t key, GVariant *data)
865 g_variant_ref_sink(data);
867 if (!sdi || !sdi->driver || !sdi->priv || !data)
869 else if (!sdi->driver->config_set)
872 sr_err(
"%s: Device instance not active, can't set config.",
875 }
else if (check_key(sdi->driver, sdi, cg, key,
SR_CONF_SET, data) !=
SR_OK)
877 else if ((ret = sr_variant_type_check(key, data)) ==
SR_OK) {
879 ret = sdi->driver->config_set(key, data, sdi, cg);
882 g_variant_unref(data);
885 sr_err(
"%s: No channel group specified.",
886 (sdi) ? sdi->driver->name :
"unknown");
904 if (!sdi || !sdi->driver)
906 else if (!sdi->driver->config_commit)
909 sr_err(
"%s: Device instance not active, can't commit config.",
913 ret = sdi->driver->config_commit(sdi);
948 const struct sr_dev_inst *sdi,
950 uint32_t key, GVariant **data)
954 if (!driver || !data)
960 if (key != SR_CONF_SCAN_OPTIONS && key != SR_CONF_DEVICE_OPTIONS) {
965 if (sdi && !sdi->priv) {
966 sr_err(
"Can't list config (sdi != NULL, sdi->priv == NULL).");
970 if (key != SR_CONF_SCAN_OPTIONS && key != SR_CONF_DEVICE_OPTIONS && !sdi) {
971 sr_err(
"Config keys other than SR_CONF_SCAN_OPTIONS and " 972 "SR_CONF_DEVICE_OPTIONS always need an sdi.");
976 if (cg && sdi && !sdi->channel_groups) {
977 sr_err(
"Can't list config for channel group, there are none.");
981 if (cg && sdi && !g_slist_find(sdi->channel_groups, cg)) {
982 sr_err(
"If a channel group is specified, it must be a valid one.");
987 sr_err(
"Need sdi when a channel group is specified.");
993 g_variant_ref_sink(*data);
997 sr_err(
"%s: No channel group specified.",
998 (sdi) ? sdi->driver->name :
"unknown");
1003 static struct sr_key_info *get_keytable(
int keytype)
1009 table = sr_key_info_config;
1012 table = sr_key_info_mq;
1015 table = sr_key_info_mqflag;
1018 sr_err(
"Invalid keytype %d", keytype);
1041 if (!(table = get_keytable(keytype)))
1044 for (i = 0; table[i].
key; i++) {
1045 if (table[i].key == key)
1068 if (!(table = get_keytable(keytype)))
1071 for (i = 0; table[i].
key; i++) {
1074 if (!strcmp(table[i].
id, keyid))
The device can measure humidity.
Device is in "min" mode, only updating upon a new min value.
Device is in "max" mode, only updating upon a new max value.
Specification on how to connect to a device.
The device supports continuous sampling.
int(* cleanup)(const struct sr_dev_driver *driver)
Called before driver is unloaded.
The device can act as a signal demodulator.
const char * name
Driver name.
The device supports setting a pattern for the logic trigger.
uint32_t key
Config key like SR_CONF_CONN, etc.
The device supports setting a sample time limit (how long the sample acquisition should run...
Sound pressure level is time-averaged (LAT), also known as Equivalent Continuous A-weighted Sound Lev...
Over-voltage protection (OVP) active.
Possible values can be enumerated.
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.
Over-voltage protection (OVP) threshold.
Device is in relative mode.
The device can act as a sound level meter.
Sound pressure level represented as a percentage of measurements that were over a preset alarm level...
The device supports setting a frame limit (how many frames should be acquired).
Parallel resistance (LCR meter model).
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 measurement range of a DMM or the output range of a power supply.
Duty cycle of a source signal.
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.
const char * id
Short, lowercase ID string, e.g.
Over-current protection (OCP) feature.
The device supports setting a pattern (pattern generator mode).
Under-voltage condition threshold.
The device can act as a programmable power supply.
Logic low-high threshold range.
Enabling/disabling channel.
GVariant * data
Key-specific data.
uint32_t key
Config key like SR_CONF_CONN, MQ value like SR_MQ_VOLTAGE, etc.
Device is in "avg" mode, averaging upon each new value.
Device is in autoranging mode.
Logic threshold: predefined levels (TTL, ECL, CMOS, etc).
The device can measure temperature.
The device can act as a function generator.
The device can act as an oscilloscope.
The device instance is actively in use in a session.
Logarithmic representation of sound pressure relative to a reference value.
Measurement is four wire (e.g.
The device supports specifying a capturefile to inject.
int(* init)(struct sr_dev_driver *driver, struct sr_context *sr_ctx)
Called when driver is loaded, e.g.
Gain (a transistor's gain, or hFE, for example).
Parallel capacitance (LCR meter model).
Sound pressure level frequency weighting.
Under-voltage condition active.
Override builtin probe names from user specs.
Sound pressure level is Z-weighted (i.e.
The device can act as an electronic load.
The device supports setting its samplerate, in Hz.
The device supports setting number of samples to be averaged over.
Over-current protection (OCP) active.
Used for setting or getting value of a config item.
Generic/unspecified error.
void * context
Device driver context, considered private.
Over-current protection (OCP) delay.
User specified forced driver attachment to unknown devices.
The device supports setting the number of logic channels.
Over-voltage protection (OVP) feature.
Over-temperature protection (OTP)
Difference from reference value.
const struct sr_key_info * sr_key_info_get(int keytype, uint32_t key)
Get information about a key, by key.
Over-temperature protection (OTP) active.
Sound pressure level is not weighted in the frequency domain, albeit without standards-defined low an...
Value is voltage drop across a diode, or NAN.
Serial communication specification, in the form:
Device mode for multi-function devices.
The number of digits (e.g.
Electrical power, usually in W, or dBm.
Series capacitance (LCR meter model).
The device can act as a multimeter.
const struct sr_key_info * sr_key_info_name_get(int keytype, const char *keyid)
Get information about a key, by name.
GArray * sr_driver_scan_options_list(const struct sr_dev_driver *driver)
Enumerate scan options supported by this driver.
Channel regulation get: "CV", "CC" or "UR", denoting constant voltage, constant current or unregulate...
Number of vertical divisions, as related to SR_CONF_VDIV.
Series inductance (LCR meter model).
The device supports specifying the capturefile unit size.
int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver)
Initialize a hardware driver.
The device supports using an external clock.
The device can act as logic analyzer.
Voltage measurement is alternating current (AC).
Sound pressure level measurement is S-weighted (1s) in the time domain.
int sr_config_commit(const struct sr_dev_inst *sdi)
Apply configuration settings to the device hardware.
Horizontal trigger position.
Number of horizontal divisions, as related to SR_CONF_TIMEBASE.
The device can measure power.
Opaque structure representing a libsigrok context.
const char * name
Full capitalized name, e.g.
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.
Structure for groups of channels that have common properties.
Unstable value (hasn't settled yet).
Data source for acquisition.
Device is in "hold" mode (repeating the last measurement).
A channel group must be specified.
Output frequency target in Hz.
Sound pressure level is C-weighted in the frequency domain, according to IEC 61672:2003.
Modbus slave address specification.
Offset of a source without strictly-defined MQ.
Time is duration (as opposed to epoch, ...).
Voltage measurement is direct current (DC).
char * name
Name of the channel group.
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 has internal storage, into which data is logged.
Parallel inductance (LCR meter model).
Sound pressure level is A-weighted in the frequency domain, according to IEC 61672:2003.
Sound pressure level time weighting.
The device is a demo device.
GSList *(* scan)(struct sr_dev_driver *driver, GSList *options)
Scan for devices.
struct sr_dev_driver ** sr_driver_list(const struct sr_context *ctx)
Return the list of supported hardware drivers.
The device supports setting trigger slope.
The device supports setting a sample number limit (how many samples should be acquired).
The device can act as an LCR meter.
The public libsigrok header file to be used by frontends.
The device supports run-length encoding (RLE).
Which external clock source to use if the device supports multiple external clock channels...
This is a true RMS measurement.
Sound pressure level measurement range.
The device supports setting a pre/post-trigger capture ratio.
Equivalent circuit model.
Sound pressure level measurement is F-weighted (125ms) in the time domain.
Device is closed, but must be open.
Over-current protection (OCP) threshold.
The device supports setting a probe factor.
Amplitude of a source without strictly-defined MQ.
Phase of a source signal.
The device can act as a scale.
The device supports averaging.
The device can switch between multiple sources, e.g.
The device can measure energy consumption.
Choice of clock edge for external clock ("r" or "f").
GSList * sr_driver_scan(struct sr_dev_driver *driver, GSList *options)
Tell a hardware driver to scan for devices.
The device supports setting its sample interval, in ms.
Number of powerline cycles for ADC integration time.
Logic threshold: custom numerical value.
The device supports swapping channels.
int datatype
Data type like SR_T_STRING, etc if applicable.
The device supports setting the number of analog channels.
Series resistance (LCR meter model).