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(struct sr_context *sr_ctx)
82 struct drv_context *drvc;
84 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
85 sr_err("Driver context malloc failed.");
89 drvc->sr_ctx = sr_ctx;
95 static GSList *hw_scan(GSList *options)
97 struct drv_context *drvc;
98 struct dev_context *devc;
99 struct sr_dev_inst *sdi;
100 struct sr_hwopt *opt;
101 struct sr_probe *probe;
103 const char *conn, *serialcomm;
107 drvc->instances = NULL;
109 conn = serialcomm = NULL;
110 for (l = options; l; l = l->next) {
112 switch (opt->hwopt) {
116 case SR_HWOPT_SERIALCOMM:
117 serialcomm = opt->value;
124 serialcomm = SERIALCOMM;
126 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
130 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
131 sr_dbg("failed to malloc devc");
135 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
140 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
142 sdi->probes = g_slist_append(sdi->probes, probe);
143 drvc->instances = g_slist_append(drvc->instances, sdi);
144 devices = g_slist_append(devices, sdi);
149 static GSList *hw_dev_list(void)
151 struct drv_context *drvc;
155 return drvc->instances;
158 static int hw_dev_open(struct sr_dev_inst *sdi)
160 struct dev_context *devc;
162 if (!(devc = sdi->priv)) {
163 sr_err("sdi->priv was NULL.");
167 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
170 sdi->status = SR_ST_ACTIVE;
175 static int hw_dev_close(struct sr_dev_inst *sdi)
177 struct dev_context *devc;
179 if (!(devc = sdi->priv)) {
180 sr_err("sdi->priv was NULL.");
184 if (devc->serial && devc->serial->fd != -1) {
185 serial_close(devc->serial);
186 sdi->status = SR_ST_INACTIVE;
192 static int hw_cleanup(void)
199 static int hw_info_get(int info_id, const void **data,
200 const struct sr_dev_inst *sdi)
212 case SR_DI_NUM_PROBES:
213 *data = GINT_TO_POINTER(1);
215 case SR_DI_PROBE_NAMES:
225 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
228 struct dev_context *devc;
230 if (sdi->status != SR_ST_ACTIVE)
233 if (!(devc = sdi->priv)) {
234 sr_err("sdi->priv was NULL.");
239 case SR_HWCAP_LIMIT_MSEC:
240 /* TODO: not yet implemented */
241 if (*(const uint64_t *)value == 0) {
242 sr_err("LIMIT_MSEC can't be 0.");
245 devc->limit_msec = *(const uint64_t *)value;
246 sr_dbg("Setting time limit to %" PRIu64 "ms.",
249 case SR_HWCAP_LIMIT_SAMPLES:
250 devc->limit_samples = *(const uint64_t *)value;
251 sr_dbg("Setting sample limit to %" PRIu64 ".",
252 devc->limit_samples);
255 sr_err("Unknown capability: %d.", hwcap);
263 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
266 struct sr_datafeed_packet packet;
267 struct sr_datafeed_header header;
268 struct sr_datafeed_meta_analog meta;
269 struct dev_context *devc;
271 if (!(devc = sdi->priv)) {
272 sr_err("sdi->priv was NULL.");
276 sr_dbg("Starting acquisition.");
278 devc->cb_data = cb_data;
280 /* Send header packet to the session bus. */
281 sr_dbg("Sending SR_DF_HEADER.");
282 packet.type = SR_DF_HEADER;
283 packet.payload = (uint8_t *)&header;
284 header.feed_version = 1;
285 gettimeofday(&header.starttime, NULL);
286 sr_session_send(devc->cb_data, &packet);
288 /* Send metadata about the SR_DF_ANALOG packets to come. */
289 sr_dbg("Sending SR_DF_META_ANALOG.");
290 packet.type = SR_DF_META_ANALOG;
291 packet.payload = &meta;
293 sr_session_send(devc->cb_data, &packet);
295 /* Poll every 150ms, or whenever some data comes in. */
296 sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
302 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
304 struct sr_datafeed_packet packet;
305 struct dev_context *devc;
307 if (sdi->status != SR_ST_ACTIVE)
310 if (!(devc = sdi->priv)) {
311 sr_err("sdi->priv was NULL.");
315 sr_dbg("Stopping acquisition.");
317 sr_source_remove(devc->serial->fd);
318 hw_dev_close((struct sr_dev_inst *)sdi);
320 /* Send end packet to the session bus. */
321 sr_dbg("Sending SR_DF_END.");
322 packet.type = SR_DF_END;
323 sr_session_send(cb_data, &packet);
328 SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
329 .name = "colead-slm",
330 .longname = "Colead SLM",
333 .cleanup = hw_cleanup,
335 .dev_list = hw_dev_list,
336 .dev_clear = clear_instances,
337 .dev_open = hw_dev_open,
338 .dev_close = hw_dev_close,
339 .info_get = hw_info_get,
340 .dev_config_set = hw_dev_config_set,
341 .dev_acquisition_start = hw_dev_acquisition_start,
342 .dev_acquisition_stop = hw_dev_acquisition_stop,