]>
Commit | Line | Data |
---|---|---|
7ec5b549 UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
21 | #include "protocol.h" | |
22 | ||
2c2be401 | 23 | static const int32_t hwopts[] = { |
0cd8e231 UH |
24 | SR_CONF_CONN, |
25 | SR_CONF_SERIALCOMM, | |
0cd8e231 UH |
26 | }; |
27 | ||
2c2be401 | 28 | static const int32_t hwcaps[] = { |
0cd8e231 UH |
29 | SR_CONF_THERMOMETER, |
30 | SR_CONF_HYGROMETER, | |
31 | SR_CONF_LIMIT_SAMPLES, | |
32 | SR_CONF_LIMIT_MSEC, | |
33 | SR_CONF_CONTINUOUS, | |
0cd8e231 | 34 | }; |
7ec5b549 | 35 | |
6f3e5335 | 36 | SR_PRIV struct sr_dev_driver mic_98581_driver_info; |
0cd8e231 UH |
37 | SR_PRIV struct sr_dev_driver mic_98583_driver_info; |
38 | ||
39 | SR_PRIV const struct mic_dev_info mic_devs[] = { | |
40 | { | |
6f3e5335 UH |
41 | "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6, |
42 | packet_valid_temp, | |
43 | &mic_98581_driver_info, receive_data_MIC_98581, | |
44 | }, | |
45 | { | |
46 | "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10, | |
47 | packet_valid_temp_hum, | |
0cd8e231 UH |
48 | &mic_98583_driver_info, receive_data_MIC_98583, |
49 | }, | |
50 | }; | |
51 | ||
52 | static int clear_instances(int idx) | |
7ec5b549 UH |
53 | { |
54 | struct sr_dev_inst *sdi; | |
55 | struct drv_context *drvc; | |
56 | struct dev_context *devc; | |
886bd5e0 | 57 | struct sr_serial_dev_inst *serial; |
7ec5b549 | 58 | GSList *l; |
0cd8e231 UH |
59 | struct sr_dev_driver *di; |
60 | ||
61 | di = mic_devs[idx].di; | |
7ec5b549 UH |
62 | |
63 | if (!(drvc = di->priv)) | |
64 | return SR_OK; | |
65 | ||
66 | for (l = drvc->instances; l; l = l->next) { | |
67 | if (!(sdi = l->data)) | |
68 | continue; | |
69 | if (!(devc = sdi->priv)) | |
70 | continue; | |
886bd5e0 UH |
71 | serial = sdi->conn; |
72 | sr_serial_dev_inst_free(serial); | |
7ec5b549 UH |
73 | sr_dev_inst_free(sdi); |
74 | } | |
75 | ||
76 | g_slist_free(drvc->instances); | |
77 | drvc->instances = NULL; | |
78 | ||
79 | return SR_OK; | |
80 | } | |
81 | ||
0cd8e231 | 82 | static int hw_init(struct sr_context *sr_ctx, int idx) |
7ec5b549 | 83 | { |
0cd8e231 UH |
84 | sr_dbg("Selected '%s' subdriver.", mic_devs[idx].di->name); |
85 | ||
29a27196 | 86 | return std_hw_init(sr_ctx, mic_devs[idx].di, LOG_PREFIX); |
7ec5b549 UH |
87 | } |
88 | ||
0cd8e231 | 89 | static GSList *scan(const char *conn, const char *serialcomm, int idx) |
7ec5b549 | 90 | { |
0cd8e231 | 91 | struct sr_dev_inst *sdi; |
7ec5b549 | 92 | struct drv_context *drvc; |
0cd8e231 UH |
93 | struct dev_context *devc; |
94 | struct sr_probe *probe; | |
95 | struct sr_serial_dev_inst *serial; | |
7ec5b549 UH |
96 | GSList *devices; |
97 | ||
0cd8e231 UH |
98 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
99 | return NULL; | |
7ec5b549 | 100 | |
0cd8e231 UH |
101 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
102 | return NULL; | |
103 | ||
104 | drvc = mic_devs[idx].di->priv; | |
7ec5b549 | 105 | devices = NULL; |
0cd8e231 | 106 | serial_flush(serial); |
7ec5b549 | 107 | |
0cd8e231 UH |
108 | /* TODO: Query device type. */ |
109 | // ret = mic_cmd_get_device_info(serial); | |
110 | ||
111 | sr_info("Found device on port %s.", conn); | |
112 | ||
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, ""))) | |
116 | goto scan_cleanup; | |
117 | ||
118 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
119 | sr_err("Device context malloc failed."); | |
120 | goto scan_cleanup; | |
121 | } | |
122 | ||
886bd5e0 UH |
123 | sdi->inst_type = SR_INST_SERIAL; |
124 | sdi->conn = serial; | |
0cd8e231 UH |
125 | |
126 | sdi->priv = devc; | |
127 | sdi->driver = mic_devs[idx].di; | |
128 | ||
129 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "Temperature"))) | |
130 | goto scan_cleanup; | |
131 | sdi->probes = g_slist_append(sdi->probes, probe); | |
132 | ||
6f3e5335 UH |
133 | if (mic_devs[idx].has_humidity) { |
134 | if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity"))) | |
135 | goto scan_cleanup; | |
136 | sdi->probes = g_slist_append(sdi->probes, probe); | |
137 | } | |
0cd8e231 UH |
138 | |
139 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
140 | devices = g_slist_append(devices, sdi); | |
141 | ||
142 | scan_cleanup: | |
143 | serial_close(serial); | |
7ec5b549 UH |
144 | |
145 | return devices; | |
146 | } | |
147 | ||
0cd8e231 UH |
148 | static GSList *hw_scan(GSList *options, int idx) |
149 | { | |
150 | struct sr_config *src; | |
151 | GSList *l, *devices; | |
152 | const char *conn, *serialcomm; | |
153 | ||
154 | conn = serialcomm = NULL; | |
155 | for (l = options; l; l = l->next) { | |
156 | src = l->data; | |
157 | switch (src->key) { | |
158 | case SR_CONF_CONN: | |
2c2be401 | 159 | conn = g_variant_get_string(src->data, NULL); |
0cd8e231 UH |
160 | break; |
161 | case SR_CONF_SERIALCOMM: | |
06c45a66 | 162 | serialcomm = g_variant_get_string(src->data, NULL); |
0cd8e231 UH |
163 | break; |
164 | } | |
165 | } | |
166 | if (!conn) | |
167 | return NULL; | |
168 | ||
169 | if (serialcomm) { | |
170 | /* Use the provided comm specs. */ | |
171 | devices = scan(conn, serialcomm, idx); | |
172 | } else { | |
173 | /* Try the default. */ | |
174 | devices = scan(conn, mic_devs[idx].conn, idx); | |
175 | } | |
176 | ||
177 | return devices; | |
178 | } | |
179 | ||
180 | static GSList *hw_dev_list(int idx) | |
7ec5b549 | 181 | { |
0e94d524 | 182 | return ((struct drv_context *)(mic_devs[idx].di->priv))->instances; |
7ec5b549 UH |
183 | } |
184 | ||
185 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
186 | { | |
886bd5e0 | 187 | struct sr_serial_dev_inst *serial; |
7ec5b549 | 188 | |
886bd5e0 UH |
189 | serial = sdi->conn; |
190 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
0cd8e231 | 191 | return SR_ERR; |
7ec5b549 | 192 | |
0cd8e231 | 193 | sdi->status = SR_ST_ACTIVE; |
7ec5b549 UH |
194 | |
195 | return SR_OK; | |
196 | } | |
197 | ||
0cd8e231 | 198 | static int hw_dev_close(struct sr_dev_inst *sdi) |
7ec5b549 | 199 | { |
886bd5e0 | 200 | struct sr_serial_dev_inst *serial; |
0cd8e231 | 201 | |
886bd5e0 UH |
202 | serial = sdi->conn; |
203 | if (serial && serial->fd != -1) { | |
204 | serial_close(serial); | |
0cd8e231 UH |
205 | sdi->status = SR_ST_INACTIVE; |
206 | } | |
7ec5b549 UH |
207 | |
208 | return SR_OK; | |
209 | } | |
210 | ||
0cd8e231 | 211 | static int hw_cleanup(int idx) |
7ec5b549 | 212 | { |
0cd8e231 | 213 | clear_instances(idx); |
7ec5b549 UH |
214 | |
215 | return SR_OK; | |
216 | } | |
217 | ||
2c2be401 | 218 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
7ec5b549 | 219 | { |
0cd8e231 | 220 | struct dev_context *devc; |
7ec5b549 | 221 | |
0cd8e231 | 222 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 223 | return SR_ERR_DEV_CLOSED; |
7ec5b549 | 224 | |
0cd8e231 UH |
225 | devc = sdi->priv; |
226 | ||
7ec5b549 | 227 | switch (id) { |
0cd8e231 | 228 | case SR_CONF_LIMIT_SAMPLES: |
2c2be401 | 229 | devc->limit_samples = g_variant_get_uint64(data); |
0cd8e231 UH |
230 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
231 | devc->limit_samples); | |
232 | break; | |
233 | case SR_CONF_LIMIT_MSEC: | |
2c2be401 | 234 | devc->limit_msec = g_variant_get_uint64(data); |
0cd8e231 UH |
235 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
236 | devc->limit_msec); | |
237 | break; | |
7ec5b549 | 238 | default: |
bd6fbf62 | 239 | return SR_ERR_NA; |
7ec5b549 UH |
240 | } |
241 | ||
0cd8e231 | 242 | return SR_OK; |
7ec5b549 UH |
243 | } |
244 | ||
2c2be401 | 245 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
7ec5b549 UH |
246 | { |
247 | (void)sdi; | |
7ec5b549 UH |
248 | |
249 | switch (key) { | |
0cd8e231 | 250 | case SR_CONF_SCAN_OPTIONS: |
2c2be401 BV |
251 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
252 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0cd8e231 UH |
253 | break; |
254 | case SR_CONF_DEVICE_OPTIONS: | |
2c2be401 BV |
255 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
256 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
0cd8e231 | 257 | break; |
7ec5b549 | 258 | default: |
bd6fbf62 | 259 | return SR_ERR_NA; |
7ec5b549 UH |
260 | } |
261 | ||
262 | return SR_OK; | |
263 | } | |
264 | ||
265 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
0cd8e231 | 266 | void *cb_data, int idx) |
7ec5b549 | 267 | { |
0cd8e231 | 268 | struct dev_context *devc; |
886bd5e0 | 269 | struct sr_serial_dev_inst *serial; |
0cd8e231 | 270 | |
e73ffd42 BV |
271 | if (sdi->status != SR_ST_ACTIVE) |
272 | return SR_ERR_DEV_CLOSED; | |
0cd8e231 | 273 | |
e73ffd42 | 274 | devc = sdi->priv; |
0cd8e231 | 275 | devc->cb_data = cb_data; |
0cd8e231 UH |
276 | devc->num_samples = 0; |
277 | devc->starttime = g_get_monotonic_time(); | |
278 | ||
279 | /* Send header packet to the session bus. */ | |
29a27196 | 280 | std_session_send_df_header(cb_data, LOG_PREFIX); |
0cd8e231 UH |
281 | |
282 | /* Poll every 100ms, or whenever some data comes in. */ | |
886bd5e0 UH |
283 | serial = sdi->conn; |
284 | sr_source_add(serial->fd, G_IO_IN, 100, | |
0cd8e231 | 285 | mic_devs[idx].receive_data, (void *)sdi); |
7ec5b549 UH |
286 | |
287 | return SR_OK; | |
288 | } | |
289 | ||
290 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
291 | { | |
cd2f0fe2 | 292 | return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, |
29a27196 | 293 | sdi->conn, LOG_PREFIX); |
7ec5b549 UH |
294 | } |
295 | ||
0cd8e231 UH |
296 | /* Driver-specific API function wrappers */ |
297 | #define HW_INIT(X) \ | |
298 | static int hw_init_##X(struct sr_context *sr_ctx) { return hw_init(sr_ctx, X); } | |
299 | #define HW_CLEANUP(X) \ | |
300 | static int hw_cleanup_##X(void) { return hw_cleanup(X); } | |
301 | #define HW_SCAN(X) \ | |
302 | static GSList *hw_scan_##X(GSList *options) { return hw_scan(options, X); } | |
303 | #define HW_DEV_LIST(X) \ | |
304 | static GSList *hw_dev_list_##X(void) { return hw_dev_list(X); } | |
305 | #define CLEAR_INSTANCES(X) \ | |
306 | static int clear_instances_##X(void) { return clear_instances(X); } | |
307 | #define HW_DEV_ACQUISITION_START(X) \ | |
308 | static int hw_dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \ | |
309 | void *cb_data) { return hw_dev_acquisition_start(sdi, cb_data, X); } | |
310 | ||
311 | /* Driver structs and API function wrappers */ | |
312 | #define DRV(ID, ID_UPPER, NAME, LONGNAME) \ | |
313 | HW_INIT(ID_UPPER) \ | |
314 | HW_CLEANUP(ID_UPPER) \ | |
315 | HW_SCAN(ID_UPPER) \ | |
316 | HW_DEV_LIST(ID_UPPER) \ | |
317 | CLEAR_INSTANCES(ID_UPPER) \ | |
318 | HW_DEV_ACQUISITION_START(ID_UPPER) \ | |
319 | SR_PRIV struct sr_dev_driver ID##_driver_info = { \ | |
320 | .name = NAME, \ | |
321 | .longname = LONGNAME, \ | |
322 | .api_version = 1, \ | |
323 | .init = hw_init_##ID_UPPER, \ | |
324 | .cleanup = hw_cleanup_##ID_UPPER, \ | |
325 | .scan = hw_scan_##ID_UPPER, \ | |
326 | .dev_list = hw_dev_list_##ID_UPPER, \ | |
327 | .dev_clear = clear_instances_##ID_UPPER, \ | |
328 | .config_get = NULL, \ | |
329 | .config_set = config_set, \ | |
330 | .config_list = config_list, \ | |
331 | .dev_open = hw_dev_open, \ | |
332 | .dev_close = hw_dev_close, \ | |
333 | .dev_acquisition_start = hw_dev_acquisition_start_##ID_UPPER, \ | |
334 | .dev_acquisition_stop = hw_dev_acquisition_stop, \ | |
335 | .priv = NULL, \ | |
7ec5b549 | 336 | }; |
0cd8e231 | 337 | |
6f3e5335 | 338 | DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581") |
0cd8e231 | 339 | DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583") |