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_98581_driver_info;
39 SR_PRIV struct sr_dev_driver mic_98583_driver_info;
41 SR_PRIV const struct mic_dev_info mic_devs[] = {
43 "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
45 &mic_98581_driver_info, receive_data_MIC_98581,
48 "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
49 packet_valid_temp_hum,
50 &mic_98583_driver_info, receive_data_MIC_98583,
54 static int clear_instances(int idx)
56 struct sr_dev_inst *sdi;
57 struct drv_context *drvc;
58 struct dev_context *devc;
60 struct sr_dev_driver *di;
62 di = mic_devs[idx].di;
64 if (!(drvc = di->priv))
67 for (l = drvc->instances; l; l = l->next) {
70 if (!(devc = sdi->priv))
72 sr_serial_dev_inst_free(devc->serial);
73 sr_dev_inst_free(sdi);
76 g_slist_free(drvc->instances);
77 drvc->instances = NULL;
82 static int hw_init(struct sr_context *sr_ctx, int idx)
84 sr_dbg("Selected '%s' subdriver.", mic_devs[idx].di->name);
86 return std_hw_init(sr_ctx, mic_devs[idx].di, DRIVER_LOG_DOMAIN);
89 static GSList *scan(const char *conn, const char *serialcomm, int idx)
91 struct sr_dev_inst *sdi;
92 struct drv_context *drvc;
93 struct dev_context *devc;
94 struct sr_probe *probe;
95 struct sr_serial_dev_inst *serial;
98 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
101 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
104 drvc = mic_devs[idx].di->priv;
106 serial_flush(serial);
108 /* TODO: Query device type. */
109 // ret = mic_cmd_get_device_info(serial);
111 sr_info("Found device on port %s.", conn);
113 /* TODO: Fill in version from protocol response. */
114 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, mic_devs[idx].vendor,
115 mic_devs[idx].device, "")))
118 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
119 sr_err("Device context malloc failed.");
123 devc->serial = serial;
126 sdi->driver = mic_devs[idx].di;
128 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "Temperature")))
130 sdi->probes = g_slist_append(sdi->probes, probe);
132 if (mic_devs[idx].has_humidity) {
133 if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
135 sdi->probes = g_slist_append(sdi->probes, probe);
138 drvc->instances = g_slist_append(drvc->instances, sdi);
139 devices = g_slist_append(devices, sdi);
142 serial_close(serial);
147 static GSList *hw_scan(GSList *options, int idx)
149 struct sr_config *src;
151 const char *conn, *serialcomm;
153 conn = serialcomm = NULL;
154 for (l = options; l; l = l->next) {
160 case SR_CONF_SERIALCOMM:
161 serialcomm = src->value;
169 /* Use the provided comm specs. */
170 devices = scan(conn, serialcomm, idx);
172 /* Try the default. */
173 devices = scan(conn, mic_devs[idx].conn, idx);
179 static GSList *hw_dev_list(int idx)
181 return ((struct drv_context *)(mic_devs[idx].di->priv))->instances;
184 static int hw_dev_open(struct sr_dev_inst *sdi)
186 struct dev_context *devc;
190 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
193 sdi->status = SR_ST_ACTIVE;
198 static int hw_dev_close(struct sr_dev_inst *sdi)
200 struct dev_context *devc;
204 if (devc->serial && devc->serial->fd != -1) {
205 serial_close(devc->serial);
206 sdi->status = SR_ST_INACTIVE;
212 static int hw_cleanup(int idx)
214 clear_instances(idx);
219 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
221 struct dev_context *devc;
223 if (sdi->status != SR_ST_ACTIVE)
229 case SR_CONF_LIMIT_SAMPLES:
230 devc->limit_samples = *(const uint64_t *)value;
231 sr_dbg("Setting sample limit to %" PRIu64 ".",
232 devc->limit_samples);
234 case SR_CONF_LIMIT_MSEC:
235 devc->limit_msec = *(const uint64_t *)value;
236 sr_dbg("Setting time limit to %" PRIu64 "ms.",
240 sr_err("Unknown config: %d.", id);
247 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
252 case SR_CONF_SCAN_OPTIONS:
255 case SR_CONF_DEVICE_OPTIONS:
265 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
266 void *cb_data, int idx)
268 struct dev_context *devc;
272 devc->cb_data = cb_data;
274 devc->num_samples = 0;
275 devc->starttime = g_get_monotonic_time();
277 /* Send header packet to the session bus. */
278 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
280 /* Poll every 100ms, or whenever some data comes in. */
281 sr_source_add(devc->serial->fd, G_IO_IN, 100,
282 mic_devs[idx].receive_data, (void *)sdi);
287 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
289 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
290 ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN);
293 /* Driver-specific API function wrappers */
295 static int hw_init_##X(struct sr_context *sr_ctx) { return hw_init(sr_ctx, X); }
296 #define HW_CLEANUP(X) \
297 static int hw_cleanup_##X(void) { return hw_cleanup(X); }
299 static GSList *hw_scan_##X(GSList *options) { return hw_scan(options, X); }
300 #define HW_DEV_LIST(X) \
301 static GSList *hw_dev_list_##X(void) { return hw_dev_list(X); }
302 #define CLEAR_INSTANCES(X) \
303 static int clear_instances_##X(void) { return clear_instances(X); }
304 #define HW_DEV_ACQUISITION_START(X) \
305 static int hw_dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \
306 void *cb_data) { return hw_dev_acquisition_start(sdi, cb_data, X); }
308 /* Driver structs and API function wrappers */
309 #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
311 HW_CLEANUP(ID_UPPER) \
313 HW_DEV_LIST(ID_UPPER) \
314 CLEAR_INSTANCES(ID_UPPER) \
315 HW_DEV_ACQUISITION_START(ID_UPPER) \
316 SR_PRIV struct sr_dev_driver ID##_driver_info = { \
318 .longname = LONGNAME, \
320 .init = hw_init_##ID_UPPER, \
321 .cleanup = hw_cleanup_##ID_UPPER, \
322 .scan = hw_scan_##ID_UPPER, \
323 .dev_list = hw_dev_list_##ID_UPPER, \
324 .dev_clear = clear_instances_##ID_UPPER, \
325 .config_get = NULL, \
326 .config_set = config_set, \
327 .config_list = config_list, \
328 .dev_open = hw_dev_open, \
329 .dev_close = hw_dev_close, \
330 .dev_acquisition_start = hw_dev_acquisition_start_##ID_UPPER, \
331 .dev_acquisition_stop = hw_dev_acquisition_stop, \
335 DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
336 DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")