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