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 int32_t hwopts[] = {
38 static const int32_t hwcaps[] = {
39 SR_CONF_SOUNDLEVELMETER,
40 SR_CONF_LIMIT_SAMPLES,
45 SR_PRIV struct sr_dev_driver colead_slm_driver_info;
46 static struct sr_dev_driver *di = &colead_slm_driver_info;
48 /* Properly close and free all devices. */
49 static int clear_instances(void)
51 struct sr_dev_inst *sdi;
52 struct drv_context *drvc;
53 struct dev_context *devc;
54 struct sr_serial_dev_inst *serial;
57 if (!(drvc = di->priv))
60 for (l = drvc->instances; l; l = l->next) {
63 if (!(devc = sdi->priv))
66 sr_serial_dev_inst_free(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 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
80 static GSList *hw_scan(GSList *options)
82 struct drv_context *drvc;
83 struct dev_context *devc;
84 struct sr_dev_inst *sdi;
85 struct sr_config *src;
86 struct sr_probe *probe;
88 const char *conn, *serialcomm;
91 drvc->instances = NULL;
95 conn = serialcomm = NULL;
96 for (l = options; l; l = l->next) {
100 conn = g_variant_get_string(src->data, NULL);
102 case SR_CONF_SERIALCOMM:
103 serialcomm = g_variant_get_string(src->data, NULL);
110 serialcomm = SERIALCOMM;
112 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
116 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
117 sr_dbg("Device context malloc failed.");
121 if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm)))
124 sdi->inst_type = SR_INST_SERIAL;
127 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
129 sdi->probes = g_slist_append(sdi->probes, probe);
130 drvc->instances = g_slist_append(drvc->instances, sdi);
131 devices = g_slist_append(devices, sdi);
136 static GSList *hw_dev_list(void)
138 return ((struct drv_context *)(di->priv))->instances;
141 static int hw_dev_open(struct sr_dev_inst *sdi)
143 struct sr_serial_dev_inst *serial;
146 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
149 sdi->status = SR_ST_ACTIVE;
154 static int hw_dev_close(struct sr_dev_inst *sdi)
156 struct sr_serial_dev_inst *serial;
159 if (serial && serial->fd != -1) {
160 serial_close(serial);
161 sdi->status = SR_ST_INACTIVE;
167 static int hw_cleanup(void)
174 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
176 struct dev_context *devc;
178 if (sdi->status != SR_ST_ACTIVE)
181 if (!(devc = sdi->priv)) {
182 sr_err("sdi->priv was NULL.");
187 case SR_CONF_LIMIT_MSEC:
188 /* TODO: not yet implemented */
189 if (g_variant_get_uint64(data) == 0) {
190 sr_err("LIMIT_MSEC can't be 0.");
193 devc->limit_msec = g_variant_get_uint64(data);;
194 sr_dbg("Setting time limit to %" PRIu64 "ms.",
197 case SR_CONF_LIMIT_SAMPLES:
198 devc->limit_samples = g_variant_get_uint64(data);
199 sr_dbg("Setting sample limit to %" PRIu64 ".",
200 devc->limit_samples);
209 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
215 case SR_CONF_SCAN_OPTIONS:
216 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
217 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
219 case SR_CONF_DEVICE_OPTIONS:
220 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
221 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
230 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
233 struct dev_context *devc;
234 struct sr_serial_dev_inst *serial;
236 if (!(devc = sdi->priv)) {
237 sr_err("sdi->priv was NULL.");
241 devc->cb_data = cb_data;
243 /* Send header packet to the session bus. */
244 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
246 /* Poll every 150ms, or whenever some data comes in. */
248 sr_source_add(serial->fd, G_IO_IN, 150, colead_slm_receive_data,
254 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
256 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
257 sdi->conn, DRIVER_LOG_DOMAIN);
260 SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
261 .name = "colead-slm",
262 .longname = "Colead SLM",
265 .cleanup = hw_cleanup,
267 .dev_list = hw_dev_list,
268 .dev_clear = clear_instances,
270 .config_set = config_set,
271 .config_list = config_list,
272 .dev_open = hw_dev_open,
273 .dev_close = hw_dev_close,
274 .dev_acquisition_start = hw_dev_acquisition_start,
275 .dev_acquisition_stop = hw_dev_acquisition_stop,