]>
Commit | Line | Data |
---|---|---|
20cbc785 AG |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20cbc785 AG |
20 | #include "protocol.h" |
21 | ||
510b3e69 | 22 | static const int32_t hwopts[] = { |
601fb67c AG |
23 | SR_CONF_CONN, |
24 | SR_CONF_SERIALCOMM, | |
601fb67c AG |
25 | }; |
26 | ||
510b3e69 | 27 | static const int32_t hwcaps[] = { |
601fb67c AG |
28 | SR_CONF_MULTIMETER, |
29 | SR_CONF_LIMIT_SAMPLES, | |
30 | SR_CONF_CONTINUOUS, | |
31 | SR_CONF_LIMIT_MSEC, | |
601fb67c AG |
32 | }; |
33 | ||
c5d6f5cc UH |
34 | SR_PRIV struct sr_dev_driver brymen_bm857_driver_info; |
35 | static struct sr_dev_driver *di = &brymen_bm857_driver_info; | |
20cbc785 | 36 | |
601fb67c AG |
37 | static int hw_init(struct sr_context *sr_ctx) |
38 | { | |
943e94f5 | 39 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); |
601fb67c AG |
40 | } |
41 | ||
42 | static void free_instance(void *inst) | |
43 | { | |
44 | struct sr_dev_inst *sdi; | |
45 | struct dev_context *devc; | |
c5d6f5cc | 46 | |
601fb67c AG |
47 | if (!(sdi = inst)) |
48 | return; | |
49 | if (!(devc = sdi->priv)) | |
50 | return; | |
51 | sr_serial_dev_inst_free(devc->serial); | |
52 | sr_dev_inst_free(sdi); | |
53 | } | |
54 | ||
20cbc785 AG |
55 | /* Properly close and free all devices. */ |
56 | static int clear_instances(void) | |
57 | { | |
20cbc785 | 58 | struct drv_context *drvc; |
20cbc785 AG |
59 | |
60 | if (!(drvc = di->priv)) | |
61 | return SR_OK; | |
62 | ||
601fb67c | 63 | g_slist_free_full(drvc->instances, free_instance); |
20cbc785 AG |
64 | drvc->instances = NULL; |
65 | ||
66 | return SR_OK; | |
67 | } | |
68 | ||
601fb67c | 69 | static GSList *brymen_scan(const char *conn, const char *serialcomm) |
20cbc785 | 70 | { |
601fb67c AG |
71 | struct sr_dev_inst *sdi; |
72 | struct dev_context *devc; | |
20cbc785 | 73 | struct drv_context *drvc; |
601fb67c AG |
74 | struct sr_probe *probe; |
75 | struct sr_serial_dev_inst *serial; | |
76 | GSList *devices; | |
77 | int ret; | |
c5d6f5cc UH |
78 | uint8_t buf[128]; |
79 | size_t len; | |
20cbc785 | 80 | |
601fb67c AG |
81 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
82 | return NULL; | |
83 | ||
c5d6f5cc | 84 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
601fb67c AG |
85 | return NULL; |
86 | ||
87 | sr_info("Probing port %s.", conn); | |
88 | ||
89 | devices = NULL; | |
90 | ||
91 | /* Request reading */ | |
c5d6f5cc UH |
92 | if ((ret = brymen_packet_request(serial)) < 0) { |
93 | sr_err("Unable to send command: %d.", ret); | |
601fb67c | 94 | goto scan_cleanup; |
20cbc785 AG |
95 | } |
96 | ||
c5d6f5cc | 97 | len = 128; |
601fb67c AG |
98 | ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length, |
99 | brymen_packet_is_valid, 1000, 9600); | |
100 | if (ret != SR_OK) | |
101 | goto scan_cleanup; | |
20cbc785 | 102 | |
601fb67c AG |
103 | sr_info("Found device on port %s.", conn); |
104 | ||
105 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", ""))) | |
106 | goto scan_cleanup; | |
107 | ||
108 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
109 | sr_err("Device context malloc failed."); | |
110 | goto scan_cleanup; | |
111 | } | |
112 | ||
113 | devc->serial = serial; | |
114 | drvc = di->priv; | |
115 | sdi->priv = devc; | |
116 | sdi->driver = di; | |
117 | ||
118 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
119 | goto scan_cleanup; | |
120 | ||
121 | sdi->probes = g_slist_append(sdi->probes, probe); | |
122 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
123 | devices = g_slist_append(devices, sdi); | |
124 | ||
601fb67c AG |
125 | scan_cleanup: |
126 | serial_close(serial); | |
127 | ||
128 | return devices; | |
20cbc785 AG |
129 | } |
130 | ||
131 | static GSList *hw_scan(GSList *options) | |
132 | { | |
133 | struct drv_context *drvc; | |
601fb67c AG |
134 | struct sr_config *src; |
135 | GSList *devices, *l; | |
136 | const char *conn, *serialcomm; | |
20cbc785 AG |
137 | |
138 | (void)options; | |
139 | ||
140 | devices = NULL; | |
141 | drvc = di->priv; | |
142 | drvc->instances = NULL; | |
143 | ||
601fb67c AG |
144 | conn = serialcomm = NULL; |
145 | for (l = options; l; l = l->next) { | |
146 | src = l->data; | |
147 | switch (src->key) { | |
c5d6f5cc | 148 | case SR_CONF_CONN: |
510b3e69 | 149 | conn = g_variant_get_string(src->data, NULL); |
c5d6f5cc UH |
150 | break; |
151 | case SR_CONF_SERIALCOMM: | |
510b3e69 | 152 | serialcomm = g_variant_get_string(src->data, NULL); |
c5d6f5cc | 153 | break; |
601fb67c AG |
154 | } |
155 | } | |
156 | if (!conn) { | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | if (serialcomm) { | |
161 | /* Use the provided comm specs. */ | |
162 | devices = brymen_scan(conn, serialcomm); | |
163 | } else { | |
c5d6f5cc | 164 | /* But 9600/8n1 should work all of the time. */ |
601fb67c AG |
165 | devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1"); |
166 | } | |
20cbc785 AG |
167 | |
168 | return devices; | |
169 | } | |
170 | ||
171 | static GSList *hw_dev_list(void) | |
172 | { | |
0e94d524 | 173 | return ((struct drv_context *)(di->priv))->instances; |
20cbc785 AG |
174 | } |
175 | ||
176 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
177 | { | |
601fb67c AG |
178 | struct dev_context *devc; |
179 | ||
180 | if (!(devc = sdi->priv)) { | |
181 | sr_err("sdi->priv was NULL."); | |
182 | return SR_ERR_BUG; | |
183 | } | |
184 | ||
185 | if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
186 | return SR_ERR; | |
187 | ||
188 | sdi->status = SR_ST_ACTIVE; | |
20cbc785 AG |
189 | |
190 | return SR_OK; | |
191 | } | |
192 | ||
193 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
194 | { | |
601fb67c AG |
195 | struct dev_context *devc; |
196 | ||
961009b0 | 197 | devc = sdi->priv; |
601fb67c AG |
198 | |
199 | if (devc->serial && devc->serial->fd != -1) { | |
200 | serial_close(devc->serial); | |
201 | sdi->status = SR_ST_INACTIVE; | |
202 | } | |
20cbc785 AG |
203 | |
204 | return SR_OK; | |
205 | } | |
206 | ||
207 | static int hw_cleanup(void) | |
208 | { | |
209 | clear_instances(); | |
210 | ||
20cbc785 AG |
211 | return SR_OK; |
212 | } | |
213 | ||
510b3e69 | 214 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
20cbc785 | 215 | { |
601fb67c | 216 | struct dev_context *devc; |
20cbc785 AG |
217 | int ret; |
218 | ||
e73ffd42 BV |
219 | if (sdi->status != SR_ST_ACTIVE) |
220 | return SR_ERR_DEV_CLOSED; | |
20cbc785 | 221 | |
601fb67c AG |
222 | if (!(devc = sdi->priv)) { |
223 | sr_err("sdi->priv was NULL."); | |
224 | return SR_ERR_BUG; | |
225 | } | |
226 | ||
20cbc785 | 227 | ret = SR_OK; |
601fb67c | 228 | switch (id) { |
c5d6f5cc | 229 | case SR_CONF_LIMIT_SAMPLES: |
510b3e69 | 230 | devc->limit_samples = g_variant_get_uint64(data); |
601fb67c | 231 | break; |
c5d6f5cc | 232 | case SR_CONF_LIMIT_MSEC: |
510b3e69 | 233 | devc->limit_msec = g_variant_get_uint64(data); |
601fb67c | 234 | break; |
20cbc785 | 235 | default: |
bd6fbf62 | 236 | ret = SR_ERR_NA; |
20cbc785 AG |
237 | } |
238 | ||
239 | return ret; | |
240 | } | |
241 | ||
510b3e69 BV |
242 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
243 | { | |
244 | (void)sdi; | |
245 | ||
246 | switch (key) { | |
247 | case SR_CONF_SCAN_OPTIONS: | |
248 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
249 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
250 | break; | |
251 | case SR_CONF_DEVICE_OPTIONS: | |
252 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
253 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
254 | break; | |
255 | default: | |
bd6fbf62 | 256 | return SR_ERR_NA; |
510b3e69 BV |
257 | } |
258 | ||
259 | return SR_OK; | |
260 | } | |
261 | ||
20cbc785 AG |
262 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
263 | void *cb_data) | |
264 | { | |
601fb67c AG |
265 | struct dev_context *devc; |
266 | ||
e73ffd42 BV |
267 | if (sdi->status != SR_ST_ACTIVE) |
268 | return SR_ERR_DEV_CLOSED; | |
269 | ||
601fb67c AG |
270 | if (!(devc = sdi->priv)) { |
271 | sr_err("sdi->priv was NULL."); | |
272 | return SR_ERR_BUG; | |
273 | } | |
274 | ||
601fb67c AG |
275 | devc->cb_data = cb_data; |
276 | ||
277 | /* | |
278 | * Reset the number of samples to take. If we've already collected our | |
279 | * quota, but we start a new session, and don't reset this, we'll just | |
280 | * quit without acquiring any new samples. | |
281 | */ | |
282 | devc->num_samples = 0; | |
283 | devc->starttime = g_get_monotonic_time(); | |
284 | ||
285 | /* Send header packet to the session bus. */ | |
4afdfd46 | 286 | std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); |
601fb67c AG |
287 | |
288 | /* Poll every 50ms, or whenever some data comes in. */ | |
289 | sr_source_add(devc->serial->fd, G_IO_IN, 50, | |
290 | brymen_dmm_receive_data, (void *)sdi); | |
20cbc785 AG |
291 | |
292 | return SR_OK; | |
293 | } | |
294 | ||
601fb67c | 295 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
20cbc785 | 296 | { |
cd2f0fe2 UH |
297 | return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close, |
298 | ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN); | |
20cbc785 AG |
299 | } |
300 | ||
c5d6f5cc UH |
301 | SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = { |
302 | .name = "brymen-bm857", | |
303 | .longname = "Brymen BM857", | |
20cbc785 AG |
304 | .api_version = 1, |
305 | .init = hw_init, | |
306 | .cleanup = hw_cleanup, | |
307 | .scan = hw_scan, | |
308 | .dev_list = hw_dev_list, | |
309 | .dev_clear = clear_instances, | |
6fab7b8f UH |
310 | .config_get = NULL, |
311 | .config_set = config_set, | |
312 | .config_list = config_list, | |
20cbc785 AG |
313 | .dev_open = hw_dev_open, |
314 | .dev_close = hw_dev_close, | |
20cbc785 AG |
315 | .dev_acquisition_start = hw_dev_acquisition_start, |
316 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
317 | .priv = NULL, | |
318 | }; |