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;
61 for (l = drvc->instances; l; l = l->next) {
64 if (!(devc = sdi->priv))
66 sr_serial_dev_inst_free(devc->serial);
67 sr_dev_inst_free(sdi);
69 g_slist_free(drvc->instances);
70 drvc->instances = NULL;
75 static int hw_init(void)
77 struct drv_context *drvc;
79 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
80 sr_err("Driver context malloc failed.");
89 static GSList *hw_scan(GSList *options)
91 struct drv_context *drvc;
92 struct dev_context *devc;
93 struct sr_dev_inst *sdi;
95 struct sr_probe *probe;
97 const char *conn, *serialcomm;
101 drvc->instances = NULL;
103 conn = serialcomm = NULL;
104 for (l = options; l; l = l->next) {
106 switch (opt->hwopt) {
110 case SR_HWOPT_SERIALCOMM:
111 serialcomm = opt->value;
118 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
122 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
123 sr_dbg("failed to malloc devc");
128 /* The Colead SL-5868P uses this. */
129 serialcomm = "2400/8n1";
131 devc->serial = sr_serial_dev_inst_new(conn, -1);
132 devc->serialcomm = g_strdup(serialcomm);
135 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
137 sdi->probes = g_slist_append(sdi->probes, probe);
138 drvc->instances = g_slist_append(drvc->instances, sdi);
139 devices = g_slist_append(devices, sdi);
144 static GSList *hw_dev_list(void)
146 struct drv_context *drvc;
150 return drvc->instances;
153 static int hw_dev_open(struct sr_dev_inst *sdi)
155 struct dev_context *devc;
157 if (!(devc = sdi->priv)) {
158 sr_err("sdi->priv was NULL.");
162 sr_dbg("opening %s with %s", devc->serial->port, devc->serialcomm);
163 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
164 if (devc->serial->fd == -1) {
165 sr_err("Couldn't open serial port '%s'.",
169 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
170 sr_err("Unable to set serial parameters.");
173 sdi->status = SR_ST_ACTIVE;
178 static int hw_dev_close(struct sr_dev_inst *sdi)
180 struct dev_context *devc;
182 if (!(devc = sdi->priv)) {
183 sr_err("sdi->priv was NULL.");
187 if (devc->serial && devc->serial->fd != -1) {
188 serial_close(devc->serial->fd);
189 devc->serial->fd = -1;
190 sdi->status = SR_ST_INACTIVE;
196 static int hw_cleanup(void)
203 static int hw_info_get(int info_id, const void **data,
204 const struct sr_dev_inst *sdi)
216 case SR_DI_NUM_PROBES:
217 *data = GINT_TO_POINTER(1);
219 case SR_DI_PROBE_NAMES:
229 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
232 struct dev_context *devc;
234 if (sdi->status != SR_ST_ACTIVE)
237 if (!(devc = sdi->priv)) {
238 sr_err("sdi->priv was NULL.");
243 case SR_HWCAP_LIMIT_MSEC:
244 /* TODO: not yet implemented */
245 if (*(const uint64_t *)value == 0) {
246 sr_err("LIMIT_MSEC can't be 0.");
249 devc->limit_msec = *(const uint64_t *)value;
250 sr_dbg("Setting time limit to %" PRIu64 "ms.",
253 case SR_HWCAP_LIMIT_SAMPLES:
254 devc->limit_samples = *(const uint64_t *)value;
255 sr_dbg("Setting sample limit to %" PRIu64 ".",
256 devc->limit_samples);
259 sr_err("Unknown capability: %d.", hwcap);
267 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
270 struct sr_datafeed_packet packet;
271 struct sr_datafeed_header header;
272 struct sr_datafeed_meta_analog meta;
273 struct dev_context *devc;
275 if (!(devc = sdi->priv)) {
276 sr_err("sdi->priv was NULL.");
280 sr_dbg("Starting acquisition.");
282 devc->cb_data = cb_data;
284 /* Send header packet to the session bus. */
285 sr_dbg("Sending SR_DF_HEADER.");
286 packet.type = SR_DF_HEADER;
287 packet.payload = (uint8_t *)&header;
288 header.feed_version = 1;
289 gettimeofday(&header.starttime, NULL);
290 sr_session_send(devc->cb_data, &packet);
292 /* Send metadata about the SR_DF_ANALOG packets to come. */
293 sr_dbg("Sending SR_DF_META_ANALOG.");
294 packet.type = SR_DF_META_ANALOG;
295 packet.payload = &meta;
297 sr_session_send(devc->cb_data, &packet);
299 /* Poll every 150ms, or whenever some data comes in. */
300 sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
306 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
309 struct sr_datafeed_packet packet;
310 struct dev_context *devc;
312 if (sdi->status != SR_ST_ACTIVE)
315 if (!(devc = sdi->priv)) {
316 sr_err("sdi->priv was NULL.");
320 sr_dbg("Stopping acquisition.");
322 sr_source_remove(devc->serial->fd);
323 hw_dev_close((struct sr_dev_inst *)sdi);
325 /* Send end packet to the session bus. */
326 sr_dbg("Sending SR_DF_END.");
327 packet.type = SR_DF_END;
328 sr_session_send(cb_data, &packet);
333 SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
334 .name = "colead-slm",
335 .longname = "Colead SLM",
338 .cleanup = hw_cleanup,
340 .dev_list = hw_dev_list,
341 .dev_clear = clear_instances,
342 .dev_open = hw_dev_open,
343 .dev_close = hw_dev_close,
344 .info_get = hw_info_get,
345 .dev_config_set = hw_dev_config_set,
346 .dev_acquisition_start = hw_dev_acquisition_start,
347 .dev_acquisition_stop = hw_dev_acquisition_stop,