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