]> sigrok.org Git - libsigrok.git/blob - device.c
sr: comments/docs
[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 probe number, starting from 0.
105  * @param trigger Trigger string, in the format used by sigrok-cli
106  *
107  * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
108  */
109 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
110                 const char *trigger)
111 {
112         GSList *l;
113         struct sr_probe *probe;
114         int ret;
115
116         if (!sdi)
117                 return SR_ERR_ARG;
118
119         ret = SR_ERR_ARG;
120         for (l = sdi->probes; l; l = l->next) {
121                 probe = l->data;
122                 if (probe->index == probenum) {
123                         /* If the probe already has a trigger, kill it first. */
124                         g_free(probe->trigger);
125                         probe->trigger = g_strdup(trigger);
126                         ret = SR_OK;
127                         break;
128                 }
129         }
130
131         return ret;
132 }
133
134 /**
135  * Determine whether the specified device has the specified capability.
136  *
137  * @param dev Pointer to the device instance to be checked. Must not be NULL.
138  *            If the device's 'driver' field is NULL (virtual device), this
139  *            function will always return FALSE (virtual devices don't have
140  *            a hardware capabilities list).
141  * @param hwcap The capability that should be checked (whether it's supported
142  *              by the specified device).
143  *
144  * @return TRUE if the device has the specified capability, FALSE otherwise.
145  *         FALSE is also returned upon invalid input parameters or other
146  *         error conditions.
147  */
148 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
149 {
150         const int *hwcaps;
151         int i;
152
153         if (!sdi || !sdi->driver)
154                 return FALSE;
155
156         if (sdi->driver->info_get(SR_DI_HWCAPS,
157                         (const void **)&hwcaps, NULL) != SR_OK)
158                 return FALSE;
159
160         for (i = 0; hwcaps[i]; i++) {
161                 if (hwcaps[i] == hwcap)
162                         return TRUE;
163         }
164
165         return FALSE;
166 }
167