]> sigrok.org Git - libsigrok.git/blame - src/std.c
std: std_gvar_tuple_array/_rational: Fix GVariantBuilder type.
[libsigrok.git] / src / std.c
CommitLineData
063e7aef 1/*
50985c20 2 * This file is part of the libsigrok project.
063e7aef
UH
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
063e7aef
UH
18 */
19
d9251a2c
UH
20/**
21 * @file
22 *
23 * Standard API helper functions.
24 *
25 * @internal
26 */
04cb9157 27
d18bf7bd
UH
28/* Needed for gettimeofday(), at least on FreeBSD. */
29#define _XOPEN_SOURCE 700
30
6ec6c43b 31#include <config.h>
697fb6dd
UH
32#include <string.h>
33#include <math.h>
d18bf7bd 34#include <sys/time.h>
063e7aef 35#include <glib.h>
c1aae900 36#include <libsigrok/libsigrok.h>
063e7aef 37#include "libsigrok-internal.h"
5a1afc09 38#include "scpi.h"
063e7aef 39
3544f848
ML
40#define LOG_PREFIX "std"
41
23772462
UH
42SR_PRIV const uint32_t NO_OPTS[1] = {};
43
063e7aef 44/**
12852b03 45 * Standard driver init() callback API helper.
063e7aef 46 *
6078d2c9 47 * This function can be used to simplify most driver's init() API callback.
063e7aef 48 *
12852b03
UH
49 * Create a new 'struct drv_context' (drvc), assign sr_ctx to it, and
50 * then assign 'drvc' to the 'struct sr_dev_driver' (di) that is passed.
063e7aef 51 *
12852b03
UH
52 * @param[in] di The driver instance to use. Must not be NULL.
53 * @param[in] sr_ctx The libsigrok context to assign. May be NULL.
063e7aef 54 *
12852b03
UH
55 * @retval SR_OK Success.
56 * @retval SR_ERR_ARG Invalid argument.
063e7aef 57 */
c45c32ce 58SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
063e7aef
UH
59{
60 struct drv_context *drvc;
61
12852b03
UH
62 if (!di) {
63 sr_err("%s: Invalid argument.", __func__);
64 return SR_ERR_ARG;
65 }
66
91219afc 67 drvc = g_malloc0(sizeof(struct drv_context));
063e7aef 68 drvc->sr_ctx = sr_ctx;
c2523f22 69 drvc->instances = NULL;
41812aca 70 di->context = drvc;
063e7aef
UH
71
72 return SR_OK;
73}
4afdfd46 74
700d6b64 75/**
12852b03 76 * Standard driver cleanup() callback API helper.
700d6b64 77 *
12852b03 78 * This function can be used to simplify most driver's cleanup() API callback.
700d6b64 79 *
12852b03 80 * Free all device instances by calling sr_dev_clear() and then release any
700d6b64
LPC
81 * resources allocated by std_init().
82 *
12852b03 83 * @param[in] di The driver instance to use. Must not be NULL.
700d6b64 84 *
12852b03
UH
85 * @retval SR_OK Success.
86 * @retval SR_ERR_ARG Invalid argument.
87 * @retval other Other error.
88 */
700d6b64
LPC
89SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
90{
91 int ret;
92
12852b03
UH
93 if (!di) {
94 sr_err("%s: Invalid argument.", __func__);
95 return SR_ERR_ARG;
96 }
97
700d6b64
LPC
98 ret = sr_dev_clear(di);
99 g_free(di->context);
100
101 return ret;
102}
103
4d67b9d9
UH
104/**
105 * Dummmy driver dev_open() callback API helper.
106 *
107 * @param[in] sdi The device instance to use. May be NULL (unused).
108 *
109 * @retval SR_OK Success.
110 */
111SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi)
112{
113 (void)sdi;
114
115 return SR_OK;
116}
117
118/**
119 * Dummmy driver dev_close() callback API helper.
120 *
121 * @param[in] sdi The device instance to use. May be NULL (unused).
122 *
123 * @retval SR_OK Success.
124 */
125SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi)
126{
127 (void)sdi;
128
129 return SR_OK;
130}
131
132/**
133 * Dummmy driver dev_acquisition_start() callback API helper.
134 *
135 * @param[in] sdi The device instance to use. May be NULL (unused).
136 *
137 * @retval SR_OK Success.
138 */
139SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi)
140{
141 (void)sdi;
142
143 return SR_OK;
144}
145
146/**
147 * Dummmy driver dev_acquisition_stop() callback API helper.
148 *
149 * @param[in] sdi The device instance to use. May be NULL (unused).
150 *
151 * @retval SR_OK Success.
152 */
153SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi)
154{
155 (void)sdi;
156
157 return SR_OK;
158}
159
4afdfd46
UH
160/**
161 * Standard API helper for sending an SR_DF_HEADER packet.
162 *
12852b03 163 * This function can be used to simplify most drivers'
6078d2c9 164 * dev_acquisition_start() API callback.
4afdfd46 165 *
12852b03 166 * @param[in] sdi The device instance to use. Must not be NULL.
4afdfd46 167 *
12852b03
UH
168 * @retval SR_OK Success.
169 * @retval SR_ERR_ARG Invalid argument.
170 * @retval other Other error.
4afdfd46 171 */
bee2b016 172SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
4afdfd46 173{
12852b03 174 const char *prefix;
4afdfd46
UH
175 int ret;
176 struct sr_datafeed_packet packet;
177 struct sr_datafeed_header header;
178
12852b03
UH
179 if (!sdi) {
180 sr_err("%s: Invalid argument.", __func__);
181 return SR_ERR_ARG;
182 }
183
184 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
185
4afdfd46 186 /* Send header packet to the session bus. */
4afdfd46
UH
187 packet.type = SR_DF_HEADER;
188 packet.payload = (uint8_t *)&header;
189 header.feed_version = 1;
190 gettimeofday(&header.starttime, NULL);
191
192 if ((ret = sr_session_send(sdi, &packet)) < 0) {
12852b03 193 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
4afdfd46
UH
194 return ret;
195 }
196
197 return SR_OK;
198}
cd2f0fe2 199
3be42bc2
UH
200/**
201 * Standard API helper for sending an SR_DF_END packet.
202 *
12852b03
UH
203 * This function can be used to simplify most drivers'
204 * dev_acquisition_stop() API callback.
205 *
206 * @param[in] sdi The device instance to use. Must not be NULL.
3be42bc2 207 *
12852b03
UH
208 * @retval SR_OK Success.
209 * @retval SR_ERR_ARG Invalid argument.
210 * @retval other Other error.
3be42bc2 211 */
bee2b016 212SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
3be42bc2 213{
12852b03 214 const char *prefix;
3be42bc2
UH
215 int ret;
216 struct sr_datafeed_packet packet;
217
12852b03
UH
218 if (!sdi) {
219 sr_err("%s: Invalid argument.", __func__);
3be42bc2 220 return SR_ERR_ARG;
12852b03
UH
221 }
222
223 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
3be42bc2 224
3be42bc2
UH
225 packet.type = SR_DF_END;
226 packet.payload = NULL;
227
228 if ((ret = sr_session_send(sdi, &packet)) < 0) {
229 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
230 return ret;
231 }
232
233 return SR_OK;
234}
235
b7602846
SA
236/**
237 * Standard API helper for sending an SR_DF_FRAME_BEGIN packet.
238 *
239 * This function can be used to simplify most drivers'
240 * frame handling.
241 *
242 * @param[in] sdi The device instance to use. Must not be NULL.
243 *
244 * @retval SR_OK Success.
245 * @retval SR_ERR_ARG Invalid argument.
246 * @retval other Other error.
247 */
248SR_PRIV int std_session_send_frame_begin(const struct sr_dev_inst *sdi)
249{
250 const char *prefix;
251 int ret;
252 struct sr_datafeed_packet packet;
253
254 if (!sdi) {
255 sr_err("%s: Invalid argument.", __func__);
256 return SR_ERR_ARG;
257 }
258
259 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
260
b7602846
SA
261 packet.type = SR_DF_FRAME_BEGIN;
262 packet.payload = NULL;
263
264 if ((ret = sr_session_send(sdi, &packet)) < 0) {
265 sr_err("%s: Failed to send SR_DF_FRAME_BEGIN packet: %d.", prefix, ret);
266 return ret;
267 }
268
269 return SR_OK;
270}
271
272/**
273 * Standard API helper for sending an SR_DF_FRAME_END packet.
274 *
275 * This function can be used to simplify most drivers'
276 * frame handling.
277 *
278 * @param[in] sdi The device instance to use. Must not be NULL.
279 *
280 * @retval SR_OK Success.
281 * @retval SR_ERR_ARG Invalid argument.
282 * @retval other Other error.
283 */
284SR_PRIV int std_session_send_frame_end(const struct sr_dev_inst *sdi)
285{
286 const char *prefix;
287 int ret;
288 struct sr_datafeed_packet packet;
289
290 if (!sdi) {
291 sr_err("%s: Invalid argument.", __func__);
292 return SR_ERR_ARG;
293 }
294
295 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
296
b7602846
SA
297 packet.type = SR_DF_FRAME_END;
298 packet.payload = NULL;
299
300 if ((ret = sr_session_send(sdi, &packet)) < 0) {
301 sr_err("%s: Failed to send SR_DF_FRAME_END packet: %d.", prefix, ret);
302 return ret;
303 }
304
305 return SR_OK;
306}
307
1df81f4b 308#ifdef HAVE_SERIAL_COMM
c4f2dfd0 309
813aab69 310/**
12852b03 311 * Standard serial driver dev_open() callback API helper.
23dc6661
BV
312 *
313 * This function can be used to implement the dev_open() driver API
314 * callback in drivers that use a serial port. The port is opened
6c592ece 315 * with the SERIAL_RDWR flag.
23dc6661 316 *
12852b03
UH
317 * @param[in] sdi The device instance to use. Must not be NULL.
318 *
23dc6661 319 * @retval SR_OK Success.
12852b03
UH
320 * @retval SR_ERR_ARG Invalid argument.
321 * @retval other Serial port open failed.
23dc6661
BV
322 */
323SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
324{
325 struct sr_serial_dev_inst *serial;
326
12852b03
UH
327 if (!sdi) {
328 sr_err("%s: Invalid argument.", __func__);
7e463623 329 return SR_ERR_ARG;
12852b03 330 }
23dc6661 331
7e463623 332 serial = sdi->conn;
23dc6661 333
7e463623 334 return serial_open(serial, SERIAL_RDWR);
23dc6661
BV
335}
336
813aab69 337/**
12852b03 338 * Standard serial driver dev_close() callback API helper.
1e7134dc
BV
339 *
340 * This function can be used to implement the dev_close() driver API
341 * callback in drivers that use a serial port.
342 *
12852b03
UH
343 * @param[in] sdi The device instance to use. Must not be NULL.
344 *
1e7134dc 345 * @retval SR_OK Success.
12852b03
UH
346 * @retval SR_ERR_ARG Invalid argument.
347 * @retval other Serial port close failed.
1e7134dc
BV
348 */
349SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
350{
351 struct sr_serial_dev_inst *serial;
352
12852b03
UH
353 if (!sdi) {
354 sr_err("%s: Invalid argument.", __func__);
093e1cba 355 return SR_ERR_ARG;
12852b03 356 }
1e7134dc 357
f1ba6b4b 358 serial = sdi->conn;
093e1cba 359
f1ba6b4b 360 return serial_close(serial);
1e7134dc
BV
361}
362
813aab69 363/**
12852b03 364 * Standard serial driver dev_acquisition_stop() callback API helper.
cd2f0fe2 365 *
12852b03 366 * This function can be used to simplify most (serial port based) drivers'
6078d2c9 367 * dev_acquisition_stop() API callback.
cd2f0fe2 368 *
12852b03
UH
369 * @param[in] sdi The device instance for which acquisition should stop.
370 * Must not be NULL.
cd2f0fe2 371 *
1477a9a6 372 * @retval SR_OK Success.
12852b03
UH
373 * @retval SR_ERR_ARG Invalid argument.
374 * @retval other Other error.
cd2f0fe2 375 */
1b38775b 376SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
cd2f0fe2 377{
12852b03
UH
378 struct sr_serial_dev_inst *serial;
379 const char *prefix;
cd2f0fe2 380 int ret;
cd2f0fe2 381
12852b03
UH
382 if (!sdi) {
383 sr_err("%s: Invalid argument.", __func__);
384 return SR_ERR_ARG;
385 }
386
387 serial = sdi->conn;
388 prefix = sdi->driver->name;
389
102f1239 390 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
ac2926b3 391 sr_err("%s: Failed to remove source: %d.", prefix, ret);
cd2f0fe2
UH
392 return ret;
393 }
394
093e1cba 395 if ((ret = sr_dev_close(sdi)) < 0) {
ac2926b3 396 sr_err("%s: Failed to close device: %d.", prefix, ret);
cd2f0fe2
UH
397 return ret;
398 }
399
12852b03 400 return std_session_send_df_end(sdi);
cd2f0fe2 401}
49f00e13 402
c4f2dfd0
UH
403#endif
404
813aab69 405/**
12852b03 406 * Standard driver dev_clear() callback API helper.
49f00e13 407 *
813aab69
MH
408 * Clear driver, this means, close all instances.
409 *
49f00e13
BV
410 * This function can be used to implement the dev_clear() driver API
411 * callback. dev_close() is called before every sr_dev_inst is cleared.
412 *
8bf18daa 413 * The only limitation is driver-specific device contexts (sdi->priv / devc).
49f00e13
BV
414 * These are freed, but any dynamic allocation within structs stored
415 * there cannot be freed.
416 *
12852b03
UH
417 * @param[in] driver The driver which will have its instances released.
418 * Must not be NULL.
419 * @param[in] clear_private If not NULL, this points to a function called
8bf18daa 420 * with sdi->priv (devc) as argument. The function can then clear
12852b03 421 * any device instance-specific resources kept there.
8bf18daa
UH
422 * It must NOT clear the struct pointed to by sdi->priv (devc),
423 * since this function will always free it after clear_private()
424 * has run.
49f00e13 425 *
12852b03
UH
426 * @retval SR_OK Success.
427 * @retval SR_ERR_ARG Invalid argument.
428 * @retval SR_ERR_BUG Implementation bug.
429 * @retval other Other error.
49f00e13 430 */
6e43c3d5 431SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
144f6660 432 std_dev_clear_callback clear_private)
49f00e13 433{
49f00e13 434 struct drv_context *drvc;
12a33563 435 struct sr_dev_inst *sdi;
7aebe22d 436 GSList *l;
49f00e13
BV
437 int ret;
438
12852b03
UH
439 if (!driver) {
440 sr_err("%s: Invalid argument.", __func__);
441 return SR_ERR_ARG;
442 }
443
444 drvc = driver->context; /* Caller checked for context != NULL. */
3a277f3b 445
49f00e13
BV
446 ret = SR_OK;
447 for (l = drvc->instances; l; l = l->next) {
49f00e13 448 if (!(sdi = l->data)) {
12852b03 449 sr_err("%s: Invalid device instance.", __func__);
49f00e13
BV
450 ret = SR_ERR_BUG;
451 continue;
452 }
49f00e13
BV
453 if (driver->dev_close)
454 driver->dev_close(sdi);
455
456 if (sdi->conn) {
1df81f4b 457#ifdef HAVE_SERIAL_COMM
c4f2dfd0 458 if (sdi->inst_type == SR_INST_SERIAL)
12a33563 459 sr_serial_dev_inst_free(sdi->conn);
c4f2dfd0 460#endif
45357ce6 461#ifdef HAVE_LIBUSB_1_0
c4f2dfd0 462 if (sdi->inst_type == SR_INST_USB)
49f00e13 463 sr_usb_dev_inst_free(sdi->conn);
a0c7e23a 464#endif
23f43dff
ML
465 if (sdi->inst_type == SR_INST_SCPI)
466 sr_scpi_free(sdi->conn);
daa39012
AJ
467 if (sdi->inst_type == SR_INST_MODBUS)
468 sr_modbus_free(sdi->conn);
49f00e13 469 }
8bf18daa
UH
470
471 /* Clear driver-specific stuff, if any. */
ae5859ff
BV
472 if (clear_private)
473 clear_private(sdi->priv);
8bf18daa
UH
474
475 /* Clear sdi->priv (devc). */
476 g_free(sdi->priv);
886413b6 477
49f00e13
BV
478 sr_dev_inst_free(sdi);
479 }
480
481 g_slist_free(drvc->instances);
482 drvc->instances = NULL;
483
484 return ret;
485}
c01bf34c 486
f778bf02
UH
487SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver)
488{
489 return std_dev_clear_with_callback(driver, NULL);
490}
491
c01bf34c 492/**
12852b03 493 * Standard driver dev_list() callback API helper.
c01bf34c 494 *
12852b03 495 * This function can be used as the dev_list() callback by most drivers.
c01bf34c 496 *
12852b03 497 * Return the devices contained in the driver context instances list.
c01bf34c 498 *
12852b03
UH
499 * @param[in] di The driver instance to use. Must not be NULL.
500 *
501 * @retval NULL Error, or the list is empty.
502 * @retval other The list of device instances of this driver.
c01bf34c
LPC
503 */
504SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
505{
12852b03
UH
506 struct drv_context *drvc;
507
508 if (!di) {
509 sr_err("%s: Invalid argument.", __func__);
510 return NULL;
511 }
512
513 drvc = di->context;
c01bf34c
LPC
514
515 return drvc->instances;
516}
15a5bfe4
LPC
517
518/**
12852b03 519 * Standard driver scan() callback API helper.
15a5bfe4
LPC
520 *
521 * This function can be used to perform common tasks required by a driver's
522 * scan() callback. It will initialize the driver for each device on the list
523 * and add the devices on the list to the driver's device instance list.
524 * Usually it should be used as the last step in the scan() callback, right
525 * before returning.
526 *
527 * Note: This function can only be used if std_init() has been called
528 * previously by the driver.
529 *
530 * Example:
531 * @code{c}
532 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
533 * {
534 * struct GSList *device;
535 * struct sr_dev_inst *sdi;
536 *
537 * sdi = g_new0(sr_dev_inst, 1);
538 * sdi->vendor = ...;
539 * ...
540 * devices = g_slist_append(devices, sdi);
541 * ...
542 * return std_scan_complete(di, devices);
543 * }
544 * @endcode
545 *
12852b03
UH
546 * @param[in] di The driver instance to use. Must not be NULL.
547 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
548 * May be NULL.
15a5bfe4
LPC
549 *
550 * @return The @p devices list.
551 */
552SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
553{
39fcfdc9 554 struct drv_context *drvc;
15a5bfe4
LPC
555 GSList *l;
556
39fcfdc9
UH
557 if (!di) {
558 sr_err("Invalid driver instance (di), cannot complete scan.");
559 return NULL;
560 }
561
562 drvc = di->context;
563
15a5bfe4
LPC
564 for (l = devices; l; l = l->next) {
565 struct sr_dev_inst *sdi = l->data;
39fcfdc9 566 if (!sdi) {
12852b03 567 sr_err("Invalid device instance, cannot complete scan.");
39fcfdc9
UH
568 return NULL;
569 }
15a5bfe4
LPC
570 sdi->driver = di;
571 }
572
573 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
574
575 return devices;
576}
e66d1892
UH
577
578SR_PRIV int std_opts_config_list(uint32_t key, GVariant **data,
579 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg,
580 const uint32_t scanopts[], size_t scansize, const uint32_t drvopts[],
581 size_t drvsize, const uint32_t devopts[], size_t devsize)
582{
583 switch (key) {
584 case SR_CONF_SCAN_OPTIONS:
585 /* Always return scanopts, regardless of sdi or cg. */
23772462 586 if (!scanopts || scanopts == NO_OPTS)
e66d1892
UH
587 return SR_ERR_ARG;
588 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
589 scanopts, scansize, sizeof(uint32_t));
590 break;
591 case SR_CONF_DEVICE_OPTIONS:
592 if (!sdi) {
593 /* sdi == NULL: return drvopts. */
23772462 594 if (!drvopts || drvopts == NO_OPTS)
e66d1892
UH
595 return SR_ERR_ARG;
596 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
597 drvopts, drvsize, sizeof(uint32_t));
598 } else if (sdi && !cg) {
599 /* sdi != NULL, cg == NULL: return devopts. */
23772462 600 if (!devopts || devopts == NO_OPTS)
e66d1892
UH
601 return SR_ERR_ARG;
602 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
603 devopts, devsize, sizeof(uint32_t));
604 } else {
605 /*
606 * Note: sdi != NULL, cg != NULL is not handled by
607 * this function since it's very driver-specific.
608 */
609 sr_err("%s: %s: sdi/cg != NULL: not handling.",
610 sdi->driver->name, __func__);
611 return SR_ERR_ARG;
612 }
613 break;
614 default:
615 return SR_ERR_NA;
616 }
617
618 return SR_OK;
619}
db944f16 620
58ffcf97 621SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n)
db944f16
UH
622{
623 unsigned int i;
624 GVariant *rational[2];
625 GVariantBuilder gvb;
626
1bb348cd 627 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
db944f16
UH
628
629 for (i = 0; i < n; i++) {
58ffcf97
UH
630 rational[0] = g_variant_new_uint64(a[i][0]);
631 rational[1] = g_variant_new_uint64(a[i][1]);
db944f16
UH
632
633 /* FIXME: Valgrind reports a memory leak here. */
634 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
635 }
636
637 return g_variant_builder_end(&gvb);
638}
639
640SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n)
641{
642 unsigned int i;
643 GVariant *rational[2];
644 GVariantBuilder gvb;
645
1bb348cd 646 g_variant_builder_init(&gvb, G_VARIANT_TYPE_TUPLE);
db944f16
UH
647
648 for (i = 0; i < n; i++) {
649 rational[0] = g_variant_new_uint64(r[i].p);
650 rational[1] = g_variant_new_uint64(r[i].q);
651
652 /* FIXME: Valgrind reports a memory leak here. */
653 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
654 }
655
656 return g_variant_builder_end(&gvb);
657}
463160cb
UH
658
659static GVariant *samplerate_helper(const uint64_t samplerates[], unsigned int n, const char *str)
660{
661 GVariant *gvar;
662 GVariantBuilder gvb;
663
664 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
665 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
666 n, sizeof(uint64_t));
667 g_variant_builder_add(&gvb, "{sv}", str, gvar);
668
669 return g_variant_builder_end(&gvb);
670}
671
672SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n)
673{
674 return samplerate_helper(samplerates, n, "samplerates");
675}
676
677SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n)
678{
679 return samplerate_helper(samplerates, n, "samplerate-steps");
680}
54d471f4
UH
681
682SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step)
683{
684 GVariantBuilder gvb;
685
686 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
687
688 g_variant_builder_add_value(&gvb, g_variant_new_double(min));
689 g_variant_builder_add_value(&gvb, g_variant_new_double(max));
690 g_variant_builder_add_value(&gvb, g_variant_new_double(step));
691
692 return g_variant_builder_end(&gvb);
693}
694
695SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3])
696{
697 unsigned int i;
698 GVariantBuilder gvb;
699
700 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
701
702 for (i = 0; i < 3; i++)
703 g_variant_builder_add_value(&gvb, g_variant_new_double(a[i]));
704
705 return g_variant_builder_end(&gvb);
706}
7bc3cfe6
UH
707
708SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const double max, const double step)
709{
653d087e 710 double d, v;
7bc3cfe6
UH
711 GVariant *gvar, *range[2];
712 GVariantBuilder gvb;
713
714 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
715
716 for (d = min; d <= max; d += step) {
653d087e
SA
717 /*
718 * We will never see exactly 0.0 because of the error we're
719 * accumulating, so catch the "zero" value and force it to be 0.
720 */
721 v = ((d > (-step / 2)) && (d < (step / 2))) ? 0 : d;
722
723 range[0] = g_variant_new_double(v);
724 range[1] = g_variant_new_double(v);
7bc3cfe6
UH
725
726 gvar = g_variant_new_tuple(range, 2);
727 g_variant_builder_add_value(&gvb, gvar);
728 }
729
730 return g_variant_builder_end(&gvb);
731}
105df674 732
a162eeb2
UH
733SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high)
734{
735 GVariant *range[2];
736
737 range[0] = g_variant_new_uint64(low);
738 range[1] = g_variant_new_uint64(high);
739
740 return g_variant_new_tuple(range, 2);
741}
742
43995cda
UH
743SR_PRIV GVariant *std_gvar_tuple_double(double low, double high)
744{
745 GVariant *range[2];
746
747 range[0] = g_variant_new_double(low);
748 range[1] = g_variant_new_double(high);
749
750 return g_variant_new_tuple(range, 2);
751}
752
769561cb 753SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n)
105df674
UH
754{
755 return g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
756 a, n, sizeof(int32_t));
757}
758
769561cb 759SR_PRIV GVariant *std_gvar_array_u32(const uint32_t a[], unsigned int n)
105df674
UH
760{
761 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
762 a, n, sizeof(uint32_t));
763}
764
769561cb 765SR_PRIV GVariant *std_gvar_array_u64(const uint64_t a[], unsigned int n)
105df674
UH
766{
767 return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
768 a, n, sizeof(uint64_t));
769}
9fb9afb5 770
70635036
FS
771SR_PRIV GVariant *std_gvar_array_str(const char *a[], unsigned int n)
772{
773 GVariant *gvar;
774 GVariantBuilder *builder;
775 unsigned int i;
776
777 builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
778
779 for (i = 0; i < n; i++)
780 g_variant_builder_add(builder, "s", a[i]);
781
782 gvar = g_variant_new("as", builder);
783 g_variant_builder_unref(builder);
784
785 return gvar;
786}
787
94e64a0b 788SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n)
9fb9afb5
UH
789{
790 unsigned int i;
791 GVariant *gvar, *range[2];
792 GVariantBuilder gvb;
793
794 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
795
796 for (i = 0; i < n; i++) {
94e64a0b
UH
797 range[0] = g_variant_new_double(a[i][0]);
798 range[1] = g_variant_new_double(a[i][1]);
9fb9afb5
UH
799 gvar = g_variant_new_tuple(range, 2);
800 g_variant_builder_add_value(&gvb, gvar);
801 }
802
803 return g_variant_builder_end(&gvb);
804}
697fb6dd
UH
805
806/* Return the index of 'data' in the array 'arr' (or -1). */
807static int find_in_array(GVariant *data, const GVariantType *type,
808 const void *arr, unsigned int n)
809{
810 const char * const *sarr;
811 const char *s;
812 const uint64_t *u64arr;
813 const uint8_t *u8arr;
814 uint64_t u64;
815 uint8_t u8;
816 unsigned int i;
817
818 if (!g_variant_is_of_type(data, type))
819 return -1;
820
821 switch (g_variant_classify(data)) {
822 case G_VARIANT_CLASS_STRING:
823 s = g_variant_get_string(data, NULL);
824 sarr = arr;
825
826 for (i = 0; i < n; i++)
827 if (!strcmp(s, sarr[i]))
828 return i;
829 break;
830 case G_VARIANT_CLASS_UINT64:
831 u64 = g_variant_get_uint64(data);
832 u64arr = arr;
833
834 for (i = 0; i < n; i++)
835 if (u64 == u64arr[i])
836 return i;
837 break;
838 case G_VARIANT_CLASS_BYTE:
839 u8 = g_variant_get_byte(data);
840 u8arr = arr;
841
842 for (i = 0; i < n; i++)
843 if (u8 == u8arr[i])
844 return i;
845 default:
846 break;
847 }
848
849 return -1;
850}
851
852SR_PRIV int std_str_idx(GVariant *data, const char *a[], unsigned int n)
853{
854 return find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
855}
856
857SR_PRIV int std_u64_idx(GVariant *data, const uint64_t a[], unsigned int n)
858{
859 return find_in_array(data, G_VARIANT_TYPE_UINT64, a, n);
860}
861
862SR_PRIV int std_u8_idx(GVariant *data, const uint8_t a[], unsigned int n)
863{
864 return find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
865}
866
867SR_PRIV int std_str_idx_s(const char *s, const char *a[], unsigned int n)
868{
869 int idx;
870 GVariant *data;
871
872 data = g_variant_new_string(s);
873 idx = find_in_array(data, G_VARIANT_TYPE_STRING, a, n);
874 g_variant_unref(data);
875
876 return idx;
877}
878
879SR_PRIV int std_u8_idx_s(uint8_t b, const uint8_t a[], unsigned int n)
880{
881 int idx;
882 GVariant *data;
883
884 data = g_variant_new_byte(b);
885 idx = find_in_array(data, G_VARIANT_TYPE_BYTE, a, n);
886 g_variant_unref(data);
887
888 return idx;
889}
890
891SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned int n)
892{
893 unsigned int i;
894 uint64_t low, high;
895
896 g_variant_get(data, "(tt)", &low, &high);
897
898 for (i = 0; i < n; i++)
899 if (a[i][0] == low && a[i][1] == high)
900 return i;
901
902 return -1;
903}
904
905SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n)
906{
907 unsigned int i;
908 double low, high;
909
910 g_variant_get(data, "(dd)", &low, &high);
911
912 for (i = 0; i < n; i++)
913 if ((fabs(a[i][0] - low) < 0.1) && ((fabs(a[i][1] - high) < 0.1)))
914 return i;
915
916 return -1;
917}
918
919SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n)
920{
921 unsigned int i;
922
923 for (i = 0; i < n; i++)
924 if (d == a[i][0])
925 return i;
926
927 return -1;
928}
fcd6a8bd
UH
929
930SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
931{
932 unsigned int i;
933
934 for (i = 0; i < n; i++)
935 if (cg == a[i])
936 return i;
937
938 return -1;
939}
c0aa074e
UH
940
941SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
942 int baudrate, int bits, int parity, int stopbits,
943 int flowcontrol, int rts, int dtr)
944{
945 (void)serial;
946 (void)baudrate;
947 (void)bits;
948 (void)parity;
949 (void)stopbits;
950 (void)flowcontrol;
951 (void)rts;
952 (void)dtr;
953
954 return SR_OK;
955}
956