2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 static const int hwopts[] = {
29 static const int hwcaps[] = {
32 SR_CONF_LIMIT_SAMPLES,
38 SR_PRIV struct sr_dev_driver mic_98583_driver_info;
40 SR_PRIV const struct mic_dev_info mic_devs[] = {
42 "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE,
43 &mic_98583_driver_info, receive_data_MIC_98583,
47 static int clear_instances(int idx)
49 struct sr_dev_inst *sdi;
50 struct drv_context *drvc;
51 struct dev_context *devc;
53 struct sr_dev_driver *di;
55 di = mic_devs[idx].di;
57 if (!(drvc = di->priv))
60 for (l = drvc->instances; l; l = l->next) {
63 if (!(devc = sdi->priv))
65 sr_serial_dev_inst_free(devc->serial);
66 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, int idx)
77 sr_dbg("Selected '%s' subdriver.", mic_devs[idx].di->name);
79 return std_hw_init(sr_ctx, mic_devs[idx].di, DRIVER_LOG_DOMAIN);
82 static GSList *scan(const char *conn, const char *serialcomm, int idx)
84 struct sr_dev_inst *sdi;
85 struct drv_context *drvc;
86 struct dev_context *devc;
87 struct sr_probe *probe;
88 struct sr_serial_dev_inst *serial;
91 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
94 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
97 drvc = mic_devs[idx].di->priv;
101 /* TODO: Query device type. */
102 // ret = mic_cmd_get_device_info(serial);
104 sr_info("Found device on port %s.", conn);
106 /* TODO: Fill in version from protocol response. */
107 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, mic_devs[idx].vendor,
108 mic_devs[idx].device, "")))
111 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
112 sr_err("Device context malloc failed.");
116 devc->serial = serial;
119 sdi->driver = mic_devs[idx].di;
121 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "Temperature")))
123 sdi->probes = g_slist_append(sdi->probes, probe);
125 if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
127 sdi->probes = g_slist_append(sdi->probes, probe);
129 drvc->instances = g_slist_append(drvc->instances, sdi);
130 devices = g_slist_append(devices, sdi);
133 serial_close(serial);
138 static GSList *hw_scan(GSList *options, int idx)
140 struct sr_config *src;
142 const char *conn, *serialcomm;
144 conn = serialcomm = NULL;
145 for (l = options; l; l = l->next) {
151 case SR_CONF_SERIALCOMM:
152 serialcomm = src->value;
160 /* Use the provided comm specs. */
161 devices = scan(conn, serialcomm, idx);
163 /* Try the default. */
164 devices = scan(conn, mic_devs[idx].conn, idx);
170 static GSList *hw_dev_list(int idx)
172 struct drv_context *drvc;
174 drvc = mic_devs[idx].di->priv;
176 return drvc->instances;
179 static int hw_dev_open(struct sr_dev_inst *sdi)
181 struct dev_context *devc;
185 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
188 sdi->status = SR_ST_ACTIVE;
193 static int hw_dev_close(struct sr_dev_inst *sdi)
195 struct dev_context *devc;
199 if (devc->serial && devc->serial->fd != -1) {
200 serial_close(devc->serial);
201 sdi->status = SR_ST_INACTIVE;
207 static int hw_cleanup(int idx)
209 clear_instances(idx);
214 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
216 struct dev_context *devc;
218 if (sdi->status != SR_ST_ACTIVE)
224 case SR_CONF_LIMIT_SAMPLES:
225 devc->limit_samples = *(const uint64_t *)value;
226 sr_dbg("Setting sample limit to %" PRIu64 ".",
227 devc->limit_samples);
229 case SR_CONF_LIMIT_MSEC:
230 devc->limit_msec = *(const uint64_t *)value;
231 sr_dbg("Setting time limit to %" PRIu64 "ms.",
235 sr_err("Unknown config: %d.", id);
242 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
247 case SR_CONF_SCAN_OPTIONS:
250 case SR_CONF_DEVICE_OPTIONS:
260 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
261 void *cb_data, int idx)
263 struct sr_datafeed_packet packet;
264 struct sr_datafeed_header header;
265 struct dev_context *devc;
269 devc->cb_data = cb_data;
271 sr_dbg("Starting acquisition.");
273 devc->num_samples = 0;
274 devc->starttime = g_get_monotonic_time();
276 /* Send header packet to the session bus. */
277 sr_dbg("Sending SR_DF_HEADER.");
278 packet.type = SR_DF_HEADER;
279 packet.payload = (uint8_t *)&header;
280 header.feed_version = 1;
281 gettimeofday(&header.starttime, NULL);
282 sr_session_send(devc->cb_data, &packet);
284 /* Poll every 100ms, or whenever some data comes in. */
285 sr_source_add(devc->serial->fd, G_IO_IN, 100,
286 mic_devs[idx].receive_data, (void *)sdi);
291 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
293 struct sr_datafeed_packet packet;
294 struct dev_context *devc;
296 if (sdi->status != SR_ST_ACTIVE) {
297 sr_err("Device inactive, can't stop acquisition.");
303 sr_dbg("Stopping acquisition.");
305 sr_source_remove(devc->serial->fd);
306 hw_dev_close((struct sr_dev_inst *)sdi);
308 /* Send end packet to the session bus. */
309 sr_dbg("Sending SR_DF_END.");
310 packet.type = SR_DF_END;
311 sr_session_send(cb_data, &packet);
316 /* Driver-specific API function wrappers */
318 static int hw_init_##X(struct sr_context *sr_ctx) { return hw_init(sr_ctx, X); }
319 #define HW_CLEANUP(X) \
320 static int hw_cleanup_##X(void) { return hw_cleanup(X); }
322 static GSList *hw_scan_##X(GSList *options) { return hw_scan(options, X); }
323 #define HW_DEV_LIST(X) \
324 static GSList *hw_dev_list_##X(void) { return hw_dev_list(X); }
325 #define CLEAR_INSTANCES(X) \
326 static int clear_instances_##X(void) { return clear_instances(X); }
327 #define HW_DEV_ACQUISITION_START(X) \
328 static int hw_dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \
329 void *cb_data) { return hw_dev_acquisition_start(sdi, cb_data, X); }
331 /* Driver structs and API function wrappers */
332 #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
334 HW_CLEANUP(ID_UPPER) \
336 HW_DEV_LIST(ID_UPPER) \
337 CLEAR_INSTANCES(ID_UPPER) \
338 HW_DEV_ACQUISITION_START(ID_UPPER) \
339 SR_PRIV struct sr_dev_driver ID##_driver_info = { \
341 .longname = LONGNAME, \
343 .init = hw_init_##ID_UPPER, \
344 .cleanup = hw_cleanup_##ID_UPPER, \
345 .scan = hw_scan_##ID_UPPER, \
346 .dev_list = hw_dev_list_##ID_UPPER, \
347 .dev_clear = clear_instances_##ID_UPPER, \
348 .config_get = NULL, \
349 .config_set = config_set, \
350 .config_list = config_list, \
351 .dev_open = hw_dev_open, \
352 .dev_close = hw_dev_close, \
353 .dev_acquisition_start = hw_dev_acquisition_start_##ID_UPPER, \
354 .dev_acquisition_stop = hw_dev_acquisition_stop, \
358 DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")