]> sigrok.org Git - libsigrok.git/blame - hardware/colead-slm/api.c
Shorten/simplify hw_dev_list() implementations.
[libsigrok.git] / hardware / colead-slm / api.c
CommitLineData
4d729ddc
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 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 <glib.h>
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
23#include "protocol.h"
a8d09e13
BV
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <string.h>
29
f306ca61
BV
30/* The Colead SL-5868P uses this. */
31#define SERIALCOMM "2400/8n1"
32
a8d09e13 33static const int hwopts[] = {
1953564a
BV
34 SR_CONF_CONN,
35 SR_CONF_SERIALCOMM,
a8d09e13
BV
36 0,
37};
38
39static const int hwcaps[] = {
1953564a
BV
40 SR_CONF_SOUNDLEVELMETER,
41 SR_CONF_LIMIT_SAMPLES,
42 SR_CONF_LIMIT_MSEC,
43 SR_CONF_CONTINUOUS,
a8d09e13
BV
44 0,
45};
46
4d729ddc
BV
47SR_PRIV struct sr_dev_driver colead_slm_driver_info;
48static struct sr_dev_driver *di = &colead_slm_driver_info;
49
50/* Properly close and free all devices. */
51static int clear_instances(void)
52{
53 struct sr_dev_inst *sdi;
54 struct drv_context *drvc;
55 struct dev_context *devc;
56 GSList *l;
57
538ac9a9
BV
58 if (!(drvc = di->priv))
59 return SR_OK;
60
4d729ddc
BV
61 for (l = drvc->instances; l; l = l->next) {
62 if (!(sdi = l->data))
63 continue;
64 if (!(devc = sdi->priv))
65 continue;
a8d09e13 66 sr_serial_dev_inst_free(devc->serial);
4d729ddc
BV
67 sr_dev_inst_free(sdi);
68 }
4d729ddc
BV
69 g_slist_free(drvc->instances);
70 drvc->instances = NULL;
71
72 return SR_OK;
73}
74
34f06b90 75static int hw_init(struct sr_context *sr_ctx)
4d729ddc 76{
063e7aef 77 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
4d729ddc
BV
78}
79
80static GSList *hw_scan(GSList *options)
81{
82 struct drv_context *drvc;
a8d09e13
BV
83 struct dev_context *devc;
84 struct sr_dev_inst *sdi;
1987b8d6 85 struct sr_config *src;
a8d09e13
BV
86 struct sr_probe *probe;
87 GSList *devices, *l;
88 const char *conn, *serialcomm;
4d729ddc 89
4d729ddc
BV
90 drvc = di->priv;
91 drvc->instances = NULL;
92
4b97c74e
UH
93 devices = NULL;
94
a8d09e13
BV
95 conn = serialcomm = NULL;
96 for (l = options; l; l = l->next) {
1987b8d6
BV
97 src = l->data;
98 switch (src->key) {
1953564a 99 case SR_CONF_CONN:
1987b8d6 100 conn = src->value;
a8d09e13 101 break;
1953564a 102 case SR_CONF_SERIALCOMM:
1987b8d6 103 serialcomm = src->value;
a8d09e13
BV
104 break;
105 }
106 }
107 if (!conn)
108 return NULL;
f306ca61
BV
109 if (!serialcomm)
110 serialcomm = SERIALCOMM;
a8d09e13
BV
111
112 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
113 "SL-5868P", NULL)))
114 return NULL;
115
116 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
4b97c74e 117 sr_dbg("Device context malloc failed.");
a8d09e13
BV
118 return NULL;
119 }
120
f306ca61
BV
121 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
122 return NULL;
a8d09e13 123
a8d09e13
BV
124 sdi->priv = devc;
125 sdi->driver = di;
126 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
127 return NULL;
128 sdi->probes = g_slist_append(sdi->probes, probe);
129 drvc->instances = g_slist_append(drvc->instances, sdi);
130 devices = g_slist_append(devices, sdi);
4d729ddc
BV
131
132 return devices;
133}
134
135static GSList *hw_dev_list(void)
136{
0e94d524 137 return ((struct drv_context *)(di->priv))->instances;
4d729ddc
BV
138}
139
140static int hw_dev_open(struct sr_dev_inst *sdi)
141{
a8d09e13
BV
142 struct dev_context *devc;
143
144 if (!(devc = sdi->priv)) {
145 sr_err("sdi->priv was NULL.");
146 return SR_ERR_BUG;
147 }
148
a54dd31e 149 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
a8d09e13 150 return SR_ERR;
f306ca61 151
a8d09e13 152 sdi->status = SR_ST_ACTIVE;
4d729ddc
BV
153
154 return SR_OK;
155}
156
157static int hw_dev_close(struct sr_dev_inst *sdi)
158{
a8d09e13
BV
159 struct dev_context *devc;
160
161 if (!(devc = sdi->priv)) {
162 sr_err("sdi->priv was NULL.");
163 return SR_ERR_BUG;
164 }
165
166 if (devc->serial && devc->serial->fd != -1) {
f306ca61 167 serial_close(devc->serial);
a8d09e13
BV
168 sdi->status = SR_ST_INACTIVE;
169 }
4d729ddc
BV
170
171 return SR_OK;
172}
173
174static int hw_cleanup(void)
175{
176 clear_instances();
177
4d729ddc
BV
178 return SR_OK;
179}
180
035a1078 181static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
4d729ddc 182{
a8d09e13 183 struct dev_context *devc;
4d729ddc 184
a8d09e13 185 if (sdi->status != SR_ST_ACTIVE)
4d729ddc 186 return SR_ERR;
a8d09e13
BV
187
188 if (!(devc = sdi->priv)) {
189 sr_err("sdi->priv was NULL.");
190 return SR_ERR_BUG;
4d729ddc
BV
191 }
192
035a1078 193 switch (id) {
1953564a 194 case SR_CONF_LIMIT_MSEC:
a8d09e13
BV
195 /* TODO: not yet implemented */
196 if (*(const uint64_t *)value == 0) {
197 sr_err("LIMIT_MSEC can't be 0.");
198 return SR_ERR;
199 }
200 devc->limit_msec = *(const uint64_t *)value;
201 sr_dbg("Setting time limit to %" PRIu64 "ms.",
202 devc->limit_msec);
203 break;
1953564a 204 case SR_CONF_LIMIT_SAMPLES:
a8d09e13
BV
205 devc->limit_samples = *(const uint64_t *)value;
206 sr_dbg("Setting sample limit to %" PRIu64 ".",
207 devc->limit_samples);
208 break;
4d729ddc 209 default:
035a1078 210 sr_err("Unknown capability: %d.", id);
a8d09e13
BV
211 return SR_ERR;
212 break;
4d729ddc
BV
213 }
214
a8d09e13 215 return SR_OK;
4d729ddc
BV
216}
217
a1c743fc
BV
218static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
219{
220
221 (void)sdi;
222
223 switch (key) {
0d485e30
BV
224 case SR_CONF_SCAN_OPTIONS:
225 *data = hwopts;
226 break;
9a6517d1
BV
227 case SR_CONF_DEVICE_OPTIONS:
228 *data = hwcaps;
229 break;
a1c743fc
BV
230 default:
231 return SR_ERR_ARG;
232 }
233
234 return SR_OK;
235}
236
4d729ddc
BV
237static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
238 void *cb_data)
239{
a8d09e13
BV
240 struct sr_datafeed_packet packet;
241 struct sr_datafeed_header header;
a8d09e13
BV
242 struct dev_context *devc;
243
244 if (!(devc = sdi->priv)) {
245 sr_err("sdi->priv was NULL.");
246 return SR_ERR_BUG;
247 }
248
249 sr_dbg("Starting acquisition.");
250
251 devc->cb_data = cb_data;
252
253 /* Send header packet to the session bus. */
254 sr_dbg("Sending SR_DF_HEADER.");
255 packet.type = SR_DF_HEADER;
256 packet.payload = (uint8_t *)&header;
257 header.feed_version = 1;
258 gettimeofday(&header.starttime, NULL);
259 sr_session_send(devc->cb_data, &packet);
260
a8d09e13
BV
261 /* Poll every 150ms, or whenever some data comes in. */
262 sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
263 (void *)sdi);
4d729ddc
BV
264
265 return SR_OK;
266}
267
69b07d14 268static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
4d729ddc 269{
a8d09e13
BV
270 struct sr_datafeed_packet packet;
271 struct dev_context *devc;
4d729ddc 272
a8d09e13 273 if (sdi->status != SR_ST_ACTIVE)
4d729ddc 274 return SR_ERR;
a8d09e13
BV
275
276 if (!(devc = sdi->priv)) {
277 sr_err("sdi->priv was NULL.");
278 return SR_ERR_BUG;
4d729ddc
BV
279 }
280
a8d09e13
BV
281 sr_dbg("Stopping acquisition.");
282
283 sr_source_remove(devc->serial->fd);
284 hw_dev_close((struct sr_dev_inst *)sdi);
285
286 /* Send end packet to the session bus. */
287 sr_dbg("Sending SR_DF_END.");
288 packet.type = SR_DF_END;
289 sr_session_send(cb_data, &packet);
4d729ddc
BV
290
291 return SR_OK;
292}
293
294SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
295 .name = "colead-slm",
296 .longname = "Colead SLM",
297 .api_version = 1,
298 .init = hw_init,
299 .cleanup = hw_cleanup,
300 .scan = hw_scan,
301 .dev_list = hw_dev_list,
302 .dev_clear = clear_instances,
035a1078 303 .config_set = config_set,
a1c743fc 304 .config_list = config_list,
4d729ddc
BV
305 .dev_open = hw_dev_open,
306 .dev_close = hw_dev_close,
4d729ddc
BV
307 .dev_acquisition_start = hw_dev_acquisition_start,
308 .dev_acquisition_stop = hw_dev_acquisition_stop,
309 .priv = NULL,
310};