]> sigrok.org Git - libsigrok.git/blob - device.c
sr: change sr_dev_probe_name_set() to use sdi
[libsigrok.git] / device.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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>
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
24
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  *
31  * @param sdi The device instance the probe is connected to.
32  * @param probenum The number of the probe whose name to set.
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.
36  *
37  * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
38  */
39 SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
40                 int probenum, const char *name)
41 {
42         GSList *l;
43         struct sr_probe *probe;
44         int ret;
45
46         if (!sdi) {
47                 sr_err("%s: sdi was NULL", __func__);
48                 return SR_ERR_ARG;
49         }
50
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                 }
60         }
61
62         return ret;
63 }
64
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  */
74 SR_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
97 /**
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.
102  *
103  * @param sdi Must not be NULL.
104  * @param probenum The number of the probe.
105  *                 Note that the probe numbers start at 1 (not 0!).
106  * @param trigger trigger string, in the format used by sigrok-cli
107  *
108  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
109  */
110 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
111                 const char *trigger)
112 {
113         GSList *l;
114         struct sr_probe *probe;
115         int ret;
116
117         if (!sdi)
118                 return SR_ERR_ARG;
119
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                 }
130         }
131
132         return ret;
133 }
134
135 /**
136  * Determine whether the specified device has the specified capability.
137  *
138  * @param dev Pointer to the device instance to be checked. Must not be NULL.
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).
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  */
149 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
150 {
151         const int *hwcaps;
152         int i;
153
154         if (!sdi || !sdi->driver)
155                 return FALSE;
156
157         if (sdi->driver->info_get(SR_DI_HWCAPS,
158                         (const void **)&hwcaps, NULL) != SR_OK)
159                 return FALSE;
160
161         for (i = 0; hwcaps[i]; i++) {
162                 if (hwcaps[i] == hwcap)
163                         return TRUE;
164         }
165
166         return FALSE;
167 }
168