]> sigrok.org Git - libsigrok.git/blame - hardware/tondaj-sl-814/api.c
Add a struct sr_context * parameter to hw_init()
[libsigrok.git] / hardware / tondaj-sl-814 / api.c
CommitLineData
aa2af324
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
c073af80 21#include <fcntl.h>
aa2af324
UH
22#include <glib.h>
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
25#include "protocol.h"
26
19ee7dff
BV
27#define SERIALCOMM "9600/8e1"
28
c073af80
UH
29static const int hwopts[] = {
30 SR_HWOPT_CONN,
31 SR_HWOPT_SERIALCOMM,
32 0,
33};
34
35static const int hwcaps[] = {
36 SR_HWCAP_SOUNDLEVELMETER,
37 SR_HWCAP_LIMIT_SAMPLES,
38 SR_HWCAP_CONTINUOUS,
39 0,
40};
41
42static const char *probe_names[] = {
43 "P1",
44 NULL,
45};
46
aa2af324
UH
47SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info;
48static struct sr_dev_driver *di = &tondaj_sl_814_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
58 if (!(drvc = di->priv))
59 return SR_OK;
60
61 for (l = drvc->instances; l; l = l->next) {
62 if (!(sdi = l->data))
63 continue;
64 if (!(devc = sdi->priv))
65 continue;
c073af80 66 sr_serial_dev_inst_free(devc->serial);
aa2af324
UH
67 sr_dev_inst_free(sdi);
68 }
69
70 g_slist_free(drvc->instances);
71 drvc->instances = NULL;
72
73 return SR_OK;
74}
75
34f06b90 76static int hw_init(struct sr_context *sr_ctx)
aa2af324
UH
77{
78 struct drv_context *drvc;
79
80 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
81 sr_err("Driver context malloc failed.");
82 return SR_ERR_MALLOC;
83 }
84
aa2af324
UH
85 di->priv = drvc;
86
87 return SR_OK;
88}
89
90static GSList *hw_scan(GSList *options)
91{
92 struct drv_context *drvc;
c073af80
UH
93 struct dev_context *devc;
94 struct sr_dev_inst *sdi;
95 struct sr_hwopt *opt;
96 struct sr_probe *probe;
97 GSList *devices, *l;
98 const char *conn, *serialcomm;
aa2af324
UH
99
100 devices = NULL;
101 drvc = di->priv;
102 drvc->instances = NULL;
103
c073af80
UH
104 conn = serialcomm = NULL;
105 for (l = options; l; l = l->next) {
106 if (!(opt = l->data)) {
107 sr_err("Invalid option data, skipping.");
108 continue;
109 }
110 switch (opt->hwopt) {
111 case SR_HWOPT_CONN:
112 conn = opt->value;
113 break;
114 case SR_HWOPT_SERIALCOMM:
115 serialcomm = opt->value;
116 break;
117 default:
118 sr_err("Unknown option %d, skipping.", opt->hwopt);
119 break;
120 }
121 }
122 if (!conn) {
123 sr_dbg("Couldn't determine connection options.");
124 return NULL;
125 }
19ee7dff
BV
126 if (!serialcomm)
127 serialcomm = SERIALCOMM;
c073af80
UH
128
129 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Tondaj",
130 "SL-814", NULL))) {
131 sr_err("Failed to create device instance.");
132 return NULL;
133 }
134
135 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
136 sr_err("Device context malloc failed.");
137 return NULL;
138 }
139
19ee7dff
BV
140 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
141 return NULL;
c073af80 142
a54dd31e 143 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
c073af80 144 return NULL;
19ee7dff 145
c073af80
UH
146 sdi->priv = devc;
147 sdi->driver = di;
148 probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probe_names[0]);
149 if (!probe) {
150 sr_err("Failed to create probe.");
151 return NULL;
152 }
153 sdi->probes = g_slist_append(sdi->probes, probe);
154 drvc->instances = g_slist_append(drvc->instances, sdi);
155 devices = g_slist_append(devices, sdi);
aa2af324
UH
156
157 return devices;
158}
159
160static GSList *hw_dev_list(void)
161{
162 struct drv_context *drvc;
163
164 drvc = di->priv;
165
166 return drvc->instances;
167}
168
169static int hw_dev_open(struct sr_dev_inst *sdi)
170{
c073af80
UH
171 struct dev_context *devc;
172
173 devc = sdi->priv;
174
a54dd31e 175 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
c073af80 176 return SR_ERR;
c073af80
UH
177
178 sdi->status = SR_ST_ACTIVE;
aa2af324
UH
179
180 return SR_OK;
181}
182
183static int hw_dev_close(struct sr_dev_inst *sdi)
184{
c073af80
UH
185 struct dev_context *devc;
186
187 devc = sdi->priv;
188
19ee7dff
BV
189 if (devc->serial && devc->serial->fd != -1) {
190 serial_close(devc->serial);
191 sdi->status = SR_ST_INACTIVE;
c073af80
UH
192 }
193
aa2af324
UH
194 return SR_OK;
195}
196
197static int hw_cleanup(void)
198{
199 clear_instances();
200
aa2af324
UH
201 return SR_OK;
202}
203
204static int hw_info_get(int info_id, const void **data,
205 const struct sr_dev_inst *sdi)
206{
c073af80
UH
207 (void)sdi;
208
aa2af324 209 switch (info_id) {
c073af80
UH
210 case SR_DI_HWOPTS:
211 *data = hwopts;
212 break;
213 case SR_DI_HWCAPS:
214 *data = hwcaps;
215 break;
216 case SR_DI_NUM_PROBES:
217 *data = GINT_TO_POINTER(1);
218 break;
219 case SR_DI_PROBE_NAMES:
220 *data = probe_names;
221 break;
aa2af324
UH
222 default:
223 sr_err("Unknown info_id: %d.", info_id);
224 return SR_ERR_ARG;
225 }
226
227 return SR_OK;
228}
229
230static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
231 const void *value)
232{
c073af80 233 struct dev_context *devc;
aa2af324
UH
234
235 if (sdi->status != SR_ST_ACTIVE) {
236 sr_err("Device inactive, can't set config options.");
237 return SR_ERR;
238 }
239
c073af80
UH
240 devc = sdi->priv;
241
aa2af324 242 switch (hwcap) {
c073af80
UH
243 case SR_HWCAP_LIMIT_SAMPLES:
244 devc->limit_samples = *(const uint64_t *)value;
245 sr_dbg("Setting sample limit to %" PRIu64 ".",
246 devc->limit_samples);
247 break;
aa2af324
UH
248 default:
249 sr_err("Unknown hardware capability: %d.", hwcap);
c073af80 250 return SR_ERR_ARG;
aa2af324
UH
251 }
252
c073af80 253 return SR_OK;
aa2af324
UH
254}
255
256static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
257 void *cb_data)
258{
c073af80
UH
259 struct sr_datafeed_packet packet;
260 struct sr_datafeed_header header;
261 struct sr_datafeed_meta_analog meta;
262 struct dev_context *devc;
263
264 devc = sdi->priv;
265 devc->cb_data = cb_data;
266
267 /* Send header packet to the session bus. */
268 sr_dbg("Sending SR_DF_HEADER.");
269 packet.type = SR_DF_HEADER;
270 packet.payload = (uint8_t *)&header;
271 header.feed_version = 1;
272 gettimeofday(&header.starttime, NULL);
273 sr_session_send(devc->cb_data, &packet);
274
275 /* Send metadata about the SR_DF_ANALOG packets to come. */
276 sr_dbg("Sending SR_DF_META_ANALOG.");
277 packet.type = SR_DF_META_ANALOG;
278 packet.payload = &meta;
279 meta.num_probes = 1;
280 sr_session_send(devc->cb_data, &packet);
281
282 /* Poll every 500ms, or whenever some data comes in. */
283 sr_source_add(devc->serial->fd, G_IO_IN, 500,
284 tondaj_sl_814_receive_data, (void *)sdi);
aa2af324
UH
285
286 return SR_OK;
287}
288
c073af80 289static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
aa2af324 290{
c073af80
UH
291 int ret, final_ret;
292 struct sr_datafeed_packet packet;
293 struct dev_context *devc;
294
295 final_ret = SR_OK;
aa2af324
UH
296
297 if (sdi->status != SR_ST_ACTIVE) {
298 sr_err("Device inactive, can't stop acquisition.");
299 return SR_ERR;
300 }
301
c073af80 302 devc = sdi->priv;
aa2af324 303
c073af80
UH
304 if ((ret = sr_source_remove(devc->serial->fd)) < 0) {
305 sr_err("Error removing source: %d.", ret);
306 final_ret = SR_ERR;
307 }
308
309 if ((ret = hw_dev_close(sdi)) < 0) {
310 sr_err("Error closing device: %d.", ret);
311 final_ret = SR_ERR;
312 }
313
314 /* Send end packet to the session bus. */
315 sr_dbg("Sending SR_DF_END.");
316 packet.type = SR_DF_END;
317 sr_session_send(cb_data, &packet);
318
319 return final_ret;
aa2af324
UH
320}
321
322SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info = {
323 .name = "tondaj-sl-814",
324 .longname = "Tondaj SL-814",
325 .api_version = 1,
326 .init = hw_init,
327 .cleanup = hw_cleanup,
328 .scan = hw_scan,
329 .dev_list = hw_dev_list,
330 .dev_clear = clear_instances,
331 .dev_open = hw_dev_open,
332 .dev_close = hw_dev_close,
333 .info_get = hw_info_get,
334 .dev_config_set = hw_dev_config_set,
335 .dev_acquisition_start = hw_dev_acquisition_start,
336 .dev_acquisition_stop = hw_dev_acquisition_stop,
337 .priv = NULL,
338};