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, see <http://www.gnu.org/licenses/>.
23 static const uint32_t scanopts[] = {
28 static const uint32_t drvopts_temp[] = {
32 static const uint32_t drvopts_temp_hum[] = {
37 static const uint32_t devopts[] = {
39 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
40 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
43 static struct sr_dev_driver mic_98581_driver_info;
44 static struct sr_dev_driver mic_98583_driver_info;
46 SR_PRIV const struct mic_dev_info mic_devs[] = {
48 "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
50 &mic_98581_driver_info, receive_data_MIC_98581,
53 "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
54 packet_valid_temp_hum,
55 &mic_98583_driver_info, receive_data_MIC_98583,
59 static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
61 struct sr_dev_inst *sdi;
62 struct dev_context *devc;
63 struct sr_serial_dev_inst *serial;
65 serial = sr_serial_dev_inst_new(conn, serialcomm);
67 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
72 /* TODO: Query device type. */
73 // ret = mic_cmd_get_device_info(serial);
75 sr_info("Found device on port %s.", conn);
77 /* TODO: Fill in version from protocol response. */
78 sdi = g_malloc0(sizeof(struct sr_dev_inst));
79 sdi->status = SR_ST_INACTIVE;
80 sdi->vendor = g_strdup(mic_devs[idx].vendor);
81 sdi->model = g_strdup(mic_devs[idx].device);
82 devc = g_malloc0(sizeof(struct dev_context));
83 sr_sw_limits_init(&devc->limits);
84 sdi->inst_type = SR_INST_SERIAL;
88 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temperature");
90 if (mic_devs[idx].has_humidity)
91 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity");
95 return std_scan_complete(mic_devs[idx].di, g_slist_append(NULL, sdi));
98 static GSList *scan(GSList *options, int idx)
100 struct sr_config *src;
102 const char *conn, *serialcomm;
104 conn = serialcomm = NULL;
105 for (l = options; l; l = l->next) {
109 conn = g_variant_get_string(src->data, NULL);
111 case SR_CONF_SERIALCOMM:
112 serialcomm = g_variant_get_string(src->data, NULL);
120 /* Use the provided comm specs. */
121 devices = mic_scan(conn, serialcomm, idx);
123 /* Try the default. */
124 devices = mic_scan(conn, mic_devs[idx].conn, idx);
130 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
131 const struct sr_channel_group *cg)
133 struct dev_context *devc;
139 return sr_sw_limits_config_set(&devc->limits, key, data);
142 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
143 const struct sr_channel_group *cg, int idx)
146 * We can't use the ternary operator here! The result would contain
147 * sizeof((cond) ? A : B) where A/B are arrays of different type/size.
148 * The ternary operator always returns the "common" type of A and B,
149 * which would be a pointer instead of either the A or B arrays.
150 * Thus, sizeof() would yield the size of a pointer, not the size
151 * of either the A or B array, which is not what we want.
153 if (mic_devs[idx].has_humidity)
154 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts_temp_hum, devopts);
156 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts_temp, devopts);
159 static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx)
161 struct dev_context *devc;
162 struct sr_serial_dev_inst *serial;
166 sr_sw_limits_acquisition_start(&devc->limits);
167 std_session_send_df_header(sdi);
169 /* Poll every 100ms, or whenever some data comes in. */
171 serial_source_add(sdi->session, serial, G_IO_IN, 100,
172 mic_devs[idx].receive_data, (void *)sdi);
177 /* Driver-specific API function wrappers */
179 static GSList *scan_##X(struct sr_dev_driver *di, GSList *options) { \
180 (void)di; return scan(options, X); }
181 #define HW_CONFIG_LIST(X) \
182 static int config_list_##X(uint32_t key, GVariant **data, \
183 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { \
184 return config_list(key, data, sdi, cg, X); }
185 #define HW_DEV_ACQUISITION_START(X) \
186 static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \
187 ) { return dev_acquisition_start(sdi, X); }
189 /* Driver structs and API function wrappers */
190 #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
192 HW_CONFIG_LIST(ID_UPPER) \
193 HW_DEV_ACQUISITION_START(ID_UPPER) \
194 static struct sr_dev_driver ID##_driver_info = { \
196 .longname = LONGNAME, \
199 .cleanup = std_cleanup, \
200 .scan = scan_##ID_UPPER, \
201 .dev_list = std_dev_list, \
202 .dev_clear = std_dev_clear, \
203 .config_get = NULL, \
204 .config_set = config_set, \
205 .config_list = config_list_##ID_UPPER, \
206 .dev_open = std_serial_dev_open, \
207 .dev_close = std_serial_dev_close, \
208 .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \
209 .dev_acquisition_stop = std_serial_dev_acquisition_stop, \
212 SR_REGISTER_DEV_DRIVER(ID##_driver_info)
214 DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
215 DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")