]> sigrok.org Git - libsigrok.git/blame - device.c
sr: change sr_dev_probe_name_set() to use sdi
[libsigrok.git] / device.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
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 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 <stdio.h>
21#include <glib.h>
45c59c8b
BV
22#include "libsigrok.h"
23#include "libsigrok-internal.h"
a1bb33af 24
94799bc4
UH
25/**
26 * Set the name of the specified probe in the specified device.
27 *
28 * If the probe already has a different name assigned to it, it will be
29 * removed, and the new name will be saved instead.
30 *
37e8b4c4 31 * @param sdi The device instance the probe is connected to.
94799bc4 32 * @param probenum The number of the probe whose name to set.
37e8b4c4
BV
33 * Note that the probe numbers start at 0.
34 * @param name The new name that the specified probe should get. A copy
35 * of the string is made.
0e3b1439 36 *
37e8b4c4 37 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
94799bc4 38 */
37e8b4c4
BV
39SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
40 int probenum, const char *name)
a1bb33af 41{
37e8b4c4
BV
42 GSList *l;
43 struct sr_probe *probe;
44 int ret;
a1bb33af 45
37e8b4c4
BV
46 if (!sdi) {
47 sr_err("%s: sdi was NULL", __func__);
0e3b1439 48 return SR_ERR_ARG;
94799bc4
UH
49 }
50
37e8b4c4
BV
51 ret = SR_ERR_ARG;
52 for (l = sdi->probes; l; l = l->next) {
53 probe = l->data;
54 if (probe->index == probenum) {
55 g_free(probe->name);
56 probe->name = g_strdup(name);
57 ret = SR_OK;
58 break;
59 }
94799bc4
UH
60 }
61
37e8b4c4 62 return ret;
a1bb33af
UH
63}
64
be5bf44d
BV
65/**
66 * Enable or disable a probe on the specified device.
67 *
68 * @param sdi The device instance the probe is connected to.
69 * @param probenum The probe number, starting from 0.
70 * @param state TRUE to enable the probe, FALSE to disable.
71 *
72 * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
73 */
74SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
75 gboolean state)
76{
77 GSList *l;
78 struct sr_probe *probe;
79 int ret;
80
81 if (!sdi)
82 return SR_ERR_ARG;
83
84 ret = SR_ERR_ARG;
85 for (l = sdi->probes; l; l = l->next) {
86 probe = l->data;
87 if (probe->index == probenum) {
88 probe->enabled = state;
89 ret = SR_OK;
90 break;
91 }
92 }
93
94 return ret;
95}
96
94799bc4 97/**
01c3e9db
UH
98 * Add a trigger to the specified device (and the specified probe).
99 *
100 * If the specified probe of this device already has a trigger, it will
101 * be silently replaced.
94799bc4 102 *
c7ee3ddb
BV
103 * @param sdi Must not be NULL.
104 * @param probenum The number of the probe.
94799bc4 105 * Note that the probe numbers start at 1 (not 0!).
c7ee3ddb 106 * @param trigger trigger string, in the format used by sigrok-cli
0e3b1439 107 *
c7ee3ddb 108 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
94799bc4 109 */
58453e58
BV
110SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
111 const char *trigger)
a1bb33af 112{
58453e58
BV
113 GSList *l;
114 struct sr_probe *probe;
115 int ret;
a1bb33af 116
58453e58 117 if (!sdi)
0e3b1439 118 return SR_ERR_ARG;
94799bc4 119
58453e58
BV
120 ret = SR_ERR_ARG;
121 for (l = sdi->probes; l; l = l->next) {
122 probe = l->data;
123 if (probe->index == probenum) {
124 /* If the probe already has a trigger, kill it first. */
125 g_free(probe->trigger);
126 probe->trigger = g_strdup(trigger);
127 ret = SR_OK;
128 break;
129 }
94799bc4 130 }
a1bb33af 131
58453e58 132 return ret;
7d658874
BV
133}
134
94799bc4
UH
135/**
136 * Determine whether the specified device has the specified capability.
137 *
a5b35a16 138 * @param dev Pointer to the device instance to be checked. Must not be NULL.
8ec95d22
UH
139 * If the device's 'driver' field is NULL (virtual device), this
140 * function will always return FALSE (virtual devices don't have
141 * a hardware capabilities list).
94799bc4
UH
142 * @param hwcap The capability that should be checked (whether it's supported
143 * by the specified device).
144 *
145 * @return TRUE, if the device has the specified capability, FALSE otherwise.
146 * FALSE is also returned upon invalid input parameters or other
147 * error conditions.
148 */
a5b35a16 149SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
7d658874 150{
915f7cc8
JH
151 const int *hwcaps;
152 int i;
7d658874 153
a5b35a16 154 if (!sdi || !sdi->driver)
8ec95d22 155 return FALSE;
94799bc4 156
a5b35a16
BV
157 if (sdi->driver->info_get(SR_DI_HWCAPS,
158 (const void **)&hwcaps, NULL) != SR_OK)
8ec95d22 159 return FALSE;
94799bc4 160
ffedd0bf 161 for (i = 0; hwcaps[i]; i++) {
a5b35a16
BV
162 if (hwcaps[i] == hwcap)
163 return TRUE;
94799bc4 164 }
218557b8 165
7d658874 166 return FALSE;
a1bb33af 167}
fd9836bf 168