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