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 static const int hwopts[] = {
36 static const int hwcaps[] = {
37 SR_HWCAP_SOUNDLEVELMETER,
38 SR_HWCAP_LIMIT_SAMPLES,
44 static const char *probe_names[] = {
49 SR_PRIV struct sr_dev_driver colead_slm_driver_info;
50 static struct sr_dev_driver *di = &colead_slm_driver_info;
52 /* Properly close and free all devices. */
53 static int clear_instances(void)
55 struct sr_dev_inst *sdi;
56 struct drv_context *drvc;
57 struct dev_context *devc;
60 if (!(drvc = di->priv))
63 for (l = drvc->instances; l; l = l->next) {
66 if (!(devc = sdi->priv))
68 sr_serial_dev_inst_free(devc->serial);
69 sr_dev_inst_free(sdi);
71 g_slist_free(drvc->instances);
72 drvc->instances = NULL;
77 static int hw_init(void)
79 struct drv_context *drvc;
81 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
82 sr_err("Driver context malloc failed.");
91 static GSList *hw_scan(GSList *options)
93 struct drv_context *drvc;
94 struct dev_context *devc;
95 struct sr_dev_inst *sdi;
97 struct sr_probe *probe;
99 const char *conn, *serialcomm;
103 drvc->instances = NULL;
105 conn = serialcomm = NULL;
106 for (l = options; l; l = l->next) {
108 switch (opt->hwopt) {
112 case SR_HWOPT_SERIALCOMM:
113 serialcomm = opt->value;
120 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
124 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
125 sr_dbg("failed to malloc devc");
130 /* The Colead SL-5868P uses this. */
131 serialcomm = "2400/8n1";
133 devc->serial = sr_serial_dev_inst_new(conn, -1);
134 devc->serialcomm = g_strdup(serialcomm);
137 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
139 sdi->probes = g_slist_append(sdi->probes, probe);
140 drvc->instances = g_slist_append(drvc->instances, sdi);
141 devices = g_slist_append(devices, sdi);
146 static GSList *hw_dev_list(void)
148 struct drv_context *drvc;
152 return drvc->instances;
155 static int hw_dev_open(struct sr_dev_inst *sdi)
157 struct dev_context *devc;
159 if (!(devc = sdi->priv)) {
160 sr_err("sdi->priv was NULL.");
164 sr_dbg("opening %s with %s", devc->serial->port, devc->serialcomm);
165 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
166 if (devc->serial->fd == -1) {
167 sr_err("Couldn't open serial port '%s'.",
171 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
172 sr_err("Unable to set serial parameters.");
175 sdi->status = SR_ST_ACTIVE;
180 static int hw_dev_close(struct sr_dev_inst *sdi)
182 struct dev_context *devc;
184 if (!(devc = sdi->priv)) {
185 sr_err("sdi->priv was NULL.");
189 if (devc->serial && devc->serial->fd != -1) {
190 serial_close(devc->serial->fd);
191 devc->serial->fd = -1;
192 sdi->status = SR_ST_INACTIVE;
198 static int hw_cleanup(void)
205 static int hw_info_get(int info_id, const void **data,
206 const struct sr_dev_inst *sdi)
218 case SR_DI_NUM_PROBES:
219 *data = GINT_TO_POINTER(1);
221 case SR_DI_PROBE_NAMES:
231 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
234 struct dev_context *devc;
236 if (sdi->status != SR_ST_ACTIVE)
239 if (!(devc = sdi->priv)) {
240 sr_err("sdi->priv was NULL.");
245 case SR_HWCAP_LIMIT_MSEC:
246 /* TODO: not yet implemented */
247 if (*(const uint64_t *)value == 0) {
248 sr_err("LIMIT_MSEC can't be 0.");
251 devc->limit_msec = *(const uint64_t *)value;
252 sr_dbg("Setting time limit to %" PRIu64 "ms.",
255 case SR_HWCAP_LIMIT_SAMPLES:
256 devc->limit_samples = *(const uint64_t *)value;
257 sr_dbg("Setting sample limit to %" PRIu64 ".",
258 devc->limit_samples);
261 sr_err("Unknown capability: %d.", hwcap);
269 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
272 struct sr_datafeed_packet packet;
273 struct sr_datafeed_header header;
274 struct sr_datafeed_meta_analog meta;
275 struct dev_context *devc;
277 if (!(devc = sdi->priv)) {
278 sr_err("sdi->priv was NULL.");
282 sr_dbg("Starting acquisition.");
284 devc->cb_data = cb_data;
286 /* Send header packet to the session bus. */
287 sr_dbg("Sending SR_DF_HEADER.");
288 packet.type = SR_DF_HEADER;
289 packet.payload = (uint8_t *)&header;
290 header.feed_version = 1;
291 gettimeofday(&header.starttime, NULL);
292 sr_session_send(devc->cb_data, &packet);
294 /* Send metadata about the SR_DF_ANALOG packets to come. */
295 sr_dbg("Sending SR_DF_META_ANALOG.");
296 packet.type = SR_DF_META_ANALOG;
297 packet.payload = &meta;
299 sr_session_send(devc->cb_data, &packet);
301 /* Poll every 150ms, or whenever some data comes in. */
302 sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
308 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
311 struct sr_datafeed_packet packet;
312 struct dev_context *devc;
314 if (sdi->status != SR_ST_ACTIVE)
317 if (!(devc = sdi->priv)) {
318 sr_err("sdi->priv was NULL.");
322 sr_dbg("Stopping acquisition.");
324 sr_source_remove(devc->serial->fd);
325 hw_dev_close((struct sr_dev_inst *)sdi);
327 /* Send end packet to the session bus. */
328 sr_dbg("Sending SR_DF_END.");
329 packet.type = SR_DF_END;
330 sr_session_send(cb_data, &packet);
335 SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
336 .name = "colead-slm",
337 .longname = "Colead SLM",
340 .cleanup = hw_cleanup,
342 .dev_list = hw_dev_list,
343 .dev_clear = clear_instances,
344 .dev_open = hw_dev_open,
345 .dev_close = hw_dev_close,
346 .info_get = hw_info_get,
347 .dev_config_set = hw_dev_config_set,
348 .dev_acquisition_start = hw_dev_acquisition_start,
349 .dev_acquisition_stop = hw_dev_acquisition_stop,