#include <string.h>
#include <glib.h>
#include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
/** @cond PRIVATE */
#define LOG_PREFIX "hwdriver"
/** @endcond */
-extern SR_PRIV struct sr_dev_driver **drivers_lists[];
-
/**
* @file
*
*/
/* Same key order/grouping as in enum sr_configkey (libsigrok.h). */
-static struct sr_config_info sr_config_info_data[] = {
+static struct sr_key_info sr_key_info_config[] = {
/* Device classes */
{SR_CONF_LOGIC_ANALYZER, SR_T_STRING, NULL, "Logic analyzer", NULL},
{SR_CONF_OSCILLOSCOPE, SR_T_STRING, NULL, "Oscilloscope", NULL},
"Connection", NULL},
{SR_CONF_SERIALCOMM, SR_T_STRING, "serialcomm",
"Serial communication", NULL},
+ {SR_CONF_MODBUSADDR, SR_T_UINT64, "modbusaddr",
+ "Modbus slave address", NULL},
/* Device (or channel group) configuration */
{SR_CONF_SAMPLERATE, SR_T_UINT64, "samplerate",
"Number of logic channels", NULL},
{SR_CONF_NUM_ANALOG_CHANNELS, SR_T_INT32, "analog_channels",
"Number of analog channels", NULL},
- {SR_CONF_OUTPUT_VOLTAGE, SR_T_FLOAT, "output_voltage",
- "Current output voltage", NULL},
- {SR_CONF_OUTPUT_VOLTAGE_TARGET, SR_T_FLOAT, "output_voltage_target",
- "Output voltage target", NULL},
- {SR_CONF_OUTPUT_CURRENT, SR_T_FLOAT, "output_current",
- "Current output current", NULL},
- {SR_CONF_OUTPUT_CURRENT_LIMIT, SR_T_FLOAT, "output_current_limit",
- "Output current limit", NULL},
- {SR_CONF_OUTPUT_ENABLED, SR_T_BOOL, "output_enabled",
- "Output enabled", NULL},
- {SR_CONF_OUTPUT_CHANNEL_CONFIG, SR_T_STRING, "output_channel_config",
- "Output channel modes", NULL},
+ {SR_CONF_VOLTAGE, SR_T_FLOAT, "voltage",
+ "Current voltage", NULL},
+ {SR_CONF_VOLTAGE_TARGET, SR_T_FLOAT, "voltage_target",
+ "Voltage target", NULL},
+ {SR_CONF_CURRENT, SR_T_FLOAT, "current",
+ "Current current", NULL},
+ {SR_CONF_CURRENT_LIMIT, SR_T_FLOAT, "current_limit",
+ "Current limit", NULL},
+ {SR_CONF_ENABLED, SR_T_BOOL, "enabled",
+ "Channel enabled", NULL},
+ {SR_CONF_CHANNEL_CONFIG, SR_T_STRING, "channel_config",
+ "Channel modes", NULL},
{SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED, SR_T_BOOL, "ovp_enabled",
"Over-voltage protection enabled", NULL},
{SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE, SR_T_BOOL, "ovp_active",
"Clock edge", NULL},
{SR_CONF_AMPLITUDE, SR_T_FLOAT, "amplitude",
"Amplitude", NULL},
- {SR_CONF_OUTPUT_REGULATION, SR_T_STRING, "output_regulation",
- "Output channel regulation", NULL},
+ {SR_CONF_REGULATION, SR_T_STRING, "regulation",
+ "Channel regulation", NULL},
{SR_CONF_OVER_TEMPERATURE_PROTECTION, SR_T_BOOL, "otp",
"Over-temperature protection", NULL},
- {SR_CONF_OUTPUT_FREQUENCY, SR_T_UINT64, "output_frequency",
+ {SR_CONF_OUTPUT_FREQUENCY, SR_T_FLOAT, "output_frequency",
"Output frequency", NULL},
+ {SR_CONF_OUTPUT_FREQUENCY_TARGET, SR_T_FLOAT, "output_frequency_target",
+ "Output frequency target", NULL},
{SR_CONF_MEASURED_QUANTITY, SR_T_STRING, "measured_quantity",
"Measured quantity", NULL},
{SR_CONF_MEASURED_2ND_QUANTITY, SR_T_STRING, "measured_2nd_quantity",
"Measured secondary quantity", NULL},
{SR_CONF_EQUIV_CIRCUIT_MODEL, SR_T_STRING, "equiv_circuit_model",
"Equivalent circuit model", NULL},
+ {SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE, SR_T_BOOL, "otp_active",
+ "Over-temperature protection active", NULL},
/* Special stuff */
{SR_CONF_SCAN_OPTIONS, SR_T_STRING, "scan_options",
SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *value)
{
- const struct sr_config_info *info;
+ const struct sr_key_info *info;
const GVariantType *type, *expected;
char *expected_string, *type_string;
- info = sr_config_info_get(key);
+ info = sr_key_info_get(SR_KEY_CONFIG, key);
if (!info)
return SR_OK;
/**
* Return the list of supported hardware drivers.
*
- * @return Pointer to the NULL-terminated list of hardware driver pointers.
+ * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
+ *
+ * @retval NULL The ctx argument was NULL, or there are no supported drivers.
+ * @retval Other Pointer to the NULL-terminated list of hardware drivers.
+ * The user should NOT g_free() this list, sr_exit() will do that.
*
- * @since 0.1.0
+ * @since 0.4.0
*/
-SR_API struct sr_dev_driver **sr_driver_list(void)
+SR_API struct sr_dev_driver **sr_driver_list(const struct sr_context *ctx)
{
- static struct sr_dev_driver **combined_list = NULL;
- struct sr_dev_driver ***lists, **drivers;
- GArray *array;
-
- if (!combined_list) {
- array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
- for (lists = drivers_lists; *lists; lists++)
- for (drivers = *lists; *drivers; drivers++)
- g_array_append_val(array, *drivers);
- combined_list = (struct sr_dev_driver **) array->data;
- g_array_free(array, FALSE);
- }
+ if (!ctx)
+ return NULL;
- return combined_list;
+ return ctx->driver_list;
}
/**
}
sr_spew("Initializing driver '%s'.", driver->name);
- if ((ret = driver->init(ctx)) < 0)
+ if ((ret = driver->init(driver, ctx)) < 0)
sr_err("Failed to initialize the driver: %d.", ret);
return ret;
struct sr_channel_group *cg)
{
struct sr_config *src;
- const struct sr_config_info *srci;
+ const struct sr_key_info *srci;
GVariant *gvar_opts;
GSList *l;
const uint32_t *opts;
break;
}
if (i == num_opts) {
- if (!(srci = sr_config_info_get(src->key)))
+ if (!(srci = sr_key_info_get(SR_KEY_CONFIG, src->key)))
/* Shouldn't happen. */
sr_err("Invalid option %d.", src->key);
else
return NULL;
}
- if (!driver->priv) {
+ if (!driver->context) {
sr_err("Driver not initialized, can't scan for devices.");
return NULL;
}
return NULL;
}
- l = driver->scan(options);
+ l = driver->scan(driver, options);
sr_spew("Scan of '%s' found %d devices.", driver->name,
g_slist_length(l));
return l;
}
-/** Call driver cleanup function for all drivers.
- * @private */
-SR_PRIV void sr_hw_cleanup_all(void)
+/**
+ * Call driver cleanup function for all drivers.
+ *
+ * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
+ *
+ * @private
+ */
+SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx)
{
int i;
struct sr_dev_driver **drivers;
- drivers = sr_driver_list();
+ if (!ctx)
+ return;
+
+ drivers = sr_driver_list(ctx);
for (i = 0; drivers[i]; i++) {
if (drivers[i]->cleanup)
- drivers[i]->cleanup();
+ drivers[i]->cleanup(drivers[i]);
+ drivers[i]->context = NULL;
}
}
}
+static void log_key(const struct sr_dev_inst *sdi,
+ const struct sr_channel_group *cg, uint32_t key, int op, GVariant *data)
+{
+ const char *opstr;
+ const struct sr_key_info *srci;
+
+ /* Don't log SR_CONF_DEVICE_OPTIONS, it's verbose and not too useful. */
+ if (key == SR_CONF_DEVICE_OPTIONS)
+ return;
+
+ opstr = op == SR_CONF_GET ? "get" : op == SR_CONF_SET ? "set" : "list";
+ srci = sr_key_info_get(SR_KEY_CONFIG, key);
+
+ sr_spew("sr_config_%s(): key %d (%s) sdi %p cg %s -> %s", opstr, key,
+ srci ? srci->id : "NULL", sdi, cg ? cg->name : "NULL",
+ data ? g_variant_print(data, TRUE) : "NULL");
+}
+
static int check_key(const struct sr_dev_driver *driver,
const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
- uint32_t key, int op)
+ uint32_t key, int op, GVariant *data)
{
- const struct sr_config_info *srci;
+ const struct sr_key_info *srci;
gsize num_opts, i;
GVariant *gvar_opts;
const uint32_t *opts;
else
suffix = "";
- if (!(srci = sr_config_info_get(key))) {
+ if (!(srci = sr_key_info_get(SR_KEY_CONFIG, key))) {
sr_err("Invalid key %d.", key);
return SR_ERR_ARG;
}
opstr = op == SR_CONF_GET ? "get" : op == SR_CONF_SET ? "set" : "list";
- sr_spew("sr_config_%s(): key %d (%s) sdi %p cg %s", opstr, key,
- srci->id, sdi, cg ? cg->name : "NULL");
+
+ switch (key) {
+ case SR_CONF_LIMIT_MSEC:
+ case SR_CONF_LIMIT_SAMPLES:
+ case SR_CONF_SAMPLERATE:
+ /* Setting any of these to 0 is not useful. */
+ if (op != SR_CONF_SET || !data)
+ break;
+ if (g_variant_get_uint64(data) == 0) {
+ sr_err("Cannot set '%s' to 0.", srci->id);
+ return SR_ERR_ARG;
+ }
+ break;
+ }
if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS, &gvar_opts) != SR_OK) {
/* Driver publishes no options. */
if (!driver->config_get)
return SR_ERR_ARG;
- if (check_key(driver, sdi, cg, key, SR_CONF_GET) != SR_OK)
+ if (check_key(driver, sdi, cg, key, SR_CONF_GET, NULL) != SR_OK)
return SR_ERR_ARG;
if ((ret = driver->config_get(key, data, sdi, cg)) == SR_OK) {
+ log_key(sdi, cg, key, SR_CONF_GET, *data);
/* Got a floating reference from the driver. Sink it here,
* caller will need to unref when done with it. */
g_variant_ref_sink(*data);
ret = SR_ERR;
else if (!sdi->driver->config_set)
ret = SR_ERR_ARG;
- else if (check_key(sdi->driver, sdi, cg, key, SR_CONF_SET) != SR_OK)
+ else if (check_key(sdi->driver, sdi, cg, key, SR_CONF_SET, data) != SR_OK)
return SR_ERR_ARG;
- else if ((ret = sr_variant_type_check(key, data)) == SR_OK)
+ else if ((ret = sr_variant_type_check(key, data)) == SR_OK) {
+ log_key(sdi, cg, key, SR_CONF_SET, data);
ret = sdi->driver->config_set(key, data, sdi, cg);
+ }
g_variant_unref(data);
else if (!driver->config_list)
return SR_ERR_ARG;
else if (key != SR_CONF_SCAN_OPTIONS && key != SR_CONF_DEVICE_OPTIONS) {
- if (check_key(driver, sdi, cg, key, SR_CONF_LIST) != SR_OK)
+ if (check_key(driver, sdi, cg, key, SR_CONF_LIST, NULL) != SR_OK)
return SR_ERR_ARG;
}
- if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK)
+ if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK) {
+ log_key(sdi, cg, key, SR_CONF_LIST, *data);
g_variant_ref_sink(*data);
+ }
return ret;
}
+static struct sr_key_info *get_keytable(int keytype)
+{
+ struct sr_key_info *table;
+
+ switch (keytype) {
+ case SR_KEY_CONFIG:
+ table = sr_key_info_config;
+ break;
+ default:
+ sr_err("Invalid keytype %d", keytype);
+ return NULL;
+ }
+
+ return table;
+}
+
/**
- * Get information about a configuration key, by key.
+ * Get information about a key, by key.
*
- * @param[in] key The configuration key.
+ * @param[in] keytype The namespace the key is in.
+ * @param[in] key The key to find.
*
- * @return A pointer to a struct sr_config_info, or NULL if the key
+ * @return A pointer to a struct sr_key_info, or NULL if the key
* was not found.
*
- * @since 0.2.0
+ * @since 0.3.0
*/
-SR_API const struct sr_config_info *sr_config_info_get(uint32_t key)
+SR_API const struct sr_key_info *sr_key_info_get(int keytype, uint32_t key)
{
+ struct sr_key_info *table;
int i;
- for (i = 0; sr_config_info_data[i].key; i++) {
- if (sr_config_info_data[i].key == key)
- return &sr_config_info_data[i];
+ if (!(table = get_keytable(keytype)))
+ return NULL;
+
+ for (i = 0; table[i].key; i++) {
+ if (table[i].key == key)
+ return &table[i];
}
return NULL;
}
/**
- * Get information about a configuration key, by name.
+ * Get information about a key, by name.
*
- * @param[in] optname The configuration key.
+ * @param[in] keytype The namespace the key is in.
+ * @param[in] keyid The key id string.
*
- * @return A pointer to a struct sr_config_info, or NULL if the key
+ * @return A pointer to a struct sr_key_info, or NULL if the key
* was not found.
*
* @since 0.2.0
*/
-SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname)
+SR_API const struct sr_key_info *sr_key_info_name_get(int keytype, const char *keyid)
{
+ struct sr_key_info *table;
int i;
- for (i = 0; sr_config_info_data[i].key; i++) {
- if (!sr_config_info_data[i].id)
+ if (!(table = get_keytable(keytype)))
+ return NULL;
+
+ for (i = 0; table[i].key; i++) {
+ if (!table[i].id)
continue;
- if (!strcmp(sr_config_info_data[i].id, optname))
- return &sr_config_info_data[i];
+ if (!strcmp(table[i].id, keyid))
+ return &table[i];
}
return NULL;