blob: 454a767fcbb2dd3274bd263bebcd7960aab72aa6 [file] [log] [blame]
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02001/*
Uwe Hermann50985c22013-04-23 22:24:30 +02002 * This file is part of the libsigrok project.
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02003 *
Bert Vermeulen13d8e032013-03-24 11:21:00 +01004 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02005 *
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 3 of the License, or
9 * (at your option) any later version.
10 *
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.
15 *
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/>.
18 */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <sys/types.h>
23#include <dirent.h>
24#include <string.h>
25#include <glib.h>
Uwe Hermann545f9782012-10-24 00:41:21 +020026#include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
Bert Vermeulen45c59c82012-07-05 00:55:07 +020027#include "libsigrok.h"
28#include "libsigrok-internal.h"
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020029
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020030/** @cond PRIVATE */
Martin Ling3544f842013-12-23 03:38:35 +000031#define LOG_PREFIX "hwdriver"
Uwe Hermann2ad1deb2014-04-25 18:40:59 +020032/** @endcond */
Uwe Hermannbd36d822013-01-29 12:55:00 +010033
Bert Vermeulen5d8c55f2014-07-11 02:14:14 +020034extern SR_PRIV struct sr_dev_driver *drivers_list[];
35
Uwe Hermann7b870c32012-10-21 16:13:36 +020036/**
Uwe Hermann393fb9c2012-10-22 00:30:12 +020037 * @file
38 *
39 * Hardware driver handling in libsigrok.
40 */
41
42/**
Uwe Hermann7b870c32012-10-21 16:13:36 +020043 * @defgroup grp_driver Hardware drivers
44 *
45 * Hardware driver handling in libsigrok.
46 *
47 * @{
48 */
Bert Vermeulen8bfdc8c2012-07-12 20:54:45 +020049
Bert Vermeulenc89c1c92013-01-21 21:58:19 +010050static struct sr_config_info sr_config_info_data[] = {
Uwe Hermannace218f2014-05-06 20:18:54 +020051 {SR_CONF_CONN, SR_T_STRING, "conn",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010052 "Connection", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020053 {SR_CONF_SERIALCOMM, SR_T_STRING, "serialcomm",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010054 "Serial communication", NULL},
Bert Vermeulen19535642013-01-21 23:22:47 +010055 {SR_CONF_SAMPLERATE, SR_T_UINT64, "samplerate",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010056 "Sample rate", NULL},
Bert Vermeulen19535642013-01-21 23:22:47 +010057 {SR_CONF_CAPTURE_RATIO, SR_T_UINT64, "captureratio",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010058 "Pre-trigger capture ratio", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020059 {SR_CONF_PATTERN_MODE, SR_T_STRING, "pattern",
Bert Vermeulen2438b732014-01-15 01:51:04 +010060 "Pattern", NULL},
Bert Vermeulen795c9de2014-05-27 23:55:26 +020061 {SR_CONF_TRIGGER_MATCH, SR_T_INT32, "triggermatch",
62 "Trigger matches", NULL},
Matt Ranostayeb1b6102013-08-24 22:01:15 -070063 {SR_CONF_EXTERNAL_CLOCK, SR_T_BOOL, "external_clock",
64 "External clock mode", NULL},
Matt Ranostay7b0a57f2013-08-27 21:19:56 -070065 {SR_CONF_SWAP, SR_T_BOOL, "swap",
66 "Swap channel order", NULL},
Bert Vermeulen19535642013-01-21 23:22:47 +010067 {SR_CONF_RLE, SR_T_BOOL, "rle",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010068 "Run Length Encoding", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020069 {SR_CONF_TRIGGER_SLOPE, SR_T_STRING, "triggerslope",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010070 "Trigger slope", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020071 {SR_CONF_TRIGGER_SOURCE, SR_T_STRING, "triggersource",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010072 "Trigger source", NULL},
Bert Vermeulen19535642013-01-21 23:22:47 +010073 {SR_CONF_HORIZ_TRIGGERPOS, SR_T_FLOAT, "horiz_triggerpos",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010074 "Horizontal trigger position", NULL},
Bert Vermeulen19535642013-01-21 23:22:47 +010075 {SR_CONF_BUFFERSIZE, SR_T_UINT64, "buffersize",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010076 "Buffer size", NULL},
Bert Vermeulen19535642013-01-21 23:22:47 +010077 {SR_CONF_TIMEBASE, SR_T_RATIONAL_PERIOD, "timebase",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010078 "Time base", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020079 {SR_CONF_FILTER, SR_T_STRING, "filter",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010080 "Filter targets", NULL},
Bert Vermeulen19535642013-01-21 23:22:47 +010081 {SR_CONF_VDIV, SR_T_RATIONAL_VOLT, "vdiv",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010082 "Volts/div", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020083 {SR_CONF_COUPLING, SR_T_STRING, "coupling",
Bert Vermeulen083d64f2013-01-20 16:35:25 +010084 "Coupling", NULL},
Bert Vermeulene6551ea2013-03-11 16:35:18 +010085 {SR_CONF_DATALOG, SR_T_BOOL, "datalog",
86 "Datalog", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020087 {SR_CONF_SPL_WEIGHT_FREQ, SR_T_STRING, "spl_weight_freq",
Bert Vermeulenfd8854c2013-06-15 12:09:31 +020088 "Sound pressure level frequency weighting", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +020089 {SR_CONF_SPL_WEIGHT_TIME, SR_T_STRING, "spl_weight_time",
Bert Vermeulenfd8854c2013-06-15 12:09:31 +020090 "Sound pressure level time weighting", NULL},
Bert Vermeulen9fd6bc22013-06-16 12:12:20 +020091 {SR_CONF_HOLD_MAX, SR_T_BOOL, "hold_max",
92 "Hold max", NULL},
93 {SR_CONF_HOLD_MIN, SR_T_BOOL, "hold_min",
94 "Hold min", NULL},
Bert Vermeulen8417eba2013-06-17 01:25:41 +020095 {SR_CONF_SPL_MEASUREMENT_RANGE, SR_T_UINT64_RANGE, "spl_meas_range",
96 "Sound pressure level measurement range", NULL},
Marcus Comstedtdb11d7d2013-08-04 16:20:07 +020097 {SR_CONF_VOLTAGE_THRESHOLD, SR_T_DOUBLE_RANGE, "voltage_threshold",
98 "Voltage threshold", NULL },
Bert Vermeulen32de50b2013-06-17 11:54:37 +020099 {SR_CONF_POWER_OFF, SR_T_BOOL, "power_off",
100 "Power off", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +0200101 {SR_CONF_DATA_SOURCE, SR_T_STRING, "data_source",
Bert Vermeulen6caeef62013-06-19 12:18:00 +0200102 "Data source", NULL},
Uwe Hermannfca75cb2014-03-24 16:11:45 +0100103 {SR_CONF_NUM_LOGIC_CHANNELS, SR_T_INT32, "logic_channels",
104 "Number of logic channels", NULL},
105 {SR_CONF_NUM_ANALOG_CHANNELS, SR_T_INT32, "analog_channels",
106 "Number of analog channels", NULL},
Bert Vermeulen471607f2014-01-08 21:15:45 +0100107 {SR_CONF_OUTPUT_VOLTAGE, SR_T_FLOAT, "output_voltage",
108 "Current output voltage", NULL},
Bert Vermeulenae431bc2014-10-16 13:22:51 +0200109 {SR_CONF_OUTPUT_VOLTAGE_TARGET, SR_T_FLOAT, "output_voltage_target",
Bert Vermeulenca95e902014-10-15 12:03:00 +0200110 "Output voltage target", NULL},
Bert Vermeulen471607f2014-01-08 21:15:45 +0100111 {SR_CONF_OUTPUT_CURRENT, SR_T_FLOAT, "output_current",
112 "Current output current", NULL},
Bert Vermeulenae431bc2014-10-16 13:22:51 +0200113 {SR_CONF_OUTPUT_CURRENT_LIMIT, SR_T_FLOAT, "output_current_limit",
Bert Vermeulenca95e902014-10-15 12:03:00 +0200114 "Output current limit", NULL},
Bert Vermeulen471607f2014-01-08 21:15:45 +0100115 {SR_CONF_OUTPUT_ENABLED, SR_T_BOOL, "output_enabled",
116 "Output enabled", NULL},
Bert Vermeulena1eaa9e2014-09-05 02:47:22 +0200117 {SR_CONF_OUTPUT_CHANNEL_CONFIG, SR_T_STRING, "output_channel_config",
Bert Vermeulen471607f2014-01-08 21:15:45 +0100118 "Output channel modes", NULL},
Bert Vermeulena1eaa9e2014-09-05 02:47:22 +0200119 {SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED, SR_T_BOOL, "ovp_enabled",
120 "Over-voltage protection enabled", NULL},
121 {SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE, SR_T_BOOL, "ovp_active",
122 "Over-voltage protection active", NULL},
123 {SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD, SR_T_FLOAT, "ovp_threshold",
124 "Over-voltage protection threshold", NULL},
125 {SR_CONF_OVER_CURRENT_PROTECTION_ENABLED, SR_T_BOOL, "ocp_enabled",
126 "Over-current protection enabled", NULL},
127 {SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE, SR_T_BOOL, "ocp_active",
128 "Over-current protection active", NULL},
129 {SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD, SR_T_FLOAT, "ocp_threshold",
130 "Over-current protection threshold", NULL},
Bert Vermeulenf0de2dd2014-01-19 17:18:59 +0100131 {SR_CONF_LIMIT_SAMPLES, SR_T_UINT64, "limit_samples",
132 "Sample limit", NULL},
Uwe Hermannace218f2014-05-06 20:18:54 +0200133 {SR_CONF_CLOCK_EDGE, SR_T_STRING, "clock_edge",
Daniel Elstner860bc592014-01-29 20:53:51 +0100134 "Clock edge", NULL},
Bert Vermeulencff7d8d2014-07-27 23:59:49 +0200135 {SR_CONF_AMPLITUDE, SR_T_FLOAT, "amplitude",
136 "Amplitude", NULL},
Bert Vermeulena1eaa9e2014-09-05 02:47:22 +0200137 {SR_CONF_OVER_TEMPERATURE_PROTECTION, SR_T_BOOL, "otp",
138 "Over-temperature protection", NULL},
139 {SR_CONF_OUTPUT_REGULATION, SR_T_STRING, "output_regulation",
140 "Output channel regulation", NULL},
Bert Vermeulen083d64f2013-01-20 16:35:25 +0100141 {0, 0, NULL, NULL, NULL},
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200142};
143
Martin Ling13fef1e2014-09-14 04:47:12 +0100144SR_PRIV const GVariantType *sr_variant_type_get(int datatype)
145{
146 switch (datatype) {
147 case SR_T_INT32:
148 return G_VARIANT_TYPE_INT32;
149 case SR_T_UINT64:
150 return G_VARIANT_TYPE_UINT64;
151 case SR_T_STRING:
152 return G_VARIANT_TYPE_STRING;
153 case SR_T_BOOL:
154 return G_VARIANT_TYPE_BOOLEAN;
155 case SR_T_FLOAT:
156 return G_VARIANT_TYPE_DOUBLE;
157 case SR_T_RATIONAL_PERIOD:
158 case SR_T_RATIONAL_VOLT:
159 case SR_T_UINT64_RANGE:
160 case SR_T_DOUBLE_RANGE:
161 return G_VARIANT_TYPE_TUPLE;
162 case SR_T_KEYVALUE:
163 return G_VARIANT_TYPE_DICTIONARY;
164 default:
165 return NULL;
166 }
167}
168
Bert Vermeulen584560f2014-09-16 17:49:20 +0200169SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *value)
Martin Ling13fef1e2014-09-14 04:47:12 +0100170{
171 const struct sr_config_info *info;
172 const GVariantType *type, *expected;
173 info = sr_config_info_get(key);
174 if (!info)
175 return SR_OK;
176 expected = sr_variant_type_get(info->datatype);
177 type = g_variant_get_type(value);
178 if (!g_variant_type_equal(type, expected)) {
179 gchar *expected_string = g_variant_type_dup_string(expected);
180 gchar *type_string = g_variant_type_dup_string(type);
181 sr_err("Wrong variant type for key '%s': expected '%s', got '%s'",
182 info->name, expected_string, type_string);
183 g_free(expected_string);
184 g_free(type_string);
185 return SR_ERR_ARG;
186 }
187 return SR_OK;
188}
189
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100190/**
Uwe Hermanncfe064d2012-02-29 19:56:15 +0100191 * Return the list of supported hardware drivers.
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100192 *
Uwe Hermannc09f0b52012-02-28 23:52:30 +0100193 * @return Pointer to the NULL-terminated list of hardware driver pointers.
Uwe Hermann47117242014-05-04 20:51:05 +0200194 *
195 * @since 0.1.0
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100196 */
Uwe Hermanncfe064d2012-02-29 19:56:15 +0100197SR_API struct sr_dev_driver **sr_driver_list(void)
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200198{
Bert Vermeulen80bf0422012-07-08 16:25:23 +0200199
Uwe Hermannc09f0b52012-02-28 23:52:30 +0100200 return drivers_list;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200201}
202
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100203/**
Uwe Hermannc09f0b52012-02-28 23:52:30 +0100204 * Initialize a hardware driver.
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100205 *
Uwe Hermannc0eea112013-01-28 19:36:16 +0100206 * This usually involves memory allocations and variable initializations
207 * within the driver, but _not_ scanning for attached devices.
208 * The API call sr_driver_scan() is used for that.
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100209 *
Uwe Hermannc0eea112013-01-28 19:36:16 +0100210 * @param ctx A libsigrok context object allocated by a previous call to
211 * sr_init(). Must not be NULL.
212 * @param driver The driver to initialize. This must be a pointer to one of
213 * the entries returned by sr_driver_list(). Must not be NULL.
214 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100215 * @retval SR_OK Success
216 * @retval SR_ERR_ARG Invalid parameter(s).
217 * @retval SR_ERR_BUG Internal errors.
218 * @retval other Another negative error code upon other errors.
Uwe Hermann47117242014-05-04 20:51:05 +0200219 *
220 * @since 0.2.0
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100221 */
Peter Stuge44fc8702012-12-03 02:42:57 +0100222SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver)
Bert Vermeulen8722c312011-04-04 05:13:29 +0200223{
Uwe Hermannc0eea112013-01-28 19:36:16 +0100224 int ret;
Bert Vermeulen8722c312011-04-04 05:13:29 +0200225
Uwe Hermannc0eea112013-01-28 19:36:16 +0100226 if (!ctx) {
227 sr_err("Invalid libsigrok context, can't initialize.");
228 return SR_ERR_ARG;
229 }
Kristoffer Sjöberg464d12c2011-12-29 17:04:31 +0100230
Uwe Hermannc0eea112013-01-28 19:36:16 +0100231 if (!driver) {
232 sr_err("Invalid driver, can't initialize.");
233 return SR_ERR_ARG;
234 }
235
236 sr_spew("Initializing driver '%s'.", driver->name);
237 if ((ret = driver->init(ctx)) < 0)
238 sr_err("Failed to initialize the driver: %d.", ret);
239
240 return ret;
Bert Vermeulen80bf0422012-07-08 16:25:23 +0200241}
Kristoffer Sjöberg464d12c2011-12-29 17:04:31 +0100242
Bert Vermeulen80bf0422012-07-08 16:25:23 +0200243/**
244 * Tell a hardware driver to scan for devices.
245 *
Bert Vermeulena5f2e702012-07-22 20:06:13 +0200246 * In addition to the detection, the devices that are found are also
247 * initialized automatically. On some devices, this involves a firmware upload,
248 * or other such measures.
249 *
250 * The order in which the system is scanned for devices is not specified. The
251 * caller should not assume or rely on any specific order.
252 *
Uwe Hermann4b97c742013-01-28 20:00:54 +0100253 * Before calling sr_driver_scan(), the user must have previously initialized
254 * the driver by calling sr_driver_init().
Bert Vermeulen80bf0422012-07-08 16:25:23 +0200255 *
Uwe Hermann4b97c742013-01-28 20:00:54 +0100256 * @param driver The driver that should scan. This must be a pointer to one of
257 * the entries returned by sr_driver_list(). Must not be NULL.
258 * @param options A list of 'struct sr_hwopt' options to pass to the driver's
259 * scanner. Can be NULL/empty.
260 *
261 * @return A GSList * of 'struct sr_dev_inst', or NULL if no devices were
262 * found (or errors were encountered). This list must be freed by the
263 * caller using g_slist_free(), but without freeing the data pointed
264 * to in the list.
Uwe Hermann47117242014-05-04 20:51:05 +0200265 *
266 * @since 0.2.0
Bert Vermeulen80bf0422012-07-08 16:25:23 +0200267 */
268SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options)
269{
Uwe Hermann4b97c742013-01-28 20:00:54 +0100270 GSList *l;
Martin Ling13fef1e2014-09-14 04:47:12 +0100271 struct sr_config *src;
Bert Vermeulen8722c312011-04-04 05:13:29 +0200272
Uwe Hermann4b97c742013-01-28 20:00:54 +0100273 if (!driver) {
274 sr_err("Invalid driver, can't scan for devices.");
275 return NULL;
276 }
Bert Vermeulen80bf0422012-07-08 16:25:23 +0200277
Uwe Hermann4b97c742013-01-28 20:00:54 +0100278 if (!driver->priv) {
279 sr_err("Driver not initialized, can't scan for devices.");
280 return NULL;
281 }
282
Martin Ling13fef1e2014-09-14 04:47:12 +0100283 for (l = options; l; l = l->next) {
284 src = l->data;
285 if (sr_variant_type_check(src->key, src->data) != SR_OK)
286 return NULL;
287 }
288
Uwe Hermann4b97c742013-01-28 20:00:54 +0100289 l = driver->scan(options);
290
291 sr_spew("Scan of '%s' found %d devices.", driver->name,
292 g_slist_length(l));
293
294 return l;
Bert Vermeulen8722c312011-04-04 05:13:29 +0200295}
296
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100297/** Call driver cleanup function for all drivers.
298 * @private */
Bert Vermeulen93a04e32012-02-15 03:18:48 +0100299SR_PRIV void sr_hw_cleanup_all(void)
Bert Vermeulen8722c312011-04-04 05:13:29 +0200300{
Uwe Hermann050e9212012-02-22 21:48:30 +0100301 int i;
Uwe Hermannc09f0b52012-02-28 23:52:30 +0100302 struct sr_dev_driver **drivers;
Bert Vermeulen8722c312011-04-04 05:13:29 +0200303
Uwe Hermanncfe064d2012-02-29 19:56:15 +0100304 drivers = sr_driver_list();
Uwe Hermannc09f0b52012-02-28 23:52:30 +0100305 for (i = 0; drivers[i]; i++) {
306 if (drivers[i]->cleanup)
307 drivers[i]->cleanup();
Bert Vermeulen8722c312011-04-04 05:13:29 +0200308 }
Bert Vermeulen8722c312011-04-04 05:13:29 +0200309}
310
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100311/** Allocate struct sr_config.
312 * A floating reference can be passed in for data.
313 * @private
314 */
Bert Vermeulen584560f2014-09-16 17:49:20 +0200315SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data)
Bert Vermeulen4c0e3102013-01-20 16:34:38 +0100316{
317 struct sr_config *src;
318
319 if (!(src = g_try_malloc(sizeof(struct sr_config))))
320 return NULL;
321 src->key = key;
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100322 src->data = g_variant_ref_sink(data);
Bert Vermeulen4c0e3102013-01-20 16:34:38 +0100323
324 return src;
325}
326
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100327/** Free struct sr_config.
328 * @private
329 */
Bert Vermeulen722db132013-03-25 20:23:13 +0100330SR_PRIV void sr_config_free(struct sr_config *src)
331{
332
333 if (!src || !src->data) {
334 sr_err("%s: invalid data!", __func__);
335 return;
336 }
337
338 g_variant_unref(src->data);
339 g_free(src);
340
341}
342
Bert Vermeulendf123802012-07-12 22:41:57 +0200343/**
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100344 * Query value of a configuration key at the given driver or device instance.
Bert Vermeulendf123802012-07-12 22:41:57 +0200345 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100346 * @param[in] driver The sr_dev_driver struct to query.
347 * @param[in] sdi (optional) If the key is specific to a device, this must
Bert Vermeulen57ecdbd2013-04-28 22:11:05 +0200348 * contain a pointer to the struct sr_dev_inst to be checked.
349 * Otherwise it must be NULL.
Uwe Hermann53b46802014-03-20 21:58:01 +0100350 * @param[in] cg The channel group on the device for which to list the
Bert Vermeulen57ecdbd2013-04-28 22:11:05 +0200351 * values, or NULL.
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100352 * @param[in] key The configuration key (SR_CONF_*).
353 * @param[in,out] data Pointer to a GVariant where the value will be stored.
354 * Must not be NULL. The caller is given ownership of the GVariant
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100355 * and must thus decrease the refcount after use. However if
356 * this function returns an error code, the field should be
357 * considered unused, and should not be unreferenced.
Bert Vermeulendf123802012-07-12 22:41:57 +0200358 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100359 * @retval SR_OK Success.
360 * @retval SR_ERR Error.
361 * @retval SR_ERR_ARG The driver doesn't know that key, but this is not to be
362 * interpreted as an error by the caller; merely as an indication
363 * that it's not applicable.
Uwe Hermann47117242014-05-04 20:51:05 +0200364 *
365 * @since 0.3.0
Bert Vermeulendf123802012-07-12 22:41:57 +0200366 */
Martin Ling8f996b82013-04-20 01:00:49 +0100367SR_API int sr_config_get(const struct sr_dev_driver *driver,
368 const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100369 const struct sr_channel_group *cg,
Bert Vermeulen584560f2014-09-16 17:49:20 +0200370 uint32_t key, GVariant **data)
Bert Vermeulendf123802012-07-12 22:41:57 +0200371{
372 int ret;
373
Bert Vermeulencbadb852013-01-26 01:18:19 +0100374 if (!driver || !data)
Bert Vermeulendf123802012-07-12 22:41:57 +0200375 return SR_ERR;
376
Bert Vermeulen6cefe512013-04-07 21:50:42 +0200377 if (!driver->config_get)
378 return SR_ERR_ARG;
379
Uwe Hermann53b46802014-03-20 21:58:01 +0100380 if ((ret = driver->config_get(key, data, sdi, cg)) == SR_OK) {
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100381 /* Got a floating reference from the driver. Sink it here,
382 * caller will need to unref when done with it. */
383 g_variant_ref_sink(*data);
384 }
Bert Vermeulenc5fb5022013-01-25 01:24:42 +0100385
386 return ret;
387}
388
Bert Vermeulena1645fc2012-02-13 03:36:32 +0100389/**
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100390 * Set value of a configuration key in a device instance.
Bert Vermeulen8bfdc8c2012-07-12 20:54:45 +0200391 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100392 * @param[in] sdi The device instance.
Uwe Hermann53b46802014-03-20 21:58:01 +0100393 * @param[in] cg The channel group on the device for which to list the
Bert Vermeulen57ecdbd2013-04-28 22:11:05 +0200394 * values, or NULL.
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100395 * @param[in] key The configuration key (SR_CONF_*).
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100396 * @param data The new value for the key, as a GVariant with GVariantType
397 * appropriate to that key. A floating reference can be passed
398 * in; its refcount will be sunk and unreferenced after use.
Bert Vermeulencbadb852013-01-26 01:18:19 +0100399 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100400 * @retval SR_OK Success.
401 * @retval SR_ERR Error.
402 * @retval SR_ERR_ARG The driver doesn't know that key, but this is not to be
403 * interpreted as an error by the caller; merely as an indication
404 * that it's not applicable.
Uwe Hermann47117242014-05-04 20:51:05 +0200405 *
406 * @since 0.3.0
Bert Vermeulencbadb852013-01-26 01:18:19 +0100407 */
Martin Ling8f996b82013-04-20 01:00:49 +0100408SR_API int sr_config_set(const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100409 const struct sr_channel_group *cg,
Bert Vermeulen584560f2014-09-16 17:49:20 +0200410 uint32_t key, GVariant *data)
Bert Vermeulencbadb852013-01-26 01:18:19 +0100411{
412 int ret;
413
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100414 g_variant_ref_sink(data);
Bert Vermeulencbadb852013-01-26 01:18:19 +0100415
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100416 if (!sdi || !sdi->driver || !data)
417 ret = SR_ERR;
418 else if (!sdi->driver->config_set)
419 ret = SR_ERR_ARG;
Martin Ling13fef1e2014-09-14 04:47:12 +0100420 else if ((ret = sr_variant_type_check(key, data)) == SR_OK)
Uwe Hermann53b46802014-03-20 21:58:01 +0100421 ret = sdi->driver->config_set(key, data, sdi, cg);
Bert Vermeulencbadb852013-01-26 01:18:19 +0100422
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100423 g_variant_unref(data);
Bert Vermeulencbadb852013-01-26 01:18:19 +0100424
425 return ret;
426}
427
428/**
Daniel Elstner2a854d72014-01-19 20:39:11 +0100429 * Apply configuration settings to the device hardware.
430 *
431 * @param sdi The device instance.
432 *
433 * @return SR_OK upon success or SR_ERR in case of error.
Uwe Hermann47117242014-05-04 20:51:05 +0200434 *
435 * @since 0.3.0
Daniel Elstner2a854d72014-01-19 20:39:11 +0100436 */
437SR_API int sr_config_commit(const struct sr_dev_inst *sdi)
438{
439 int ret;
440
441 if (!sdi || !sdi->driver)
442 ret = SR_ERR;
443 else if (!sdi->driver->config_commit)
444 ret = SR_OK;
445 else
446 ret = sdi->driver->config_commit(sdi);
447
448 return ret;
449}
450
451/**
Bert Vermeulencbadb852013-01-26 01:18:19 +0100452 * List all possible values for a configuration key.
453 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100454 * @param[in] driver The sr_dev_driver struct to query.
455 * @param[in] sdi (optional) If the key is specific to a device, this must
Bert Vermeulen57ecdbd2013-04-28 22:11:05 +0200456 * contain a pointer to the struct sr_dev_inst to be checked.
Uwe Hermann53b46802014-03-20 21:58:01 +0100457 * @param[in] cg The channel group on the device for which to list the
Bert Vermeulen57ecdbd2013-04-28 22:11:05 +0200458 * values, or NULL.
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100459 * @param[in] key The configuration key (SR_CONF_*).
460 * @param[in,out] data A pointer to a GVariant where the list will be stored.
461 * The caller is given ownership of the GVariant and must thus
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100462 * unref the GVariant after use. However if this function
463 * returns an error code, the field should be considered
464 * unused, and should not be unreferenced.
Bert Vermeulencbadb852013-01-26 01:18:19 +0100465 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100466 * @retval SR_OK Success.
467 * @retval SR_ERR Error.
468 * @retval SR_ERR_ARG The driver doesn't know that key, but this is not to be
469 * interpreted as an error by the caller; merely as an indication
470 * that it's not applicable.
Uwe Hermann47117242014-05-04 20:51:05 +0200471 *
472 * @since 0.3.0
Bert Vermeulencbadb852013-01-26 01:18:19 +0100473 */
Martin Ling8f996b82013-04-20 01:00:49 +0100474SR_API int sr_config_list(const struct sr_dev_driver *driver,
475 const struct sr_dev_inst *sdi,
Uwe Hermann53b46802014-03-20 21:58:01 +0100476 const struct sr_channel_group *cg,
Bert Vermeulen584560f2014-09-16 17:49:20 +0200477 uint32_t key, GVariant **data)
Bert Vermeulencbadb852013-01-26 01:18:19 +0100478{
479 int ret;
480
Bert Vermeulen6cefe512013-04-07 21:50:42 +0200481 if (!driver || !data)
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100482 ret = SR_ERR;
Bert Vermeulend8284802013-04-07 22:51:45 +0200483 else if (!driver->config_list)
Bert Vermeulen6cefe512013-04-07 21:50:42 +0200484 ret = SR_ERR_ARG;
Uwe Hermann53b46802014-03-20 21:58:01 +0100485 else if ((ret = driver->config_list(key, data, sdi, cg)) == SR_OK)
Bert Vermeulenbc1c2f02013-03-25 15:38:44 +0100486 g_variant_ref_sink(*data);
Bert Vermeulencbadb852013-01-26 01:18:19 +0100487
488 return ret;
489}
490
491/**
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100492 * Get information about a configuration key, by key.
Bert Vermeulencbadb852013-01-26 01:18:19 +0100493 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100494 * @param[in] key The configuration key.
Bert Vermeulen8bfdc8c2012-07-12 20:54:45 +0200495 *
Bert Vermeulenc89c1c92013-01-21 21:58:19 +0100496 * @return A pointer to a struct sr_config_info, or NULL if the key
Bert Vermeulen15cb43d2012-08-05 03:18:07 +0200497 * was not found.
Uwe Hermann47117242014-05-04 20:51:05 +0200498 *
499 * @since 0.2.0
Bert Vermeulen8bfdc8c2012-07-12 20:54:45 +0200500 */
Bert Vermeulen584560f2014-09-16 17:49:20 +0200501SR_API const struct sr_config_info *sr_config_info_get(uint32_t key)
Bert Vermeulen8bfdc8c2012-07-12 20:54:45 +0200502{
503 int i;
504
Bert Vermeulenc89c1c92013-01-21 21:58:19 +0100505 for (i = 0; sr_config_info_data[i].key; i++) {
506 if (sr_config_info_data[i].key == key)
507 return &sr_config_info_data[i];
Bert Vermeulen15cb43d2012-08-05 03:18:07 +0200508 }
509
510 return NULL;
511}
512
513/**
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100514 * Get information about a configuration key, by name.
Bert Vermeulen15cb43d2012-08-05 03:18:07 +0200515 *
Matthias Heidbrink67eb6bc2014-02-05 14:46:10 +0100516 * @param[in] optname The configuration key.
Bert Vermeulen15cb43d2012-08-05 03:18:07 +0200517 *
Bert Vermeulenc89c1c92013-01-21 21:58:19 +0100518 * @return A pointer to a struct sr_config_info, or NULL if the key
Bert Vermeulen15cb43d2012-08-05 03:18:07 +0200519 * was not found.
Uwe Hermann47117242014-05-04 20:51:05 +0200520 *
521 * @since 0.2.0
Bert Vermeulen15cb43d2012-08-05 03:18:07 +0200522 */
Bert Vermeulenc89c1c92013-01-21 21:58:19 +0100523SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname)
Bert Vermeulen15cb43d2012-08-05 03:18:07 +0200524{
525 int i;
526
Bert Vermeulenc89c1c92013-01-21 21:58:19 +0100527 for (i = 0; sr_config_info_data[i].key; i++) {
528 if (!strcmp(sr_config_info_data[i].id, optname))
529 return &sr_config_info_data[i];
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200530 }
531
Uwe Hermann49d0ce52010-05-19 00:38:14 +0200532 return NULL;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +0200533}
534
Uwe Hermann7b870c32012-10-21 16:13:36 +0200535/** @} */