2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
24 #include <sys/types.h>
30 /* The Colead SL-5868P uses this. */
31 #define SERIALCOMM "2400/8n1"
33 static const int hwopts[] = {
39 static const int hwcaps[] = {
40 SR_HWCAP_SOUNDLEVELMETER,
41 SR_HWCAP_LIMIT_SAMPLES,
47 static const char *probe_names[] = {
52 SR_PRIV struct sr_dev_driver colead_slm_driver_info;
53 static struct sr_dev_driver *di = &colead_slm_driver_info;
55 /* Properly close and free all devices. */
56 static int clear_instances(void)
58 struct sr_dev_inst *sdi;
59 struct drv_context *drvc;
60 struct dev_context *devc;
63 if (!(drvc = di->priv))
66 for (l = drvc->instances; l; l = l->next) {
69 if (!(devc = sdi->priv))
71 sr_serial_dev_inst_free(devc->serial);
72 sr_dev_inst_free(sdi);
74 g_slist_free(drvc->instances);
75 drvc->instances = NULL;
80 static int hw_init(void)
82 struct drv_context *drvc;
84 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
85 sr_err("Driver context malloc failed.");
94 static GSList *hw_scan(GSList *options)
96 struct drv_context *drvc;
97 struct dev_context *devc;
98 struct sr_dev_inst *sdi;
100 struct sr_probe *probe;
102 const char *conn, *serialcomm;
106 drvc->instances = NULL;
108 conn = serialcomm = NULL;
109 for (l = options; l; l = l->next) {
111 switch (opt->hwopt) {
115 case SR_HWOPT_SERIALCOMM:
116 serialcomm = opt->value;
123 serialcomm = SERIALCOMM;
125 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
129 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
130 sr_dbg("failed to malloc devc");
134 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
139 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
141 sdi->probes = g_slist_append(sdi->probes, probe);
142 drvc->instances = g_slist_append(drvc->instances, sdi);
143 devices = g_slist_append(devices, sdi);
148 static GSList *hw_dev_list(void)
150 struct drv_context *drvc;
154 return drvc->instances;
157 static int hw_dev_open(struct sr_dev_inst *sdi)
159 struct dev_context *devc;
161 if (!(devc = sdi->priv)) {
162 sr_err("sdi->priv was NULL.");
166 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
169 sdi->status = SR_ST_ACTIVE;
174 static int hw_dev_close(struct sr_dev_inst *sdi)
176 struct dev_context *devc;
178 if (!(devc = sdi->priv)) {
179 sr_err("sdi->priv was NULL.");
183 if (devc->serial && devc->serial->fd != -1) {
184 serial_close(devc->serial);
185 sdi->status = SR_ST_INACTIVE;
191 static int hw_cleanup(void)
198 static int hw_info_get(int info_id, const void **data,
199 const struct sr_dev_inst *sdi)
211 case SR_DI_NUM_PROBES:
212 *data = GINT_TO_POINTER(1);
214 case SR_DI_PROBE_NAMES:
224 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
227 struct dev_context *devc;
229 if (sdi->status != SR_ST_ACTIVE)
232 if (!(devc = sdi->priv)) {
233 sr_err("sdi->priv was NULL.");
238 case SR_HWCAP_LIMIT_MSEC:
239 /* TODO: not yet implemented */
240 if (*(const uint64_t *)value == 0) {
241 sr_err("LIMIT_MSEC can't be 0.");
244 devc->limit_msec = *(const uint64_t *)value;
245 sr_dbg("Setting time limit to %" PRIu64 "ms.",
248 case SR_HWCAP_LIMIT_SAMPLES:
249 devc->limit_samples = *(const uint64_t *)value;
250 sr_dbg("Setting sample limit to %" PRIu64 ".",
251 devc->limit_samples);
254 sr_err("Unknown capability: %d.", hwcap);
262 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
265 struct sr_datafeed_packet packet;
266 struct sr_datafeed_header header;
267 struct sr_datafeed_meta_analog meta;
268 struct dev_context *devc;
270 if (!(devc = sdi->priv)) {
271 sr_err("sdi->priv was NULL.");
275 sr_dbg("Starting acquisition.");
277 devc->cb_data = cb_data;
279 /* Send header packet to the session bus. */
280 sr_dbg("Sending SR_DF_HEADER.");
281 packet.type = SR_DF_HEADER;
282 packet.payload = (uint8_t *)&header;
283 header.feed_version = 1;
284 gettimeofday(&header.starttime, NULL);
285 sr_session_send(devc->cb_data, &packet);
287 /* Send metadata about the SR_DF_ANALOG packets to come. */
288 sr_dbg("Sending SR_DF_META_ANALOG.");
289 packet.type = SR_DF_META_ANALOG;
290 packet.payload = &meta;
292 sr_session_send(devc->cb_data, &packet);
294 /* Poll every 150ms, or whenever some data comes in. */
295 sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
301 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
303 struct sr_datafeed_packet packet;
304 struct dev_context *devc;
306 if (sdi->status != SR_ST_ACTIVE)
309 if (!(devc = sdi->priv)) {
310 sr_err("sdi->priv was NULL.");
314 sr_dbg("Stopping acquisition.");
316 sr_source_remove(devc->serial->fd);
317 hw_dev_close((struct sr_dev_inst *)sdi);
319 /* Send end packet to the session bus. */
320 sr_dbg("Sending SR_DF_END.");
321 packet.type = SR_DF_END;
322 sr_session_send(cb_data, &packet);
327 SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
328 .name = "colead-slm",
329 .longname = "Colead SLM",
332 .cleanup = hw_cleanup,
334 .dev_list = hw_dev_list,
335 .dev_clear = clear_instances,
336 .dev_open = hw_dev_open,
337 .dev_close = hw_dev_close,
338 .info_get = hw_info_get,
339 .dev_config_set = hw_dev_config_set,
340 .dev_acquisition_start = hw_dev_acquisition_start,
341 .dev_acquisition_stop = hw_dev_acquisition_stop,