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 SR_PRIV struct sr_dev_driver colead_slm_driver_info;
48 static struct sr_dev_driver *di = &colead_slm_driver_info;
50 /* Properly close and free all devices. */
51 static int clear_instances(void)
53 struct sr_dev_inst *sdi;
54 struct drv_context *drvc;
55 struct dev_context *devc;
58 if (!(drvc = di->priv))
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(struct sr_context *sr_ctx)
77 struct drv_context *drvc;
79 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
80 sr_err("Driver context malloc failed.");
84 drvc->sr_ctx = sr_ctx;
90 static GSList *hw_scan(GSList *options)
92 struct drv_context *drvc;
93 struct dev_context *devc;
94 struct sr_dev_inst *sdi;
95 struct sr_config *src;
96 struct sr_probe *probe;
98 const char *conn, *serialcomm;
102 drvc->instances = NULL;
104 conn = serialcomm = NULL;
105 for (l = options; l; l = l->next) {
111 case SR_HWOPT_SERIALCOMM:
112 serialcomm = src->value;
119 serialcomm = SERIALCOMM;
121 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
125 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
126 sr_dbg("failed to malloc devc");
130 if (!(devc->serial = sr_serial_dev_inst_new(conn, 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 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
165 sdi->status = SR_ST_ACTIVE;
170 static int hw_dev_close(struct sr_dev_inst *sdi)
172 struct dev_context *devc;
174 if (!(devc = sdi->priv)) {
175 sr_err("sdi->priv was NULL.");
179 if (devc->serial && devc->serial->fd != -1) {
180 serial_close(devc->serial);
181 sdi->status = SR_ST_INACTIVE;
187 static int hw_cleanup(void)
194 static int hw_info_get(int info_id, const void **data,
195 const struct sr_dev_inst *sdi)
213 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
216 struct dev_context *devc;
218 if (sdi->status != SR_ST_ACTIVE)
221 if (!(devc = sdi->priv)) {
222 sr_err("sdi->priv was NULL.");
227 case SR_HWCAP_LIMIT_MSEC:
228 /* TODO: not yet implemented */
229 if (*(const uint64_t *)value == 0) {
230 sr_err("LIMIT_MSEC can't be 0.");
233 devc->limit_msec = *(const uint64_t *)value;
234 sr_dbg("Setting time limit to %" PRIu64 "ms.",
237 case SR_HWCAP_LIMIT_SAMPLES:
238 devc->limit_samples = *(const uint64_t *)value;
239 sr_dbg("Setting sample limit to %" PRIu64 ".",
240 devc->limit_samples);
243 sr_err("Unknown capability: %d.", hwcap);
251 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
254 struct sr_datafeed_packet packet;
255 struct sr_datafeed_header header;
256 struct dev_context *devc;
258 if (!(devc = sdi->priv)) {
259 sr_err("sdi->priv was NULL.");
263 sr_dbg("Starting acquisition.");
265 devc->cb_data = cb_data;
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);
275 /* Poll every 150ms, or whenever some data comes in. */
276 sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
282 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
284 struct sr_datafeed_packet packet;
285 struct dev_context *devc;
287 if (sdi->status != SR_ST_ACTIVE)
290 if (!(devc = sdi->priv)) {
291 sr_err("sdi->priv was NULL.");
295 sr_dbg("Stopping acquisition.");
297 sr_source_remove(devc->serial->fd);
298 hw_dev_close((struct sr_dev_inst *)sdi);
300 /* Send end packet to the session bus. */
301 sr_dbg("Sending SR_DF_END.");
302 packet.type = SR_DF_END;
303 sr_session_send(cb_data, &packet);
308 SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
309 .name = "colead-slm",
310 .longname = "Colead SLM",
313 .cleanup = hw_cleanup,
315 .dev_list = hw_dev_list,
316 .dev_clear = clear_instances,
317 .dev_open = hw_dev_open,
318 .dev_close = hw_dev_close,
319 .info_get = hw_info_get,
320 .dev_config_set = hw_dev_config_set,
321 .dev_acquisition_start = hw_dev_acquisition_start,
322 .dev_acquisition_stop = hw_dev_acquisition_stop,