]> sigrok.org Git - libsigrok.git/blob - device.c
sr: remove dead/obsolete code
[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 dev TODO
32  * @param probenum The number of the probe whose name to set.
33  *                 Note that the probe numbers start at 1 (not 0!).
34  * @param name The new name that the specified probe should get.
35  *
36  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR
37  *         upon other errors.
38  *         If something other than SR_OK is returned, 'dev' is unchanged.
39  */
40 SR_API int sr_dev_probe_name_set(struct sr_dev *dev, int probenum,
41                                  const char *name)
42 {
43         struct sr_probe *p;
44
45         if (!dev) {
46                 sr_err("dev: %s: dev was NULL", __func__);
47                 return SR_ERR_ARG;
48         }
49
50         p = sr_dev_probe_find(dev, probenum);
51         if (!p) {
52                 sr_err("dev: %s: probe %d not found", __func__, probenum);
53                 return SR_ERR; /* TODO: More specific error? */
54         }
55
56         /* TODO: Sanity check on 'name'. */
57
58         /* If the probe already has a name, kill it first. */
59         g_free(p->name);
60
61         p->name = g_strdup(name);
62
63         return SR_OK;
64 }
65
66 /**
67  * Enable or disable a probe on the specified device.
68  *
69  * @param sdi The device instance the probe is connected to.
70  * @param probenum The probe number, starting from 0.
71  * @param state TRUE to enable the probe, FALSE to disable.
72  *
73  * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
74  */
75 SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
76                 gboolean state)
77 {
78         GSList *l;
79         struct sr_probe *probe;
80         int ret;
81
82         if (!sdi)
83                 return SR_ERR_ARG;
84
85         ret = SR_ERR_ARG;
86         for (l = sdi->probes; l; l = l->next) {
87                 probe = l->data;
88                 if (probe->index == probenum) {
89                         probe->enabled = state;
90                         ret = SR_OK;
91                         break;
92                 }
93         }
94
95         return ret;
96 }
97
98 /**
99  * Add a trigger to the specified device (and the specified probe).
100  *
101  * If the specified probe of this device already has a trigger, it will
102  * be silently replaced.
103  *
104  * @param sdi Must not be NULL.
105  * @param probenum The number of the probe.
106  *                 Note that the probe numbers start at 1 (not 0!).
107  * @param trigger trigger string, in the format used by sigrok-cli
108  *
109  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
110  */
111 SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
112                 const char *trigger)
113 {
114         GSList *l;
115         struct sr_probe *probe;
116         int ret;
117
118         if (!sdi)
119                 return SR_ERR_ARG;
120
121         ret = SR_ERR_ARG;
122         for (l = sdi->probes; l; l = l->next) {
123                 probe = l->data;
124                 if (probe->index == probenum) {
125                         /* If the probe already has a trigger, kill it first. */
126                         g_free(probe->trigger);
127                         probe->trigger = g_strdup(trigger);
128                         ret = SR_OK;
129                         break;
130                 }
131         }
132
133         return ret;
134 }
135
136 /**
137  * Determine whether the specified device has the specified capability.
138  *
139  * @param dev Pointer to the device instance to be checked. Must not be NULL.
140  *            If the device's 'driver' field is NULL (virtual device), this
141  *            function will always return FALSE (virtual devices don't have
142  *            a hardware capabilities list).
143  * @param hwcap The capability that should be checked (whether it's supported
144  *              by the specified device).
145  *
146  * @return TRUE, if the device has the specified capability, FALSE otherwise.
147  *         FALSE is also returned upon invalid input parameters or other
148  *         error conditions.
149  */
150 SR_API gboolean sr_dev_has_hwcap(const struct sr_dev_inst *sdi, int hwcap)
151 {
152         const int *hwcaps;
153         int i;
154
155         if (!sdi || !sdi->driver)
156                 return FALSE;
157
158         if (sdi->driver->info_get(SR_DI_HWCAPS,
159                         (const void **)&hwcaps, NULL) != SR_OK)
160                 return FALSE;
161
162         for (i = 0; hwcaps[i]; i++) {
163                 if (hwcaps[i] == hwcap)
164                         return TRUE;
165         }
166
167         return FALSE;
168 }
169